Mbed OS Reference
Loading...
Searching...
No Matches
cipher.h
Go to the documentation of this file.
1/**
2 * \file cipher.h
3 *
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.
7 *
8 * \author Adriaan de Jong <dejong@fox-it.com>
9 */
10/*
11 * Copyright The Mbed TLS Contributors
12 * SPDX-License-Identifier: Apache-2.0
13 *
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
17 *
18 * http://www.apache.org/licenses/LICENSE-2.0
19 *
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.
25 */
26
27#ifndef MBEDTLS_CIPHER_H
28#define MBEDTLS_CIPHER_H
29
30#if !defined(MBEDTLS_CONFIG_FILE)
31#include "mbedtls/config.h"
32#else
33#include MBEDTLS_CONFIG_FILE
34#endif
35
36#include <stddef.h>
38
39#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
40#define MBEDTLS_CIPHER_MODE_AEAD
41#endif
42
43#if defined(MBEDTLS_CIPHER_MODE_CBC)
44#define MBEDTLS_CIPHER_MODE_WITH_PADDING
45#endif
46
47#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
48 defined(MBEDTLS_CHACHA20_C)
49#define MBEDTLS_CIPHER_MODE_STREAM
50#endif
51
52#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
53 !defined(inline) && !defined(__cplusplus)
54#define inline __inline
55#endif
56
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. */
64
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. */
67
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. */
70
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75/**
76* \addtogroup mbedtls
77* \{
78* \defgroup mbedtls_cipher_module Cipher
79* \{
80*/
81
82/**
83 * \brief Supported cipher types.
84 *
85 * \warning RC4 and DES are considered weak ciphers and their use
86 * constitutes a security risk. Arm recommends considering stronger
87 * ciphers instead.
88 */
89typedef enum {
90 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
91 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
92 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
93 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
94 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
95 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
96 MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
97 MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
98 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
99 MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
101
102/**
103 * \brief Supported {cipher type, cipher mode} pairs.
104 *
105 * \warning RC4 and DES are considered weak ciphers and their use
106 * constitutes a security risk. Arm recommends considering stronger
107 * ciphers instead.
108 */
109typedef enum {
110 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
111 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
112 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
113 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
114 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
115 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
116 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
117 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
118 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
119 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
120 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
121 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
122 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
123 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
124 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
125 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
126 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
127 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
128 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
129 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
130 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
131 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
132 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
133 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
134 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
135 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
136 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
137 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
138 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
139 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
140 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
141 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
142 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
143 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
144 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
145 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
146 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
147 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
148 MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
149 MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
150 MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
151 MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
152 MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
153 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
154 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
155 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
156 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
157 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
158 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
159 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
160 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
161 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
162 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
163 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
164 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
165 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
166 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
167 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
168 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
169 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
170 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
171 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
172 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
173 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
174 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
175 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
176 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
177 MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
178 MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
179 MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
180 MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
181 MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
182 MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
183 MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
184 MBEDTLS_CIPHER_AES_128_KW, /**< AES cipher with 128-bit NIST KW mode. */
185 MBEDTLS_CIPHER_AES_192_KW, /**< AES cipher with 192-bit NIST KW mode. */
186 MBEDTLS_CIPHER_AES_256_KW, /**< AES cipher with 256-bit NIST KW mode. */
187 MBEDTLS_CIPHER_AES_128_KWP, /**< AES cipher with 128-bit NIST KWP mode. */
188 MBEDTLS_CIPHER_AES_192_KWP, /**< AES cipher with 192-bit NIST KWP mode. */
189 MBEDTLS_CIPHER_AES_256_KWP, /**< AES cipher with 256-bit NIST KWP mode. */
191
192/** Supported cipher modes. */
193typedef enum {
194 MBEDTLS_MODE_NONE = 0, /**< None. */
195 MBEDTLS_MODE_ECB, /**< The ECB cipher mode. */
196 MBEDTLS_MODE_CBC, /**< The CBC cipher mode. */
197 MBEDTLS_MODE_CFB, /**< The CFB cipher mode. */
198 MBEDTLS_MODE_OFB, /**< The OFB cipher mode. */
199 MBEDTLS_MODE_CTR, /**< The CTR cipher mode. */
200 MBEDTLS_MODE_GCM, /**< The GCM cipher mode. */
201 MBEDTLS_MODE_STREAM, /**< The stream cipher mode. */
202 MBEDTLS_MODE_CCM, /**< The CCM cipher mode. */
203 MBEDTLS_MODE_XTS, /**< The XTS cipher mode. */
204 MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
205 MBEDTLS_MODE_KW, /**< The SP800-38F KW mode */
206 MBEDTLS_MODE_KWP, /**< The SP800-38F KWP mode */
208
209/** Supported cipher padding types. */
210typedef enum {
211 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
212 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
213 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
214 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
215 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
217
218/** Type of operation. */
219typedef enum {
220 MBEDTLS_OPERATION_NONE = -1,
221 MBEDTLS_DECRYPT = 0, ///< Operation type for decryption
222 MBEDTLS_ENCRYPT, ///< Operation type for encryption
224
225enum {
226 /** Undefined key length. */
228 /** Key length, in bits (including parity), for DES keys. */
230 /** Key length in bits, including parity, for DES in two-key EDE. */
232 /** Key length in bits, including parity, for DES in three-key EDE. */
234};
235
236/** Maximum length of any IV, in Bytes. */
237/* This should ideally be derived automatically from list of ciphers.
238 * This should be kept in sync with MBEDTLS_SSL_MAX_IV_LENGTH defined
239 * in ssl_internal.h. */
240#define MBEDTLS_MAX_IV_LENGTH 16
241
242/** Maximum block size of any cipher, in Bytes. */
243/* This should ideally be derived automatically from list of ciphers.
244 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
245 * in ssl_internal.h. */
246#define MBEDTLS_MAX_BLOCK_LENGTH 16
247
248/** Maximum key length, in Bytes. */
249/* This should ideally be derived automatically from list of ciphers.
250 * For now, only check whether XTS is enabled which uses 64 Byte keys,
251 * and use 32 Bytes as an upper bound for the maximum key length otherwise.
252 * This should be kept in sync with MBEDTLS_SSL_MAX_BLOCK_LENGTH defined
253 * in ssl_internal.h, which however deliberately ignores the case of XTS
254 * since the latter isn't used in SSL/TLS. */
255#if defined(MBEDTLS_CIPHER_MODE_XTS)
256#define MBEDTLS_MAX_KEY_LENGTH 64
257#else
258#define MBEDTLS_MAX_KEY_LENGTH 32
259#endif /* MBEDTLS_CIPHER_MODE_XTS */
260
261/**
262 * Base cipher information (opaque struct).
263 */
265
266/**
267 * CMAC context (opaque struct).
268 */
270
271/**
272 * Cipher information. Allows calling cipher functions
273 * in a generic way.
274 */
276{
277 /** Full cipher identifier. For example,
278 * MBEDTLS_CIPHER_AES_256_CBC.
279 */
281
282 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
284
285 /** The cipher key length, in bits. This is the
286 * default length for variable sized ciphers.
287 * Includes parity bits for ciphers like DES.
288 */
289 unsigned int key_bitlen;
290
291 /** Name of the cipher. */
292 const char * name;
293
294 /** IV or nonce size, in Bytes.
295 * For ciphers that accept variable IV sizes,
296 * this is the recommended size.
297 */
298 unsigned int iv_size;
299
300 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
301 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
302 * cipher supports variable IV or variable key sizes, respectively.
303 */
304 int flags;
305
306 /** The block size, in Bytes. */
307 unsigned int block_size;
308
309 /** Struct for base cipher information and functions. */
311
313
314/**
315 * Generic cipher context.
316 */
318{
319 /** Information about the associated cipher. */
321
322 /** Key length to use. */
324
325 /** Operation that the key of the context has been
326 * initialized for.
327 */
329
330#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
331 /** Padding functions to use, if relevant for
332 * the specific cipher mode.
333 */
334 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
335 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
336#endif
337
338 /** Buffer for input that has not been processed yet. */
340
341 /** Number of Bytes that have not been processed yet. */
343
344 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
345 * for XTS-mode. */
346 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
347
348 /** IV size in Bytes, for ciphers with variable-length IVs. */
349 size_t iv_size;
350
351 /** The cipher-specific context. */
353
354#if defined(MBEDTLS_CMAC_C)
355 /** CMAC-specific context. */
357#endif
358
359#if defined(MBEDTLS_USE_PSA_CRYPTO)
360 /** Indicates whether the cipher operations should be performed
361 * by Mbed TLS' own crypto library or an external implementation
362 * of the PSA Crypto API.
363 * This is unset if the cipher context was established through
364 * mbedtls_cipher_setup(), and set if it was established through
365 * mbedtls_cipher_setup_psa().
366 */
367 unsigned char psa_enabled;
368#endif /* MBEDTLS_USE_PSA_CRYPTO */
369
371
372/**
373 * \brief This function retrieves the list of ciphers supported
374 * by the generic cipher module.
375 *
376 * For any cipher identifier in the returned list, you can
377 * obtain the corresponding generic cipher information structure
378 * via mbedtls_cipher_info_from_type(), which can then be used
379 * to prepare a cipher context via mbedtls_cipher_setup().
380 *
381 *
382 * \return A statically-allocated array of cipher identifiers
383 * of type cipher_type_t. The last entry is zero.
384 */
385const int *mbedtls_cipher_list( void );
386
387/**
388 * \brief This function retrieves the cipher-information
389 * structure associated with the given cipher name.
390 *
391 * \param cipher_name Name of the cipher to search for. This must not be
392 * \c NULL.
393 *
394 * \return The cipher information structure associated with the
395 * given \p cipher_name.
396 * \return \c NULL if the associated cipher information is not found.
397 */
399
400/**
401 * \brief This function retrieves the cipher-information
402 * structure associated with the given cipher type.
403 *
404 * \param cipher_type Type of the cipher to search for.
405 *
406 * \return The cipher information structure associated with the
407 * given \p cipher_type.
408 * \return \c NULL if the associated cipher information is not found.
409 */
411
412/**
413 * \brief This function retrieves the cipher-information
414 * structure associated with the given cipher ID,
415 * key size and mode.
416 *
417 * \param cipher_id The ID of the cipher to search for. For example,
418 * #MBEDTLS_CIPHER_ID_AES.
419 * \param key_bitlen The length of the key in bits.
420 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
421 *
422 * \return The cipher information structure associated with the
423 * given \p cipher_id.
424 * \return \c NULL if the associated cipher information is not found.
425 */
427 int key_bitlen,
428 const mbedtls_cipher_mode_t mode );
429
430/**
431 * \brief This function initializes a \p cipher_context as NONE.
432 *
433 * \param ctx The context to be initialized. This must not be \c NULL.
434 */
436
437/**
438 * \brief This function frees and clears the cipher-specific
439 * context of \p ctx. Freeing \p ctx itself remains the
440 * responsibility of the caller.
441 *
442 * \param ctx The context to be freed. If this is \c NULL, the
443 * function has no effect, otherwise this must point to an
444 * initialized context.
445 */
447
448
449/**
450 * \brief This function initializes a cipher context for
451 * use with the given cipher primitive.
452 *
453 * \param ctx The context to initialize. This must be initialized.
454 * \param cipher_info The cipher to use.
455 *
456 * \return \c 0 on success.
457 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
458 * parameter-verification failure.
459 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
460 * cipher-specific context fails.
461 *
462 * \internal Currently, the function also clears the structure.
463 * In future versions, the caller will be required to call
464 * mbedtls_cipher_init() on the structure first.
465 */
467 const mbedtls_cipher_info_t *cipher_info );
468
469#if defined(MBEDTLS_USE_PSA_CRYPTO)
470/**
471 * \brief This function initializes a cipher context for
472 * PSA-based use with the given cipher primitive.
473 *
474 * \note See #MBEDTLS_USE_PSA_CRYPTO for information on PSA.
475 *
476 * \param ctx The context to initialize. May not be \c NULL.
477 * \param cipher_info The cipher to use.
478 * \param taglen For AEAD ciphers, the length in bytes of the
479 * authentication tag to use. Subsequent uses of
480 * mbedtls_cipher_auth_encrypt() or
481 * mbedtls_cipher_auth_decrypt() must provide
482 * the same tag length.
483 * For non-AEAD ciphers, the value must be \c 0.
484 *
485 * \return \c 0 on success.
486 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
487 * parameter-verification failure.
488 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
489 * cipher-specific context fails.
490 */
491int mbedtls_cipher_setup_psa( mbedtls_cipher_context_t *ctx,
492 const mbedtls_cipher_info_t *cipher_info,
493 size_t taglen );
494#endif /* MBEDTLS_USE_PSA_CRYPTO */
495
496/**
497 * \brief This function returns the block size of the given cipher.
498 *
499 * \param ctx The context of the cipher. This must be initialized.
500 *
501 * \return The block size of the underlying cipher.
502 * \return \c 0 if \p ctx has not been initialized.
503 */
504static inline unsigned int mbedtls_cipher_get_block_size(
505 const mbedtls_cipher_context_t *ctx )
506{
507 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
508 if( ctx->cipher_info == NULL )
509 return 0;
510
511 return ctx->cipher_info->block_size;
512}
513
514/**
515 * \brief This function returns the mode of operation for
516 * the cipher. For example, MBEDTLS_MODE_CBC.
517 *
518 * \param ctx The context of the cipher. This must be initialized.
519 *
520 * \return The mode of operation.
521 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
522 */
523static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
524 const mbedtls_cipher_context_t *ctx )
525{
526 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, MBEDTLS_MODE_NONE );
527 if( ctx->cipher_info == NULL )
528 return MBEDTLS_MODE_NONE;
529
530 return ctx->cipher_info->mode;
531}
532
533/**
534 * \brief This function returns the size of the IV or nonce
535 * of the cipher, in Bytes.
536 *
537 * \param ctx The context of the cipher. This must be initialized.
538 *
539 * \return The recommended IV size if no IV has been set.
540 * \return \c 0 for ciphers not using an IV or a nonce.
541 * \return The actual size if an IV has been set.
542 */
543static inline int mbedtls_cipher_get_iv_size(
544 const mbedtls_cipher_context_t *ctx )
545{
546 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
547 if( ctx->cipher_info == NULL )
548 return 0;
549
550 if( ctx->iv_size != 0 )
551 return (int) ctx->iv_size;
552
553 return (int) ctx->cipher_info->iv_size;
554}
555
556/**
557 * \brief This function returns the type of the given cipher.
558 *
559 * \param ctx The context of the cipher. This must be initialized.
560 *
561 * \return The type of the cipher.
562 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
563 */
564static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
565 const mbedtls_cipher_context_t *ctx )
566{
567 MBEDTLS_INTERNAL_VALIDATE_RET(
568 ctx != NULL, MBEDTLS_CIPHER_NONE );
569 if( ctx->cipher_info == NULL )
570 return MBEDTLS_CIPHER_NONE;
571
572 return ctx->cipher_info->type;
573}
574
575/**
576 * \brief This function returns the name of the given cipher
577 * as a string.
578 *
579 * \param ctx The context of the cipher. This must be initialized.
580 *
581 * \return The name of the cipher.
582 * \return NULL if \p ctx has not been not initialized.
583 */
584static inline const char *mbedtls_cipher_get_name(
585 const mbedtls_cipher_context_t *ctx )
586{
587 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
588 if( ctx->cipher_info == NULL )
589 return 0;
590
591 return ctx->cipher_info->name;
592}
593
594/**
595 * \brief This function returns the key length of the cipher.
596 *
597 * \param ctx The context of the cipher. This must be initialized.
598 *
599 * \return The key length of the cipher in bits.
600 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
601 * initialized.
602 */
603static inline int mbedtls_cipher_get_key_bitlen(
604 const mbedtls_cipher_context_t *ctx )
605{
606 MBEDTLS_INTERNAL_VALIDATE_RET(
607 ctx != NULL, MBEDTLS_KEY_LENGTH_NONE );
608 if( ctx->cipher_info == NULL )
610
611 return (int) ctx->cipher_info->key_bitlen;
612}
613
614/**
615 * \brief This function returns the operation of the given cipher.
616 *
617 * \param ctx The context of the cipher. This must be initialized.
618 *
619 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
620 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
621 */
622static inline mbedtls_operation_t mbedtls_cipher_get_operation(
623 const mbedtls_cipher_context_t *ctx )
624{
625 MBEDTLS_INTERNAL_VALIDATE_RET(
626 ctx != NULL, MBEDTLS_OPERATION_NONE );
627 if( ctx->cipher_info == NULL )
628 return MBEDTLS_OPERATION_NONE;
629
630 return ctx->operation;
631}
632
633/**
634 * \brief This function sets the key to use with the given context.
635 *
636 * \param ctx The generic cipher context. This must be initialized and
637 * bound to a cipher information structure.
638 * \param key The key to use. This must be a readable buffer of at
639 * least \p key_bitlen Bits.
640 * \param key_bitlen The key length to use, in Bits.
641 * \param operation The operation that the key will be used for:
642 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
643 *
644 * \return \c 0 on success.
645 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
646 * parameter-verification failure.
647 * \return A cipher-specific error code on failure.
648 */
650 const unsigned char *key,
651 int key_bitlen,
652 const mbedtls_operation_t operation );
653
654#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
655/**
656 * \brief This function sets the padding mode, for cipher modes
657 * that use padding.
658 *
659 * The default passing mode is PKCS7 padding.
660 *
661 * \param ctx The generic cipher context. This must be initialized and
662 * bound to a cipher information structure.
663 * \param mode The padding mode.
664 *
665 * \return \c 0 on success.
666 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
667 * if the selected padding mode is not supported.
668 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
669 * does not support padding.
670 */
671int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
673#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
674
675/**
676 * \brief This function sets the initialization vector (IV)
677 * or nonce.
678 *
679 * \note Some ciphers do not use IVs nor nonce. For these
680 * ciphers, this function has no effect.
681 *
682 * \param ctx The generic cipher context. This must be initialized and
683 * bound to a cipher information structure.
684 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
685 * must be a readable buffer of at least \p iv_len Bytes.
686 * \param iv_len The IV length for ciphers with variable-size IV.
687 * This parameter is discarded by ciphers with fixed-size IV.
688 *
689 * \return \c 0 on success.
690 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
691 * parameter-verification failure.
692 */
694 const unsigned char *iv,
695 size_t iv_len );
696
697/**
698 * \brief This function resets the cipher state.
699 *
700 * \param ctx The generic cipher context. This must be initialized.
701 *
702 * \return \c 0 on success.
703 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
704 * parameter-verification failure.
705 */
707
708#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
709/**
710 * \brief This function adds additional data for AEAD ciphers.
711 * Currently supported with GCM and ChaCha20+Poly1305.
712 * This must be called exactly once, after
713 * mbedtls_cipher_reset().
714 *
715 * \param ctx The generic cipher context. This must be initialized.
716 * \param ad The additional data to use. This must be a readable
717 * buffer of at least \p ad_len Bytes.
718 * \param ad_len The length of \p ad in Bytes.
719 *
720 * \return \c 0 on success.
721 * \return A specific error code on failure.
722 */
723int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
724 const unsigned char *ad, size_t ad_len );
725#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
726
727/**
728 * \brief The generic cipher update function. It encrypts or
729 * decrypts using the given cipher context. Writes as
730 * many block-sized blocks of data as possible to output.
731 * Any data that cannot be written immediately is either
732 * added to the next block, or flushed when
733 * mbedtls_cipher_finish() is called.
734 * Exception: For MBEDTLS_MODE_ECB, expects a single block
735 * in size. For example, 16 Bytes for AES.
736 *
737 * \note If the underlying cipher is used in GCM mode, all calls
738 * to this function, except for the last one before
739 * mbedtls_cipher_finish(), must have \p ilen as a
740 * multiple of the block size of the cipher.
741 *
742 * \param ctx The generic cipher context. This must be initialized and
743 * bound to a key.
744 * \param input The buffer holding the input data. This must be a
745 * readable buffer of at least \p ilen Bytes.
746 * \param ilen The length of the input data.
747 * \param output The buffer for the output data. This must be able to
748 * hold at least `ilen + block_size`. This must not be the
749 * same buffer as \p input.
750 * \param olen The length of the output data, to be updated with the
751 * actual number of Bytes written. This must not be
752 * \c NULL.
753 *
754 * \return \c 0 on success.
755 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
756 * parameter-verification failure.
757 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
758 * unsupported mode for a cipher.
759 * \return A cipher-specific error code on failure.
760 */
762 const unsigned char *input,
763 size_t ilen, unsigned char *output,
764 size_t *olen );
765
766/**
767 * \brief The generic cipher finalization function. If data still
768 * needs to be flushed from an incomplete block, the data
769 * contained in it is padded to the size of
770 * the last block, and written to the \p output buffer.
771 *
772 * \param ctx The generic cipher context. This must be initialized and
773 * bound to a key.
774 * \param output The buffer to write data to. This needs to be a writable
775 * buffer of at least \p block_size Bytes.
776 * \param olen The length of the data written to the \p output buffer.
777 * This may not be \c NULL.
778 *
779 * \return \c 0 on success.
780 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
781 * parameter-verification failure.
782 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
783 * expecting a full block but not receiving one.
784 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
785 * while decrypting.
786 * \return A cipher-specific error code on failure.
787 */
789 unsigned char *output, size_t *olen );
790
791#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
792/**
793 * \brief This function writes a tag for AEAD ciphers.
794 * Currently supported with GCM and ChaCha20+Poly1305.
795 * This must be called after mbedtls_cipher_finish().
796 *
797 * \param ctx The generic cipher context. This must be initialized,
798 * bound to a key, and have just completed a cipher
799 * operation through mbedtls_cipher_finish() the tag for
800 * which should be written.
801 * \param tag The buffer to write the tag to. This must be a writable
802 * buffer of at least \p tag_len Bytes.
803 * \param tag_len The length of the tag to write.
804 *
805 * \return \c 0 on success.
806 * \return A specific error code on failure.
807 */
808int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
809 unsigned char *tag, size_t tag_len );
810
811/**
812 * \brief This function checks the tag for AEAD ciphers.
813 * Currently supported with GCM and ChaCha20+Poly1305.
814 * This must be called after mbedtls_cipher_finish().
815 *
816 * \param ctx The generic cipher context. This must be initialized.
817 * \param tag The buffer holding the tag. This must be a readable
818 * buffer of at least \p tag_len Bytes.
819 * \param tag_len The length of the tag to check.
820 *
821 * \return \c 0 on success.
822 * \return A specific error code on failure.
823 */
824int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
825 const unsigned char *tag, size_t tag_len );
826#endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
827
828/**
829 * \brief The generic all-in-one encryption/decryption function,
830 * for all ciphers except AEAD constructs.
831 *
832 * \param ctx The generic cipher context. This must be initialized.
833 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
834 * This must be a readable buffer of at least \p iv_len
835 * Bytes.
836 * \param iv_len The IV length for ciphers with variable-size IV.
837 * This parameter is discarded by ciphers with fixed-size
838 * IV.
839 * \param input The buffer holding the input data. This must be a
840 * readable buffer of at least \p ilen Bytes.
841 * \param ilen The length of the input data in Bytes.
842 * \param output The buffer for the output data. This must be able to
843 * hold at least `ilen + block_size`. This must not be the
844 * same buffer as \p input.
845 * \param olen The length of the output data, to be updated with the
846 * actual number of Bytes written. This must not be
847 * \c NULL.
848 *
849 * \note Some ciphers do not use IVs nor nonce. For these
850 * ciphers, use \p iv = NULL and \p iv_len = 0.
851 *
852 * \return \c 0 on success.
853 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
854 * parameter-verification failure.
855 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
856 * expecting a full block but not receiving one.
857 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
858 * while decrypting.
859 * \return A cipher-specific error code on failure.
860 */
862 const unsigned char *iv, size_t iv_len,
863 const unsigned char *input, size_t ilen,
864 unsigned char *output, size_t *olen );
865
866#if defined(MBEDTLS_CIPHER_MODE_AEAD)
867#if ! defined(MBEDTLS_DEPRECATED_REMOVED)
868#if defined(MBEDTLS_DEPRECATED_WARNING)
869#define MBEDTLS_DEPRECATED __attribute__((deprecated))
870#else
871#define MBEDTLS_DEPRECATED
872#endif /* MBEDTLS_DEPRECATED_WARNING */
873/**
874 * \brief The generic authenticated encryption (AEAD) function.
875 *
876 * \deprecated Superseded by mbedtls_cipher_auth_encrypt_ext().
877 *
878 * \note This function only supports AEAD algorithms, not key
879 * wrapping algorithms such as NIST_KW; for this, see
880 * mbedtls_cipher_auth_encrypt_ext().
881 *
882 * \param ctx The generic cipher context. This must be initialized and
883 * bound to a key associated with an AEAD algorithm.
884 * \param iv The nonce to use. This must be a readable buffer of
885 * at least \p iv_len Bytes and must not be \c NULL.
886 * \param iv_len The length of the nonce. This must satisfy the
887 * constraints imposed by the AEAD cipher used.
888 * \param ad The additional data to authenticate. This must be a
889 * readable buffer of at least \p ad_len Bytes, and may
890 * be \c NULL is \p ad_len is \c 0.
891 * \param ad_len The length of \p ad.
892 * \param input The buffer holding the input data. This must be a
893 * readable buffer of at least \p ilen Bytes, and may be
894 * \c NULL if \p ilen is \c 0.
895 * \param ilen The length of the input data.
896 * \param output The buffer for the output data. This must be a
897 * writable buffer of at least \p ilen Bytes, and must
898 * not be \c NULL.
899 * \param olen This will be filled with the actual number of Bytes
900 * written to the \p output buffer. This must point to a
901 * writable object of type \c size_t.
902 * \param tag The buffer for the authentication tag. This must be a
903 * writable buffer of at least \p tag_len Bytes. See note
904 * below regarding restrictions with PSA-based contexts.
905 * \param tag_len The desired length of the authentication tag. This
906 * must match the constraints imposed by the AEAD cipher
907 * used, and in particular must not be \c 0.
908 *
909 * \note If the context is based on PSA (that is, it was set up
910 * with mbedtls_cipher_setup_psa()), then it is required
911 * that \c tag == output + ilen. That is, the tag must be
912 * appended to the ciphertext as recommended by RFC 5116.
913 *
914 * \return \c 0 on success.
915 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
916 * parameter-verification failure.
917 * \return A cipher-specific error code on failure.
918 */
919int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
920 const unsigned char *iv, size_t iv_len,
921 const unsigned char *ad, size_t ad_len,
922 const unsigned char *input, size_t ilen,
923 unsigned char *output, size_t *olen,
924 unsigned char *tag, size_t tag_len )
925 MBEDTLS_DEPRECATED;
926
927/**
928 * \brief The generic authenticated decryption (AEAD) function.
929 *
930 * \deprecated Superseded by mbedtls_cipher_auth_decrypt_ext().
931 *
932 * \note This function only supports AEAD algorithms, not key
933 * wrapping algorithms such as NIST_KW; for this, see
934 * mbedtls_cipher_auth_decrypt_ext().
935 *
936 * \note If the data is not authentic, then the output buffer
937 * is zeroed out to prevent the unauthentic plaintext being
938 * used, making this interface safer.
939 *
940 * \param ctx The generic cipher context. This must be initialized and
941 * bound to a key associated with an AEAD algorithm.
942 * \param iv The nonce to use. This must be a readable buffer of
943 * at least \p iv_len Bytes and must not be \c NULL.
944 * \param iv_len The length of the nonce. This must satisfy the
945 * constraints imposed by the AEAD cipher used.
946 * \param ad The additional data to authenticate. This must be a
947 * readable buffer of at least \p ad_len Bytes, and may
948 * be \c NULL is \p ad_len is \c 0.
949 * \param ad_len The length of \p ad.
950 * \param input The buffer holding the input data. This must be a
951 * readable buffer of at least \p ilen Bytes, and may be
952 * \c NULL if \p ilen is \c 0.
953 * \param ilen The length of the input data.
954 * \param output The buffer for the output data. This must be a
955 * writable buffer of at least \p ilen Bytes, and must
956 * not be \c NULL.
957 * \param olen This will be filled with the actual number of Bytes
958 * written to the \p output buffer. This must point to a
959 * writable object of type \c size_t.
960 * \param tag The buffer for the authentication tag. This must be a
961 * readable buffer of at least \p tag_len Bytes. See note
962 * below regarding restrictions with PSA-based contexts.
963 * \param tag_len The length of the authentication tag. This must match
964 * the constraints imposed by the AEAD cipher used, and in
965 * particular must not be \c 0.
966 *
967 * \note If the context is based on PSA (that is, it was set up
968 * with mbedtls_cipher_setup_psa()), then it is required
969 * that \c tag == input + len. That is, the tag must be
970 * appended to the ciphertext as recommended by RFC 5116.
971 *
972 * \return \c 0 on success.
973 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
974 * parameter-verification failure.
975 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
976 * \return A cipher-specific error code on failure.
977 */
978int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
979 const unsigned char *iv, size_t iv_len,
980 const unsigned char *ad, size_t ad_len,
981 const unsigned char *input, size_t ilen,
982 unsigned char *output, size_t *olen,
983 const unsigned char *tag, size_t tag_len )
984 MBEDTLS_DEPRECATED;
985#undef MBEDTLS_DEPRECATED
986#endif /* MBEDTLS_DEPRECATED_REMOVED */
987#endif /* MBEDTLS_CIPHER_MODE_AEAD */
988
989#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
990/**
991 * \brief The authenticated encryption (AEAD/NIST_KW) function.
992 *
993 * \note For AEAD modes, the tag will be appended to the
994 * ciphertext, as recommended by RFC 5116.
995 * (NIST_KW doesn't have a separate tag.)
996 *
997 * \param ctx The generic cipher context. This must be initialized and
998 * bound to a key, with an AEAD algorithm or NIST_KW.
999 * \param iv The nonce to use. This must be a readable buffer of
1000 * at least \p iv_len Bytes and may be \c NULL if \p
1001 * iv_len is \c 0.
1002 * \param iv_len The length of the nonce. For AEAD ciphers, this must
1003 * satisfy the constraints imposed by the cipher used.
1004 * For NIST_KW, this must be \c 0.
1005 * \param ad The additional data to authenticate. This must be a
1006 * readable buffer of at least \p ad_len Bytes, and may
1007 * be \c NULL is \p ad_len is \c 0.
1008 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
1009 * \param input The buffer holding the input data. This must be a
1010 * readable buffer of at least \p ilen Bytes, and may be
1011 * \c NULL if \p ilen is \c 0.
1012 * \param ilen The length of the input data.
1013 * \param output The buffer for the output data. This must be a
1014 * writable buffer of at least \p output_len Bytes, and
1015 * must not be \c NULL.
1016 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1017 * ciphers, this must be at least \p ilen + \p tag_len.
1018 * For NIST_KW, this must be at least \p ilen + 8
1019 * (rounded up to a multiple of 8 if KWP is used);
1020 * \p ilen + 15 is always a safe value.
1021 * \param olen This will be filled with the actual number of Bytes
1022 * written to the \p output buffer. This must point to a
1023 * writable object of type \c size_t.
1024 * \param tag_len The desired length of the authentication tag. For AEAD
1025 * ciphers, this must match the constraints imposed by
1026 * the cipher used, and in particular must not be \c 0.
1027 * For NIST_KW, this must be \c 0.
1028 *
1029 * \return \c 0 on success.
1030 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1031 * parameter-verification failure.
1032 * \return A cipher-specific error code on failure.
1033 */
1034int mbedtls_cipher_auth_encrypt_ext( mbedtls_cipher_context_t *ctx,
1035 const unsigned char *iv, size_t iv_len,
1036 const unsigned char *ad, size_t ad_len,
1037 const unsigned char *input, size_t ilen,
1038 unsigned char *output, size_t output_len,
1039 size_t *olen, size_t tag_len );
1040
1041/**
1042 * \brief The authenticated encryption (AEAD/NIST_KW) function.
1043 *
1044 * \note If the data is not authentic, then the output buffer
1045 * is zeroed out to prevent the unauthentic plaintext being
1046 * used, making this interface safer.
1047 *
1048 * \note For AEAD modes, the tag must be appended to the
1049 * ciphertext, as recommended by RFC 5116.
1050 * (NIST_KW doesn't have a separate tag.)
1051 *
1052 * \param ctx The generic cipher context. This must be initialized and
1053 * bound to a key, with an AEAD algorithm or NIST_KW.
1054 * \param iv The nonce to use. This must be a readable buffer of
1055 * at least \p iv_len Bytes and may be \c NULL if \p
1056 * iv_len is \c 0.
1057 * \param iv_len The length of the nonce. For AEAD ciphers, this must
1058 * satisfy the constraints imposed by the cipher used.
1059 * For NIST_KW, this must be \c 0.
1060 * \param ad The additional data to authenticate. This must be a
1061 * readable buffer of at least \p ad_len Bytes, and may
1062 * be \c NULL is \p ad_len is \c 0.
1063 * \param ad_len The length of \p ad. For NIST_KW, this must be \c 0.
1064 * \param input The buffer holding the input data. This must be a
1065 * readable buffer of at least \p ilen Bytes, and may be
1066 * \c NULL if \p ilen is \c 0.
1067 * \param ilen The length of the input data. For AEAD ciphers this
1068 * must be at least \p tag_len. For NIST_KW this must be
1069 * at least \c 8.
1070 * \param output The buffer for the output data. This must be a
1071 * writable buffer of at least \p output_len Bytes, and
1072 * may be \c NULL if \p output_len is \c 0.
1073 * \param output_len The length of the \p output buffer in Bytes. For AEAD
1074 * ciphers, this must be at least \p ilen - \p tag_len.
1075 * For NIST_KW, this must be at least \p ilen - 8.
1076 * \param olen This will be filled with the actual number of Bytes
1077 * written to the \p output buffer. This must point to a
1078 * writable object of type \c size_t.
1079 * \param tag_len The actual length of the authentication tag. For AEAD
1080 * ciphers, this must match the constraints imposed by
1081 * the cipher used, and in particular must not be \c 0.
1082 * For NIST_KW, this must be \c 0.
1083 *
1084 * \return \c 0 on success.
1085 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
1086 * parameter-verification failure.
1087 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
1088 * \return A cipher-specific error code on failure.
1089 */
1090int mbedtls_cipher_auth_decrypt_ext( mbedtls_cipher_context_t *ctx,
1091 const unsigned char *iv, size_t iv_len,
1092 const unsigned char *ad, size_t ad_len,
1093 const unsigned char *input, size_t ilen,
1094 unsigned char *output, size_t output_len,
1095 size_t *olen, size_t tag_len );
1096#endif /* MBEDTLS_CIPHER_MODE_AEAD || MBEDTLS_NIST_KW_C */
1097
1098/// \}
1099/// \}
1100
1101#ifdef __cplusplus
1102}
1103#endif
1104
1105#endif /* MBEDTLS_CIPHER_H */
Configuration options (set of defines)
int mbedtls_cipher_setup(mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info)
This function initializes a cipher context for use with the given cipher primitive.
mbedtls_cipher_type_t
Supported {cipher type, cipher mode} pairs.
Definition: cipher.h:109
int mbedtls_cipher_setkey(mbedtls_cipher_context_t *ctx, const unsigned char *key, int key_bitlen, const mbedtls_operation_t operation)
This function sets the key to use with the given context.
int mbedtls_cipher_crypt(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
The generic all-in-one encryption/decryption function, for all ciphers except AEAD constructs.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_type(const mbedtls_cipher_type_t cipher_type)
This function retrieves the cipher-information structure associated with the given cipher type.
int mbedtls_cipher_reset(mbedtls_cipher_context_t *ctx)
This function resets the cipher state.
int mbedtls_cipher_set_iv(mbedtls_cipher_context_t *ctx, const unsigned char *iv, size_t iv_len)
This function sets the initialization vector (IV) or nonce.
mbedtls_cipher_padding_t
Supported cipher padding types.
Definition: cipher.h:210
int mbedtls_cipher_finish(mbedtls_cipher_context_t *ctx, unsigned char *output, size_t *olen)
The generic cipher finalization function.
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_values(const mbedtls_cipher_id_t cipher_id, int key_bitlen, const mbedtls_cipher_mode_t mode)
This function retrieves the cipher-information structure associated with the given cipher ID,...
void mbedtls_cipher_init(mbedtls_cipher_context_t *ctx)
This function initializes a cipher_context as NONE.
void mbedtls_cipher_free(mbedtls_cipher_context_t *ctx)
This function frees and clears the cipher-specific context of ctx.
int mbedtls_cipher_update(mbedtls_cipher_context_t *ctx, const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen)
The generic cipher update function.
mbedtls_operation_t
Type of operation.
Definition: cipher.h:219
const mbedtls_cipher_info_t * mbedtls_cipher_info_from_string(const char *cipher_name)
This function retrieves the cipher-information structure associated with the given cipher name.
const int * mbedtls_cipher_list(void)
This function retrieves the list of ciphers supported by the generic cipher module.
#define MBEDTLS_MAX_BLOCK_LENGTH
Maximum block size of any cipher, in Bytes.
Definition: cipher.h:246
mbedtls_cipher_mode_t
Supported cipher modes.
Definition: cipher.h:193
#define MBEDTLS_MAX_IV_LENGTH
Maximum length of any IV, in Bytes.
Definition: cipher.h:240
mbedtls_cipher_id_t
Supported cipher types.
Definition: cipher.h:89
@ MBEDTLS_CIPHER_AES_128_ECB
AES cipher with 128-bit ECB mode.
Definition: cipher.h:112
@ MBEDTLS_CIPHER_ARIA_256_CTR
Aria cipher with 256-bit key and CTR mode.
Definition: cipher.h:170
@ MBEDTLS_CIPHER_CAMELLIA_128_GCM
Camellia cipher with 128-bit GCM mode.
Definition: cipher.h:139
@ MBEDTLS_CIPHER_AES_128_XTS
AES 128-bit cipher in XTS block mode.
Definition: cipher.h:180
@ MBEDTLS_CIPHER_CHACHA20
ChaCha20 stream cipher.
Definition: cipher.h:182
@ MBEDTLS_CIPHER_DES_EDE3_CBC
DES cipher with EDE3 CBC mode.
Definition: cipher.h:147
@ MBEDTLS_CIPHER_DES_ECB
DES cipher with ECB mode.
Definition: cipher.h:142
@ MBEDTLS_CIPHER_ARIA_128_GCM
Aria cipher with 128-bit key and GCM mode.
Definition: cipher.h:171
@ MBEDTLS_CIPHER_AES_128_CBC
AES cipher with 128-bit CBC mode.
Definition: cipher.h:115
@ MBEDTLS_CIPHER_AES_192_GCM
AES cipher with 192-bit GCM mode.
Definition: cipher.h:125
@ MBEDTLS_CIPHER_BLOWFISH_CTR
Blowfish cipher with CTR mode.
Definition: cipher.h:151
@ MBEDTLS_CIPHER_AES_128_OFB
AES 128-bit cipher in OFB mode.
Definition: cipher.h:177
@ MBEDTLS_CIPHER_ARIA_192_ECB
Aria cipher with 192-bit key and ECB mode.
Definition: cipher.h:160
@ MBEDTLS_CIPHER_CAMELLIA_256_GCM
Camellia cipher with 256-bit GCM mode.
Definition: cipher.h:141
@ MBEDTLS_CIPHER_DES_EDE_ECB
DES cipher with EDE ECB mode.
Definition: cipher.h:144
@ MBEDTLS_CIPHER_BLOWFISH_CFB64
Blowfish cipher with CFB64 mode.
Definition: cipher.h:150
@ MBEDTLS_CIPHER_ARIA_256_CFB128
Aria cipher with 256-bit key and CFB-128 mode.
Definition: cipher.h:167
@ MBEDTLS_CIPHER_ARIA_192_CBC
Aria cipher with 192-bit key and CBC mode.
Definition: cipher.h:163
@ MBEDTLS_CIPHER_CAMELLIA_192_CBC
Camellia cipher with 192-bit CBC mode.
Definition: cipher.h:131
@ MBEDTLS_CIPHER_ARIA_128_CTR
Aria cipher with 128-bit key and CTR mode.
Definition: cipher.h:168
@ MBEDTLS_CIPHER_ARIA_192_CCM
Aria cipher with 192-bit key and CCM mode.
Definition: cipher.h:175
@ MBEDTLS_CIPHER_CAMELLIA_192_GCM
Camellia cipher with 192-bit GCM mode.
Definition: cipher.h:140
@ MBEDTLS_CIPHER_AES_192_OFB
AES 192-bit cipher in OFB mode.
Definition: cipher.h:178
@ MBEDTLS_CIPHER_AES_256_ECB
AES cipher with 256-bit ECB mode.
Definition: cipher.h:114
@ MBEDTLS_CIPHER_AES_256_CTR
AES cipher with 256-bit CTR mode.
Definition: cipher.h:123
@ MBEDTLS_CIPHER_AES_192_CCM
AES cipher with 192-bit CCM mode.
Definition: cipher.h:154
@ MBEDTLS_CIPHER_AES_128_CFB128
AES cipher with 128-bit CFB128 mode.
Definition: cipher.h:118
@ MBEDTLS_CIPHER_CAMELLIA_192_CFB128
Camellia cipher with 192-bit CFB128 mode.
Definition: cipher.h:134
@ MBEDTLS_CIPHER_CAMELLIA_128_CCM
Camellia cipher with 128-bit CCM mode.
Definition: cipher.h:156
@ MBEDTLS_CIPHER_AES_128_CTR
AES cipher with 128-bit CTR mode.
Definition: cipher.h:121
@ MBEDTLS_CIPHER_ARIA_192_GCM
Aria cipher with 192-bit key and GCM mode.
Definition: cipher.h:172
@ MBEDTLS_CIPHER_AES_256_XTS
AES 256-bit cipher in XTS block mode.
Definition: cipher.h:181
@ MBEDTLS_CIPHER_AES_192_CFB128
AES cipher with 192-bit CFB128 mode.
Definition: cipher.h:119
@ MBEDTLS_CIPHER_ARIA_256_ECB
Aria cipher with 256-bit key and ECB mode.
Definition: cipher.h:161
@ MBEDTLS_CIPHER_CAMELLIA_256_CCM
Camellia cipher with 256-bit CCM mode.
Definition: cipher.h:158
@ MBEDTLS_CIPHER_AES_256_GCM
AES cipher with 256-bit GCM mode.
Definition: cipher.h:126
@ MBEDTLS_CIPHER_DES_CBC
DES cipher with CBC mode.
Definition: cipher.h:143
@ MBEDTLS_CIPHER_CAMELLIA_128_CFB128
Camellia cipher with 128-bit CFB128 mode.
Definition: cipher.h:133
@ MBEDTLS_CIPHER_CAMELLIA_128_CBC
Camellia cipher with 128-bit CBC mode.
Definition: cipher.h:130
@ MBEDTLS_CIPHER_AES_256_CCM
AES cipher with 256-bit CCM mode.
Definition: cipher.h:155
@ MBEDTLS_CIPHER_CAMELLIA_256_CFB128
Camellia cipher with 256-bit CFB128 mode.
Definition: cipher.h:135
@ MBEDTLS_CIPHER_ARIA_192_CTR
Aria cipher with 192-bit key and CTR mode.
Definition: cipher.h:169
@ MBEDTLS_CIPHER_BLOWFISH_CBC
Blowfish cipher with CBC mode.
Definition: cipher.h:149
@ MBEDTLS_CIPHER_CAMELLIA_256_ECB
Camellia cipher with 256-bit ECB mode.
Definition: cipher.h:129
@ MBEDTLS_CIPHER_AES_256_KW
AES cipher with 256-bit NIST KW mode.
Definition: cipher.h:186
@ MBEDTLS_CIPHER_AES_128_GCM
AES cipher with 128-bit GCM mode.
Definition: cipher.h:124
@ MBEDTLS_CIPHER_CAMELLIA_192_ECB
Camellia cipher with 192-bit ECB mode.
Definition: cipher.h:128
@ MBEDTLS_CIPHER_AES_256_CFB128
AES cipher with 256-bit CFB128 mode.
Definition: cipher.h:120
@ MBEDTLS_CIPHER_NONE
Placeholder to mark the end of cipher-pair lists.
Definition: cipher.h:110
@ MBEDTLS_CIPHER_CHACHA20_POLY1305
ChaCha20-Poly1305 AEAD cipher.
Definition: cipher.h:183
@ MBEDTLS_CIPHER_CAMELLIA_128_ECB
Camellia cipher with 128-bit ECB mode.
Definition: cipher.h:127
@ MBEDTLS_CIPHER_AES_192_CBC
AES cipher with 192-bit CBC mode.
Definition: cipher.h:116
@ MBEDTLS_CIPHER_CAMELLIA_192_CCM
Camellia cipher with 192-bit CCM mode.
Definition: cipher.h:157
@ MBEDTLS_CIPHER_ARIA_128_CCM
Aria cipher with 128-bit key and CCM mode.
Definition: cipher.h:174
@ MBEDTLS_CIPHER_AES_192_CTR
AES cipher with 192-bit CTR mode.
Definition: cipher.h:122
@ MBEDTLS_CIPHER_AES_128_CCM
AES cipher with 128-bit CCM mode.
Definition: cipher.h:153
@ MBEDTLS_CIPHER_DES_EDE_CBC
DES cipher with EDE CBC mode.
Definition: cipher.h:145
@ MBEDTLS_CIPHER_NULL
The identity stream cipher.
Definition: cipher.h:111
@ MBEDTLS_CIPHER_ARIA_256_CBC
Aria cipher with 256-bit key and CBC mode.
Definition: cipher.h:164
@ MBEDTLS_CIPHER_AES_256_OFB
AES 256-bit cipher in OFB mode.
Definition: cipher.h:179
@ MBEDTLS_CIPHER_ARIA_192_CFB128
Aria cipher with 192-bit key and CFB-128 mode.
Definition: cipher.h:166
@ MBEDTLS_CIPHER_CAMELLIA_128_CTR
Camellia cipher with 128-bit CTR mode.
Definition: cipher.h:136
@ MBEDTLS_CIPHER_BLOWFISH_ECB
Blowfish cipher with ECB mode.
Definition: cipher.h:148
@ MBEDTLS_CIPHER_AES_256_KWP
AES cipher with 256-bit NIST KWP mode.
Definition: cipher.h:189
@ MBEDTLS_CIPHER_AES_256_CBC
AES cipher with 256-bit CBC mode.
Definition: cipher.h:117
@ MBEDTLS_CIPHER_ARC4_128
RC4 cipher with 128-bit mode.
Definition: cipher.h:152
@ MBEDTLS_CIPHER_CAMELLIA_192_CTR
Camellia cipher with 192-bit CTR mode.
Definition: cipher.h:137
@ MBEDTLS_CIPHER_AES_128_KW
AES cipher with 128-bit NIST KW mode.
Definition: cipher.h:184
@ MBEDTLS_CIPHER_AES_192_KW
AES cipher with 192-bit NIST KW mode.
Definition: cipher.h:185
@ MBEDTLS_CIPHER_AES_192_KWP
AES cipher with 192-bit NIST KWP mode.
Definition: cipher.h:188
@ MBEDTLS_CIPHER_AES_192_ECB
AES cipher with 192-bit ECB mode.
Definition: cipher.h:113
@ MBEDTLS_CIPHER_ARIA_256_GCM
Aria cipher with 256-bit key and GCM mode.
Definition: cipher.h:173
@ MBEDTLS_CIPHER_AES_128_KWP
AES cipher with 128-bit NIST KWP mode.
Definition: cipher.h:187
@ MBEDTLS_CIPHER_DES_EDE3_ECB
DES cipher with EDE3 ECB mode.
Definition: cipher.h:146
@ MBEDTLS_CIPHER_ARIA_128_CBC
Aria cipher with 128-bit key and CBC mode.
Definition: cipher.h:162
@ MBEDTLS_CIPHER_CAMELLIA_256_CTR
Camellia cipher with 256-bit CTR mode.
Definition: cipher.h:138
@ MBEDTLS_CIPHER_ARIA_128_ECB
Aria cipher with 128-bit key and ECB mode.
Definition: cipher.h:159
@ MBEDTLS_CIPHER_CAMELLIA_256_CBC
Camellia cipher with 256-bit CBC mode.
Definition: cipher.h:132
@ MBEDTLS_CIPHER_ARIA_256_CCM
Aria cipher with 256-bit key and CCM mode.
Definition: cipher.h:176
@ MBEDTLS_CIPHER_ARIA_128_CFB128
Aria cipher with 128-bit key and CFB-128 mode.
Definition: cipher.h:165
@ MBEDTLS_PADDING_ZEROS
Zero padding (not reversible).
Definition: cipher.h:214
@ MBEDTLS_PADDING_ONE_AND_ZEROS
ISO/IEC 7816-4 padding.
Definition: cipher.h:212
@ MBEDTLS_PADDING_PKCS7
PKCS7 padding (default).
Definition: cipher.h:211
@ MBEDTLS_PADDING_ZEROS_AND_LEN
ANSI X.923 padding.
Definition: cipher.h:213
@ MBEDTLS_PADDING_NONE
Never pad (full blocks only).
Definition: cipher.h:215
@ MBEDTLS_DECRYPT
Operation type for decryption.
Definition: cipher.h:221
@ MBEDTLS_ENCRYPT
Operation type for encryption.
Definition: cipher.h:222
@ MBEDTLS_KEY_LENGTH_DES
Key length, in bits (including parity), for DES keys.
Definition: cipher.h:229
@ MBEDTLS_KEY_LENGTH_NONE
Undefined key length.
Definition: cipher.h:227
@ MBEDTLS_KEY_LENGTH_DES_EDE
Key length in bits, including parity, for DES in two-key EDE.
Definition: cipher.h:231
@ MBEDTLS_KEY_LENGTH_DES_EDE3
Key length in bits, including parity, for DES in three-key EDE.
Definition: cipher.h:233
@ MBEDTLS_MODE_ECB
The ECB cipher mode.
Definition: cipher.h:195
@ MBEDTLS_MODE_CCM
The CCM cipher mode.
Definition: cipher.h:202
@ MBEDTLS_MODE_STREAM
The stream cipher mode.
Definition: cipher.h:201
@ MBEDTLS_MODE_NONE
None.
Definition: cipher.h:194
@ MBEDTLS_MODE_CFB
The CFB cipher mode.
Definition: cipher.h:197
@ MBEDTLS_MODE_CTR
The CTR cipher mode.
Definition: cipher.h:199
@ MBEDTLS_MODE_GCM
The GCM cipher mode.
Definition: cipher.h:200
@ MBEDTLS_MODE_KW
The SP800-38F KW mode.
Definition: cipher.h:205
@ MBEDTLS_MODE_CBC
The CBC cipher mode.
Definition: cipher.h:196
@ MBEDTLS_MODE_OFB
The OFB cipher mode.
Definition: cipher.h:198
@ MBEDTLS_MODE_KWP
The SP800-38F KWP mode.
Definition: cipher.h:206
@ MBEDTLS_MODE_CHACHAPOLY
The ChaCha-Poly cipher mode.
Definition: cipher.h:204
@ MBEDTLS_MODE_XTS
The XTS cipher mode.
Definition: cipher.h:203
@ MBEDTLS_CIPHER_ID_3DES
The Triple DES cipher.
Definition: cipher.h:94
@ MBEDTLS_CIPHER_ID_CAMELLIA
The Camellia cipher.
Definition: cipher.h:95
@ MBEDTLS_CIPHER_ID_DES
The DES cipher.
Definition: cipher.h:93
@ MBEDTLS_CIPHER_ID_ARC4
The RC4 cipher.
Definition: cipher.h:97
@ MBEDTLS_CIPHER_ID_NULL
The identity cipher, treated as a stream cipher.
Definition: cipher.h:91
@ MBEDTLS_CIPHER_ID_AES
The AES cipher.
Definition: cipher.h:92
@ MBEDTLS_CIPHER_ID_ARIA
The Aria cipher.
Definition: cipher.h:98
@ MBEDTLS_CIPHER_ID_NONE
Placeholder to mark the end of cipher ID lists.
Definition: cipher.h:90
@ MBEDTLS_CIPHER_ID_CHACHA20
The ChaCha20 cipher.
Definition: cipher.h:99
@ MBEDTLS_CIPHER_ID_BLOWFISH
The Blowfish cipher.
Definition: cipher.h:96
Common and shared functions used by multiple modules in the Mbed TLS library.
Base cipher information.
Generic cipher context.
Definition: cipher.h:318
size_t iv_size
IV size in Bytes, for ciphers with variable-length IVs.
Definition: cipher.h:349
mbedtls_cmac_context_t * cmac_ctx
CMAC-specific context.
Definition: cipher.h:356
void * cipher_ctx
The cipher-specific context.
Definition: cipher.h:352
size_t unprocessed_len
Number of Bytes that have not been processed yet.
Definition: cipher.h:342
unsigned char unprocessed_data[16]
Buffer for input that has not been processed yet.
Definition: cipher.h:339
int key_bitlen
Key length to use.
Definition: cipher.h:323
const mbedtls_cipher_info_t * cipher_info
Information about the associated cipher.
Definition: cipher.h:320
mbedtls_operation_t operation
Operation that the key of the context has been initialized for.
Definition: cipher.h:328
unsigned char iv[16]
Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number for XTS-mode.
Definition: cipher.h:346
Cipher information.
Definition: cipher.h:276
mbedtls_cipher_type_t type
Full cipher identifier.
Definition: cipher.h:280
const mbedtls_cipher_base_t * base
Struct for base cipher information and functions.
Definition: cipher.h:310
unsigned int key_bitlen
The cipher key length, in bits.
Definition: cipher.h:289
const char * name
Name of the cipher.
Definition: cipher.h:292
int flags
Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating wh...
Definition: cipher.h:304
unsigned int block_size
The block size, in Bytes.
Definition: cipher.h:307
unsigned int iv_size
IV or nonce size, in Bytes.
Definition: cipher.h:298
mbedtls_cipher_mode_t mode
The cipher mode.
Definition: cipher.h:283
The CMAC context structure.
Definition: cmac.h:66