blob: 34ffbbf2e8e046233c4cd6258877581d316e11ba [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02003
k-stachowiakd8727232019-07-29 17:46:29 +02004#if defined(MBEDTLS_AES_C)
5#include "mbedtls/aes.h"
6#endif
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +020010#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +020011
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +010012#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
13#define MBEDTLS_CIPHER_AUTH_CRYPT
14#endif
15
Gilles Peskine0be02bd2021-07-19 16:32:54 +020016/* Check the internal consistency of a cipher info structure, and
17 * check it against mbedtls_cipher_info_from_xxx(). */
18static int check_cipher_info( mbedtls_cipher_type_t type,
19 const mbedtls_cipher_info_t *info )
20{
Max Fillinger72abd8a2021-11-28 14:02:36 +010021 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020022
23 TEST_ASSERT( info != NULL );
24 TEST_EQUAL( type, mbedtls_cipher_info_get_type( info ) );
25 TEST_EQUAL( type, info->type );
26 TEST_ASSERT( mbedtls_cipher_info_from_type( type ) == info );
27
28 TEST_EQUAL( info->mode, mbedtls_cipher_info_get_mode( info ) );
29
30 /* Insist that get_name() return the string from the structure and
31 * not a copy. A copy would have an unknown storage duration. */
32 TEST_ASSERT( mbedtls_cipher_info_get_name( info ) == info->name );
33 TEST_ASSERT( mbedtls_cipher_info_from_string( info->name ) == info );
34
35 key_bitlen = mbedtls_cipher_info_get_key_bitlen( info );
Max Fillingerc60c3a02021-11-09 22:38:56 +010036 block_size = mbedtls_cipher_info_get_block_size( info );
37 iv_size = mbedtls_cipher_info_get_iv_size( info );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020038 if( info->type == MBEDTLS_CIPHER_NULL )
Max Fillingerc60c3a02021-11-09 22:38:56 +010039 {
Gilles Peskine6ac8f942021-09-01 08:31:49 +020040 TEST_ASSERT( key_bitlen == 0 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010041 TEST_ASSERT( block_size == 1 );
42 TEST_ASSERT( iv_size == 0 );
43 }
Gilles Peskine6ac8f942021-09-01 08:31:49 +020044 else if( info->mode == MBEDTLS_MODE_XTS )
45 {
46 TEST_ASSERT( key_bitlen == 256 ||
47 key_bitlen == 384 ||
48 key_bitlen == 512 );
49 }
50 else if( ! strncmp( info->name, "DES-EDE3-", 9 ) )
51 {
52 TEST_ASSERT( key_bitlen == 192 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010053 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
54 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020055 }
56 else if( ! strncmp( info->name, "DES-EDE-", 8 ) )
57 {
58 TEST_ASSERT( key_bitlen == 128 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010059 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
60 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020061 }
62 else if( ! strncmp( info->name, "DES-", 4 ) )
63 {
64 TEST_ASSERT( key_bitlen == 64 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010065 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
66 TEST_ASSERT( block_size == 8 );
67 }
68 else if( ! strncmp( info->name, "AES", 3 ) )
69 {
70 TEST_ASSERT( key_bitlen == 128 ||
71 key_bitlen == 192 ||
72 key_bitlen == 256 );
73 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
74 TEST_ASSERT( block_size == 16 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020075 }
76 else
77 {
78 TEST_ASSERT( key_bitlen == 128 ||
79 key_bitlen == 192 ||
80 key_bitlen == 256 );
81 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +020082
Max Fillingerc60c3a02021-11-09 22:38:56 +010083 if( strstr( info->name, "-ECB" ) != NULL )
84 {
85 TEST_ASSERT( iv_size == 0 );
86 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
87 }
88 else if( strstr( info->name, "-CBC" ) != NULL ||
89 strstr( info->name, "-CTR" ) != NULL )
90 {
91 TEST_ASSERT( iv_size == block_size );
92 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
93 }
94 else if( strstr( info->name, "-GCM" ) != NULL )
95 {
96 TEST_ASSERT( iv_size == block_size - 4 );
97 TEST_ASSERT( mbedtls_cipher_info_has_variable_iv_size( info ) );
98 }
99
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200100 return( 1 );
101
102exit:
103 return( 0 );
104}
105
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100106#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
107/* Helper for resetting key/direction
108 *
109 * The documentation doesn't explicitly say whether calling
110 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
111 * the default software implementation, but only by accident. It isn't
112 * guaranteed to work with new ciphers or with alternative implementations of
113 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
114 * it, and instead start with a fresh context.
115 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100116static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100117 int use_psa, size_t tag_len, const data_t *key, int direction )
118{
119 mbedtls_cipher_free( ctx );
120 mbedtls_cipher_init( ctx );
121
Przemek Stekiele58ca8b2022-05-13 15:48:41 +0200122#if !defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_DEPRECATED_REMOVED) || \
123 !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100124 (void) use_psa;
125 (void) tag_len;
126#else
127 if( use_psa == 1 )
128 {
129 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
130 mbedtls_cipher_info_from_type( cipher_id ),
131 tag_len ) );
132 }
133 else
Przemek Stekiele58ca8b2022-05-13 15:48:41 +0200134#endif /* !MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_DEPRECATED_REMOVED ||
135 !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100136 {
137 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
138 mbedtls_cipher_info_from_type( cipher_id ) ) );
139 }
140
141 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
142 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100143 return( 1 );
144
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100145exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100146 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100147}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100148
149/*
150 * Check if a buffer is all-0 bytes:
151 * return 1 if it is,
152 * 0 if it isn't.
153 */
154int buffer_is_all_zero( const uint8_t *buf, size_t size )
155{
156 for( size_t i = 0; i < size; i++ )
157 if( buf[i] != 0 )
158 return 0;
159 return 1;
160}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100161#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200167 * END_DEPENDENCIES
168 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000169
Paul Bakker33b43f12013-08-20 11:48:36 +0200170/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100171void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100172{
173 const int *cipher_type;
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200176 {
177 const mbedtls_cipher_info_t *info =
178 mbedtls_cipher_info_from_type( *cipher_type );
179 mbedtls_test_set_step( *cipher_type );
180 if( ! check_cipher_info( *cipher_type, info ) )
181 goto exit;
182 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100183}
184/* END_CASE */
185
186/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200188{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 mbedtls_cipher_context_t valid_ctx;
190 mbedtls_cipher_context_t invalid_ctx;
191 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
192 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
193 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
194 int valid_size = sizeof(valid_buffer);
195 int valid_bitlen = valid_size * 8;
196 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
197 *( mbedtls_cipher_list() ) );
198 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200199
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 mbedtls_cipher_init( &valid_ctx );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500203 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200204
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100205 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
206
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 /* mbedtls_cipher_setup() */
208 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
209 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200210
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500211 /* mbedtls_cipher_get_block_size() */
212 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200213
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 /* mbedtls_cipher_get_cipher_mode() */
215 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
216 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200217
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 /* mbedtls_cipher_get_iv_size() */
219 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200220
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 /* mbedtls_cipher_get_type() */
222 TEST_ASSERT(
223 mbedtls_cipher_get_type( &invalid_ctx ) ==
224 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200225
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 /* mbedtls_cipher_get_name() */
227 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200228
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 /* mbedtls_cipher_get_key_bitlen() */
230 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
231 MBEDTLS_KEY_LENGTH_NONE );
232
233 /* mbedtls_cipher_get_operation() */
234 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
235 MBEDTLS_OPERATION_NONE );
236
237 /* mbedtls_cipher_setkey() */
238 TEST_ASSERT(
239 mbedtls_cipher_setkey( &invalid_ctx,
240 valid_buffer,
241 valid_bitlen,
242 valid_operation ) ==
243 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
244
245 /* mbedtls_cipher_set_iv() */
246 TEST_ASSERT(
247 mbedtls_cipher_set_iv( &invalid_ctx,
248 valid_buffer,
249 valid_size ) ==
250 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
251
252 /* mbedtls_cipher_reset() */
253 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
254 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200255
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200256#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500257 /* mbedtls_cipher_update_ad() */
258 TEST_ASSERT(
259 mbedtls_cipher_update_ad( &invalid_ctx,
260 valid_buffer,
261 valid_size ) ==
262 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
263#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
264
265#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
266 /* mbedtls_cipher_set_padding_mode() */
267 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
268 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200269#endif
270
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500271 /* mbedtls_cipher_update() */
272 TEST_ASSERT(
273 mbedtls_cipher_update( &invalid_ctx,
274 valid_buffer,
275 valid_size,
276 valid_buffer,
277 &size_t_var ) ==
278 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200279
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 /* mbedtls_cipher_finish() */
281 TEST_ASSERT(
282 mbedtls_cipher_finish( &invalid_ctx,
283 valid_buffer,
284 &size_t_var ) ==
285 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200286
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200287#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 /* mbedtls_cipher_write_tag() */
289 TEST_ASSERT(
290 mbedtls_cipher_write_tag( &invalid_ctx,
291 valid_buffer,
292 valid_size ) ==
293 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200294
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 /* mbedtls_cipher_check_tag() */
296 TEST_ASSERT(
297 mbedtls_cipher_check_tag( &invalid_ctx,
298 valid_buffer,
299 valid_size ) ==
300 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
301#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
302
303exit:
304 mbedtls_cipher_free( &invalid_ctx );
305 mbedtls_cipher_free( &valid_ctx );
306}
307/* END_CASE */
308
TRodziewicz062f3532021-05-25 15:15:57 +0200309/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310void cipher_invalid_param_conditional( )
311{
312 mbedtls_cipher_context_t valid_ctx;
313
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500314 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
316 int valid_size = sizeof(valid_buffer);
317 int valid_bitlen = valid_size * 8;
318 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
319 *( mbedtls_cipher_list() ) );
320
Ronald Cron875b5fb2021-05-21 08:50:00 +0200321 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
323 mbedtls_cipher_setkey( &valid_ctx,
324 valid_buffer,
325 valid_bitlen,
326 invalid_operation ) );
327
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328exit:
TRodziewicz70199552021-05-27 13:52:59 +0200329 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200330}
331/* END_CASE */
332
Paul Bakker6a9c7252016-07-14 13:46:10 +0100333/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100334void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100335{
336 const mbedtls_cipher_info_t *cipher_info;
337 mbedtls_cipher_context_t ctx;
338 unsigned char input[32];
339 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300340#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100341 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300342#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100343 size_t olen = 0;
344
345 mbedtls_cipher_init( &ctx );
346 memset( input, 0, sizeof( input ) );
347 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300348#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100349 memset( iv, 0, sizeof( iv ) );
350
351 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300352 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100353 TEST_ASSERT( NULL != cipher_info );
354
355 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
356
357 /* IV too big */
358 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
359 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
360
361 /* IV too small */
362 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
363 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
364
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300365 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300366 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300367#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300368 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300369 TEST_ASSERT( NULL != cipher_info );
370
371 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
372
Paul Bakker6a9c7252016-07-14 13:46:10 +0100373 /* Update ECB with partial block */
374 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
375 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
376
377exit:
378 mbedtls_cipher_free( &ctx );
379}
380/* END_CASE */
381
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200382/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100383void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200384 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200385{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200386 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100387 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000388 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200389 unsigned char ad[13];
390 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200391 unsigned char inbuf[64];
392 unsigned char encbuf[64];
393 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 const mbedtls_cipher_info_t *cipher_info;
396 mbedtls_cipher_context_t ctx_dec;
397 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000398
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200399 /*
400 * Prepare contexts
401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_cipher_init( &ctx_dec );
403 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200404
405 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
407 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200411 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
412 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
414 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200415 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
416 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
419 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200422 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
425 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200426 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200427#else
428 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200430
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200431 /*
432 * Do a few encode/decode cycles
433 */
434 for( i = 0; i < 3; i++ )
435 {
436 memset( iv , 0x00 + i, sizeof( iv ) );
437 memset( ad, 0x10 + i, sizeof( ad ) );
438 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
439
440 memset( encbuf, 0, sizeof( encbuf ) );
441 memset( decbuf, 0, sizeof( decbuf ) );
442 memset( tag, 0, sizeof( tag ) );
443
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200444 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
445 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
446 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100447 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
448 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100449 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200450 else
451 iv_len = sizeof(iv);
452
453 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
454 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
457 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200458
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200459#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
461 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200462#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200464 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
465 TEST_ASSERT( block_size != 0 );
466
Paul Bakker8123e9d2011-01-06 15:37:30 +0000467 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200469 total_len = outlen;
470
471 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200472 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200473 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200474 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200477 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000478
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200479#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200481#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200482
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200483 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200484 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200485 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200486 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000487
488 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200490 total_len = outlen;
491
492 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200493 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200494 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200495 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200498 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000499
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200500#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200502#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200503
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200504 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200505 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000506 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200507 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000508
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200509 /*
510 * Done
511 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_cipher_free( &ctx_dec );
514 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200515}
Paul Bakker33b43f12013-08-20 11:48:36 +0200516/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000517
Paul Bakker33b43f12013-08-20 11:48:36 +0200518/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100519void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
520 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200521{
Paul Bakker33b43f12013-08-20 11:48:36 +0200522 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200523 unsigned char key[32];
524 unsigned char iv[16];
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 const mbedtls_cipher_info_t *cipher_info;
527 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200528
529 unsigned char inbuf[64];
530 unsigned char encbuf[64];
531
532 size_t outlen = 0;
533
534 memset( key, 0, 32 );
535 memset( iv , 0, 16 );
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200538
539 memset( inbuf, 5, 64 );
540 memset( encbuf, 0, 64 );
541
542 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200544 TEST_ASSERT( NULL != cipher_info );
545
546 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200547 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
549#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
550 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200551#else
552 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
554 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
555 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200556#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200558#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200559
560 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
562 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200563
564 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200565exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200567}
Paul Bakker33b43f12013-08-20 11:48:36 +0200568/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200569
Paul Bakker33b43f12013-08-20 11:48:36 +0200570/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200571void dec_empty_buf( int cipher,
572 int expected_update_ret,
573 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200574{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100576
577 unsigned char *iv = NULL;
578 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_cipher_context_t ctx_dec;
581 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000582
583 unsigned char encbuf[64];
584 unsigned char decbuf[64];
585
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000586 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000587
588 memset( key, 0, 32 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200591
Paul Bakker8123e9d2011-01-06 15:37:30 +0000592 memset( encbuf, 0, 64 );
593 memset( decbuf, 0, 64 );
594
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200595 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100596 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000597 TEST_ASSERT( NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100598
599 if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
600 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
601 iv_len = 12;
602
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100603 ASSERT_ALLOC( iv, iv_len );
604 memset( iv , 0, iv_len );
605
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100606 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200607
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200608 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100610 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
611 key, cipher_info->key_bitlen,
612 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100614 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200617
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200618#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200620#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621
622 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200623 TEST_ASSERT( expected_update_ret ==
624 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000625 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100626
k-stachowiakd8727232019-07-29 17:46:29 +0200627 if ( expected_finish_ret == 0 &&
628 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
629 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100630 {
631 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
632 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200633 * decrypting an empty buffer.
634 * On the other hand, CBC and ECB ciphers need a full block of input.
635 */
636 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100637 }
638
k-stachowiakd8727232019-07-29 17:46:29 +0200639 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
640 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000641 TEST_ASSERT( 0 == outlen );
642
Paul Bakkerbd51b262014-07-10 15:26:12 +0200643exit:
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100644 mbedtls_free( iv );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200646}
Paul Bakker33b43f12013-08-20 11:48:36 +0200647/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000648
Paul Bakker33b43f12013-08-20 11:48:36 +0200649/* BEGIN_CASE */
650void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700651 int second_length_val, int pad_mode,
652 int first_encrypt_output_len, int second_encrypt_output_len,
653 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200654{
Paul Bakker33b43f12013-08-20 11:48:36 +0200655 size_t first_length = first_length_val;
656 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000657 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200658 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200659 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000660 unsigned char key[32];
661 unsigned char iv[16];
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_cipher_context_t ctx_dec;
664 mbedtls_cipher_context_t ctx_enc;
665 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666
667 unsigned char inbuf[64];
668 unsigned char encbuf[64];
669 unsigned char decbuf[64];
670
Paul Bakker23986e52011-04-24 08:57:21 +0000671 size_t outlen = 0;
672 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673
674 memset( key, 0, 32 );
675 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_cipher_init( &ctx_dec );
678 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200679
Paul Bakker8123e9d2011-01-06 15:37:30 +0000680 memset( inbuf, 5, 64 );
681 memset( encbuf, 0, 64 );
682 memset( decbuf, 0, 64 );
683
684 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200687
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200688 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
689 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
692 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000693
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700694#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
695 if( -1 != pad_mode )
696 {
697 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
698 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
699 }
700#else
701 (void) pad_mode;
702#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
703
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200704 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
705 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
706 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100707 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
708 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100709 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200710 else
711 iv_len = sizeof(iv);
712
713 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
714 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
717 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200718
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200719#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
721 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200722#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200724 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
725 TEST_ASSERT( block_size != 0 );
726
Paul Bakker8123e9d2011-01-06 15:37:30 +0000727 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700729 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000730 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700732 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200734 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200735 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200736 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200737 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000740 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200741 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200742 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200743 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200744 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000745
746 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700747 second_length = totaloutlen - first_length;
748 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
749 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200750 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700751 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
752 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
753 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200754
755 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200756 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200757 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200758 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200759
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700760 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200761 totaloutlen += outlen;
762
763 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000764
765 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
766
Paul Bakkerbd51b262014-07-10 15:26:12 +0200767exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_cipher_free( &ctx_dec );
769 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200770}
Paul Bakker33b43f12013-08-20 11:48:36 +0200771/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000772
Paul Bakker33b43f12013-08-20 11:48:36 +0200773/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100774void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
775 data_t * iv, data_t * cipher,
776 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100777 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200778{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200779 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200781 size_t outlen, total_len;
782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200784
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200785 memset( output, 0x00, sizeof( output ) );
786
Azim Khanf1aaec92017-05-30 14:23:15 +0100787#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100788 ((void) ad);
789 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200790#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200791
792 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200793 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100795 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200797 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200799#else
800 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100802 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200804#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100805 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200806#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200807
Azim Khand30ca132017-06-09 04:32:58 +0100808 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200809 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100810 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200811 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200813 &outlen ) );
814 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200815#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100816 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200817#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200818
819 /* check plaintext only if everything went fine */
820 if( 0 == finish_result && 0 == tag_result )
821 {
Azim Khand30ca132017-06-09 04:32:58 +0100822 TEST_ASSERT( total_len == clear->len );
823 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200824 }
825
Paul Bakkerbd51b262014-07-10 15:26:12 +0200826exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200828}
829/* END_CASE */
830
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100831/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100832void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
833 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000834 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200835{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100836 /*
837 * Take an AEAD ciphertext + tag and perform a pair
838 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000839 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100840 * decryption and encryption are inverse to one another.
841 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000842
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200843 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100844 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200847 size_t outlen;
848
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100849 unsigned char *cipher_plus_tag = NULL;
850 size_t cipher_plus_tag_len;
851 unsigned char *decrypt_buf = NULL;
852 size_t decrypt_buf_len = 0;
853 unsigned char *encrypt_buf = NULL;
854 size_t encrypt_buf_len = 0;
855
Gilles Peskine70edd682020-12-03 20:27:27 +0100856 /* Null pointers are documented as valid for inputs of length 0.
857 * The test framework passes non-null pointers, so set them to NULL.
858 * key, cipher and tag can't be empty. */
859 if( iv->len == 0 )
860 iv->x = NULL;
861 if( ad->len == 0 )
862 ad->x = NULL;
863 if( clear->len == 0 )
864 clear->x = NULL;
865
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100866 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200867
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100868 /* Initialize PSA Crypto */
869#if defined(MBEDTLS_USE_PSA_CRYPTO)
870 if( use_psa == 1 )
871 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000872#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100873 (void) use_psa;
874#endif
875
876 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100877 * Are we using NIST_KW? with padding?
878 */
879 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
880 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
881 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
882 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
883 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
884 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
885 using_nist_kw_padding;
886
887 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100888 * Prepare context for decryption
889 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100890 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
891 MBEDTLS_DECRYPT ) )
892 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200893
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100894 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100895 * prepare buffer for decryption
896 * (we need the tag appended to the ciphertext)
897 */
898 cipher_plus_tag_len = cipher->len + tag->len;
899 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
900 memcpy( cipher_plus_tag, cipher->x, cipher->len );
901 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
902
903 /*
904 * Compute length of output buffer according to the documentation
905 */
906 if( using_nist_kw )
907 decrypt_buf_len = cipher_plus_tag_len - 8;
908 else
909 decrypt_buf_len = cipher_plus_tag_len - tag->len;
910
911
912 /*
913 * Try decrypting to a buffer that's 1B too small
914 */
915 if( decrypt_buf_len != 0 )
916 {
917 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
918
919 outlen = 0;
920 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
921 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
922 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
923 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
924
925 mbedtls_free( decrypt_buf );
926 decrypt_buf = NULL;
927 }
928
929 /*
930 * Authenticate and decrypt, and check result
931 */
932 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
933
934 outlen = 0;
935 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
936 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
937 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
938
939 if( strcmp( result, "FAIL" ) == 0 )
940 {
941 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100942 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100943 }
944 else
945 {
946 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100947 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100948 }
949
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100950 mbedtls_free( decrypt_buf );
951 decrypt_buf = NULL;
952
953 /*
954 * Encrypt back if test data was authentic
955 */
956 if( strcmp( result, "FAIL" ) != 0 )
957 {
958 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100959 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
960 MBEDTLS_ENCRYPT ) )
961 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100962
963 /*
964 * Compute size of output buffer according to documentation
965 */
966 if( using_nist_kw )
967 {
968 encrypt_buf_len = clear->len + 8;
969 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
970 encrypt_buf_len += 8 - encrypt_buf_len % 8;
971 }
972 else
973 {
974 encrypt_buf_len = clear->len + tag->len;
975 }
976
977 /*
978 * Try encrypting with an output buffer that's 1B too small
979 */
980 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
981
982 outlen = 0;
983 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
984 ad->x, ad->len, clear->x, clear->len,
985 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
986 TEST_ASSERT( ret != 0 );
987
988 mbedtls_free( encrypt_buf );
989 encrypt_buf = NULL;
990
991 /*
992 * Encrypt and check the result
993 */
994 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
995
996 outlen = 0;
997 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
998 ad->x, ad->len, clear->x, clear->len,
999 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
1000 TEST_ASSERT( ret == 0 );
1001
1002 TEST_ASSERT( outlen == cipher->len + tag->len );
1003 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
1004 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
1005 tag->x, tag->len ) == 0 );
1006
1007 mbedtls_free( encrypt_buf );
1008 encrypt_buf = NULL;
1009 }
1010
Paul Bakkerbd51b262014-07-10 15:26:12 +02001011exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001012
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001013 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001014 mbedtls_free( decrypt_buf );
1015 mbedtls_free( encrypt_buf );
1016 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001017
Hanno Beckera13272d2018-11-12 16:27:30 +00001018#if defined(MBEDTLS_USE_PSA_CRYPTO)
1019 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001020 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +00001021#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001022}
1023/* END_CASE */
1024
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001025/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001026void test_vec_ecb( int cipher_id, int operation, data_t * key,
1027 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001028 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001029{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001031 unsigned char output[32];
1032 size_t outlen;
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001035
Paul Bakker5e0efa72013-09-08 23:04:04 +02001036 memset( output, 0x00, sizeof( output ) );
1037
1038 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001039 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001041
Paul Bakker5e0efa72013-09-08 23:04:04 +02001042
Azim Khand30ca132017-06-09 04:32:58 +01001043 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001044
Azim Khand30ca132017-06-09 04:32:58 +01001045 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001047 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1049 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001050 &outlen ) );
1051 TEST_ASSERT( 0 == outlen );
1052
1053 /* check plaintext only if everything went fine */
1054 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001055 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001057
Paul Bakkerbd51b262014-07-10 15:26:12 +02001058exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001060}
1061/* END_CASE */
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +02001064void test_vec_crypt( int cipher_id, int operation, data_t *key,
1065 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +00001066 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +03001067{
Ron Eldor7b012442017-09-25 17:03:12 +03001068 mbedtls_cipher_context_t ctx;
1069 unsigned char output[32];
1070 size_t outlen;
1071
1072 mbedtls_cipher_init( &ctx );
1073
Ron Eldor7b012442017-09-25 17:03:12 +03001074 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001075
1076 /* Prepare context */
Przemek Stekiele58ca8b2022-05-13 15:48:41 +02001077#if !defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_DEPRECATED_REMOVED) || \
1078 !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001079 (void) use_psa;
1080#else
1081 if( use_psa == 1 )
1082 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001083 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001084 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001085 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001086 }
1087 else
Przemek Stekiele58ca8b2022-05-13 15:48:41 +02001088#endif /* !MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_DEPRECATED_REMOVED ||
1089 !MBEDTLS_TEST_DEPRECATED*/
Ron Eldor7b012442017-09-25 17:03:12 +03001090 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001091 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001092
Ronald Cron9ed40732020-06-25 09:03:34 +02001093 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001094 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1095 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1096
Ronald Cron9ed40732020-06-25 09:03:34 +02001097 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1098 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001099 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001100 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001101 /* check plaintext only if everything went fine */
1102 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001103 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001104
1105exit:
1106 mbedtls_cipher_free( &ctx );
Przemek Stekiel61922d12022-05-12 13:51:51 +02001107#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001108 PSA_DONE( );
Przemek Stekiel61922d12022-05-12 13:51:51 +02001109#endif /* !MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_DEPRECATED_REMOVED */
Ron Eldor7b012442017-09-25 17:03:12 +03001110}
1111/* END_CASE */
1112
1113/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001114void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 const mbedtls_cipher_info_t *cipher_info;
1117 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001122 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001123 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001126
Paul Bakkerbd51b262014-07-10 15:26:12 +02001127exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001128 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001129}
Paul Bakker33b43f12013-08-20 11:48:36 +02001130/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001133void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001134 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001135{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_cipher_info_t cipher_info;
1137 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001138 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001139
1140 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 mbedtls_cipher_init( &ctx );
1142 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001143 ctx.cipher_info = &cipher_info;
1144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001146
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001147
Azim Khand30ca132017-06-09 04:32:58 +01001148 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001149 if( 0 == ret )
1150 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001151}
Paul Bakker33b43f12013-08-20 11:48:36 +02001152/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001153
1154/* BEGIN_CASE */
Thomas Daubney5dcbc4d2022-02-17 13:46:27 +00001155void iv_len_validity( int cipher_id, char * cipher_string,
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001156 int iv_len_val, int ret )
1157{
1158 size_t iv_len = iv_len_val;
1159 unsigned char iv[16];
1160
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001161 /* Initialise iv buffer */
1162 memset( iv, 0, sizeof( iv ) );
1163
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001164 const mbedtls_cipher_info_t *cipher_info;
1165 mbedtls_cipher_context_t ctx_dec;
1166 mbedtls_cipher_context_t ctx_enc;
1167
1168 /*
1169 * Prepare contexts
1170 */
1171 mbedtls_cipher_init( &ctx_dec );
1172 mbedtls_cipher_init( &ctx_enc );
1173
1174 /* Check and get info structures */
1175 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
1176 TEST_ASSERT( NULL != cipher_info );
1177 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
1178 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
1179 cipher_string ) == 0 );
1180
1181 /* Initialise enc and dec contexts */
1182 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
1183 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
1184
1185 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
1186 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
1187
1188exit:
1189 mbedtls_cipher_free( &ctx_dec );
1190 mbedtls_cipher_free( &ctx_enc );
1191}
1192/* END_CASE */