Skip to content

Commit 648317c

Browse files
danielinuxtmael
authored andcommitted
[TA-100] Fixed ECC384. Adding RSA.
1 parent 3056e97 commit 648317c

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
@@ -4707,7 +4707,8 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
47074707
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
47084708
defined(WOLFSSL_MICROCHIP_TA100)
47094709
/* For SECP256R1 use hardware */
4710-
if (private_key->dp->id == ECC_SECP256R1) {
4710+
if (private_key->dp->id == ECC_SECP256R1 &&
4711+
private_key->slot != ATECC_INVALID_SLOT) {
47114712
err = atmel_ecc_create_pms(private_key->slot, public_key->pubkey_raw, out);
47124713
*outlen = private_key->dp->size;
47134714
}
@@ -5615,6 +5616,21 @@ int wc_ecc_make_pub_ex(ecc_key* key, ecc_point* pubOut, WC_RNG* rng)
56155616
return err;
56165617
}
56175618

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

56195635
static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
56205636
int curve_id, int flags)
@@ -5711,12 +5727,17 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
57115727
key->dp->id == ECC_SECP256K1 ||
57125728
key->dp->id == ECC_BRAINPOOLP256R1) { /* supports more than ECC256R1 curve */
57135729
#else
5714-
if (key->dp->id == ECC_SECP256R1) {
5730+
if (key->dp->id == ECC_SECP256R1 ||
5731+
key->dp->id == ECC_SECP224R1 ||
5732+
key->dp->id == ECC_SECP384R1 ||
5733+
key->dp->id == ECC_SECP256K1 ||
5734+
key->dp->id == ECC_BRAINPOOLP256R1) {
57155735
#endif
57165736
key->type = ECC_PRIVATEKEY;
57175737
if (key->slot == ATECC_INVALID_SLOT)
57185738
key->slot = atmel_ecc_alloc(ATMEL_SLOT_ECDHE);
5719-
err = atmel_ecc_create_key(key->slot, curve_id, key->pubkey_raw);
5739+
err = atmel_ecc_create_key(key->slot, ta100_curve_id_for_key(key),
5740+
key->pubkey_raw);
57205741

57215742
/* populate key->pubkey */
57225743
if (err == 0
@@ -5725,16 +5746,16 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
57255746
#endif
57265747
) {
57275748
err = mp_read_unsigned_bin(key->pubkey.x, key->pubkey_raw,
5728-
ECC_MAX_CRYPTO_HW_SIZE);
5749+
(word32)key->dp->size);
57295750
}
57305751
if (err == 0
57315752
#ifdef ALT_ECC_SIZE
57325753
&& key->pubkey.y
57335754
#endif
57345755
) {
57355756
err = mp_read_unsigned_bin(key->pubkey.y,
5736-
key->pubkey_raw + ECC_MAX_CRYPTO_HW_SIZE,
5737-
ECC_MAX_CRYPTO_HW_SIZE);
5757+
key->pubkey_raw + key->dp->size,
5758+
(word32)key->dp->size);
57385759
}
57395760
}
57405761
else {
@@ -6209,6 +6230,14 @@ int wc_ecc_init_ex(ecc_key* key, void* heap, int devId)
62096230
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
62106231
defined(WOLFSSL_MICROCHIP_TA100)
62116232
key->slot = ATECC_INVALID_SLOT;
6233+
#ifdef WOLFSSL_MICROCHIP_TA100
6234+
/* TA100 needs pubkey initialized to populate after genkey */
6235+
ret = mp_init_multi(key->pubkey.x, key->pubkey.y, key->pubkey.z,
6236+
NULL, NULL, NULL);
6237+
if (ret != MP_OKAY) {
6238+
return MEMORY_E;
6239+
}
6240+
#endif
62126241
#else
62136242
#if defined(WOLFSSL_KCAPI_ECC)
62146243
key->handle = NULL;
@@ -6450,9 +6479,22 @@ static int wc_ecc_sign_hash_hw(const byte* in, word32 inlen,
64506479

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

64576499
if (err != 0) {
64586500
return err;
@@ -9290,8 +9332,22 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
92909332
}
92919333
#endif /* WOLFSSL_SE050 */
92929334

9293-
#if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
9294-
defined(WOLFSSL_MICROCHIP_TA100)
9335+
#if defined(WOLFSSL_MICROCHIP_TA100)
9336+
if (ta100_curve_id_for_key(key) == ECC_SECP256R1) {
9337+
err = atmel_ecc_verify(hash, sigRS, key->pubkey_raw, res);
9338+
if (err != 0) {
9339+
return err;
9340+
}
9341+
(void)hashlen;
9342+
}
9343+
else {
9344+
err = atmel_ecc_verify_ex(hash, hashlen, sigRS, key->pubkey_raw,
9345+
keySz * 2, ta100_curve_id_for_key(key), res);
9346+
if (err != 0) {
9347+
return err;
9348+
}
9349+
}
9350+
#elif defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A)
92959351
err = atmel_ecc_verify(hash, sigRS, key->pubkey_raw, res);
92969352
if (err != 0) {
92979353
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
@@ -3393,12 +3393,17 @@ static int RsaPublicEncryptEx(const byte* in, word32 inLen, byte* out,
33933393
#elif defined(WOLFSSL_MICROCHIP_TA100)
33943394
if (rsa_type == RSA_PUBLIC_ENCRYPT &&
33953395
pad_value == RSA_BLOCK_TYPE_2) {
3396-
3397-
return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key);
3396+
if (key->uKeyH != 0) {
3397+
return wc_Microchip_rsa_encrypt(in, inLen, out, outLen, key);
3398+
}
3399+
return WC_HW_E;
33983400
}
33993401
else if (rsa_type == RSA_PRIVATE_ENCRYPT &&
34003402
pad_value == RSA_BLOCK_TYPE_1) {
3401-
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
3403+
if (key->rKeyH != 0) {
3404+
return wc_Microchip_rsa_sign(in, inLen, out, outLen, key);
3405+
}
3406+
return WC_HW_E;
34023407
}
34033408
#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
34043409
if (rsa_type == RSA_PUBLIC_ENCRYPT && pad_value == RSA_BLOCK_TYPE_2) {
@@ -3565,13 +3570,18 @@ static int RsaPrivateDecryptEx(const byte* in, word32 inLen, byte* out,
35653570
#elif defined(WOLFSSL_MICROCHIP_TA100)
35663571
if (rsa_type == RSA_PRIVATE_DECRYPT &&
35673572
pad_value == RSA_BLOCK_TYPE_2) {
3568-
3569-
return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key);
3573+
if (key->rKeyH != 0) {
3574+
return wc_Microchip_rsa_decrypt(in, inLen, out, outLen, key);
3575+
}
3576+
return WC_HW_E;
35703577
}
35713578
else if (rsa_type == RSA_PUBLIC_DECRYPT &&
35723579
pad_value == RSA_BLOCK_TYPE_1) {
3573-
int tmp;
3574-
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
3580+
if (key->uKeyH != 0) {
3581+
int tmp;
3582+
return wc_Microchip_rsa_verify(in, inLen, out, outLen, key, &tmp);
3583+
}
3584+
return WC_HW_E;
35753585
}
35763586
#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_RSA)
35773587
if (rsa_type == RSA_PRIVATE_DECRYPT && pad_value == RSA_BLOCK_TYPE_2) {
@@ -4423,6 +4433,12 @@ int wc_RsaEncryptSize(const RsaKey* key)
44234433

44244434
ret = mp_unsigned_bin_size(&key->n);
44254435

4436+
#if defined(WOLFSSL_MICROCHIP_TA100)
4437+
if (ret == 0 && (key->rKeyH != 0 || key->uKeyH != 0)) {
4438+
ret = 2048 / 8;
4439+
}
4440+
#endif
4441+
44264442
#ifdef WOLF_CRYPTO_CB
44274443
if (ret == 0 && key->devId != INVALID_DEVID) {
44284444
if (wc_CryptoCb_RsaGetSize(key, &ret) == WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {

0 commit comments

Comments
 (0)