Skip to content

Commit 081b839

Browse files
authored
Merge pull request #8779 from douzzer/20250515-smallstack-refactors-and-tests
20250515-smallstack-refactors-and-tests
2 parents a1b6442 + 4018689 commit 081b839

7 files changed

Lines changed: 202 additions & 20 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Stack Size warnings
2+
3+
# START OF COMMON SECTION
4+
on:
5+
push:
6+
branches: [ 'master', 'main', 'release/**' ]
7+
pull_request:
8+
branches: [ '*' ]
9+
10+
concurrency:
11+
group: ${{ github.workflow }}-${{ github.ref }}
12+
cancel-in-progress: true
13+
# END OF COMMON SECTION
14+
15+
jobs:
16+
build_library:
17+
strategy:
18+
matrix:
19+
config: [
20+
# defaults, noasm
21+
'--disable-asm',
22+
23+
# defaults + native PQ, no asm
24+
'--disable-asm --enable-experimental --enable-kyber=yes,original --enable-lms --enable-xmss --enable-dilithium',
25+
26+
# all-crypto + native PQ, no asm
27+
'--disable-asm --enable-all-crypto --enable-experimental --enable-kyber=yes,original --enable-lms --enable-xmss --enable-dilithium',
28+
29+
# defaults, intelasm + sp-asm
30+
'--enable-intelasm --enable-sp-asm',
31+
32+
# defaults + native PQ, intelasm + sp-asm
33+
'--enable-intelasm --enable-sp-asm --enable-experimental --enable-kyber=yes,original --enable-lms --enable-xmss --enable-dilithium',
34+
35+
# all-crypto + native PQ, intelasm + sp-asm
36+
'--enable-intelasm --enable-sp-asm --enable-all-crypto --enable-experimental --enable-kyber=yes,original --enable-lms --enable-xmss --enable-dilithium'
37+
]
38+
name: build library
39+
if: github.repository_owner == 'wolfssl'
40+
runs-on: ubuntu-22.04
41+
# This should be a safe limit for the tests to run.
42+
timeout-minutes: 6
43+
steps:
44+
- uses: actions/checkout@v4
45+
name: Checkout wolfSSL
46+
47+
- name: install_multilib
48+
run: sudo apt-get install -y gcc-multilib
49+
50+
- name: Build wolfCrypt with smallstack and stack depth warnings, and run testwolfcrypt
51+
run: |
52+
./autogen.sh || $(exit 2)
53+
echo "running ./configure ... ${{ matrix.config }}"
54+
./configure --enable-cryptonly --disable-cryptocb --disable-testcert --enable-smallstack --enable-smallstackcache --enable-crypttests --disable-benchmark --disable-examples --with-max-rsa-bits=16384 --enable-stacksize=verbose CFLAGS="-Wframe-larger-than=2048 -Wstack-usage=4096 -DWOLFSSL_TEST_MAX_RELATIVE_STACK_BYTES=8192 -DTEST_ALWAYS_RUN_TO_END" ${{ matrix.config }} || $(exit 3)
55+
make -j 4 || $(exit 4)
56+
./wolfcrypt/test/testwolfcrypt

wolfcrypt/src/asn.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24682,7 +24682,11 @@ WOLFSSL_API int wc_GetSubjectPubKeyInfoDerFromCert(const byte* certDer,
2468224682
byte* pubKeyDer,
2468324683
word32* pubKeyDerSz)
2468424684
{
24685-
DecodedCert cert;
24685+
#ifdef WOLFSSL_SMALL_STACK
24686+
DecodedCert* cert;
24687+
#else
24688+
DecodedCert cert[1];
24689+
#endif
2468624690
int ret;
2468724691
word32 startIdx;
2468824692
word32 idx;
@@ -24693,16 +24697,22 @@ WOLFSSL_API int wc_GetSubjectPubKeyInfoDerFromCert(const byte* certDer,
2469324697
return BAD_FUNC_ARG;
2469424698
}
2469524699

24700+
#ifdef WOLFSSL_SMALL_STACK
24701+
cert = (DecodedCert*)XMALLOC(sizeof(*cert), NULL, DYNAMIC_TYPE_TMP_BUFFER);
24702+
if (cert == NULL)
24703+
return MEMORY_E;
24704+
#endif
24705+
2469624706
length = 0;
2469724707
badDate = 0;
2469824708

24699-
wc_InitDecodedCert(&cert, certDer, certDerSz, NULL);
24709+
wc_InitDecodedCert(cert, certDer, certDerSz, NULL);
2470024710

2470124711
/* Parse up to the SubjectPublicKeyInfo */
24702-
ret = wc_GetPubX509(&cert, 0, &badDate);
24712+
ret = wc_GetPubX509(cert, 0, &badDate);
2470324713
if (ret >= 0) {
2470424714
/* Save the starting index of SubjectPublicKeyInfo */
24705-
startIdx = cert.srcIdx;
24715+
startIdx = cert->srcIdx;
2470624716

2470724717
/* Get the length of the SubjectPublicKeyInfo sequence */
2470824718
idx = startIdx;
@@ -24728,7 +24738,11 @@ WOLFSSL_API int wc_GetSubjectPubKeyInfoDerFromCert(const byte* certDer,
2472824738
}
2472924739

2473024740
*pubKeyDerSz = length;
24731-
wc_FreeDecodedCert(&cert);
24741+
wc_FreeDecodedCert(cert);
24742+
24743+
#ifdef WOLFSSL_SMALL_STACK
24744+
XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
24745+
#endif
2473224746

2473324747
return ret;
2473424748
}

wolfcrypt/src/dilithium.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6152,7 +6152,11 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
61526152

61536153
/* Step 11: Start rejection sampling loop */
61546154
do {
6155+
#ifdef WOLFSSL_SMALL_STACK
6156+
byte *w1e = NULL;
6157+
#else
61556158
byte w1e[DILITHIUM_MAX_W1_ENC_SZ];
6159+
#endif
61566160
sword32* w = w1;
61576161
sword32* y_ntt = z;
61586162
sword32* cs2 = ct0;
@@ -6182,11 +6186,20 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
61826186
if (valid) {
61836187
#endif
61846188
/* Step 15: Encode w1. */
6185-
dilithium_vec_encode_w1(w1, params->k, params->gamma2, w1e);
6186-
/* Step 15: Hash mu and encoded w1.
6187-
* Step 32: Hash is stored in signature. */
6188-
ret = dilithium_hash256(&key->shake, mu, DILITHIUM_MU_SZ,
6189-
w1e, params->w1EncSz, commit, params->lambda / 4);
6189+
#ifdef WOLFSSL_SMALL_STACK
6190+
w1e = (byte *)XMALLOC(DILITHIUM_MAX_W1_ENC_SZ, key->heap,
6191+
DYNAMIC_TYPE_DILITHIUM);
6192+
if (w1e == NULL)
6193+
ret = MEMORY_E;
6194+
if (ret == 0)
6195+
#endif
6196+
{
6197+
dilithium_vec_encode_w1(w1, params->k, params->gamma2, w1e);
6198+
/* Step 15: Hash mu and encoded w1.
6199+
* Step 32: Hash is stored in signature. */
6200+
ret = dilithium_hash256(&key->shake, mu, DILITHIUM_MU_SZ,
6201+
w1e, params->w1EncSz, commit, params->lambda / 4);
6202+
}
61906203
if (ret == 0) {
61916204
/* Step 17: Compute c from first 256 bits of commit. */
61926205
ret = dilithium_sample_in_ball(params->level, &key->shake,
@@ -6237,6 +6250,10 @@ static int dilithium_sign_with_seed_mu(dilithium_key* key,
62376250
params->gamma2, params->omega, h) >= 0);
62386251
}
62396252
}
6253+
6254+
#ifdef WOLFSSL_SMALL_STACK
6255+
XFREE(w1e, key->heap, DYNAMIC_TYPE_DILITHIUM);
6256+
#endif
62406257
}
62416258

62426259
if (!valid) {

wolfcrypt/src/ecc.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3915,7 +3915,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
39153915
#endif
39163916
int i, err;
39173917
#ifdef WOLFSSL_SMALL_STACK_CACHE
3918-
ecc_key key;
3918+
ecc_key *key = NULL;
39193919
#endif
39203920
mp_digit mp;
39213921

@@ -3942,10 +3942,13 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
39423942
XMEMSET(M, 0, sizeof(M));
39433943

39443944
#ifdef WOLFSSL_SMALL_STACK_CACHE
3945-
err = ecc_key_tmp_init(&key, heap);
3945+
key = (ecc_key *)XMALLOC(sizeof(*key), heap, DYNAMIC_TYPE_ECC);
3946+
if (key == NULL)
3947+
return MEMORY_E;
3948+
err = ecc_key_tmp_init(key, heap);
39463949
if (err != MP_OKAY)
39473950
goto exit;
3948-
R->key = &key;
3951+
R->key = key;
39493952
#endif /* WOLFSSL_SMALL_STACK_CACHE */
39503953

39513954
/* alloc ram for window temps */
@@ -3958,7 +3961,7 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
39583961
goto exit;
39593962
}
39603963
#ifdef WOLFSSL_SMALL_STACK_CACHE
3961-
M[i]->key = &key;
3964+
M[i]->key = key;
39623965
#endif
39633966
}
39643967

@@ -4000,7 +4003,8 @@ int wc_ecc_mulmod_ex2(const mp_int* k, ecc_point* G, ecc_point* R, mp_int* a,
40004003
}
40014004
#ifdef WOLFSSL_SMALL_STACK_CACHE
40024005
R->key = NULL;
4003-
ecc_key_tmp_final(&key, heap);
4006+
ecc_key_tmp_final(key, heap);
4007+
XFREE(key, heap, DYNAMIC_TYPE_ECC);
40044008
#endif /* WOLFSSL_SMALL_STACK_CACHE */
40054009

40064010
return err;

wolfcrypt/src/wc_mlkem.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,8 @@ static MLKEM_NOINLINE int mlkemkey_decapsulate(MlKemKey* key, byte* m,
11441144
sword16* w;
11451145
unsigned int k = 0;
11461146
unsigned int compVecSz;
1147-
#if !defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC)
1147+
#if defined(WOLFSSL_SMALL_STACK) || \
1148+
(!defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC))
11481149
sword16* u = NULL;
11491150
#else
11501151
sword16 u[(WC_ML_KEM_MAX_K + 1) * MLKEM_N];
@@ -1198,7 +1199,8 @@ static MLKEM_NOINLINE int mlkemkey_decapsulate(MlKemKey* key, byte* m,
11981199
break;
11991200
}
12001201

1201-
#if !defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC)
1202+
#if defined(WOLFSSL_SMALL_STACK) || \
1203+
(!defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC))
12021204
if (ret == 0) {
12031205
/* Allocate dynamic memory for a vector and a polynomial. */
12041206
u = (sword16*)XMALLOC((k + 1) * MLKEM_N * sizeof(sword16), key->heap,
@@ -1254,7 +1256,8 @@ static MLKEM_NOINLINE int mlkemkey_decapsulate(MlKemKey* key, byte* m,
12541256
/* Step 8: return m */
12551257
}
12561258

1257-
#if !defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC)
1259+
#if defined(WOLFSSL_SMALL_STACK) || \
1260+
(!defined(USE_INTEL_SPEEDUP) && !defined(WOLFSSL_NO_MALLOC))
12581261
/* Dispose of dynamically memory allocated in function. */
12591262
XFREE(u, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
12601263
#endif

wolfcrypt/src/wc_mlkem_poly.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2274,14 +2274,31 @@ void mlkem_decapsulate(const sword16* s, sword16* w, sword16* u,
22742274
static int mlkem_gen_matrix_k2_avx2(sword16* a, byte* seed, int transposed)
22752275
{
22762276
int i;
2277+
#ifdef WOLFSSL_SMALL_STACK
2278+
byte *rand = NULL;
2279+
word64 *state = NULL;
2280+
#else
22772281
byte rand[4 * GEN_MATRIX_SIZE + 2];
22782282
word64 state[25 * 4];
2283+
#endif
22792284
unsigned int ctr0;
22802285
unsigned int ctr1;
22812286
unsigned int ctr2;
22822287
unsigned int ctr3;
22832288
byte* p;
22842289

2290+
#ifdef WOLFSSL_SMALL_STACK
2291+
rand = (byte*)XMALLOC(4 * GEN_MATRIX_SIZE + 2, NULL,
2292+
DYNAMIC_TYPE_TMP_BUFFER);
2293+
state = (word64*)XMALLOC(sizeof(word64) * 25 * 4, NULL,
2294+
DYNAMIC_TYPE_TMP_BUFFER);
2295+
if ((rand == NULL) || (state == NULL)) {
2296+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2297+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2298+
return MEMORY_E;
2299+
}
2300+
#endif
2301+
22852302
/* Loading 64 bits, only using 48 bits. Loading 2 bytes more than used. */
22862303
rand[4 * GEN_MATRIX_SIZE + 0] = 0xff;
22872304
rand[4 * GEN_MATRIX_SIZE + 1] = 0xff;
@@ -2345,6 +2362,11 @@ static int mlkem_gen_matrix_k2_avx2(sword16* a, byte* seed, int transposed)
23452362
p, XOF_BLOCK_SIZE);
23462363
}
23472364

2365+
#ifdef WOLFSSL_SMALL_STACK
2366+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2367+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2368+
#endif
2369+
23482370
return 0;
23492371
}
23502372
#endif
@@ -2365,14 +2387,31 @@ static int mlkem_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
23652387
{
23662388
int i;
23672389
int k;
2390+
#ifdef WOLFSSL_SMALL_STACK
2391+
byte *rand = NULL;
2392+
word64 *state = NULL;
2393+
#else
23682394
byte rand[4 * GEN_MATRIX_SIZE + 2];
23692395
word64 state[25 * 4];
2396+
#endif
23702397
unsigned int ctr0;
23712398
unsigned int ctr1;
23722399
unsigned int ctr2;
23732400
unsigned int ctr3;
23742401
byte* p;
23752402

2403+
#ifdef WOLFSSL_SMALL_STACK
2404+
rand = (byte*)XMALLOC(4 * GEN_MATRIX_SIZE + 2, NULL,
2405+
DYNAMIC_TYPE_TMP_BUFFER);
2406+
state = (word64*)XMALLOC(sizeof(word64) * 25 * 4, NULL,
2407+
DYNAMIC_TYPE_TMP_BUFFER);
2408+
if ((rand == NULL) || (state == NULL)) {
2409+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2410+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2411+
return MEMORY_E;
2412+
}
2413+
#endif
2414+
23762415
/* Loading 64 bits, only using 48 bits. Loading 2 bytes more than used. */
23772416
rand[4 * GEN_MATRIX_SIZE + 0] = 0xff;
23782417
rand[4 * GEN_MATRIX_SIZE + 1] = 0xff;
@@ -2473,6 +2512,11 @@ static int mlkem_gen_matrix_k3_avx2(sword16* a, byte* seed, int transposed)
24732512
XOF_BLOCK_SIZE);
24742513
}
24752514

2515+
#ifdef WOLFSSL_SMALL_STACK
2516+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2517+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2518+
#endif
2519+
24762520
return 0;
24772521
}
24782522
#endif
@@ -2492,14 +2536,31 @@ static int mlkem_gen_matrix_k4_avx2(sword16* a, byte* seed, int transposed)
24922536
{
24932537
int i;
24942538
int k;
2539+
#ifdef WOLFSSL_SMALL_STACK
2540+
byte *rand = NULL;
2541+
word64 *state = NULL;
2542+
#else
24952543
byte rand[4 * GEN_MATRIX_SIZE + 2];
24962544
word64 state[25 * 4];
2545+
#endif
24972546
unsigned int ctr0;
24982547
unsigned int ctr1;
24992548
unsigned int ctr2;
25002549
unsigned int ctr3;
25012550
byte* p;
25022551

2552+
#ifdef WOLFSSL_SMALL_STACK
2553+
rand = (byte*)XMALLOC(4 * GEN_MATRIX_SIZE + 2, NULL,
2554+
DYNAMIC_TYPE_TMP_BUFFER);
2555+
state = (word64*)XMALLOC(sizeof(word64) * 25 * 4, NULL,
2556+
DYNAMIC_TYPE_TMP_BUFFER);
2557+
if ((rand == NULL) || (state == NULL)) {
2558+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2559+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2560+
return MEMORY_E;
2561+
}
2562+
#endif
2563+
25032564
/* Loading 64 bits, only using 48 bits. Loading 2 bytes more than used. */
25042565
rand[4 * GEN_MATRIX_SIZE + 0] = 0xff;
25052566
rand[4 * GEN_MATRIX_SIZE + 1] = 0xff;
@@ -2563,6 +2624,11 @@ static int mlkem_gen_matrix_k4_avx2(sword16* a, byte* seed, int transposed)
25632624
a += 4 * MLKEM_N;
25642625
}
25652626

2627+
#ifdef WOLFSSL_SMALL_STACK
2628+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2629+
XFREE(state, NULL, DYNAMIC_TYPE_TMP_BUFFER);
2630+
#endif
2631+
25662632
return 0;
25672633
}
25682634
#endif /* WOLFSSL_KYBER1024 || WOLFSSL_WC_ML_KEM_1024 */
@@ -4120,7 +4186,17 @@ static int mlkem_get_noise_k2_avx2(MLKEM_PRF_T* prf, sword16* vec1,
41204186
sword16* vec2, sword16* poly, byte* seed)
41214187
{
41224188
int ret = 0;
4189+
#ifdef WOLFSSL_SMALL_STACK
4190+
byte *rand;
4191+
#else
41234192
byte rand[4 * PRF_RAND_SZ];
4193+
#endif
4194+
4195+
#ifdef WOLFSSL_SMALL_STACK
4196+
rand = (byte*)XMALLOC(4 * PRF_RAND_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4197+
if (rand == NULL)
4198+
return MEMORY_E;
4199+
#endif
41244200

41254201
mlkem_get_noise_x4_eta3_avx2(rand, seed);
41264202
mlkem_cbd_eta3_avx2(vec1 , rand + 0 * PRF_RAND_SZ);
@@ -4137,6 +4213,10 @@ static int mlkem_get_noise_k2_avx2(MLKEM_PRF_T* prf, sword16* vec1,
41374213
ret = mlkem_get_noise_eta2_avx2(prf, poly, seed);
41384214
}
41394215

4216+
#ifdef WOLFSSL_SMALL_STACK
4217+
XFREE(rand, NULL, DYNAMIC_TYPE_TMP_BUFFER);
4218+
#endif
4219+
41404220
return ret;
41414221
}
41424222
#endif

0 commit comments

Comments
 (0)