blob: 9f80b1e5079b440160460e82c0aa36f6b0698322 [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 Becker43c24b82019-05-01 09:45:57 +010041#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd856c822019-04-29 17:30:59 +010042 unsigned char cid0[ SSL_CID_LEN_MIN ];
43 unsigned char cid1[ SSL_CID_LEN_MIN ];
44
45 rnd_std_rand( NULL, cid0, sizeof( cid0 ) );
46 rnd_std_rand( NULL, cid1, sizeof( cid1 ) );
Hanno Becker43c24b82019-05-01 09:45:57 +010047#else
48 ((void) cid0_len);
49 ((void) cid1_len);
50#endif /* MBEDTLS_SSL_CID */
Hanno Beckerd856c822019-04-29 17:30:59 +010051
Hanno Beckera18d1322018-01-03 14:27:32 +000052 maclen = 0;
53
54 /* Pick cipher */
55 cipher_info = mbedtls_cipher_info_from_type( cipher_type );
56 CHK( cipher_info != NULL );
57 CHK( cipher_info->iv_size <= 16 );
58 CHK( cipher_info->key_bitlen % 8 == 0 );
59
60 /* Pick keys */
61 keylen = cipher_info->key_bitlen / 8;
Hanno Becker78d1f702019-04-05 09:56:10 +010062 /* Allocate `keylen + 1` bytes to ensure that we get
63 * a non-NULL pointers from `mbedtls_calloc` even if
64 * `keylen == 0` in the case of the NULL cipher. */
65 CHK( ( key0 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
66 CHK( ( key1 = mbedtls_calloc( 1, keylen + 1 ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +000067 memset( key0, 0x1, keylen );
68 memset( key1, 0x2, keylen );
69
70 /* Setup cipher contexts */
71 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_enc, cipher_info ) == 0 );
72 CHK( mbedtls_cipher_setup( &t_in->cipher_ctx_dec, cipher_info ) == 0 );
73 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_enc, cipher_info ) == 0 );
74 CHK( mbedtls_cipher_setup( &t_out->cipher_ctx_dec, cipher_info ) == 0 );
75
76#if defined(MBEDTLS_CIPHER_MODE_CBC)
77 if( cipher_info->mode == MBEDTLS_MODE_CBC )
78 {
79 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_enc,
80 MBEDTLS_PADDING_NONE ) == 0 );
81 CHK( mbedtls_cipher_set_padding_mode( &t_in->cipher_ctx_dec,
82 MBEDTLS_PADDING_NONE ) == 0 );
83 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_enc,
84 MBEDTLS_PADDING_NONE ) == 0 );
85 CHK( mbedtls_cipher_set_padding_mode( &t_out->cipher_ctx_dec,
86 MBEDTLS_PADDING_NONE ) == 0 );
87 }
88#endif /* MBEDTLS_CIPHER_MODE_CBC */
89
90 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_enc, key0,
91 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
92 CHK( mbedtls_cipher_setkey( &t_in->cipher_ctx_dec, key1,
93 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
94 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_enc, key1,
95 keylen << 3, MBEDTLS_ENCRYPT ) == 0 );
96 CHK( mbedtls_cipher_setkey( &t_out->cipher_ctx_dec, key0,
97 keylen << 3, MBEDTLS_DECRYPT ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +000098
99 /* Setup MAC contexts */
100#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
101 if( cipher_info->mode == MBEDTLS_MODE_CBC ||
102 cipher_info->mode == MBEDTLS_MODE_STREAM )
103 {
104 mbedtls_md_info_t const *md_info;
105 unsigned char *md0, *md1;
106
107 /* Pick hash */
108 md_info = mbedtls_md_info_from_type( hash_id );
109 CHK( md_info != NULL );
110
111 /* Pick hash keys */
112 maclen = mbedtls_md_get_size( md_info );
Hanno Becker3ee54212019-04-04 16:31:26 +0100113 CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
114 CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000115 memset( md0, 0x5, maclen );
116 memset( md1, 0x6, maclen );
117
118 CHK( mbedtls_md_setup( &t_out->md_ctx_enc, md_info, 1 ) == 0 );
119 CHK( mbedtls_md_setup( &t_out->md_ctx_dec, md_info, 1 ) == 0 );
120 CHK( mbedtls_md_setup( &t_in->md_ctx_enc, md_info, 1 ) == 0 );
121 CHK( mbedtls_md_setup( &t_in->md_ctx_dec, md_info, 1 ) == 0 );
122
123 if( ver > MBEDTLS_SSL_MINOR_VERSION_0 )
124 {
125 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_enc,
126 md0, maclen ) == 0 );
127 CHK( mbedtls_md_hmac_starts( &t_in->md_ctx_dec,
128 md1, maclen ) == 0 );
129 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_enc,
130 md1, maclen ) == 0 );
131 CHK( mbedtls_md_hmac_starts( &t_out->md_ctx_dec,
132 md0, maclen ) == 0 );
133 }
134#if defined(MBEDTLS_SSL_PROTO_SSL3)
135 else
136 {
137 memcpy( &t_in->mac_enc, md0, maclen );
138 memcpy( &t_in->mac_dec, md1, maclen );
139 memcpy( &t_out->mac_enc, md1, maclen );
140 memcpy( &t_out->mac_dec, md0, maclen );
141 }
142#endif
143
Hanno Becker3ee54212019-04-04 16:31:26 +0100144 mbedtls_free( md0 );
145 mbedtls_free( md1 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000146 }
147#else
148 ((void) hash_id);
149#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
150
151
152 /* Pick IV's (regardless of whether they
153 * are being used by the transform). */
154 ivlen = cipher_info->iv_size;
155 memset( iv_enc, 0x3, sizeof( iv_enc ) );
156 memset( iv_dec, 0x4, sizeof( iv_dec ) );
157
158 /*
159 * Setup transforms
160 */
161
162#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
163 t_out->encrypt_then_mac = etm;
164 t_in->encrypt_then_mac = etm;
165#else
166 ((void) etm);
167#endif
168
169 t_out->minor_ver = ver;
170 t_in->minor_ver = ver;
171 t_out->ivlen = ivlen;
172 t_in->ivlen = ivlen;
173
174 switch( cipher_info->mode )
175 {
176 case MBEDTLS_MODE_GCM:
177 case MBEDTLS_MODE_CCM:
178 t_out->fixed_ivlen = 4;
179 t_in->fixed_ivlen = 4;
180 t_out->maclen = 0;
181 t_in->maclen = 0;
182 switch( tag_mode )
183 {
184 case 0: /* Full tag */
185 t_out->taglen = 16;
186 t_in->taglen = 16;
187 break;
188 case 1: /* Partial tag */
189 t_out->taglen = 8;
190 t_in->taglen = 8;
191 break;
192 default:
193 return( 1 );
194 }
195 break;
196
197 case MBEDTLS_MODE_CHACHAPOLY:
198 t_out->fixed_ivlen = 12;
199 t_in->fixed_ivlen = 12;
200 t_out->maclen = 0;
201 t_in->maclen = 0;
202 switch( tag_mode )
203 {
204 case 0: /* Full tag */
205 t_out->taglen = 16;
206 t_in->taglen = 16;
207 break;
208 case 1: /* Partial tag */
209 t_out->taglen = 8;
210 t_in->taglen = 8;
211 break;
212 default:
213 return( 1 );
214 }
215 break;
216
217 case MBEDTLS_MODE_STREAM:
218 case MBEDTLS_MODE_CBC:
219 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
220 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
221 t_out->taglen = 0;
222 t_in->taglen = 0;
223 switch( tag_mode )
224 {
225 case 0: /* Full tag */
226 t_out->maclen = maclen;
227 t_in->maclen = maclen;
228 break;
229 case 1: /* Partial tag */
230 t_out->maclen = 10;
231 t_in->maclen = 10;
232 break;
233 default:
234 return( 1 );
235 }
236 break;
237 default:
238 return( 1 );
239 break;
240 }
241
242 /* Setup IV's */
243
244 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
245 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
246 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
247 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
248
Hanno Becker43c24b82019-05-01 09:45:57 +0100249#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100250 /* Add CID */
251 memcpy( &t_in->in_cid, cid0, cid0_len );
252 memcpy( &t_in->out_cid, cid1, cid1_len );
253 t_in->in_cid_len = cid0_len;
254 t_in->out_cid_len = cid1_len;
255 memcpy( &t_out->in_cid, cid1, cid1_len );
256 memcpy( &t_out->out_cid, cid0, cid0_len );
257 t_out->in_cid_len = cid1_len;
258 t_out->out_cid_len = cid0_len;
Hanno Becker43c24b82019-05-01 09:45:57 +0100259#endif /* MBEDTLS_SSL_CID */
Hanno Beckerd856c822019-04-29 17:30:59 +0100260
Hanno Becker81e16a32019-03-01 11:21:44 +0000261cleanup:
262
Hanno Becker3ee54212019-04-04 16:31:26 +0100263 mbedtls_free( key0 );
264 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +0000265
Hanno Beckera5780f12019-04-05 09:55:37 +0100266 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +0000267}
268
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200269/* END_HEADER */
270
271/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200273 * END_DEPENDENCIES
274 */
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100277void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200278{
Azim Khand30ca132017-06-09 04:32:58 +0100279 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200281 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200282
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200283 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200284 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200285
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200286 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
287 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200288 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
289 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200290 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200291
292 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100293 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200294 {
Azim Khand30ca132017-06-09 04:32:58 +0100295 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200297 }
298
299 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100300 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200304 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200305}
306/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100307
308/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
309void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
310{
311 mbedtls_ssl_context ssl;
312 mbedtls_ssl_init( &ssl );
313
314 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
315 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
316
317 mbedtls_ssl_free( &ssl );
318}
Darryl Green11999bb2018-03-13 15:22:58 +0000319/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000320
321/* BEGIN_CASE */
322void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100323 int etm, int tag_mode, int ver,
324 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +0000325{
326 /*
327 * Test several record encryptions and decryptions
328 * with plenty of space before and after the data
329 * within the record buffer.
330 */
331
332 int ret;
333 int num_records = 16;
334 mbedtls_ssl_context ssl; /* ONLY for debugging */
335
336 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000337 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +0000338 size_t const buflen = 512;
339 mbedtls_record rec, rec_backup;
340
341 mbedtls_ssl_init( &ssl );
342 mbedtls_ssl_transform_init( &t0 );
343 mbedtls_ssl_transform_init( &t1 );
344 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100345 etm, tag_mode, ver,
346 (size_t) cid0_len,
347 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000348
Hanno Becker3ee54212019-04-04 16:31:26 +0100349 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000350
351 while( num_records-- > 0 )
352 {
353 mbedtls_ssl_transform *t_dec, *t_enc;
354 /* Take turns in who's sending and who's receiving. */
355 if( num_records % 3 == 0 )
356 {
357 t_dec = &t0;
358 t_enc = &t1;
359 }
360 else
361 {
362 t_dec = &t1;
363 t_enc = &t0;
364 }
365
366 /*
367 * The record header affects the transformation in two ways:
368 * 1) It determines the AEAD additional data
369 * 2) The record counter sometimes determines the IV.
370 *
371 * Apart from that, the fields don't have influence.
372 * In particular, it is currently not the responsibility
373 * of ssl_encrypt/decrypt_buf to check if the transform
374 * version matches the record version, or that the
375 * type is sensible.
376 */
377
378 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
379 rec.type = 42;
380 rec.ver[0] = num_records;
381 rec.ver[1] = num_records;
Hanno Becker43c24b82019-05-01 09:45:57 +0100382#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100383 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +0100384#endif /* MBEDTLS_SSL_CID */
Hanno Beckera18d1322018-01-03 14:27:32 +0000385
386 rec.buf = buf;
387 rec.buf_len = buflen;
388 rec.data_offset = 16;
389 /* Make sure to vary the length to exercise different
390 * paddings. */
391 rec.data_len = 1 + num_records;
392
393 memset( rec.buf + rec.data_offset, 42, rec.data_len );
394
395 /* Make a copy for later comparison */
396 rec_backup = rec;
397
398 /* Encrypt record */
399 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
400 rnd_std_rand, NULL );
401 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
402 if( ret != 0 )
403 {
404 continue;
405 }
406
407 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +0100408 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
409 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000410
411 /* Compare results */
412 TEST_ASSERT( rec.type == rec_backup.type );
413 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
414 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
415 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
416 TEST_ASSERT( rec.data_len == rec_backup.data_len );
417 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
418 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
419 rec_backup.buf + rec_backup.data_offset,
420 rec.data_len ) == 0 );
421 }
422
Hanno Becker81e16a32019-03-01 11:21:44 +0000423exit:
424
Hanno Beckera18d1322018-01-03 14:27:32 +0000425 /* Cleanup */
426 mbedtls_ssl_free( &ssl );
427 mbedtls_ssl_transform_free( &t0 );
428 mbedtls_ssl_transform_free( &t1 );
429
Hanno Becker3ee54212019-04-04 16:31:26 +0100430 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +0000431}
432/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000433
434/* BEGIN_CASE */
435void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100436 int etm, int tag_mode, int ver,
437 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +0000438{
439 /*
440 * Test pairs of encryption and decryption with an increasing
441 * amount of space in the record buffer - in more detail:
442 * 1) Try to encrypt with 0, 1, 2, ... bytes available
443 * in front of the plaintext, and expect the encryption
444 * to succeed starting from some offset. Always keep
445 * enough space in the end of the buffer.
446 * 2) Try to encrypt with 0, 1, 2, ... bytes available
447 * at the end of the plaintext, and expect the encryption
448 * to succeed starting from some offset. Always keep
449 * enough space at the beginning of the buffer.
450 * 3) Try to encrypt with 0, 1, 2, ... bytes available
451 * both at the front and end of the plaintext,
452 * and expect the encryption to succeed starting from
453 * some offset.
454 *
455 * If encryption succeeds, check that decryption succeeds
456 * and yields the original record.
457 */
458
459 mbedtls_ssl_context ssl; /* ONLY for debugging */
460
461 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000462 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +0100463 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000464 mbedtls_record rec, rec_backup;
465
466 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +0100467 int mode; /* Mode 1, 2 or 3 as explained above */
468 size_t offset; /* Available space at beginning/end/both */
469 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000470
Hanno Beckerd856c822019-04-29 17:30:59 +0100471 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
472 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000473
474 int seen_success; /* Indicates if in the current mode we've
475 * already seen a successful test. */
476
477 mbedtls_ssl_init( &ssl );
478 mbedtls_ssl_transform_init( &t0 );
479 mbedtls_ssl_transform_init( &t1 );
480 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100481 etm, tag_mode, ver,
482 (size_t) cid0_len,
483 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000484
Hanno Becker3ee54212019-04-04 16:31:26 +0100485 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000486
487 for( mode=1; mode <= 3; mode++ )
488 {
489 seen_success = 0;
490 for( offset=0; offset <= threshold; offset++ )
491 {
492 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +0100493 t_dec = &t0;
494 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000495
496 memset( rec.ctr, offset, sizeof( rec.ctr ) );
497 rec.type = 42;
498 rec.ver[0] = offset;
499 rec.ver[1] = offset;
500 rec.buf = buf;
501 rec.buf_len = buflen;
Hanno Becker43c24b82019-05-01 09:45:57 +0100502#if defined(MBEDTLS_SSL_CID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100503 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +0100504#endif /* MBEDTLS_SSL_CID */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000505
506 switch( mode )
507 {
508 case 1: /* Space in the beginning */
509 rec.data_offset = offset;
510 rec.data_len = buflen - offset - default_post_padding;
511 break;
512
513 case 2: /* Space in the end */
514 rec.data_offset = default_pre_padding;
515 rec.data_len = buflen - default_pre_padding - offset;
516 break;
517
518 case 3: /* Space in the beginning and end */
519 rec.data_offset = offset;
520 rec.data_len = buflen - 2 * offset;
521 break;
522
523 default:
524 TEST_ASSERT( 0 );
525 break;
526 }
527
528 memset( rec.buf + rec.data_offset, 42, rec.data_len );
529
530 /* Make a copy for later comparison */
531 rec_backup = rec;
532
533 /* Encrypt record */
534 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
535
536 if( ( mode == 1 || mode == 2 ) && seen_success )
537 {
538 TEST_ASSERT( ret == 0 );
539 }
540 else
541 {
542 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
543 if( ret == 0 )
544 seen_success = 1;
545 }
546
547 if( ret != 0 )
548 continue;
549
550 /* Decrypt record with t_dec */
551 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
552
553 /* Compare results */
554 TEST_ASSERT( rec.type == rec_backup.type );
555 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
556 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
557 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
558 TEST_ASSERT( rec.data_len == rec_backup.data_len );
559 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
560 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
561 rec_backup.buf + rec_backup.data_offset,
562 rec.data_len ) == 0 );
563 }
564
565 TEST_ASSERT( seen_success == 1 );
566 }
567
Hanno Becker81e16a32019-03-01 11:21:44 +0000568exit:
569
Hanno Beckerb3268da2018-01-05 15:20:24 +0000570 /* Cleanup */
571 mbedtls_ssl_free( &ssl );
572 mbedtls_ssl_transform_free( &t0 );
573 mbedtls_ssl_transform_free( &t1 );
574
Hanno Becker3ee54212019-04-04 16:31:26 +0100575 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000576}
577/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +0300578
579/* BEGIN_CASE */
580void ssl_tls_prf( int type, data_t * secret, data_t * random,
581 char *label, data_t *result_hex_str, int exp_ret )
582{
583 unsigned char *output;
584
585 output = mbedtls_calloc( 1, result_hex_str->len );
586 if( output == NULL )
587 goto exit;
588
Ron Eldor6b9b1b82019-05-15 17:04:33 +0300589#if defined(MBEDTLS_USE_PSA_CRYPTO)
590 TEST_ASSERT( psa_crypto_init() == 0 );
591#endif
592
Ron Eldor824ad7b2019-05-13 14:09:00 +0300593 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
594 label, random->x, random->len,
595 output, result_hex_str->len ) == exp_ret );
596
597 if( exp_ret == 0 )
598 {
599 TEST_ASSERT( hexcmp( output, result_hex_str->x,
600 result_hex_str->len, result_hex_str->len ) == 0 );
601 }
602exit:
603
604 mbedtls_free( output );
605}
606/* END_CASE */