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 (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
12 * SPDX-License-Identifier: GPL-2.0
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 *
28 * This file is part of Mbed TLS (https://tls.mbed.org)
29 */
30
31 #ifndef MBEDTLS_CIPHER_H
32 #define MBEDTLS_CIPHER_H
33
34 #if !defined(MBEDTLS_CONFIG_FILE)
35 #include "config.h"
36 #else
37 #include MBEDTLS_CONFIG_FILE
38 #endif
39
40 #include <stddef.h>
41 #include "mbedtls/platform_util.h"
42
43 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
44 #define MBEDTLS_CIPHER_MODE_AEAD
45 #endif
46
47 #if defined(MBEDTLS_CIPHER_MODE_CBC)
48 #define MBEDTLS_CIPHER_MODE_WITH_PADDING
49 #endif
50
51 #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER) || \
52 defined(MBEDTLS_CHACHA20_C)
53 #define MBEDTLS_CIPHER_MODE_STREAM
54 #endif
55
56 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
57 !defined(inline) && !defined(__cplusplus)
58 #define inline __inline
59 #endif
60
61 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
62 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters. */
63 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
64 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
65 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
66 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
67 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid. For example, because it was freed. */
68
69 /* MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED is deprecated and should not be used. */
70 #define MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED -0x6400 /**< Cipher hardware accelerator failed. */
71
72 #define MBEDTLS_CIPHER_VARIABLE_IV_LEN 0x01 /**< Cipher accepts IVs of variable length. */
73 #define MBEDTLS_CIPHER_VARIABLE_KEY_LEN 0x02 /**< Cipher accepts keys of variable length. */
74
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78
79 /**
80 * \brief Supported cipher types.
81 *
82 * \warning RC4 and DES are considered weak ciphers and their use
83 * constitutes a security risk. Arm recommends considering stronger
84 * ciphers instead.
85 */
86 typedef enum {
87 MBEDTLS_CIPHER_ID_NONE = 0, /**< Placeholder to mark the end of cipher ID lists. */
88 MBEDTLS_CIPHER_ID_NULL, /**< The identity cipher, treated as a stream cipher. */
89 MBEDTLS_CIPHER_ID_AES, /**< The AES cipher. */
90 MBEDTLS_CIPHER_ID_DES, /**< The DES cipher. */
91 MBEDTLS_CIPHER_ID_3DES, /**< The Triple DES cipher. */
92 MBEDTLS_CIPHER_ID_CAMELLIA, /**< The Camellia cipher. */
93 MBEDTLS_CIPHER_ID_BLOWFISH, /**< The Blowfish cipher. */
94 MBEDTLS_CIPHER_ID_ARC4, /**< The RC4 cipher. */
95 MBEDTLS_CIPHER_ID_ARIA, /**< The Aria cipher. */
96 MBEDTLS_CIPHER_ID_CHACHA20, /**< The ChaCha20 cipher. */
97 } mbedtls_cipher_id_t;
98
99 /**
100 * \brief Supported {cipher type, cipher mode} pairs.
101 *
102 * \warning RC4 and DES are considered weak ciphers and their use
103 * constitutes a security risk. Arm recommends considering stronger
104 * ciphers instead.
105 */
106 typedef enum {
107 MBEDTLS_CIPHER_NONE = 0, /**< Placeholder to mark the end of cipher-pair lists. */
108 MBEDTLS_CIPHER_NULL, /**< The identity stream cipher. */
109 MBEDTLS_CIPHER_AES_128_ECB, /**< AES cipher with 128-bit ECB mode. */
110 MBEDTLS_CIPHER_AES_192_ECB, /**< AES cipher with 192-bit ECB mode. */
111 MBEDTLS_CIPHER_AES_256_ECB, /**< AES cipher with 256-bit ECB mode. */
112 MBEDTLS_CIPHER_AES_128_CBC, /**< AES cipher with 128-bit CBC mode. */
113 MBEDTLS_CIPHER_AES_192_CBC, /**< AES cipher with 192-bit CBC mode. */
114 MBEDTLS_CIPHER_AES_256_CBC, /**< AES cipher with 256-bit CBC mode. */
115 MBEDTLS_CIPHER_AES_128_CFB128, /**< AES cipher with 128-bit CFB128 mode. */
116 MBEDTLS_CIPHER_AES_192_CFB128, /**< AES cipher with 192-bit CFB128 mode. */
117 MBEDTLS_CIPHER_AES_256_CFB128, /**< AES cipher with 256-bit CFB128 mode. */
118 MBEDTLS_CIPHER_AES_128_CTR, /**< AES cipher with 128-bit CTR mode. */
119 MBEDTLS_CIPHER_AES_192_CTR, /**< AES cipher with 192-bit CTR mode. */
120 MBEDTLS_CIPHER_AES_256_CTR, /**< AES cipher with 256-bit CTR mode. */
121 MBEDTLS_CIPHER_AES_128_GCM, /**< AES cipher with 128-bit GCM mode. */
122 MBEDTLS_CIPHER_AES_192_GCM, /**< AES cipher with 192-bit GCM mode. */
123 MBEDTLS_CIPHER_AES_256_GCM, /**< AES cipher with 256-bit GCM mode. */
124 MBEDTLS_CIPHER_CAMELLIA_128_ECB, /**< Camellia cipher with 128-bit ECB mode. */
125 MBEDTLS_CIPHER_CAMELLIA_192_ECB, /**< Camellia cipher with 192-bit ECB mode. */
126 MBEDTLS_CIPHER_CAMELLIA_256_ECB, /**< Camellia cipher with 256-bit ECB mode. */
127 MBEDTLS_CIPHER_CAMELLIA_128_CBC, /**< Camellia cipher with 128-bit CBC mode. */
128 MBEDTLS_CIPHER_CAMELLIA_192_CBC, /**< Camellia cipher with 192-bit CBC mode. */
129 MBEDTLS_CIPHER_CAMELLIA_256_CBC, /**< Camellia cipher with 256-bit CBC mode. */
130 MBEDTLS_CIPHER_CAMELLIA_128_CFB128, /**< Camellia cipher with 128-bit CFB128 mode. */
131 MBEDTLS_CIPHER_CAMELLIA_192_CFB128, /**< Camellia cipher with 192-bit CFB128 mode. */
132 MBEDTLS_CIPHER_CAMELLIA_256_CFB128, /**< Camellia cipher with 256-bit CFB128 mode. */
133 MBEDTLS_CIPHER_CAMELLIA_128_CTR, /**< Camellia cipher with 128-bit CTR mode. */
134 MBEDTLS_CIPHER_CAMELLIA_192_CTR, /**< Camellia cipher with 192-bit CTR mode. */
135 MBEDTLS_CIPHER_CAMELLIA_256_CTR, /**< Camellia cipher with 256-bit CTR mode. */
136 MBEDTLS_CIPHER_CAMELLIA_128_GCM, /**< Camellia cipher with 128-bit GCM mode. */
137 MBEDTLS_CIPHER_CAMELLIA_192_GCM, /**< Camellia cipher with 192-bit GCM mode. */
138 MBEDTLS_CIPHER_CAMELLIA_256_GCM, /**< Camellia cipher with 256-bit GCM mode. */
139 MBEDTLS_CIPHER_DES_ECB, /**< DES cipher with ECB mode. */
140 MBEDTLS_CIPHER_DES_CBC, /**< DES cipher with CBC mode. */
141 MBEDTLS_CIPHER_DES_EDE_ECB, /**< DES cipher with EDE ECB mode. */
142 MBEDTLS_CIPHER_DES_EDE_CBC, /**< DES cipher with EDE CBC mode. */
143 MBEDTLS_CIPHER_DES_EDE3_ECB, /**< DES cipher with EDE3 ECB mode. */
144 MBEDTLS_CIPHER_DES_EDE3_CBC, /**< DES cipher with EDE3 CBC mode. */
145 MBEDTLS_CIPHER_BLOWFISH_ECB, /**< Blowfish cipher with ECB mode. */
146 MBEDTLS_CIPHER_BLOWFISH_CBC, /**< Blowfish cipher with CBC mode. */
147 MBEDTLS_CIPHER_BLOWFISH_CFB64, /**< Blowfish cipher with CFB64 mode. */
148 MBEDTLS_CIPHER_BLOWFISH_CTR, /**< Blowfish cipher with CTR mode. */
149 MBEDTLS_CIPHER_ARC4_128, /**< RC4 cipher with 128-bit mode. */
150 MBEDTLS_CIPHER_AES_128_CCM, /**< AES cipher with 128-bit CCM mode. */
151 MBEDTLS_CIPHER_AES_192_CCM, /**< AES cipher with 192-bit CCM mode. */
152 MBEDTLS_CIPHER_AES_256_CCM, /**< AES cipher with 256-bit CCM mode. */
153 MBEDTLS_CIPHER_CAMELLIA_128_CCM, /**< Camellia cipher with 128-bit CCM mode. */
154 MBEDTLS_CIPHER_CAMELLIA_192_CCM, /**< Camellia cipher with 192-bit CCM mode. */
155 MBEDTLS_CIPHER_CAMELLIA_256_CCM, /**< Camellia cipher with 256-bit CCM mode. */
156 MBEDTLS_CIPHER_ARIA_128_ECB, /**< Aria cipher with 128-bit key and ECB mode. */
157 MBEDTLS_CIPHER_ARIA_192_ECB, /**< Aria cipher with 192-bit key and ECB mode. */
158 MBEDTLS_CIPHER_ARIA_256_ECB, /**< Aria cipher with 256-bit key and ECB mode. */
159 MBEDTLS_CIPHER_ARIA_128_CBC, /**< Aria cipher with 128-bit key and CBC mode. */
160 MBEDTLS_CIPHER_ARIA_192_CBC, /**< Aria cipher with 192-bit key and CBC mode. */
161 MBEDTLS_CIPHER_ARIA_256_CBC, /**< Aria cipher with 256-bit key and CBC mode. */
162 MBEDTLS_CIPHER_ARIA_128_CFB128, /**< Aria cipher with 128-bit key and CFB-128 mode. */
163 MBEDTLS_CIPHER_ARIA_192_CFB128, /**< Aria cipher with 192-bit key and CFB-128 mode. */
164 MBEDTLS_CIPHER_ARIA_256_CFB128, /**< Aria cipher with 256-bit key and CFB-128 mode. */
165 MBEDTLS_CIPHER_ARIA_128_CTR, /**< Aria cipher with 128-bit key and CTR mode. */
166 MBEDTLS_CIPHER_ARIA_192_CTR, /**< Aria cipher with 192-bit key and CTR mode. */
167 MBEDTLS_CIPHER_ARIA_256_CTR, /**< Aria cipher with 256-bit key and CTR mode. */
168 MBEDTLS_CIPHER_ARIA_128_GCM, /**< Aria cipher with 128-bit key and GCM mode. */
169 MBEDTLS_CIPHER_ARIA_192_GCM, /**< Aria cipher with 192-bit key and GCM mode. */
170 MBEDTLS_CIPHER_ARIA_256_GCM, /**< Aria cipher with 256-bit key and GCM mode. */
171 MBEDTLS_CIPHER_ARIA_128_CCM, /**< Aria cipher with 128-bit key and CCM mode. */
172 MBEDTLS_CIPHER_ARIA_192_CCM, /**< Aria cipher with 192-bit key and CCM mode. */
173 MBEDTLS_CIPHER_ARIA_256_CCM, /**< Aria cipher with 256-bit key and CCM mode. */
174 MBEDTLS_CIPHER_AES_128_OFB, /**< AES 128-bit cipher in OFB mode. */
175 MBEDTLS_CIPHER_AES_192_OFB, /**< AES 192-bit cipher in OFB mode. */
176 MBEDTLS_CIPHER_AES_256_OFB, /**< AES 256-bit cipher in OFB mode. */
177 MBEDTLS_CIPHER_AES_128_XTS, /**< AES 128-bit cipher in XTS block mode. */
178 MBEDTLS_CIPHER_AES_256_XTS, /**< AES 256-bit cipher in XTS block mode. */
179 MBEDTLS_CIPHER_CHACHA20, /**< ChaCha20 stream cipher. */
180 MBEDTLS_CIPHER_CHACHA20_POLY1305, /**< ChaCha20-Poly1305 AEAD cipher. */
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_XTS, /**< The XTS cipher mode. */
195 MBEDTLS_MODE_CHACHAPOLY, /**< The ChaCha-Poly cipher mode. */
196 } mbedtls_cipher_mode_t;
197
198 /** Supported cipher padding types. */
199 typedef enum {
200 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default). */
201 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding. */
202 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding. */
203 MBEDTLS_PADDING_ZEROS, /**< Zero padding (not reversible). */
204 MBEDTLS_PADDING_NONE, /**< Never pad (full blocks only). */
205 } mbedtls_cipher_padding_t;
206
207 /** Type of operation. */
208 typedef enum {
209 MBEDTLS_OPERATION_NONE = -1,
210 MBEDTLS_DECRYPT = 0,
211 MBEDTLS_ENCRYPT,
212 } mbedtls_operation_t;
213
214 enum {
215 /** Undefined key length. */
216 MBEDTLS_KEY_LENGTH_NONE = 0,
217 /** Key length, in bits (including parity), for DES keys. */
218 MBEDTLS_KEY_LENGTH_DES = 64,
219 /** Key length in bits, including parity, for DES in two-key EDE. */
220 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
221 /** Key length in bits, including parity, for DES in three-key EDE. */
222 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
223 };
224
225 /** Maximum length of any IV, in Bytes. */
226 #define MBEDTLS_MAX_IV_LENGTH 16
227 /** Maximum block size of any cipher, in Bytes. */
228 #define MBEDTLS_MAX_BLOCK_LENGTH 16
229
230 /**
231 * Base cipher information (opaque struct).
232 */
233 typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
234
235 /**
236 * CMAC context (opaque struct).
237 */
238 typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
239
240 /**
241 * Cipher information. Allows calling cipher functions
242 * in a generic way.
243 */
244 typedef struct mbedtls_cipher_info_t
245 {
246 /** Full cipher identifier. For example,
247 * MBEDTLS_CIPHER_AES_256_CBC.
248 */
249 mbedtls_cipher_type_t type;
250
251 /** The cipher mode. For example, MBEDTLS_MODE_CBC. */
252 mbedtls_cipher_mode_t mode;
253
254 /** The cipher key length, in bits. This is the
255 * default length for variable sized ciphers.
256 * Includes parity bits for ciphers like DES.
257 */
258 unsigned int key_bitlen;
259
260 /** Name of the cipher. */
261 const char * name;
262
263 /** IV or nonce size, in Bytes.
264 * For ciphers that accept variable IV sizes,
265 * this is the recommended size.
266 */
267 unsigned int iv_size;
268
269 /** Bitflag comprised of MBEDTLS_CIPHER_VARIABLE_IV_LEN and
270 * MBEDTLS_CIPHER_VARIABLE_KEY_LEN indicating whether the
271 * cipher supports variable IV or variable key sizes, respectively.
272 */
273 int flags;
274
275 /** The block size, in Bytes. */
276 unsigned int block_size;
277
278 /** Struct for base cipher information and functions. */
279 const mbedtls_cipher_base_t *base;
280
281 } mbedtls_cipher_info_t;
282
283 /**
284 * Generic cipher context.
285 */
286 typedef struct mbedtls_cipher_context_t
287 {
288 /** Information about the associated cipher. */
289 const mbedtls_cipher_info_t *cipher_info;
290
291 /** Key length to use. */
292 int key_bitlen;
293
294 /** Operation that the key of the context has been
295 * initialized for.
296 */
297 mbedtls_operation_t operation;
298
299 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
300 /** Padding functions to use, if relevant for
301 * the specific cipher mode.
302 */
303 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
304 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
305 #endif
306
307 /** Buffer for input that has not been processed yet. */
308 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
309
310 /** Number of Bytes that have not been processed yet. */
311 size_t unprocessed_len;
312
313 /** Current IV or NONCE_COUNTER for CTR-mode, data unit (or sector) number
314 * for XTS-mode. */
315 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
316
317 /** IV size in Bytes, for ciphers with variable-length IVs. */
318 size_t iv_size;
319
320 /** The cipher-specific context. */
321 void *cipher_ctx;
322
323 #if defined(MBEDTLS_CMAC_C)
324 /** CMAC-specific context. */
325 mbedtls_cmac_context_t *cmac_ctx;
326 #endif
327 } mbedtls_cipher_context_t;
328
329 /**
330 * \brief This function retrieves the list of ciphers supported by the generic
331 * cipher module.
332 *
333 * \return A statically-allocated array of ciphers. The last entry
334 * is zero.
335 */
336 const int *mbedtls_cipher_list( void );
337
338 /**
339 * \brief This function retrieves the cipher-information
340 * structure associated with the given cipher name.
341 *
342 * \param cipher_name Name of the cipher to search for. This must not be
343 * \c NULL.
344 *
345 * \return The cipher information structure associated with the
346 * given \p cipher_name.
347 * \return \c NULL if the associated cipher information is not found.
348 */
349 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
350
351 /**
352 * \brief This function retrieves the cipher-information
353 * structure associated with the given cipher type.
354 *
355 * \param cipher_type Type of the cipher to search for.
356 *
357 * \return The cipher information structure associated with the
358 * given \p cipher_type.
359 * \return \c NULL if the associated cipher information is not found.
360 */
361 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
362
363 /**
364 * \brief This function retrieves the cipher-information
365 * structure associated with the given cipher ID,
366 * key size and mode.
367 *
368 * \param cipher_id The ID of the cipher to search for. For example,
369 * #MBEDTLS_CIPHER_ID_AES.
370 * \param key_bitlen The length of the key in bits.
371 * \param mode The cipher mode. For example, #MBEDTLS_MODE_CBC.
372 *
373 * \return The cipher information structure associated with the
374 * given \p cipher_id.
375 * \return \c NULL if the associated cipher information is not found.
376 */
377 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
378 int key_bitlen,
379 const mbedtls_cipher_mode_t mode );
380
381 /**
382 * \brief This function initializes a \p cipher_context as NONE.
383 *
384 * \param ctx The context to be initialized. This must not be \c NULL.
385 */
386 void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
387
388 /**
389 * \brief This function frees and clears the cipher-specific
390 * context of \p ctx. Freeing \p ctx itself remains the
391 * responsibility of the caller.
392 *
393 * \param ctx The context to be freed. If this is \c NULL, the
394 * function has no effect, otherwise this must point to an
395 * initialized context.
396 */
397 void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
398
399
400 /**
401 * \brief This function initializes and fills the cipher-context
402 * structure with the appropriate values. It also clears
403 * the structure.
404 *
405 * \param ctx The context to initialize. This must be initialized.
406 * \param cipher_info The cipher to use.
407 *
408 * \return \c 0 on success.
409 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
410 * parameter-verification failure.
411 * \return #MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
412 * cipher-specific context fails.
413 *
414 * \internal Currently, the function also clears the structure.
415 * In future versions, the caller will be required to call
416 * mbedtls_cipher_init() on the structure first.
417 */
418 int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx,
419 const mbedtls_cipher_info_t *cipher_info );
420
421 /**
422 * \brief This function returns the block size of the given cipher.
423 *
424 * \param ctx The context of the cipher. This must be initialized.
425 *
426 * \return The block size of the underlying cipher.
427 * \return \c 0 if \p ctx has not been initialized.
428 */
mbedtls_cipher_get_block_size(const mbedtls_cipher_context_t * ctx)429 static inline unsigned int mbedtls_cipher_get_block_size(
430 const mbedtls_cipher_context_t *ctx )
431 {
432 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
433 if( ctx->cipher_info == NULL )
434 return 0;
435
436 return ctx->cipher_info->block_size;
437 }
438
439 /**
440 * \brief This function returns the mode of operation for
441 * the cipher. For example, MBEDTLS_MODE_CBC.
442 *
443 * \param ctx The context of the cipher. This must be initialized.
444 *
445 * \return The mode of operation.
446 * \return #MBEDTLS_MODE_NONE if \p ctx has not been initialized.
447 */
mbedtls_cipher_get_cipher_mode(const mbedtls_cipher_context_t * ctx)448 static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode(
449 const mbedtls_cipher_context_t *ctx )
450 {
451 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, MBEDTLS_MODE_NONE );
452 if( ctx->cipher_info == NULL )
453 return MBEDTLS_MODE_NONE;
454
455 return ctx->cipher_info->mode;
456 }
457
458 /**
459 * \brief This function returns the size of the IV or nonce
460 * of the cipher, in Bytes.
461 *
462 * \param ctx The context of the cipher. This must be initialized.
463 *
464 * \return The recommended IV size if no IV has been set.
465 * \return \c 0 for ciphers not using an IV or a nonce.
466 * \return The actual size if an IV has been set.
467 */
mbedtls_cipher_get_iv_size(const mbedtls_cipher_context_t * ctx)468 static inline int mbedtls_cipher_get_iv_size(
469 const mbedtls_cipher_context_t *ctx )
470 {
471 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
472 if( ctx->cipher_info == NULL )
473 return 0;
474
475 if( ctx->iv_size != 0 )
476 return (int) ctx->iv_size;
477
478 return (int) ctx->cipher_info->iv_size;
479 }
480
481 /**
482 * \brief This function returns the type of the given cipher.
483 *
484 * \param ctx The context of the cipher. This must be initialized.
485 *
486 * \return The type of the cipher.
487 * \return #MBEDTLS_CIPHER_NONE if \p ctx has not been initialized.
488 */
mbedtls_cipher_get_type(const mbedtls_cipher_context_t * ctx)489 static inline mbedtls_cipher_type_t mbedtls_cipher_get_type(
490 const mbedtls_cipher_context_t *ctx )
491 {
492 MBEDTLS_INTERNAL_VALIDATE_RET(
493 ctx != NULL, MBEDTLS_CIPHER_NONE );
494 if( ctx->cipher_info == NULL )
495 return MBEDTLS_CIPHER_NONE;
496
497 return ctx->cipher_info->type;
498 }
499
500 /**
501 * \brief This function returns the name of the given cipher
502 * as a string.
503 *
504 * \param ctx The context of the cipher. This must be initialized.
505 *
506 * \return The name of the cipher.
507 * \return NULL if \p ctx has not been not initialized.
508 */
mbedtls_cipher_get_name(const mbedtls_cipher_context_t * ctx)509 static inline const char *mbedtls_cipher_get_name(
510 const mbedtls_cipher_context_t *ctx )
511 {
512 MBEDTLS_INTERNAL_VALIDATE_RET( ctx != NULL, 0 );
513 if( ctx->cipher_info == NULL )
514 return 0;
515
516 return ctx->cipher_info->name;
517 }
518
519 /**
520 * \brief This function returns the key length of the cipher.
521 *
522 * \param ctx The context of the cipher. This must be initialized.
523 *
524 * \return The key length of the cipher in bits.
525 * \return #MBEDTLS_KEY_LENGTH_NONE if ctx \p has not been
526 * initialized.
527 */
mbedtls_cipher_get_key_bitlen(const mbedtls_cipher_context_t * ctx)528 static inline int mbedtls_cipher_get_key_bitlen(
529 const mbedtls_cipher_context_t *ctx )
530 {
531 MBEDTLS_INTERNAL_VALIDATE_RET(
532 ctx != NULL, MBEDTLS_KEY_LENGTH_NONE );
533 if( ctx->cipher_info == NULL )
534 return MBEDTLS_KEY_LENGTH_NONE;
535
536 return (int) ctx->cipher_info->key_bitlen;
537 }
538
539 /**
540 * \brief This function returns the operation of the given cipher.
541 *
542 * \param ctx The context of the cipher. This must be initialized.
543 *
544 * \return The type of operation: #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
545 * \return #MBEDTLS_OPERATION_NONE if \p ctx has not been initialized.
546 */
mbedtls_cipher_get_operation(const mbedtls_cipher_context_t * ctx)547 static inline mbedtls_operation_t mbedtls_cipher_get_operation(
548 const mbedtls_cipher_context_t *ctx )
549 {
550 MBEDTLS_INTERNAL_VALIDATE_RET(
551 ctx != NULL, MBEDTLS_OPERATION_NONE );
552 if( ctx->cipher_info == NULL )
553 return MBEDTLS_OPERATION_NONE;
554
555 return ctx->operation;
556 }
557
558 /**
559 * \brief This function sets the key to use with the given context.
560 *
561 * \param ctx The generic cipher context. This must be initialized and
562 * bound to a cipher information structure.
563 * \param key The key to use. This must be a readable buffer of at
564 * least \p key_bitlen Bits.
565 * \param key_bitlen The key length to use, in Bits.
566 * \param operation The operation that the key will be used for:
567 * #MBEDTLS_ENCRYPT or #MBEDTLS_DECRYPT.
568 *
569 * \return \c 0 on success.
570 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
571 * parameter-verification failure.
572 * \return A cipher-specific error code on failure.
573 */
574 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx,
575 const unsigned char *key,
576 int key_bitlen,
577 const mbedtls_operation_t operation );
578
579 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
580 /**
581 * \brief This function sets the padding mode, for cipher modes
582 * that use padding.
583 *
584 * The default passing mode is PKCS7 padding.
585 *
586 * \param ctx The generic cipher context. This must be initialized and
587 * bound to a cipher information structure.
588 * \param mode The padding mode.
589 *
590 * \return \c 0 on success.
591 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
592 * if the selected padding mode is not supported.
593 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
594 * does not support padding.
595 */
596 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx,
597 mbedtls_cipher_padding_t mode );
598 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
599
600 /**
601 * \brief This function sets the initialization vector (IV)
602 * or nonce.
603 *
604 * \note Some ciphers do not use IVs nor nonce. For these
605 * ciphers, this function has no effect.
606 *
607 * \param ctx The generic cipher context. This must be initialized and
608 * bound to a cipher information structure.
609 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers. This
610 * must be a readable buffer of at least \p iv_len Bytes.
611 * \param iv_len The IV length for ciphers with variable-size IV.
612 * This parameter is discarded by ciphers with fixed-size IV.
613 *
614 * \return \c 0 on success.
615 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
616 * parameter-verification failure.
617 */
618 int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
619 const unsigned char *iv,
620 size_t iv_len );
621
622 /**
623 * \brief This function resets the cipher state.
624 *
625 * \param ctx The generic cipher context. This must be initialized.
626 *
627 * \return \c 0 on success.
628 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
629 * parameter-verification failure.
630 */
631 int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
632
633 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
634 /**
635 * \brief This function adds additional data for AEAD ciphers.
636 * Currently supported with GCM and ChaCha20+Poly1305.
637 * This must be called exactly once, after
638 * mbedtls_cipher_reset().
639 *
640 * \param ctx The generic cipher context. This must be initialized.
641 * \param ad The additional data to use. This must be a readable
642 * buffer of at least \p ad_len Bytes.
643 * \param ad_len the Length of \p ad Bytes.
644 *
645 * \return \c 0 on success.
646 * \return A specific error code on failure.
647 */
648 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
649 const unsigned char *ad, size_t ad_len );
650 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
651
652 /**
653 * \brief The generic cipher update function. It encrypts or
654 * decrypts using the given cipher context. Writes as
655 * many block-sized blocks of data as possible to output.
656 * Any data that cannot be written immediately is either
657 * added to the next block, or flushed when
658 * mbedtls_cipher_finish() is called.
659 * Exception: For MBEDTLS_MODE_ECB, expects a single block
660 * in size. For example, 16 Bytes for AES.
661 *
662 * \note If the underlying cipher is used in GCM mode, all calls
663 * to this function, except for the last one before
664 * mbedtls_cipher_finish(), must have \p ilen as a
665 * multiple of the block size of the cipher.
666 *
667 * \param ctx The generic cipher context. This must be initialized and
668 * bound to a key.
669 * \param input The buffer holding the input data. This must be a
670 * readable buffer of at least \p ilen Bytes.
671 * \param ilen The length of the input data.
672 * \param output The buffer for the output data. This must be able to
673 * hold at least `ilen + block_size`. This must not be the
674 * same buffer as \p input.
675 * \param olen The length of the output data, to be updated with the
676 * actual number of Bytes written. This must not be
677 * \c NULL.
678 *
679 * \return \c 0 on success.
680 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
681 * parameter-verification failure.
682 * \return #MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
683 * unsupported mode for a cipher.
684 * \return A cipher-specific error code on failure.
685 */
686 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
687 size_t ilen, unsigned char *output, size_t *olen );
688
689 /**
690 * \brief The generic cipher finalization function. If data still
691 * needs to be flushed from an incomplete block, the data
692 * contained in it is padded to the size of
693 * the last block, and written to the \p output buffer.
694 *
695 * \param ctx The generic cipher context. This must be initialized and
696 * bound to a key.
697 * \param output The buffer to write data to. This needs to be a writable
698 * buffer of at least \p block_size Bytes.
699 * \param olen The length of the data written to the \p output buffer.
700 * This may not be \c NULL.
701 *
702 * \return \c 0 on success.
703 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
704 * parameter-verification failure.
705 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
706 * expecting a full block but not receiving one.
707 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
708 * while decrypting.
709 * \return A cipher-specific error code on failure.
710 */
711 int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
712 unsigned char *output, size_t *olen );
713
714 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
715 /**
716 * \brief This function writes a tag for AEAD ciphers.
717 * Currently supported with GCM and ChaCha20+Poly1305.
718 * This must be called after mbedtls_cipher_finish().
719 *
720 * \param ctx The generic cipher context. This must be initialized,
721 * bound to a key, and have just completed a cipher
722 * operation through mbedtls_cipher_finish() the tag for
723 * which should be written.
724 * \param tag The buffer to write the tag to. This must be a writable
725 * buffer of at least \p tag_len Bytes.
726 * \param tag_len The length of the tag to write.
727 *
728 * \return \c 0 on success.
729 * \return A specific error code on failure.
730 */
731 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
732 unsigned char *tag, size_t tag_len );
733
734 /**
735 * \brief This function checks the tag for AEAD ciphers.
736 * Currently supported with GCM and ChaCha20+Poly1305.
737 * This must be called after mbedtls_cipher_finish().
738 *
739 * \param ctx The generic cipher context. This must be initialized.
740 * \param tag The buffer holding the tag. This must be a readable
741 * buffer of at least \p tag_len Bytes.
742 * \param tag_len The length of the tag to check.
743 *
744 * \return \c 0 on success.
745 * \return A specific error code on failure.
746 */
747 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
748 const unsigned char *tag, size_t tag_len );
749 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
750
751 /**
752 * \brief The generic all-in-one encryption/decryption function,
753 * for all ciphers except AEAD constructs.
754 *
755 * \param ctx The generic cipher context. This must be initialized.
756 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
757 * This must be a readable buffer of at least \p iv_len
758 * Bytes.
759 * \param iv_len The IV length for ciphers with variable-size IV.
760 * This parameter is discarded by ciphers with fixed-size
761 * IV.
762 * \param input The buffer holding the input data. This must be a
763 * readable buffer of at least \p ilen Bytes.
764 * \param ilen The length of the input data in Bytes.
765 * \param output The buffer for the output data. This must be able to
766 * hold at least `ilen + block_size`. This must not be the
767 * same buffer as \p input.
768 * \param olen The length of the output data, to be updated with the
769 * actual number of Bytes written. This must not be
770 * \c NULL.
771 *
772 * \note Some ciphers do not use IVs nor nonce. For these
773 * ciphers, use \p iv = NULL and \p iv_len = 0.
774 *
775 * \return \c 0 on success.
776 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
777 * parameter-verification failure.
778 * \return #MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED on decryption
779 * expecting a full block but not receiving one.
780 * \return #MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
781 * while decrypting.
782 * \return A cipher-specific error code on failure.
783 */
784 int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
785 const unsigned char *iv, size_t iv_len,
786 const unsigned char *input, size_t ilen,
787 unsigned char *output, size_t *olen );
788
789 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
790 /**
791 * \brief The generic autenticated encryption (AEAD) function.
792 *
793 * \param ctx The generic cipher context. This must be initialized and
794 * bound to a key.
795 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
796 * This must be a readable buffer of at least \p iv_len
797 * Bytes.
798 * \param iv_len The IV length for ciphers with variable-size IV.
799 * This parameter is discarded by ciphers with fixed-size IV.
800 * \param ad The additional data to authenticate. This must be a
801 * readable buffer of at least \p ad_len Bytes.
802 * \param ad_len The length of \p ad.
803 * \param input The buffer holding the input data. This must be a
804 * readable buffer of at least \p ilen Bytes.
805 * \param ilen The length of the input data.
806 * \param output The buffer for the output data. This must be able to
807 * hold at least \p ilen Bytes.
808 * \param olen The length of the output data, to be updated with the
809 * actual number of Bytes written. This must not be
810 * \c NULL.
811 * \param tag The buffer for the authentication tag. This must be a
812 * writable buffer of at least \p tag_len Bytes.
813 * \param tag_len The desired length of the authentication tag.
814 *
815 * \return \c 0 on success.
816 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
817 * parameter-verification failure.
818 * \return A cipher-specific error code on failure.
819 */
820 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
821 const unsigned char *iv, size_t iv_len,
822 const unsigned char *ad, size_t ad_len,
823 const unsigned char *input, size_t ilen,
824 unsigned char *output, size_t *olen,
825 unsigned char *tag, size_t tag_len );
826
827 /**
828 * \brief The generic autenticated decryption (AEAD) function.
829 *
830 * \note If the data is not authentic, then the output buffer
831 * is zeroed out to prevent the unauthentic plaintext being
832 * used, making this interface safer.
833 *
834 * \param ctx The generic cipher context. This must be initialized and
835 * and bound to a key.
836 * \param iv The IV to use, or NONCE_COUNTER for CTR-mode ciphers.
837 * This must be a readable buffer of at least \p iv_len
838 * Bytes.
839 * \param iv_len The IV length for ciphers with variable-size IV.
840 * This parameter is discarded by ciphers with fixed-size IV.
841 * \param ad The additional data to be authenticated. This must be a
842 * readable buffer of at least \p ad_len Bytes.
843 * \param ad_len The length of \p ad.
844 * \param input The buffer holding the input data. This must be a
845 * readable buffer of at least \p ilen Bytes.
846 * \param ilen The length of the input data.
847 * \param output The buffer for the output data.
848 * This must be able to hold at least \p ilen Bytes.
849 * \param olen The length of the output data, to be updated with the
850 * actual number of Bytes written. This must not be
851 * \c NULL.
852 * \param tag The buffer holding the authentication tag. This must be
853 * a readable buffer of at least \p tag_len Bytes.
854 * \param tag_len The length of the authentication tag.
855 *
856 * \return \c 0 on success.
857 * \return #MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on
858 * parameter-verification failure.
859 * \return #MBEDTLS_ERR_CIPHER_AUTH_FAILED if data is not authentic.
860 * \return A cipher-specific error code on failure.
861 */
862 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
863 const unsigned char *iv, size_t iv_len,
864 const unsigned char *ad, size_t ad_len,
865 const unsigned char *input, size_t ilen,
866 unsigned char *output, size_t *olen,
867 const unsigned char *tag, size_t tag_len );
868 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
869
870 #ifdef __cplusplus
871 }
872 #endif
873
874 #endif /* MBEDTLS_CIPHER_H */
875