blob: 2c0a1ec84d1648e1e877bc49ecbb3d50e48f569b [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"
k-stachowiakd8727232019-07-29 17:46:29 +02003#include "mbedtls/aes.h"
k-stachowiakd8727232019-07-29 17:46:29 +02004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00006#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02007#endif
Gilles Peskine5386f6b2019-08-01 12:47:40 +02008
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +01009#if defined(MBEDTLS_CIPHER_MODE_AEAD) || defined(MBEDTLS_NIST_KW_C)
10#define MBEDTLS_CIPHER_AUTH_CRYPT
11#endif
12
Gilles Peskine0be02bd2021-07-19 16:32:54 +020013/* Check the internal consistency of a cipher info structure, and
14 * check it against mbedtls_cipher_info_from_xxx(). */
15static int check_cipher_info( mbedtls_cipher_type_t type,
16 const mbedtls_cipher_info_t *info )
17{
Max Fillinger72abd8a2021-11-28 14:02:36 +010018 size_t key_bitlen, block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020019
20 TEST_ASSERT( info != NULL );
21 TEST_EQUAL( type, mbedtls_cipher_info_get_type( info ) );
22 TEST_EQUAL( type, info->type );
23 TEST_ASSERT( mbedtls_cipher_info_from_type( type ) == info );
24
25 TEST_EQUAL( info->mode, mbedtls_cipher_info_get_mode( info ) );
26
27 /* Insist that get_name() return the string from the structure and
28 * not a copy. A copy would have an unknown storage duration. */
29 TEST_ASSERT( mbedtls_cipher_info_get_name( info ) == info->name );
30 TEST_ASSERT( mbedtls_cipher_info_from_string( info->name ) == info );
31
32 key_bitlen = mbedtls_cipher_info_get_key_bitlen( info );
Max Fillingerc60c3a02021-11-09 22:38:56 +010033 block_size = mbedtls_cipher_info_get_block_size( info );
34 iv_size = mbedtls_cipher_info_get_iv_size( info );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020035 if( info->type == MBEDTLS_CIPHER_NULL )
Max Fillingerc60c3a02021-11-09 22:38:56 +010036 {
Gilles Peskine6ac8f942021-09-01 08:31:49 +020037 TEST_ASSERT( key_bitlen == 0 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010038 TEST_ASSERT( block_size == 1 );
39 TEST_ASSERT( iv_size == 0 );
40 }
Gilles Peskine6ac8f942021-09-01 08:31:49 +020041 else if( info->mode == MBEDTLS_MODE_XTS )
42 {
43 TEST_ASSERT( key_bitlen == 256 ||
44 key_bitlen == 384 ||
45 key_bitlen == 512 );
46 }
47 else if( ! strncmp( info->name, "DES-EDE3-", 9 ) )
48 {
49 TEST_ASSERT( key_bitlen == 192 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010050 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
51 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020052 }
53 else if( ! strncmp( info->name, "DES-EDE-", 8 ) )
54 {
55 TEST_ASSERT( key_bitlen == 128 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010056 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
57 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020058 }
59 else if( ! strncmp( info->name, "DES-", 4 ) )
60 {
61 TEST_ASSERT( key_bitlen == 64 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010062 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
63 TEST_ASSERT( block_size == 8 );
64 }
65 else if( ! strncmp( info->name, "AES", 3 ) )
66 {
67 TEST_ASSERT( key_bitlen == 128 ||
68 key_bitlen == 192 ||
69 key_bitlen == 256 );
70 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
71 TEST_ASSERT( block_size == 16 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020072 }
73 else
74 {
75 TEST_ASSERT( key_bitlen == 128 ||
76 key_bitlen == 192 ||
77 key_bitlen == 256 );
78 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +020079
Max Fillingerc60c3a02021-11-09 22:38:56 +010080 if( strstr( info->name, "-ECB" ) != NULL )
81 {
82 TEST_ASSERT( iv_size == 0 );
83 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
84 }
85 else if( strstr( info->name, "-CBC" ) != NULL ||
86 strstr( info->name, "-CTR" ) != NULL )
87 {
88 TEST_ASSERT( iv_size == block_size );
89 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
90 }
91 else if( strstr( info->name, "-GCM" ) != NULL )
92 {
93 TEST_ASSERT( iv_size == block_size - 4 );
94 TEST_ASSERT( mbedtls_cipher_info_has_variable_iv_size( info ) );
95 }
96
Gilles Peskine0be02bd2021-07-19 16:32:54 +020097 return( 1 );
98
99exit:
100 return( 0 );
101}
102
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100103#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
104/* Helper for resetting key/direction
105 *
106 * The documentation doesn't explicitly say whether calling
107 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
108 * the default software implementation, but only by accident. It isn't
109 * guaranteed to work with new ciphers or with alternative implementations of
110 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
111 * it, and instead start with a fresh context.
112 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100113static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100114 int use_psa, size_t tag_len, const data_t *key, int direction )
115{
116 mbedtls_cipher_free( ctx );
117 mbedtls_cipher_init( ctx );
118
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -0400119#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100120 (void) use_psa;
121 (void) tag_len;
122#else
123 if( use_psa == 1 )
124 {
125 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
126 mbedtls_cipher_info_from_type( cipher_id ),
127 tag_len ) );
128 }
129 else
Przemek Stekiel476d9c42022-05-19 12:26:33 +0200130#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED */
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100131 {
132 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
133 mbedtls_cipher_info_from_type( cipher_id ) ) );
134 }
135
136 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
137 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100138 return( 1 );
139
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100140exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100141 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100142}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100143
144/*
145 * Check if a buffer is all-0 bytes:
146 * return 1 if it is,
147 * 0 if it isn't.
148 */
149int buffer_is_all_zero( const uint8_t *buf, size_t size )
150{
151 for( size_t i = 0; i < size; i++ )
152 if( buf[i] != 0 )
153 return 0;
154 return 1;
155}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100156#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
157
Paul Bakker33b43f12013-08-20 11:48:36 +0200158/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Paul Bakker33b43f12013-08-20 11:48:36 +0200160/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200162 * END_DEPENDENCIES
163 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000164
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100166void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100167{
168 const int *cipher_type;
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200171 {
172 const mbedtls_cipher_info_t *info =
173 mbedtls_cipher_info_from_type( *cipher_type );
174 mbedtls_test_set_step( *cipher_type );
175 if( ! check_cipher_info( *cipher_type, info ) )
176 goto exit;
177 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100178}
179/* END_CASE */
180
181/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500182void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200183{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500184 mbedtls_cipher_context_t valid_ctx;
185 mbedtls_cipher_context_t invalid_ctx;
186 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
187 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
188 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
189 int valid_size = sizeof(valid_buffer);
190 int valid_bitlen = valid_size * 8;
191 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
192 *( mbedtls_cipher_list() ) );
193 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200194
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200196
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 mbedtls_cipher_init( &valid_ctx );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200199
Gilles Peskinedc8ecda2021-12-10 14:28:31 +0100200 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, valid_info ) == 0 );
201
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 /* mbedtls_cipher_setup() */
203 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
204 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200205
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 /* mbedtls_cipher_get_block_size() */
207 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 /* mbedtls_cipher_get_cipher_mode() */
210 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
211 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200212
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 /* mbedtls_cipher_get_iv_size() */
214 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200215
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 /* mbedtls_cipher_get_type() */
217 TEST_ASSERT(
218 mbedtls_cipher_get_type( &invalid_ctx ) ==
219 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200220
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500221 /* mbedtls_cipher_get_name() */
222 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200223
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 /* mbedtls_cipher_get_key_bitlen() */
225 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
226 MBEDTLS_KEY_LENGTH_NONE );
227
228 /* mbedtls_cipher_get_operation() */
229 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
230 MBEDTLS_OPERATION_NONE );
231
232 /* mbedtls_cipher_setkey() */
233 TEST_ASSERT(
234 mbedtls_cipher_setkey( &invalid_ctx,
235 valid_buffer,
236 valid_bitlen,
237 valid_operation ) ==
238 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
239
240 /* mbedtls_cipher_set_iv() */
241 TEST_ASSERT(
242 mbedtls_cipher_set_iv( &invalid_ctx,
243 valid_buffer,
244 valid_size ) ==
245 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
246
247 /* mbedtls_cipher_reset() */
248 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
249 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200250
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200251#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500252 /* mbedtls_cipher_update_ad() */
253 TEST_ASSERT(
254 mbedtls_cipher_update_ad( &invalid_ctx,
255 valid_buffer,
256 valid_size ) ==
257 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
258#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
259
260#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
261 /* mbedtls_cipher_set_padding_mode() */
262 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
263 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200264#endif
265
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 /* mbedtls_cipher_update() */
267 TEST_ASSERT(
268 mbedtls_cipher_update( &invalid_ctx,
269 valid_buffer,
270 valid_size,
271 valid_buffer,
272 &size_t_var ) ==
273 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200274
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 /* mbedtls_cipher_finish() */
276 TEST_ASSERT(
277 mbedtls_cipher_finish( &invalid_ctx,
278 valid_buffer,
279 &size_t_var ) ==
280 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200281
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200282#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500283 /* mbedtls_cipher_write_tag() */
284 TEST_ASSERT(
285 mbedtls_cipher_write_tag( &invalid_ctx,
286 valid_buffer,
287 valid_size ) ==
288 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200289
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 /* mbedtls_cipher_check_tag() */
291 TEST_ASSERT(
292 mbedtls_cipher_check_tag( &invalid_ctx,
293 valid_buffer,
294 valid_size ) ==
295 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
296#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
297
298exit:
299 mbedtls_cipher_free( &invalid_ctx );
300 mbedtls_cipher_free( &valid_ctx );
301}
302/* END_CASE */
303
Tuvshinzaya Erdenekhuu6c689272022-07-29 14:45:55 +0100304/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305void cipher_invalid_param_conditional( )
306{
307 mbedtls_cipher_context_t valid_ctx;
308
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500309 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500310 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
311 int valid_size = sizeof(valid_buffer);
312 int valid_bitlen = valid_size * 8;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313
Ronald Cron875b5fb2021-05-21 08:50:00 +0200314 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
316 mbedtls_cipher_setkey( &valid_ctx,
317 valid_buffer,
318 valid_bitlen,
319 invalid_operation ) );
320
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321exit:
TRodziewicz70199552021-05-27 13:52:59 +0200322 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200323}
324/* END_CASE */
325
Paul Bakker6a9c7252016-07-14 13:46:10 +0100326/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100327void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100328{
329 const mbedtls_cipher_info_t *cipher_info;
330 mbedtls_cipher_context_t ctx;
331 unsigned char input[32];
332 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300333#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100334 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300335#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100336 size_t olen = 0;
337
338 mbedtls_cipher_init( &ctx );
339 memset( input, 0, sizeof( input ) );
340 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300341#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100342 memset( iv, 0, sizeof( iv ) );
343
344 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300345 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100346 TEST_ASSERT( NULL != cipher_info );
347
348 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
349
350 /* IV too big */
351 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
352 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
353
354 /* IV too small */
355 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
356 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
357
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300358 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300359 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300360#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300361 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300362 TEST_ASSERT( NULL != cipher_info );
363
364 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
365
Paul Bakker6a9c7252016-07-14 13:46:10 +0100366 /* Update ECB with partial block */
367 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
368 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
369
370exit:
371 mbedtls_cipher_free( &ctx );
372}
373/* END_CASE */
374
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200375/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100376void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200377 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200378{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200379 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100380 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000381 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200382 unsigned char ad[13];
383 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200384 unsigned char inbuf[64];
385 unsigned char encbuf[64];
386 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 const mbedtls_cipher_info_t *cipher_info;
389 mbedtls_cipher_context_t ctx_dec;
390 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000391
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200392 /*
393 * Prepare contexts
394 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_cipher_init( &ctx_dec );
396 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200397
398 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000399
400 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200404 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
405 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406
407 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200408 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
409 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
412 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200415 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
418 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200419 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200420#else
421 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200423
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200424 /*
425 * Do a few encode/decode cycles
426 */
427 for( i = 0; i < 3; i++ )
428 {
429 memset( iv , 0x00 + i, sizeof( iv ) );
430 memset( ad, 0x10 + i, sizeof( ad ) );
431 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
432
433 memset( encbuf, 0, sizeof( encbuf ) );
434 memset( decbuf, 0, sizeof( decbuf ) );
435 memset( tag, 0, sizeof( tag ) );
436
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200437 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
438 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
439 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100440 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
441 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100442 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200443 else
444 iv_len = sizeof(iv);
445
446 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
447 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
450 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200451
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200452#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
454 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200455#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200457 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
458 TEST_ASSERT( block_size != 0 );
459
Paul Bakker8123e9d2011-01-06 15:37:30 +0000460 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200462 total_len = outlen;
463
464 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200465 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200466 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200467 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200470 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000471
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200472#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200474#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200475
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200476 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200477 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200478 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200479 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000480
481 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200483 total_len = outlen;
484
485 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200486 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200487 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200488 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200491 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000492
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200493#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200495#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200496
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200497 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200498 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000499 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200500 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200502 /*
503 * Done
504 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200505exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_cipher_free( &ctx_dec );
507 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200508}
Paul Bakker33b43f12013-08-20 11:48:36 +0200509/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000510
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100512void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
513 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200514{
Paul Bakker33b43f12013-08-20 11:48:36 +0200515 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200516 unsigned char key[32];
517 unsigned char iv[16];
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_cipher_info_t *cipher_info;
520 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200521
522 unsigned char inbuf[64];
523 unsigned char encbuf[64];
524
525 size_t outlen = 0;
526
527 memset( key, 0, 32 );
528 memset( iv , 0, 16 );
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200531
532 memset( inbuf, 5, 64 );
533 memset( encbuf, 0, 64 );
534
535 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200537 TEST_ASSERT( NULL != cipher_info );
538
539 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200540 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
542#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
543 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200544#else
545 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
547 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
548 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200549#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200551#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200552
553 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
555 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200556
557 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200558exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200560}
Paul Bakker33b43f12013-08-20 11:48:36 +0200561/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200562
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200564void dec_empty_buf( int cipher,
565 int expected_update_ret,
566 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200567{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000568 unsigned char key[32];
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100569
570 unsigned char *iv = NULL;
571 size_t iv_len = 16;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_cipher_context_t ctx_dec;
574 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000575
576 unsigned char encbuf[64];
577 unsigned char decbuf[64];
578
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000579 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000580
581 memset( key, 0, 32 );
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);
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100591
592 if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
593 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
594 iv_len = 12;
595
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100596 ASSERT_ALLOC( iv, iv_len );
597 memset( iv , 0, iv_len );
598
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100599 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200600
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200601 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000602
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100603 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
604 key, cipher_info->key_bitlen,
605 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100607 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200610
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200611#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200613#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000614
615 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200616 TEST_ASSERT( expected_update_ret ==
617 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000618 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100619
k-stachowiakd8727232019-07-29 17:46:29 +0200620 if ( expected_finish_ret == 0 &&
621 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
622 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100623 {
624 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
625 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200626 * decrypting an empty buffer.
627 * On the other hand, CBC and ECB ciphers need a full block of input.
628 */
629 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100630 }
631
k-stachowiakd8727232019-07-29 17:46:29 +0200632 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
633 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000634 TEST_ASSERT( 0 == outlen );
635
Paul Bakkerbd51b262014-07-10 15:26:12 +0200636exit:
Andrzej Kurekb9fbc112022-01-14 16:31:39 +0100637 mbedtls_free( iv );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200639}
Paul Bakker33b43f12013-08-20 11:48:36 +0200640/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000641
Paul Bakker33b43f12013-08-20 11:48:36 +0200642/* BEGIN_CASE */
643void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700644 int second_length_val, int pad_mode,
645 int first_encrypt_output_len, int second_encrypt_output_len,
646 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200647{
Paul Bakker33b43f12013-08-20 11:48:36 +0200648 size_t first_length = first_length_val;
649 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000650 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200651 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200652 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000653 unsigned char key[32];
654 unsigned char iv[16];
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 mbedtls_cipher_context_t ctx_dec;
657 mbedtls_cipher_context_t ctx_enc;
658 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000659
660 unsigned char inbuf[64];
661 unsigned char encbuf[64];
662 unsigned char decbuf[64];
663
Paul Bakker23986e52011-04-24 08:57:21 +0000664 size_t outlen = 0;
665 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000666
667 memset( key, 0, 32 );
668 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_cipher_init( &ctx_dec );
671 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200672
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673 memset( inbuf, 5, 64 );
674 memset( encbuf, 0, 64 );
675 memset( decbuf, 0, 64 );
676
677 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000679 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200680
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200681 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
682 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
685 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000686
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700687#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
688 if( -1 != pad_mode )
689 {
690 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
691 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
692 }
693#else
694 (void) pad_mode;
695#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
696
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200697 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
698 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
699 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
Andrzej Kurek63439ed2021-12-01 22:19:33 +0100700 else if( cipher_info->type == MBEDTLS_CIPHER_CHACHA20 ||
701 cipher_info->type == MBEDTLS_CIPHER_CHACHA20_POLY1305 )
Andrzej Kurek33ca6af2021-12-01 21:58:05 +0100702 iv_len = 12;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200703 else
704 iv_len = sizeof(iv);
705
706 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
707 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
710 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200711
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200712#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
714 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200715#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000716
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200717 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
718 TEST_ASSERT( block_size != 0 );
719
Paul Bakker8123e9d2011-01-06 15:37:30 +0000720 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700722 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000723 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700725 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200727 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200728 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200729 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200730 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000733 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200734 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200735 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200736 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200737 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000738
739 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700740 second_length = totaloutlen - first_length;
741 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
742 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200743 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700744 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
745 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
746 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200747
748 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200749 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200750 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200751 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200752
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700753 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200754 totaloutlen += outlen;
755
756 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000757
758 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
759
Paul Bakkerbd51b262014-07-10 15:26:12 +0200760exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_cipher_free( &ctx_dec );
762 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200763}
Paul Bakker33b43f12013-08-20 11:48:36 +0200764/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000765
Paul Bakker33b43f12013-08-20 11:48:36 +0200766/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100767void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
768 data_t * iv, data_t * cipher,
769 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100770 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200771{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200772 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200774 size_t outlen, total_len;
775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200777
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200778 memset( output, 0x00, sizeof( output ) );
779
Azim Khanf1aaec92017-05-30 14:23:15 +0100780#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100781 ((void) ad);
782 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200783#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200784
785 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200786 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100788 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200790 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200792#else
793 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100795 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200797#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100798 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200799#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200800
Azim Khand30ca132017-06-09 04:32:58 +0100801 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200802 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100803 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200804 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200806 &outlen ) );
807 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200808#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100809 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200810#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200811
812 /* check plaintext only if everything went fine */
813 if( 0 == finish_result && 0 == tag_result )
814 {
Azim Khand30ca132017-06-09 04:32:58 +0100815 TEST_ASSERT( total_len == clear->len );
816 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200817 }
818
Paul Bakkerbd51b262014-07-10 15:26:12 +0200819exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200821}
822/* END_CASE */
823
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100824/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100825void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
826 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000827 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200828{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100829 /*
830 * Take an AEAD ciphertext + tag and perform a pair
831 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000832 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100833 * decryption and encryption are inverse to one another.
834 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000835
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200836 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100837 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000838
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200840 size_t outlen;
841
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100842 unsigned char *cipher_plus_tag = NULL;
843 size_t cipher_plus_tag_len;
844 unsigned char *decrypt_buf = NULL;
845 size_t decrypt_buf_len = 0;
846 unsigned char *encrypt_buf = NULL;
847 size_t encrypt_buf_len = 0;
848
Gilles Peskine70edd682020-12-03 20:27:27 +0100849 /* Null pointers are documented as valid for inputs of length 0.
850 * The test framework passes non-null pointers, so set them to NULL.
851 * key, cipher and tag can't be empty. */
852 if( iv->len == 0 )
853 iv->x = NULL;
854 if( ad->len == 0 )
855 ad->x = NULL;
856 if( clear->len == 0 )
857 clear->x = NULL;
858
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100859 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200860
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100861 /* Initialize PSA Crypto */
862#if defined(MBEDTLS_USE_PSA_CRYPTO)
863 if( use_psa == 1 )
864 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000865#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100866 (void) use_psa;
867#endif
868
869 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100870 * Are we using NIST_KW? with padding?
871 */
872 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
873 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
874 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
875 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
876 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
877 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
878 using_nist_kw_padding;
879
880 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100881 * Prepare context for decryption
882 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100883 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
884 MBEDTLS_DECRYPT ) )
885 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200886
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100887 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100888 * prepare buffer for decryption
889 * (we need the tag appended to the ciphertext)
890 */
891 cipher_plus_tag_len = cipher->len + tag->len;
892 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
893 memcpy( cipher_plus_tag, cipher->x, cipher->len );
894 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
895
896 /*
897 * Compute length of output buffer according to the documentation
898 */
899 if( using_nist_kw )
900 decrypt_buf_len = cipher_plus_tag_len - 8;
901 else
902 decrypt_buf_len = cipher_plus_tag_len - tag->len;
903
904
905 /*
906 * Try decrypting to a buffer that's 1B too small
907 */
908 if( decrypt_buf_len != 0 )
909 {
910 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
911
912 outlen = 0;
913 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
914 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
915 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
916 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
917
918 mbedtls_free( decrypt_buf );
919 decrypt_buf = NULL;
920 }
921
922 /*
923 * Authenticate and decrypt, and check result
924 */
925 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
926
927 outlen = 0;
928 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
929 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
930 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
931
932 if( strcmp( result, "FAIL" ) == 0 )
933 {
934 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100935 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100936 }
937 else
938 {
939 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100940 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100941 }
942
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100943 mbedtls_free( decrypt_buf );
944 decrypt_buf = NULL;
945
946 /*
947 * Encrypt back if test data was authentic
948 */
949 if( strcmp( result, "FAIL" ) != 0 )
950 {
951 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100952 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
953 MBEDTLS_ENCRYPT ) )
954 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100955
956 /*
957 * Compute size of output buffer according to documentation
958 */
959 if( using_nist_kw )
960 {
961 encrypt_buf_len = clear->len + 8;
962 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
963 encrypt_buf_len += 8 - encrypt_buf_len % 8;
964 }
965 else
966 {
967 encrypt_buf_len = clear->len + tag->len;
968 }
969
970 /*
971 * Try encrypting with an output buffer that's 1B too small
972 */
973 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
974
975 outlen = 0;
976 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
977 ad->x, ad->len, clear->x, clear->len,
978 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
979 TEST_ASSERT( ret != 0 );
980
981 mbedtls_free( encrypt_buf );
982 encrypt_buf = NULL;
983
984 /*
985 * Encrypt and check the result
986 */
987 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
988
989 outlen = 0;
990 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
991 ad->x, ad->len, clear->x, clear->len,
992 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
993 TEST_ASSERT( ret == 0 );
994
995 TEST_ASSERT( outlen == cipher->len + tag->len );
996 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
997 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
998 tag->x, tag->len ) == 0 );
999
1000 mbedtls_free( encrypt_buf );
1001 encrypt_buf = NULL;
1002 }
1003
Paul Bakkerbd51b262014-07-10 15:26:12 +02001004exit:
Hanno Beckera13272d2018-11-12 16:27:30 +00001005
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001006 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +01001007 mbedtls_free( decrypt_buf );
1008 mbedtls_free( encrypt_buf );
1009 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001010
Hanno Beckera13272d2018-11-12 16:27:30 +00001011#if defined(MBEDTLS_USE_PSA_CRYPTO)
1012 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001013 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +00001014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001015}
1016/* END_CASE */
1017
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001018/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001019void test_vec_ecb( int cipher_id, int operation, data_t * key,
1020 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001021 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001024 unsigned char output[32];
1025 size_t outlen;
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001028
Paul Bakker5e0efa72013-09-08 23:04:04 +02001029 memset( output, 0x00, sizeof( output ) );
1030
1031 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001032 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001034
Paul Bakker5e0efa72013-09-08 23:04:04 +02001035
Azim Khand30ca132017-06-09 04:32:58 +01001036 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001037
Azim Khand30ca132017-06-09 04:32:58 +01001038 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001040 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1042 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001043 &outlen ) );
1044 TEST_ASSERT( 0 == outlen );
1045
1046 /* check plaintext only if everything went fine */
1047 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001048 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001050
Paul Bakkerbd51b262014-07-10 15:26:12 +02001051exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001053}
1054/* END_CASE */
1055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +02001057void test_vec_crypt( int cipher_id, int operation, data_t *key,
1058 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +00001059 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +03001060{
Ron Eldor7b012442017-09-25 17:03:12 +03001061 mbedtls_cipher_context_t ctx;
1062 unsigned char output[32];
1063 size_t outlen;
1064
1065 mbedtls_cipher_init( &ctx );
1066
Ron Eldor7b012442017-09-25 17:03:12 +03001067 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001068
1069 /* Prepare context */
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001070#if !defined(MBEDTLS_USE_PSA_CRYPTO) || !defined(MBEDTLS_TEST_DEPRECATED)
Hanno Beckere43164e2018-11-12 12:46:35 +00001071 (void) use_psa;
1072#else
1073 if( use_psa == 1 )
1074 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001075 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001076 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001077 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001078 }
1079 else
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001080#endif /* !MBEDTLS_USE_PSA_CRYPTO || !MBEDTLS_TEST_DEPRECATED*/
Ron Eldor7b012442017-09-25 17:03:12 +03001081 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001082 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001083
Ronald Cron9ed40732020-06-25 09:03:34 +02001084 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001085 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1086 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1087
Ronald Cron9ed40732020-06-25 09:03:34 +02001088 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1089 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001090 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001091 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001092 /* check plaintext only if everything went fine */
1093 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001094 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001095
1096exit:
1097 mbedtls_cipher_free( &ctx );
Andrzej Kurek8f26c8a2022-10-20 05:19:47 -04001098#if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_TEST_DEPRECATED)
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001099 PSA_DONE( );
Przemek Stekiel476d9c42022-05-19 12:26:33 +02001100#endif /* MBEDTLS_USE_PSA_CRYPTO && defined(MBEDTLS_TEST_DEPRECATED */
Ron Eldor7b012442017-09-25 17:03:12 +03001101}
1102/* END_CASE */
1103
1104/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001105void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001106{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 const mbedtls_cipher_info_t *cipher_info;
1108 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001113 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001114 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001117
Paul Bakkerbd51b262014-07-10 15:26:12 +02001118exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001120}
Paul Bakker33b43f12013-08-20 11:48:36 +02001121/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001124void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001125 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_cipher_info_t cipher_info;
1128 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001129 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001130
1131 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 mbedtls_cipher_init( &ctx );
1133 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001134 ctx.cipher_info = &cipher_info;
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001137
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001138
Azim Khand30ca132017-06-09 04:32:58 +01001139 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001140 if( 0 == ret )
1141 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001142}
Paul Bakker33b43f12013-08-20 11:48:36 +02001143/* END_CASE */
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001144
1145/* BEGIN_CASE */
Thomas Daubney5dcbc4d2022-02-17 13:46:27 +00001146void iv_len_validity( int cipher_id, char * cipher_string,
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001147 int iv_len_val, int ret )
1148{
1149 size_t iv_len = iv_len_val;
1150 unsigned char iv[16];
1151
Thomas Daubney3a066ec2022-02-17 12:01:28 +00001152 /* Initialise iv buffer */
1153 memset( iv, 0, sizeof( iv ) );
1154
Andrzej Kurek33ca6af2021-12-01 21:58:05 +01001155 const mbedtls_cipher_info_t *cipher_info;
1156 mbedtls_cipher_context_t ctx_dec;
1157 mbedtls_cipher_context_t ctx_enc;
1158
1159 /*
1160 * Prepare contexts
1161 */
1162 mbedtls_cipher_init( &ctx_dec );
1163 mbedtls_cipher_init( &ctx_enc );
1164
1165 /* Check and get info structures */
1166 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
1167 TEST_ASSERT( NULL != cipher_info );
1168 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
1169 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
1170 cipher_string ) == 0 );
1171
1172 /* Initialise enc and dec contexts */
1173 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
1174 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
1175
1176 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
1177 TEST_ASSERT( ret == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
1178
1179exit:
1180 mbedtls_cipher_free( &ctx_dec );
1181 mbedtls_cipher_free( &ctx_enc );
1182}
1183/* END_CASE */