From c7b1dc8f941355af75f89a29406af359bba2c7db Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Fri, 19 Feb 2021 16:39:45 -0800 Subject: [PATCH 1/3] Fix Cryptocell ECC tests --- IDE/CRYPTOCELL/user_settings.h | 1 + wolfcrypt/src/ecc.c | 36 ++++++++++++++++++---------------- wolfcrypt/test/test.c | 11 +++++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/IDE/CRYPTOCELL/user_settings.h b/IDE/CRYPTOCELL/user_settings.h index bbc3bab28..2d85c910c 100644 --- a/IDE/CRYPTOCELL/user_settings.h +++ b/IDE/CRYPTOCELL/user_settings.h @@ -41,6 +41,7 @@ extern "C" { #endif #if defined(WOLFSSL_CRYPTOCELL) + /* see SASI_AES_KEY_MAX_SIZE_IN_BYTES in the nRF5 SDK */ #define AES_MAX_KEY_SIZE 128 #endif /* WOLFSSL_CRYPTOCELL*/ diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index f073f0c3a..b7b56e5d6 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -4542,7 +4542,7 @@ int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id, } #elif defined(WOLFSSL_CRYPTOCELL) - pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id)); raw_size = (word32)(key->dp->size)*2 + 1; /* generate first key pair */ @@ -7950,7 +7950,23 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, #ifdef WOLFSSL_SILABS_SE_ACCEL err = silabs_ecc_import(key, keysize); #endif +#ifdef WOLFSSL_CRYPTOCELL + const CRYS_ECPKI_Domain_t* pDomain; + CRYS_ECPKI_BUILD_TempData_t tempBuff; + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id)); + + /* create public key from external key buffer */ + err = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, + (byte*)in-1, /* re-adjust */ + inLen+1, /* original input */ + &key->ctx.pubKey, + &tempBuff); + + if (err != SA_SILIB_RET_OK){ + WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed"); + } +#endif #ifdef WOLFSSL_VALIDATE_ECC_IMPORT if (err == MP_OKAY) err = wc_ecc_check_key(key); @@ -8110,21 +8126,7 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, return ret; #ifdef WOLFSSL_CRYPTOCELL - pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); - - if (pub != NULL && pub[0] != '\0') { - /* create public key from external key buffer */ - ret = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, - (byte*)pub, - pubSz, - &key->ctx.pubKey, - &tempBuff); - - if (ret != SA_SILIB_RET_OK){ - WOLFSSL_MSG("CRYS_ECPKI_BuildPublKeyFullCheck failed"); - return ret; - } - } + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id)); /* import private key */ if (priv != NULL && priv[0] != '\0') { @@ -8396,7 +8398,7 @@ static int wc_ecc_import_raw_private(ecc_key* key, const char* qx, } if (err == MP_OKAY) { - pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(curve_id)); + pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id)); /* create public key from external key buffer */ err = CRYS_ECPKI_BuildPublKeyFullCheck(pDomain, diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 1d1bc566b..d10a34697 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -7700,7 +7700,8 @@ static int aes_key_size_test(void) if (ret != 0 || keySize != sizeof(key16)) ERROR_OUT(-5310, out); #endif - +#if !defined(WOLFSSL_CRYPTOCELL) +/* Cryptocell only supports AES-128 key size */ ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION); #ifdef WOLFSSL_AES_192 if (ret != 0) @@ -7726,7 +7727,7 @@ static int aes_key_size_test(void) if (ret != 0 || keySize != sizeof(key32)) ERROR_OUT(-5314, out); #endif - +#endif /* !WOLFSSL_CRYPTOCELL */ out: #ifdef WOLFSSL_SMALL_STACK @@ -21367,7 +21368,9 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, #ifdef HAVE_ECC_SIGN /* ECC w/out Shamir has issue with all 0 digest */ /* WC_BIGINT doesn't have 0 len well on hardware */ -#if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) + /* Cryptocell has issues with all 0 digest */ +#if defined(ECC_SHAMIR) && !defined(WOLFSSL_ASYNC_CRYPT) && \ + !defined(WOLFSSL_CRYPTOCELL) /* test DSA sign hash with zeros */ for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) { digest[i] = 0; @@ -21404,7 +21407,7 @@ static int ecc_test_curve_size(WC_RNG* rng, int keySize, int testVerifyCount, TEST_SLEEP(); } #endif /* HAVE_ECC_VERIFY */ -#endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT */ +#endif /* ECC_SHAMIR && !WOLFSSL_ASYNC_CRYPT && !WOLFSSL_CRYPTOCELL */ /* test DSA sign hash with sequence (0,1,2,3,4,...) */ for (i = 0; i < (int)ECC_DIGEST_SIZE; i++) { From 9bfbc999d98a8974b9278db84b3a68e61dcf732f Mon Sep 17 00:00:00 2001 From: Tesfa Mael Date: Tue, 23 Feb 2021 13:21:50 -0800 Subject: [PATCH 2/3] Move variable declarations to the top --- wolfcrypt/src/ecc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index b7b56e5d6..cbfb707b5 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -7770,7 +7770,10 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, #endif int keysize = 0; byte pointType; - +#ifdef WOLFSSL_CRYPTOCELL + const CRYS_ECPKI_Domain_t* pDomain; + CRYS_ECPKI_BUILD_TempData_t tempBuff; +#endif if (in == NULL || key == NULL) return BAD_FUNC_ARG; @@ -7951,9 +7954,6 @@ int wc_ecc_import_x963_ex(const byte* in, word32 inLen, ecc_key* key, err = silabs_ecc_import(key, keysize); #endif #ifdef WOLFSSL_CRYPTOCELL - const CRYS_ECPKI_Domain_t* pDomain; - CRYS_ECPKI_BUILD_TempData_t tempBuff; - pDomain = CRYS_ECPKI_GetEcDomain(cc310_mapCurve(key->dp->id)); /* create public key from external key buffer */ From 3a3c0be43f85ebc6da95ebbcc86634ef6195ce73 Mon Sep 17 00:00:00 2001 From: David Garske Date: Wed, 24 Feb 2021 15:05:27 -0800 Subject: [PATCH 3/3] Fixes for build warnings for CryptoCell with ECC and RSA. --- IDE/CRYPTOCELL/user_settings.h | 3 +++ wolfcrypt/src/ecc.c | 18 +++++++++--------- wolfcrypt/src/signature.c | 23 +++++++++++++++-------- wolfcrypt/test/test.c | 9 +++++++-- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/IDE/CRYPTOCELL/user_settings.h b/IDE/CRYPTOCELL/user_settings.h index 2d85c910c..78087f11b 100644 --- a/IDE/CRYPTOCELL/user_settings.h +++ b/IDE/CRYPTOCELL/user_settings.h @@ -138,6 +138,9 @@ extern "C" { #if 1 #define HAVE_ECC + #include + /* strings.h required for strncasecmp */ + /* Manually define enabled curves */ #undef ECC_USER_CURVES #define ECC_USER_CURVES diff --git a/wolfcrypt/src/ecc.c b/wolfcrypt/src/ecc.c index cbfb707b5..b6c21dce6 100644 --- a/wolfcrypt/src/ecc.c +++ b/wolfcrypt/src/ecc.c @@ -1178,7 +1178,8 @@ static int wc_ecc_export_x963_compressed(ecc_key*, byte* out, word32* outLen); #if (defined(WOLFSSL_VALIDATE_ECC_KEYGEN) || !defined(WOLFSSL_SP_MATH)) && \ - !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) + !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \ + !defined(WOLFSSL_CRYPTOCELL) static int ecc_check_pubkey_order(ecc_key* key, ecc_point* pubkey, mp_int* a, mp_int* prime, mp_int* order); #endif @@ -3824,7 +3825,7 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out, err = CRYS_ECDH_SVDP_DH(&public_key->ctx.pubKey, &private_key->ctx.privKey, out, - outlen, + (uint32_t*)outlen, &tempBuff); if (err != SA_SILIB_RET_OK){ @@ -4563,7 +4564,7 @@ int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id, err = CRYS_ECPKI_ExportPublKey(&key->ctx.pubKey, CRYS_EC_PointUncompressed, &ucompressed_key[0], - &raw_size); + (uint32_t*)&raw_size); if (err == SA_SILIB_RET_OK && key->pubkey.x && key->pubkey.y) { err = mp_read_unsigned_bin(key->pubkey.x, @@ -4577,7 +4578,7 @@ int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id, if (err == MP_OKAY) { err = CRYS_ECPKI_ExportPrivKey(&key->ctx.privKey, ucompressed_key, - &raw_size); + (uint32_t*)&raw_size); } if (err == SA_SILIB_RET_OK) { @@ -4993,7 +4994,7 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen, (byte*)in, msgLenInBytes, out, - &raw_sig_size); + (uint32_t*)&raw_sig_size); if (err != SA_SILIB_RET_OK){ WOLFSSL_MSG("CRYS_ECDSA_Sign failed"); @@ -7656,7 +7657,8 @@ int wc_ecc_check_key(ecc_key* key) #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \ defined(WOLFSSL_CRYPTOCELL) || defined(WOLFSSL_SILABS_SE_ACCEL) - err = 0; /* consider key check success on ATECC508/608A */ + err = 0; /* consider key check success on ATECC508/608A and CryptoCell */ + (void)err; #else #ifdef USE_ECC_B_PARAM @@ -7751,9 +7753,8 @@ int wc_ecc_check_key(ecc_key* key) #endif FREE_CURVE_SPECS(); - - return err; #endif /* WOLFSSL_ATECC508A */ + return err; #else return WC_KEY_SIZE_E; #endif /* !WOLFSSL_SP_MATH */ @@ -8095,7 +8096,6 @@ int wc_ecc_import_private_key_ex(const byte* priv, word32 privSz, int ret; #ifdef WOLFSSL_CRYPTOCELL const CRYS_ECPKI_Domain_t* pDomain; - CRYS_ECPKI_BUILD_TempData_t tempBuff; #endif if (key == NULL || priv == NULL) return BAD_FUNC_ARG; diff --git a/wolfcrypt/src/signature.c b/wolfcrypt/src/signature.c index 6e623a130..50c883906 100644 --- a/wolfcrypt/src/signature.c +++ b/wolfcrypt/src/signature.c @@ -48,6 +48,13 @@ /* Signature wrapper disabled check */ #ifndef NO_SIG_WRAPPER +#ifdef WOLFSSL_CRYPTOCELL +extern int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig, + RsaKey* key, CRYS_RSA_HASH_OpMode_t mode); +extern int cc310_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, + word32 outLen, RsaKey* key, CRYS_RSA_HASH_OpMode_t mode); +#endif + #if !defined(NO_RSA) && !defined(NO_ASN) static int wc_SignatureDerEncode(enum wc_HashType hash_type, byte* hash_data, word32 hash_len, word32* hash_enc_len) @@ -178,12 +185,12 @@ int wc_SignatureVerifyHash( #ifndef NO_RSA #ifdef WOLFSSL_CRYPTOCELL if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) { - ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, key, - cc310_hashModeRSA(hash_type, 0)); + ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, + (RsaKey*)key, cc310_hashModeRSA(hash_type, 0)); } else { - ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, key, - cc310_hashModeRSA(hash_type, 1)); + ret = cc310_RsaSSL_Verify(hash_data, hash_len, (byte*)sig, + (RsaKey*)key, cc310_hashModeRSA(hash_type, 1)); } #else @@ -400,12 +407,12 @@ int wc_SignatureGenerateHash_ex( /* use expected signature size (incoming sig_len could be larger buffer */ *sig_len = wc_SignatureGetSize(sig_type, key, key_len); if (sig_type == WC_SIGNATURE_TYPE_RSA_W_ENC) { - ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len, key, - cc310_hashModeRSA(hash_type, 0)); + ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len, + (RsaKey*)key, cc310_hashModeRSA(hash_type, 0)); } else { - ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len, key, - cc310_hashModeRSA(hash_type, 1)); + ret = cc310_RsaSSL_Sign(hash_data, hash_len, sig, *sig_len, + (RsaKey*)key, cc310_hashModeRSA(hash_type, 1)); } #else /* Create signature using provided RSA key */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index d10a34697..4c02fc006 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -7621,9 +7621,11 @@ static int aes_key_size_test(void) #endif byte key16[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 }; +#ifndef WOLFSSL_CRYPTOCELL byte key24[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37 }; +#endif byte key32[] = { 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, @@ -7700,7 +7702,7 @@ static int aes_key_size_test(void) if (ret != 0 || keySize != sizeof(key16)) ERROR_OUT(-5310, out); #endif -#if !defined(WOLFSSL_CRYPTOCELL) +#ifndef WOLFSSL_CRYPTOCELL /* Cryptocell only supports AES-128 key size */ ret = wc_AesSetKey(aes, key24, sizeof(key24), iv, AES_ENCRYPTION); #ifdef WOLFSSL_AES_192 @@ -14435,7 +14437,9 @@ static int rsa_keygen_test(WC_RNG* rng) #endif int ret; byte* der = NULL; +#ifndef WOLFSSL_CRYPTOCELL word32 idx = 0; +#endif int derSz = 0; #if !defined(WOLFSSL_SP_MATH) && !defined(HAVE_FIPS) int keySz = 1024; @@ -14495,8 +14499,9 @@ static int rsa_keygen_test(WC_RNG* rng) if (ret != 0) { ERROR_OUT(-7875, exit_rsa); } + +#ifndef WOLFSSL_CRYPTOCELL idx = 0; -#if !defined(WOLFSSL_CRYPTOCELL) /* The private key part of the key gen pairs from cryptocell can't be exported */ ret = wc_RsaPrivateKeyDecode(der, &idx, genKey, derSz); if (ret != 0) {