4 * \brief Generic cipher wrapper for mbed TLS
6 * \author Adriaan de Jong <dejong@fox-it.com>
8 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
9 * SPDX-License-Identifier: GPL-2.0
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.
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.
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.
25 * This file is part of mbed TLS (https://tls.mbed.org)
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "mbedtls/config.h"
31 #include MBEDTLS_CONFIG_FILE
34 #if defined(MBEDTLS_CIPHER_C)
36 #include "mbedtls/cipher.h"
37 #include "mbedtls/cipher_internal.h"
38 #include "mbedtls/platform_util.h"
43 #if defined(MBEDTLS_CHACHAPOLY_C)
44 #include "mbedtls/chachapoly.h"
47 #if defined(MBEDTLS_GCM_C)
48 #include "mbedtls/gcm.h"
51 #if defined(MBEDTLS_CCM_C)
52 #include "mbedtls/ccm.h"
55 #if defined(MBEDTLS_CHACHA20_C)
56 #include "mbedtls/chacha20.h"
59 #if defined(MBEDTLS_CMAC_C)
60 #include "mbedtls/cmac.h"
63 #if defined(MBEDTLS_PLATFORM_C)
64 #include "mbedtls/platform.h"
66 #define mbedtls_calloc calloc
67 #define mbedtls_free free
70 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
71 /* Compare the contents of two buffers in constant time.
72 * Returns 0 if the contents are bitwise identical, otherwise returns
74 * This is currently only used by GCM and ChaCha20+Poly1305.
76 static int mbedtls_constant_time_memcmp( const void *v1
, const void *v2
, size_t len
)
78 const unsigned char *p1
= (const unsigned char*) v1
;
79 const unsigned char *p2
= (const unsigned char*) v2
;
83 for( diff
= 0, i
= 0; i
< len
; i
++ )
84 diff
|= p1
[i
] ^ p2
[i
];
88 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
90 static int supported_init
= 0;
92 const int *mbedtls_cipher_list( void )
94 const mbedtls_cipher_definition_t
*def
;
97 if( ! supported_init
)
99 def
= mbedtls_cipher_definitions
;
100 type
= mbedtls_cipher_supported
;
102 while( def
->type
!= 0 )
103 *type
++ = (*def
++).type
;
110 return( mbedtls_cipher_supported
);
113 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_type( const mbedtls_cipher_type_t cipher_type
)
115 const mbedtls_cipher_definition_t
*def
;
117 for( def
= mbedtls_cipher_definitions
; def
->info
!= NULL
; def
++ )
118 if( def
->type
== cipher_type
)
124 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_string( const char *cipher_name
)
126 const mbedtls_cipher_definition_t
*def
;
128 if( NULL
== cipher_name
)
131 for( def
= mbedtls_cipher_definitions
; def
->info
!= NULL
; def
++ )
132 if( ! strcmp( def
->info
->name
, cipher_name
) )
138 const mbedtls_cipher_info_t
*mbedtls_cipher_info_from_values( const mbedtls_cipher_id_t cipher_id
,
140 const mbedtls_cipher_mode_t mode
)
142 const mbedtls_cipher_definition_t
*def
;
144 for( def
= mbedtls_cipher_definitions
; def
->info
!= NULL
; def
++ )
145 if( def
->info
->base
->cipher
== cipher_id
&&
146 def
->info
->key_bitlen
== (unsigned) key_bitlen
&&
147 def
->info
->mode
== mode
)
153 void mbedtls_cipher_init( mbedtls_cipher_context_t
*ctx
)
155 memset( ctx
, 0, sizeof( mbedtls_cipher_context_t
) );
158 void mbedtls_cipher_free( mbedtls_cipher_context_t
*ctx
)
163 #if defined(MBEDTLS_CMAC_C)
166 mbedtls_platform_zeroize( ctx
->cmac_ctx
,
167 sizeof( mbedtls_cmac_context_t
) );
168 mbedtls_free( ctx
->cmac_ctx
);
172 if( ctx
->cipher_ctx
)
173 ctx
->cipher_info
->base
->ctx_free_func( ctx
->cipher_ctx
);
175 mbedtls_platform_zeroize( ctx
, sizeof(mbedtls_cipher_context_t
) );
178 int mbedtls_cipher_setup( mbedtls_cipher_context_t
*ctx
, const mbedtls_cipher_info_t
*cipher_info
)
180 if( NULL
== cipher_info
|| NULL
== ctx
)
181 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
183 memset( ctx
, 0, sizeof( mbedtls_cipher_context_t
) );
185 if( NULL
== ( ctx
->cipher_ctx
= cipher_info
->base
->ctx_alloc_func() ) )
186 return( MBEDTLS_ERR_CIPHER_ALLOC_FAILED
);
188 ctx
->cipher_info
= cipher_info
;
190 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
192 * Ignore possible errors caused by a cipher mode that doesn't use padding
194 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
195 (void) mbedtls_cipher_set_padding_mode( ctx
, MBEDTLS_PADDING_PKCS7
);
197 (void) mbedtls_cipher_set_padding_mode( ctx
, MBEDTLS_PADDING_NONE
);
199 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
204 int mbedtls_cipher_setkey( mbedtls_cipher_context_t
*ctx
, const unsigned char *key
,
205 int key_bitlen
, const mbedtls_operation_t operation
)
207 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
)
208 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
210 if( ( ctx
->cipher_info
->flags
& MBEDTLS_CIPHER_VARIABLE_KEY_LEN
) == 0 &&
211 (int) ctx
->cipher_info
->key_bitlen
!= key_bitlen
)
213 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
216 ctx
->key_bitlen
= key_bitlen
;
217 ctx
->operation
= operation
;
220 * For OFB, CFB and CTR mode always use the encryption key schedule
222 if( MBEDTLS_ENCRYPT
== operation
||
223 MBEDTLS_MODE_CFB
== ctx
->cipher_info
->mode
||
224 MBEDTLS_MODE_OFB
== ctx
->cipher_info
->mode
||
225 MBEDTLS_MODE_CTR
== ctx
->cipher_info
->mode
)
227 return ctx
->cipher_info
->base
->setkey_enc_func( ctx
->cipher_ctx
, key
,
231 if( MBEDTLS_DECRYPT
== operation
)
232 return ctx
->cipher_info
->base
->setkey_dec_func( ctx
->cipher_ctx
, key
,
235 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
238 int mbedtls_cipher_set_iv( mbedtls_cipher_context_t
*ctx
,
239 const unsigned char *iv
, size_t iv_len
)
241 size_t actual_iv_size
;
243 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== iv
)
244 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
246 /* avoid buffer overflow in ctx->iv */
247 if( iv_len
> MBEDTLS_MAX_IV_LENGTH
)
248 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
250 if( ( ctx
->cipher_info
->flags
& MBEDTLS_CIPHER_VARIABLE_IV_LEN
) != 0 )
251 actual_iv_size
= iv_len
;
254 actual_iv_size
= ctx
->cipher_info
->iv_size
;
256 /* avoid reading past the end of input buffer */
257 if( actual_iv_size
> iv_len
)
258 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
261 #if defined(MBEDTLS_CHACHA20_C)
262 if ( ctx
->cipher_info
->type
== MBEDTLS_CIPHER_CHACHA20
)
264 if ( 0 != mbedtls_chacha20_starts( (mbedtls_chacha20_context
*)ctx
->cipher_ctx
,
266 0U ) ) /* Initial counter value */
268 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
273 memcpy( ctx
->iv
, iv
, actual_iv_size
);
274 ctx
->iv_size
= actual_iv_size
;
279 int mbedtls_cipher_reset( mbedtls_cipher_context_t
*ctx
)
281 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
)
282 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
284 ctx
->unprocessed_len
= 0;
289 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
290 int mbedtls_cipher_update_ad( mbedtls_cipher_context_t
*ctx
,
291 const unsigned char *ad
, size_t ad_len
)
293 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
)
294 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
296 #if defined(MBEDTLS_GCM_C)
297 if( MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
)
299 return mbedtls_gcm_starts( (mbedtls_gcm_context
*) ctx
->cipher_ctx
, ctx
->operation
,
300 ctx
->iv
, ctx
->iv_size
, ad
, ad_len
);
304 #if defined(MBEDTLS_CHACHAPOLY_C)
305 if (MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
)
308 mbedtls_chachapoly_mode_t mode
;
310 mode
= ( ctx
->operation
== MBEDTLS_ENCRYPT
)
311 ? MBEDTLS_CHACHAPOLY_ENCRYPT
312 : MBEDTLS_CHACHAPOLY_DECRYPT
;
314 result
= mbedtls_chachapoly_starts( (mbedtls_chachapoly_context
*) ctx
->cipher_ctx
,
320 return mbedtls_chachapoly_update_aad( (mbedtls_chachapoly_context
*) ctx
->cipher_ctx
,
327 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
329 int mbedtls_cipher_update( mbedtls_cipher_context_t
*ctx
, const unsigned char *input
,
330 size_t ilen
, unsigned char *output
, size_t *olen
)
333 size_t block_size
= 0;
335 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== olen
)
337 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
341 block_size
= mbedtls_cipher_get_block_size( ctx
);
343 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_ECB
)
345 if( ilen
!= block_size
)
346 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED
);
350 if( 0 != ( ret
= ctx
->cipher_info
->base
->ecb_func( ctx
->cipher_ctx
,
351 ctx
->operation
, input
, output
) ) )
359 #if defined(MBEDTLS_GCM_C)
360 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_GCM
)
363 return mbedtls_gcm_update( (mbedtls_gcm_context
*) ctx
->cipher_ctx
, ilen
, input
,
368 #if defined(MBEDTLS_CHACHAPOLY_C)
369 if ( ctx
->cipher_info
->type
== MBEDTLS_CIPHER_CHACHA20_POLY1305
)
372 return mbedtls_chachapoly_update( (mbedtls_chachapoly_context
*) ctx
->cipher_ctx
,
373 ilen
, input
, output
);
377 if ( 0 == block_size
)
379 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT
;
382 if( input
== output
&&
383 ( ctx
->unprocessed_len
!= 0 || ilen
% block_size
) )
385 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
388 #if defined(MBEDTLS_CIPHER_MODE_CBC)
389 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_CBC
)
394 * If there is not enough data for a full block, cache it.
396 if( ( ctx
->operation
== MBEDTLS_DECRYPT
&& NULL
!= ctx
->add_padding
&&
397 ilen
<= block_size
- ctx
->unprocessed_len
) ||
398 ( ctx
->operation
== MBEDTLS_DECRYPT
&& NULL
== ctx
->add_padding
&&
399 ilen
< block_size
- ctx
->unprocessed_len
) ||
400 ( ctx
->operation
== MBEDTLS_ENCRYPT
&&
401 ilen
< block_size
- ctx
->unprocessed_len
) )
403 memcpy( &( ctx
->unprocessed_data
[ctx
->unprocessed_len
] ), input
,
406 ctx
->unprocessed_len
+= ilen
;
411 * Process cached data first
413 if( 0 != ctx
->unprocessed_len
)
415 copy_len
= block_size
- ctx
->unprocessed_len
;
417 memcpy( &( ctx
->unprocessed_data
[ctx
->unprocessed_len
] ), input
,
420 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
421 ctx
->operation
, block_size
, ctx
->iv
,
422 ctx
->unprocessed_data
, output
) ) )
428 output
+= block_size
;
429 ctx
->unprocessed_len
= 0;
436 * Cache final, incomplete block
440 if( 0 == block_size
)
442 return MBEDTLS_ERR_CIPHER_INVALID_CONTEXT
;
445 /* Encryption: only cache partial blocks
446 * Decryption w/ padding: always keep at least one whole block
447 * Decryption w/o padding: only cache partial blocks
449 copy_len
= ilen
% block_size
;
451 ctx
->operation
== MBEDTLS_DECRYPT
&&
452 NULL
!= ctx
->add_padding
)
454 copy_len
= block_size
;
457 memcpy( ctx
->unprocessed_data
, &( input
[ilen
- copy_len
] ),
460 ctx
->unprocessed_len
+= copy_len
;
465 * Process remaining full blocks
469 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
470 ctx
->operation
, ilen
, ctx
->iv
, input
, output
) ) )
480 #endif /* MBEDTLS_CIPHER_MODE_CBC */
482 #if defined(MBEDTLS_CIPHER_MODE_CFB)
483 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_CFB
)
485 if( 0 != ( ret
= ctx
->cipher_info
->base
->cfb_func( ctx
->cipher_ctx
,
486 ctx
->operation
, ilen
, &ctx
->unprocessed_len
, ctx
->iv
,
496 #endif /* MBEDTLS_CIPHER_MODE_CFB */
498 #if defined(MBEDTLS_CIPHER_MODE_OFB)
499 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_OFB
)
501 if( 0 != ( ret
= ctx
->cipher_info
->base
->ofb_func( ctx
->cipher_ctx
,
502 ilen
, &ctx
->unprocessed_len
, ctx
->iv
, input
, output
) ) )
511 #endif /* MBEDTLS_CIPHER_MODE_OFB */
513 #if defined(MBEDTLS_CIPHER_MODE_CTR)
514 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_CTR
)
516 if( 0 != ( ret
= ctx
->cipher_info
->base
->ctr_func( ctx
->cipher_ctx
,
517 ilen
, &ctx
->unprocessed_len
, ctx
->iv
,
518 ctx
->unprocessed_data
, input
, output
) ) )
527 #endif /* MBEDTLS_CIPHER_MODE_CTR */
529 #if defined(MBEDTLS_CIPHER_MODE_XTS)
530 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_XTS
)
532 if( ctx
->unprocessed_len
> 0 ) {
533 /* We can only process an entire data unit at a time. */
534 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
537 ret
= ctx
->cipher_info
->base
->xts_func( ctx
->cipher_ctx
,
538 ctx
->operation
, ilen
, ctx
->iv
, input
, output
);
548 #endif /* MBEDTLS_CIPHER_MODE_XTS */
550 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
551 if( ctx
->cipher_info
->mode
== MBEDTLS_MODE_STREAM
)
553 if( 0 != ( ret
= ctx
->cipher_info
->base
->stream_func( ctx
->cipher_ctx
,
554 ilen
, input
, output
) ) )
563 #endif /* MBEDTLS_CIPHER_MODE_STREAM */
565 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
568 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
569 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
571 * PKCS7 (and PKCS5) padding: fill with ll bytes, with ll = padding_len
573 static void add_pkcs_padding( unsigned char *output
, size_t output_len
,
576 size_t padding_len
= output_len
- data_len
;
579 for( i
= 0; i
< padding_len
; i
++ )
580 output
[data_len
+ i
] = (unsigned char) padding_len
;
583 static int get_pkcs_padding( unsigned char *input
, size_t input_len
,
587 unsigned char padding_len
, bad
= 0;
589 if( NULL
== input
|| NULL
== data_len
)
590 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
592 padding_len
= input
[input_len
- 1];
593 *data_len
= input_len
- padding_len
;
595 /* Avoid logical || since it results in a branch */
596 bad
|= padding_len
> input_len
;
597 bad
|= padding_len
== 0;
599 /* The number of bytes checked must be independent of padding_len,
600 * so pick input_len, which is usually 8 or 16 (one block) */
601 pad_idx
= input_len
- padding_len
;
602 for( i
= 0; i
< input_len
; i
++ )
603 bad
|= ( input
[i
] ^ padding_len
) * ( i
>= pad_idx
);
605 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING
* ( bad
!= 0 ) );
607 #endif /* MBEDTLS_CIPHER_PADDING_PKCS7 */
609 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
611 * One and zeros padding: fill with 80 00 ... 00
613 static void add_one_and_zeros_padding( unsigned char *output
,
614 size_t output_len
, size_t data_len
)
616 size_t padding_len
= output_len
- data_len
;
619 output
[data_len
] = 0x80;
620 for( i
= 1; i
< padding_len
; i
++ )
621 output
[data_len
+ i
] = 0x00;
624 static int get_one_and_zeros_padding( unsigned char *input
, size_t input_len
,
628 unsigned char done
= 0, prev_done
, bad
;
630 if( NULL
== input
|| NULL
== data_len
)
631 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
635 for( i
= input_len
; i
> 0; i
-- )
638 done
|= ( input
[i
- 1] != 0 );
639 *data_len
|= ( i
- 1 ) * ( done
!= prev_done
);
640 bad
^= input
[i
- 1] * ( done
!= prev_done
);
643 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING
* ( bad
!= 0 ) );
646 #endif /* MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS */
648 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
650 * Zeros and len padding: fill with 00 ... 00 ll, where ll is padding length
652 static void add_zeros_and_len_padding( unsigned char *output
,
653 size_t output_len
, size_t data_len
)
655 size_t padding_len
= output_len
- data_len
;
658 for( i
= 1; i
< padding_len
; i
++ )
659 output
[data_len
+ i
- 1] = 0x00;
660 output
[output_len
- 1] = (unsigned char) padding_len
;
663 static int get_zeros_and_len_padding( unsigned char *input
, size_t input_len
,
667 unsigned char padding_len
, bad
= 0;
669 if( NULL
== input
|| NULL
== data_len
)
670 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
672 padding_len
= input
[input_len
- 1];
673 *data_len
= input_len
- padding_len
;
675 /* Avoid logical || since it results in a branch */
676 bad
|= padding_len
> input_len
;
677 bad
|= padding_len
== 0;
679 /* The number of bytes checked must be independent of padding_len */
680 pad_idx
= input_len
- padding_len
;
681 for( i
= 0; i
< input_len
- 1; i
++ )
682 bad
|= input
[i
] * ( i
>= pad_idx
);
684 return( MBEDTLS_ERR_CIPHER_INVALID_PADDING
* ( bad
!= 0 ) );
686 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN */
688 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
690 * Zero padding: fill with 00 ... 00
692 static void add_zeros_padding( unsigned char *output
,
693 size_t output_len
, size_t data_len
)
697 for( i
= data_len
; i
< output_len
; i
++ )
701 static int get_zeros_padding( unsigned char *input
, size_t input_len
,
705 unsigned char done
= 0, prev_done
;
707 if( NULL
== input
|| NULL
== data_len
)
708 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
711 for( i
= input_len
; i
> 0; i
-- )
714 done
|= ( input
[i
-1] != 0 );
715 *data_len
|= i
* ( done
!= prev_done
);
720 #endif /* MBEDTLS_CIPHER_PADDING_ZEROS */
723 * No padding: don't pad :)
725 * There is no add_padding function (check for NULL in mbedtls_cipher_finish)
726 * but a trivial get_padding function
728 static int get_no_padding( unsigned char *input
, size_t input_len
,
731 if( NULL
== input
|| NULL
== data_len
)
732 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
734 *data_len
= input_len
;
738 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
740 int mbedtls_cipher_finish( mbedtls_cipher_context_t
*ctx
,
741 unsigned char *output
, size_t *olen
)
743 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== olen
)
744 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
748 if( MBEDTLS_MODE_CFB
== ctx
->cipher_info
->mode
||
749 MBEDTLS_MODE_OFB
== ctx
->cipher_info
->mode
||
750 MBEDTLS_MODE_CTR
== ctx
->cipher_info
->mode
||
751 MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
||
752 MBEDTLS_MODE_XTS
== ctx
->cipher_info
->mode
||
753 MBEDTLS_MODE_STREAM
== ctx
->cipher_info
->mode
)
758 if ( ( MBEDTLS_CIPHER_CHACHA20
== ctx
->cipher_info
->type
) ||
759 ( MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
) )
764 if( MBEDTLS_MODE_ECB
== ctx
->cipher_info
->mode
)
766 if( ctx
->unprocessed_len
!= 0 )
767 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED
);
772 #if defined(MBEDTLS_CIPHER_MODE_CBC)
773 if( MBEDTLS_MODE_CBC
== ctx
->cipher_info
->mode
)
777 if( MBEDTLS_ENCRYPT
== ctx
->operation
)
779 /* check for 'no padding' mode */
780 if( NULL
== ctx
->add_padding
)
782 if( 0 != ctx
->unprocessed_len
)
783 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED
);
788 ctx
->add_padding( ctx
->unprocessed_data
, mbedtls_cipher_get_iv_size( ctx
),
789 ctx
->unprocessed_len
);
791 else if( mbedtls_cipher_get_block_size( ctx
) != ctx
->unprocessed_len
)
794 * For decrypt operations, expect a full block,
795 * or an empty block if no padding
797 if( NULL
== ctx
->add_padding
&& 0 == ctx
->unprocessed_len
)
800 return( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED
);
804 if( 0 != ( ret
= ctx
->cipher_info
->base
->cbc_func( ctx
->cipher_ctx
,
805 ctx
->operation
, mbedtls_cipher_get_block_size( ctx
), ctx
->iv
,
806 ctx
->unprocessed_data
, output
) ) )
811 /* Set output size for decryption */
812 if( MBEDTLS_DECRYPT
== ctx
->operation
)
813 return ctx
->get_padding( output
, mbedtls_cipher_get_block_size( ctx
),
816 /* Set output size for encryption */
817 *olen
= mbedtls_cipher_get_block_size( ctx
);
822 #endif /* MBEDTLS_CIPHER_MODE_CBC */
824 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
827 #if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
828 int mbedtls_cipher_set_padding_mode( mbedtls_cipher_context_t
*ctx
, mbedtls_cipher_padding_t mode
)
831 MBEDTLS_MODE_CBC
!= ctx
->cipher_info
->mode
)
833 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
838 #if defined(MBEDTLS_CIPHER_PADDING_PKCS7)
839 case MBEDTLS_PADDING_PKCS7
:
840 ctx
->add_padding
= add_pkcs_padding
;
841 ctx
->get_padding
= get_pkcs_padding
;
844 #if defined(MBEDTLS_CIPHER_PADDING_ONE_AND_ZEROS)
845 case MBEDTLS_PADDING_ONE_AND_ZEROS
:
846 ctx
->add_padding
= add_one_and_zeros_padding
;
847 ctx
->get_padding
= get_one_and_zeros_padding
;
850 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS_AND_LEN)
851 case MBEDTLS_PADDING_ZEROS_AND_LEN
:
852 ctx
->add_padding
= add_zeros_and_len_padding
;
853 ctx
->get_padding
= get_zeros_and_len_padding
;
856 #if defined(MBEDTLS_CIPHER_PADDING_ZEROS)
857 case MBEDTLS_PADDING_ZEROS
:
858 ctx
->add_padding
= add_zeros_padding
;
859 ctx
->get_padding
= get_zeros_padding
;
862 case MBEDTLS_PADDING_NONE
:
863 ctx
->add_padding
= NULL
;
864 ctx
->get_padding
= get_no_padding
;
868 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
873 #endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
875 #if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
876 int mbedtls_cipher_write_tag( mbedtls_cipher_context_t
*ctx
,
877 unsigned char *tag
, size_t tag_len
)
879 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
|| NULL
== tag
)
880 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
882 if( MBEDTLS_ENCRYPT
!= ctx
->operation
)
883 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
885 #if defined(MBEDTLS_GCM_C)
886 if( MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
)
887 return mbedtls_gcm_finish( (mbedtls_gcm_context
*) ctx
->cipher_ctx
, tag
, tag_len
);
890 #if defined(MBEDTLS_CHACHAPOLY_C)
891 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
)
893 /* Don't allow truncated MAC for Poly1305 */
894 if ( tag_len
!= 16U )
895 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
897 return mbedtls_chachapoly_finish( (mbedtls_chachapoly_context
*) ctx
->cipher_ctx
,
905 int mbedtls_cipher_check_tag( mbedtls_cipher_context_t
*ctx
,
906 const unsigned char *tag
, size_t tag_len
)
908 unsigned char check_tag
[16];
911 if( NULL
== ctx
|| NULL
== ctx
->cipher_info
||
912 MBEDTLS_DECRYPT
!= ctx
->operation
)
914 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
917 #if defined(MBEDTLS_GCM_C)
918 if( MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
)
920 if( tag_len
> sizeof( check_tag
) )
921 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
923 if( 0 != ( ret
= mbedtls_gcm_finish( (mbedtls_gcm_context
*) ctx
->cipher_ctx
,
924 check_tag
, tag_len
) ) )
929 /* Check the tag in "constant-time" */
930 if( mbedtls_constant_time_memcmp( tag
, check_tag
, tag_len
) != 0 )
931 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED
);
935 #endif /* MBEDTLS_GCM_C */
937 #if defined(MBEDTLS_CHACHAPOLY_C)
938 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
)
940 /* Don't allow truncated MAC for Poly1305 */
941 if ( tag_len
!= sizeof( check_tag
) )
942 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
944 ret
= mbedtls_chachapoly_finish( (mbedtls_chachapoly_context
*) ctx
->cipher_ctx
,
951 /* Check the tag in "constant-time" */
952 if( mbedtls_constant_time_memcmp( tag
, check_tag
, tag_len
) != 0 )
953 return( MBEDTLS_ERR_CIPHER_AUTH_FAILED
);
957 #endif /* MBEDTLS_CHACHAPOLY_C */
961 #endif /* MBEDTLS_GCM_C || MBEDTLS_CHACHAPOLY_C */
964 * Packet-oriented wrapper for non-AEAD modes
966 int mbedtls_cipher_crypt( mbedtls_cipher_context_t
*ctx
,
967 const unsigned char *iv
, size_t iv_len
,
968 const unsigned char *input
, size_t ilen
,
969 unsigned char *output
, size_t *olen
)
974 if( ( ret
= mbedtls_cipher_set_iv( ctx
, iv
, iv_len
) ) != 0 )
977 if( ( ret
= mbedtls_cipher_reset( ctx
) ) != 0 )
980 if( ( ret
= mbedtls_cipher_update( ctx
, input
, ilen
, output
, olen
) ) != 0 )
983 if( ( ret
= mbedtls_cipher_finish( ctx
, output
+ *olen
, &finish_olen
) ) != 0 )
986 *olen
+= finish_olen
;
991 #if defined(MBEDTLS_CIPHER_MODE_AEAD)
993 * Packet-oriented encryption for AEAD modes
995 int mbedtls_cipher_auth_encrypt( mbedtls_cipher_context_t
*ctx
,
996 const unsigned char *iv
, size_t iv_len
,
997 const unsigned char *ad
, size_t ad_len
,
998 const unsigned char *input
, size_t ilen
,
999 unsigned char *output
, size_t *olen
,
1000 unsigned char *tag
, size_t tag_len
)
1002 #if defined(MBEDTLS_GCM_C)
1003 if( MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
)
1006 return( mbedtls_gcm_crypt_and_tag( ctx
->cipher_ctx
, MBEDTLS_GCM_ENCRYPT
, ilen
,
1007 iv
, iv_len
, ad
, ad_len
, input
, output
,
1010 #endif /* MBEDTLS_GCM_C */
1011 #if defined(MBEDTLS_CCM_C)
1012 if( MBEDTLS_MODE_CCM
== ctx
->cipher_info
->mode
)
1015 return( mbedtls_ccm_encrypt_and_tag( ctx
->cipher_ctx
, ilen
,
1016 iv
, iv_len
, ad
, ad_len
, input
, output
,
1019 #endif /* MBEDTLS_CCM_C */
1020 #if defined(MBEDTLS_CHACHAPOLY_C)
1021 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
)
1023 /* ChachaPoly has fixed length nonce and MAC (tag) */
1024 if ( ( iv_len
!= ctx
->cipher_info
->iv_size
) ||
1025 ( tag_len
!= 16U ) )
1027 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1031 return( mbedtls_chachapoly_encrypt_and_tag( ctx
->cipher_ctx
,
1032 ilen
, iv
, ad
, ad_len
, input
, output
, tag
) );
1034 #endif /* MBEDTLS_CHACHAPOLY_C */
1036 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
1040 * Packet-oriented decryption for AEAD modes
1042 int mbedtls_cipher_auth_decrypt( mbedtls_cipher_context_t
*ctx
,
1043 const unsigned char *iv
, size_t iv_len
,
1044 const unsigned char *ad
, size_t ad_len
,
1045 const unsigned char *input
, size_t ilen
,
1046 unsigned char *output
, size_t *olen
,
1047 const unsigned char *tag
, size_t tag_len
)
1049 #if defined(MBEDTLS_GCM_C)
1050 if( MBEDTLS_MODE_GCM
== ctx
->cipher_info
->mode
)
1055 ret
= mbedtls_gcm_auth_decrypt( ctx
->cipher_ctx
, ilen
,
1056 iv
, iv_len
, ad
, ad_len
,
1057 tag
, tag_len
, input
, output
);
1059 if( ret
== MBEDTLS_ERR_GCM_AUTH_FAILED
)
1060 ret
= MBEDTLS_ERR_CIPHER_AUTH_FAILED
;
1064 #endif /* MBEDTLS_GCM_C */
1065 #if defined(MBEDTLS_CCM_C)
1066 if( MBEDTLS_MODE_CCM
== ctx
->cipher_info
->mode
)
1071 ret
= mbedtls_ccm_auth_decrypt( ctx
->cipher_ctx
, ilen
,
1072 iv
, iv_len
, ad
, ad_len
,
1073 input
, output
, tag
, tag_len
);
1075 if( ret
== MBEDTLS_ERR_CCM_AUTH_FAILED
)
1076 ret
= MBEDTLS_ERR_CIPHER_AUTH_FAILED
;
1080 #endif /* MBEDTLS_CCM_C */
1081 #if defined(MBEDTLS_CHACHAPOLY_C)
1082 if ( MBEDTLS_CIPHER_CHACHA20_POLY1305
== ctx
->cipher_info
->type
)
1086 /* ChachaPoly has fixed length nonce and MAC (tag) */
1087 if ( ( iv_len
!= ctx
->cipher_info
->iv_size
) ||
1088 ( tag_len
!= 16U ) )
1090 return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA
);
1094 ret
= mbedtls_chachapoly_auth_decrypt( ctx
->cipher_ctx
, ilen
,
1095 iv
, ad
, ad_len
, tag
, input
, output
);
1097 if( ret
== MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED
)
1098 ret
= MBEDTLS_ERR_CIPHER_AUTH_FAILED
;
1102 #endif /* MBEDTLS_CHACHAPOLY_C */
1104 return( MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE
);
1106 #endif /* MBEDTLS_CIPHER_MODE_AEAD */
1108 #endif /* MBEDTLS_CIPHER_C */