From d84dbe559ff84d1daba03b37c3ef0df05dc6fb55 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Wed, 21 Aug 2019 14:31:52 +0100 Subject: [PATCH] TinyCrypt Test: Adapt pub/prv ECC key parsing tests to TinyCrypt --- tests/suites/test_suite_pkparse.data | 102 +++++++++++++++-------- tests/suites/test_suite_pkparse.function | 43 +++++++++- 2 files changed, 106 insertions(+), 39 deletions(-) diff --git a/tests/suites/test_suite_pkparse.data b/tests/suites/test_suite_pkparse.data index 4add252df..335d76413 100644 --- a/tests/suites/test_suite_pkparse.data +++ b/tests/suites/test_suite_pkparse.data @@ -953,123 +953,151 @@ Parse Public RSA Key #4 (PKCS#1 wrapped, DER) pk_parse_public_keyfile_rsa:"data_files/rsa_pkcs1_2048_public.der":0 Parse Public EC Key #1 (RFC 5480, DER) -depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_pub.der":0 Parse Public EC Key #2 (RFC 5480, PEM) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_pub.pem":0 Parse Public EC Key #3 (RFC 5480, secp224r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP224R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP224R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_224_pub.pem":0 -Parse Public EC Key #4 (RFC 5480, secp256r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse Public EC Key #4 (RFC 5480, secp256r1, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT +pk_parse_public_keyfile_ec:"data_files/ec_256_pub.pem":0 + +Parse Public EC Key #4 (RFC 5480, secp256r1, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_256_pub.pem":0 Parse Public EC Key #5 (RFC 5480, secp384r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_384_pub.pem":0 Parse Public EC Key #6 (RFC 5480, secp521r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP521R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP521R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_521_pub.pem":0 Parse Public EC Key #7 (RFC 5480, brainpoolP256r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP256R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_bp256_pub.pem":0 Parse Public EC Key #8 (RFC 5480, brainpoolP384r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP384R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP384R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_bp384_pub.pem":0 Parse Public EC Key #9 (RFC 5480, brainpoolP512r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_public_keyfile_ec:"data_files/ec_bp512_pub.pem":0 Parse EC Key #1 (SEC1 DER) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.sec1.der":"NULL":0 Parse EC Key #2 (SEC1 PEM) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.sec1.pem":"NULL":0 Parse EC Key #3 (SEC1 PEM encrypted) -depends_on:MBEDTLS_DES_C:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_MD5_C +depends_on:MBEDTLS_DES_C:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:MBEDTLS_CIPHER_MODE_CBC:MBEDTLS_MD5_C:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.sec1.pw.pem":"polar":0 Parse EC Key #4 (PKCS8 DER) -depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8.der":"NULL":0 -Parse EC Key #4a (PKCS8 DER, no public key) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #4a (PKCS8 DER, no public key, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopub.der":"NULL":0 -Parse EC Key #4b (PKCS8 DER, no public key, with parameters) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #4a (PKCS8 DER, no public key, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopub.der":"NULL":0 + +Parse EC Key #4b (PKCS8 DER, no public key, with parameters, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopubparam.der":"NULL":0 + +Parse EC Key #4b (PKCS8 DER, no public key, with parameters, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopubparam.der":"NULL":0 Parse EC Key #4c (PKCS8 DER, with parameters) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8param.der":"NULL":0 Parse EC Key #5 (PKCS8 PEM) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8.pem":"NULL":0 -Parse EC Key #5a (PKCS8 PEM, no public key) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #5a (PKCS8 PEM, no public key, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopub.pem":"NULL":0 -Parse EC Key #5b (PKCS8 PEM, no public key, with parameters) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #5a (PKCS8 PEM, no public key, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopub.pem":"NULL":0 + +Parse EC Key #5b (PKCS8 PEM, no public key, with parameters, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopubparam.pem":"NULL":0 -Parse EC Key #5c (PKCS8 PEM, with parameters) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #5b (PKCS8 PEM, no public key, with parameters, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_prv.pk8nopubparam.pem":"NULL":0 + +Parse EC Key #5c (PKCS8 PEM, with parameters, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_prv.pk8param.pem":"NULL":0 + +Parse EC Key #5c (PKCS8 PEM, with parameters, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8param.pem":"NULL":0 Parse EC Key #6 (PKCS8 encrypted DER) -depends_on:MBEDTLS_ARC4_C:MBEDTLS_SHA1_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_ARC4_C:MBEDTLS_SHA1_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8.pw.der":"polar":0 Parse EC Key #7 (PKCS8 encrypted PEM) -depends_on:MBEDTLS_ARC4_C:MBEDTLS_SHA1_C:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED +depends_on:MBEDTLS_ARC4_C:MBEDTLS_SHA1_C:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP192R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.pk8.pw.pem":"polar":0 Parse EC Key #8 (SEC1 PEM, secp224r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP224R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP224R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_224_prv.pem":"NULL":0 -Parse EC Key #9 (SEC1 PEM, secp256r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED +Parse EC Key #9 (SEC1 PEM, secp256r1, legacy ECC) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT +pk_parse_keyfile_ec:"data_files/ec_256_prv.pem":"NULL":0 + +Parse EC Key #9 (SEC1 PEM, secp256r1, TinyCrypt) +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_256_prv.pem":"NULL":0 Parse EC Key #10 (SEC1 PEM, secp384r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP384R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_384_prv.pem":"NULL":0 Parse EC Key #11 (SEC1 PEM, secp521r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP521R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP521R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_521_prv.pem":"NULL":0 Parse EC Key #12 (SEC1 PEM, bp256r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP256R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP256R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_bp256_prv.pem":"NULL":0 Parse EC Key #13 (SEC1 PEM, bp384r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP384R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP384R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_bp384_prv.pem":"NULL":0 Parse EC Key #14 (SEC1 PEM, bp512r1) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_BP512R1_ENABLED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_bp512_prv.pem":"NULL":0 Parse EC Key #15 (SEC1 DER, secp256k1, SpecifiedECDomain) -depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256K1_ENABLED:MBEDTLS_PK_PARSE_EC_EXTENDED +depends_on:MBEDTLS_PEM_PARSE_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256K1_ENABLED:MBEDTLS_PK_PARSE_EC_EXTENDED:!MBEDTLS_USE_TINYCRYPT pk_parse_keyfile_ec:"data_files/ec_prv.specdom.der":"NULL":0 Key ASN1 (Incorrect first tag) diff --git a/tests/suites/test_suite_pkparse.function b/tests/suites/test_suite_pkparse.function index 3eb0397e6..a4d9466c4 100644 --- a/tests/suites/test_suite_pkparse.function +++ b/tests/suites/test_suite_pkparse.function @@ -63,9 +63,13 @@ exit: } /* END_CASE */ -/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */ +/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ void pk_parse_public_keyfile_ec( char * key_file, int result ) { +#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT) + ((void) key_file); + ((void) result); +#else mbedtls_pk_context ctx; int res; @@ -77,20 +81,37 @@ void pk_parse_public_keyfile_ec( char * key_file, int result ) if( res == 0 ) { +#if !defined(MBEDTLS_USE_TINYCRYPT) mbedtls_ecp_keypair *eckey; +#else + mbedtls_uecc_keypair *uecckey; +#endif TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) ); + +#if !defined(MBEDTLS_USE_TINYCRYPT) eckey = mbedtls_pk_ec( ctx ); TEST_ASSERT( mbedtls_ecp_check_pubkey( &eckey->grp, &eckey->Q ) == 0 ); +#else + uecckey = mbedtls_pk_uecc( ctx ); + TEST_ASSERT( uECC_valid_public_key( uecckey->public_key, + uECC_secp256r1() ) == 0 ); +#endif /* MBEDTLS_USE_TINYCRYPT */ } exit: mbedtls_pk_free( &ctx ); +#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */ } /* END_CASE */ -/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */ +/* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ void pk_parse_keyfile_ec( char * key_file, char * password, int result ) { +#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_USE_TINYCRYPT) + ((void) key_file); + ((void) password); + ((void) result); +#else mbedtls_pk_context ctx; int res; @@ -102,14 +123,32 @@ void pk_parse_keyfile_ec( char * key_file, char * password, int result ) if( res == 0 ) { +#if !defined(MBEDTLS_USE_TINYCRYPT) mbedtls_ecp_keypair *eckey; +#else + mbedtls_uecc_keypair *uecckey; + unsigned char tmp_pubkey[ 2 * NUM_ECC_BYTES ]; +#endif TEST_ASSERT( mbedtls_pk_can_do( &ctx, MBEDTLS_PK_ECKEY ) ); + +#if !defined(MBEDTLS_USE_TINYCRYPT) eckey = mbedtls_pk_ec( ctx ); TEST_ASSERT( mbedtls_ecp_check_privkey( &eckey->grp, &eckey->d ) == 0 ); +#else + uecckey = mbedtls_pk_uecc( ctx ); + TEST_ASSERT( uECC_valid_public_key( uecckey->public_key, + uECC_secp256r1() ) == 0 ); + TEST_ASSERT( uECC_compute_public_key( uecckey->private_key, + tmp_pubkey, + uECC_secp256r1() ) != 0 ); + TEST_ASSERT( memcmp( tmp_pubkey, uecckey->public_key, + sizeof( tmp_pubkey ) ) == 0 ); +#endif /* MBEDTLS_USE_TINYCRYPT */ } exit: mbedtls_pk_free( &ctx ); +#endif /* !MBEDTLS_ECP_C && !MBEDTLS_USE_TINYCRYPT */ } /* END_CASE */