Repository navigation
Conversation
There was a problem hiding this comment.
🔵 Needs a closer look
Reported tests cover only 8-bit-char hosts, leaving digit copying, shifting, and zeroization unverified on TI C28x after the cryptographic buffer-length changes.
No open issues 🎉
What changed in this PR
This PR corrects SP integer memory-operation sizes on targets with characters wider than 8 bits, such as TI C28x, while preserving behavior on 8-bit-character targets.
Changes:
- Separates digit sizes in character units (
SP_WORD_SIZEOF) from octet counts (SP_WORD_OCTETS). - Updates octet-based calculations and alignment declarations to use
SP_WORD_OCTETS. - Adds a compile-time check that
SP_WORD_SIZEOFmatchessizeof(sp_int_digit).
| File | Description |
|---|---|
wolfssl/wolfcrypt/sp_int.h |
Defines separate size macros, updates alignment, and adds a static assertion. |
wolfcrypt/test/test.c |
Keeps the oversized-input test expressed in octets. |
wolfcrypt/src/sp_int.c |
Uses octet counts for binary conversion and digit-capacity calculations. |
Review effort: Balanced
|
f8b0ff4 to
5acc102
Compare
5acc102 to
6f0da66
Compare
6f0da66 to
9acbfce
Compare
There was a problem hiding this comment.
🔵 Needs a closer look
The secure-erasure and memory-boundary changes on 16-bit-char targets need maintainer validation of the new canary tests with the C28x toolchain/runtime, since supplied results cover only 8-bit-char hosts.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
6ed5c35 to
5440197
Compare
|
Retest this please |
5440197 to
b23df82
Compare
b23df82 to
3f3bef7
Compare
3f3bef7 to
9934145
Compare


Description
SP_WORD_SIZEOFholds the number of octets in an SP digit. This is confusing, because its name suggests a size incharunits, likesizeof.SP_WORD_SIZEOFwas also used with two meanings: as a count of octets and as a size incharunits. Functions such assp_read_unsigned_bin()andsp_to_unsigned_bin_len_ct()work with octets, while memory functions such asForceZero(),XMEMCPY(),XMEMMOVE()andXMEMSET()take a size incharunits.This is not a problem on targets with an 8-bit
char, where the two values are the same. On targets with a 16-bitchar, such as the TI C28x, they differ. For example, withSP_WORD_SIZE32:char:SP_WORD_SIZEOFis 4 (octets) andsizeof(sp_int_digit)is 4 (chars).char:SP_WORD_SIZEOFis 4 (octets) andsizeof(sp_int_digit)is 2 (chars).As a result, the memory function calls cover twice the intended region on such targets. For example,
ForceZero()insp_clear()zeroes memory past the end ofdp[].Other places in
sp_int.calso assume an 8-bitchar:_sp_mul()usessizeof(i) * 8as the bit width ofi, so some partial products are skipped and the product can be wrong.sp_to_unsigned_bin_len()andsp_to_unsigned_bin_len_ct()store each octet of a digit with abytecast or mask that does not truncate to an octet, so output bytes can hold more than one octet.Fixes ZD#22792
Changes
Replace
SP_WORD_SIZEOFin wolfSSL according to how it is used:XALIGNED()and preprocessor conditionals: use the newSP_WORD_OCTETS. It is an integer literal, so it can be used inXALIGNED()and#if.ForceZero(),XMEMCPY(),XMEMMOVE(),XMEMSET()): usesizeof(sp_int_digit).SP_WORD_SIZEOFremains only as a deprecated alias ofSP_WORD_OCTETS. It is kept mainly for FIPS test code maintained outside of the wolfSSL repository, and also for user code. It is only defined where acharis 8 bits, where the two meanings agree, and it is not available when building wolfSSL itself.Fix the other places in
sp_int.c:_sp_mul(): useCHAR_BIT * sizeof(i)instead ofsizeof(i) * 8.sp_to_unsigned_bin_len()andsp_to_unsigned_bin_len_ct(): useWC_OCTET()instead of a(byte)cast or abytemask alone.Add regression tests to
mp_test()that check, with a canary after the digits, that the memory operations insp_clear(),sp_forcezero(),sp_copy(),sp_lshd(),sp_mul_2d(),sp_rshb()andsp_mod_2d()stay within the allocated digits. They can only detect the bug where acharis not 8 bits.Extend
mp_test_to_unsigned_bin_len_ct()to check the output against the expected octets, using a value with adjacent non-zero octets. It previously only compared againstmp_to_unsigned_bin_len(), with a value that has no adjacent non-zero octets.Testing
WOLFSSL_CRYPT_ONLY,WOLFSSL_ECC,WOLFSSL_SP_MATH_ALLandWOLFSSL_PUBLIC_MP:CMAKE_C_FLAGS="-DSP_WORD_SIZE=32 -DWOLFSSL_SP_INT_NEGATIVE":testwolfcryptpasses.SP_WORD_SIZE(64):testwolfcryptpasses.CHAR_BIT16): built with TI C2000 CGT 22.6.2.LTS (-v28 -ml -O2 --abi=coffabi) and run on the F283x CPU Cycle Accurate Simulator of CCS 5.5, both on Windows, 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, ECC P-256, SHA-2, AES,NO_ASN,NO_CERTS, test-only RNG seed):wolfcrypt_test()passes with this PR.sp_int.cbefore this PR, and pass with this PR.mp_test()fails before the_sp_mul(),sp_to_unsigned_bin_len()andsp_to_unsigned_bin_len_ct()fixes, and passes with them.Checklist