4 * \brief This file contains an abstraction interface for use with the cipher
5 * primitives provided by the library. It provides a common interface to all of
6 * the available cipher operations.
8 * \author Adriaan de Jong <dejong@fox-it.com>
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0
14 * Licensed under the Apache License, Version 2.0 (the "License"); you may
15 * not use this file except in compliance with the License.
16 * You may obtain a copy of the License at
18 * http://www.apache.org/licenses/LICENSE-2.0
20 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
22 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the License for the specific language governing permissions and
24 * limitations under the License.
27 #ifndef MBEDTLS_CIPHER_H
28 #define MBEDTLS_CIPHER_H
30 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include "mbedtls/config.h"
33 #include MBEDTLS_CONFIG_FILE
37 #include "mbedtls/platform_util.h"
39 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
40 #define MBEDTLS_CIPHER_MODE_AEAD
43 #if defined(MBEDTLS_CIPHER_MODE_CBC)
44 #define MBEDTLS_CIPHER_MODE_WITH_PADDING
47 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
48 defined(MBEDTLS_CHACHA20_C)
49 #define MBEDTLS_CIPHER_MODE_STREAM
52 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
53 !defined(inline) && !defined(__cplusplus)
54 #define inline __inline
57 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
58 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
59 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
60 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
61 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
62 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
63 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
65 /* MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED is deprecated and should not be used. */
66 #define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
68 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
69 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
76 * \brief Supported cipher types.
78 * \warning RC4 and DES are considered weak ciphers and their use
79 * constitutes a security risk. Arm recommends considering stronger
83 MBEDTLS_CIPHER_ID_NONE
= 0, /**< Placeholder to mark the end of cipher ID lists. */
84 MBEDTLS_CIPHER_ID_NULL
, /**< The identity cipher, treated as a stream cipher. */
85 MBEDTLS_CIPHER_ID_AES
, /**< The AES cipher. */
86 MBEDTLS_CIPHER_ID_DES
, /**< The DES cipher. */
87 MBEDTLS_CIPHER_ID_3DES
, /**< The Triple DES cipher. */
88 MBEDTLS_CIPHER_ID_CAMELLIA
, /**< The Camellia cipher. */
89 MBEDTLS_CIPHER_ID_BLOWFISH
, /**< The Blowfish cipher. */
90 MBEDTLS_CIPHER_ID_ARC4
, /**< The RC4 cipher. */
91 MBEDTLS_CIPHER_ID_ARIA
, /**< The Aria cipher. */
92 MBEDTLS_CIPHER_ID_CHACHA20
, /**< The ChaCha20 cipher. */
93 } mbedtls_cipher_id_t
;
96 * \brief Supported {cipher type, cipher mode} pairs.
98 * \warning RC4 and DES are considered weak ciphers and their use
99 * constitutes a security risk. Arm recommends considering stronger
103 MBEDTLS_CIPHER_NONE
= 0, /**< Placeholder to mark the end of cipher-pair lists. */
104 MBEDTLS_CIPHER_NULL
, /**< The identity stream cipher. */
105 MBEDTLS_CIPHER_AES_128_ECB
, /**< AES cipher with 128-bit ECB mode. */
106 MBEDTLS_CIPHER_AES_192_ECB
, /**< AES cipher with 192-bit ECB mode. */
107 MBEDTLS_CIPHER_AES_256_ECB
, /**< AES cipher with 256-bit ECB mode. */
108 MBEDTLS_CIPHER_AES_128_CBC
, /**< AES cipher with 128-bit CBC mode. */
109 MBEDTLS_CIPHER_AES_192_CBC
, /**< AES cipher with 192-bit CBC mode. */
110 MBEDTLS_CIPHER_AES_256_CBC
, /**< AES cipher with 256-bit CBC mode. */
111 MBEDTLS_CIPHER_AES_128_CFB128
, /**< AES cipher with 128-bit CFB128 mode. */
112 MBEDTLS_CIPHER_AES_192_CFB128
, /**< AES cipher with 192-bit CFB128 mode. */
113 MBEDTLS_CIPHER_AES_256_CFB128
, /**< AES cipher with 256-bit CFB128 mode. */
114 MBEDTLS_CIPHER_AES_128_CTR
, /**< AES cipher with 128-bit CTR mode. */
115 MBEDTLS_CIPHER_AES_192_CTR
, /**< AES cipher with 192-bit CTR mode. */
116 MBEDTLS_CIPHER_AES_256_CTR
, /**< AES cipher with 256-bit CTR mode. */
117 MBEDTLS_CIPHER_AES_128_GCM
, /**< AES cipher with 128-bit GCM mode. */
118 MBEDTLS_CIPHER_AES_192_GCM
, /**< AES cipher with 192-bit GCM mode. */
119 MBEDTLS_CIPHER_AES_256_GCM
, /**< AES cipher with 256-bit GCM mode. */
120 MBEDTLS_CIPHER_CAMELLIA_128_ECB
, /**< Camellia cipher with 128-bit ECB mode. */
121 MBEDTLS_CIPHER_CAMELLIA_192_ECB
, /**< Camellia cipher with 192-bit ECB mode. */
122 MBEDTLS_CIPHER_CAMELLIA_256_ECB
, /**< Camellia cipher with 256-bit ECB mode. */
123 MBEDTLS_CIPHER_CAMELLIA_128_CBC
, /**< Camellia cipher with 128-bit CBC mode. */
124 MBEDTLS_CIPHER_CAMELLIA_192_CBC
, /**< Camellia cipher with 192-bit CBC mode. */
125 MBEDTLS_CIPHER_CAMELLIA_256_CBC
, /**< Camellia cipher with 256-bit CBC mode. */
126 MBEDTLS_CIPHER_CAMELLIA_128_CFB128
, /**< Camellia cipher with 128-bit CFB128 mode. */
127 MBEDTLS_CIPHER_CAMELLIA_192_CFB128
, /**< Camellia cipher with 192-bit CFB128 mode. */
128 MBEDTLS_CIPHER_CAMELLIA_256_CFB128
, /**< Camellia cipher with 256-bit CFB128 mode. */
129 MBEDTLS_CIPHER_CAMELLIA_128_CTR
, /**< Camellia cipher with 128-bit CTR mode. */
130 MBEDTLS_CIPHER_CAMELLIA_192_CTR
, /**< Camellia cipher with 192-bit CTR mode. */
131 MBEDTLS_CIPHER_CAMELLIA_256_CTR
, /**< Camellia cipher with 256-bit CTR mode. */
132 MBEDTLS_CIPHER_CAMELLIA_128_GCM
, /**< Camellia cipher with 128-bit GCM mode. */
133 MBEDTLS_CIPHER_CAMELLIA_192_GCM
, /**< Camellia cipher with 192-bit GCM mode. */
134 MBEDTLS_CIPHER_CAMELLIA_256_GCM
, /**< Camellia cipher with 256-bit GCM mode. */
135 MBEDTLS_CIPHER_DES_ECB
, /**< DES cipher with ECB mode. */
136 MBEDTLS_CIPHER_DES_CBC
, /**< DES cipher with CBC mode. */
137 MBEDTLS_CIPHER_DES_EDE_ECB
, /**< DES cipher with EDE ECB mode. */
138 MBEDTLS_CIPHER_DES_EDE_CBC
, /**< DES cipher with EDE CBC mode. */
139 MBEDTLS_CIPHER_DES_EDE3_ECB
, /**< DES cipher with EDE3 ECB mode. */
140 MBEDTLS_CIPHER_DES_EDE3_CBC
, /**< DES cipher with EDE3 CBC mode. */
141 MBEDTLS_CIPHER_BLOWFISH_ECB
, /**< Blowfish cipher with ECB mode. */
142 MBEDTLS_CIPHER_BLOWFISH_CBC
, /**< Blowfish cipher with CBC mode. */
143 MBEDTLS_CIPHER_BLOWFISH_CFB64
, /**< Blowfish cipher with CFB64 mode. */
144 MBEDTLS_CIPHER_BLOWFISH_CTR
, /**< Blowfish cipher with CTR mode. */
145 MBEDTLS_CIPHER_ARC4_128
, /**< RC4 cipher with 128-bit mode. */
146 MBEDTLS_CIPHER_AES_128_CCM
, /**< AES cipher with 128-bit CCM mode. */
147 MBEDTLS_CIPHER_AES_192_CCM
, /**< AES cipher with 192-bit CCM mode. */
148 MBEDTLS_CIPHER_AES_256_CCM
, /**< AES cipher with 256-bit CCM mode. */
149 MBEDTLS_CIPHER_CAMELLIA_128_CCM
, /**< Camellia cipher with 128-bit CCM mode. */
150 MBEDTLS_CIPHER_CAMELLIA_192_CCM
, /**< Camellia cipher with 192-bit CCM mode. */
151 MBEDTLS_CIPHER_CAMELLIA_256_CCM
, /**< Camellia cipher with 256-bit CCM mode. */
152 MBEDTLS_CIPHER_ARIA_128_ECB
, /**< Aria cipher with 128-bit key and ECB mode. */
153 MBEDTLS_CIPHER_ARIA_192_ECB
, /**< Aria cipher with 192-bit key and ECB mode. */
154 MBEDTLS_CIPHER_ARIA_256_ECB
, /**< Aria cipher with 256-bit key and ECB mode. */
155 MBEDTLS_CIPHER_ARIA_128_CBC
, /**< Aria cipher with 128-bit key and CBC mode. */
156 MBEDTLS_CIPHER_ARIA_192_CBC
, /**< Aria cipher with 192-bit key and CBC mode. */
157 MBEDTLS_CIPHER_ARIA_256_CBC
, /**< Aria cipher with 256-bit key and CBC mode. */
158 MBEDTLS_CIPHER_ARIA_128_CFB128
, /**< Aria cipher with 128-bit key and CFB-128 mode. */
159 MBEDTLS_CIPHER_ARIA_192_CFB128
, /**< Aria cipher with 192-bit key and CFB-128 mode. */
160 MBEDTLS_CIPHER_ARIA_256_CFB128
, /**< Aria cipher with 256-bit key and CFB-128 mode. */
161 MBEDTLS_CIPHER_ARIA_128_CTR
, /**< Aria cipher with 128-bit key and CTR mode. */
162 MBEDTLS_CIPHER_ARIA_192_CTR
, /**< Aria cipher with 192-bit key and CTR mode. */
163 MBEDTLS_CIPHER_ARIA_256_CTR
, /**< Aria cipher with 256-bit key and CTR mode. */
164 MBEDTLS_CIPHER_ARIA_128_GCM
, /**< Aria cipher with 128-bit key and GCM mode. */
165 MBEDTLS_CIPHER_ARIA_192_GCM
, /**< Aria cipher with 192-bit key and GCM mode. */
166 MBEDTLS_CIPHER_ARIA_256_GCM
, /**< Aria cipher with 256-bit key and GCM mode. */
167 MBEDTLS_CIPHER_ARIA_128_CCM
, /**< Aria cipher with 128-bit key and CCM mode. */
168 MBEDTLS_CIPHER_ARIA_192_CCM
, /**< Aria cipher with 192-bit key and CCM mode. */
169 MBEDTLS_CIPHER_ARIA_256_CCM
, /**< Aria cipher with 256-bit key and CCM mode. */
170 MBEDTLS_CIPHER_AES_128_OFB
, /**< AES 128-bit cipher in OFB mode. */
171 MBEDTLS_CIPHER_AES_192_OFB
, /**< AES 192-bit cipher in OFB mode. */
172 MBEDTLS_CIPHER_AES_256_OFB
, /**< AES 256-bit cipher in OFB mode. */
173 MBEDTLS_CIPHER_AES_128_XTS
, /**< AES 128-bit cipher in XTS block mode. */
174 MBEDTLS_CIPHER_AES_256_XTS
, /**< AES 256-bit cipher in XTS block mode. */
175 MBEDTLS_CIPHER_CHACHA20
, /**< ChaCha20 stream cipher. */
176 MBEDTLS_CIPHER_CHACHA20_POLY1305
, /**< ChaCha20-Poly1305 AEAD cipher. */
177 MBEDTLS_CIPHER_AES_128_KW
, /**< AES cipher with 128-bit NIST KW mode. */
178 MBEDTLS_CIPHER_AES_192_KW
, /**< AES cipher with 192-bit NIST KW mode. */
179 MBEDTLS_CIPHER_AES_256_KW
, /**< AES cipher with 256-bit NIST KW mode. */
180 MBEDTLS_CIPHER_AES_128_KWP
, /**< AES cipher with 128-bit NIST KWP mode. */
181 MBEDTLS_CIPHER_AES_192_KWP
, /**< AES cipher with 192-bit NIST KWP mode. */
182 MBEDTLS_CIPHER_AES_256_KWP
, /**< AES cipher with 256-bit NIST KWP mode. */
183 } mbedtls_cipher_type_t
;
185 /** Supported cipher modes. */
187 MBEDTLS_MODE_NONE
= 0, /**< None. */
188 MBEDTLS_MODE_ECB
, /**< The ECB cipher mode. */
189 MBEDTLS_MODE_CBC
, /**< The CBC cipher mode. */
190 MBEDTLS_MODE_CFB
, /**< The CFB cipher mode. */
191 MBEDTLS_MODE_OFB
, /**< The OFB cipher mode. */
192 MBEDTLS_MODE_CTR
, /**< The CTR cipher mode. */
193 MBEDTLS_MODE_GCM
, /**< The GCM cipher mode. */
194 MBEDTLS_MODE_STREAM
, /**< The stream cipher mode. */
195 MBEDTLS_MODE_CCM
, /**< The CCM cipher mode. */
196 MBEDTLS_MODE_XTS
, /**< The XTS cipher mode. */
197 MBEDTLS_MODE_CHACHAPOLY
, /**< The ChaCha-Poly cipher mode. */
198 MBEDTLS_MODE_KW
, /**< The SP800-38F KW mode */
199 MBEDTLS_MODE_KWP
, /**< The SP800-38F KWP mode */
200 } mbedtls_cipher_mode_t
;
202 /** Supported cipher padding types. */
204 MBEDTLS_PADDING_PKCS7
= 0, /**< PKCS7 padding (default). */
205 MBEDTLS_PADDING_ONE_AND_ZEROS
, /**< ISO/IEC 7816-4 padding. */
206 MBEDTLS_PADDING_ZEROS_AND_LEN
, /**< ANSI X.923 padding. */
207 MBEDTLS_PADDING_ZEROS
, /**< Zero padding (not reversible). */
208 MBEDTLS_PADDING_NONE
, /**< Never pad (full blocks only). */
209 } mbedtls_cipher_padding_t
;
211 /** Type of operation. */
213 MBEDTLS_OPERATION_NONE
= -1,
216 } mbedtls_operation_t
;
219 /** Undefined key length. */
220 MBEDTLS_KEY_LENGTH_NONE
= 0,
221 /** Key length, in bits (including parity), for DES keys. */
222 MBEDTLS_KEY_LENGTH_DES
= 64,
223 /** Key length in bits, including parity, for DES in two-key EDE. */
224 MBEDTLS_KEY_LENGTH_DES_EDE
= 128,
225 /** Key length in bits, including parity, for DES in three-key EDE. */
226 MBEDTLS_KEY_LENGTH_DES_EDE3
= 192,
229 /** Maximum length of any IV, in Bytes. */
230 /* This should ideally be derived automatically from list of ciphers.
231 * This should be kept in sync with MBEDTLS_SSL_MAX_IV_LENGTH defined
232 * in ssl_internal.h. */
233 #define MBEDTLS_MAX_IV_LENGTH 16
235 /** Maximum block size of any cipher, in Bytes. */
236 /* This should ideally be derived automatically from list of ciphers.
237 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
238 * in ssl_internal.h. */
239 #define MBEDTLS_MAX_BLOCK_LENGTH 16
241 /** Maximum key length, in Bytes. */
242 /* This should ideally be derived automatically from list of ciphers.
243 * For now, only check whether XTS is enabled which uses 64 Byte keys,
244 * and use 32 Bytes as an upper bound for the maximum key length otherwise.
245 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
246 * in ssl_internal.h, which however deliberately ignores the case of XTS
247 * since the latter isn't used in SSL/TLS. */
248 #if defined(MBEDTLS_CIPHER_MODE_XTS)
249 #define MBEDTLS_MAX_KEY_LENGTH 64
251 #define MBEDTLS_MAX_KEY_LENGTH 32
252 #endif /* MBEDTLS_CIPHER_MODE_XTS */
255 * Base cipher information (opaque struct).
257 typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t
;
260 * CMAC context (opaque struct).
262 typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t
;
265 * Cipher information. Allows calling cipher functions
268 typedef struct mbedtls_cipher_info_t
{
269 /** Full cipher identifier. For example,
270 * MBEDTLS_CIPHER_AES_256_CBC.
272 mbedtls_cipher_type_t type
;
274 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
275 mbedtls_cipher_mode_t mode
;
277 /** The cipher key length, in bits. This is the
278 * default length for variable sized ciphers.
279 * Includes parity bits for ciphers like DES.
281 unsigned int key_bitlen
;
283 /** Name of the cipher. */
286 /** IV or nonce size, in Bytes.
287 * For ciphers that accept variable IV sizes,
288 * this is the recommended size.
290 unsigned int iv_size
;
292 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
293 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
294 * cipher supports variable IV or variable key sizes, respectively.
298 /** The block size, in Bytes. */
299 unsigned int block_size
;
301 /** Struct for base cipher information and functions. */
302 const mbedtls_cipher_base_t
*base
;
304 } mbedtls_cipher_info_t
;
307 * Generic cipher context.
309 typedef struct mbedtls_cipher_context_t
{
310 /** Information about the associated cipher. */
311 const mbedtls_cipher_info_t
*cipher_info
;
313 /** Key length to use. */
316 /** Operation that the key of the context has been
319 mbedtls_operation_t operation
;
321 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
322 /** Padding functions to use, if relevant for
323 * the specific cipher mode.
325 void (*add_padding
)(unsigned char *output
, size_t olen
, size_t data_len
);
326 int (*get_padding
)(unsigned char *input
, size_t ilen
, size_t *data_len
);
329 /** Buffer for input that has not been processed yet. */
330 unsigned char unprocessed_data
[MBEDTLS_MAX_BLOCK_LENGTH
];
332 /** Number of Bytes that have not been processed yet. */
333 size_t unprocessed_len
;
335 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
337 unsigned char iv
[MBEDTLS_MAX_IV_LENGTH
];
339 /** IV size in Bytes, for ciphers with variable-length IVs. */
342 /** The cipher-specific context. */
345 #if defined(MBEDTLS_CMAC_C)
346 /** CMAC-specific context. */
347 mbedtls_cmac_context_t
*cmac_ctx
;
350 #if defined(MBEDTLS_USE_PSA_CRYPTO)
351 /** Indicates whether the cipher operations should be performed
352 * by Mbed TLS' own crypto library or an external implementation
353 * of the PSA Crypto API.
354 * This is unset if the cipher context was established through
355 * mbedtls_cipher_setup(), and set if it was established through
356 * mbedtls_cipher_setup_psa().
358 unsigned char psa_enabled
;
359 #endif /* MBEDTLS_USE_PSA_CRYPTO */
361 } mbedtls_cipher_context_t
;
364 * \brief This function retrieves the list of ciphers supported
365 * by the generic cipher module.
367 * For any cipher identifier in the returned list, you can
368 * obtain the corresponding generic cipher information structure
369 * via mbedtls_cipher_info_from_type(), which can then be used
370 * to prepare a cipher context via mbedtls_cipher_setup().
373 * \return A statically-allocated array of cipher identifiers
374 * of type cipher_type_t. The last entry is zero.
376 const int *mbedtls_cipher_list(void);
379 * \brief This function retrieves the cipher-information
380 * structure associated with the given cipher name.
382 * \param cipher_name Name of the cipher to search for. This must not be
385 * \return The cipher information structure associated with the
386 * given \p cipher_name.
387 * \return \c NULL if the associated cipher information is not found.
389 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_string(const char *cipher_name
);
392 * \brief This function retrieves the cipher-information
393 * structure associated with the given cipher type.
395 * \param cipher_type Type of the cipher to search for.
397 * \return The cipher information structure associated with the
398 * given \p cipher_type.
399 * \return \c NULL if the associated cipher information is not found.
401 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type
);
404 * \brief This function retrieves the cipher-information
405 * structure associated with the given cipher ID,
408 * \param cipher_id The ID of the cipher to search for. For example,
409 * #MBEDTLS_CIPHER_ID_AES.
410 * \param key_bitlen The length of the key in bits.
411 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
413 * \return The cipher information structure associated with the
414 * given \p cipher_id.
415 * \return \c NULL if the associated cipher information is not found.
417 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id
,
419 const mbedtls_cipher_mode_t mode
);
422 * \brief This function initializes a \p cipher_context as NONE.
424 * \param ctx The context to be initialized. This must not be \c NULL.
426 void mbedtls_cipher_init(mbedtls_cipher_context_t
*ctx
);
429 * \brief This function frees and clears the cipher-specific
430 * context of \p ctx. Freeing \p ctx itself remains the
431 * responsibility of the caller.
433 * \param ctx The context to be freed. If this is \c NULL, the
434 * function has no effect, otherwise this must point to an
435 * initialized context.
437 void mbedtls_cipher_free(mbedtls_cipher_context_t
*ctx
);
441 * \brief This function initializes a cipher context for
442 * use with the given cipher primitive.
444 * \param ctx The context to initialize. This must be initialized.
445 * \param cipher_info The cipher to use.
447 * \return \c 0 on success.
448 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
449 * parameter-verification failure.
450 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
451 * cipher-specific context fails.
453 * \internal Currently, the function also clears the structure.
454 * In future versions, the caller will be required to call
455 * mbedtls_cipher_init() on the structure first.
457 int mbedtls_cipher_setup(mbedtls_cipher_context_t
*ctx
,
458 const mbedtls_cipher_info_t
*cipher_info
);
460 #if defined(MBEDTLS_USE_PSA_CRYPTO)
462 * \brief This function initializes a cipher context for
463 * PSA-based use with the given cipher primitive.
465 * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
467 * \param ctx The context to initialize. May not be \c NULL.
468 * \param cipher_info The cipher to use.
469 * \param taglen For AEAD ciphers, the length in bytes of the
470 * authentication tag to use. Subsequent uses of
471 * mbedtls_cipher_auth_encrypt() or
472 * mbedtls_cipher_auth_decrypt() must provide
473 * the same tag length.
474 * For non-AEAD ciphers, the value must be \c 0.
476 * \return \c 0 on success.
477 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
478 * parameter-verification failure.
479 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
480 * cipher-specific context fails.
482 int mbedtls_cipher_setup_psa(mbedtls_cipher_context_t
*ctx
,
483 const mbedtls_cipher_info_t
*cipher_info
,
485 #endif /* MBEDTLS_USE_PSA_CRYPTO */
488 * \brief This function returns the block size of the given cipher.
490 * \param ctx The context of the cipher. This must be initialized.
492 * \return The block size of the underlying cipher.
493 * \return \c 0 if \p ctx has not been initialized.
495 static inline unsigned int mbedtls_cipher_get_block_size(
496 const mbedtls_cipher_context_t
*ctx
) {
497 MBEDTLS_INTERNAL_VALIDATE_RET(ctx
!= NULL
, 0);
498 if (ctx
->cipher_info
== NULL
)
501 return ctx
->cipher_info
->block_size
;
505 * \brief This function returns the mode of operation for
506 * the cipher. For example, MBEDTLS_MODE_CBC.
508 * \param ctx The context of the cipher. This must be initialized.
510 * \return The mode of operation.
511 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
513 static inline mbedtls_cipher_mode_t
mbedtls_cipher_get_cipher_mode(
514 const mbedtls_cipher_context_t
*ctx
) {
515 MBEDTLS_INTERNAL_VALIDATE_RET(ctx
!= NULL
, MBEDTLS_MODE_NONE
);
516 if (ctx
->cipher_info
== NULL
)
517 return MBEDTLS_MODE_NONE
;
519 return ctx
->cipher_info
->mode
;
523 * \brief This function returns the size of the IV or nonce
524 * of the cipher, in Bytes.
526 * \param ctx The context of the cipher. This must be initialized.
528 * \return The recommended IV size if no IV has been set.
529 * \return \c 0 for ciphers not using an IV or a nonce.
530 * \return The actual size if an IV has been set.
532 static inline int mbedtls_cipher_get_iv_size(
533 const mbedtls_cipher_context_t
*ctx
) {
534 MBEDTLS_INTERNAL_VALIDATE_RET(ctx
!= NULL
, 0);
535 if (ctx
->cipher_info
== NULL
)
538 if (ctx
->iv_size
!= 0)
539 return (int) ctx
->iv_size
;
541 return (int) ctx
->cipher_info
->iv_size
;
545 * \brief This function returns the type of the given cipher.
547 * \param ctx The context of the cipher. This must be initialized.
549 * \return The type of the cipher.
550 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
552 static inline mbedtls_cipher_type_t
mbedtls_cipher_get_type(
553 const mbedtls_cipher_context_t
*ctx
) {
554 MBEDTLS_INTERNAL_VALIDATE_RET(
555 ctx
!= NULL
, MBEDTLS_CIPHER_NONE
);
556 if (ctx
->cipher_info
== NULL
)
557 return MBEDTLS_CIPHER_NONE
;
559 return ctx
->cipher_info
->type
;
563 * \brief This function returns the name of the given cipher
566 * \param ctx The context of the cipher. This must be initialized.
568 * \return The name of the cipher.
569 * \return NULL if \p ctx has not been not initialized.
571 static inline const char *mbedtls_cipher_get_name(
572 const mbedtls_cipher_context_t
*ctx
) {
573 MBEDTLS_INTERNAL_VALIDATE_RET(ctx
!= NULL
, 0);
574 if (ctx
->cipher_info
== NULL
)
577 return ctx
->cipher_info
->name
;
581 * \brief This function returns the key length of the cipher.
583 * \param ctx The context of the cipher. This must be initialized.
585 * \return The key length of the cipher in bits.
586 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
589 static inline int mbedtls_cipher_get_key_bitlen(
590 const mbedtls_cipher_context_t
*ctx
) {
591 MBEDTLS_INTERNAL_VALIDATE_RET(
592 ctx
!= NULL
, MBEDTLS_KEY_LENGTH_NONE
);
593 if (ctx
->cipher_info
== NULL
)
594 return MBEDTLS_KEY_LENGTH_NONE
;
596 return (int) ctx
->cipher_info
->key_bitlen
;
600 * \brief This function returns the operation of the given cipher.
602 * \param ctx The context of the cipher. This must be initialized.
604 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
605 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
607 static inline mbedtls_operation_t
mbedtls_cipher_get_operation(
608 const mbedtls_cipher_context_t
*ctx
) {
609 MBEDTLS_INTERNAL_VALIDATE_RET(
610 ctx
!= NULL
, MBEDTLS_OPERATION_NONE
);
611 if (ctx
->cipher_info
== NULL
)
612 return MBEDTLS_OPERATION_NONE
;
614 return ctx
->operation
;
618 * \brief This function sets the key to use with the given context.
620 * \param ctx The generic cipher context. This must be initialized and
621 * bound to a cipher information structure.
622 * \param key The key to use. This must be a readable buffer of at
623 * least \p key_bitlen Bits.
624 * \param key_bitlen The key length to use, in Bits.
625 * \param operation The operation that the key will be used for:
626 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
628 * \return \c 0 on success.
629 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
630 * parameter-verification failure.
631 * \return A cipher-specific error code on failure.
633 int mbedtls_cipher_setkey(mbedtls_cipher_context_t
*ctx
,
634 const unsigned char *key
,
636 const mbedtls_operation_t operation
);
638 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
640 * \brief This function sets the padding mode, for cipher modes
643 * The default passing mode is PKCS7 padding.
645 * \param ctx The generic cipher context. This must be initialized and
646 * bound to a cipher information structure.
647 * \param mode The padding mode.
649 * \return \c 0 on success.
650 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
651 * if the selected padding mode is not supported.
652 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
653 * does not support padding.
655 int mbedtls_cipher_set_padding_mode(mbedtls_cipher_context_t
*ctx
,
656 mbedtls_cipher_padding_t mode
);
657 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
660 * \brief This function sets the initialization vector (IV)
663 * \note Some ciphers do not use IVs nor nonce. For these
664 * ciphers, this function has no effect.
666 * \param ctx The generic cipher context. This must be initialized and
667 * bound to a cipher information structure.
668 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
669 * must be a readable buffer of at least \p iv_len Bytes.
670 * \param iv_len The IV length for ciphers with variable-size IV.
671 * This parameter is discarded by ciphers with fixed-size IV.
673 * \return \c 0 on success.
674 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
675 * parameter-verification failure.
677 int mbedtls_cipher_set_iv(mbedtls_cipher_context_t
*ctx
,
678 const unsigned char *iv
,
682 * \brief This function resets the cipher state.
684 * \param ctx The generic cipher context. This must be initialized.
686 * \return \c 0 on success.
687 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
688 * parameter-verification failure.
690 int mbedtls_cipher_reset(mbedtls_cipher_context_t
*ctx
);
692 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
694 * \brief This function adds additional data for AEAD ciphers.
695 * Currently supported with GCM and ChaCha20+Poly1305.
696 * This must be called exactly once, after
697 * mbedtls_cipher_reset().
699 * \param ctx The generic cipher context. This must be initialized.
700 * \param ad The additional data to use. This must be a readable
701 * buffer of at least \p ad_len Bytes.
702 * \param ad_len The length of \p ad in Bytes.
704 * \return \c 0 on success.
705 * \return A specific error code on failure.
707 int mbedtls_cipher_update_ad(mbedtls_cipher_context_t
*ctx
,
708 const unsigned char *ad
, size_t ad_len
);
709 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
712 * \brief The generic cipher update function. It encrypts or
713 * decrypts using the given cipher context. Writes as
714 * many block-sized blocks of data as possible to output.
715 * Any data that cannot be written immediately is either
716 * added to the next block, or flushed when
717 * mbedtls_cipher_finish() is called.
718 * Exception: For MBEDTLS_MODE_ECB, expects a single block
719 * in size. For example, 16 Bytes for AES.
721 * \note If the underlying cipher is used in GCM mode, all calls
722 * to this function, except for the last one before
723 * mbedtls_cipher_finish(), must have \p ilen as a
724 * multiple of the block size of the cipher.
726 * \param ctx The generic cipher context. This must be initialized and
728 * \param input The buffer holding the input data. This must be a
729 * readable buffer of at least \p ilen Bytes.
730 * \param ilen The length of the input data.
731 * \param output The buffer for the output data. This must be able to
732 * hold at least `ilen + block_size`. This must not be the
733 * same buffer as \p input.
734 * \param olen The length of the output data, to be updated with the
735 * actual number of Bytes written. This must not be
738 * \return \c 0 on success.
739 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
740 * parameter-verification failure.
741 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
742 * unsupported mode for a cipher.
743 * \return A cipher-specific error code on failure.
745 int mbedtls_cipher_update(mbedtls_cipher_context_t
*ctx
,
746 const unsigned char *input
,
747 size_t ilen
, unsigned char *output
,
751 * \brief The generic cipher finalization function. If data still
752 * needs to be flushed from an incomplete block, the data
753 * contained in it is padded to the size of
754 * the last block, and written to the \p output buffer.
756 * \param ctx The generic cipher context. This must be initialized and
758 * \param output The buffer to write data to. This needs to be a writable
759 * buffer of at least \p block_size Bytes.
760 * \param olen The length of the data written to the \p output buffer.
761 * This may not be \c NULL.
763 * \return \c 0 on success.
764 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
765 * parameter-verification failure.
766 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
767 * expecting a full block but not receiving one.
768 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
770 * \return A cipher-specific error code on failure.
772 int mbedtls_cipher_finish(mbedtls_cipher_context_t
*ctx
,
773 unsigned char *output
, size_t *olen
);
775 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
777 * \brief This function writes a tag for AEAD ciphers.
778 * Currently supported with GCM and ChaCha20+Poly1305.
779 * This must be called after mbedtls_cipher_finish().
781 * \param ctx The generic cipher context. This must be initialized,
782 * bound to a key, and have just completed a cipher
783 * operation through mbedtls_cipher_finish() the tag for
784 * which should be written.
785 * \param tag The buffer to write the tag to. This must be a writable
786 * buffer of at least \p tag_len Bytes.
787 * \param tag_len The length of the tag to write.
789 * \return \c 0 on success.
790 * \return A specific error code on failure.
792 int mbedtls_cipher_write_tag(mbedtls_cipher_context_t
*ctx
,
793 unsigned char *tag
, size_t tag_len
);
796 * \brief This function checks the tag for AEAD ciphers.
797 * Currently supported with GCM and ChaCha20+Poly1305.
798 * This must be called after mbedtls_cipher_finish().
800 * \param ctx The generic cipher context. This must be initialized.
801 * \param tag The buffer holding the tag. This must be a readable
802 * buffer of at least \p tag_len Bytes.
803 * \param tag_len The length of the tag to check.
805 * \return \c 0 on success.
806 * \return A specific error code on failure.
808 int mbedtls_cipher_check_tag(mbedtls_cipher_context_t
*ctx
,
809 const unsigned char *tag
, size_t tag_len
);
810 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
813 * \brief The generic all-in-one encryption/decryption function,
814 * for all ciphers except AEAD constructs.
816 * \param ctx The generic cipher context. This must be initialized.
817 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
818 * This must be a readable buffer of at least \p iv_len
820 * \param iv_len The IV length for ciphers with variable-size IV.
821 * This parameter is discarded by ciphers with fixed-size
823 * \param input The buffer holding the input data. This must be a
824 * readable buffer of at least \p ilen Bytes.
825 * \param ilen The length of the input data in Bytes.
826 * \param output The buffer for the output data. This must be able to
827 * hold at least `ilen + block_size`. This must not be the
828 * same buffer as \p input.
829 * \param olen The length of the output data, to be updated with the
830 * actual number of Bytes written. This must not be
833 * \note Some ciphers do not use IVs nor nonce. For these
834 * ciphers, use \p iv = NULL and \p iv_len = 0.
836 * \return \c 0 on success.
837 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
838 * parameter-verification failure.
839 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
840 * expecting a full block but not receiving one.
841 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
843 * \return A cipher-specific error code on failure.
845 int mbedtls_cipher_crypt(mbedtls_cipher_context_t
*ctx
,
846 const unsigned char *iv
, size_t iv_len
,
847 const unsigned char *input
, size_t ilen
,
848 unsigned char *output
, size_t *olen
);
850 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
851 #if ! defined(MBEDTLS_DEPRECATED_REMOVED)
852 #if defined(MBEDTLS_DEPRECATED_WARNING)
853 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
855 #define MBEDTLS_DEPRECATED
856 #endif /* MBEDTLS_DEPRECATED_WARNING */
858 * \brief The generic authenticated encryption (AEAD) function.
860 * \deprecated Superseded by mbedtls_cipher_auth_encrypt_ext().
862 * \note This function only supports AEAD algorithms, not key
863 * wrapping algorithms such as NIST_KW; for this, see
864 * mbedtls_cipher_auth_encrypt_ext().
866 * \param ctx The generic cipher context. This must be initialized and
867 * bound to a key associated with an AEAD algorithm.
868 * \param iv The nonce to use. This must be a readable buffer of
869 * at least \p iv_len Bytes and must not be \c NULL.
870 * \param iv_len The length of the nonce. This must satisfy the
871 * constraints imposed by the AEAD cipher used.
872 * \param ad The additional data to authenticate. This must be a
873 * readable buffer of at least \p ad_len Bytes, and may
874 * be \c NULL is \p ad_len is \c 0.
875 * \param ad_len The length of \p ad.
876 * \param input The buffer holding the input data. This must be a
877 * readable buffer of at least \p ilen Bytes, and may be
878 * \c NULL if \p ilen is \c 0.
879 * \param ilen The length of the input data.
880 * \param output The buffer for the output data. This must be a
881 * writable buffer of at least \p ilen Bytes, and must
883 * \param olen This will be filled with the actual number of Bytes
884 * written to the \p output buffer. This must point to a
885 * writable object of type \c size_t.
886 * \param tag The buffer for the authentication tag. This must be a
887 * writable buffer of at least \p tag_len Bytes. See note
888 * below regarding restrictions with PSA-based contexts.
889 * \param tag_len The desired length of the authentication tag. This
890 * must match the constraints imposed by the AEAD cipher
891 * used, and in particular must not be \c 0.
893 * \note If the context is based on PSA (that is, it was set up
894 * with mbedtls_cipher_setup_psa()), then it is required
895 * that \c tag == output + ilen. That is, the tag must be
896 * appended to the ciphertext as recommended by RFC 5116.
898 * \return \c 0 on success.
899 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
900 * parameter-verification failure.
901 * \return A cipher-specific error code on failure.
903 int mbedtls_cipher_auth_encrypt(mbedtls_cipher_context_t
*ctx
,
904 const unsigned char *iv
, size_t iv_len
,
905 const unsigned char *ad
, size_t ad_len
,
906 const unsigned char *input
, size_t ilen
,
907 unsigned char *output
, size_t *olen
,
908 unsigned char *tag
, size_t tag_len
)
912 * \brief The generic authenticated decryption (AEAD) function.
914 * \deprecated Superseded by mbedtls_cipher_auth_decrypt_ext().
916 * \note This function only supports AEAD algorithms, not key
917 * wrapping algorithms such as NIST_KW; for this, see
918 * mbedtls_cipher_auth_decrypt_ext().
920 * \note If the data is not authentic, then the output buffer
921 * is zeroed out to prevent the unauthentic plaintext being
922 * used, making this interface safer.
924 * \param ctx The generic cipher context. This must be initialized and
925 * bound to a key associated with an AEAD algorithm.
926 * \param iv The nonce to use. This must be a readable buffer of
927 * at least \p iv_len Bytes and must not be \c NULL.
928 * \param iv_len The length of the nonce. This must satisfy the
929 * constraints imposed by the AEAD cipher used.
930 * \param ad The additional data to authenticate. This must be a
931 * readable buffer of at least \p ad_len Bytes, and may
932 * be \c NULL is \p ad_len is \c 0.
933 * \param ad_len The length of \p ad.
934 * \param input The buffer holding the input data. This must be a
935 * readable buffer of at least \p ilen Bytes, and may be
936 * \c NULL if \p ilen is \c 0.
937 * \param ilen The length of the input data.
938 * \param output The buffer for the output data. This must be a
939 * writable buffer of at least \p ilen Bytes, and must
941 * \param olen This will be filled with the actual number of Bytes
942 * written to the \p output buffer. This must point to a
943 * writable object of type \c size_t.
944 * \param tag The buffer for the authentication tag. This must be a
945 * readable buffer of at least \p tag_len Bytes. See note
946 * below regarding restrictions with PSA-based contexts.
947 * \param tag_len The length of the authentication tag. This must match
948 * the constraints imposed by the AEAD cipher used, and in
949 * particular must not be \c 0.
951 * \note If the context is based on PSA (that is, it was set up
952 * with mbedtls_cipher_setup_psa()), then it is required
953 * that \c tag == input + len. That is, the tag must be
954 * appended to the ciphertext as recommended by RFC 5116.
956 * \return \c 0 on success.
957 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
958 * parameter-verification failure.
959 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
960 * \return A cipher-specific error code on failure.
962 int mbedtls_cipher_auth_decrypt(mbedtls_cipher_context_t
*ctx
,
963 const unsigned char *iv
, size_t iv_len
,
964 const unsigned char *ad
, size_t ad_len
,
965 const unsigned char *input
, size_t ilen
,
966 unsigned char *output
, size_t *olen
,
967 const unsigned char *tag
, size_t tag_len
)
969 #undef MBEDTLS_DEPRECATED
970 #endif /* MBEDTLS_DEPRECATED_REMOVED */
971 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
973 #if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
975 * \brief The authenticated encryption (AEAD/NIST_KW) function.
977 * \note For AEAD modes, the tag will be appended to the
978 * ciphertext, as recommended by RFC 5116.
979 * (NIST_KW doesn't have a separate tag.)
981 * \param ctx The generic cipher context. This must be initialized and
982 * bound to a key, with an AEAD algorithm or NIST_KW.
983 * \param iv The nonce to use. This must be a readable buffer of
984 * at least \p iv_len Bytes and may be \c NULL if \p
986 * \param iv_len The length of the nonce. For AEAD ciphers, this must
987 * satisfy the constraints imposed by the cipher used.
988 * For NIST_KW, this must be \c 0.
989 * \param ad The additional data to authenticate. This must be a
990 * readable buffer of at least \p ad_len Bytes, and may
991 * be \c NULL is \p ad_len is \c 0.
992 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
993 * \param input The buffer holding the input data. This must be a
994 * readable buffer of at least \p ilen Bytes, and may be
995 * \c NULL if \p ilen is \c 0.
996 * \param ilen The length of the input data.
997 * \param output The buffer for the output data. This must be a
998 * writable buffer of at least \p output_len Bytes, and
999 * must not be \c NULL.
1000 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1001 * ciphers, this must be at least \p ilen + \p tag_len.
1002 * For NIST_KW, this must be at least \p ilen + 8
1003 * (rounded up to a multiple of 8 if KWP is used);
1004 * \p ilen + 15 is always a safe value.
1005 * \param olen This will be filled with the actual number of Bytes
1006 * written to the \p output buffer. This must point to a
1007 * writable object of type \c size_t.
1008 * \param tag_len The desired length of the authentication tag. For AEAD
1009 * ciphers, this must match the constraints imposed by
1010 * the cipher used, and in particular must not be \c 0.
1011 * For NIST_KW, this must be \c 0.
1013 * \return \c 0 on success.
1014 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1015 * parameter-verification failure.
1016 * \return A cipher-specific error code on failure.
1018 int mbedtls_cipher_auth_encrypt_ext(mbedtls_cipher_context_t
*ctx
,
1019 const unsigned char *iv
, size_t iv_len
,
1020 const unsigned char *ad
, size_t ad_len
,
1021 const unsigned char *input
, size_t ilen
,
1022 unsigned char *output
, size_t output_len
,
1023 size_t *olen
, size_t tag_len
);
1026 * \brief The authenticated encryption (AEAD/NIST_KW) function.
1028 * \note If the data is not authentic, then the output buffer
1029 * is zeroed out to prevent the unauthentic plaintext being
1030 * used, making this interface safer.
1032 * \note For AEAD modes, the tag must be appended to the
1033 * ciphertext, as recommended by RFC 5116.
1034 * (NIST_KW doesn't have a separate tag.)
1036 * \param ctx The generic cipher context. This must be initialized and
1037 * bound to a key, with an AEAD algorithm or NIST_KW.
1038 * \param iv The nonce to use. This must be a readable buffer of
1039 * at least \p iv_len Bytes and may be \c NULL if \p
1041 * \param iv_len The length of the nonce. For AEAD ciphers, this must
1042 * satisfy the constraints imposed by the cipher used.
1043 * For NIST_KW, this must be \c 0.
1044 * \param ad The additional data to authenticate. This must be a
1045 * readable buffer of at least \p ad_len Bytes, and may
1046 * be \c NULL is \p ad_len is \c 0.
1047 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
1048 * \param input The buffer holding the input data. This must be a
1049 * readable buffer of at least \p ilen Bytes, and may be
1050 * \c NULL if \p ilen is \c 0.
1051 * \param ilen The length of the input data. For AEAD ciphers this
1052 * must be at least \p tag_len. For NIST_KW this must be
1054 * \param output The buffer for the output data. This must be a
1055 * writable buffer of at least \p output_len Bytes, and
1056 * may be \c NULL if \p output_len is \c 0.
1057 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1058 * ciphers, this must be at least \p ilen - \p tag_len.
1059 * For NIST_KW, this must be at least \p ilen - 8.
1060 * \param olen This will be filled with the actual number of Bytes
1061 * written to the \p output buffer. This must point to a
1062 * writable object of type \c size_t.
1063 * \param tag_len The actual length of the authentication tag. For AEAD
1064 * ciphers, this must match the constraints imposed by
1065 * the cipher used, and in particular must not be \c 0.
1066 * For NIST_KW, this must be \c 0.
1068 * \return \c 0 on success.
1069 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1070 * parameter-verification failure.
1071 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
1072 * \return A cipher-specific error code on failure.
1074 int mbedtls_cipher_auth_decrypt_ext(mbedtls_cipher_context_t
*ctx
,
1075 const unsigned char *iv
, size_t iv_len
,
1076 const unsigned char *ad
, size_t ad_len
,
1077 const unsigned char *input
, size_t ilen
,
1078 unsigned char *output
, size_t output_len
,
1079 size_t *olen
, size_t tag_len
);
1080 #endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1085 #endif /* MBEDTLS_CIPHER_H */