blob: 744e4f6e3f70521f3fa4cf9fde6bf2ed1b2be607 [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
21
22static int build_transforms( mbedtls_ssl_transform *t_in,
23 mbedtls_ssl_transform *t_out,
24 int cipher_type, int hash_id,
25 int etm, int tag_mode, int ver )
26{
27 mbedtls_cipher_info_t const *cipher_info;
Hanno Beckera5780f12019-04-05 09:55:37 +010028 int ret = 0;
Hanno Beckera18d1322018-01-03 14:27:32 +000029
30 size_t keylen, maclen, ivlen;
Hanno Becker81e16a32019-03-01 11:21:44 +000031 unsigned char *key0 = NULL, *key1 = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +000032 unsigned char iv_enc[16], iv_dec[16];
33
34 maclen = 0;
35
36 /* Pick cipher */
37 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
38 CHK( cipher_info != NULL );
39 CHK( cipher_info->iv_size <= 16 );
40 CHK( cipher_info->key_bitlen % 8 == 0 );
41
42 /* Pick keys */
43 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +010044 /* Allocate `keylen + 1` bytes to ensure that we get
45 * a non-NULL pointers from `mbedtls_calloc` even if
46 * `keylen == 0` in the case of the NULL cipher. */
47 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
48 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +000049 memset( key0, 0x1, keylen );
50 memset( key1, 0x2, keylen );
51
52 /* Setup cipher contexts */
53 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
54 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
55 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
56 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
57
58#if defined(MBEDTLS_CIPHER_MODE_CBC)
59 if( cipher_info->mode == MBEDTLS_MODE_CBC )
60 {
61 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
62 MBEDTLS_PADDING_NONE ) == 0 );
63 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
64 MBEDTLS_PADDING_NONE ) == 0 );
65 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
66 MBEDTLS_PADDING_NONE ) == 0 );
67 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
68 MBEDTLS_PADDING_NONE ) == 0 );
69 }
70#endif /* MBEDTLS_CIPHER_MODE_CBC */
71
72 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
73 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
74 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
75 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
76 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
77 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
78 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
79 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +000080
81 /* Setup MAC contexts */
82#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
83 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
84 cipher_info->mode == MBEDTLS_MODE_STREAM )
85 {
86 mbedtls_md_info_t const *md_info;
87 unsigned char *md0, *md1;
88
89 /* Pick hash */
90 md_info = mbedtls_md_info_from_type( hash_id );
91 CHK( md_info != NULL );
92
93 /* Pick hash keys */
94 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +010095 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
96 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +000097 memset( md0, 0x5, maclen );
98 memset( md1, 0x6, maclen );
99
100 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
101 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
102 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
103 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
104
105 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
106 {
107 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
108 md0, maclen ) == 0 );
109 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
110 md1, maclen ) == 0 );
111 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
112 md1, maclen ) == 0 );
113 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
114 md0, maclen ) == 0 );
115 }
116#if defined(MBEDTLS_SSL_PROTO_SSL3)
117 else
118 {
119 memcpy( &t_in->mac_enc, md0, maclen );
120 memcpy( &t_in->mac_dec, md1, maclen );
121 memcpy( &t_out->mac_enc, md1, maclen );
122 memcpy( &t_out->mac_dec, md0, maclen );
123 }
124#endif
125
Hanno Becker3ee54212019-04-04 16:31:26 +0100126 mbedtls_free( md0 );
127 mbedtls_free( md1 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000128 }
129#else
130 ((void) hash_id);
131#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
132
133
134 /* Pick IV's (regardless of whether they
135 * are being used by the transform). */
136 ivlen = cipher_info->iv_size;
137 memset( iv_enc, 0x3, sizeof( iv_enc ) );
138 memset( iv_dec, 0x4, sizeof( iv_dec ) );
139
140 /*
141 * Setup transforms
142 */
143
144#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
145 t_out->encrypt_then_mac = etm;
146 t_in->encrypt_then_mac = etm;
147#else
148 ((void) etm);
149#endif
150
151 t_out->minor_ver = ver;
152 t_in->minor_ver = ver;
153 t_out->ivlen = ivlen;
154 t_in->ivlen = ivlen;
155
156 switch( cipher_info->mode )
157 {
158 case MBEDTLS_MODE_GCM:
159 case MBEDTLS_MODE_CCM:
160 t_out->fixed_ivlen = 4;
161 t_in->fixed_ivlen = 4;
162 t_out->maclen = 0;
163 t_in->maclen = 0;
164 switch( tag_mode )
165 {
166 case 0: /* Full tag */
167 t_out->taglen = 16;
168 t_in->taglen = 16;
169 break;
170 case 1: /* Partial tag */
171 t_out->taglen = 8;
172 t_in->taglen = 8;
173 break;
174 default:
175 return( 1 );
176 }
177 break;
178
179 case MBEDTLS_MODE_CHACHAPOLY:
180 t_out->fixed_ivlen = 12;
181 t_in->fixed_ivlen = 12;
182 t_out->maclen = 0;
183 t_in->maclen = 0;
184 switch( tag_mode )
185 {
186 case 0: /* Full tag */
187 t_out->taglen = 16;
188 t_in->taglen = 16;
189 break;
190 case 1: /* Partial tag */
191 t_out->taglen = 8;
192 t_in->taglen = 8;
193 break;
194 default:
195 return( 1 );
196 }
197 break;
198
199 case MBEDTLS_MODE_STREAM:
200 case MBEDTLS_MODE_CBC:
201 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
202 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
203 t_out->taglen = 0;
204 t_in->taglen = 0;
205 switch( tag_mode )
206 {
207 case 0: /* Full tag */
208 t_out->maclen = maclen;
209 t_in->maclen = maclen;
210 break;
211 case 1: /* Partial tag */
212 t_out->maclen = 10;
213 t_in->maclen = 10;
214 break;
215 default:
216 return( 1 );
217 }
218 break;
219 default:
220 return( 1 );
221 break;
222 }
223
224 /* Setup IV's */
225
226 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
227 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
228 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
229 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
230
Hanno Becker81e16a32019-03-01 11:21:44 +0000231cleanup:
232
Hanno Becker3ee54212019-04-04 16:31:26 +0100233 mbedtls_free( key0 );
234 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +0000235
Hanno Beckera5780f12019-04-05 09:55:37 +0100236 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +0000237}
238
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200239/* END_HEADER */
240
241/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200243 * END_DEPENDENCIES
244 */
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100247void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200248{
Azim Khand30ca132017-06-09 04:32:58 +0100249 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200251 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200252
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200253 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200254 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200255
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200256 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
257 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200258 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
259 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200260 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200261
262 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100263 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200264 {
Azim Khand30ca132017-06-09 04:32:58 +0100265 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200267 }
268
269 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100270 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200274 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200275}
276/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100277
278/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
279void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
280{
281 mbedtls_ssl_context ssl;
282 mbedtls_ssl_init( &ssl );
283
284 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
285 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
286
287 mbedtls_ssl_free( &ssl );
288}
Darryl Green11999bb2018-03-13 15:22:58 +0000289/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000290
291/* BEGIN_CASE */
292void ssl_crypt_record( int cipher_type, int hash_id,
293 int etm, int tag_mode, int ver )
294{
295 /*
296 * Test several record encryptions and decryptions
297 * with plenty of space before and after the data
298 * within the record buffer.
299 */
300
301 int ret;
302 int num_records = 16;
303 mbedtls_ssl_context ssl; /* ONLY for debugging */
304
305 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000306 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +0000307 size_t const buflen = 512;
308 mbedtls_record rec, rec_backup;
309
310 mbedtls_ssl_init( &ssl );
311 mbedtls_ssl_transform_init( &t0 );
312 mbedtls_ssl_transform_init( &t1 );
313 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
314 etm, tag_mode, ver ) == 0 );
315
Hanno Becker3ee54212019-04-04 16:31:26 +0100316 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000317
318 while( num_records-- > 0 )
319 {
320 mbedtls_ssl_transform *t_dec, *t_enc;
321 /* Take turns in who's sending and who's receiving. */
322 if( num_records % 3 == 0 )
323 {
324 t_dec = &t0;
325 t_enc = &t1;
326 }
327 else
328 {
329 t_dec = &t1;
330 t_enc = &t0;
331 }
332
333 /*
334 * The record header affects the transformation in two ways:
335 * 1) It determines the AEAD additional data
336 * 2) The record counter sometimes determines the IV.
337 *
338 * Apart from that, the fields don't have influence.
339 * In particular, it is currently not the responsibility
340 * of ssl_encrypt/decrypt_buf to check if the transform
341 * version matches the record version, or that the
342 * type is sensible.
343 */
344
345 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
346 rec.type = 42;
347 rec.ver[0] = num_records;
348 rec.ver[1] = num_records;
349
350 rec.buf = buf;
351 rec.buf_len = buflen;
352 rec.data_offset = 16;
353 /* Make sure to vary the length to exercise different
354 * paddings. */
355 rec.data_len = 1 + num_records;
356
357 memset( rec.buf + rec.data_offset, 42, rec.data_len );
358
359 /* Make a copy for later comparison */
360 rec_backup = rec;
361
362 /* Encrypt record */
363 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
364 rnd_std_rand, NULL );
365 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
366 if( ret != 0 )
367 {
368 continue;
369 }
370
371 /* Decrypt record with t_dec */
372 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
373
374 /* Compare results */
375 TEST_ASSERT( rec.type == rec_backup.type );
376 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
377 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
378 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
379 TEST_ASSERT( rec.data_len == rec_backup.data_len );
380 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
381 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
382 rec_backup.buf + rec_backup.data_offset,
383 rec.data_len ) == 0 );
384 }
385
Hanno Becker81e16a32019-03-01 11:21:44 +0000386exit:
387
Hanno Beckera18d1322018-01-03 14:27:32 +0000388 /* Cleanup */
389 mbedtls_ssl_free( &ssl );
390 mbedtls_ssl_transform_free( &t0 );
391 mbedtls_ssl_transform_free( &t1 );
392
Hanno Becker3ee54212019-04-04 16:31:26 +0100393 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +0000394}
395/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000396
397/* BEGIN_CASE */
398void ssl_crypt_record_small( int cipher_type, int hash_id,
399 int etm, int tag_mode, int ver )
400{
401 /*
402 * Test pairs of encryption and decryption with an increasing
403 * amount of space in the record buffer - in more detail:
404 * 1) Try to encrypt with 0, 1, 2, ... bytes available
405 * in front of the plaintext, and expect the encryption
406 * to succeed starting from some offset. Always keep
407 * enough space in the end of the buffer.
408 * 2) Try to encrypt with 0, 1, 2, ... bytes available
409 * at the end of the plaintext, and expect the encryption
410 * to succeed starting from some offset. Always keep
411 * enough space at the beginning of the buffer.
412 * 3) Try to encrypt with 0, 1, 2, ... bytes available
413 * both at the front and end of the plaintext,
414 * and expect the encryption to succeed starting from
415 * some offset.
416 *
417 * If encryption succeeds, check that decryption succeeds
418 * and yields the original record.
419 */
420
421 mbedtls_ssl_context ssl; /* ONLY for debugging */
422
423 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000424 unsigned char *buf = NULL;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000425 size_t const buflen = 150;
426 mbedtls_record rec, rec_backup;
427
428 int ret;
429 int mode; /* Mode 1, 2 or 3 as explained above */
430 size_t offset; /* Available space at beginning/end/both */
431 size_t threshold = 64; /* Maximum offset to test against */
432
433 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
434 size_t default_post_padding = 64; /* Post-padding to use in mode 1 */
435
436 int seen_success; /* Indicates if in the current mode we've
437 * already seen a successful test. */
438
439 mbedtls_ssl_init( &ssl );
440 mbedtls_ssl_transform_init( &t0 );
441 mbedtls_ssl_transform_init( &t1 );
442 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
443 etm, tag_mode, ver ) == 0 );
444
Hanno Becker3ee54212019-04-04 16:31:26 +0100445 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000446
447 for( mode=1; mode <= 3; mode++ )
448 {
449 seen_success = 0;
450 for( offset=0; offset <= threshold; offset++ )
451 {
452 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +0100453 t_dec = &t0;
454 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000455
456 memset( rec.ctr, offset, sizeof( rec.ctr ) );
457 rec.type = 42;
458 rec.ver[0] = offset;
459 rec.ver[1] = offset;
460 rec.buf = buf;
461 rec.buf_len = buflen;
462
463 switch( mode )
464 {
465 case 1: /* Space in the beginning */
466 rec.data_offset = offset;
467 rec.data_len = buflen - offset - default_post_padding;
468 break;
469
470 case 2: /* Space in the end */
471 rec.data_offset = default_pre_padding;
472 rec.data_len = buflen - default_pre_padding - offset;
473 break;
474
475 case 3: /* Space in the beginning and end */
476 rec.data_offset = offset;
477 rec.data_len = buflen - 2 * offset;
478 break;
479
480 default:
481 TEST_ASSERT( 0 );
482 break;
483 }
484
485 memset( rec.buf + rec.data_offset, 42, rec.data_len );
486
487 /* Make a copy for later comparison */
488 rec_backup = rec;
489
490 /* Encrypt record */
491 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
492
493 if( ( mode == 1 || mode == 2 ) && seen_success )
494 {
495 TEST_ASSERT( ret == 0 );
496 }
497 else
498 {
499 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
500 if( ret == 0 )
501 seen_success = 1;
502 }
503
504 if( ret != 0 )
505 continue;
506
507 /* Decrypt record with t_dec */
508 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
509
510 /* Compare results */
511 TEST_ASSERT( rec.type == rec_backup.type );
512 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
513 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
514 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
515 TEST_ASSERT( rec.data_len == rec_backup.data_len );
516 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
517 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
518 rec_backup.buf + rec_backup.data_offset,
519 rec.data_len ) == 0 );
520 }
521
522 TEST_ASSERT( seen_success == 1 );
523 }
524
Hanno Becker81e16a32019-03-01 11:21:44 +0000525exit:
526
Hanno Beckerb3268da2018-01-05 15:20:24 +0000527 /* Cleanup */
528 mbedtls_ssl_free( &ssl );
529 mbedtls_ssl_transform_free( &t0 );
530 mbedtls_ssl_transform_free( &t1 );
531
Hanno Becker3ee54212019-04-04 16:31:26 +0100532 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000533}
534/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +0300535
536/* BEGIN_CASE */
537void ssl_tls_prf( int type, data_t * secret, data_t * random,
538 char *label, data_t *result_hex_str, int exp_ret )
539{
540 unsigned char *output;
541
542 output = mbedtls_calloc( 1, result_hex_str->len );
543 if( output == NULL )
544 goto exit;
545
Ron Eldor6b9b1b82019-05-15 17:04:33 +0300546#if defined(MBEDTLS_USE_PSA_CRYPTO)
547 TEST_ASSERT( psa_crypto_init() == 0 );
548#endif
549
Ron Eldor824ad7b2019-05-13 14:09:00 +0300550 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
551 label, random->x, random->len,
552 output, result_hex_str->len ) == exp_ret );
553
554 if( exp_ret == 0 )
555 {
556 TEST_ASSERT( hexcmp( output, result_hex_str->x,
557 result_hex_str->len, result_hex_str->len ) == 0 );
558 }
559exit:
560
561 mbedtls_free( output );
562}
563/* END_CASE */