Skip to content

Commit ef6f156

Browse files
authored
Merge pull request #7980 from ejohnstown/small-stack-fp
FP SmallStack Fix
2 parents 5ef617a + 47e5140 commit ef6f156

1 file changed

Lines changed: 20 additions & 20 deletions

File tree

wolfcrypt/src/tfm.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2430,7 +2430,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
24302430
fp_int *res;
24312431
fp_digit buf, mp;
24322432
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
2433-
#ifndef WOLFSSL_NO_MALLOC
2433+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
24342434
fp_int *M;
24352435
#else
24362436
fp_int M[(1 << 6) + 1];
@@ -2455,7 +2455,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
24552455
return err;
24562456
}
24572457

2458-
#ifndef WOLFSSL_NO_MALLOC
2458+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
24592459
/* only allocate space for what's needed for window plus res */
24602460
M = (fp_int*)XMALLOC(sizeof(fp_int)*((1 << winsize) + 1), NULL,
24612461
DYNAMIC_TYPE_BIGINT);
@@ -2482,7 +2482,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
24822482
/* now we need R mod m */
24832483
err = fp_montgomery_calc_normalization (res, P);
24842484
if (err != FP_OKAY) {
2485-
#ifndef WOLFSSL_NO_MALLOC
2485+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
24862486
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
24872487
#endif
24882488
return err;
@@ -2493,7 +2493,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
24932493
/* G > P so we reduce it first */
24942494
err = fp_mod(G, P, &M[1]);
24952495
if (err != FP_OKAY) {
2496-
#ifndef WOLFSSL_NO_MALLOC
2496+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
24972497
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
24982498
#endif
24992499
return err;
@@ -2503,7 +2503,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
25032503
}
25042504
err = fp_mulmod (&M[1], res, P, &M[1]);
25052505
if (err != FP_OKAY) {
2506-
#ifndef WOLFSSL_NO_MALLOC
2506+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25072507
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25082508
#endif
25092509
return err;
@@ -2516,14 +2516,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
25162516
err = fp_sqr (&M[(word32)(1 << (winsize - 1))],
25172517
&M[(word32)(1 << (winsize - 1))]);
25182518
if (err != FP_OKAY) {
2519-
#ifndef WOLFSSL_NO_MALLOC
2519+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25202520
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25212521
#endif
25222522
return err;
25232523
}
25242524
err = fp_montgomery_reduce_ex(&M[(word32)(1 << (winsize - 1))], P, mp, 0);
25252525
if (err != FP_OKAY) {
2526-
#ifndef WOLFSSL_NO_MALLOC
2526+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25272527
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25282528
#endif
25292529
return err;
@@ -2534,14 +2534,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
25342534
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
25352535
err = fp_mul(&M[x - 1], &M[1], &M[x]);
25362536
if (err != FP_OKAY) {
2537-
#ifndef WOLFSSL_NO_MALLOC
2537+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25382538
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25392539
#endif
25402540
return err;
25412541
}
25422542
err = fp_montgomery_reduce_ex(&M[x], P, mp, 0);
25432543
if (err != FP_OKAY) {
2544-
#ifndef WOLFSSL_NO_MALLOC
2544+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25452545
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25462546
#endif
25472547
return err;
@@ -2585,14 +2585,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
25852585
if (mode == 1 && y == 0) {
25862586
err = fp_sqr(res, res);
25872587
if (err != FP_OKAY) {
2588-
#ifndef WOLFSSL_NO_MALLOC
2588+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25892589
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25902590
#endif
25912591
return err;
25922592
}
25932593
err = fp_montgomery_reduce_ex(res, P, mp, 0);
25942594
if (err != FP_OKAY) {
2595-
#ifndef WOLFSSL_NO_MALLOC
2595+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
25962596
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
25972597
#endif
25982598
return err;
@@ -2610,14 +2610,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
26102610
for (x = 0; x < winsize; x++) {
26112611
err = fp_sqr(res, res);
26122612
if (err != FP_OKAY) {
2613-
#ifndef WOLFSSL_NO_MALLOC
2613+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26142614
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26152615
#endif
26162616
return err;
26172617
}
26182618
err = fp_montgomery_reduce_ex(res, P, mp, 0);
26192619
if (err != FP_OKAY) {
2620-
#ifndef WOLFSSL_NO_MALLOC
2620+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26212621
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26222622
#endif
26232623
return err;
@@ -2627,14 +2627,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
26272627
/* then multiply */
26282628
err = fp_mul(res, &M[bitbuf], res);
26292629
if (err != FP_OKAY) {
2630-
#ifndef WOLFSSL_NO_MALLOC
2630+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26312631
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26322632
#endif
26332633
return err;
26342634
}
26352635
err = fp_montgomery_reduce_ex(res, P, mp, 0);
26362636
if (err != FP_OKAY) {
2637-
#ifndef WOLFSSL_NO_MALLOC
2637+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26382638
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26392639
#endif
26402640
return err;
@@ -2653,14 +2653,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
26532653
for (x = 0; x < bitcpy; x++) {
26542654
err = fp_sqr(res, res);
26552655
if (err != FP_OKAY) {
2656-
#ifndef WOLFSSL_NO_MALLOC
2656+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26572657
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26582658
#endif
26592659
return err;
26602660
}
26612661
err = fp_montgomery_reduce_ex(res, P, mp, 0);
26622662
if (err != FP_OKAY) {
2663-
#ifndef WOLFSSL_NO_MALLOC
2663+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26642664
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26652665
#endif
26662666
return err;
@@ -2672,14 +2672,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
26722672
/* then multiply */
26732673
err = fp_mul(res, &M[1], res);
26742674
if (err != FP_OKAY) {
2675-
#ifndef WOLFSSL_NO_MALLOC
2675+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26762676
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26772677
#endif
26782678
return err;
26792679
}
26802680
err = fp_montgomery_reduce_ex(res, P, mp, 0);
26812681
if (err != FP_OKAY) {
2682-
#ifndef WOLFSSL_NO_MALLOC
2682+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
26832683
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
26842684
#endif
26852685
return err;
@@ -2699,7 +2699,7 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
26992699
/* swap res with Y */
27002700
fp_copy (res, Y);
27012701

2702-
#ifndef WOLFSSL_NO_MALLOC
2702+
#if defined(WOLFSSL_SMALL_STACK) && !defined(WOLFSSL_NO_MALLOC)
27032703
XFREE(M, NULL, DYNAMIC_TYPE_BIGINT);
27042704
#endif
27052705
return err;

0 commit comments

Comments
 (0)