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 | |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame^] | 7 | static int uecc_rng_wrapper( uint8_t *dest, unsigned int size ) |
| 8 | { |
| 9 | int ret; |
| 10 | ret = rnd_std_rand( NULL, dest, size ); |
| 11 | if( ret == 0 ) |
| 12 | return( (int) size ); |
| 13 | |
| 14 | return( 0 ); |
| 15 | } |
| 16 | |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 17 | /* END_HEADER */ |
| 18 | |
| 19 | /* BEGIN_DEPENDENCIES |
| 20 | * depends_on:MBEDTLS_USE_TINYCRYPT |
| 21 | * END_DEPENDENCIES |
| 22 | */ |
| 23 | |
| 24 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 25 | void test_ecdh() |
| 26 | { |
| 27 | uint8_t private1[NUM_ECC_BYTES] = {0}; |
| 28 | uint8_t private2[NUM_ECC_BYTES] = {0}; |
| 29 | uint8_t public1[2*NUM_ECC_BYTES] = {0}; |
| 30 | uint8_t public2[2*NUM_ECC_BYTES] = {0}; |
| 31 | uint8_t secret1[NUM_ECC_BYTES] = {0}; |
| 32 | uint8_t secret2[NUM_ECC_BYTES] = {0}; |
| 33 | |
| 34 | const struct uECC_Curve_t * curve = uECC_secp256r1(); |
| 35 | |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame^] | 36 | uECC_set_rng( &uecc_rng_wrapper ); |
| 37 | |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 38 | TEST_ASSERT( uECC_make_key( public1, private1, curve ) != 0 ); |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame^] | 39 | |
Jarno Lamsa | 7c5dc6b | 2019-08-26 13:12:35 +0300 | [diff] [blame] | 40 | TEST_ASSERT( uECC_make_key( public2, private2, curve ) != 0 ); |
| 41 | |
| 42 | TEST_ASSERT( uECC_shared_secret( public2, private1, secret1, curve ) != 0 ); |
| 43 | |
| 44 | TEST_ASSERT( uECC_shared_secret( public1, private2, secret2, curve ) != 0 ); |
| 45 | |
| 46 | TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 ); |
| 47 | } |
| 48 | /* END_CASE */ |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 49 | |
| 50 | /* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ |
| 51 | void test_ecdsa() |
| 52 | { |
| 53 | uint8_t private[NUM_ECC_BYTES] = {0}; |
| 54 | uint8_t public[2*NUM_ECC_BYTES] = {0}; |
| 55 | uint8_t hash[NUM_ECC_BYTES] = {0}; |
| 56 | uint8_t sig[2*NUM_ECC_BYTES] = {0}; |
| 57 | unsigned int hash_words[NUM_ECC_WORDS] = {0}; |
| 58 | |
| 59 | const struct uECC_Curve_t * curve = uECC_secp256r1(); |
| 60 | |
Jarno Lamsa | 34fcbfe | 2019-08-26 14:37:33 +0300 | [diff] [blame^] | 61 | uECC_set_rng( &uecc_rng_wrapper ); |
| 62 | |
Jarno Lamsa | 6c2f76e | 2019-08-26 13:34:45 +0300 | [diff] [blame] | 63 | uECC_generate_random_int( hash_words, curve->n, |
| 64 | BITS_TO_WORDS( curve->num_n_bits ) ); |
| 65 | |
| 66 | uECC_vli_nativeToBytes( hash, NUM_ECC_BYTES, hash_words ); |
| 67 | |
| 68 | TEST_ASSERT( uECC_make_key( public, private, curve ) != 0 ); |
| 69 | |
| 70 | TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig, curve ) != 0 ); |
| 71 | |
| 72 | TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig, curve ) != 0 ); |
| 73 | } |
| 74 | /* END_CASE */ |