Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 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 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 19 | * Copyright The Mbed TLS Contributors |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 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. |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #ifndef MBEDTLS_NIST_KW_H |
| 36 | #define MBEDTLS_NIST_KW_H |
| 37 | |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 38 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 39 | #include "mbedtls/config.h" |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 40 | #else |
| 41 | #include MBEDTLS_CONFIG_FILE |
| 42 | #endif |
| 43 | |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 44 | #include "mbedtls/cipher.h" |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 45 | |
| 46 | #ifdef __cplusplus |
| 47 | extern "C" { |
| 48 | #endif |
| 49 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 50 | typedef enum { |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 51 | MBEDTLS_KW_MODE_KW = 0, |
| 52 | MBEDTLS_KW_MODE_KWP = 1 |
| 53 | } mbedtls_nist_kw_mode_t; |
| 54 | |
| 55 | #if !defined(MBEDTLS_NIST_KW_ALT) |
| 56 | // Regular implementation |
| 57 | // |
| 58 | |
| 59 | /** |
| 60 | * \brief The key wrapping context-type definition. The key wrapping context is passed |
| 61 | * to the APIs called. |
| 62 | * |
| 63 | * \note The definition of this type may change in future library versions. |
| 64 | * Don't make any assumptions on this context! |
| 65 | */ |
| 66 | typedef struct { |
| 67 | mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */ |
| 68 | } mbedtls_nist_kw_context; |
| 69 | |
| 70 | #else /* MBEDTLS_NIST_key wrapping_ALT */ |
| 71 | #include "nist_kw_alt.h" |
| 72 | #endif /* MBEDTLS_NIST_KW_ALT */ |
| 73 | |
| 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(). |
| 78 | * |
| 79 | * \param ctx The key wrapping context to initialize. |
| 80 | * |
| 81 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | void mbedtls_nist_kw_init(mbedtls_nist_kw_context *ctx); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * \brief This function initializes the key wrapping context set in the |
| 86 | * \p ctx parameter and sets the encryption key. |
| 87 | * |
| 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 |
| 93 | * |
| 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. |
| 99 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 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); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * \brief This function releases and clears the specified key wrapping context |
| 108 | * and underlying cipher sub-context. |
| 109 | * |
| 110 | * \param ctx The key wrapping context to clear. |
| 111 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | void mbedtls_nist_kw_free(mbedtls_nist_kw_context *ctx); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * \brief This function encrypts a buffer using key wrapping. |
| 116 | * |
| 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. |
| 130 | * |
| 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. |
| 134 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 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); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * \brief This function decrypts a buffer using key wrapping. |
| 141 | * |
| 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. |
| 156 | * |
| 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. |
| 161 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 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); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 165 | |
| 166 | |
| 167 | #if defined(MBEDTLS_SELF_TEST) && defined(MBEDTLS_AES_C) |
| 168 | /** |
| 169 | * \brief The key wrapping checkup routine. |
| 170 | * |
| 171 | * \return \c 0 on success. |
| 172 | * \return \c 1 on failure. |
| 173 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | int mbedtls_nist_kw_self_test(int verbose); |
Ron Eldor | 466a57f | 2018-05-03 16:54:28 +0300 | [diff] [blame] | 175 | #endif /* MBEDTLS_SELF_TEST && MBEDTLS_AES_C */ |
| 176 | |
| 177 | #ifdef __cplusplus |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | #endif /* MBEDTLS_NIST_KW_H */ |