Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / mbedtls / repo / tests / suites / test_suite_ssl.function
1 /* BEGIN_HEADER */
2 #include <mbedtls/ssl.h>
3 #include <mbedtls/ssl_internal.h>
4
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 ) )                            \
15         {                                       \
16             ret = -1;                           \
17             goto cleanup;                       \
18         }                                       \
19     } while( 0 )
20
21 #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
26
27 static int build_transforms( mbedtls_ssl_transform *t_in,
28                              mbedtls_ssl_transform *t_out,
29                              int cipher_type, int hash_id,
30                              int etm, int tag_mode, int ver,
31                              size_t cid0_len,
32                              size_t cid1_len )
33 {
34     mbedtls_cipher_info_t const *cipher_info;
35     int ret = 0;
36
37     size_t keylen, maclen, ivlen;
38     unsigned char *key0 = NULL, *key1 = NULL;
39     unsigned char iv_enc[16], iv_dec[16];
40
41 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
42     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 ) );
47 #else
48     ((void) cid0_len);
49     ((void) cid1_len);
50 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
51
52     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;
62     /* 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 );
67     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 );
98
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 );
113         CHK( ( md0 = mbedtls_calloc( 1, maclen ) ) != NULL );
114         CHK( ( md1 = mbedtls_calloc( 1, maclen ) ) != NULL );
115         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
144         mbedtls_free( md0 );
145         mbedtls_free( md1 );
146     }
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     defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
164     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
250 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
251     /* 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;
260 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
261
262 cleanup:
263
264     mbedtls_free( key0 );
265     mbedtls_free( key1 );
266
267     return( ret );
268 }
269
270 /* END_HEADER */
271
272 /* BEGIN_DEPENDENCIES
273  * depends_on:MBEDTLS_SSL_TLS_C
274  * END_DEPENDENCIES
275  */
276
277 /* BEGIN_CASE depends_on:MBEDTLS_SSL_DTLS_ANTI_REPLAY */
278 void ssl_dtls_replay( data_t * prevs, data_t * new, int ret )
279 {
280     uint32_t len = 0;
281     mbedtls_ssl_context ssl;
282     mbedtls_ssl_config conf;
283
284     mbedtls_ssl_init( &ssl );
285     mbedtls_ssl_config_init( &conf );
286
287     TEST_ASSERT( mbedtls_ssl_config_defaults( &conf,
288                  MBEDTLS_SSL_IS_CLIENT,
289                  MBEDTLS_SSL_TRANSPORT_DATAGRAM,
290                  MBEDTLS_SSL_PRESET_DEFAULT ) == 0 );
291     TEST_ASSERT( mbedtls_ssl_setup( &ssl, &conf ) == 0 );
292
293     /* Read previous record numbers */
294     for( len = 0; len < prevs->len; len += 6 )
295     {
296         memcpy( ssl.in_ctr + 2, prevs->x + len, 6 );
297         mbedtls_ssl_dtls_replay_update( &ssl );
298     }
299
300     /* Check new number */
301     memcpy( ssl.in_ctr + 2, new->x, 6 );
302     TEST_ASSERT( mbedtls_ssl_dtls_replay_check( &ssl ) == ret );
303
304     mbedtls_ssl_free( &ssl );
305     mbedtls_ssl_config_free( &conf );
306 }
307 /* END_CASE */
308
309 /* BEGIN_CASE depends_on:MBEDTLS_X509_CRT_PARSE_C */
310 void 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 }
320 /* END_CASE */
321
322 /* BEGIN_CASE */
323 void ssl_crypt_record( int cipher_type, int hash_id,
324                        int etm, int tag_mode, int ver,
325                        int cid0_len, int cid1_len )
326 {
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;
338     unsigned char *buf = NULL;
339     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,
346                                    etm, tag_mode, ver,
347                                    (size_t) cid0_len,
348                                    (size_t) cid1_len ) == 0 );
349
350     TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
351
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;
383 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
384         rec.cid_len = 0;
385 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
386
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 */
409         ret = mbedtls_ssl_decrypt_buf( &ssl, t_dec, &rec );
410         TEST_ASSERT( ret == 0 );
411
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
424 exit:
425
426     /* Cleanup */
427     mbedtls_ssl_free( &ssl );
428     mbedtls_ssl_transform_free( &t0 );
429     mbedtls_ssl_transform_free( &t1 );
430
431     mbedtls_free( buf );
432 }
433 /* END_CASE */
434
435 /* BEGIN_CASE */
436 void ssl_crypt_record_small( int cipher_type, int hash_id,
437                              int etm, int tag_mode, int ver,
438                              int cid0_len, int cid1_len )
439 {
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;
463     unsigned char *buf = NULL;
464     size_t const buflen = 256;
465     mbedtls_record rec, rec_backup;
466
467     int ret;
468     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        */
471
472     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 */
474
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,
482                                    etm, tag_mode, ver,
483                                    (size_t) cid0_len,
484                                    (size_t) cid1_len ) == 0 );
485
486     TEST_ASSERT( ( buf = mbedtls_calloc( 1, buflen ) ) != NULL );
487
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;
494             t_dec = &t0;
495             t_enc = &t1;
496
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;
503 #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
504             rec.cid_len = 0;
505 #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
506
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
569 exit:
570
571     /* Cleanup */
572     mbedtls_ssl_free( &ssl );
573     mbedtls_ssl_transform_free( &t0 );
574     mbedtls_ssl_transform_free( &t1 );
575
576     mbedtls_free( buf );
577 }
578 /* END_CASE */
579
580 /* BEGIN_CASE */
581 void 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
590 #if defined(MBEDTLS_USE_PSA_CRYPTO)
591     TEST_ASSERT( psa_crypto_init() == 0 );
592 #endif
593
594     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     }
603 exit:
604
605     mbedtls_free( output );
606 }
607 /* END_CASE */