Skip to content

Commit c37edb0

Browse files
committed
Fix STM32 PKA V2 (STM32U5) point multiply missing order/coefB.
1 parent be8000d commit c37edb0

1 file changed

Lines changed: 47 additions & 3 deletions

File tree

wolfcrypt/src/port/st/stm32.c

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,43 @@ static int stm32_get_from_hexstr(const char* hex, uint8_t* dst, int sz)
638638
return stm32_getabs_from_hexstr(hex, dst, sz, NULL);
639639
}
640640

641-
642641
/* STM32 PKA supports up to 640-bit numbers */
643642
#define STM32_MAX_ECC_SIZE (80)
644643

644+
#ifdef WOLFSSL_STM32_PKA_V2
645+
/* find curve based on prime/modulus and return order/coefB */
646+
static int stm32_get_curve_params(mp_int* modulus,
647+
uint8_t* order, uint8_t* coefB)
648+
{
649+
int res, i, found = 0;
650+
mp_int modulusChk;
651+
res = mp_init(&modulusChk);
652+
if (res != MP_OKAY)
653+
return res;
654+
for (i = 0; ecc_sets[i].size != 0 && ecc_sets[i].name != NULL; i++) {
655+
const ecc_set_type* curve = &ecc_sets[i];
656+
/* match based on curve prime */
657+
if ((res = mp_read_radix(&modulusChk, curve->prime, MP_RADIX_HEX)) ==
658+
MP_OKAY && (mp_cmp(modulus, &modulusChk) == MP_EQ))
659+
{
660+
found = 1;
661+
if (order) {
662+
res = stm32_get_from_hexstr(curve->order, order, curve->size);
663+
}
664+
if (coefB) {
665+
res = stm32_get_from_hexstr(curve->Bf, coefB, curve->size);
666+
}
667+
break;
668+
}
669+
}
670+
mp_clear(&modulusChk);
671+
if (!found && res == MP_OKAY) {
672+
res = MP_RANGE;
673+
}
674+
return res;
675+
}
676+
#endif /* WOLFSSL_STM32_PKA_V2 */
677+
645678

646679
/**
647680
Perform a point multiplication (timing resistant)
@@ -706,8 +739,19 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point *G, ecc_point *R, mp_int* a,
706739
#ifdef WOLFSSL_STM32_PKA_V2
707740
XMEMSET(order, 0, sizeof(order));
708741
XMEMSET(coefB, 0, sizeof(coefB));
709-
if (res == MP_OKAY && o != NULL)
710-
res = stm32_get_from_mp_int(order, o, szModulus);
742+
if (res == MP_OKAY) {
743+
if (o != NULL) {
744+
/* use provided order and get coefB */
745+
res = stm32_get_from_mp_int(order, o, szModulus);
746+
if (res == MP_OKAY) {
747+
res = stm32_get_curve_params(modulus, NULL, coefB);
748+
}
749+
}
750+
else {
751+
/* get order and coefB for matching prime */
752+
res = stm32_get_curve_params(modulus, order, coefB);
753+
}
754+
}
711755
#endif
712756
if (res != MP_OKAY)
713757
return res;

0 commit comments

Comments
 (0)