blob: 20020c241e65d9d243674ef649ca81bf1ef4c8f8 [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{
21 size_t key_bitlen;
Max Fillingerc60c3a02021-11-09 22:38:56 +010022 int block_size, iv_size;
Gilles Peskine0be02bd2021-07-19 16:32:54 +020023
24 TEST_ASSERT( info != NULL );
25 TEST_EQUAL( type, mbedtls_cipher_info_get_type( info ) );
26 TEST_EQUAL( type, info->type );
27 TEST_ASSERT( mbedtls_cipher_info_from_type( type ) == info );
28
29 TEST_EQUAL( info->mode, mbedtls_cipher_info_get_mode( info ) );
30
31 /* Insist that get_name() return the string from the structure and
32 * not a copy. A copy would have an unknown storage duration. */
33 TEST_ASSERT( mbedtls_cipher_info_get_name( info ) == info->name );
34 TEST_ASSERT( mbedtls_cipher_info_from_string( info->name ) == info );
35
36 key_bitlen = mbedtls_cipher_info_get_key_bitlen( info );
Max Fillingerc60c3a02021-11-09 22:38:56 +010037 block_size = mbedtls_cipher_info_get_block_size( info );
38 iv_size = mbedtls_cipher_info_get_iv_size( info );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020039 if( info->type == MBEDTLS_CIPHER_NULL )
Max Fillingerc60c3a02021-11-09 22:38:56 +010040 {
Gilles Peskine6ac8f942021-09-01 08:31:49 +020041 TEST_ASSERT( key_bitlen == 0 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010042 TEST_ASSERT( block_size == 1 );
43 TEST_ASSERT( iv_size == 0 );
44 }
Gilles Peskine6ac8f942021-09-01 08:31:49 +020045 else if( info->mode == MBEDTLS_MODE_XTS )
46 {
47 TEST_ASSERT( key_bitlen == 256 ||
48 key_bitlen == 384 ||
49 key_bitlen == 512 );
50 }
51 else if( ! strncmp( info->name, "DES-EDE3-", 9 ) )
52 {
53 TEST_ASSERT( key_bitlen == 192 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010054 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
55 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020056 }
57 else if( ! strncmp( info->name, "DES-EDE-", 8 ) )
58 {
59 TEST_ASSERT( key_bitlen == 128 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010060 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
61 TEST_ASSERT( block_size == 8 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020062 }
63 else if( ! strncmp( info->name, "DES-", 4 ) )
64 {
65 TEST_ASSERT( key_bitlen == 64 );
Max Fillingerc60c3a02021-11-09 22:38:56 +010066 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
67 TEST_ASSERT( block_size == 8 );
68 }
69 else if( ! strncmp( info->name, "AES", 3 ) )
70 {
71 TEST_ASSERT( key_bitlen == 128 ||
72 key_bitlen == 192 ||
73 key_bitlen == 256 );
74 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_key_bitlen( info ) );
75 TEST_ASSERT( block_size == 16 );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020076 }
77 else
78 {
79 TEST_ASSERT( key_bitlen == 128 ||
80 key_bitlen == 192 ||
81 key_bitlen == 256 );
82 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +020083
Max Fillingerc60c3a02021-11-09 22:38:56 +010084 if( strstr( info->name, "-ECB" ) != NULL )
85 {
86 TEST_ASSERT( iv_size == 0 );
87 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
88 }
89 else if( strstr( info->name, "-CBC" ) != NULL ||
90 strstr( info->name, "-CTR" ) != NULL )
91 {
92 TEST_ASSERT( iv_size == block_size );
93 TEST_ASSERT( ! mbedtls_cipher_info_has_variable_iv_size( info ) );
94 }
95 else if( strstr( info->name, "-GCM" ) != NULL )
96 {
97 TEST_ASSERT( iv_size == block_size - 4 );
98 TEST_ASSERT( mbedtls_cipher_info_has_variable_iv_size( info ) );
99 }
100
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200101 return( 1 );
102
103exit:
104 return( 0 );
105}
106
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100107#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
108/* Helper for resetting key/direction
109 *
110 * The documentation doesn't explicitly say whether calling
111 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
112 * the default software implementation, but only by accident. It isn't
113 * guaranteed to work with new ciphers or with alternative implementations of
114 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
115 * it, and instead start with a fresh context.
116 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100117static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100118 int use_psa, size_t tag_len, const data_t *key, int direction )
119{
120 mbedtls_cipher_free( ctx );
121 mbedtls_cipher_init( ctx );
122
123#if !defined(MBEDTLS_USE_PSA_CRYPTO)
124 (void) use_psa;
125 (void) tag_len;
126#else
127 if( use_psa == 1 )
128 {
129 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
130 mbedtls_cipher_info_from_type( cipher_id ),
131 tag_len ) );
132 }
133 else
134#endif /* MBEDTLS_USE_PSA_CRYPTO */
135 {
136 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
137 mbedtls_cipher_info_from_type( cipher_id ) ) );
138 }
139
140 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
141 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100142 return( 1 );
143
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100144exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100145 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100146}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100147
148/*
149 * Check if a buffer is all-0 bytes:
150 * return 1 if it is,
151 * 0 if it isn't.
152 */
153int buffer_is_all_zero( const uint8_t *buf, size_t size )
154{
155 for( size_t i = 0; i < size; i++ )
156 if( buf[i] != 0 )
157 return 0;
158 return 1;
159}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100160#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
161
Paul Bakker33b43f12013-08-20 11:48:36 +0200162/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000163
Paul Bakker33b43f12013-08-20 11:48:36 +0200164/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200166 * END_DEPENDENCIES
167 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000168
Paul Bakker33b43f12013-08-20 11:48:36 +0200169/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100170void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100171{
172 const int *cipher_type;
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200175 {
176 const mbedtls_cipher_info_t *info =
177 mbedtls_cipher_info_from_type( *cipher_type );
178 mbedtls_test_set_step( *cipher_type );
179 if( ! check_cipher_info( *cipher_type, info ) )
180 goto exit;
181 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100182}
183/* END_CASE */
184
185/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200187{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 mbedtls_cipher_context_t valid_ctx;
189 mbedtls_cipher_context_t invalid_ctx;
190 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
191 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
192 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
193 int valid_size = sizeof(valid_buffer);
194 int valid_bitlen = valid_size * 8;
195 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
196 *( mbedtls_cipher_list() ) );
197 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200198
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500199 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200200
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500201 mbedtls_cipher_init( &valid_ctx );
202 mbedtls_cipher_setup( &valid_ctx, valid_info );
203 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200204
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
TRodziewicz062f3532021-05-25 15:15:57 +0200307/* BEGIN_CASE depends_on:NOT_DEFINED */
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;
316 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
317 *( mbedtls_cipher_list() ) );
318
Ronald Cron875b5fb2021-05-21 08:50:00 +0200319 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
321 mbedtls_cipher_setkey( &valid_ctx,
322 valid_buffer,
323 valid_bitlen,
324 invalid_operation ) );
325
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326exit:
TRodziewicz70199552021-05-27 13:52:59 +0200327 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200328}
329/* END_CASE */
330
Paul Bakker6a9c7252016-07-14 13:46:10 +0100331/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100332void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100333{
334 const mbedtls_cipher_info_t *cipher_info;
335 mbedtls_cipher_context_t ctx;
336 unsigned char input[32];
337 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300338#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100339 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300340#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100341 size_t olen = 0;
342
343 mbedtls_cipher_init( &ctx );
344 memset( input, 0, sizeof( input ) );
345 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300346#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100347 memset( iv, 0, sizeof( iv ) );
348
349 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300350 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100351 TEST_ASSERT( NULL != cipher_info );
352
353 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
354
355 /* IV too big */
356 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
357 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
358
359 /* IV too small */
360 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
361 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
362
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300363 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300364 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300365#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300366 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300367 TEST_ASSERT( NULL != cipher_info );
368
369 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
370
Paul Bakker6a9c7252016-07-14 13:46:10 +0100371 /* Update ECB with partial block */
372 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
373 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
374
375exit:
376 mbedtls_cipher_free( &ctx );
377}
378/* END_CASE */
379
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200380/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100381void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200383{
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200384 size_t length = length_val, outlen, total_len, i, block_size, iv_len;
Jaeden Amerod906b812018-06-08 11:03:16 +0100385 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200387 unsigned char ad[13];
388 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200389 unsigned char inbuf[64];
390 unsigned char encbuf[64];
391 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 const mbedtls_cipher_info_t *cipher_info;
394 mbedtls_cipher_context_t ctx_dec;
395 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000396
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200397 /*
398 * Prepare contexts
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_cipher_init( &ctx_dec );
401 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200402
403 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000404
405 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200409 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
410 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000411
412 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200413 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
414 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
417 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200420 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
423 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200424 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200425#else
426 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200428
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200429 /*
430 * Do a few encode/decode cycles
431 */
432 for( i = 0; i < 3; i++ )
433 {
434 memset( iv , 0x00 + i, sizeof( iv ) );
435 memset( ad, 0x10 + i, sizeof( ad ) );
436 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
437
438 memset( encbuf, 0, sizeof( encbuf ) );
439 memset( decbuf, 0, sizeof( decbuf ) );
440 memset( tag, 0, sizeof( tag ) );
441
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200442 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
443 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
444 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
445 else
446 iv_len = sizeof(iv);
447
448 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
449 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
452 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200453
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200454#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
456 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200457#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000458
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200459 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
460 TEST_ASSERT( block_size != 0 );
461
Paul Bakker8123e9d2011-01-06 15:37:30 +0000462 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200464 total_len = outlen;
465
466 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200467 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200468 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200469 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200472 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000473
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200474#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200476#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200477
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200478 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200479 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200480 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200481 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000482
483 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200485 total_len = outlen;
486
487 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200488 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200489 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200490 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200493 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000494
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200495#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200497#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200498
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200499 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200500 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000501 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200502 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000503
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200504 /*
505 * Done
506 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200507exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 mbedtls_cipher_free( &ctx_dec );
509 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200510}
Paul Bakker33b43f12013-08-20 11:48:36 +0200511/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000512
Paul Bakker33b43f12013-08-20 11:48:36 +0200513/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100514void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
515 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200516{
Paul Bakker33b43f12013-08-20 11:48:36 +0200517 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200518 unsigned char key[32];
519 unsigned char iv[16];
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 const mbedtls_cipher_info_t *cipher_info;
522 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200523
524 unsigned char inbuf[64];
525 unsigned char encbuf[64];
526
527 size_t outlen = 0;
528
529 memset( key, 0, 32 );
530 memset( iv , 0, 16 );
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200533
534 memset( inbuf, 5, 64 );
535 memset( encbuf, 0, 64 );
536
537 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200539 TEST_ASSERT( NULL != cipher_info );
540
541 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200542 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
544#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
545 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200546#else
547 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
549 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
550 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200551#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200553#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200554
555 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
557 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200558
559 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200560exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200562}
Paul Bakker33b43f12013-08-20 11:48:36 +0200563/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200564
Paul Bakker33b43f12013-08-20 11:48:36 +0200565/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200566void dec_empty_buf( int cipher,
567 int expected_update_ret,
568 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200569{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000570 unsigned char key[32];
571 unsigned char iv[16];
572
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 );
582 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200585
Paul Bakker8123e9d2011-01-06 15:37:30 +0000586 memset( encbuf, 0, 64 );
587 memset( decbuf, 0, 64 );
588
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200589 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100590 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000591 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100592 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200593
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200594 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000595
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100596 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
597 key, cipher_info->key_bitlen,
598 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200603
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200604#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200606#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000607
608 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200609 TEST_ASSERT( expected_update_ret ==
610 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000611 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100612
k-stachowiakd8727232019-07-29 17:46:29 +0200613 if ( expected_finish_ret == 0 &&
614 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
615 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100616 {
617 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
618 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200619 * decrypting an empty buffer.
620 * On the other hand, CBC and ECB ciphers need a full block of input.
621 */
622 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100623 }
624
k-stachowiakd8727232019-07-29 17:46:29 +0200625 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
626 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000627 TEST_ASSERT( 0 == outlen );
628
Paul Bakkerbd51b262014-07-10 15:26:12 +0200629exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200631}
Paul Bakker33b43f12013-08-20 11:48:36 +0200632/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633
Paul Bakker33b43f12013-08-20 11:48:36 +0200634/* BEGIN_CASE */
635void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700636 int second_length_val, int pad_mode,
637 int first_encrypt_output_len, int second_encrypt_output_len,
638 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200639{
Paul Bakker33b43f12013-08-20 11:48:36 +0200640 size_t first_length = first_length_val;
641 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000642 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200643 size_t block_size;
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200644 size_t iv_len;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000645 unsigned char key[32];
646 unsigned char iv[16];
647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_cipher_context_t ctx_dec;
649 mbedtls_cipher_context_t ctx_enc;
650 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000651
652 unsigned char inbuf[64];
653 unsigned char encbuf[64];
654 unsigned char decbuf[64];
655
Paul Bakker23986e52011-04-24 08:57:21 +0000656 size_t outlen = 0;
657 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658
659 memset( key, 0, 32 );
660 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 mbedtls_cipher_init( &ctx_dec );
663 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200664
Paul Bakker8123e9d2011-01-06 15:37:30 +0000665 memset( inbuf, 5, 64 );
666 memset( encbuf, 0, 64 );
667 memset( decbuf, 0, 64 );
668
669 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200672
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200673 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
674 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
677 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000678
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700679#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
680 if( -1 != pad_mode )
681 {
682 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
683 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
684 }
685#else
686 (void) pad_mode;
687#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
688
Mateusz Starzykf257a6e2021-10-27 16:27:44 +0200689 if( NULL != strstr( cipher_info->name, "CCM*-NO-TAG") )
690 iv_len = 13; /* For CCM, IV length is expected to be between 7 and 13 bytes.
691 * For CCM*-NO-TAG, IV length must be exactly 13 bytes long. */
692 else
693 iv_len = sizeof(iv);
694
695 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, iv_len ) );
696 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, iv_len ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
699 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200700
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200701#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
703 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200704#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000705
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200706 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
707 TEST_ASSERT( block_size != 0 );
708
Paul Bakker8123e9d2011-01-06 15:37:30 +0000709 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700711 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000712 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700714 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000715 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200716 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200717 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200718 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200719 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000722 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200723 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200724 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200725 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200726 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000727
728 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700729 second_length = totaloutlen - first_length;
730 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
731 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200732 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700733 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
734 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
735 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200736
737 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 + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200741
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700742 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200743 totaloutlen += outlen;
744
745 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000746
747 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
748
Paul Bakkerbd51b262014-07-10 15:26:12 +0200749exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 mbedtls_cipher_free( &ctx_dec );
751 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200752}
Paul Bakker33b43f12013-08-20 11:48:36 +0200753/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000754
Paul Bakker33b43f12013-08-20 11:48:36 +0200755/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100756void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
757 data_t * iv, data_t * cipher,
758 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100759 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200760{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200761 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200763 size_t outlen, total_len;
764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200766
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200767 memset( output, 0x00, sizeof( output ) );
768
Azim Khanf1aaec92017-05-30 14:23:15 +0100769#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100770 ((void) ad);
771 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200772#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200773
774 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200775 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100777 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200779 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200781#else
782 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100784 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200786#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100787 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200788#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200789
Azim Khand30ca132017-06-09 04:32:58 +0100790 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200791 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100792 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200793 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200795 &outlen ) );
796 total_len += outlen;
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( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200799#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200800
801 /* check plaintext only if everything went fine */
802 if( 0 == finish_result && 0 == tag_result )
803 {
Azim Khand30ca132017-06-09 04:32:58 +0100804 TEST_ASSERT( total_len == clear->len );
805 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200806 }
807
Paul Bakkerbd51b262014-07-10 15:26:12 +0200808exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200809 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200810}
811/* END_CASE */
812
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100813/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100814void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
815 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000816 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200817{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100818 /*
819 * Take an AEAD ciphertext + tag and perform a pair
820 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000821 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100822 * decryption and encryption are inverse to one another.
823 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000824
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200825 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100826 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200828 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200829 size_t outlen;
830
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100831 unsigned char *cipher_plus_tag = NULL;
832 size_t cipher_plus_tag_len;
833 unsigned char *decrypt_buf = NULL;
834 size_t decrypt_buf_len = 0;
835 unsigned char *encrypt_buf = NULL;
836 size_t encrypt_buf_len = 0;
837
Gilles Peskine70edd682020-12-03 20:27:27 +0100838 /* Null pointers are documented as valid for inputs of length 0.
839 * The test framework passes non-null pointers, so set them to NULL.
840 * key, cipher and tag can't be empty. */
841 if( iv->len == 0 )
842 iv->x = NULL;
843 if( ad->len == 0 )
844 ad->x = NULL;
845 if( clear->len == 0 )
846 clear->x = NULL;
847
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100848 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200849
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100850 /* Initialize PSA Crypto */
851#if defined(MBEDTLS_USE_PSA_CRYPTO)
852 if( use_psa == 1 )
853 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000854#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100855 (void) use_psa;
856#endif
857
858 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100859 * Are we using NIST_KW? with padding?
860 */
861 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
862 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
863 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
864 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
865 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
866 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
867 using_nist_kw_padding;
868
869 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100870 * Prepare context for decryption
871 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100872 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
873 MBEDTLS_DECRYPT ) )
874 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200875
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100876 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100877 * prepare buffer for decryption
878 * (we need the tag appended to the ciphertext)
879 */
880 cipher_plus_tag_len = cipher->len + tag->len;
881 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
882 memcpy( cipher_plus_tag, cipher->x, cipher->len );
883 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
884
885 /*
886 * Compute length of output buffer according to the documentation
887 */
888 if( using_nist_kw )
889 decrypt_buf_len = cipher_plus_tag_len - 8;
890 else
891 decrypt_buf_len = cipher_plus_tag_len - tag->len;
892
893
894 /*
895 * Try decrypting to a buffer that's 1B too small
896 */
897 if( decrypt_buf_len != 0 )
898 {
899 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
900
901 outlen = 0;
902 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
903 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
904 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
905 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
906
907 mbedtls_free( decrypt_buf );
908 decrypt_buf = NULL;
909 }
910
911 /*
912 * Authenticate and decrypt, and check result
913 */
914 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
915
916 outlen = 0;
917 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
918 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
919 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
920
921 if( strcmp( result, "FAIL" ) == 0 )
922 {
923 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100924 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100925 }
926 else
927 {
928 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100929 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100930 }
931
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100932 mbedtls_free( decrypt_buf );
933 decrypt_buf = NULL;
934
935 /*
936 * Encrypt back if test data was authentic
937 */
938 if( strcmp( result, "FAIL" ) != 0 )
939 {
940 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100941 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
942 MBEDTLS_ENCRYPT ) )
943 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100944
945 /*
946 * Compute size of output buffer according to documentation
947 */
948 if( using_nist_kw )
949 {
950 encrypt_buf_len = clear->len + 8;
951 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
952 encrypt_buf_len += 8 - encrypt_buf_len % 8;
953 }
954 else
955 {
956 encrypt_buf_len = clear->len + tag->len;
957 }
958
959 /*
960 * Try encrypting with an output buffer that's 1B too small
961 */
962 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
963
964 outlen = 0;
965 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
966 ad->x, ad->len, clear->x, clear->len,
967 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
968 TEST_ASSERT( ret != 0 );
969
970 mbedtls_free( encrypt_buf );
971 encrypt_buf = NULL;
972
973 /*
974 * Encrypt and check the result
975 */
976 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
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, &outlen, tag->len );
982 TEST_ASSERT( ret == 0 );
983
984 TEST_ASSERT( outlen == cipher->len + tag->len );
985 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
986 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
987 tag->x, tag->len ) == 0 );
988
989 mbedtls_free( encrypt_buf );
990 encrypt_buf = NULL;
991 }
992
Paul Bakkerbd51b262014-07-10 15:26:12 +0200993exit:
Hanno Beckera13272d2018-11-12 16:27:30 +0000994
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200995 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100996 mbedtls_free( decrypt_buf );
997 mbedtls_free( encrypt_buf );
998 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200999
Hanno Beckera13272d2018-11-12 16:27:30 +00001000#if defined(MBEDTLS_USE_PSA_CRYPTO)
1001 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001002 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +00001003#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +02001004}
1005/* END_CASE */
1006
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +02001007/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +01001008void test_vec_ecb( int cipher_id, int operation, data_t * key,
1009 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +01001010 )
Paul Bakker5e0efa72013-09-08 23:04:04 +02001011{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +02001013 unsigned char output[32];
1014 size_t outlen;
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001017
Paul Bakker5e0efa72013-09-08 23:04:04 +02001018 memset( output, 0x00, sizeof( output ) );
1019
1020 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001021 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001023
Paul Bakker5e0efa72013-09-08 23:04:04 +02001024
Azim Khand30ca132017-06-09 04:32:58 +01001025 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001026
Azim Khand30ca132017-06-09 04:32:58 +01001027 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +02001029 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
1031 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +02001032 &outlen ) );
1033 TEST_ASSERT( 0 == outlen );
1034
1035 /* check plaintext only if everything went fine */
1036 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +01001037 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001039
Paul Bakkerbd51b262014-07-10 15:26:12 +02001040exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +02001042}
1043/* END_CASE */
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +02001046void test_vec_crypt( int cipher_id, int operation, data_t *key,
1047 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +00001048 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +03001049{
Ron Eldor7b012442017-09-25 17:03:12 +03001050 mbedtls_cipher_context_t ctx;
1051 unsigned char output[32];
1052 size_t outlen;
1053
1054 mbedtls_cipher_init( &ctx );
1055
Ron Eldor7b012442017-09-25 17:03:12 +03001056 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001057
1058 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +00001059#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1060 (void) use_psa;
1061#else
1062 if( use_psa == 1 )
1063 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001064 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001065 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001066 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001067 }
1068 else
1069#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001070 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001071 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001072
Ronald Cron9ed40732020-06-25 09:03:34 +02001073 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001074 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1075 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1076
Ronald Cron9ed40732020-06-25 09:03:34 +02001077 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1078 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001079 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001080 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001081 /* check plaintext only if everything went fine */
1082 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001083 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001084
1085exit:
1086 mbedtls_cipher_free( &ctx );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001087#if defined(MBEDTLS_USE_PSA_CRYPTO)
1088 PSA_DONE( );
1089#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001090}
1091/* END_CASE */
1092
1093/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001094void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 const mbedtls_cipher_info_t *cipher_info;
1097 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001102 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001103 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001106
Paul Bakkerbd51b262014-07-10 15:26:12 +02001107exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001109}
Paul Bakker33b43f12013-08-20 11:48:36 +02001110/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001113void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001114 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001115{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 mbedtls_cipher_info_t cipher_info;
1117 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001118 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001119
1120 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_cipher_init( &ctx );
1122 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001123 ctx.cipher_info = &cipher_info;
1124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001126
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001127
Azim Khand30ca132017-06-09 04:32:58 +01001128 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001129 if( 0 == ret )
1130 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001131}
Paul Bakker33b43f12013-08-20 11:48:36 +02001132/* END_CASE */