Mbed OS Reference
Loading...
Searching...
No Matches
nist_kw.h
Go to the documentation of this file.
1/**
2 * \file nist_kw.h
3 *
4 * \brief This file provides an API for key wrapping (KW) and key wrapping with
5 * padding (KWP) as defined in NIST SP 800-38F.
6 * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-38F.pdf
7 *
8 * Key wrapping specifies a deterministic authenticated-encryption mode
9 * of operation, according to <em>NIST SP 800-38F: Recommendation for
10 * Block Cipher Modes of Operation: Methods for Key Wrapping</em>. Its
11 * purpose is to protect cryptographic keys.
12 *
13 * Its equivalent is RFC 3394 for KW, and RFC 5649 for KWP.
14 * https://tools.ietf.org/html/rfc3394
15 * https://tools.ietf.org/html/rfc5649
16 *
17 */
18/*
19 * Copyright The Mbed TLS Contributors
20 * SPDX-License-Identifier: Apache-2.0
21 *
22 * Licensed under the Apache License, Version 2.0 (the "License"); you may
23 * not use this file except in compliance with the License.
24 * You may obtain a copy of the License at
25 *
26 * http://www.apache.org/licenses/LICENSE-2.0
27 *
28 * Unless required by applicable law or agreed to in writing, software
29 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
30 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
33 */
34
35#ifndef MBEDTLS_NIST_KW_H
36#define MBEDTLS_NIST_KW_H
37
38#if !defined(MBEDTLS_CONFIG_FILE)
39#include "mbedtls/config.h"
40#else
41#include MBEDTLS_CONFIG_FILE
42#endif
43
44#include "mbedtls/cipher.h"
45
46/**
47 * \addtogroup mbedtls
48 * \{
49 * \defgroup mbedtls_nist_kw_module NIST Key Wrapping
50 * \{
51 */
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57typedef enum
58{
59 MBEDTLS_KW_MODE_KW = 0,
60 MBEDTLS_KW_MODE_KWP = 1
61} mbedtls_nist_kw_mode_t;
62
63#if !defined(MBEDTLS_NIST_KW_ALT)
64// Regular implementation
65//
66
67/**
68 * \brief The key wrapping context-type definition. The key wrapping context is passed
69 * to the APIs called.
70 *
71 * \note The definition of this type may change in future library versions.
72 * Don't make any assumptions on this context!
73 */
74typedef struct {
75 mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
77
78#else /* MBEDTLS_NIST_key wrapping_ALT */
79#include "nist_kw_alt.h"
80#endif /* MBEDTLS_NIST_KW_ALT */
81
82/**
83 * \brief This function initializes the specified key wrapping context
84 * to make references valid and prepare the context
85 * for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().
86 *
87 * \param ctx The key wrapping context to initialize.
88 *
89 */
91
92/**
93 * \brief This function initializes the key wrapping context set in the
94 * \p ctx parameter and sets the encryption key.
95 *
96 * \param ctx The key wrapping context.
97 * \param cipher The 128-bit block cipher to use. Only AES is supported.
98 * \param key The Key Encryption Key (KEK).
99 * \param keybits The KEK size in bits. This must be acceptable by the cipher.
100 * \param is_wrap Specify whether the operation within the context is wrapping or unwrapping
101 *
102 * \return \c 0 on success.
103 * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.
104 * \return \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers
105 * which are not supported.
106 * \return cipher-specific error code on failure of the underlying cipher.
107 */
109 mbedtls_cipher_id_t cipher,
110 const unsigned char *key,
111 unsigned int keybits,
112 const int is_wrap );
113
114/**
115 * \brief This function releases and clears the specified key wrapping context
116 * and underlying cipher sub-context.
117 *
118 * \param ctx The key wrapping context to clear.
119 */
121
122/**
123 * \brief This function encrypts a buffer using key wrapping.
124 *
125 * \param ctx The key wrapping context to use for encryption.
126 * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
127 * \param input The buffer holding the input data.
128 * \param in_len The length of the input data in Bytes.
129 * The input uses units of 8 Bytes called semiblocks.
130 * <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>
131 * <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>
132 * \param[out] output The buffer holding the output data.
133 * <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>
134 * <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of
135 * 8 bytes for KWP (15 bytes at most).</li></ul>
136 * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
137 * \param[in] out_size The capacity of the output buffer.
138 *
139 * \return \c 0 on success.
140 * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
141 * \return cipher-specific error code on failure of the underlying cipher.
142 */
143int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
144 const unsigned char *input, size_t in_len,
145 unsigned char *output, size_t* out_len, size_t out_size );
146
147/**
148 * \brief This function decrypts a buffer using key wrapping.
149 *
150 * \param ctx The key wrapping context to use for decryption.
151 * \param mode The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
152 * \param input The buffer holding the input data.
153 * \param in_len The length of the input data in Bytes.
154 * The input uses units of 8 Bytes called semiblocks.
155 * The input must be a multiple of semiblocks.
156 * <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>
157 * <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>
158 * \param[out] output The buffer holding the output data.
159 * The output buffer's minimal length is 8 bytes shorter than \p in_len.
160 * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
161 * For KWP mode, the length could be up to 15 bytes shorter than \p in_len,
162 * depending on how much padding was added to the data.
163 * \param[in] out_size The capacity of the output buffer.
164 *
165 * \return \c 0 on success.
166 * \return \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
167 * \return \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.
168 * \return cipher-specific error code on failure of the underlying cipher.
169 */
170int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
171 const unsigned char *input, size_t in_len,
172 unsigned char *output, size_t* out_len, size_t out_size);
173
174
175#if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
176/**
177 * \brief The key wrapping checkup routine.
178 *
179 * \return \c 0 on success.
180 * \return \c 1 on failure.
181 */
182int mbedtls_nist_kw_self_test( int verbose );
183#endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
184
185#ifdef __cplusplus
186}
187#endif
188
189/// \}
190/// \}
191
192#endif /* MBEDTLS_NIST_KW_H */
This file contains an abstraction interface for use with the cipher primitives provided by the librar...
Configuration options (set of defines)
mbedtls_cipher_id_t
Supported cipher types.
Definition: cipher.h:89
int mbedtls_nist_kw_setkey(mbedtls_nist_kw_context *ctx, mbedtls_cipher_id_t cipher, const unsigned char *key, unsigned int keybits, const int is_wrap)
This function initializes the key wrapping context set in the ctx parameter and sets the encryption k...
int mbedtls_nist_kw_wrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode, const unsigned char *input, size_t in_len, unsigned char *output, size_t *out_len, size_t out_size)
This function encrypts a buffer using key wrapping.
int mbedtls_nist_kw_unwrap(mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode, const unsigned char *input, size_t in_len, unsigned char *output, size_t *out_len, size_t out_size)
This function decrypts a buffer using key wrapping.
void mbedtls_nist_kw_free(mbedtls_nist_kw_context *ctx)
This function releases and clears the specified key wrapping context and underlying cipher sub-contex...
void mbedtls_nist_kw_init(mbedtls_nist_kw_context *ctx)
This function initializes the specified key wrapping context to make references valid and prepare the...
Generic cipher context.
Definition: cipher.h:318
The key wrapping context-type definition.
Definition: nist_kw.h:74
mbedtls_cipher_context_t cipher_ctx
Definition: nist_kw.h:75