blob: 37468df71a6d8c2c8a598fbd5a48157a1134c324 [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 Stekiel476d9c42022-05-19 12:26:33 +0200122#if !defined(MBEDTLS_USE_PSA_CRYPO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100123 (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
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200133#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100134 {
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 );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200202
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100203 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
204
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 /* mbedtls_cipher_setup() */
206 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
207 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 /* mbedtls_cipher_get_block_size() */
210 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200211
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 /* mbedtls_cipher_get_cipher_mode() */
213 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
214 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200215
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 /* mbedtls_cipher_get_iv_size() */
217 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200218
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 /* mbedtls_cipher_get_type() */
220 TEST_ASSERT(
221 mbedtls_cipher_get_type( &invalid_ctx ) ==
222 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200223
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 /* mbedtls_cipher_get_name() */
225 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200226
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 /* mbedtls_cipher_get_key_bitlen() */
228 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
229 MBEDTLS_KEY_LENGTH_NONE );
230
231 /* mbedtls_cipher_get_operation() */
232 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
233 MBEDTLS_OPERATION_NONE );
234
235 /* mbedtls_cipher_setkey() */
236 TEST_ASSERT(
237 mbedtls_cipher_setkey( &invalid_ctx,
238 valid_buffer,
239 valid_bitlen,
240 valid_operation ) ==
241 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
242
243 /* mbedtls_cipher_set_iv() */
244 TEST_ASSERT(
245 mbedtls_cipher_set_iv( &invalid_ctx,
246 valid_buffer,
247 valid_size ) ==
248 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
249
250 /* mbedtls_cipher_reset() */
251 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
252 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200253
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200254#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 /* mbedtls_cipher_update_ad() */
256 TEST_ASSERT(
257 mbedtls_cipher_update_ad( &invalid_ctx,
258 valid_buffer,
259 valid_size ) ==
260 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
261#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
262
263#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
264 /* mbedtls_cipher_set_padding_mode() */
265 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
266 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200267#endif
268
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500269 /* mbedtls_cipher_update() */
270 TEST_ASSERT(
271 mbedtls_cipher_update( &invalid_ctx,
272 valid_buffer,
273 valid_size,
274 valid_buffer,
275 &size_t_var ) ==
276 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200277
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 /* mbedtls_cipher_finish() */
279 TEST_ASSERT(
280 mbedtls_cipher_finish( &invalid_ctx,
281 valid_buffer,
282 &size_t_var ) ==
283 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200284
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200285#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 /* mbedtls_cipher_write_tag() */
287 TEST_ASSERT(
288 mbedtls_cipher_write_tag( &invalid_ctx,
289 valid_buffer,
290 valid_size ) ==
291 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200292
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500293 /* mbedtls_cipher_check_tag() */
294 TEST_ASSERT(
295 mbedtls_cipher_check_tag( &invalid_ctx,
296 valid_buffer,
297 valid_size ) ==
298 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
299#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
300
301exit:
302 mbedtls_cipher_free( &invalid_ctx );
303 mbedtls_cipher_free( &valid_ctx );
304}
305/* END_CASE */
306
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100307/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308void cipher_invalid_param_conditional( )
309{
310 mbedtls_cipher_context_t valid_ctx;
311
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500312 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
314 int valid_size = sizeof(valid_buffer);
315 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316
Ronald Cron875b5fb2021-05-21 08:50:00 +0200317 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
319 mbedtls_cipher_setkey( &valid_ctx,
320 valid_buffer,
321 valid_bitlen,
322 invalid_operation ) );
323
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324exit:
TRodziewicz70199552021-05-27 13:52:59 +0200325 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200326}
327/* END_CASE */
328
Paul Bakker6a9c7252016-07-14 13:46:10 +0100329/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100330void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100331{
332 const mbedtls_cipher_info_t *cipher_info;
333 mbedtls_cipher_context_t ctx;
334 unsigned char input[32];
335 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300336#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100337 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300338#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100339 size_t olen = 0;
340
341 mbedtls_cipher_init( &ctx );
342 memset( input, 0, sizeof( input ) );
343 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300344#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100345 memset( iv, 0, sizeof( iv ) );
346
347 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300348 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100349 TEST_ASSERT( NULL != cipher_info );
350
351 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
352
353 /* IV too big */
354 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
355 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
356
357 /* IV too small */
358 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
359 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
360
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300361 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300362 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300363#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300364 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300365 TEST_ASSERT( NULL != cipher_info );
366
367 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
368
Paul Bakker6a9c7252016-07-14 13:46:10 +0100369 /* Update ECB with partial block */
370 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
371 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
372
373exit:
374 mbedtls_cipher_free( &ctx );
375}
376/* END_CASE */
377
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200378/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100379void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200380 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200381{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200382 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100383 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000384 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200385 unsigned char ad[13];
386 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200387 unsigned char inbuf[64];
388 unsigned char encbuf[64];
389 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 const mbedtls_cipher_info_t *cipher_info;
392 mbedtls_cipher_context_t ctx_dec;
393 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000394
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200395 /*
396 * Prepare contexts
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_cipher_init( &ctx_dec );
399 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200400
401 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402
403 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000405 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200407 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
408 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000409
410 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200411 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
412 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
415 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200418 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
421 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200422 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200423#else
424 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200426
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200427 /*
428 * Do a few encode/decode cycles
429 */
430 for( i = 0; i < 3; i++ )
431 {
432 memset( iv , 0x00 + i, sizeof( iv ) );
433 memset( ad, 0x10 + i, sizeof( ad ) );
434 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
435
436 memset( encbuf, 0, sizeof( encbuf ) );
437 memset( decbuf, 0, sizeof( decbuf ) );
438 memset( tag, 0, sizeof( tag ) );
439
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200440 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
441 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
442 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100443 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
444 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100445 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200446 else
447 iv_len = sizeof(iv);
448
449 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
450 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
453 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200454
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200455#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
457 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200458#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200460 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
461 TEST_ASSERT( block_size != 0 );
462
Paul Bakker8123e9d2011-01-06 15:37:30 +0000463 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200465 total_len = outlen;
466
467 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200468 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200469 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200470 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200473 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000474
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200475#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200477#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200478
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200479 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200480 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200481 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200482 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483
484 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200486 total_len = outlen;
487
488 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200489 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200490 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200491 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200494 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000495
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200496#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200498#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200499
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200500 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200501 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000502 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200503 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000504
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200505 /*
506 * Done
507 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200508exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 mbedtls_cipher_free( &ctx_dec );
510 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200511}
Paul Bakker33b43f12013-08-20 11:48:36 +0200512/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000513
Paul Bakker33b43f12013-08-20 11:48:36 +0200514/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100515void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
516 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200517{
Paul Bakker33b43f12013-08-20 11:48:36 +0200518 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200519 unsigned char key[32];
520 unsigned char iv[16];
521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 const mbedtls_cipher_info_t *cipher_info;
523 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200524
525 unsigned char inbuf[64];
526 unsigned char encbuf[64];
527
528 size_t outlen = 0;
529
530 memset( key, 0, 32 );
531 memset( iv , 0, 16 );
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200534
535 memset( inbuf, 5, 64 );
536 memset( encbuf, 0, 64 );
537
538 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200540 TEST_ASSERT( NULL != cipher_info );
541
542 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200543 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
545#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
546 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200547#else
548 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
550 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
551 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200552#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200554#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200555
556 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
558 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200559
560 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200563}
Paul Bakker33b43f12013-08-20 11:48:36 +0200564/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200565
Paul Bakker33b43f12013-08-20 11:48:36 +0200566/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200567void dec_empty_buf( int cipher,
568 int expected_update_ret,
569 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200570{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000571 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100572
573 unsigned char *iv = NULL;
574 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_cipher_context_t ctx_dec;
577 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000578
579 unsigned char encbuf[64];
580 unsigned char decbuf[64];
581
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000582 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583
584 memset( key, 0, 32 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200587
Paul Bakker8123e9d2011-01-06 15:37:30 +0000588 memset( encbuf, 0, 64 );
589 memset( decbuf, 0, 64 );
590
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200591 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100592 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000593 TEST_ASSERT( NULL != cipher_info);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100594
595 if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
596 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
597 iv_len = 12;
598
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100599 ASSERT_ALLOC( iv, iv_len );
600 memset( iv , 0, iv_len );
601
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100602 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200603
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200604 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000605
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100606 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
607 key, cipher_info->key_bitlen,
608 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000609
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100610 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200613
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200614#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200616#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000617
618 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200619 TEST_ASSERT( expected_update_ret ==
620 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000621 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100622
k-stachowiakd8727232019-07-29 17:46:29 +0200623 if ( expected_finish_ret == 0 &&
624 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
625 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100626 {
627 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
628 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200629 * decrypting an empty buffer.
630 * On the other hand, CBC and ECB ciphers need a full block of input.
631 */
632 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100633 }
634
k-stachowiakd8727232019-07-29 17:46:29 +0200635 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
636 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000637 TEST_ASSERT( 0 == outlen );
638
Paul Bakkerbd51b262014-07-10 15:26:12 +0200639exit:
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100640 mbedtls_free( iv );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200642}
Paul Bakker33b43f12013-08-20 11:48:36 +0200643/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000644
Paul Bakker33b43f12013-08-20 11:48:36 +0200645/* BEGIN_CASE */
646void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700647 int second_length_val, int pad_mode,
648 int first_encrypt_output_len, int second_encrypt_output_len,
649 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200650{
Paul Bakker33b43f12013-08-20 11:48:36 +0200651 size_t first_length = first_length_val;
652 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000653 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200654 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200655 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000656 unsigned char key[32];
657 unsigned char iv[16];
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_cipher_context_t ctx_dec;
660 mbedtls_cipher_context_t ctx_enc;
661 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000662
663 unsigned char inbuf[64];
664 unsigned char encbuf[64];
665 unsigned char decbuf[64];
666
Paul Bakker23986e52011-04-24 08:57:21 +0000667 size_t outlen = 0;
668 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000669
670 memset( key, 0, 32 );
671 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_cipher_init( &ctx_dec );
674 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200675
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676 memset( inbuf, 5, 64 );
677 memset( encbuf, 0, 64 );
678 memset( decbuf, 0, 64 );
679
680 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000682 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200683
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200684 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
685 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
688 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000689
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700690#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
691 if( -1 != pad_mode )
692 {
693 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
694 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
695 }
696#else
697 (void) pad_mode;
698#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
699
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200700 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
701 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
702 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100703 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
704 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100705 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200706 else
707 iv_len = sizeof(iv);
708
709 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
710 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
713 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200714
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200715#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
717 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200718#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000719
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200720 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
721 TEST_ASSERT( block_size != 0 );
722
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700725 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700728 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000729 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200730 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200731 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200732 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200733 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000736 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200737 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200738 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200739 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200740 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000741
742 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700743 second_length = totaloutlen - first_length;
744 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
745 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200746 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700747 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
748 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
749 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200750
751 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200752 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200753 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200754 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200755
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700756 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200757 totaloutlen += outlen;
758
759 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000760
761 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
762
Paul Bakkerbd51b262014-07-10 15:26:12 +0200763exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 mbedtls_cipher_free( &ctx_dec );
765 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200766}
Paul Bakker33b43f12013-08-20 11:48:36 +0200767/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000768
Paul Bakker33b43f12013-08-20 11:48:36 +0200769/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100770void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
771 data_t * iv, data_t * cipher,
772 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100773 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200774{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200775 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200777 size_t outlen, total_len;
778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200780
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200781 memset( output, 0x00, sizeof( output ) );
782
Azim Khanf1aaec92017-05-30 14:23:15 +0100783#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100784 ((void) ad);
785 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200786#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200787
788 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200789 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100791 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200793 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200795#else
796 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100798 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200800#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100801 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200802#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200803
Azim Khand30ca132017-06-09 04:32:58 +0100804 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200805 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100806 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200807 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200809 &outlen ) );
810 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200811#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100812 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200813#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200814
815 /* check plaintext only if everything went fine */
816 if( 0 == finish_result && 0 == tag_result )
817 {
Azim Khand30ca132017-06-09 04:32:58 +0100818 TEST_ASSERT( total_len == clear->len );
819 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200820 }
821
Paul Bakkerbd51b262014-07-10 15:26:12 +0200822exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200823 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200824}
825/* END_CASE */
826
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100827/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100828void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
829 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000830 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200831{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100832 /*
833 * Take an AEAD ciphertext + tag and perform a pair
834 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000835 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100836 * decryption and encryption are inverse to one another.
837 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000838
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200839 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100840 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200842 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200843 size_t outlen;
844
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100845 unsigned char *cipher_plus_tag = NULL;
846 size_t cipher_plus_tag_len;
847 unsigned char *decrypt_buf = NULL;
848 size_t decrypt_buf_len = 0;
849 unsigned char *encrypt_buf = NULL;
850 size_t encrypt_buf_len = 0;
851
Gilles Peskine70edd682020-12-03 20:27:27 +0100852 /* Null pointers are documented as valid for inputs of length 0.
853 * The test framework passes non-null pointers, so set them to NULL.
854 * key, cipher and tag can't be empty. */
855 if( iv->len == 0 )
856 iv->x = NULL;
857 if( ad->len == 0 )
858 ad->x = NULL;
859 if( clear->len == 0 )
860 clear->x = NULL;
861
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100862 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200863
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100864 /* Initialize PSA Crypto */
865#if defined(MBEDTLS_USE_PSA_CRYPTO)
866 if( use_psa == 1 )
867 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000868#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100869 (void) use_psa;
870#endif
871
872 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100873 * Are we using NIST_KW? with padding?
874 */
875 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
876 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
877 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
878 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
879 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
880 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
881 using_nist_kw_padding;
882
883 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100884 * Prepare context for decryption
885 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100886 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
887 MBEDTLS_DECRYPT ) )
888 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200889
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100890 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100891 * prepare buffer for decryption
892 * (we need the tag appended to the ciphertext)
893 */
894 cipher_plus_tag_len = cipher->len + tag->len;
895 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
896 memcpy( cipher_plus_tag, cipher->x, cipher->len );
897 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
898
899 /*
900 * Compute length of output buffer according to the documentation
901 */
902 if( using_nist_kw )
903 decrypt_buf_len = cipher_plus_tag_len - 8;
904 else
905 decrypt_buf_len = cipher_plus_tag_len - tag->len;
906
907
908 /*
909 * Try decrypting to a buffer that's 1B too small
910 */
911 if( decrypt_buf_len != 0 )
912 {
913 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
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 - 1, &outlen, tag->len );
919 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
920
921 mbedtls_free( decrypt_buf );
922 decrypt_buf = NULL;
923 }
924
925 /*
926 * Authenticate and decrypt, and check result
927 */
928 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
929
930 outlen = 0;
931 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
932 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
933 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
934
935 if( strcmp( result, "FAIL" ) == 0 )
936 {
937 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100938 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100939 }
940 else
941 {
942 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100943 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100944 }
945
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100946 mbedtls_free( decrypt_buf );
947 decrypt_buf = NULL;
948
949 /*
950 * Encrypt back if test data was authentic
951 */
952 if( strcmp( result, "FAIL" ) != 0 )
953 {
954 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100955 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
956 MBEDTLS_ENCRYPT ) )
957 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100958
959 /*
960 * Compute size of output buffer according to documentation
961 */
962 if( using_nist_kw )
963 {
964 encrypt_buf_len = clear->len + 8;
965 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
966 encrypt_buf_len += 8 - encrypt_buf_len % 8;
967 }
968 else
969 {
970 encrypt_buf_len = clear->len + tag->len;
971 }
972
973 /*
974 * Try encrypting with an output buffer that's 1B too small
975 */
976 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
977
978 outlen = 0;
979 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
980 ad->x, ad->len, clear->x, clear->len,
981 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
982 TEST_ASSERT( ret != 0 );
983
984 mbedtls_free( encrypt_buf );
985 encrypt_buf = NULL;
986
987 /*
988 * Encrypt and check the result
989 */
990 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
991
992 outlen = 0;
993 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
994 ad->x, ad->len, clear->x, clear->len,
995 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
996 TEST_ASSERT( ret == 0 );
997
998 TEST_ASSERT( outlen == cipher->len + tag->len );
999 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
1000 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
1001 tag->x, tag->len ) == 0 );
1002
1003 mbedtls_free( encrypt_buf );
1004 encrypt_buf = NULL;
1005 }
1006
Paul Bakkerbd51b262014-07-10 15:26:12 +02001007exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001008
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001009 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001010 mbedtls_free( decrypt_buf );
1011 mbedtls_free( encrypt_buf );
1012 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001013
Hanno Beckera13272d2018-11-12 16:27:30 +00001014#if defined(MBEDTLS_USE_PSA_CRYPTO)
1015 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001016 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +00001017#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001018}
1019/* END_CASE */
1020
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001021/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001022void test_vec_ecb( int cipher_id, int operation, data_t * key,
1023 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001024 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001027 unsigned char output[32];
1028 size_t outlen;
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001031
Paul Bakker5e0efa72013-09-08 23:04:04 +02001032 memset( output, 0x00, sizeof( output ) );
1033
1034 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001035 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001037
Paul Bakker5e0efa72013-09-08 23:04:04 +02001038
Azim Khand30ca132017-06-09 04:32:58 +01001039 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001040
Azim Khand30ca132017-06-09 04:32:58 +01001041 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001043 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1045 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001046 &outlen ) );
1047 TEST_ASSERT( 0 == outlen );
1048
1049 /* check plaintext only if everything went fine */
1050 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001051 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001053
Paul Bakkerbd51b262014-07-10 15:26:12 +02001054exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001056}
1057/* END_CASE */
1058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +02001060void test_vec_crypt( int cipher_id, int operation, data_t *key,
1061 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +00001062 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +03001063{
Ron Eldor7b012442017-09-25 17:03:12 +03001064 mbedtls_cipher_context_t ctx;
1065 unsigned char output[32];
1066 size_t outlen;
1067
1068 mbedtls_cipher_init( &ctx );
1069
Ron Eldor7b012442017-09-25 17:03:12 +03001070 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001071
1072 /* Prepare context */
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001073#if !defined(MBEDTLS_USE_PSA_CRYPO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001074 (void) use_psa;
1075#else
1076 if( use_psa == 1 )
1077 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001078 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001079 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001080 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001081 }
1082 else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001083#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Ron Eldor7b012442017-09-25 17:03:12 +03001084 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001085 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001086
Ronald Cron9ed40732020-06-25 09:03:34 +02001087 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001088 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1089 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1090
Ronald Cron9ed40732020-06-25 09:03:34 +02001091 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1092 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001093 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001094 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001095 /* check plaintext only if everything went fine */
1096 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001097 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001098
1099exit:
1100 mbedtls_cipher_free( &ctx );
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001101#if defined(MBEDTLS_USE_PSA_CRYPO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001102 PSA_DONE( );
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001103#endif /* MBEDTLS_USE_PSA_CRYPTO && defined(MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001104}
1105/* END_CASE */
1106
1107/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001108void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001109{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 const mbedtls_cipher_info_t *cipher_info;
1111 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001116 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001117 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001120
Paul Bakkerbd51b262014-07-10 15:26:12 +02001121exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001123}
Paul Bakker33b43f12013-08-20 11:48:36 +02001124/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001127void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001128 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001129{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_cipher_info_t cipher_info;
1131 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001132 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001133
1134 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_cipher_init( &ctx );
1136 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001137 ctx.cipher_info = &cipher_info;
1138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001140
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001141
Azim Khand30ca132017-06-09 04:32:58 +01001142 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001143 if( 0 == ret )
1144 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001145}
Paul Bakker33b43f12013-08-20 11:48:36 +02001146/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001147
1148/* BEGIN_CASE */
Thomas Daubney5dcbc4d2022-02-17 13:46:27 +00001149void iv_len_validity( int cipher_id, char * cipher_string,
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001150 int iv_len_val, int ret )
1151{
1152 size_t iv_len = iv_len_val;
1153 unsigned char iv[16];
1154
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001155 /* Initialise iv buffer */
1156 memset( iv, 0, sizeof( iv ) );
1157
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001158 const mbedtls_cipher_info_t *cipher_info;
1159 mbedtls_cipher_context_t ctx_dec;
1160 mbedtls_cipher_context_t ctx_enc;
1161
1162 /*
1163 * Prepare contexts
1164 */
1165 mbedtls_cipher_init( &ctx_dec );
1166 mbedtls_cipher_init( &ctx_enc );
1167
1168 /* Check and get info structures */
1169 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
1170 TEST_ASSERT( NULL != cipher_info );
1171 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
1172 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
1173 cipher_string ) == 0 );
1174
1175 /* Initialise enc and dec contexts */
1176 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
1177 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
1178
1179 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
1180 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
1181
1182exit:
1183 mbedtls_cipher_free( &ctx_dec );
1184 mbedtls_cipher_free( &ctx_enc );
1185}
1186/* END_CASE */