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