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 #include "mbedtls/private_access.h"
38 
39 #include "mbedtls/build_info.h"
40 
41 #include "mbedtls/cipher.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 typedef enum
48 {
49     MBEDTLS_KW_MODE_KW = 0,
50     MBEDTLS_KW_MODE_KWP = 1
51 } mbedtls_nist_kw_mode_t;
52 
53 #if !defined(MBEDTLS_NIST_KW_ALT)
54 // Regular implementation
55 //
56 
57 /**
58  * \brief    The key wrapping context-type definition. The key wrapping context is passed
59  *           to the APIs called.
60  *
61  * \note     The definition of this type may change in future library versions.
62  *           Don't make any assumptions on this context!
63  */
64 typedef struct {
65     mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx);    /*!< The cipher context used. */
66 } mbedtls_nist_kw_context;
67 
68 #else  /* MBEDTLS_NIST_key wrapping_ALT */
69 #include "nist_kw_alt.h"
70 #endif /* MBEDTLS_NIST_KW_ALT */
71 
72 /**
73  * \brief           This function initializes the specified key wrapping context
74  *                  to make references valid and prepare the context
75  *                  for mbedtls_nist_kw_setkey() or mbedtls_nist_kw_free().
76  *
77  * \param ctx       The key wrapping context to initialize.
78  *
79  */
80 void mbedtls_nist_kw_init( mbedtls_nist_kw_context *ctx );
81 
82 /**
83  * \brief           This function initializes the key wrapping context set in the
84  *                  \p ctx parameter and sets the encryption key.
85  *
86  * \param ctx       The key wrapping context.
87  * \param cipher    The 128-bit block cipher to use. Only AES is supported.
88  * \param key       The Key Encryption Key (KEK).
89  * \param keybits   The KEK size in bits. This must be acceptable by the cipher.
90  * \param is_wrap   Specify whether the operation within the context is wrapping or unwrapping
91  *
92  * \return          \c 0 on success.
93  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for any invalid input.
94  * \return          \c MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE for 128-bit block ciphers
95  *                  which are not supported.
96  * \return          cipher-specific error code on failure of the underlying cipher.
97  */
98 int mbedtls_nist_kw_setkey( mbedtls_nist_kw_context *ctx,
99                             mbedtls_cipher_id_t cipher,
100                             const unsigned char *key,
101                             unsigned int keybits,
102                             const int is_wrap );
103 
104 /**
105  * \brief   This function releases and clears the specified key wrapping context
106  *          and underlying cipher sub-context.
107  *
108  * \param ctx       The key wrapping context to clear.
109  */
110 void mbedtls_nist_kw_free( mbedtls_nist_kw_context *ctx );
111 
112 /**
113  * \brief           This function encrypts a buffer using key wrapping.
114  *
115  * \param ctx       The key wrapping context to use for encryption.
116  * \param mode      The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
117  * \param input     The buffer holding the input data.
118  * \param in_len    The length of the input data in Bytes.
119  *                  The input uses units of 8 Bytes called semiblocks.
120  *                  <ul><li>For KW mode: a multiple of 8 bytes between 16 and 2^57-8 inclusive. </li>
121  *                  <li>For KWP mode: any length between 1 and 2^32-1 inclusive.</li></ul>
122  * \param[out] output    The buffer holding the output data.
123  *                  <ul><li>For KW mode: Must be at least 8 bytes larger than \p in_len.</li>
124  *                  <li>For KWP mode: Must be at least 8 bytes larger rounded up to a multiple of
125  *                  8 bytes for KWP (15 bytes at most).</li></ul>
126  * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
127  * \param[in] out_size The capacity of the output buffer.
128  *
129  * \return          \c 0 on success.
130  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
131  * \return          cipher-specific error code on failure of the underlying cipher.
132  */
133 int mbedtls_nist_kw_wrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
134                           const unsigned char *input, size_t in_len,
135                           unsigned char *output, size_t* out_len, size_t out_size );
136 
137 /**
138  * \brief           This function decrypts a buffer using key wrapping.
139  *
140  * \param ctx       The key wrapping context to use for decryption.
141  * \param mode      The key wrapping mode to use (MBEDTLS_KW_MODE_KW or MBEDTLS_KW_MODE_KWP)
142  * \param input     The buffer holding the input data.
143  * \param in_len    The length of the input data in Bytes.
144  *                  The input uses units of 8 Bytes called semiblocks.
145  *                  The input must be a multiple of semiblocks.
146  *                  <ul><li>For KW mode: a multiple of 8 bytes between 24 and 2^57 inclusive. </li>
147  *                  <li>For KWP mode: a multiple of 8 bytes between 16 and 2^32 inclusive.</li></ul>
148  * \param[out] output    The buffer holding the output data.
149  *                  The output buffer's minimal length is 8 bytes shorter than \p in_len.
150  * \param[out] out_len The number of bytes written to the output buffer. \c 0 on failure.
151  *                  For KWP mode, the length could be up to 15 bytes shorter than \p in_len,
152  *                  depending on how much padding was added to the data.
153  * \param[in] out_size The capacity of the output buffer.
154  *
155  * \return          \c 0 on success.
156  * \return          \c MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA for invalid input length.
157  * \return          \c MBEDTLS_ERR_CIPHER_AUTH_FAILED for verification failure of the ciphertext.
158  * \return          cipher-specific error code on failure of the underlying cipher.
159  */
160 int mbedtls_nist_kw_unwrap( mbedtls_nist_kw_context *ctx, mbedtls_nist_kw_mode_t mode,
161                             const unsigned char *input, size_t in_len,
162                             unsigned char *output, size_t* out_len, size_t out_size);
163 
164 
165 #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C)
166 /**
167  * \brief          The key wrapping checkup routine.
168  *
169  * \return         \c 0 on success.
170  * \return         \c 1 on failure.
171  */
172 int mbedtls_nist_kw_self_test( int verbose );
173 #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif /* MBEDTLS_NIST_KW_H */
180