Repository navigation
Conversation
|
There was a problem hiding this comment.
🟡 Changes recommended
Critical random-generation bugs with CHAR_BIT == 16 and SP_WORD_SIZE == 8 produce out-of-range or uninitialized digits, malformed candidates and bases, and potential infinite loops.
3 open issues
What changed in this PR
Extends wolfCrypt’s SP math support for targets with characters wider than 8 bits, such as TI C28x. Building on #12094, it distinguishes octet counts from memory sizes and fills digit storage with packed random data.
Changes:
- Adds
mp_rand_mem()and uses it for random digits, prime candidates, and Miller–Rabin bases. - Corrects SP size handling and adds regression tests for randomness, prime lengths, memory boundaries, and octet export.
- Includes
wolfmath.cin the C2000 compile script.
| File | Description |
|---|---|
wolfssl/wolfcrypt/wolfmath.h |
Declares the internal random-memory helper. |
wolfssl/wolfcrypt/sp_int.h |
Introduces SP_WORD_OCTETS and a conditional legacy alias. |
wolfcrypt/test/test.c |
Adds wide-character randomness, boundary, and export regression tests. |
wolfcrypt/src/wolfmath.c |
Packs random octets into memory cells and updates digit generation. |
wolfcrypt/src/sp_int.c |
Corrects size units and octet export; updates prime and random-base generation. |
tests/unit-mcdc/test_dh_fault_whitebox.c |
Updates a comment to reference SP_WORD_OCTETS. |
IDE/C2000/compile.sh |
Adds wolfmath.c to the compiled sources. |
Review effort: Balanced
d682f9b to
25b8dfa
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The mp_rand_mem() call bypasses the white-box RNG hook, making the allocation-fault sweep nondeterministic and allowing RNG allocations under WOLFSSL_SMALL_STACK to consume failures intended for Miller–Rabin.
1 open issue
3 resolved since last review
Review effort: Balanced
| while (trials > 0) { | ||
| /* Generate random trial number. */ | ||
| err = wc_RNG_GenerateBlock(rng, (byte*)b->dp, baseSz); | ||
| err = mp_rand_mem(rng, b->dp, WC_PACKED_CELLS(baseSz)); |
25b8dfa to
af28a74
Compare


Description
wc_RNG_GenerateBlock()puts one octet in eachbyte. Some functions use it to generate random data directly into SP digits:mp_rand()andmp_get_rand_digit(), used for RSA blinding andWOLFSSL_ECC_BLIND_K.sp_rand_prime(), used for DH and DSA parameter generation._sp_prime_random_trials(), used for the random bases of the Miller-Rabin test.This is not a problem on targets with an 8-bit
char. On targets with a 16-bitchar, such as the TI C28x, eachbyteholds an octet in its lower 8 bits, so the upper 8 bits of everybytein the digits are always zero.sp_rand_prime()also passes the length in octets as the length inbytes, so it writes twice the memory needed and sets the top bits of the candidate outside of the digits used.Depends on #12094, for the canary used in the new test.
Fixes zd#22792
Changes
Add
mp_rand_mem()towolfmath.c, which fills memory with random bits. Where acharis wider than 8 bits, it generates octets in small chunks and packsWC_OCTETS_PER_BYTEof them into eachbyte, lowest first. Where acharis 8 bits, it is one call towc_RNG_GenerateBlock(), so the random data in the digits is unchanged.Use
mp_rand_mem()in:mp_rand()andmp_get_rand_digit().sp_rand_prime()and_sp_prime_random_trials(), with the length inbytes fromWC_PACKED_CELLS().sp_rand_prime()now sets the top bits in thebyteand bit position of the last octet.Add tests to
mp_test():mp_test_rand(): every octet position of the digits frommp_rand()andmp_get_rand_digit()has a bit set.mp_test_canary_rand_prime(): the prime frommp_rand_prime()has the requested number of bits, and the canary after the digits is unchanged.Add
wolfmath.cto the files compiled byIDE/C2000/compile.sh.Testing
user_settings.hwithWOLFSSL_SP_MATH_ALL,WOLFSSL_PUBLIC_MP,WOLFSSL_KEY_GEN, RSA and ECC:mp_test()passes, including the new tests.CHAR_BIT16), both on Windows: the F283x CPU Cycle Accurate Simulator of CCS 5.5, and TI C2000 CGT 22.6.2.LTS (-v28 -ml -O2 --abi=coffabi), with a wolfCrypt-onlyuser_settings.h(WC_16BIT_CPU,WOLFSSL_SP_MATH_ALL,SP_WORD_SIZE 32,WOLFSSL_SP_ALLOW_16BIT_CPU,WOLFSSL_PUBLIC_MP,WOLFSSL_KEY_GEN, DH, ECC P-256,NO_ASN,NO_CERTS, test-only RNG seed):mp_test()passes with this PR.IDE/C2000/compile.shwith TI C2000 CGT 22.6.2.LTS on Windows: all files compile, includingwolfmath.c.Checklist