blob: 2efc434dc413a381a7498647e7e8207b4508f2b0 [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
122#if !defined(MBEDTLS_USE_PSA_CRYPTO)
123 (void) use_psa;
124 (void) tag_len;
125#else
126 if( use_psa == 1 )
127 {
128 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
129 mbedtls_cipher_info_from_type( cipher_id ),
130 tag_len ) );
131 }
132 else
133#endif /* MBEDTLS_USE_PSA_CRYPTO */
134 {
135 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
136 mbedtls_cipher_info_from_type( cipher_id ) ) );
137 }
138
139 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
140 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100141 return( 1 );
142
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100143exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100144 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100145}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100146
147/*
148 * Check if a buffer is all-0 bytes:
149 * return 1 if it is,
150 * 0 if it isn't.
151 */
152int buffer_is_all_zero( const uint8_t *buf, size_t size )
153{
154 for( size_t i = 0; i < size; i++ )
155 if( buf[i] != 0 )
156 return 0;
157 return 1;
158}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100159#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
160
Paul Bakker33b43f12013-08-20 11:48:36 +0200161/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000162
Paul Bakker33b43f12013-08-20 11:48:36 +0200163/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200165 * END_DEPENDENCIES
166 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000167
Paul Bakker33b43f12013-08-20 11:48:36 +0200168/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100169void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100170{
171 const int *cipher_type;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200174 {
175 const mbedtls_cipher_info_t *info =
176 mbedtls_cipher_info_from_type( *cipher_type );
177 mbedtls_test_set_step( *cipher_type );
178 if( ! check_cipher_info( *cipher_type, info ) )
179 goto exit;
180 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100181}
182/* END_CASE */
183
184/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200186{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 mbedtls_cipher_context_t valid_ctx;
188 mbedtls_cipher_context_t invalid_ctx;
189 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
190 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
191 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
192 int valid_size = sizeof(valid_buffer);
193 int valid_bitlen = valid_size * 8;
194 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
195 *( mbedtls_cipher_list() ) );
196 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200197
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200199
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500200 mbedtls_cipher_init( &valid_ctx );
201 mbedtls_cipher_setup( &valid_ctx, valid_info );
202 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200203
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 /* mbedtls_cipher_setup() */
205 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
206 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200207
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500208 /* mbedtls_cipher_get_block_size() */
209 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200210
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500211 /* mbedtls_cipher_get_cipher_mode() */
212 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
213 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200214
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 /* mbedtls_cipher_get_iv_size() */
216 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200217
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 /* mbedtls_cipher_get_type() */
219 TEST_ASSERT(
220 mbedtls_cipher_get_type( &invalid_ctx ) ==
221 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200222
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500223 /* mbedtls_cipher_get_name() */
224 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200225
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 /* mbedtls_cipher_get_key_bitlen() */
227 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
228 MBEDTLS_KEY_LENGTH_NONE );
229
230 /* mbedtls_cipher_get_operation() */
231 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
232 MBEDTLS_OPERATION_NONE );
233
234 /* mbedtls_cipher_setkey() */
235 TEST_ASSERT(
236 mbedtls_cipher_setkey( &invalid_ctx,
237 valid_buffer,
238 valid_bitlen,
239 valid_operation ) ==
240 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
241
242 /* mbedtls_cipher_set_iv() */
243 TEST_ASSERT(
244 mbedtls_cipher_set_iv( &invalid_ctx,
245 valid_buffer,
246 valid_size ) ==
247 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
248
249 /* mbedtls_cipher_reset() */
250 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
251 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200252
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200253#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 /* mbedtls_cipher_update_ad() */
255 TEST_ASSERT(
256 mbedtls_cipher_update_ad( &invalid_ctx,
257 valid_buffer,
258 valid_size ) ==
259 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
260#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
261
262#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
263 /* mbedtls_cipher_set_padding_mode() */
264 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
265 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200266#endif
267
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 /* mbedtls_cipher_update() */
269 TEST_ASSERT(
270 mbedtls_cipher_update( &invalid_ctx,
271 valid_buffer,
272 valid_size,
273 valid_buffer,
274 &size_t_var ) ==
275 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200276
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500277 /* mbedtls_cipher_finish() */
278 TEST_ASSERT(
279 mbedtls_cipher_finish( &invalid_ctx,
280 valid_buffer,
281 &size_t_var ) ==
282 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200283
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200284#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 /* mbedtls_cipher_write_tag() */
286 TEST_ASSERT(
287 mbedtls_cipher_write_tag( &invalid_ctx,
288 valid_buffer,
289 valid_size ) ==
290 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200291
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 /* mbedtls_cipher_check_tag() */
293 TEST_ASSERT(
294 mbedtls_cipher_check_tag( &invalid_ctx,
295 valid_buffer,
296 valid_size ) ==
297 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
298#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
299
300exit:
301 mbedtls_cipher_free( &invalid_ctx );
302 mbedtls_cipher_free( &valid_ctx );
303}
304/* END_CASE */
305
TRodziewicz062f3532021-05-25 15:15:57 +0200306/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500307void cipher_invalid_param_conditional( )
308{
309 mbedtls_cipher_context_t valid_ctx;
310
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500311 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
313 int valid_size = sizeof(valid_buffer);
314 int valid_bitlen = valid_size * 8;
315 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
316 *( mbedtls_cipher_list() ) );
317
Ronald Cron875b5fb2021-05-21 08:50:00 +0200318 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
320 mbedtls_cipher_setkey( &valid_ctx,
321 valid_buffer,
322 valid_bitlen,
323 invalid_operation ) );
324
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500325exit:
TRodziewicz70199552021-05-27 13:52:59 +0200326 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200327}
328/* END_CASE */
329
Paul Bakker6a9c7252016-07-14 13:46:10 +0100330/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100331void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100332{
333 const mbedtls_cipher_info_t *cipher_info;
334 mbedtls_cipher_context_t ctx;
335 unsigned char input[32];
336 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300337#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100338 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300339#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100340 size_t olen = 0;
341
342 mbedtls_cipher_init( &ctx );
343 memset( input, 0, sizeof( input ) );
344 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300345#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100346 memset( iv, 0, sizeof( iv ) );
347
348 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300349 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100350 TEST_ASSERT( NULL != cipher_info );
351
352 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
353
354 /* IV too big */
355 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
356 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
357
358 /* IV too small */
359 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
360 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
361
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300362 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300363 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300364#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300365 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300366 TEST_ASSERT( NULL != cipher_info );
367
368 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
369
Paul Bakker6a9c7252016-07-14 13:46:10 +0100370 /* Update ECB with partial block */
371 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
372 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
373
374exit:
375 mbedtls_cipher_free( &ctx );
376}
377/* END_CASE */
378
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200379/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100380void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200381 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200382{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200383 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100384 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200386 unsigned char ad[13];
387 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200388 unsigned char inbuf[64];
389 unsigned char encbuf[64];
390 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 const mbedtls_cipher_info_t *cipher_info;
393 mbedtls_cipher_context_t ctx_dec;
394 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200396 /*
397 * Prepare contexts
398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_cipher_init( &ctx_dec );
400 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200401
402 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000403
404 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200408 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
409 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000410
411 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200412 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
413 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
416 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200419 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
422 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200423 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200424#else
425 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200427
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200428 /*
429 * Do a few encode/decode cycles
430 */
431 for( i = 0; i < 3; i++ )
432 {
433 memset( iv , 0x00 + i, sizeof( iv ) );
434 memset( ad, 0x10 + i, sizeof( ad ) );
435 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
436
437 memset( encbuf, 0, sizeof( encbuf ) );
438 memset( decbuf, 0, sizeof( decbuf ) );
439 memset( tag, 0, sizeof( tag ) );
440
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200441 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
442 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
443 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
444 else
445 iv_len = sizeof(iv);
446
447 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
448 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
451 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200452
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200453#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
455 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200456#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200458 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
459 TEST_ASSERT( block_size != 0 );
460
Paul Bakker8123e9d2011-01-06 15:37:30 +0000461 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200463 total_len = outlen;
464
465 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200466 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200467 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200468 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200471 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000472
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200473#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200475#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200476
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200477 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200478 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200479 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200480 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000481
482 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200484 total_len = outlen;
485
486 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200487 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200488 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200489 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200492 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000493
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200494#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200496#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200497
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200498 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200499 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000500 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200501 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200503 /*
504 * Done
505 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200506exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 mbedtls_cipher_free( &ctx_dec );
508 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200509}
Paul Bakker33b43f12013-08-20 11:48:36 +0200510/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000511
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100513void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
514 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200515{
Paul Bakker33b43f12013-08-20 11:48:36 +0200516 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200517 unsigned char key[32];
518 unsigned char iv[16];
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 const mbedtls_cipher_info_t *cipher_info;
521 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200522
523 unsigned char inbuf[64];
524 unsigned char encbuf[64];
525
526 size_t outlen = 0;
527
528 memset( key, 0, 32 );
529 memset( iv , 0, 16 );
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200532
533 memset( inbuf, 5, 64 );
534 memset( encbuf, 0, 64 );
535
536 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200538 TEST_ASSERT( NULL != cipher_info );
539
540 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200541 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
543#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
544 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200545#else
546 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
548 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
549 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200550#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200552#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200553
554 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
556 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200557
558 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200559exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200561}
Paul Bakker33b43f12013-08-20 11:48:36 +0200562/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200563
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200565void dec_empty_buf( int cipher,
566 int expected_update_ret,
567 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200568{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000569 unsigned char key[32];
570 unsigned char iv[16];
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_cipher_context_t ctx_dec;
573 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000574
575 unsigned char encbuf[64];
576 unsigned char decbuf[64];
577
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000578 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000579
580 memset( key, 0, 32 );
581 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200584
Paul Bakker8123e9d2011-01-06 15:37:30 +0000585 memset( encbuf, 0, 64 );
586 memset( decbuf, 0, 64 );
587
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200588 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100589 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000590 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100591 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200592
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200593 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000594
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100595 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
596 key, cipher_info->key_bitlen,
597 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200602
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200603#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200605#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
607 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200608 TEST_ASSERT( expected_update_ret ==
609 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000610 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100611
k-stachowiakd8727232019-07-29 17:46:29 +0200612 if ( expected_finish_ret == 0 &&
613 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
614 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100615 {
616 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
617 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200618 * decrypting an empty buffer.
619 * On the other hand, CBC and ECB ciphers need a full block of input.
620 */
621 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100622 }
623
k-stachowiakd8727232019-07-29 17:46:29 +0200624 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
625 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626 TEST_ASSERT( 0 == outlen );
627
Paul Bakkerbd51b262014-07-10 15:26:12 +0200628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200630}
Paul Bakker33b43f12013-08-20 11:48:36 +0200631/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000632
Paul Bakker33b43f12013-08-20 11:48:36 +0200633/* BEGIN_CASE */
634void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700635 int second_length_val, int pad_mode,
636 int first_encrypt_output_len, int second_encrypt_output_len,
637 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200638{
Paul Bakker33b43f12013-08-20 11:48:36 +0200639 size_t first_length = first_length_val;
640 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000641 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200642 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200643 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644 unsigned char key[32];
645 unsigned char iv[16];
646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_cipher_context_t ctx_dec;
648 mbedtls_cipher_context_t ctx_enc;
649 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000650
651 unsigned char inbuf[64];
652 unsigned char encbuf[64];
653 unsigned char decbuf[64];
654
Paul Bakker23986e52011-04-24 08:57:21 +0000655 size_t outlen = 0;
656 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000657
658 memset( key, 0, 32 );
659 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_cipher_init( &ctx_dec );
662 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200663
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 memset( inbuf, 5, 64 );
665 memset( encbuf, 0, 64 );
666 memset( decbuf, 0, 64 );
667
668 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000670 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200671
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200672 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
673 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
676 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000677
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700678#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
679 if( -1 != pad_mode )
680 {
681 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
682 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
683 }
684#else
685 (void) pad_mode;
686#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
687
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200688 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
689 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
690 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
691 else
692 iv_len = sizeof(iv);
693
694 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
695 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
698 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200699
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200700#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
702 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200703#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000704
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200705 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
706 TEST_ASSERT( block_size != 0 );
707
Paul Bakker8123e9d2011-01-06 15:37:30 +0000708 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700710 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000711 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700713 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000714 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200715 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200716 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200717 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200718 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000721 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200722 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200723 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200724 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200725 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726
727 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700728 second_length = totaloutlen - first_length;
729 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
730 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200731 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700732 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
733 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
734 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200735
736 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200737 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200738 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200739 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200740
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700741 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200742 totaloutlen += outlen;
743
744 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000745
746 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
747
Paul Bakkerbd51b262014-07-10 15:26:12 +0200748exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_cipher_free( &ctx_dec );
750 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200751}
Paul Bakker33b43f12013-08-20 11:48:36 +0200752/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000753
Paul Bakker33b43f12013-08-20 11:48:36 +0200754/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100755void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
756 data_t * iv, data_t * cipher,
757 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100758 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200759{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200760 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200762 size_t outlen, total_len;
763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200765
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200766 memset( output, 0x00, sizeof( output ) );
767
Azim Khanf1aaec92017-05-30 14:23:15 +0100768#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100769 ((void) ad);
770 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200771#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200772
773 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200774 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100776 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200778 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200780#else
781 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100783 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200785#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100786 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200787#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200788
Azim Khand30ca132017-06-09 04:32:58 +0100789 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200790 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100791 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200792 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200794 &outlen ) );
795 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200796#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100797 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200798#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200799
800 /* check plaintext only if everything went fine */
801 if( 0 == finish_result && 0 == tag_result )
802 {
Azim Khand30ca132017-06-09 04:32:58 +0100803 TEST_ASSERT( total_len == clear->len );
804 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200805 }
806
Paul Bakkerbd51b262014-07-10 15:26:12 +0200807exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200809}
810/* END_CASE */
811
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100812/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100813void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
814 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000815 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200816{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100817 /*
818 * Take an AEAD ciphertext + tag and perform a pair
819 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000820 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100821 * decryption and encryption are inverse to one another.
822 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000823
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200824 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100825 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200828 size_t outlen;
829
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100830 unsigned char *cipher_plus_tag = NULL;
831 size_t cipher_plus_tag_len;
832 unsigned char *decrypt_buf = NULL;
833 size_t decrypt_buf_len = 0;
834 unsigned char *encrypt_buf = NULL;
835 size_t encrypt_buf_len = 0;
836
Gilles Peskine70edd682020-12-03 20:27:27 +0100837 /* Null pointers are documented as valid for inputs of length 0.
838 * The test framework passes non-null pointers, so set them to NULL.
839 * key, cipher and tag can't be empty. */
840 if( iv->len == 0 )
841 iv->x = NULL;
842 if( ad->len == 0 )
843 ad->x = NULL;
844 if( clear->len == 0 )
845 clear->x = NULL;
846
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100847 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200848
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100849 /* Initialize PSA Crypto */
850#if defined(MBEDTLS_USE_PSA_CRYPTO)
851 if( use_psa == 1 )
852 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000853#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100854 (void) use_psa;
855#endif
856
857 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100858 * Are we using NIST_KW? with padding?
859 */
860 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
861 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
862 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
863 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
864 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
865 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
866 using_nist_kw_padding;
867
868 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100869 * Prepare context for decryption
870 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100871 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
872 MBEDTLS_DECRYPT ) )
873 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200874
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100875 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100876 * prepare buffer for decryption
877 * (we need the tag appended to the ciphertext)
878 */
879 cipher_plus_tag_len = cipher->len + tag->len;
880 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
881 memcpy( cipher_plus_tag, cipher->x, cipher->len );
882 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
883
884 /*
885 * Compute length of output buffer according to the documentation
886 */
887 if( using_nist_kw )
888 decrypt_buf_len = cipher_plus_tag_len - 8;
889 else
890 decrypt_buf_len = cipher_plus_tag_len - tag->len;
891
892
893 /*
894 * Try decrypting to a buffer that's 1B too small
895 */
896 if( decrypt_buf_len != 0 )
897 {
898 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
899
900 outlen = 0;
901 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
902 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
903 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
904 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
905
906 mbedtls_free( decrypt_buf );
907 decrypt_buf = NULL;
908 }
909
910 /*
911 * Authenticate and decrypt, and check result
912 */
913 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
914
915 outlen = 0;
916 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
917 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
918 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
919
920 if( strcmp( result, "FAIL" ) == 0 )
921 {
922 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100923 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100924 }
925 else
926 {
927 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100928 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100929 }
930
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100931 mbedtls_free( decrypt_buf );
932 decrypt_buf = NULL;
933
934 /*
935 * Encrypt back if test data was authentic
936 */
937 if( strcmp( result, "FAIL" ) != 0 )
938 {
939 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100940 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
941 MBEDTLS_ENCRYPT ) )
942 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100943
944 /*
945 * Compute size of output buffer according to documentation
946 */
947 if( using_nist_kw )
948 {
949 encrypt_buf_len = clear->len + 8;
950 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
951 encrypt_buf_len += 8 - encrypt_buf_len % 8;
952 }
953 else
954 {
955 encrypt_buf_len = clear->len + tag->len;
956 }
957
958 /*
959 * Try encrypting with an output buffer that's 1B too small
960 */
961 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
962
963 outlen = 0;
964 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
965 ad->x, ad->len, clear->x, clear->len,
966 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
967 TEST_ASSERT( ret != 0 );
968
969 mbedtls_free( encrypt_buf );
970 encrypt_buf = NULL;
971
972 /*
973 * Encrypt and check the result
974 */
975 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
976
977 outlen = 0;
978 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
979 ad->x, ad->len, clear->x, clear->len,
980 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
981 TEST_ASSERT( ret == 0 );
982
983 TEST_ASSERT( outlen == cipher->len + tag->len );
984 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
985 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
986 tag->x, tag->len ) == 0 );
987
988 mbedtls_free( encrypt_buf );
989 encrypt_buf = NULL;
990 }
991
Paul Bakkerbd51b262014-07-10 15:26:12 +0200992exit:
Hanno Beckera13272d2018-11-12 16:27:30 +0000993
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200994 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100995 mbedtls_free( decrypt_buf );
996 mbedtls_free( encrypt_buf );
997 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200998
Hanno Beckera13272d2018-11-12 16:27:30 +0000999#if defined(MBEDTLS_USE_PSA_CRYPTO)
1000 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001001 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +00001002#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001003}
1004/* END_CASE */
1005
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001006/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001007void test_vec_ecb( int cipher_id, int operation, data_t * key,
1008 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001009 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001010{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001012 unsigned char output[32];
1013 size_t outlen;
1014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001016
Paul Bakker5e0efa72013-09-08 23:04:04 +02001017 memset( output, 0x00, sizeof( output ) );
1018
1019 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001020 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001022
Paul Bakker5e0efa72013-09-08 23:04:04 +02001023
Azim Khand30ca132017-06-09 04:32:58 +01001024 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001025
Azim Khand30ca132017-06-09 04:32:58 +01001026 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001028 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1030 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001031 &outlen ) );
1032 TEST_ASSERT( 0 == outlen );
1033
1034 /* check plaintext only if everything went fine */
1035 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001036 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001038
Paul Bakkerbd51b262014-07-10 15:26:12 +02001039exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001041}
1042/* END_CASE */
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +02001045void test_vec_crypt( int cipher_id, int operation, data_t *key,
1046 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +00001047 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +03001048{
Ron Eldor7b012442017-09-25 17:03:12 +03001049 mbedtls_cipher_context_t ctx;
1050 unsigned char output[32];
1051 size_t outlen;
1052
1053 mbedtls_cipher_init( &ctx );
1054
Ron Eldor7b012442017-09-25 17:03:12 +03001055 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001056
1057 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +00001058#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1059 (void) use_psa;
1060#else
1061 if( use_psa == 1 )
1062 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001063 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001064 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001065 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001066 }
1067 else
1068#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001069 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001070 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001071
Ronald Cron9ed40732020-06-25 09:03:34 +02001072 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001073 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1074 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1075
Ronald Cron9ed40732020-06-25 09:03:34 +02001076 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1077 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001078 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001079 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001080 /* check plaintext only if everything went fine */
1081 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001082 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001083
1084exit:
1085 mbedtls_cipher_free( &ctx );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001086#if defined(MBEDTLS_USE_PSA_CRYPTO)
1087 PSA_DONE( );
1088#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001089}
1090/* END_CASE */
1091
1092/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001093void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 const mbedtls_cipher_info_t *cipher_info;
1096 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001101 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001102 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001105
Paul Bakkerbd51b262014-07-10 15:26:12 +02001106exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001108}
Paul Bakker33b43f12013-08-20 11:48:36 +02001109/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001112void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001113 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001114{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_cipher_info_t cipher_info;
1116 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001117 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001118
1119 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_cipher_init( &ctx );
1121 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001122 ctx.cipher_info = &cipher_info;
1123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001125
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001126
Azim Khand30ca132017-06-09 04:32:58 +01001127 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001128 if( 0 == ret )
1129 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001130}
Paul Bakker33b43f12013-08-20 11:48:36 +02001131/* END_CASE */