mbed TLS Build
tests/suites/test_suite_dhm.function@1:1a219dea6cb5, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 16:03:38 2019 +0000
- Revision:
- 1:1a219dea6cb5
- Parent:
- 0:cdf462088d13
compartir a Pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markrad | 0:cdf462088d13 | 1 | /* BEGIN_HEADER */ |
markrad | 0:cdf462088d13 | 2 | #include "mbedtls/dhm.h" |
markrad | 0:cdf462088d13 | 3 | /* END_HEADER */ |
markrad | 0:cdf462088d13 | 4 | |
markrad | 0:cdf462088d13 | 5 | /* BEGIN_DEPENDENCIES |
markrad | 0:cdf462088d13 | 6 | * depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C |
markrad | 0:cdf462088d13 | 7 | * END_DEPENDENCIES |
markrad | 0:cdf462088d13 | 8 | */ |
markrad | 0:cdf462088d13 | 9 | |
markrad | 0:cdf462088d13 | 10 | /* BEGIN_CASE */ |
markrad | 0:cdf462088d13 | 11 | void dhm_do_dhm( int radix_P, char *input_P, |
markrad | 0:cdf462088d13 | 12 | int radix_G, char *input_G ) |
markrad | 0:cdf462088d13 | 13 | { |
markrad | 0:cdf462088d13 | 14 | mbedtls_dhm_context ctx_srv; |
markrad | 0:cdf462088d13 | 15 | mbedtls_dhm_context ctx_cli; |
markrad | 0:cdf462088d13 | 16 | unsigned char ske[1000]; |
markrad | 0:cdf462088d13 | 17 | unsigned char *p = ske; |
markrad | 0:cdf462088d13 | 18 | unsigned char pub_cli[1000]; |
markrad | 0:cdf462088d13 | 19 | unsigned char sec_srv[1000]; |
markrad | 0:cdf462088d13 | 20 | unsigned char sec_cli[1000]; |
markrad | 0:cdf462088d13 | 21 | size_t ske_len = 0; |
markrad | 0:cdf462088d13 | 22 | size_t pub_cli_len = 0; |
markrad | 0:cdf462088d13 | 23 | size_t sec_srv_len; |
markrad | 0:cdf462088d13 | 24 | size_t sec_cli_len; |
markrad | 0:cdf462088d13 | 25 | int x_size, i; |
markrad | 0:cdf462088d13 | 26 | rnd_pseudo_info rnd_info; |
markrad | 0:cdf462088d13 | 27 | |
markrad | 0:cdf462088d13 | 28 | mbedtls_dhm_init( &ctx_srv ); |
markrad | 0:cdf462088d13 | 29 | mbedtls_dhm_init( &ctx_cli ); |
markrad | 0:cdf462088d13 | 30 | memset( ske, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 31 | memset( pub_cli, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 32 | memset( sec_srv, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 33 | memset( sec_cli, 0x00, 1000 ); |
markrad | 0:cdf462088d13 | 34 | memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) ); |
markrad | 0:cdf462088d13 | 35 | |
markrad | 0:cdf462088d13 | 36 | /* |
markrad | 0:cdf462088d13 | 37 | * Set params |
markrad | 0:cdf462088d13 | 38 | */ |
markrad | 0:cdf462088d13 | 39 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.P, radix_P, input_P ) == 0 ); |
markrad | 0:cdf462088d13 | 40 | TEST_ASSERT( mbedtls_mpi_read_string( &ctx_srv.G, radix_G, input_G ) == 0 ); |
markrad | 0:cdf462088d13 | 41 | x_size = mbedtls_mpi_size( &ctx_srv.P ); |
markrad | 0:cdf462088d13 | 42 | pub_cli_len = x_size; |
markrad | 0:cdf462088d13 | 43 | |
markrad | 0:cdf462088d13 | 44 | /* |
markrad | 0:cdf462088d13 | 45 | * First key exchange |
markrad | 0:cdf462088d13 | 46 | */ |
markrad | 0:cdf462088d13 | 47 | TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 48 | ske[ske_len++] = 0; |
markrad | 0:cdf462088d13 | 49 | ske[ske_len++] = 0; |
markrad | 0:cdf462088d13 | 50 | TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 ); |
markrad | 0:cdf462088d13 | 51 | |
markrad | 0:cdf462088d13 | 52 | TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 53 | TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 ); |
markrad | 0:cdf462088d13 | 54 | |
markrad | 0:cdf462088d13 | 55 | TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 56 | TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 ); |
markrad | 0:cdf462088d13 | 57 | |
markrad | 0:cdf462088d13 | 58 | TEST_ASSERT( sec_srv_len == sec_cli_len ); |
markrad | 0:cdf462088d13 | 59 | TEST_ASSERT( sec_srv_len != 0 ); |
markrad | 0:cdf462088d13 | 60 | TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 ); |
markrad | 0:cdf462088d13 | 61 | |
markrad | 0:cdf462088d13 | 62 | /* Re-do calc_secret on server a few times to test update of blinding values */ |
markrad | 0:cdf462088d13 | 63 | for( i = 0; i < 3; i++ ) |
markrad | 0:cdf462088d13 | 64 | { |
markrad | 0:cdf462088d13 | 65 | sec_srv_len = 1000; |
markrad | 0:cdf462088d13 | 66 | TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 67 | |
markrad | 0:cdf462088d13 | 68 | TEST_ASSERT( sec_srv_len == sec_cli_len ); |
markrad | 0:cdf462088d13 | 69 | TEST_ASSERT( sec_srv_len != 0 ); |
markrad | 0:cdf462088d13 | 70 | TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 ); |
markrad | 0:cdf462088d13 | 71 | } |
markrad | 0:cdf462088d13 | 72 | |
markrad | 0:cdf462088d13 | 73 | /* |
markrad | 0:cdf462088d13 | 74 | * Second key exchange to test change of blinding values on server |
markrad | 0:cdf462088d13 | 75 | */ |
markrad | 0:cdf462088d13 | 76 | p = ske; |
markrad | 0:cdf462088d13 | 77 | |
markrad | 0:cdf462088d13 | 78 | TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 79 | ske[ske_len++] = 0; |
markrad | 0:cdf462088d13 | 80 | ske[ske_len++] = 0; |
markrad | 0:cdf462088d13 | 81 | TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 ); |
markrad | 0:cdf462088d13 | 82 | |
markrad | 0:cdf462088d13 | 83 | TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 84 | TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 ); |
markrad | 0:cdf462088d13 | 85 | |
markrad | 0:cdf462088d13 | 86 | TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ), &sec_srv_len, &rnd_pseudo_rand, &rnd_info ) == 0 ); |
markrad | 0:cdf462088d13 | 87 | TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 ); |
markrad | 0:cdf462088d13 | 88 | |
markrad | 0:cdf462088d13 | 89 | TEST_ASSERT( sec_srv_len == sec_cli_len ); |
markrad | 0:cdf462088d13 | 90 | TEST_ASSERT( sec_srv_len != 0 ); |
markrad | 0:cdf462088d13 | 91 | TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 ); |
markrad | 0:cdf462088d13 | 92 | |
markrad | 0:cdf462088d13 | 93 | exit: |
markrad | 0:cdf462088d13 | 94 | mbedtls_dhm_free( &ctx_srv ); |
markrad | 0:cdf462088d13 | 95 | mbedtls_dhm_free( &ctx_cli ); |
markrad | 0:cdf462088d13 | 96 | } |
markrad | 0:cdf462088d13 | 97 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 98 | |
markrad | 0:cdf462088d13 | 99 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ |
markrad | 0:cdf462088d13 | 100 | void dhm_file( char *filename, char *p, char *g, int len ) |
markrad | 0:cdf462088d13 | 101 | { |
markrad | 0:cdf462088d13 | 102 | mbedtls_dhm_context ctx; |
markrad | 0:cdf462088d13 | 103 | mbedtls_mpi P, G; |
markrad | 0:cdf462088d13 | 104 | |
markrad | 0:cdf462088d13 | 105 | mbedtls_dhm_init( &ctx ); |
markrad | 0:cdf462088d13 | 106 | mbedtls_mpi_init( &P ); mbedtls_mpi_init( &G ); |
markrad | 0:cdf462088d13 | 107 | |
markrad | 0:cdf462088d13 | 108 | TEST_ASSERT( mbedtls_mpi_read_string( &P, 16, p ) == 0 ); |
markrad | 0:cdf462088d13 | 109 | TEST_ASSERT( mbedtls_mpi_read_string( &G, 16, g ) == 0 ); |
markrad | 0:cdf462088d13 | 110 | |
markrad | 0:cdf462088d13 | 111 | TEST_ASSERT( mbedtls_dhm_parse_dhmfile( &ctx, filename ) == 0 ); |
markrad | 0:cdf462088d13 | 112 | |
markrad | 0:cdf462088d13 | 113 | TEST_ASSERT( ctx.len == (size_t) len ); |
markrad | 0:cdf462088d13 | 114 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &P ) == 0 ); |
markrad | 0:cdf462088d13 | 115 | TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.G, &G ) == 0 ); |
markrad | 0:cdf462088d13 | 116 | |
markrad | 0:cdf462088d13 | 117 | exit: |
markrad | 0:cdf462088d13 | 118 | mbedtls_mpi_free( &P ); mbedtls_mpi_free( &G ); |
markrad | 0:cdf462088d13 | 119 | mbedtls_dhm_free( &ctx ); |
markrad | 0:cdf462088d13 | 120 | } |
markrad | 0:cdf462088d13 | 121 | /* END_CASE */ |
markrad | 0:cdf462088d13 | 122 | |
markrad | 0:cdf462088d13 | 123 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
markrad | 0:cdf462088d13 | 124 | void dhm_selftest() |
markrad | 0:cdf462088d13 | 125 | { |
markrad | 0:cdf462088d13 | 126 | TEST_ASSERT( mbedtls_dhm_self_test( 1 ) == 0 ); |
markrad | 0:cdf462088d13 | 127 | } |
markrad | 0:cdf462088d13 | 128 | /* END_CASE */ |