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