From 6c2f76e9cdae1b6c361238047ec909b35f3d8b8a Mon Sep 17 00:00:00 2001 From: Jarno Lamsa Date: Mon, 26 Aug 2019 13:34:45 +0300 Subject: [PATCH] Add a unit test for ECDSA Add a basic unit test for the ECDSA part of the tinycrypt. It generates keys, signs and verifies. Modified from tinycrypt tests found in tinycrypt-repository. --- tests/suites/test_suite_tinycrypt.data | 3 +++ tests/suites/test_suite_tinycrypt.function | 24 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/suites/test_suite_tinycrypt.data b/tests/suites/test_suite_tinycrypt.data index 85d599276..d76550efe 100644 --- a/tests/suites/test_suite_tinycrypt.data +++ b/tests/suites/test_suite_tinycrypt.data @@ -1,2 +1,5 @@ Tinycrypt ECDH test_ecdh: + +Tinycrypt ECDSA +test_ecdsa: diff --git a/tests/suites/test_suite_tinycrypt.function b/tests/suites/test_suite_tinycrypt.function index eb347ea49..698058f01 100644 --- a/tests/suites/test_suite_tinycrypt.function +++ b/tests/suites/test_suite_tinycrypt.function @@ -33,3 +33,27 @@ void test_ecdh() TEST_ASSERT( memcmp( secret1, secret2, sizeof( secret1 ) ) == 0 ); } /* END_CASE */ + +/* BEGIN_CASE depends_on:MBEDTLS_USE_TINYCRYPT */ +void test_ecdsa() +{ + uint8_t private[NUM_ECC_BYTES] = {0}; + uint8_t public[2*NUM_ECC_BYTES] = {0}; + uint8_t hash[NUM_ECC_BYTES] = {0}; + uint8_t sig[2*NUM_ECC_BYTES] = {0}; + unsigned int hash_words[NUM_ECC_WORDS] = {0}; + + const struct uECC_Curve_t * curve = uECC_secp256r1(); + + uECC_generate_random_int( hash_words, curve->n, + BITS_TO_WORDS( curve->num_n_bits ) ); + + uECC_vli_nativeToBytes( hash, NUM_ECC_BYTES, hash_words ); + + TEST_ASSERT( uECC_make_key( public, private, curve ) != 0 ); + + TEST_ASSERT( uECC_sign( private, hash, sizeof( hash ), sig, curve ) != 0 ); + + TEST_ASSERT( uECC_verify( public, hash, sizeof( hash ), sig, curve ) != 0 ); +} +/* END_CASE */ \ No newline at end of file