diff --git a/wolfcrypt/src/tfm.c b/wolfcrypt/src/tfm.c index b163a35ff..04e639ff3 100644 --- a/wolfcrypt/src/tfm.c +++ b/wolfcrypt/src/tfm.c @@ -1710,9 +1710,13 @@ static int _fp_exptmod_ct(fp_int * G, fp_int * X, int digits, fp_int * P, static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) { fp_int *res; - fp_int *M; fp_digit buf, mp; int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize; +#ifndef WOLFSSL_NO_MALLOC + fp_int *M; +#else + fp_int M[(1 << 6) + 1]; +#endif /* find window size */ x = fp_count_bits (X); @@ -1733,12 +1737,14 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) return err; } +#ifndef WOLFSSL_NO_MALLOC /* only allocate space for what's needed for window plus res */ M = (fp_int*)XMALLOC(sizeof(fp_int)*((1 << winsize) + 1), NULL, DYNAMIC_TYPE_BIGINT); if (M == NULL) { return FP_MEM; } +#endif res = &M[(word32)(1 << winsize)]; /* init M array */ @@ -1774,7 +1780,9 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) fp_sqr (&M[(word32)(1 << (winsize - 1))], &M[(word32)(1 << (winsize - 1))]); err = fp_montgomery_reduce (&M[(word32)(1 << (winsize - 1))], P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } } @@ -1783,12 +1791,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { err = fp_mul(&M[x - 1], &M[1], &M[x]); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } err = fp_montgomery_reduce(&M[x], P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } } @@ -1830,12 +1842,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) if (mode == 1 && y == 0) { err = fp_sqr(res, res); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } fp_montgomery_reduce(res, P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } continue; @@ -1851,12 +1867,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = 0; x < winsize; x++) { err = fp_sqr(res, res); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } err = fp_montgomery_reduce(res, P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } } @@ -1864,12 +1884,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* then multiply */ err = fp_mul(res, &M[bitbuf], res); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } err = fp_montgomery_reduce(res, P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } @@ -1886,12 +1910,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) for (x = 0; x < bitcpy; x++) { err = fp_sqr(res, res); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } err = fp_montgomery_reduce(res, P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } @@ -1901,12 +1929,16 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* then multiply */ err = fp_mul(res, &M[1], res); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } err = fp_montgomery_reduce(res, P, mp); if (err != FP_OKAY) { +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; } } @@ -1924,7 +1956,9 @@ static int _fp_exptmod_nct(fp_int * G, fp_int * X, fp_int * P, fp_int * Y) /* swap res with Y */ fp_copy (res, Y); +#ifndef WOLFSSL_NO_MALLOC XFREE(M, NULL, DYNAMIC_TYPE_BIGINT); +#endif return err; }