Skip to content

Commit 9d01411

Browse files
authored
Merge pull request #6433 from SparkiDev/memusage_7
Memory usage: reduce stack usage
2 parents 5a59807 + 98a717e commit 9d01411

2 files changed

Lines changed: 33 additions & 17 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6767,9 +6767,9 @@ WARN_UNUSED_RESULT int AES_GCM_encrypt_C(
67676767
word32 partial = sz % AES_BLOCK_SIZE;
67686768
const byte* p = in;
67696769
byte* c = out;
6770-
ALIGN32 byte counter[AES_BLOCK_SIZE];
6771-
ALIGN32 byte initialCounter[AES_BLOCK_SIZE];
6772-
ALIGN32 byte scratch[AES_BLOCK_SIZE];
6770+
ALIGN16 byte counter[AES_BLOCK_SIZE];
6771+
ALIGN16 byte initialCounter[AES_BLOCK_SIZE];
6772+
ALIGN16 byte scratch[AES_BLOCK_SIZE];
67736773

67746774
if (ivSz == GCM_NONCE_MID_SZ) {
67756775
/* Counter is IV with bottom 4 bytes set to: 0x00,0x00,0x00,0x01. */
@@ -7289,10 +7289,10 @@ int WARN_UNUSED_RESULT AES_GCM_decrypt_C(
72897289
word32 partial = sz % AES_BLOCK_SIZE;
72907290
const byte* c = in;
72917291
byte* p = out;
7292-
ALIGN32 byte counter[AES_BLOCK_SIZE];
7293-
ALIGN32 byte scratch[AES_BLOCK_SIZE];
7294-
ALIGN32 byte Tprime[AES_BLOCK_SIZE];
7295-
ALIGN32 byte EKY0[AES_BLOCK_SIZE];
7292+
ALIGN16 byte counter[AES_BLOCK_SIZE];
7293+
ALIGN16 byte scratch[AES_BLOCK_SIZE];
7294+
ALIGN16 byte Tprime[AES_BLOCK_SIZE];
7295+
ALIGN16 byte EKY0[AES_BLOCK_SIZE];
72967296
sword32 res;
72977297

72987298
if (ivSz == GCM_NONCE_MID_SZ) {

wolfcrypt/src/sp_int.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12708,7 +12708,11 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1270812708
*/
1270912709
err = sp_mont_norm(t[1], m);
1271012710
if (err == MP_OKAY) {
12711-
err = _sp_mulmod(t[0], t[1], m, t[0]);
12711+
err = sp_mul(t[0], t[1], t[0]);
12712+
}
12713+
if (err == MP_OKAY) {
12714+
/* t[0] = t[0] mod m, temporary size has to be bigger than t[0]. */
12715+
err = _sp_div(t[0], m, NULL, t[0], t[0]->used + 1);
1271212716
}
1271312717
if (err == MP_OKAY) {
1271412718
/* 4. t[1] = t[0]
@@ -12886,7 +12890,11 @@ static int _sp_exptmod_mont_ex(const sp_int* b, const sp_int* e, int bits,
1288612890
err = sp_mont_norm(t[0], m);
1288712891
if (err == MP_OKAY) {
1288812892
/* 3. t[1] = ToMont(t[1]) */
12889-
err = _sp_mulmod(t[1], t[0], m, t[1]);
12893+
err = sp_mul(t[1], t[0], t[1]);
12894+
}
12895+
if (err == MP_OKAY) {
12896+
/* t[1] = t[1] mod m, temporary size has to be bigger than t[1]. */
12897+
err = _sp_div(t[1], m, NULL, t[1], t[1]->used + 1);
1289012898
}
1289112899

1289212900
/* 4. For i in 2..(2 ^ w) - 1 */
@@ -13491,16 +13499,13 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1349113499
sp_int* r)
1349213500
{
1349313501
int i = 0;
13494-
int c = 0;
13495-
int y = 0;
1349613502
int bits;
1349713503
int winBits;
1349813504
int preCnt;
1349913505
int err = MP_OKAY;
1350013506
int done = 0;
1350113507
sp_int* tr = NULL;
1350213508
sp_int* bm = NULL;
13503-
sp_int_digit mask;
1350413509
/* Maximum winBits is 6 and preCnt is (1 << (winBits - 1)). */
1350513510
#ifndef WOLFSSL_SP_NO_MALLOC
1350613511
DECL_DYN_SP_INT_ARRAY(t, m->used * 2 + 1, (1 << 5) + 2);
@@ -13532,8 +13537,6 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1353213537
}
1353313538
/* Top bit of exponent fixed as 1 for pre-calculated window. */
1353413539
preCnt = 1 << (winBits - 1);
13535-
/* Mask for calculating index into pre-computed table. */
13536-
mask = (sp_int_digit)preCnt - 1;
1353713540

1353813541
/* Allocate sp_ints for:
1353913542
* - pre-computation table
@@ -13573,16 +13576,21 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1357313576
}
1357413577

1357513578
if ((!done) && (err == MP_OKAY)) {
13579+
int y = 0;
13580+
int c = 0;
1357613581
sp_int_digit mp;
13577-
sp_int_digit n;
1357813582

1357913583
/* Calculate Montgomery multiplier for reduction. */
1358013584
_sp_mont_setup(m, &mp);
1358113585
/* Calculate Montgomery normalizer for modulus. */
1358213586
err = sp_mont_norm(t[0], m);
1358313587
if (err == MP_OKAY) {
1358413588
/* 2. Convert base to Montgomery form. */
13585-
err = _sp_mulmod(bm, t[0], m, bm);
13589+
err = sp_mul(bm, t[0], bm);
13590+
}
13591+
if (err == MP_OKAY) {
13592+
/* bm = bm mod m, temporary size has to be bigger than bm->used. */
13593+
err = _sp_div(bm, m, NULL, bm, bm->used + 1);
1358613594
}
1358713595
if (err == MP_OKAY) {
1358813596
/* Copy Montgomery form of base into first element of table. */
@@ -13608,6 +13616,10 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1360813616
* if less than windows bits in exponent, 1 in Montgomery form.
1360913617
*/
1361013618
if (err == MP_OKAY) {
13619+
sp_int_digit n;
13620+
/* Mask for calculating index into pre-computed table. */
13621+
sp_int_digit mask = (sp_int_digit)preCnt - 1;
13622+
1361113623
/* Find the top bit. */
1361213624
i = (bits - 1) >> SP_WORD_SHIFT;
1361313625
n = e->dp[i--];
@@ -13833,7 +13845,11 @@ static int _sp_exptmod_nct(const sp_int* b, const sp_int* e, const sp_int* m,
1383313845
err = sp_mont_norm(t[1], m);
1383413846
if (err == MP_OKAY) {
1383513847
/* 1. Convert base to Montgomery form. */
13836-
err = _sp_mulmod(t[0], t[1], m, t[0]);
13848+
err = sp_mul(t[0], t[1], t[0]);
13849+
}
13850+
if (err == MP_OKAY) {
13851+
/* t[0] = t[0] mod m, temporary size has to be bigger than t[0]. */
13852+
err = _sp_div(t[0], m, NULL, t[0], t[0]->used + 1);
1383713853
}
1383813854
if (err == MP_OKAY) {
1383913855
/* 2. Result starts as Montgomery form of base (assuming e > 0). */

0 commit comments

Comments
 (0)