blob: 0640779a855a988d365f74b26f5c2ba4363d6455 [file] [log] [blame]
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include <mbedtls/ssl.h>
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +02003#include <mbedtls/ssl_internal.h>
Hanno Beckera18d1322018-01-03 14:27:32 +00004
5/*
6 * Helper function setting up inverse record transformations
7 * using given cipher, hash, EtM mode, authentication tag length,
8 * and version.
9 */
10
11#define CHK( x ) \
12 do \
13 { \
14 if( !( x ) ) \
Hanno Becker81e16a32019-03-01 11:21:44 +000015 { \
Hanno Beckera5780f12019-04-05 09:55:37 +010016 ret = -1; \
Hanno Becker81e16a32019-03-01 11:21:44 +000017 goto cleanup; \
18 } \
Hanno Beckera18d1322018-01-03 14:27:32 +000019 } while( 0 )
20
Hanno Beckerd856c822019-04-29 17:30:59 +010021#if MBEDTLS_SSL_CID_OUT_LEN_MAX > MBEDTLS_SSL_CID_IN_LEN_MAX
22#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_IN_LEN_MAX
23#else
24#define SSL_CID_LEN_MIN MBEDTLS_SSL_CID_OUT_LEN_MAX
25#endif
Hanno Beckera18d1322018-01-03 14:27:32 +000026
27static int build_transforms( mbedtls_ssl_transform *t_in,
28 mbedtls_ssl_transform *t_out,
29 int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +010030 int etm, int tag_mode, int ver,
31 size_t cid0_len,
32 size_t cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +000033{
34 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +010035 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +000036
37 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +000038 unsigned char *key0 = NULL, *key1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +000039 unsigned char iv_enc[16], iv_dec[16];
40
Hanno Beckerd856c822019-04-29 17:30:59 +010041 unsigned char cid0[ SSL_CID_LEN_MIN ];
42 unsigned char cid1[ SSL_CID_LEN_MIN ];
43
44 rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
45 rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
46
Hanno Beckera18d1322018-01-03 14:27:32 +000047 maclen = 0;
48
49 /* Pick cipher */
50 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
51 CHK( cipher_info != NULL );
52 CHK( cipher_info->iv_size <= 16 );
53 CHK( cipher_info->key_bitlen % 8 == 0 );
54
55 /* Pick keys */
56 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +010057 /* Allocate `keylen + 1` bytes to ensure that we get
58 * a non-NULL pointers from `mbedtls_calloc` even if
59 * `keylen == 0` in the case of the NULL cipher. */
60 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
61 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +000062 memset( key0, 0x1, keylen );
63 memset( key1, 0x2, keylen );
64
65 /* Setup cipher contexts */
66 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
67 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
68 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
69 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
70
71#if defined(MBEDTLS_CIPHER_MODE_CBC)
72 if( cipher_info->mode == MBEDTLS_MODE_CBC )
73 {
74 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
75 MBEDTLS_PADDING_NONE ) == 0 );
76 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
77 MBEDTLS_PADDING_NONE ) == 0 );
78 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
79 MBEDTLS_PADDING_NONE ) == 0 );
80 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
81 MBEDTLS_PADDING_NONE ) == 0 );
82 }
83#endif /* MBEDTLS_CIPHER_MODE_CBC */
84
85 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
86 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
87 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
88 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
89 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
90 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
91 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
92 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +000093
94 /* Setup MAC contexts */
95#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
96 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
97 cipher_info->mode == MBEDTLS_MODE_STREAM )
98 {
99 mbedtls_md_info_t const *md_info;
100 unsigned char *md0, *md1;
101
102 /* Pick hash */
103 md_info = mbedtls_md_info_from_type( hash_id );
104 CHK( md_info != NULL );
105
106 /* Pick hash keys */
107 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +0100108 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
109 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000110 memset( md0, 0x5, maclen );
111 memset( md1, 0x6, maclen );
112
113 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
114 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
115 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
116 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
117
118 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
119 {
120 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
121 md0, maclen ) == 0 );
122 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
123 md1, maclen ) == 0 );
124 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
125 md1, maclen ) == 0 );
126 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
127 md0, maclen ) == 0 );
128 }
129#if defined(MBEDTLS_SSL_PROTO_SSL3)
130 else
131 {
132 memcpy( &t_in->mac_enc, md0, maclen );
133 memcpy( &t_in->mac_dec, md1, maclen );
134 memcpy( &t_out->mac_enc, md1, maclen );
135 memcpy( &t_out->mac_dec, md0, maclen );
136 }
137#endif
138
Hanno Becker3ee54212019-04-04 16:31:26 +0100139 mbedtls_free( md0 );
140 mbedtls_free( md1 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000141 }
142#else
143 ((void) hash_id);
144#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
145
146
147 /* Pick IV's (regardless of whether they
148 * are being used by the transform). */
149 ivlen = cipher_info->iv_size;
150 memset( iv_enc, 0x3, sizeof( iv_enc ) );
151 memset( iv_dec, 0x4, sizeof( iv_dec ) );
152
153 /*
154 * Setup transforms
155 */
156
157#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
158 t_out->encrypt_then_mac = etm;
159 t_in->encrypt_then_mac = etm;
160#else
161 ((void) etm);
162#endif
163
164 t_out->minor_ver = ver;
165 t_in->minor_ver = ver;
166 t_out->ivlen = ivlen;
167 t_in->ivlen = ivlen;
168
169 switch( cipher_info->mode )
170 {
171 case MBEDTLS_MODE_GCM:
172 case MBEDTLS_MODE_CCM:
173 t_out->fixed_ivlen = 4;
174 t_in->fixed_ivlen = 4;
175 t_out->maclen = 0;
176 t_in->maclen = 0;
177 switch( tag_mode )
178 {
179 case 0: /* Full tag */
180 t_out->taglen = 16;
181 t_in->taglen = 16;
182 break;
183 case 1: /* Partial tag */
184 t_out->taglen = 8;
185 t_in->taglen = 8;
186 break;
187 default:
188 return( 1 );
189 }
190 break;
191
192 case MBEDTLS_MODE_CHACHAPOLY:
193 t_out->fixed_ivlen = 12;
194 t_in->fixed_ivlen = 12;
195 t_out->maclen = 0;
196 t_in->maclen = 0;
197 switch( tag_mode )
198 {
199 case 0: /* Full tag */
200 t_out->taglen = 16;
201 t_in->taglen = 16;
202 break;
203 case 1: /* Partial tag */
204 t_out->taglen = 8;
205 t_in->taglen = 8;
206 break;
207 default:
208 return( 1 );
209 }
210 break;
211
212 case MBEDTLS_MODE_STREAM:
213 case MBEDTLS_MODE_CBC:
214 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
215 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
216 t_out->taglen = 0;
217 t_in->taglen = 0;
218 switch( tag_mode )
219 {
220 case 0: /* Full tag */
221 t_out->maclen = maclen;
222 t_in->maclen = maclen;
223 break;
224 case 1: /* Partial tag */
225 t_out->maclen = 10;
226 t_in->maclen = 10;
227 break;
228 default:
229 return( 1 );
230 }
231 break;
232 default:
233 return( 1 );
234 break;
235 }
236
237 /* Setup IV's */
238
239 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
240 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
241 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
242 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
243
Hanno Beckerd856c822019-04-29 17:30:59 +0100244 /* Add CID */
245 memcpy( &t_in->in_cid, cid0, cid0_len );
246 memcpy( &t_in->out_cid, cid1, cid1_len );
247 t_in->in_cid_len = cid0_len;
248 t_in->out_cid_len = cid1_len;
249 memcpy( &t_out->in_cid, cid1, cid1_len );
250 memcpy( &t_out->out_cid, cid0, cid0_len );
251 t_out->in_cid_len = cid1_len;
252 t_out->out_cid_len = cid0_len;
253
Hanno Becker81e16a32019-03-01 11:21:44 +0000254cleanup:
255
Hanno Becker3ee54212019-04-04 16:31:26 +0100256 mbedtls_free( key0 );
257 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +0000258
Hanno Beckera5780f12019-04-05 09:55:37 +0100259 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +0000260}
261
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200262/* END_HEADER */
263
264/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200266 * END_DEPENDENCIES
267 */
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100270void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200271{
Azim Khand30ca132017-06-09 04:32:58 +0100272 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200274 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200275
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200276 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200277 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200278
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200279 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
280 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200281 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
282 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200283 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200284
285 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100286 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200287 {
Azim Khand30ca132017-06-09 04:32:58 +0100288 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200290 }
291
292 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100293 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200297 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200298}
299/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100300
301/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
302void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
303{
304 mbedtls_ssl_context ssl;
305 mbedtls_ssl_init( &ssl );
306
307 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
308 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
309
310 mbedtls_ssl_free( &ssl );
311}
Darryl Green11999bb2018-03-13 15:22:58 +0000312/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000313
314/* BEGIN_CASE */
315void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100316 int etm, int tag_mode, int ver,
317 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +0000318{
319 /*
320 * Test several record encryptions and decryptions
321 * with plenty of space before and after the data
322 * within the record buffer.
323 */
324
325 int ret;
326 int num_records = 16;
327 mbedtls_ssl_context ssl; /* ONLY for debugging */
328
329 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000330 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +0000331 size_t const buflen = 512;
332 mbedtls_record rec, rec_backup;
333
334 mbedtls_ssl_init( &ssl );
335 mbedtls_ssl_transform_init( &t0 );
336 mbedtls_ssl_transform_init( &t1 );
337 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100338 etm, tag_mode, ver,
339 (size_t) cid0_len,
340 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000341
Hanno Becker3ee54212019-04-04 16:31:26 +0100342 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000343
344 while( num_records-- > 0 )
345 {
346 mbedtls_ssl_transform *t_dec, *t_enc;
347 /* Take turns in who's sending and who's receiving. */
348 if( num_records % 3 == 0 )
349 {
350 t_dec = &t0;
351 t_enc = &t1;
352 }
353 else
354 {
355 t_dec = &t1;
356 t_enc = &t0;
357 }
358
359 /*
360 * The record header affects the transformation in two ways:
361 * 1) It determines the AEAD additional data
362 * 2) The record counter sometimes determines the IV.
363 *
364 * Apart from that, the fields don't have influence.
365 * In particular, it is currently not the responsibility
366 * of ssl_encrypt/decrypt_buf to check if the transform
367 * version matches the record version, or that the
368 * type is sensible.
369 */
370
371 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
372 rec.type = 42;
373 rec.ver[0] = num_records;
374 rec.ver[1] = num_records;
Hanno Beckerd856c822019-04-29 17:30:59 +0100375 rec.cid_len = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +0000376
377 rec.buf = buf;
378 rec.buf_len = buflen;
379 rec.data_offset = 16;
380 /* Make sure to vary the length to exercise different
381 * paddings. */
382 rec.data_len = 1 + num_records;
383
384 memset( rec.buf + rec.data_offset, 42, rec.data_len );
385
386 /* Make a copy for later comparison */
387 rec_backup = rec;
388
389 /* Encrypt record */
390 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
391 rnd_std_rand, NULL );
392 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
393 if( ret != 0 )
394 {
395 continue;
396 }
397
398 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +0100399 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
400 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000401
402 /* Compare results */
403 TEST_ASSERT( rec.type == rec_backup.type );
404 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
405 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
406 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
407 TEST_ASSERT( rec.data_len == rec_backup.data_len );
408 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
409 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
410 rec_backup.buf + rec_backup.data_offset,
411 rec.data_len ) == 0 );
412 }
413
Hanno Becker81e16a32019-03-01 11:21:44 +0000414exit:
415
Hanno Beckera18d1322018-01-03 14:27:32 +0000416 /* Cleanup */
417 mbedtls_ssl_free( &ssl );
418 mbedtls_ssl_transform_free( &t0 );
419 mbedtls_ssl_transform_free( &t1 );
420
Hanno Becker3ee54212019-04-04 16:31:26 +0100421 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +0000422}
423/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000424
425/* BEGIN_CASE */
426void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100427 int etm, int tag_mode, int ver,
428 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +0000429{
430 /*
431 * Test pairs of encryption and decryption with an increasing
432 * amount of space in the record buffer - in more detail:
433 * 1) Try to encrypt with 0, 1, 2, ... bytes available
434 * in front of the plaintext, and expect the encryption
435 * to succeed starting from some offset. Always keep
436 * enough space in the end of the buffer.
437 * 2) Try to encrypt with 0, 1, 2, ... bytes available
438 * at the end of the plaintext, and expect the encryption
439 * to succeed starting from some offset. Always keep
440 * enough space at the beginning of the buffer.
441 * 3) Try to encrypt with 0, 1, 2, ... bytes available
442 * both at the front and end of the plaintext,
443 * and expect the encryption to succeed starting from
444 * some offset.
445 *
446 * If encryption succeeds, check that decryption succeeds
447 * and yields the original record.
448 */
449
450 mbedtls_ssl_context ssl; /* ONLY for debugging */
451
452 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000453 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +0100454 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000455 mbedtls_record rec, rec_backup;
456
457 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +0100458 int mode; /* Mode 1, 2 or 3 as explained above */
459 size_t offset; /* Available space at beginning/end/both */
460 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000461
Hanno Beckerd856c822019-04-29 17:30:59 +0100462 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
463 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000464
465 int seen_success; /* Indicates if in the current mode we've
466 * already seen a successful test. */
467
468 mbedtls_ssl_init( &ssl );
469 mbedtls_ssl_transform_init( &t0 );
470 mbedtls_ssl_transform_init( &t1 );
471 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100472 etm, tag_mode, ver,
473 (size_t) cid0_len,
474 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000475
Hanno Becker3ee54212019-04-04 16:31:26 +0100476 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000477
478 for( mode=1; mode <= 3; mode++ )
479 {
480 seen_success = 0;
481 for( offset=0; offset <= threshold; offset++ )
482 {
483 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +0100484 t_dec = &t0;
485 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000486
487 memset( rec.ctr, offset, sizeof( rec.ctr ) );
488 rec.type = 42;
489 rec.ver[0] = offset;
490 rec.ver[1] = offset;
491 rec.buf = buf;
492 rec.buf_len = buflen;
Hanno Beckerd856c822019-04-29 17:30:59 +0100493 rec.cid_len = 0;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000494
495 switch( mode )
496 {
497 case 1: /* Space in the beginning */
498 rec.data_offset = offset;
499 rec.data_len = buflen - offset - default_post_padding;
500 break;
501
502 case 2: /* Space in the end */
503 rec.data_offset = default_pre_padding;
504 rec.data_len = buflen - default_pre_padding - offset;
505 break;
506
507 case 3: /* Space in the beginning and end */
508 rec.data_offset = offset;
509 rec.data_len = buflen - 2 * offset;
510 break;
511
512 default:
513 TEST_ASSERT( 0 );
514 break;
515 }
516
517 memset( rec.buf + rec.data_offset, 42, rec.data_len );
518
519 /* Make a copy for later comparison */
520 rec_backup = rec;
521
522 /* Encrypt record */
523 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
524
525 if( ( mode == 1 || mode == 2 ) && seen_success )
526 {
527 TEST_ASSERT( ret == 0 );
528 }
529 else
530 {
531 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
532 if( ret == 0 )
533 seen_success = 1;
534 }
535
536 if( ret != 0 )
537 continue;
538
539 /* Decrypt record with t_dec */
540 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
541
542 /* Compare results */
543 TEST_ASSERT( rec.type == rec_backup.type );
544 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
545 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
546 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
547 TEST_ASSERT( rec.data_len == rec_backup.data_len );
548 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
549 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
550 rec_backup.buf + rec_backup.data_offset,
551 rec.data_len ) == 0 );
552 }
553
554 TEST_ASSERT( seen_success == 1 );
555 }
556
Hanno Becker81e16a32019-03-01 11:21:44 +0000557exit:
558
Hanno Beckerb3268da2018-01-05 15:20:24 +0000559 /* Cleanup */
560 mbedtls_ssl_free( &ssl );
561 mbedtls_ssl_transform_free( &t0 );
562 mbedtls_ssl_transform_free( &t1 );
563
Hanno Becker3ee54212019-04-04 16:31:26 +0100564 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000565}
566/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +0300567
568/* BEGIN_CASE */
569void ssl_tls_prf( int type, data_t * secret, data_t * random,
570 char *label, data_t *result_hex_str, int exp_ret )
571{
572 unsigned char *output;
573
574 output = mbedtls_calloc( 1, result_hex_str->len );
575 if( output == NULL )
576 goto exit;
577
Ron Eldor6b9b1b82019-05-15 17:04:33 +0300578#if defined(MBEDTLS_USE_PSA_CRYPTO)
579 TEST_ASSERT( psa_crypto_init() == 0 );
580#endif
581
Ron Eldor824ad7b2019-05-13 14:09:00 +0300582 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
583 label, random->x, random->len,
584 output, result_hex_str->len ) == exp_ret );
585
586 if( exp_ret == 0 )
587 {
588 TEST_ASSERT( hexcmp( output, result_hex_str->x,
589 result_hex_str->len, result_hex_str->len ) == 0 );
590 }
591exit:
592
593 mbedtls_free( output );
594}
595/* END_CASE */