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