mbed TLS Build

Dependents:   Slave-prot-prod

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?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/ecdh.h"
markrad 0:cdf462088d13 3 /* END_HEADER */
markrad 0:cdf462088d13 4
markrad 0:cdf462088d13 5 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 6 * depends_on:MBEDTLS_ECDH_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 ecdh_primitive_random( int id )
markrad 0:cdf462088d13 12 {
markrad 0:cdf462088d13 13 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 14 mbedtls_ecp_point qA, qB;
markrad 0:cdf462088d13 15 mbedtls_mpi dA, dB, zA, zB;
markrad 0:cdf462088d13 16 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 17
markrad 0:cdf462088d13 18 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 19 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
markrad 0:cdf462088d13 20 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
markrad 0:cdf462088d13 21 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB );
markrad 0:cdf462088d13 22 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 23
markrad 0:cdf462088d13 24 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 25
markrad 0:cdf462088d13 26 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA, &rnd_pseudo_rand, &rnd_info )
markrad 0:cdf462088d13 27 == 0 );
markrad 0:cdf462088d13 28 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB, &rnd_pseudo_rand, &rnd_info )
markrad 0:cdf462088d13 29 == 0 );
markrad 0:cdf462088d13 30 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA,
markrad 0:cdf462088d13 31 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 32 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB,
markrad 0:cdf462088d13 33 NULL, NULL ) == 0 );
markrad 0:cdf462088d13 34
markrad 0:cdf462088d13 35 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &zB ) == 0 );
markrad 0:cdf462088d13 36
markrad 0:cdf462088d13 37 exit:
markrad 0:cdf462088d13 38 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 39 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
markrad 0:cdf462088d13 40 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
markrad 0:cdf462088d13 41 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB );
markrad 0:cdf462088d13 42 }
markrad 0:cdf462088d13 43 /* END_CASE */
markrad 0:cdf462088d13 44
markrad 0:cdf462088d13 45 /* BEGIN_CASE */
markrad 0:cdf462088d13 46 void ecdh_primitive_testvec( int id, char *dA_str, char *xA_str, char *yA_str,
markrad 0:cdf462088d13 47 char *dB_str, char *xB_str, char *yB_str,
markrad 0:cdf462088d13 48 char *z_str )
markrad 0:cdf462088d13 49 {
markrad 0:cdf462088d13 50 mbedtls_ecp_group grp;
markrad 0:cdf462088d13 51 mbedtls_ecp_point qA, qB;
markrad 0:cdf462088d13 52 mbedtls_mpi dA, dB, zA, zB, check;
markrad 0:cdf462088d13 53 unsigned char rnd_buf_A[MBEDTLS_ECP_MAX_BYTES];
markrad 0:cdf462088d13 54 unsigned char rnd_buf_B[MBEDTLS_ECP_MAX_BYTES];
markrad 0:cdf462088d13 55 rnd_buf_info rnd_info_A, rnd_info_B;
markrad 0:cdf462088d13 56
markrad 0:cdf462088d13 57 mbedtls_ecp_group_init( &grp );
markrad 0:cdf462088d13 58 mbedtls_ecp_point_init( &qA ); mbedtls_ecp_point_init( &qB );
markrad 0:cdf462088d13 59 mbedtls_mpi_init( &dA ); mbedtls_mpi_init( &dB );
markrad 0:cdf462088d13 60 mbedtls_mpi_init( &zA ); mbedtls_mpi_init( &zB ); mbedtls_mpi_init( &check );
markrad 0:cdf462088d13 61
markrad 0:cdf462088d13 62 TEST_ASSERT( mbedtls_ecp_group_load( &grp, id ) == 0 );
markrad 0:cdf462088d13 63
markrad 0:cdf462088d13 64 rnd_info_A.buf = rnd_buf_A;
markrad 0:cdf462088d13 65 rnd_info_A.length = unhexify( rnd_buf_A, dA_str );
markrad 0:cdf462088d13 66
markrad 0:cdf462088d13 67 /* Fix rnd_buf_A by shifting it left if necessary */
markrad 0:cdf462088d13 68 if( grp.nbits % 8 != 0 )
markrad 0:cdf462088d13 69 {
markrad 0:cdf462088d13 70 unsigned char shift = 8 - ( grp.nbits % 8 );
markrad 0:cdf462088d13 71 size_t i;
markrad 0:cdf462088d13 72
markrad 0:cdf462088d13 73 for( i = 0; i < rnd_info_A.length - 1; i++ )
markrad 0:cdf462088d13 74 rnd_buf_A[i] = rnd_buf_A[i] << shift
markrad 0:cdf462088d13 75 | rnd_buf_A[i+1] >> ( 8 - shift );
markrad 0:cdf462088d13 76
markrad 0:cdf462088d13 77 rnd_buf_A[rnd_info_A.length-1] <<= shift;
markrad 0:cdf462088d13 78 }
markrad 0:cdf462088d13 79
markrad 0:cdf462088d13 80 rnd_info_B.buf = rnd_buf_B;
markrad 0:cdf462088d13 81 rnd_info_B.length = unhexify( rnd_buf_B, dB_str );
markrad 0:cdf462088d13 82
markrad 0:cdf462088d13 83 /* Fix rnd_buf_B by shifting it left if necessary */
markrad 0:cdf462088d13 84 if( grp.nbits % 8 != 0 )
markrad 0:cdf462088d13 85 {
markrad 0:cdf462088d13 86 unsigned char shift = 8 - ( grp.nbits % 8 );
markrad 0:cdf462088d13 87 size_t i;
markrad 0:cdf462088d13 88
markrad 0:cdf462088d13 89 for( i = 0; i < rnd_info_B.length - 1; i++ )
markrad 0:cdf462088d13 90 rnd_buf_B[i] = rnd_buf_B[i] << shift
markrad 0:cdf462088d13 91 | rnd_buf_B[i+1] >> ( 8 - shift );
markrad 0:cdf462088d13 92
markrad 0:cdf462088d13 93 rnd_buf_B[rnd_info_B.length-1] <<= shift;
markrad 0:cdf462088d13 94 }
markrad 0:cdf462088d13 95
markrad 0:cdf462088d13 96 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dA, &qA,
markrad 0:cdf462088d13 97 rnd_buffer_rand, &rnd_info_A ) == 0 );
markrad 0:cdf462088d13 98 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qA ) );
markrad 0:cdf462088d13 99 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xA_str ) == 0 );
markrad 0:cdf462088d13 100 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.X, &check ) == 0 );
markrad 0:cdf462088d13 101 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yA_str ) == 0 );
markrad 0:cdf462088d13 102 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qA.Y, &check ) == 0 );
markrad 0:cdf462088d13 103
markrad 0:cdf462088d13 104 TEST_ASSERT( mbedtls_ecdh_gen_public( &grp, &dB, &qB,
markrad 0:cdf462088d13 105 rnd_buffer_rand, &rnd_info_B ) == 0 );
markrad 0:cdf462088d13 106 TEST_ASSERT( ! mbedtls_ecp_is_zero( &qB ) );
markrad 0:cdf462088d13 107 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, xB_str ) == 0 );
markrad 0:cdf462088d13 108 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.X, &check ) == 0 );
markrad 0:cdf462088d13 109 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, yB_str ) == 0 );
markrad 0:cdf462088d13 110 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &qB.Y, &check ) == 0 );
markrad 0:cdf462088d13 111
markrad 0:cdf462088d13 112 TEST_ASSERT( mbedtls_mpi_read_string( &check, 16, z_str ) == 0 );
markrad 0:cdf462088d13 113 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zA, &qB, &dA, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 114 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zA, &check ) == 0 );
markrad 0:cdf462088d13 115 TEST_ASSERT( mbedtls_ecdh_compute_shared( &grp, &zB, &qA, &dB, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 116 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &zB, &check ) == 0 );
markrad 0:cdf462088d13 117
markrad 0:cdf462088d13 118 exit:
markrad 0:cdf462088d13 119 mbedtls_ecp_group_free( &grp );
markrad 0:cdf462088d13 120 mbedtls_ecp_point_free( &qA ); mbedtls_ecp_point_free( &qB );
markrad 0:cdf462088d13 121 mbedtls_mpi_free( &dA ); mbedtls_mpi_free( &dB );
markrad 0:cdf462088d13 122 mbedtls_mpi_free( &zA ); mbedtls_mpi_free( &zB ); mbedtls_mpi_free( &check );
markrad 0:cdf462088d13 123 }
markrad 0:cdf462088d13 124 /* END_CASE */
markrad 0:cdf462088d13 125
markrad 0:cdf462088d13 126 /* BEGIN_CASE */
markrad 0:cdf462088d13 127 void ecdh_exchange( int id )
markrad 0:cdf462088d13 128 {
markrad 0:cdf462088d13 129 mbedtls_ecdh_context srv, cli;
markrad 0:cdf462088d13 130 unsigned char buf[1000];
markrad 0:cdf462088d13 131 const unsigned char *vbuf;
markrad 0:cdf462088d13 132 size_t len;
markrad 0:cdf462088d13 133 rnd_pseudo_info rnd_info;
markrad 0:cdf462088d13 134
markrad 0:cdf462088d13 135 mbedtls_ecdh_init( &srv );
markrad 0:cdf462088d13 136 mbedtls_ecdh_init( &cli );
markrad 0:cdf462088d13 137 memset( &rnd_info, 0x00, sizeof( rnd_pseudo_info ) );
markrad 0:cdf462088d13 138
markrad 0:cdf462088d13 139 TEST_ASSERT( mbedtls_ecp_group_load( &srv.grp, id ) == 0 );
markrad 0:cdf462088d13 140
markrad 0:cdf462088d13 141 memset( buf, 0x00, sizeof( buf ) ); vbuf = buf;
markrad 0:cdf462088d13 142 TEST_ASSERT( mbedtls_ecdh_make_params( &srv, &len, buf, 1000,
markrad 0:cdf462088d13 143 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 144 TEST_ASSERT( mbedtls_ecdh_read_params( &cli, &vbuf, buf + len ) == 0 );
markrad 0:cdf462088d13 145
markrad 0:cdf462088d13 146 memset( buf, 0x00, sizeof( buf ) );
markrad 0:cdf462088d13 147 TEST_ASSERT( mbedtls_ecdh_make_public( &cli, &len, buf, 1000,
markrad 0:cdf462088d13 148 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 149 TEST_ASSERT( mbedtls_ecdh_read_public( &srv, buf, len ) == 0 );
markrad 0:cdf462088d13 150
markrad 0:cdf462088d13 151 TEST_ASSERT( mbedtls_ecdh_calc_secret( &srv, &len, buf, 1000,
markrad 0:cdf462088d13 152 &rnd_pseudo_rand, &rnd_info ) == 0 );
markrad 0:cdf462088d13 153 TEST_ASSERT( mbedtls_ecdh_calc_secret( &cli, &len, buf, 1000, NULL, NULL ) == 0 );
markrad 0:cdf462088d13 154 TEST_ASSERT( mbedtls_mpi_cmp_mpi( &srv.z, &cli.z ) == 0 );
markrad 0:cdf462088d13 155
markrad 0:cdf462088d13 156 exit:
markrad 0:cdf462088d13 157 mbedtls_ecdh_free( &srv );
markrad 0:cdf462088d13 158 mbedtls_ecdh_free( &cli );
markrad 0:cdf462088d13 159 }
markrad 0:cdf462088d13 160 /* END_CASE */