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