Skip to content

Fill digits with random bits on targets where a char is wider than 8 bit - #12140

Open
kojiws wants to merge 2 commits into
wolfSSL:masterfrom
kojiws:fix_rng_fill_digits
Open

kojiws wants to merge 2 commits into
wolfSSL:masterfrom
kojiws:fix_rng_fill_digits

Conversation

@kojiws

@kojiws kojiws commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

Description

wc_RNG_GenerateBlock() puts one octet in each byte. Some functions use it to generate random data directly into SP digits:

  • mp_rand() and mp_get_rand_digit(), used for RSA blinding and WOLFSSL_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-bit char, such as the TI C28x, each byte holds an octet in its lower 8 bits, so the upper 8 bits of every byte in the digits are always zero. sp_rand_prime() also passes the length in octets as the length in bytes, 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() to wolfmath.c, which fills memory with random bits. Where a char is wider than 8 bits, it generates octets in small chunks and packs WC_OCTETS_PER_BYTE of them into each byte, lowest first. Where a char is 8 bits, it is one call to wc_RNG_GenerateBlock(), so the random data in the digits is unchanged.

Use mp_rand_mem() in:

  • mp_rand() and mp_get_rand_digit().
  • sp_rand_prime() and _sp_prime_random_trials(), with the length in bytes from WC_PACKED_CELLS(). sp_rand_prime() now sets the top bits in the byte and bit position of the last octet.

Add tests to mp_test():

  • mp_test_rand(): every octet position of the digits from mp_rand() and mp_get_rand_digit() has a bit set.
  • mp_test_canary_rand_prime(): the prime from mp_rand_prime() has the requested number of bits, and the canary after the digits is unchanged.

Add wolfmath.c to the files compiled by IDE/C2000/compile.sh.

Testing

  • Linux (x86_64, gcc 13.3), with a user_settings.h with WOLFSSL_SP_MATH_ALL, WOLFSSL_PUBLIC_MP, WOLFSSL_KEY_GEN, RSA and ECC:
    • mp_test() passes, including the new tests.
  • TI C28x (CHAR_BIT 16), 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-only user_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.
    • The new tests fail before this PR, and pass with this PR.
  • IDE/C2000/compile.sh with TI C2000 CGT 22.6.2.LTS on Windows: all files compile, including wolfmath.c.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

Copilot AI balanced review requested due to automatic review settings November 16, 2026 10:17
@kojiws kojiws self-assigned this Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

gcc-arm-cortex-m0plus

  • FLASH: .text +20 B (+0.0%, 71,311 B / 262,144 B, total: 27% used)

gcc-arm-cortex-m3

  • FLASH: .text +16 B (+0.0%, 132,289 B / 262,144 B, total: 50% used)

gcc-arm-cortex-m4-dtls13

  • FLASH: .text +64 B (+0.0%, 197,344 B / 1,048,576 B, total: 19% used)

gcc-arm-cortex-m4-min-ecc

  • FLASH: .text +64 B (+0.1%, 68,561 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-sp-math

  • FLASH: .text +64 B (+0.1%, 68,561 B / 262,144 B, total: 26% used)

gcc-arm-cortex-m4-tls12

  • FLASH: .text +64 B (+0.0%, 133,065 B / 262,144 B, total: 51% used)

gcc-arm-cortex-m7-tls13

  • FLASH: .text +64 B (+0.0%, 251,970 B / 262,144 B, total: 96% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.c in 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

Comment thread wolfcrypt/src/sp_int.c Resolved
Comment thread wolfcrypt/src/sp_int.c Resolved
Comment thread wolfcrypt/src/wolfmath.c Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread wolfcrypt/src/sp_int.c
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));
@kojiws
kojiws force-pushed the fix_rng_fill_digits branch from 25b8dfa to af28a74 Compare November 17, 2026 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants