open-quantum-safe / liboqs

C library for prototyping and experimenting with quantum-resistant cryptography
https://openquantumsafe.org/
Other
1.82k stars 447 forks source link

huge stack usage #1824

Open songlingatpan opened 3 months ago

songlingatpan commented 3 months ago

My stack limit is 64kb. In multiple KEM implementation, it utilized much stack memory. It causes stack overflow. We can increase the stack size to 384kb to solve the issue. However, can we use heap to reduce stack usage? Thanks a lot.

0 0x00007ffff158871a in PQCLEAN_HQC192_CLEAN_vect_mul (o=<error reading variable: Cannot access memory at address 0x7ffff1ab69e8>, v1=<error reading variable: Cannot access memory at address 0x7ffff1ab69e0>,

v2=<error reading variable: Cannot access memory at address 0x7ffff1ab69d8>) at ../src/kem/hqc/pqclean_hqc-192_clean/gf2x.c:191

1 0x00007ffff1588961 in PQCLEAN_HQC192_CLEAN_hqc_pke_keygen (pk=0xd3e075dd3c "", sk=0xd3e075c93c "") at ../src/kem/hqc/pqclean_hqc-192_clean/hqc.c:51

2 0x00007ffff1588dd9 in PQCLEAN_HQC192_CLEAN_crypto_kem_keypair (pk=0xd3e075dd3c "", sk=0xd3e075c93c "") at ../src/kem/hqc/pqclean_hqc-192_clean/kem.c:31

3 0x00007ffff1586498 in OQS_KEM_hqc_192_keypair (public_key=0xd3e075dd3c "", secret_key=0xd3e075c93c "") at ../src/kem/hqc/kem_hqc_192.c:38

4 0x00007ffff19da46f in OQS_KEM_keypair (kem=0xd3e0b24100, public_key=0xd3e075dd3c "", secret_key=0xd3e075c93c "") at ../src/kem/kem.c:518

5 0x00007ffff1f2c9f5 in oqsx_key_gen_oqs (gen_kem=, key=0xd3e05d0a00) at ../oqsprov/oqsprov_keys.c:1685

6 oqsx_key_gen_oqs (gen_kem=, key=0xd3e05d0a00) at ../oqsprov/oqsprov_keys.c:1682

Some big stack usages as below: void PQCLEAN_HQC192_CLEAN_vect_mul(uint64_t o, const uint64_t v1, const uint64_t *v2) { uint64_t stack[VEC_N_SIZE_64 << 3] = {0}; uint64_t o_karat[VEC_N_SIZE_64 << 1] = {0};

SWilson4 commented 2 months ago

We do note large stack usage of some algorithms (i.e., McEliece) in the README and other documentation. However, our cutoff for what we consider "large" is (I believe) ~1 MB.

We don't currently provide any mechanism for using heap over stack. To do so would require us to modify upstream code, which would require significant maintenance effort on our part.