blob: 104a52f229124d36f3fb0217593f2d244fab6d50 [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 Beckera0e20d02019-05-15 14:03:01 +010041#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
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);
Hanno Beckera0e20d02019-05-15 14:03:01 +010050#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
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
Jaeden Amero2de07f12019-06-05 13:32:08 +0100162#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
163 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Beckera18d1322018-01-03 14:27:32 +0000164 t_out->encrypt_then_mac = etm;
165 t_in->encrypt_then_mac = etm;
166#else
167 ((void) etm);
168#endif
169
170 t_out->minor_ver = ver;
171 t_in->minor_ver = ver;
172 t_out->ivlen = ivlen;
173 t_in->ivlen = ivlen;
174
175 switch( cipher_info->mode )
176 {
177 case MBEDTLS_MODE_GCM:
178 case MBEDTLS_MODE_CCM:
179 t_out->fixed_ivlen = 4;
180 t_in->fixed_ivlen = 4;
181 t_out->maclen = 0;
182 t_in->maclen = 0;
183 switch( tag_mode )
184 {
185 case 0: /* Full tag */
186 t_out->taglen = 16;
187 t_in->taglen = 16;
188 break;
189 case 1: /* Partial tag */
190 t_out->taglen = 8;
191 t_in->taglen = 8;
192 break;
193 default:
194 return( 1 );
195 }
196 break;
197
198 case MBEDTLS_MODE_CHACHAPOLY:
199 t_out->fixed_ivlen = 12;
200 t_in->fixed_ivlen = 12;
201 t_out->maclen = 0;
202 t_in->maclen = 0;
203 switch( tag_mode )
204 {
205 case 0: /* Full tag */
206 t_out->taglen = 16;
207 t_in->taglen = 16;
208 break;
209 case 1: /* Partial tag */
210 t_out->taglen = 8;
211 t_in->taglen = 8;
212 break;
213 default:
214 return( 1 );
215 }
216 break;
217
218 case MBEDTLS_MODE_STREAM:
219 case MBEDTLS_MODE_CBC:
220 t_out->fixed_ivlen = 0; /* redundant, must be 0 */
221 t_in->fixed_ivlen = 0; /* redundant, must be 0 */
222 t_out->taglen = 0;
223 t_in->taglen = 0;
224 switch( tag_mode )
225 {
226 case 0: /* Full tag */
227 t_out->maclen = maclen;
228 t_in->maclen = maclen;
229 break;
230 case 1: /* Partial tag */
231 t_out->maclen = 10;
232 t_in->maclen = 10;
233 break;
234 default:
235 return( 1 );
236 }
237 break;
238 default:
239 return( 1 );
240 break;
241 }
242
243 /* Setup IV's */
244
245 memcpy( &t_in->iv_dec, iv_dec, sizeof( iv_dec ) );
246 memcpy( &t_in->iv_enc, iv_enc, sizeof( iv_enc ) );
247 memcpy( &t_out->iv_dec, iv_enc, sizeof( iv_enc ) );
248 memcpy( &t_out->iv_enc, iv_dec, sizeof( iv_dec ) );
249
Hanno Beckera0e20d02019-05-15 14:03:01 +0100250#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100251 /* Add CID */
252 memcpy( &t_in->in_cid, cid0, cid0_len );
253 memcpy( &t_in->out_cid, cid1, cid1_len );
254 t_in->in_cid_len = cid0_len;
255 t_in->out_cid_len = cid1_len;
256 memcpy( &t_out->in_cid, cid1, cid1_len );
257 memcpy( &t_out->out_cid, cid0, cid0_len );
258 t_out->in_cid_len = cid1_len;
259 t_out->out_cid_len = cid0_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +0100260#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerd856c822019-04-29 17:30:59 +0100261
Hanno Becker81e16a32019-03-01 11:21:44 +0000262cleanup:
263
Hanno Becker3ee54212019-04-04 16:31:26 +0100264 mbedtls_free( key0 );
265 mbedtls_free( key1 );
Hanno Becker81e16a32019-03-01 11:21:44 +0000266
Hanno Beckera5780f12019-04-05 09:55:37 +0100267 return( ret );
Hanno Beckera18d1322018-01-03 14:27:32 +0000268}
269
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200270/* END_HEADER */
271
272/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 * depends_on:MBEDTLS_SSL_TLS_C
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200274 * END_DEPENDENCIES
275 */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277/* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Azim Khan5fcca462018-06-29 11:05:32 +0100278void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200279{
Azim Khand30ca132017-06-09 04:32:58 +0100280 uint32_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_ssl_context ssl;
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200282 mbedtls_ssl_config conf;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200283
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200284 mbedtls_ssl_init( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200285 mbedtls_ssl_config_init( &conf );
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +0200286
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +0200287 TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
288 MBEDTLS_SSL_IS_CLIENT,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +0200289 MBEDTLS_SSL_TRANSPORT_DATAGRAM,
290 MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200291 TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200292
293 /* Read previous record numbers */
Azim Khand30ca132017-06-09 04:32:58 +0100294 for( len = 0; len < prevs->len; len += 6 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200295 {
Azim Khand30ca132017-06-09 04:32:58 +0100296 memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_ssl_dtls_replay_update( &ssl );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200298 }
299
300 /* Check new number */
Azim Khand30ca132017-06-09 04:32:58 +0100301 memcpy( ssl.in_ctr + 2, new->x, 6 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_ssl_free( &ssl );
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +0200305 mbedtls_ssl_config_free( &conf );
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +0200306}
307/* END_CASE */
Hanno Beckerb25c0c72017-05-05 11:24:30 +0100308
309/* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
310void ssl_set_hostname_twice( char *hostname0, char *hostname1 )
311{
312 mbedtls_ssl_context ssl;
313 mbedtls_ssl_init( &ssl );
314
315 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname0 ) == 0 );
316 TEST_ASSERT( mbedtls_ssl_set_hostname( &ssl, hostname1 ) == 0 );
317
318 mbedtls_ssl_free( &ssl );
319}
Darryl Green11999bb2018-03-13 15:22:58 +0000320/* END_CASE */
Hanno Beckera18d1322018-01-03 14:27:32 +0000321
322/* BEGIN_CASE */
323void ssl_crypt_record( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100324 int etm, int tag_mode, int ver,
325 int cid0_len, int cid1_len )
Hanno Beckera18d1322018-01-03 14:27:32 +0000326{
327 /*
328 * Test several record encryptions and decryptions
329 * with plenty of space before and after the data
330 * within the record buffer.
331 */
332
333 int ret;
334 int num_records = 16;
335 mbedtls_ssl_context ssl; /* ONLY for debugging */
336
337 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000338 unsigned char *buf = NULL;
Hanno Beckera18d1322018-01-03 14:27:32 +0000339 size_t const buflen = 512;
340 mbedtls_record rec, rec_backup;
341
342 mbedtls_ssl_init( &ssl );
343 mbedtls_ssl_transform_init( &t0 );
344 mbedtls_ssl_transform_init( &t1 );
345 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100346 etm, tag_mode, ver,
347 (size_t) cid0_len,
348 (size_t) cid1_len ) == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000349
Hanno Becker3ee54212019-04-04 16:31:26 +0100350 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckera18d1322018-01-03 14:27:32 +0000351
352 while( num_records-- > 0 )
353 {
354 mbedtls_ssl_transform *t_dec, *t_enc;
355 /* Take turns in who's sending and who's receiving. */
356 if( num_records % 3 == 0 )
357 {
358 t_dec = &t0;
359 t_enc = &t1;
360 }
361 else
362 {
363 t_dec = &t1;
364 t_enc = &t0;
365 }
366
367 /*
368 * The record header affects the transformation in two ways:
369 * 1) It determines the AEAD additional data
370 * 2) The record counter sometimes determines the IV.
371 *
372 * Apart from that, the fields don't have influence.
373 * In particular, it is currently not the responsibility
374 * of ssl_encrypt/decrypt_buf to check if the transform
375 * version matches the record version, or that the
376 * type is sensible.
377 */
378
379 memset( rec.ctr, num_records, sizeof( rec.ctr ) );
380 rec.type = 42;
381 rec.ver[0] = num_records;
382 rec.ver[1] = num_records;
Hanno Beckera0e20d02019-05-15 14:03:01 +0100383#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100384 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +0100385#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckera18d1322018-01-03 14:27:32 +0000386
387 rec.buf = buf;
388 rec.buf_len = buflen;
389 rec.data_offset = 16;
390 /* Make sure to vary the length to exercise different
391 * paddings. */
392 rec.data_len = 1 + num_records;
393
394 memset( rec.buf + rec.data_offset, 42, rec.data_len );
395
396 /* Make a copy for later comparison */
397 rec_backup = rec;
398
399 /* Encrypt record */
400 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec,
401 rnd_std_rand, NULL );
402 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
403 if( ret != 0 )
404 {
405 continue;
406 }
407
408 /* Decrypt record with t_dec */
Hanno Beckerd856c822019-04-29 17:30:59 +0100409 ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
410 TEST_ASSERT( ret == 0 );
Hanno Beckera18d1322018-01-03 14:27:32 +0000411
412 /* Compare results */
413 TEST_ASSERT( rec.type == rec_backup.type );
414 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
415 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
416 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
417 TEST_ASSERT( rec.data_len == rec_backup.data_len );
418 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
419 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
420 rec_backup.buf + rec_backup.data_offset,
421 rec.data_len ) == 0 );
422 }
423
Hanno Becker81e16a32019-03-01 11:21:44 +0000424exit:
425
Hanno Beckera18d1322018-01-03 14:27:32 +0000426 /* Cleanup */
427 mbedtls_ssl_free( &ssl );
428 mbedtls_ssl_transform_free( &t0 );
429 mbedtls_ssl_transform_free( &t1 );
430
Hanno Becker3ee54212019-04-04 16:31:26 +0100431 mbedtls_free( buf );
Hanno Beckera18d1322018-01-03 14:27:32 +0000432}
433/* END_CASE */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000434
435/* BEGIN_CASE */
436void ssl_crypt_record_small( int cipher_type, int hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100437 int etm, int tag_mode, int ver,
438 int cid0_len, int cid1_len )
Hanno Beckerb3268da2018-01-05 15:20:24 +0000439{
440 /*
441 * Test pairs of encryption and decryption with an increasing
442 * amount of space in the record buffer - in more detail:
443 * 1) Try to encrypt with 0, 1, 2, ... bytes available
444 * in front of the plaintext, and expect the encryption
445 * to succeed starting from some offset. Always keep
446 * enough space in the end of the buffer.
447 * 2) Try to encrypt with 0, 1, 2, ... bytes available
448 * at the end of the plaintext, and expect the encryption
449 * to succeed starting from some offset. Always keep
450 * enough space at the beginning of the buffer.
451 * 3) Try to encrypt with 0, 1, 2, ... bytes available
452 * both at the front and end of the plaintext,
453 * and expect the encryption to succeed starting from
454 * some offset.
455 *
456 * If encryption succeeds, check that decryption succeeds
457 * and yields the original record.
458 */
459
460 mbedtls_ssl_context ssl; /* ONLY for debugging */
461
462 mbedtls_ssl_transform t0, t1;
Hanno Becker81e16a32019-03-01 11:21:44 +0000463 unsigned char *buf = NULL;
Hanno Beckerd856c822019-04-29 17:30:59 +0100464 size_t const buflen = 256;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000465 mbedtls_record rec, rec_backup;
466
467 int ret;
Hanno Beckerd856c822019-04-29 17:30:59 +0100468 int mode; /* Mode 1, 2 or 3 as explained above */
469 size_t offset; /* Available space at beginning/end/both */
470 size_t threshold = 96; /* Maximum offset to test against */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000471
Hanno Beckerd856c822019-04-29 17:30:59 +0100472 size_t default_pre_padding = 64; /* Pre-padding to use in mode 2 */
473 size_t default_post_padding = 128; /* Post-padding to use in mode 1 */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000474
475 int seen_success; /* Indicates if in the current mode we've
476 * already seen a successful test. */
477
478 mbedtls_ssl_init( &ssl );
479 mbedtls_ssl_transform_init( &t0 );
480 mbedtls_ssl_transform_init( &t1 );
481 TEST_ASSERT( build_transforms( &t0, &t1, cipher_type, hash_id,
Hanno Beckerd856c822019-04-29 17:30:59 +0100482 etm, tag_mode, ver,
483 (size_t) cid0_len,
484 (size_t) cid1_len ) == 0 );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000485
Hanno Becker3ee54212019-04-04 16:31:26 +0100486 TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000487
488 for( mode=1; mode <= 3; mode++ )
489 {
490 seen_success = 0;
491 for( offset=0; offset <= threshold; offset++ )
492 {
493 mbedtls_ssl_transform *t_dec, *t_enc;
Hanno Becker6c87b3f2019-04-29 17:24:44 +0100494 t_dec = &t0;
495 t_enc = &t1;
Hanno Beckerb3268da2018-01-05 15:20:24 +0000496
497 memset( rec.ctr, offset, sizeof( rec.ctr ) );
498 rec.type = 42;
499 rec.ver[0] = offset;
500 rec.ver[1] = offset;
501 rec.buf = buf;
502 rec.buf_len = buflen;
Hanno Beckera0e20d02019-05-15 14:03:01 +0100503#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd856c822019-04-29 17:30:59 +0100504 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +0100505#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerb3268da2018-01-05 15:20:24 +0000506
507 switch( mode )
508 {
509 case 1: /* Space in the beginning */
510 rec.data_offset = offset;
511 rec.data_len = buflen - offset - default_post_padding;
512 break;
513
514 case 2: /* Space in the end */
515 rec.data_offset = default_pre_padding;
516 rec.data_len = buflen - default_pre_padding - offset;
517 break;
518
519 case 3: /* Space in the beginning and end */
520 rec.data_offset = offset;
521 rec.data_len = buflen - 2 * offset;
522 break;
523
524 default:
525 TEST_ASSERT( 0 );
526 break;
527 }
528
529 memset( rec.buf + rec.data_offset, 42, rec.data_len );
530
531 /* Make a copy for later comparison */
532 rec_backup = rec;
533
534 /* Encrypt record */
535 ret = mbedtls_ssl_encrypt_buf( &ssl, t_enc, &rec, rnd_std_rand, NULL );
536
537 if( ( mode == 1 || mode == 2 ) && seen_success )
538 {
539 TEST_ASSERT( ret == 0 );
540 }
541 else
542 {
543 TEST_ASSERT( ret == 0 || ret == MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
544 if( ret == 0 )
545 seen_success = 1;
546 }
547
548 if( ret != 0 )
549 continue;
550
551 /* Decrypt record with t_dec */
552 TEST_ASSERT( mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec ) == 0 );
553
554 /* Compare results */
555 TEST_ASSERT( rec.type == rec_backup.type );
556 TEST_ASSERT( memcmp( rec.ctr, rec_backup.ctr, 8 ) == 0 );
557 TEST_ASSERT( rec.ver[0] == rec_backup.ver[0] );
558 TEST_ASSERT( rec.ver[1] == rec_backup.ver[1] );
559 TEST_ASSERT( rec.data_len == rec_backup.data_len );
560 TEST_ASSERT( rec.data_offset == rec_backup.data_offset );
561 TEST_ASSERT( memcmp( rec.buf + rec.data_offset,
562 rec_backup.buf + rec_backup.data_offset,
563 rec.data_len ) == 0 );
564 }
565
566 TEST_ASSERT( seen_success == 1 );
567 }
568
Hanno Becker81e16a32019-03-01 11:21:44 +0000569exit:
570
Hanno Beckerb3268da2018-01-05 15:20:24 +0000571 /* Cleanup */
572 mbedtls_ssl_free( &ssl );
573 mbedtls_ssl_transform_free( &t0 );
574 mbedtls_ssl_transform_free( &t1 );
575
Hanno Becker3ee54212019-04-04 16:31:26 +0100576 mbedtls_free( buf );
Hanno Beckerb3268da2018-01-05 15:20:24 +0000577}
578/* END_CASE */
Ron Eldor824ad7b2019-05-13 14:09:00 +0300579
580/* BEGIN_CASE */
581void ssl_tls_prf( int type, data_t * secret, data_t * random,
582 char *label, data_t *result_hex_str, int exp_ret )
583{
584 unsigned char *output;
585
586 output = mbedtls_calloc( 1, result_hex_str->len );
587 if( output == NULL )
588 goto exit;
589
Ron Eldor6b9b1b82019-05-15 17:04:33 +0300590#if defined(MBEDTLS_USE_PSA_CRYPTO)
591 TEST_ASSERT( psa_crypto_init() == 0 );
592#endif
593
Ron Eldor824ad7b2019-05-13 14:09:00 +0300594 TEST_ASSERT( mbedtls_ssl_tls_prf( type, secret->x, secret->len,
595 label, random->x, random->len,
596 output, result_hex_str->len ) == exp_ret );
597
598 if( exp_ret == 0 )
599 {
600 TEST_ASSERT( hexcmp( output, result_hex_str->x,
601 result_hex_str->len, result_hex_str->len ) == 0 );
602 }
603exit:
604
605 mbedtls_free( output );
606}
607/* END_CASE */