blob: c809d9a280908dae4d01eb95176a6276e25a02d2 [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;
22
23 TEST_ASSERT( info != NULL );
24 TEST_EQUAL( type, mbedtls_cipher_info_get_type( info ) );
25 TEST_EQUAL( type, info->type );
26 TEST_ASSERT( mbedtls_cipher_info_from_type( type ) == info );
27
28 TEST_EQUAL( info->mode, mbedtls_cipher_info_get_mode( info ) );
29
30 /* Insist that get_name() return the string from the structure and
31 * not a copy. A copy would have an unknown storage duration. */
32 TEST_ASSERT( mbedtls_cipher_info_get_name( info ) == info->name );
33 TEST_ASSERT( mbedtls_cipher_info_from_string( info->name ) == info );
34
35 key_bitlen = mbedtls_cipher_info_get_key_bitlen( info );
Gilles Peskine6ac8f942021-09-01 08:31:49 +020036 if( info->type == MBEDTLS_CIPHER_NULL )
37 TEST_ASSERT( key_bitlen == 0 );
38 else if( info->mode == MBEDTLS_MODE_XTS )
39 {
40 TEST_ASSERT( key_bitlen == 256 ||
41 key_bitlen == 384 ||
42 key_bitlen == 512 );
43 }
44 else if( ! strncmp( info->name, "DES-EDE3-", 9 ) )
45 {
46 TEST_ASSERT( key_bitlen == 192 );
47 }
48 else if( ! strncmp( info->name, "DES-EDE-", 8 ) )
49 {
50 TEST_ASSERT( key_bitlen == 128 );
51 }
52 else if( ! strncmp( info->name, "DES-", 4 ) )
53 {
54 TEST_ASSERT( key_bitlen == 64 );
55 }
56 else
57 {
58 TEST_ASSERT( key_bitlen == 128 ||
59 key_bitlen == 192 ||
60 key_bitlen == 256 );
61 }
Gilles Peskine0be02bd2021-07-19 16:32:54 +020062
63 return( 1 );
64
65exit:
66 return( 0 );
67}
68
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010069#if defined(MBEDTLS_CIPHER_AUTH_CRYPT)
70/* Helper for resetting key/direction
71 *
72 * The documentation doesn't explicitly say whether calling
73 * mbedtls_cipher_setkey() twice is allowed or not. This currently works with
74 * the default software implementation, but only by accident. It isn't
75 * guaranteed to work with new ciphers or with alternative implementations of
76 * individual ciphers, and it doesn't work with the PSA wrappers. So don't do
77 * it, and instead start with a fresh context.
78 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +010079static int cipher_reset_key( mbedtls_cipher_context_t *ctx, int cipher_id,
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +010080 int use_psa, size_t tag_len, const data_t *key, int direction )
81{
82 mbedtls_cipher_free( ctx );
83 mbedtls_cipher_init( ctx );
84
85#if !defined(MBEDTLS_USE_PSA_CRYPTO)
86 (void) use_psa;
87 (void) tag_len;
88#else
89 if( use_psa == 1 )
90 {
91 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( ctx,
92 mbedtls_cipher_info_from_type( cipher_id ),
93 tag_len ) );
94 }
95 else
96#endif /* MBEDTLS_USE_PSA_CRYPTO */
97 {
98 TEST_ASSERT( 0 == mbedtls_cipher_setup( ctx,
99 mbedtls_cipher_info_from_type( cipher_id ) ) );
100 }
101
102 TEST_ASSERT( 0 == mbedtls_cipher_setkey( ctx, key->x, 8 * key->len,
103 direction ) );
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100104 return( 1 );
105
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100106exit:
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100107 return( 0 );
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100108}
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100109
110/*
111 * Check if a buffer is all-0 bytes:
112 * return 1 if it is,
113 * 0 if it isn't.
114 */
115int buffer_is_all_zero( const uint8_t *buf, size_t size )
116{
117 for( size_t i = 0; i < size; i++ )
118 if( buf[i] != 0 )
119 return 0;
120 return 1;
121}
Manuel Pégourié-Gonnard89a8fe52020-11-27 09:32:55 +0100122#endif /* MBEDTLS_CIPHER_AUTH_CRYPT */
123
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000125
Paul Bakker33b43f12013-08-20 11:48:36 +0200126/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +0200128 * END_DEPENDENCIES
129 */
Paul Bakker5690efc2011-05-26 13:16:06 +0000130
Paul Bakker33b43f12013-08-20 11:48:36 +0200131/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100132void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100133{
134 const int *cipher_type;
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200137 {
138 const mbedtls_cipher_info_t *info =
139 mbedtls_cipher_info_from_type( *cipher_type );
140 mbedtls_test_set_step( *cipher_type );
141 if( ! check_cipher_info( *cipher_type, info ) )
142 goto exit;
143 }
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +0100144}
145/* END_CASE */
146
147/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500148void cipher_invalid_param_unconditional( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200149{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500150 mbedtls_cipher_context_t valid_ctx;
151 mbedtls_cipher_context_t invalid_ctx;
152 mbedtls_operation_t valid_operation = MBEDTLS_ENCRYPT;
153 mbedtls_cipher_padding_t valid_mode = MBEDTLS_PADDING_ZEROS;
154 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
155 int valid_size = sizeof(valid_buffer);
156 int valid_bitlen = valid_size * 8;
157 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
158 *( mbedtls_cipher_list() ) );
159 size_t size_t_var;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200160
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 (void)valid_mode; /* In some configurations this is unused */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200162
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 mbedtls_cipher_init( &valid_ctx );
164 mbedtls_cipher_setup( &valid_ctx, valid_info );
165 mbedtls_cipher_init( &invalid_ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200166
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 /* mbedtls_cipher_setup() */
168 TEST_ASSERT( mbedtls_cipher_setup( &valid_ctx, NULL ) ==
169 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200170
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 /* mbedtls_cipher_get_block_size() */
172 TEST_ASSERT( mbedtls_cipher_get_block_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200173
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 /* mbedtls_cipher_get_cipher_mode() */
175 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &invalid_ctx ) ==
176 MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200177
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500178 /* mbedtls_cipher_get_iv_size() */
179 TEST_ASSERT( mbedtls_cipher_get_iv_size( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200180
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 /* mbedtls_cipher_get_type() */
182 TEST_ASSERT(
183 mbedtls_cipher_get_type( &invalid_ctx ) ==
184 MBEDTLS_CIPHER_NONE);
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200185
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 /* mbedtls_cipher_get_name() */
187 TEST_ASSERT( mbedtls_cipher_get_name( &invalid_ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200188
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500189 /* mbedtls_cipher_get_key_bitlen() */
190 TEST_ASSERT( mbedtls_cipher_get_key_bitlen( &invalid_ctx ) ==
191 MBEDTLS_KEY_LENGTH_NONE );
192
193 /* mbedtls_cipher_get_operation() */
194 TEST_ASSERT( mbedtls_cipher_get_operation( &invalid_ctx ) ==
195 MBEDTLS_OPERATION_NONE );
196
197 /* mbedtls_cipher_setkey() */
198 TEST_ASSERT(
199 mbedtls_cipher_setkey( &invalid_ctx,
200 valid_buffer,
201 valid_bitlen,
202 valid_operation ) ==
203 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
204
205 /* mbedtls_cipher_set_iv() */
206 TEST_ASSERT(
207 mbedtls_cipher_set_iv( &invalid_ctx,
208 valid_buffer,
209 valid_size ) ==
210 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
211
212 /* mbedtls_cipher_reset() */
213 TEST_ASSERT( mbedtls_cipher_reset( &invalid_ctx ) ==
214 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200215
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200216#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 /* mbedtls_cipher_update_ad() */
218 TEST_ASSERT(
219 mbedtls_cipher_update_ad( &invalid_ctx,
220 valid_buffer,
221 valid_size ) ==
222 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
223#endif /* defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C) */
224
225#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
226 /* mbedtls_cipher_set_padding_mode() */
227 TEST_ASSERT( mbedtls_cipher_set_padding_mode( &invalid_ctx, valid_mode ) ==
228 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200229#endif
230
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 /* mbedtls_cipher_update() */
232 TEST_ASSERT(
233 mbedtls_cipher_update( &invalid_ctx,
234 valid_buffer,
235 valid_size,
236 valid_buffer,
237 &size_t_var ) ==
238 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200239
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240 /* mbedtls_cipher_finish() */
241 TEST_ASSERT(
242 mbedtls_cipher_finish( &invalid_ctx,
243 valid_buffer,
244 &size_t_var ) ==
245 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200246
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200247#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 /* mbedtls_cipher_write_tag() */
249 TEST_ASSERT(
250 mbedtls_cipher_write_tag( &invalid_ctx,
251 valid_buffer,
252 valid_size ) ==
253 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200254
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500255 /* mbedtls_cipher_check_tag() */
256 TEST_ASSERT(
257 mbedtls_cipher_check_tag( &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
263exit:
264 mbedtls_cipher_free( &invalid_ctx );
265 mbedtls_cipher_free( &valid_ctx );
266}
267/* END_CASE */
268
TRodziewicz062f3532021-05-25 15:15:57 +0200269/* BEGIN_CASE depends_on:NOT_DEFINED */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270void cipher_invalid_param_conditional( )
271{
272 mbedtls_cipher_context_t valid_ctx;
273
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 mbedtls_operation_t invalid_operation = 100;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
276 int valid_size = sizeof(valid_buffer);
277 int valid_bitlen = valid_size * 8;
278 const mbedtls_cipher_info_t *valid_info = mbedtls_cipher_info_from_type(
279 *( mbedtls_cipher_list() ) );
280
Ronald Cron875b5fb2021-05-21 08:50:00 +0200281 TEST_EQUAL(
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA,
283 mbedtls_cipher_setkey( &valid_ctx,
284 valid_buffer,
285 valid_bitlen,
286 invalid_operation ) );
287
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288exit:
TRodziewicz70199552021-05-27 13:52:59 +0200289 ;
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200290}
291/* END_CASE */
292
Paul Bakker6a9c7252016-07-14 13:46:10 +0100293/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
Azim Khanf1aaec92017-05-30 14:23:15 +0100294void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +0100295{
296 const mbedtls_cipher_info_t *cipher_info;
297 mbedtls_cipher_context_t ctx;
298 unsigned char input[32];
299 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300300#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100301 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300302#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100303 size_t olen = 0;
304
305 mbedtls_cipher_init( &ctx );
306 memset( input, 0, sizeof( input ) );
307 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300308#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100309 memset( iv, 0, sizeof( iv ) );
310
311 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300312 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100313 TEST_ASSERT( NULL != cipher_info );
314
315 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
316
317 /* IV too big */
318 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
319 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
320
321 /* IV too small */
322 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
323 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
324
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300325 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300326 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300327#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300328 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300329 TEST_ASSERT( NULL != cipher_info );
330
331 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
332
Paul Bakker6a9c7252016-07-14 13:46:10 +0100333 /* Update ECB with partial block */
334 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
335 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
336
337exit:
338 mbedtls_cipher_free( &ctx );
339}
340/* END_CASE */
341
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200342/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100343void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200344 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200345{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200346 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100347 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000348 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200349 unsigned char ad[13];
350 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200351 unsigned char inbuf[64];
352 unsigned char encbuf[64];
353 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 const mbedtls_cipher_info_t *cipher_info;
356 mbedtls_cipher_context_t ctx_dec;
357 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000358
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200359 /*
360 * Prepare contexts
361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 mbedtls_cipher_init( &ctx_dec );
363 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200364
365 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000366
367 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Gilles Peskine0be02bd2021-07-19 16:32:54 +0200371 TEST_ASSERT( strcmp( mbedtls_cipher_info_get_name( cipher_info ),
372 cipher_string ) == 0 );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000373
374 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200375 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
376 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
379 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200382 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
385 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200386 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200387#else
388 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200390
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200391 /*
392 * Do a few encode/decode cycles
393 */
394 for( i = 0; i < 3; i++ )
395 {
396 memset( iv , 0x00 + i, sizeof( iv ) );
397 memset( ad, 0x10 + i, sizeof( ad ) );
398 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
399
400 memset( encbuf, 0, sizeof( encbuf ) );
401 memset( decbuf, 0, sizeof( decbuf ) );
402 memset( tag, 0, sizeof( tag ) );
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
405 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
408 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200409
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200410#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
412 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200413#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000414
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200415 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
416 TEST_ASSERT( block_size != 0 );
417
Paul Bakker8123e9d2011-01-06 15:37:30 +0000418 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200420 total_len = outlen;
421
422 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200423 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200424 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200425 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200428 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000429
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200430#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200432#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200433
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200434 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200435 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200436 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200437 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438
439 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200441 total_len = outlen;
442
443 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200444 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200445 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200446 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200449 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000450
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200451#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200453#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200454
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200455 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200456 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000457 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200458 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000459
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200460 /*
461 * Done
462 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200463exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_cipher_free( &ctx_dec );
465 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200466}
Paul Bakker33b43f12013-08-20 11:48:36 +0200467/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000468
Paul Bakker33b43f12013-08-20 11:48:36 +0200469/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +0100470void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
471 int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200472{
Paul Bakker33b43f12013-08-20 11:48:36 +0200473 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200474 unsigned char key[32];
475 unsigned char iv[16];
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 const mbedtls_cipher_info_t *cipher_info;
478 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200479
480 unsigned char inbuf[64];
481 unsigned char encbuf[64];
482
483 size_t outlen = 0;
484
485 memset( key, 0, 32 );
486 memset( iv , 0, 16 );
487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200489
490 memset( inbuf, 5, 64 );
491 memset( encbuf, 0, 64 );
492
493 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200495 TEST_ASSERT( NULL != cipher_info );
496
497 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200498 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
500#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
501 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200502#else
503 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
505 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
506 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200507#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200509#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200510
511 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
513 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200514
515 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200516exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200518}
Paul Bakker33b43f12013-08-20 11:48:36 +0200519/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200520
Paul Bakker33b43f12013-08-20 11:48:36 +0200521/* BEGIN_CASE */
k-stachowiakd8727232019-07-29 17:46:29 +0200522void dec_empty_buf( int cipher,
523 int expected_update_ret,
524 int expected_finish_ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200525{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000526 unsigned char key[32];
527 unsigned char iv[16];
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_cipher_context_t ctx_dec;
530 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000531
532 unsigned char encbuf[64];
533 unsigned char decbuf[64];
534
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000535 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000536
537 memset( key, 0, 32 );
538 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200541
Paul Bakker8123e9d2011-01-06 15:37:30 +0000542 memset( encbuf, 0, 64 );
543 memset( decbuf, 0, 64 );
544
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200545 /* Initialise context */
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100546 cipher_info = mbedtls_cipher_info_from_type( cipher );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000547 TEST_ASSERT( NULL != cipher_info);
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100548 TEST_ASSERT( sizeof(key) * 8 >= cipher_info->key_bitlen );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200549
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200550 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000551
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100552 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec,
553 key, cipher_info->key_bitlen,
554 MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200559
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200560#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200562#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000563
564 /* decode 0-byte string */
k-stachowiakd8727232019-07-29 17:46:29 +0200565 TEST_ASSERT( expected_update_ret ==
566 mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000567 TEST_ASSERT( 0 == outlen );
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100568
k-stachowiakd8727232019-07-29 17:46:29 +0200569 if ( expected_finish_ret == 0 &&
570 ( cipher_info->mode == MBEDTLS_MODE_CBC ||
571 cipher_info->mode == MBEDTLS_MODE_ECB ) )
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100572 {
573 /* Non-CBC and non-ECB ciphers are OK with decrypting empty buffers and
574 * return success, not MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED, when
k-stachowiakd8727232019-07-29 17:46:29 +0200575 * decrypting an empty buffer.
576 * On the other hand, CBC and ECB ciphers need a full block of input.
577 */
578 expected_finish_ret = MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED;
Jaeden Amero5ab80ef2019-06-05 15:35:08 +0100579 }
580
k-stachowiakd8727232019-07-29 17:46:29 +0200581 TEST_ASSERT( expected_finish_ret == mbedtls_cipher_finish(
582 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000583 TEST_ASSERT( 0 == outlen );
584
Paul Bakkerbd51b262014-07-10 15:26:12 +0200585exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200587}
Paul Bakker33b43f12013-08-20 11:48:36 +0200588/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000589
Paul Bakker33b43f12013-08-20 11:48:36 +0200590/* BEGIN_CASE */
591void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700592 int second_length_val, int pad_mode,
593 int first_encrypt_output_len, int second_encrypt_output_len,
594 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200595{
Paul Bakker33b43f12013-08-20 11:48:36 +0200596 size_t first_length = first_length_val;
597 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000598 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200599 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000600 unsigned char key[32];
601 unsigned char iv[16];
602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_cipher_context_t ctx_dec;
604 mbedtls_cipher_context_t ctx_enc;
605 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000606
607 unsigned char inbuf[64];
608 unsigned char encbuf[64];
609 unsigned char decbuf[64];
610
Paul Bakker23986e52011-04-24 08:57:21 +0000611 size_t outlen = 0;
612 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000613
614 memset( key, 0, 32 );
615 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_cipher_init( &ctx_dec );
618 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200619
Paul Bakker8123e9d2011-01-06 15:37:30 +0000620 memset( inbuf, 5, 64 );
621 memset( encbuf, 0, 64 );
622 memset( decbuf, 0, 64 );
623
624 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000626 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200627
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200628 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
629 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
632 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000633
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700634#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
635 if( -1 != pad_mode )
636 {
637 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
638 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
639 }
640#else
641 (void) pad_mode;
642#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
645 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
648 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200649
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200650#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
652 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200653#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000654
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200655 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
656 TEST_ASSERT( block_size != 0 );
657
Paul Bakker8123e9d2011-01-06 15:37:30 +0000658 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700660 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000661 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700663 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000664 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200665 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200666 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200667 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200668 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000671 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200672 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200673 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200674 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200675 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000676
677 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700678 second_length = totaloutlen - first_length;
679 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
680 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200681 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700682 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
683 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
684 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200685
686 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200687 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200688 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200689 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200690
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700691 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200692 totaloutlen += outlen;
693
694 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000695
696 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
697
Paul Bakkerbd51b262014-07-10 15:26:12 +0200698exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_cipher_free( &ctx_dec );
700 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200701}
Paul Bakker33b43f12013-08-20 11:48:36 +0200702/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000703
Paul Bakker33b43f12013-08-20 11:48:36 +0200704/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100705void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
706 data_t * iv, data_t * cipher,
707 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100708 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200709{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200710 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200712 size_t outlen, total_len;
713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200715
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200716 memset( output, 0x00, sizeof( output ) );
717
Azim Khanf1aaec92017-05-30 14:23:15 +0100718#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100719 ((void) ad);
720 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200721#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200722
723 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200724 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100726 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200728 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200730#else
731 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100733 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200735#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100736 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200737#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200738
Azim Khand30ca132017-06-09 04:32:58 +0100739 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200740 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100741 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200742 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200744 &outlen ) );
745 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200746#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100747 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200748#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200749
750 /* check plaintext only if everything went fine */
751 if( 0 == finish_result && 0 == tag_result )
752 {
Azim Khand30ca132017-06-09 04:32:58 +0100753 TEST_ASSERT( total_len == clear->len );
754 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200755 }
756
Paul Bakkerbd51b262014-07-10 15:26:12 +0200757exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200759}
760/* END_CASE */
761
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100762/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_AUTH_CRYPT */
Azim Khan5fcca462018-06-29 11:05:32 +0100763void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
764 data_t * ad, data_t * cipher, data_t * tag,
Hanno Beckera13272d2018-11-12 16:27:30 +0000765 char * result, data_t * clear, int use_psa )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200766{
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100767 /*
768 * Take an AEAD ciphertext + tag and perform a pair
769 * of AEAD decryption and AEAD encryption. Check that
Hanno Beckera13272d2018-11-12 16:27:30 +0000770 * this results in the expected plaintext, and that
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100771 * decryption and encryption are inverse to one another.
772 */
Hanno Beckera13272d2018-11-12 16:27:30 +0000773
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200774 int ret;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100775 int using_nist_kw, using_nist_kw_padding;
Hanno Beckera13272d2018-11-12 16:27:30 +0000776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200778 size_t outlen;
779
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100780 unsigned char *cipher_plus_tag = NULL;
781 size_t cipher_plus_tag_len;
782 unsigned char *decrypt_buf = NULL;
783 size_t decrypt_buf_len = 0;
784 unsigned char *encrypt_buf = NULL;
785 size_t encrypt_buf_len = 0;
786
Gilles Peskine70edd682020-12-03 20:27:27 +0100787 /* Null pointers are documented as valid for inputs of length 0.
788 * The test framework passes non-null pointers, so set them to NULL.
789 * key, cipher and tag can't be empty. */
790 if( iv->len == 0 )
791 iv->x = NULL;
792 if( ad->len == 0 )
793 ad->x = NULL;
794 if( clear->len == 0 )
795 clear->x = NULL;
796
Manuel Pégourié-Gonnard513c2432020-12-01 10:34:57 +0100797 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200798
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100799 /* Initialize PSA Crypto */
800#if defined(MBEDTLS_USE_PSA_CRYPTO)
801 if( use_psa == 1 )
802 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckera13272d2018-11-12 16:27:30 +0000803#else
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100804 (void) use_psa;
805#endif
806
807 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100808 * Are we using NIST_KW? with padding?
809 */
810 using_nist_kw_padding = cipher_id == MBEDTLS_CIPHER_AES_128_KWP ||
811 cipher_id == MBEDTLS_CIPHER_AES_192_KWP ||
812 cipher_id == MBEDTLS_CIPHER_AES_256_KWP;
813 using_nist_kw = cipher_id == MBEDTLS_CIPHER_AES_128_KW ||
814 cipher_id == MBEDTLS_CIPHER_AES_192_KW ||
815 cipher_id == MBEDTLS_CIPHER_AES_256_KW ||
816 using_nist_kw_padding;
817
818 /*
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100819 * Prepare context for decryption
820 */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100821 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
822 MBEDTLS_DECRYPT ) )
823 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200824
Manuel Pégourié-Gonnard4c1a1002020-11-26 10:22:50 +0100825 /*
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100826 * prepare buffer for decryption
827 * (we need the tag appended to the ciphertext)
828 */
829 cipher_plus_tag_len = cipher->len + tag->len;
830 ASSERT_ALLOC( cipher_plus_tag, cipher_plus_tag_len );
831 memcpy( cipher_plus_tag, cipher->x, cipher->len );
832 memcpy( cipher_plus_tag + cipher->len, tag->x, tag->len );
833
834 /*
835 * Compute length of output buffer according to the documentation
836 */
837 if( using_nist_kw )
838 decrypt_buf_len = cipher_plus_tag_len - 8;
839 else
840 decrypt_buf_len = cipher_plus_tag_len - tag->len;
841
842
843 /*
844 * Try decrypting to a buffer that's 1B too small
845 */
846 if( decrypt_buf_len != 0 )
847 {
848 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len - 1 );
849
850 outlen = 0;
851 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
852 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
853 decrypt_buf, decrypt_buf_len - 1, &outlen, tag->len );
854 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
855
856 mbedtls_free( decrypt_buf );
857 decrypt_buf = NULL;
858 }
859
860 /*
861 * Authenticate and decrypt, and check result
862 */
863 ASSERT_ALLOC( decrypt_buf, decrypt_buf_len );
864
865 outlen = 0;
866 ret = mbedtls_cipher_auth_decrypt_ext( &ctx, iv->x, iv->len,
867 ad->x, ad->len, cipher_plus_tag, cipher_plus_tag_len,
868 decrypt_buf, decrypt_buf_len, &outlen, tag->len );
869
870 if( strcmp( result, "FAIL" ) == 0 )
871 {
872 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Manuel Pégourié-Gonnardf215ef82020-12-03 12:33:31 +0100873 TEST_ASSERT( buffer_is_all_zero( decrypt_buf, decrypt_buf_len ) );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100874 }
875 else
876 {
877 TEST_ASSERT( ret == 0 );
Gilles Peskinea2971ea2020-12-03 20:36:02 +0100878 ASSERT_COMPARE( decrypt_buf, outlen, clear->x, clear->len );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100879 }
880
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100881 mbedtls_free( decrypt_buf );
882 decrypt_buf = NULL;
883
884 /*
885 * Encrypt back if test data was authentic
886 */
887 if( strcmp( result, "FAIL" ) != 0 )
888 {
889 /* prepare context for encryption */
Gilles Peskine8a3d2342020-12-03 21:06:15 +0100890 if( ! cipher_reset_key( &ctx, cipher_id, use_psa, tag->len, key,
891 MBEDTLS_ENCRYPT ) )
892 goto exit;
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100893
894 /*
895 * Compute size of output buffer according to documentation
896 */
897 if( using_nist_kw )
898 {
899 encrypt_buf_len = clear->len + 8;
900 if( using_nist_kw_padding && encrypt_buf_len % 8 != 0 )
901 encrypt_buf_len += 8 - encrypt_buf_len % 8;
902 }
903 else
904 {
905 encrypt_buf_len = clear->len + tag->len;
906 }
907
908 /*
909 * Try encrypting with an output buffer that's 1B too small
910 */
911 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len - 1 );
912
913 outlen = 0;
914 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
915 ad->x, ad->len, clear->x, clear->len,
916 encrypt_buf, encrypt_buf_len - 1, &outlen, tag->len );
917 TEST_ASSERT( ret != 0 );
918
919 mbedtls_free( encrypt_buf );
920 encrypt_buf = NULL;
921
922 /*
923 * Encrypt and check the result
924 */
925 ASSERT_ALLOC( encrypt_buf, encrypt_buf_len );
926
927 outlen = 0;
928 ret = mbedtls_cipher_auth_encrypt_ext( &ctx, iv->x, iv->len,
929 ad->x, ad->len, clear->x, clear->len,
930 encrypt_buf, encrypt_buf_len, &outlen, tag->len );
931 TEST_ASSERT( ret == 0 );
932
933 TEST_ASSERT( outlen == cipher->len + tag->len );
934 TEST_ASSERT( memcmp( encrypt_buf, cipher->x, cipher->len ) == 0 );
935 TEST_ASSERT( memcmp( encrypt_buf + cipher->len,
936 tag->x, tag->len ) == 0 );
937
938 mbedtls_free( encrypt_buf );
939 encrypt_buf = NULL;
940 }
941
Paul Bakkerbd51b262014-07-10 15:26:12 +0200942exit:
Hanno Beckera13272d2018-11-12 16:27:30 +0000943
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200944 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard53f10e72020-11-30 10:17:01 +0100945 mbedtls_free( decrypt_buf );
946 mbedtls_free( encrypt_buf );
947 mbedtls_free( cipher_plus_tag );
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200948
Hanno Beckera13272d2018-11-12 16:27:30 +0000949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 if( use_psa == 1 )
Gilles Peskine5386f6b2019-08-01 12:47:40 +0200951 PSA_DONE( );
Hanno Beckera13272d2018-11-12 16:27:30 +0000952#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200953}
954/* END_CASE */
955
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200956/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100957void test_vec_ecb( int cipher_id, int operation, data_t * key,
958 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +0100959 )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200962 unsigned char output[32];
963 size_t outlen;
964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200966
Paul Bakker5e0efa72013-09-08 23:04:04 +0200967 memset( output, 0x00, sizeof( output ) );
968
969 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200970 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200972
Paul Bakker5e0efa72013-09-08 23:04:04 +0200973
Azim Khand30ca132017-06-09 04:32:58 +0100974 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200975
Azim Khand30ca132017-06-09 04:32:58 +0100976 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200978 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
980 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200981 &outlen ) );
982 TEST_ASSERT( 0 == outlen );
983
984 /* check plaintext only if everything went fine */
985 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +0100986 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200988
Paul Bakkerbd51b262014-07-10 15:26:12 +0200989exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200991}
992/* END_CASE */
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ronald Cron9ed40732020-06-25 09:03:34 +0200995void test_vec_crypt( int cipher_id, int operation, data_t *key,
996 data_t *iv, data_t *input, data_t *result,
Hanno Beckere43164e2018-11-12 12:46:35 +0000997 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +0300998{
Ron Eldor7b012442017-09-25 17:03:12 +0300999 mbedtls_cipher_context_t ctx;
1000 unsigned char output[32];
1001 size_t outlen;
1002
1003 mbedtls_cipher_init( &ctx );
1004
Ron Eldor7b012442017-09-25 17:03:12 +03001005 memset( output, 0x00, sizeof( output ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001006
1007 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +00001008#if !defined(MBEDTLS_USE_PSA_CRYPTO)
1009 (void) use_psa;
1010#else
1011 if( use_psa == 1 )
1012 {
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001013 PSA_ASSERT( psa_crypto_init( ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001014 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001015 mbedtls_cipher_info_from_type( cipher_id ), 0 ) );
Hanno Beckere43164e2018-11-12 12:46:35 +00001016 }
1017 else
1018#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001019 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckera13272d2018-11-12 16:27:30 +00001020 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001021
Ronald Cron9ed40732020-06-25 09:03:34 +02001022 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001023 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
1024 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
1025
Ronald Cron9ed40732020-06-25 09:03:34 +02001026 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv->len ? iv->x : NULL,
1027 iv->len, input->x, input->len,
Ron Eldor7b012442017-09-25 17:03:12 +03001028 output, &outlen ) );
Ronald Cron9ed40732020-06-25 09:03:34 +02001029 TEST_ASSERT( result->len == outlen );
Ron Eldor7b012442017-09-25 17:03:12 +03001030 /* check plaintext only if everything went fine */
1031 if( 0 == finish_result )
Ronald Cron9ed40732020-06-25 09:03:34 +02001032 TEST_ASSERT( 0 == memcmp( output, result->x, outlen ) );
Ron Eldor7b012442017-09-25 17:03:12 +03001033
1034exit:
1035 mbedtls_cipher_free( &ctx );
Gilles Peskine5386f6b2019-08-01 12:47:40 +02001036#if defined(MBEDTLS_USE_PSA_CRYPTO)
1037 PSA_DONE( );
1038#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +03001039}
1040/* END_CASE */
1041
1042/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +02001043void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001044{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 const mbedtls_cipher_info_t *cipher_info;
1046 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001051 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001052 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +02001055
Paul Bakkerbd51b262014-07-10 15:26:12 +02001056exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001058}
Paul Bakker33b43f12013-08-20 11:48:36 +02001059/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +00001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +01001062void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +01001063 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_cipher_info_t cipher_info;
1066 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +01001067 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001068
1069 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_cipher_init( &ctx );
1071 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001072 ctx.cipher_info = &cipher_info;
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001075
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +02001076
Azim Khand30ca132017-06-09 04:32:58 +01001077 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +02001078 if( 0 == ret )
1079 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +02001080}
Paul Bakker33b43f12013-08-20 11:48:36 +02001081/* END_CASE */