Skip to content

Commit f53985a

Browse files
committed
[TA-100] Fixed ECC384. Adding RSA.
1 parent 948e3e5 commit f53985a

7 files changed

Lines changed: 308 additions & 19 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4708,7 +4708,8 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
47084708
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
47094709
defined(WOLFSSL_MICROCHIP_TA100)
47104710
/* For SECP256R1 use hardware */
4711-
if (private_key->dp->id == ECC_SECP256R1) {
4711+
if (private_key->dp->id == ECC_SECP256R1 &&
4712+
private_key->slot != ATECC_INVALID_SLOT) {
47124713
err = atmel_ecc_create_pms(private_key->slot, public_key->pubkey_raw, out);
47134714
*outlen = private_key->dp->size;
47144715
}
@@ -5616,6 +5617,21 @@ int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng)
56165617
return err;
56175618
}
56185619

5620+
#if defined(WOLFSSL_MICROCHIP_TA100)
5621+
static WC_INLINE int ta100_curve_id_for_key(const ecc_key* key)
5622+
{
5623+
if (key != NULL && key->dp != NULL) {
5624+
switch (key->dp->size) {
5625+
case 28: return ECC_SECP224R1;
5626+
case 32: return ECC_SECP256R1;
5627+
case 48: return ECC_SECP384R1;
5628+
default: return key->dp->id;
5629+
}
5630+
}
5631+
return ECC_CURVE_DEF;
5632+
}
5633+
#endif
5634+
56195635

56205636
static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
56215637
int curve_id, int flags)
@@ -5712,12 +5728,17 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
57125728
key->dp->id == ECC_SECP256K1 ||
57135729
key->dp->id == ECC_BRAINPOOLP256R1) { /* supports more than ECC256R1 curve */
57145730
#else
5715-
if (key->dp->id == ECC_SECP256R1) {
5731+
if (key->dp->id == ECC_SECP256R1 ||
5732+
key->dp->id == ECC_SECP224R1 ||
5733+
key->dp->id == ECC_SECP384R1 ||
5734+
key->dp->id == ECC_SECP256K1 ||
5735+
key->dp->id == ECC_BRAINPOOLP256R1) {
57165736
#endif
57175737
key->type = ECC_PRIVATEKEY;
57185738
if (key->slot == ATECC_INVALID_SLOT)
57195739
key->slot = atmel_ecc_alloc(ATMEL_SLOT_ECDHE);
5720-
err = atmel_ecc_create_key(key->slot, curve_id, key->pubkey_raw);
5740+
err = atmel_ecc_create_key(key->slot, ta100_curve_id_for_key(key),
5741+
key->pubkey_raw);
57215742

57225743
/* populate key->pubkey */
57235744
if (err == 0
@@ -5726,16 +5747,16 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
57265747
#endif
57275748
) {
57285749
err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw,
5729-
ECC_MAX_CRYPTO_HW_SIZE);
5750+
(word32)key->dp->size);
57305751
}
57315752
if (err == 0
57325753
#ifdef ALT_ECC_SIZE
57335754
&& key->pubkey.y
57345755
#endif
57355756
) {
57365757
err = mp_read_unsigned_bin(key->pubkey.y,
5737-
key->pubkey_raw + ECC_MAX_CRYPTO_HW_SIZE,
5738-
ECC_MAX_CRYPTO_HW_SIZE);
5758+
key->pubkey_raw + key->dp->size,
5759+
(word32)key->dp->size);
57395760
}
57405761
}
57415762
else {
@@ -6210,6 +6231,14 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
62106231
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
62116232
defined(WOLFSSL_MICROCHIP_TA100)
62126233
key->slot = ATECC_INVALID_SLOT;
6234+
#ifdef WOLFSSL_MICROCHIP_TA100
6235+
/* TA100 needs pubkey initialized to populate after genkey */
6236+
ret = mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z,
6237+
NULL, NULL, NULL);
6238+
if (ret != MP_OKAY) {
6239+
return MEMORY_E;
6240+
}
6241+
#endif
62136242
#else
62146243
#if defined(WOLFSSL_KCAPI_ECC)
62156244
key->handle = NULL;
@@ -6451,9 +6480,22 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen,
64516480

64526481
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
64536482
defined(WOLFSSL_MICROCHIP_TA100)
6483+
#if defined(WOLFSSL_MICROCHIP_TA100)
6484+
if (ta100_curve_id_for_key(key) == ECC_SECP256R1) {
6485+
(void)inlen;
6486+
/* Sign: Result is 32-bytes of R then 32-bytes of S */
6487+
err = atmel_ecc_sign(key->slot, in, out);
6488+
}
6489+
else {
6490+
/* Sign: Result is raw R||S */
6491+
err = atmel_ecc_sign_ex(key->slot, ta100_curve_id_for_key(key),
6492+
in, inlen, out);
6493+
}
6494+
#else
64546495
(void)inlen;
64556496
/* Sign: Result is 32-bytes of R then 32-bytes of S */
64566497
err = atmel_ecc_sign(key->slot, in, out);
6498+
#endif
64576499

64586500
if (err != 0) {
64596501
return err;
@@ -9284,8 +9326,22 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
92849326
}
92859327
#endif /* WOLFSSL_SE050 */
92869328

9287-
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
9288-
defined(WOLFSSL_MICROCHIP_TA100)
9329+
#if defined(WOLFSSL_MICROCHIP_TA100)
9330+
if (ta100_curve_id_for_key(key) == ECC_SECP256R1) {
9331+
err = atmel_ecc_verify(hash, sigRS, key->pubkey_raw, res);
9332+
if (err != 0) {
9333+
return err;
9334+
}
9335+
(void)hashlen;
9336+
}
9337+
else {
9338+
err = atmel_ecc_verify_ex(hash, hashlen, sigRS, key->pubkey_raw,
9339+
keySz * 2, ta100_curve_id_for_key(key), res);
9340+
if (err != 0) {
9341+
return err;
9342+
}
9343+
}
9344+
#elif defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
92899345
err = atmel_ecc_verify(hash, sigRS, key->pubkey_raw, res);
92909346
if (err != 0) {
92919347
return err;

wolfcrypt/src/port/atmel/atmel.c

Lines changed: 172 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,21 @@ static uint8_t getCurveType(int curve_id)
648648
}
649649
}
650650
#endif /* WOLFSSL_MICROCHIP_TA100 */
651+
652+
#ifdef WOLFSSL_MICROCHIP_TA100
653+
static int getCurveSizeBytes(int curve_id)
654+
{
655+
switch (curve_id) {
656+
case ECC_SECP224R1: return 28;
657+
case ECC_SECP256R1: return 32;
658+
case ECC_SECP384R1: return 48;
659+
case ECC_SECP256K1: return 32;
660+
case ECC_BRAINPOOLP256R1: return 32;
661+
case ECC_CURVE_DEF: return 32;
662+
default: return -1;
663+
}
664+
}
665+
#endif /* WOLFSSL_MICROCHIP_TA100 */
651666
int atmel_ecc_create_key(int slotId, int curve_id, byte* peerKey)
652667
{
653668
int ret;
@@ -665,9 +680,55 @@ int atmel_ecc_create_key(int slotId, int curve_id, byte* peerKey)
665680

666681
#endif
667682
/* generate new ephemeral key on device */
683+
#ifdef WOLFSSL_MICROCHIP_TA100
684+
#if defined(TA100_ECC_TRACE)
685+
printf("[TA100] atmel_ecc_create_key: slot=%d curve_id=%d curve_size=%d curve_type=%d\r\n",
686+
slotId, curve_id, getCurveSizeBytes(curve_id), getCurveType(curve_id));
687+
#endif
688+
{
689+
ATCA_STATUS status;
690+
ta_element_attributes_t key_attr;
691+
uint8_t is_valid = 0;
692+
int curve_size = getCurveSizeBytes(curve_id);
693+
int curve_type = getCurveType(curve_id);
694+
size_t pubkey_len = (size_t)(curve_size * 2);
695+
696+
if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC)
697+
return NOT_COMPILED_IN;
698+
699+
status = talib_is_handle_valid(atcab_get_device(),
700+
(uint32_t)MAP_TO_HANDLE(slotId), &is_valid);
701+
if (status == ATCA_SUCCESS && is_valid == 0x01) {
702+
status = talib_delete_handle(atcab_get_device(),
703+
(uint32_t)MAP_TO_HANDLE(slotId));
704+
}
705+
if (status != ATCA_SUCCESS)
706+
return atmel_ecc_translate_err(status);
707+
708+
status = talib_handle_init_private_key(&key_attr,
709+
(uint8_t)curve_type, TA_ALG_MODE_ECC_ECDSA,
710+
TA_PROP_SIGN_INT_EXT_DIGEST, TA_PROP_KEY_AGREEMENT_OUT_BUFF);
711+
if (status != ATCA_SUCCESS)
712+
return atmel_ecc_translate_err(status);
713+
714+
ta100_fix_property_endian(&key_attr);
715+
status = talib_create_element_with_handle(atcab_get_device(),
716+
(uint32_t)MAP_TO_HANDLE(slotId), &key_attr);
717+
if (status != ATCA_SUCCESS)
718+
return atmel_ecc_translate_err(status);
719+
720+
status = talib_genkey_base(atcab_get_device(), TA_KEYGEN_MODE_NEWKEY,
721+
(uint32_t)MAP_TO_HANDLE(slotId), peerKey, &pubkey_len);
722+
#if defined(TA100_ECC_TRACE)
723+
printf("[TA100] atmel_ecc_create_key: genkey status=%d pubkey_len=%u\r\n",
724+
status, (unsigned)pubkey_len);
725+
#endif
726+
return atmel_ecc_translate_err(status);
727+
}
728+
#endif
729+
668730
ret = atcab_genkey(MAP_TO_HANDLE(slotId), peerKey);
669-
ret = atmel_ecc_translate_err(ret);
670-
return ret;
731+
return atmel_ecc_translate_err(ret);
671732
}
672733

673734
int atmel_ecc_sign(int slotId, const byte* message, byte* signature)
@@ -692,6 +753,111 @@ int atmel_ecc_verify(const byte* message, const byte* signature,
692753
return ret;
693754
}
694755

756+
#ifdef WOLFSSL_MICROCHIP_TA100
757+
int atmel_ecc_sign_ex(int slotId, int curve_id, const byte* message,
758+
word32 message_len, byte* signature)
759+
{
760+
int ret;
761+
int curve_size = getCurveSizeBytes(curve_id);
762+
int curve_type = getCurveType(curve_id);
763+
uint16_t sign_size;
764+
const byte* msg = message;
765+
uint16_t msg_len;
766+
byte tmp_msg[TA_SIGN_P384_MSG_SIZE];
767+
byte tmp_sig[TA_SIGN_P384_SIG_SIZE];
768+
769+
if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC)
770+
return NOT_COMPILED_IN;
771+
772+
sign_size = (uint16_t)(curve_size * 2);
773+
if (sign_size > sizeof(tmp_sig))
774+
return BAD_FUNC_ARG;
775+
msg_len = (uint16_t)message_len;
776+
if (msg_len != (uint16_t)curve_size) {
777+
if (msg_len > (uint16_t)curve_size) {
778+
msg_len = (uint16_t)curve_size;
779+
} else {
780+
XMEMSET(tmp_msg, 0, (word32)curve_size);
781+
XMEMCPY(tmp_msg + (curve_size - msg_len), message, msg_len);
782+
msg = tmp_msg;
783+
msg_len = (uint16_t)curve_size;
784+
}
785+
}
786+
#if defined(TA100_ECC_TRACE)
787+
printf("[TA100] atmel_ecc_sign_ex: curve_size=%d msg_len=%u\r\n",
788+
curve_size, (unsigned)msg_len);
789+
#endif
790+
ret = talib_sign_external(atcab_get_device(), (uint8_t)curve_type,
791+
MAP_TO_HANDLE(slotId), TA_HANDLE_INPUT_BUFFER, msg,
792+
msg_len, tmp_sig, &sign_size);
793+
794+
if (ret != ATCA_SUCCESS)
795+
return atmel_ecc_translate_err(ret);
796+
797+
/* Always return raw R||S, each padded to curve size */
798+
XMEMSET(signature, 0, (word32)(curve_size * 2));
799+
if (sign_size == (uint16_t)(curve_size * 2)) {
800+
XMEMCPY(signature, tmp_sig, sign_size);
801+
}
802+
else if ((sign_size % 2) == 0 && sign_size < (uint16_t)(curve_size * 2)) {
803+
uint16_t half = (uint16_t)(sign_size / 2);
804+
if (half > (uint16_t)curve_size)
805+
return BAD_FUNC_ARG;
806+
XMEMCPY(signature + (curve_size - half), tmp_sig, half);
807+
XMEMCPY(signature + curve_size + (curve_size - half),
808+
tmp_sig + half, half);
809+
}
810+
else {
811+
return ASN_PARSE_E;
812+
}
813+
814+
return 0;
815+
}
816+
817+
int atmel_ecc_verify_ex(const byte* message, word32 message_len,
818+
const byte* signature, const byte* pubkey, word32 pubkey_len,
819+
int curve_id, int* pVerified)
820+
{
821+
int ret;
822+
int curve_size = getCurveSizeBytes(curve_id);
823+
int curve_type = getCurveType(curve_id);
824+
uint16_t sig_len;
825+
const byte* msg = message;
826+
uint16_t msg_len;
827+
byte tmp_msg[TA_VERIFY_P384_MSG_SIZE];
828+
bool verified = false;
829+
830+
if (curve_size <= 0 || curve_type == MICROCHIP_INVALID_ECC)
831+
return NOT_COMPILED_IN;
832+
833+
sig_len = (uint16_t)(curve_size * 2);
834+
msg_len = (uint16_t)message_len;
835+
if (msg_len != (uint16_t)curve_size) {
836+
if (msg_len > (uint16_t)curve_size) {
837+
msg_len = (uint16_t)curve_size;
838+
} else {
839+
XMEMSET(tmp_msg, 0, (word32)curve_size);
840+
XMEMCPY(tmp_msg + (curve_size - msg_len), message, msg_len);
841+
msg = tmp_msg;
842+
msg_len = (uint16_t)curve_size;
843+
}
844+
}
845+
#if defined(TA100_ECC_TRACE)
846+
printf("[TA100] atmel_ecc_verify_ex: curve_size=%d msg_len=%u\r\n",
847+
curve_size, (unsigned)msg_len);
848+
#endif
849+
ret = talib_verify(atcab_get_device(), (uint8_t)curve_type,
850+
TA_HANDLE_INPUT_BUFFER, TA_HANDLE_INPUT_BUFFER, signature, sig_len,
851+
msg, msg_len, pubkey, (uint16_t)pubkey_len,
852+
&verified);
853+
854+
ret = atmel_ecc_translate_err(ret);
855+
if (pVerified)
856+
*pVerified = (int)verified;
857+
return ret;
858+
}
859+
#endif /* WOLFSSL_MICROCHIP_TA100 */
860+
695861
#endif /* HAVE_ECC */
696862
#endif /* WOLFSSL_ATECC508A || WOLFSSL_ATECC608A || WOLFSSL_MICROCHIP_TA100 */
697863

@@ -1335,7 +1501,11 @@ int atcatls_sign_certificate_cb(WOLFSSL* ssl, const byte* in, unsigned int inSz,
13351501
return WC_HW_WAIT_E;
13361502

13371503
/* We can only sign with P-256 */
1504+
#ifdef WOLFSSL_MICROCHIP_TA100
1505+
ret = atmel_ecc_sign_ex(slotId, ECC_SECP256R1, in, inSz, sigRs);
1506+
#else
13381507
ret = atmel_ecc_sign(MAP_TO_HANDLE(slotId), in, sigRs);
1508+
#endif
13391509
if (ret != ATCA_SUCCESS) {
13401510
ret = WC_HW_E; goto exit;
13411511
}

wolfcrypt/src/rsa.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3385,12 +3385,17 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33853385
#elif defined(WOLFSSL_MICROCHIP_TA100)
33863386
if (rsa_type == RSA_PUBLIC_ENCRYPT &&
33873387
pad_value == RSA_BLOCK_TYPE_2) {
3388-
3389-
return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key);
3388+
if (key->uKeyH != 0) {
3389+
return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key);
3390+
}
3391+
return WC_HW_E;
33903392
}
33913393
else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
33923394
pad_value == RSA_BLOCK_TYPE_1) {
3393-
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
3395+
if (key->rKeyH != 0) {
3396+
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
3397+
}
3398+
return WC_HW_E;
33943399
}
33953400
#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
33963401
if (rsa_type == RSA_PUBLIC_ENCRYPT && pad_value == RSA_BLOCK_TYPE_2) {
@@ -3557,13 +3562,18 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35573562
#elif defined(WOLFSSL_MICROCHIP_TA100)
35583563
if (rsa_type == RSA_PRIVATE_DECRYPT &&
35593564
pad_value == RSA_BLOCK_TYPE_2) {
3560-
3561-
return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key);
3565+
if (key->rKeyH != 0) {
3566+
return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key);
3567+
}
3568+
return WC_HW_E;
35623569
}
35633570
else if (rsa_type == RSA_PUBLIC_DECRYPT &&
35643571
pad_value == RSA_BLOCK_TYPE_1) {
3565-
int tmp;
3566-
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
3572+
if (key->uKeyH != 0) {
3573+
int tmp;
3574+
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
3575+
}
3576+
return WC_HW_E;
35673577
}
35683578
#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
35693579
if (rsa_type == RSA_PRIVATE_DECRYPT && pad_value == RSA_BLOCK_TYPE_2) {
@@ -4415,6 +4425,12 @@ int wc_RsaEncryptSize(const RsaKey* key)
44154425

44164426
ret = mp_unsigned_bin_size(&key->n);
44174427

4428+
#if defined(WOLFSSL_MICROCHIP_TA100)
4429+
if (ret == 0 && (key->rKeyH != 0 || key->uKeyH != 0)) {
4430+
ret = 2048 / 8;
4431+
}
4432+
#endif
4433+
44184434
#ifdef WOLF_CRYPTO_CB
44194435
if (ret == 0 && key->devId != INVALID_DEVID) {
44204436
if (wc_CryptoCb_RsaGetSize(key, &ret) == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {

0 commit comments

Comments
 (0)