Skip to content

Commit fc3977f

Browse files
authored
Merge pull request #7098 from dgarske/stm32_pka
Fixes for STM32 PKA
2 parents b8392ef + c37edb0 commit fc3977f

2 files changed

Lines changed: 74 additions & 20 deletions

File tree

IDE/STM32Cube/default_conf.ftl

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,28 @@ extern ${variable.value} ${variable.name};
213213
/* ------------------------------------------------------------------------- */
214214
/* Math Configuration */
215215
/* ------------------------------------------------------------------------- */
216-
/* 1=Fast (stack)
217-
* 2=Normal (heap)
218-
* 3=Single Precision C (only common curves/key sizes)
219-
* 4=Single Precision ASM Cortex-M3+
220-
* 5=Single Precision ASM Cortex-M0 (Generic Thumb)
221-
* 6=Single Precision C all small
222-
* 7=Single Precision C all big
216+
/* 1=Fast (stack) (tfm.c)
217+
* 2=Normal (heap) (integer.c)
218+
* 3-5=Single Precision: only common curves/key sizes:
219+
* (ECC 256/384/521 and RSA/DH 2048/3072/4096)
220+
* 3=Single Precision C (sp_c32.c)
221+
* 4=Single Precision ASM Cortex-M3+ (sp_cortexm.c)
222+
* 5=Single Precision ASM Cortex-M0 (sp_armthumb.c)
223+
* 6=Wolf multi-precision C small (sp_int.c)
224+
* 7=Wolf multi-precision C big (sp_int.c)
223225
*/
226+
224227
#if defined(WOLF_CONF_MATH) && WOLF_CONF_MATH == 1
225228
/* fast (stack) math - tfm.c */
226229
#define USE_FAST_MATH
227230
#define TFM_TIMING_RESISTANT
228231

232+
#if !defined(NO_RSA) || !defined(NO_DH)
233+
/* Maximum math bits (Max DH/RSA key bits * 2) */
234+
#undef FP_MAX_BITS
235+
#define FP_MAX_BITS 4096
236+
#endif
237+
229238
/* Optimizations (TFM_ARM, TFM_ASM or none) */
230239
//#define TFM_NO_ASM
231240
//#define TFM_ASM
@@ -240,19 +249,26 @@ extern ${variable.value} ${variable.name};
240249
#endif
241250
#if defined(WOLF_CONF_RSA) && WOLF_CONF_RSA == 1
242251
#define WOLFSSL_HAVE_SP_RSA
252+
//#define WOLFSSL_SP_NO_2048
253+
//#define WOLFSSL_SP_NO_3072
254+
//#define WOLFSSL_SP_4096
243255
#endif
244256
#if defined(WOLF_CONF_DH) && WOLF_CONF_DH == 1
245257
#define WOLFSSL_HAVE_SP_DH
246258
#endif
247259
#if defined(WOLF_CONF_ECC) && WOLF_CONF_ECC == 1
248260
#define WOLFSSL_HAVE_SP_ECC
261+
//#define WOLFSSL_SP_NO_256
262+
//#define WOLFSSL_SP_384
263+
//#define WOLFSSL_SP_521
249264
#endif
250265
#if WOLF_CONF_MATH == 6 || WOLF_CONF_MATH == 7
251266
#define WOLFSSL_SP_MATH_ALL /* use sp_int.c multi precision math */
267+
//#define WOLFSSL_SP_ARM_THUMB /* enable ARM Thumb ASM speedups */
252268
#else
253269
#define WOLFSSL_SP_MATH /* disable non-standard curves / key sizes */
254270
#endif
255-
#define SP_WORD_SIZE 32
271+
#define SP_WORD_SIZE 32 /* force 32-bit mode */
256272

257273
/* Enable to put all math on stack (no heap) */
258274
//#define WOLFSSL_SP_NO_MALLOC
@@ -331,12 +347,6 @@ extern ${variable.value} ${variable.name};
331347
/* RSA */
332348
#undef NO_RSA
333349
#if defined(WOLF_CONF_RSA) && WOLF_CONF_RSA == 1
334-
#ifdef USE_FAST_MATH
335-
/* Maximum math bits (Max RSA key bits * 2) */
336-
#undef FP_MAX_BITS
337-
#define FP_MAX_BITS 4096
338-
#endif
339-
340350
/* half as much memory but twice as slow */
341351
#undef RSA_LOW_MEM
342352
//#define RSA_LOW_MEM
@@ -390,8 +400,8 @@ extern ${variable.value} ${variable.name};
390400
//#define HAVE_COMP_KEY
391401

392402
#ifdef USE_FAST_MATH
393-
#ifdef NO_RSA
394-
/* Custom fastmath size if not using RSA */
403+
#if defined(NO_RSA) && defined(NO_DH)
404+
/* Custom fastmath size if not using RSA/DH */
395405
/* MAX = ROUND32(ECC BITS) * 2 */
396406
#define FP_MAX_BITS (256 * 2)
397407
#else

wolfcrypt/src/port/st/stm32.c

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ static int stm32_getabs_from_mp_int(uint8_t *dst, const mp_int *a, int sz,
595595
#else
596596
*abs_sign = 1; /* default to negative */
597597
#endif
598-
res = mp_abs(a, &x);
598+
res = mp_abs((mp_int*)a, &x);
599599
if (res == MP_OKAY)
600600
res = stm32_get_from_mp_int(dst, &x, sz);
601601
mp_clear(&x);
@@ -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)