[MBEDTLS]
[reactos.git] / reactos / sdk / include / reactos / libs / mbedtls / cipher.h
1 /**
2 * \file cipher.h
3 *
4 * \brief Generic cipher wrapper.
5 *
6 * \author Adriaan de Jong <dejong@fox-it.com>
7 *
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: GPL-2.0
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * This file is part of mbed TLS (https://tls.mbed.org)
26 */
27
28 #ifndef MBEDTLS_CIPHER_H
29 #define MBEDTLS_CIPHER_H
30
31 #if !defined(MBEDTLS_CONFIG_FILE)
32 #include "config.h"
33 #else
34 #include MBEDTLS_CONFIG_FILE
35 #endif
36
37 #include <stddef.h>
38
39 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CCM_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)
48 #define MBEDTLS_CIPHER_MODE_STREAM
49 #endif
50
51 #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
52 !defined(inline) && !defined(__cplusplus)
53 #define inline __inline
54 #endif
55
56 #define MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE -0x6080 /**< The selected feature is not available. */
57 #define MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA -0x6100 /**< Bad input parameters to function. */
58 #define MBEDTLS_ERR_CIPHER_ALLOC_FAILED -0x6180 /**< Failed to allocate memory. */
59 #define MBEDTLS_ERR_CIPHER_INVALID_PADDING -0x6200 /**< Input data contains invalid padding and is rejected. */
60 #define MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED -0x6280 /**< Decryption of block requires a full block. */
61 #define MBEDTLS_ERR_CIPHER_AUTH_FAILED -0x6300 /**< Authentication failed (for AEAD modes). */
62 #define MBEDTLS_ERR_CIPHER_INVALID_CONTEXT -0x6380 /**< The context is invalid, eg because it was free()ed. */
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 typedef enum {
72 MBEDTLS_CIPHER_ID_NONE = 0,
73 MBEDTLS_CIPHER_ID_NULL,
74 MBEDTLS_CIPHER_ID_AES,
75 MBEDTLS_CIPHER_ID_DES,
76 MBEDTLS_CIPHER_ID_3DES,
77 MBEDTLS_CIPHER_ID_CAMELLIA,
78 MBEDTLS_CIPHER_ID_BLOWFISH,
79 MBEDTLS_CIPHER_ID_ARC4,
80 } mbedtls_cipher_id_t;
81
82 typedef enum {
83 MBEDTLS_CIPHER_NONE = 0,
84 MBEDTLS_CIPHER_NULL,
85 MBEDTLS_CIPHER_AES_128_ECB,
86 MBEDTLS_CIPHER_AES_192_ECB,
87 MBEDTLS_CIPHER_AES_256_ECB,
88 MBEDTLS_CIPHER_AES_128_CBC,
89 MBEDTLS_CIPHER_AES_192_CBC,
90 MBEDTLS_CIPHER_AES_256_CBC,
91 MBEDTLS_CIPHER_AES_128_CFB128,
92 MBEDTLS_CIPHER_AES_192_CFB128,
93 MBEDTLS_CIPHER_AES_256_CFB128,
94 MBEDTLS_CIPHER_AES_128_CTR,
95 MBEDTLS_CIPHER_AES_192_CTR,
96 MBEDTLS_CIPHER_AES_256_CTR,
97 MBEDTLS_CIPHER_AES_128_GCM,
98 MBEDTLS_CIPHER_AES_192_GCM,
99 MBEDTLS_CIPHER_AES_256_GCM,
100 MBEDTLS_CIPHER_CAMELLIA_128_ECB,
101 MBEDTLS_CIPHER_CAMELLIA_192_ECB,
102 MBEDTLS_CIPHER_CAMELLIA_256_ECB,
103 MBEDTLS_CIPHER_CAMELLIA_128_CBC,
104 MBEDTLS_CIPHER_CAMELLIA_192_CBC,
105 MBEDTLS_CIPHER_CAMELLIA_256_CBC,
106 MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
107 MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
108 MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
109 MBEDTLS_CIPHER_CAMELLIA_128_CTR,
110 MBEDTLS_CIPHER_CAMELLIA_192_CTR,
111 MBEDTLS_CIPHER_CAMELLIA_256_CTR,
112 MBEDTLS_CIPHER_CAMELLIA_128_GCM,
113 MBEDTLS_CIPHER_CAMELLIA_192_GCM,
114 MBEDTLS_CIPHER_CAMELLIA_256_GCM,
115 MBEDTLS_CIPHER_DES_ECB,
116 MBEDTLS_CIPHER_DES_CBC,
117 MBEDTLS_CIPHER_DES_EDE_ECB,
118 MBEDTLS_CIPHER_DES_EDE_CBC,
119 MBEDTLS_CIPHER_DES_EDE3_ECB,
120 MBEDTLS_CIPHER_DES_EDE3_CBC,
121 MBEDTLS_CIPHER_BLOWFISH_ECB,
122 MBEDTLS_CIPHER_BLOWFISH_CBC,
123 MBEDTLS_CIPHER_BLOWFISH_CFB64,
124 MBEDTLS_CIPHER_BLOWFISH_CTR,
125 MBEDTLS_CIPHER_ARC4_128,
126 MBEDTLS_CIPHER_AES_128_CCM,
127 MBEDTLS_CIPHER_AES_192_CCM,
128 MBEDTLS_CIPHER_AES_256_CCM,
129 MBEDTLS_CIPHER_CAMELLIA_128_CCM,
130 MBEDTLS_CIPHER_CAMELLIA_192_CCM,
131 MBEDTLS_CIPHER_CAMELLIA_256_CCM,
132 } mbedtls_cipher_type_t;
133
134 typedef enum {
135 MBEDTLS_MODE_NONE = 0,
136 MBEDTLS_MODE_ECB,
137 MBEDTLS_MODE_CBC,
138 MBEDTLS_MODE_CFB,
139 MBEDTLS_MODE_OFB, /* Unused! */
140 MBEDTLS_MODE_CTR,
141 MBEDTLS_MODE_GCM,
142 MBEDTLS_MODE_STREAM,
143 MBEDTLS_MODE_CCM,
144 } mbedtls_cipher_mode_t;
145
146 typedef enum {
147 MBEDTLS_PADDING_PKCS7 = 0, /**< PKCS7 padding (default) */
148 MBEDTLS_PADDING_ONE_AND_ZEROS, /**< ISO/IEC 7816-4 padding */
149 MBEDTLS_PADDING_ZEROS_AND_LEN, /**< ANSI X.923 padding */
150 MBEDTLS_PADDING_ZEROS, /**< zero padding (not reversible!) */
151 MBEDTLS_PADDING_NONE, /**< never pad (full blocks only) */
152 } mbedtls_cipher_padding_t;
153
154 typedef enum {
155 MBEDTLS_OPERATION_NONE = -1,
156 MBEDTLS_DECRYPT = 0,
157 MBEDTLS_ENCRYPT,
158 } mbedtls_operation_t;
159
160 enum {
161 /** Undefined key length */
162 MBEDTLS_KEY_LENGTH_NONE = 0,
163 /** Key length, in bits (including parity), for DES keys */
164 MBEDTLS_KEY_LENGTH_DES = 64,
165 /** Key length, in bits (including parity), for DES in two key EDE */
166 MBEDTLS_KEY_LENGTH_DES_EDE = 128,
167 /** Key length, in bits (including parity), for DES in three-key EDE */
168 MBEDTLS_KEY_LENGTH_DES_EDE3 = 192,
169 };
170
171 /** Maximum length of any IV, in bytes */
172 #define MBEDTLS_MAX_IV_LENGTH 16
173 /** Maximum block size of any cipher, in bytes */
174 #define MBEDTLS_MAX_BLOCK_LENGTH 16
175
176 /**
177 * Base cipher information (opaque struct).
178 */
179 typedef struct mbedtls_cipher_base_t mbedtls_cipher_base_t;
180
181 /**
182 * CMAC context (opaque struct).
183 */
184 typedef struct mbedtls_cmac_context_t mbedtls_cmac_context_t;
185
186 /**
187 * Cipher information. Allows cipher functions to be called in a generic way.
188 */
189 typedef struct {
190 /** Full cipher identifier (e.g. MBEDTLS_CIPHER_AES_256_CBC) */
191 mbedtls_cipher_type_t type;
192
193 /** Cipher mode (e.g. MBEDTLS_MODE_CBC) */
194 mbedtls_cipher_mode_t mode;
195
196 /** Cipher key length, in bits (default length for variable sized ciphers)
197 * (Includes parity bits for ciphers like DES) */
198 unsigned int key_bitlen;
199
200 /** Name of the cipher */
201 const char * name;
202
203 /** IV/NONCE size, in bytes.
204 * For cipher that accept many sizes: recommended size */
205 unsigned int iv_size;
206
207 /** Flags for variable IV size, variable key size, etc. */
208 int flags;
209
210 /** block size, in bytes */
211 unsigned int block_size;
212
213 /** Base cipher information and functions */
214 const mbedtls_cipher_base_t *base;
215
216 } mbedtls_cipher_info_t;
217
218 /**
219 * Generic cipher context.
220 */
221 typedef struct {
222 /** Information about the associated cipher */
223 const mbedtls_cipher_info_t *cipher_info;
224
225 /** Key length to use */
226 int key_bitlen;
227
228 /** Operation that the context's key has been initialised for */
229 mbedtls_operation_t operation;
230
231 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
232 /** Padding functions to use, if relevant for cipher mode */
233 void (*add_padding)( unsigned char *output, size_t olen, size_t data_len );
234 int (*get_padding)( unsigned char *input, size_t ilen, size_t *data_len );
235 #endif
236
237 /** Buffer for data that hasn't been encrypted yet */
238 unsigned char unprocessed_data[MBEDTLS_MAX_BLOCK_LENGTH];
239
240 /** Number of bytes that still need processing */
241 size_t unprocessed_len;
242
243 /** Current IV or NONCE_COUNTER for CTR-mode */
244 unsigned char iv[MBEDTLS_MAX_IV_LENGTH];
245
246 /** IV size in bytes (for ciphers with variable-length IVs) */
247 size_t iv_size;
248
249 /** Cipher-specific context */
250 void *cipher_ctx;
251
252 #if defined(MBEDTLS_CMAC_C)
253 /** CMAC Specific context */
254 mbedtls_cmac_context_t *cmac_ctx;
255 #endif
256 } mbedtls_cipher_context_t;
257
258 /**
259 * \brief Returns the list of ciphers supported by the generic cipher module.
260 *
261 * \return a statically allocated array of ciphers, the last entry
262 * is 0.
263 */
264 const int *mbedtls_cipher_list( void );
265
266 /**
267 * \brief Returns the cipher information structure associated
268 * with the given cipher name.
269 *
270 * \param cipher_name Name of the cipher to search for.
271 *
272 * \return the cipher information structure associated with the
273 * given cipher_name, or NULL if not found.
274 */
275 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_string( const char *cipher_name );
276
277 /**
278 * \brief Returns the cipher information structure associated
279 * with the given cipher type.
280 *
281 * \param cipher_type Type of the cipher to search for.
282 *
283 * \return the cipher information structure associated with the
284 * given cipher_type, or NULL if not found.
285 */
286 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type );
287
288 /**
289 * \brief Returns the cipher information structure associated
290 * with the given cipher id, key size and mode.
291 *
292 * \param cipher_id Id of the cipher to search for
293 * (e.g. MBEDTLS_CIPHER_ID_AES)
294 * \param key_bitlen Length of the key in bits
295 * \param mode Cipher mode (e.g. MBEDTLS_MODE_CBC)
296 *
297 * \return the cipher information structure associated with the
298 * given cipher_type, or NULL if not found.
299 */
300 const mbedtls_cipher_info_t *mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id,
301 int key_bitlen,
302 const mbedtls_cipher_mode_t mode );
303
304 /**
305 * \brief Initialize a cipher_context (as NONE)
306 */
307 void mbedtls_cipher_init( mbedtls_cipher_context_t *ctx );
308
309 /**
310 * \brief Free and clear the cipher-specific context of ctx.
311 * Freeing ctx itself remains the responsibility of the
312 * caller.
313 */
314 void mbedtls_cipher_free( mbedtls_cipher_context_t *ctx );
315
316 /**
317 * \brief Initialises and fills the cipher context structure with
318 * the appropriate values.
319 *
320 * \note Currently also clears structure. In future versions you
321 * will be required to call mbedtls_cipher_init() on the structure
322 * first.
323 *
324 * \param ctx context to initialise. May not be NULL.
325 * \param cipher_info cipher to use.
326 *
327 * \return 0 on success,
328 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA on parameter failure,
329 * MBEDTLS_ERR_CIPHER_ALLOC_FAILED if allocation of the
330 * cipher-specific context failed.
331 */
332 int mbedtls_cipher_setup( mbedtls_cipher_context_t *ctx, const mbedtls_cipher_info_t *cipher_info );
333
334 /**
335 * \brief Returns the block size of the given cipher.
336 *
337 * \param ctx cipher's context. Must have been initialised.
338 *
339 * \return size of the cipher's blocks, or 0 if ctx has not been
340 * initialised.
341 */
342 static inline unsigned int mbedtls_cipher_get_block_size( const mbedtls_cipher_context_t *ctx )
343 {
344 if( NULL == ctx || NULL == ctx->cipher_info )
345 return 0;
346
347 return ctx->cipher_info->block_size;
348 }
349
350 /**
351 * \brief Returns the mode of operation for the cipher.
352 * (e.g. MBEDTLS_MODE_CBC)
353 *
354 * \param ctx cipher's context. Must have been initialised.
355 *
356 * \return mode of operation, or MBEDTLS_MODE_NONE if ctx
357 * has not been initialised.
358 */
359 static inline mbedtls_cipher_mode_t mbedtls_cipher_get_cipher_mode( const mbedtls_cipher_context_t *ctx )
360 {
361 if( NULL == ctx || NULL == ctx->cipher_info )
362 return MBEDTLS_MODE_NONE;
363
364 return ctx->cipher_info->mode;
365 }
366
367 /**
368 * \brief Returns the size of the cipher's IV/NONCE in bytes.
369 *
370 * \param ctx cipher's context. Must have been initialised.
371 *
372 * \return If IV has not been set yet: (recommended) IV size
373 * (0 for ciphers not using IV/NONCE).
374 * If IV has already been set: actual size.
375 */
376 static inline int mbedtls_cipher_get_iv_size( const mbedtls_cipher_context_t *ctx )
377 {
378 if( NULL == ctx || NULL == ctx->cipher_info )
379 return 0;
380
381 if( ctx->iv_size != 0 )
382 return (int) ctx->iv_size;
383
384 return (int) ctx->cipher_info->iv_size;
385 }
386
387 /**
388 * \brief Returns the type of the given cipher.
389 *
390 * \param ctx cipher's context. Must have been initialised.
391 *
392 * \return type of the cipher, or MBEDTLS_CIPHER_NONE if ctx has
393 * not been initialised.
394 */
395 static inline mbedtls_cipher_type_t mbedtls_cipher_get_type( const mbedtls_cipher_context_t *ctx )
396 {
397 if( NULL == ctx || NULL == ctx->cipher_info )
398 return MBEDTLS_CIPHER_NONE;
399
400 return ctx->cipher_info->type;
401 }
402
403 /**
404 * \brief Returns the name of the given cipher, as a string.
405 *
406 * \param ctx cipher's context. Must have been initialised.
407 *
408 * \return name of the cipher, or NULL if ctx was not initialised.
409 */
410 static inline const char *mbedtls_cipher_get_name( const mbedtls_cipher_context_t *ctx )
411 {
412 if( NULL == ctx || NULL == ctx->cipher_info )
413 return 0;
414
415 return ctx->cipher_info->name;
416 }
417
418 /**
419 * \brief Returns the key length of the cipher.
420 *
421 * \param ctx cipher's context. Must have been initialised.
422 *
423 * \return cipher's key length, in bits, or
424 * MBEDTLS_KEY_LENGTH_NONE if ctx has not been
425 * initialised.
426 */
427 static inline int mbedtls_cipher_get_key_bitlen( const mbedtls_cipher_context_t *ctx )
428 {
429 if( NULL == ctx || NULL == ctx->cipher_info )
430 return MBEDTLS_KEY_LENGTH_NONE;
431
432 return (int) ctx->cipher_info->key_bitlen;
433 }
434
435 /**
436 * \brief Returns the operation of the given cipher.
437 *
438 * \param ctx cipher's context. Must have been initialised.
439 *
440 * \return operation (MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT),
441 * or MBEDTLS_OPERATION_NONE if ctx has not been
442 * initialised.
443 */
444 static inline mbedtls_operation_t mbedtls_cipher_get_operation( const mbedtls_cipher_context_t *ctx )
445 {
446 if( NULL == ctx || NULL == ctx->cipher_info )
447 return MBEDTLS_OPERATION_NONE;
448
449 return ctx->operation;
450 }
451
452 /**
453 * \brief Set the key to use with the given context.
454 *
455 * \param ctx generic cipher context. May not be NULL. Must have been
456 * initialised using cipher_context_from_type or
457 * cipher_context_from_string.
458 * \param key The key to use.
459 * \param key_bitlen key length to use, in bits.
460 * \param operation Operation that the key will be used for, either
461 * MBEDTLS_ENCRYPT or MBEDTLS_DECRYPT.
462 *
463 * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
464 * parameter verification fails or a cipher specific
465 * error code.
466 */
467 int mbedtls_cipher_setkey( mbedtls_cipher_context_t *ctx, const unsigned char *key,
468 int key_bitlen, const mbedtls_operation_t operation );
469
470 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
471 /**
472 * \brief Set padding mode, for cipher modes that use padding.
473 * (Default: PKCS7 padding.)
474 *
475 * \param ctx generic cipher context
476 * \param mode padding mode
477 *
478 * \returns 0 on success, MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
479 * if selected padding mode is not supported, or
480 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if the cipher mode
481 * does not support padding.
482 */
483 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t *ctx, mbedtls_cipher_padding_t mode );
484 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
485
486 /**
487 * \brief Set the initialization vector (IV) or nonce
488 *
489 * \param ctx generic cipher context
490 * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
491 * \param iv_len IV length for ciphers with variable-size IV;
492 * discarded by ciphers with fixed-size IV.
493 *
494 * \returns 0 on success, or MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
495 *
496 * \note Some ciphers don't use IVs nor NONCE. For these
497 * ciphers, this function has no effect.
498 */
499 int mbedtls_cipher_set_iv( mbedtls_cipher_context_t *ctx,
500 const unsigned char *iv, size_t iv_len );
501
502 /**
503 * \brief Finish preparation of the given context
504 *
505 * \param ctx generic cipher context
506 *
507 * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
508 * if parameter verification fails.
509 */
510 int mbedtls_cipher_reset( mbedtls_cipher_context_t *ctx );
511
512 #if defined(MBEDTLS_GCM_C)
513 /**
514 * \brief Add additional data (for AEAD ciphers).
515 * Currently only supported with GCM.
516 * Must be called exactly once, after mbedtls_cipher_reset().
517 *
518 * \param ctx generic cipher context
519 * \param ad Additional data to use.
520 * \param ad_len Length of ad.
521 *
522 * \return 0 on success, or a specific error code.
523 */
524 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t *ctx,
525 const unsigned char *ad, size_t ad_len );
526 #endif /* MBEDTLS_GCM_C */
527
528 /**
529 * \brief Generic cipher update function. Encrypts/decrypts
530 * using the given cipher context. Writes as many block
531 * size'd blocks of data as possible to output. Any data
532 * that cannot be written immediately will either be added
533 * to the next block, or flushed when cipher_final is
534 * called.
535 * Exception: for MBEDTLS_MODE_ECB, expects single block
536 * in size (e.g. 16 bytes for AES)
537 *
538 * \param ctx generic cipher context
539 * \param input buffer holding the input data
540 * \param ilen length of the input data
541 * \param output buffer for the output data. Should be able to hold at
542 * least ilen + block_size. Cannot be the same buffer as
543 * input!
544 * \param olen length of the output data, will be filled with the
545 * actual number of bytes written.
546 *
547 * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
548 * parameter verification fails,
549 * MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE on an
550 * unsupported mode for a cipher or a cipher specific
551 * error code.
552 *
553 * \note If the underlying cipher is GCM, all calls to this
554 * function, except the last one before mbedtls_cipher_finish(),
555 * must have ilen a multiple of the block size.
556 */
557 int mbedtls_cipher_update( mbedtls_cipher_context_t *ctx, const unsigned char *input,
558 size_t ilen, unsigned char *output, size_t *olen );
559
560 /**
561 * \brief Generic cipher finalisation function. If data still
562 * needs to be flushed from an incomplete block, data
563 * contained within it will be padded with the size of
564 * the last block, and written to the output buffer.
565 *
566 * \param ctx Generic cipher context
567 * \param output buffer to write data to. Needs block_size available.
568 * \param olen length of the data written to the output buffer.
569 *
570 * \returns 0 on success, MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA if
571 * parameter verification fails,
572 * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
573 * expected a full block but was not provided one,
574 * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
575 * while decrypting or a cipher specific error code.
576 */
577 int mbedtls_cipher_finish( mbedtls_cipher_context_t *ctx,
578 unsigned char *output, size_t *olen );
579
580 #if defined(MBEDTLS_GCM_C)
581 /**
582 * \brief Write tag for AEAD ciphers.
583 * Currently only supported with GCM.
584 * Must be called after mbedtls_cipher_finish().
585 *
586 * \param ctx Generic cipher context
587 * \param tag buffer to write the tag
588 * \param tag_len Length of the tag to write
589 *
590 * \return 0 on success, or a specific error code.
591 */
592 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t *ctx,
593 unsigned char *tag, size_t tag_len );
594
595 /**
596 * \brief Check tag for AEAD ciphers.
597 * Currently only supported with GCM.
598 * Must be called after mbedtls_cipher_finish().
599 *
600 * \param ctx Generic cipher context
601 * \param tag Buffer holding the tag
602 * \param tag_len Length of the tag to check
603 *
604 * \return 0 on success, or a specific error code.
605 */
606 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t *ctx,
607 const unsigned char *tag, size_t tag_len );
608 #endif /* MBEDTLS_GCM_C */
609
610 /**
611 * \brief Generic all-in-one encryption/decryption
612 * (for all ciphers except AEAD constructs).
613 *
614 * \param ctx generic cipher context
615 * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
616 * \param iv_len IV length for ciphers with variable-size IV;
617 * discarded by ciphers with fixed-size IV.
618 * \param input buffer holding the input data
619 * \param ilen length of the input data
620 * \param output buffer for the output data. Should be able to hold at
621 * least ilen + block_size. Cannot be the same buffer as
622 * input!
623 * \param olen length of the output data, will be filled with the
624 * actual number of bytes written.
625 *
626 * \note Some ciphers don't use IVs nor NONCE. For these
627 * ciphers, use iv = NULL and iv_len = 0.
628 *
629 * \returns 0 on success, or
630 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
631 * MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED if decryption
632 * expected a full block but was not provided one, or
633 * MBEDTLS_ERR_CIPHER_INVALID_PADDING on invalid padding
634 * while decrypting, or
635 * a cipher specific error code.
636 */
637 int mbedtls_cipher_crypt( mbedtls_cipher_context_t *ctx,
638 const unsigned char *iv, size_t iv_len,
639 const unsigned char *input, size_t ilen,
640 unsigned char *output, size_t *olen );
641
642 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
643 /**
644 * \brief Generic autenticated encryption (AEAD ciphers).
645 *
646 * \param ctx generic cipher context
647 * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
648 * \param iv_len IV length for ciphers with variable-size IV;
649 * discarded by ciphers with fixed-size IV.
650 * \param ad Additional data to authenticate.
651 * \param ad_len Length of ad.
652 * \param input buffer holding the input data
653 * \param ilen length of the input data
654 * \param output buffer for the output data.
655 * Should be able to hold at least ilen.
656 * \param olen length of the output data, will be filled with the
657 * actual number of bytes written.
658 * \param tag buffer for the authentication tag
659 * \param tag_len desired tag length
660 *
661 * \returns 0 on success, or
662 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
663 * a cipher specific error code.
664 */
665 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t *ctx,
666 const unsigned char *iv, size_t iv_len,
667 const unsigned char *ad, size_t ad_len,
668 const unsigned char *input, size_t ilen,
669 unsigned char *output, size_t *olen,
670 unsigned char *tag, size_t tag_len );
671
672 /**
673 * \brief Generic autenticated decryption (AEAD ciphers).
674 *
675 * \param ctx generic cipher context
676 * \param iv IV to use (or NONCE_COUNTER for CTR-mode ciphers)
677 * \param iv_len IV length for ciphers with variable-size IV;
678 * discarded by ciphers with fixed-size IV.
679 * \param ad Additional data to be authenticated.
680 * \param ad_len Length of ad.
681 * \param input buffer holding the input data
682 * \param ilen length of the input data
683 * \param output buffer for the output data.
684 * Should be able to hold at least ilen.
685 * \param olen length of the output data, will be filled with the
686 * actual number of bytes written.
687 * \param tag buffer holding the authentication tag
688 * \param tag_len length of the authentication tag
689 *
690 * \returns 0 on success, or
691 * MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA, or
692 * MBEDTLS_ERR_CIPHER_AUTH_FAILED if data isn't authentic,
693 * or a cipher specific error code.
694 *
695 * \note If the data is not authentic, then the output buffer
696 * is zeroed out to prevent the unauthentic plaintext to
697 * be used by mistake, making this interface safer.
698 */
699 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t *ctx,
700 const unsigned char *iv, size_t iv_len,
701 const unsigned char *ad, size_t ad_len,
702 const unsigned char *input, size_t ilen,
703 unsigned char *output, size_t *olen,
704 const unsigned char *tag, size_t tag_len );
705 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
706
707 #ifdef __cplusplus
708 }
709 #endif
710
711 #endif /* MBEDTLS_CIPHER_H */