Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
| 2 | |
| 3 | #include "tinycrypt/ecc.h" |
| 4 | #include "tinycrypt/ecc_dh.h" |
| 5 | #include "tinycrypt/ecc_dsa.h" |
| 6 | |
| 7 | /* END_HEADER */ |
| 8 | |
| 9 | /* BEGIN_DEPENDENCIES |
| 10 | * depends_on:MBEDTLS_USE_TINYCRYPT |
| 11 | * END_DEPENDENCIES |
| 12 | */ |
| 13 | |
| 14 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 15 | void test_ecdh() |
| 16 | { |
| 17 | uint8_t private1[NUM_ECC_BYTES] = {0}; |
| 18 | uint8_t private2[NUM_ECC_BYTES] = {0}; |
| 19 | uint8_t public1[2*NUM_ECC_BYTES] = {0}; |
| 20 | uint8_t public2[2*NUM_ECC_BYTES] = {0}; |
| 21 | uint8_t secret1[NUM_ECC_BYTES] = {0}; |
| 22 | uint8_t secret2[NUM_ECC_BYTES] = {0}; |
| 23 | |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame] | 24 | uECC_set_rng( &uecc_rng_wrapper ); |
| 25 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 26 | TEST_ASSERT( uECC_make_key( public1, private1 ) == UECC_SUCCESS ); |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 28 | TEST_ASSERT( uECC_make_key( public2, private2 ) == UECC_SUCCESS ); |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 30 | TEST_ASSERT( uECC_shared_secret( public2, private1, secret1 ) == UECC_SUCCESS ); |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 32 | TEST_ASSERT( uECC_shared_secret( public1, private2, secret2 ) == UECC_SUCCESS ); |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 33 | |
| 34 | TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 ); |
| 35 | } |
| 36 | /* END_CASE */ |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 37 | |
| 38 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 39 | void test_ecdsa() |
| 40 | { |
| 41 | uint8_t private[NUM_ECC_BYTES] = {0}; |
| 42 | uint8_t public[2*NUM_ECC_BYTES] = {0}; |
| 43 | uint8_t hash[NUM_ECC_BYTES] = {0}; |
| 44 | uint8_t sig[2*NUM_ECC_BYTES] = {0}; |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 45 | |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame] | 46 | uECC_set_rng( &uecc_rng_wrapper ); |
| 47 | |
Jarno Lamsa | f35f35b | 2019-09-02 15:36:49 +0300 | [diff] [blame] | 48 | TEST_ASSERT( rnd_std_rand( NULL, hash, NUM_ECC_BYTES ) == 0 ); |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 49 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 50 | TEST_ASSERT( uECC_make_key( public, private ) == UECC_SUCCESS ); |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 51 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 52 | TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig ) == UECC_SUCCESS ); |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 53 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 54 | TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig ) == UECC_SUCCESS ); |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 55 | } |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 56 | /* END_CASE */ |
| 57 | |
| 58 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 59 | void ecdh_primitive_testvec( data_t * private1, data_t * xA_str, |
| 60 | data_t * yA_str, data_t * private2, |
| 61 | data_t * xB_str, data_t * yB_str, data_t * z_str ) |
| 62 | { |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 63 | uint8_t public1[2*NUM_ECC_BYTES] = {0}; |
| 64 | uint8_t public2[2*NUM_ECC_BYTES] = {0}; |
| 65 | uint8_t secret1[NUM_ECC_BYTES] = {0}; |
| 66 | uint8_t secret2[NUM_ECC_BYTES] = {0}; |
| 67 | |
| 68 | memcpy( public1, xA_str->x, xA_str->len ); |
| 69 | memcpy( public1 + NUM_ECC_BYTES, yA_str->x, yA_str->len ); |
| 70 | memcpy( public2, xB_str->x, xB_str->len ); |
| 71 | memcpy( public2 + NUM_ECC_BYTES, yB_str->x, yB_str->len ); |
| 72 | |
| 73 | // Compute shared secrets and compare to test vector secret |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 74 | TEST_ASSERT( uECC_shared_secret( public2, private1->x, secret1 ) == UECC_SUCCESS ); |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 76 | TEST_ASSERT( uECC_shared_secret( public1, private2->x, secret2 ) == UECC_SUCCESS ); |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 77 | |
| 78 | TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 ); |
| 79 | TEST_ASSERT( memcmp( secret1, z_str->x, sizeof( secret1 ) ) == 0 ); |
| 80 | TEST_ASSERT( memcmp( secret2, z_str->x, sizeof( secret2 ) ) == 0 ); |
| 81 | } |
| 82 | /* END_CASE */ |
| 83 | |
| 84 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 85 | void ecdsa_primitive_testvec( data_t * xQ_str, data_t * yQ_str, |
Manuel Pégourié-Gonnard | 10d8e8e | 2019-11-06 10:30:26 +0100 | [diff] [blame] | 86 | data_t * hash, data_t * r_str, data_t * s_str ) |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 87 | { |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 88 | uint8_t pub_bytes[2*NUM_ECC_BYTES] = {0}; |
| 89 | uint8_t sig_bytes[2*NUM_ECC_BYTES] = {0}; |
| 90 | |
| 91 | memcpy( pub_bytes, xQ_str->x, xQ_str->len ); |
| 92 | memcpy( pub_bytes + NUM_ECC_BYTES, yQ_str->x, yQ_str->len ); |
| 93 | memcpy( sig_bytes, r_str->x, r_str->len ); |
| 94 | memcpy( sig_bytes + NUM_ECC_BYTES, s_str->x, r_str->len ); |
| 95 | |
| 96 | TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 97 | sig_bytes ) == UECC_SUCCESS ); |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 98 | |
| 99 | // Alter the signature and check the verification fails |
| 100 | for( int i = 0; i < 2*NUM_ECC_BYTES; i++ ) |
| 101 | { |
| 102 | uint8_t temp = sig_bytes[i]; |
| 103 | sig_bytes[i] = ( sig_bytes[i] + 1 ) % 256; |
| 104 | TEST_ASSERT( uECC_verify( pub_bytes, hash->x, hash->len, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 105 | sig_bytes ) == UECC_FAILURE ); |
Jarno Lamsa | a7e0f63 | 2019-09-02 09:47:37 +0300 | [diff] [blame] | 106 | sig_bytes[i] = temp; |
| 107 | } |
| 108 | |
| 109 | } |
| 110 | /* END_CASE */ |