ThePassionate opened a new pull request, #21939:
URL: https://github.com/apache/nuttx/pull/21939

   ## Summary
   
   The software RSA path is slow and not constant-time: on sim the `rsa` test 
needs ~25 minutes for its first case and does not finish the second one, and 
the plain bignum modular exponentiation leaks timing information about the 
private exponent.
   
   * **crypto: add constant-time RSA core from BearSSL** - new `crypto/rsa.c` 
with the RSA public and private operations taken from the BearSSL "i31" 
implementation: 31-bit word big integers, Montgomery multiplication and 
windowed modular exponentiation, all constant-time with respect to the operand 
values. `rsa_private()` uses the CRT form when p, q, dp, dq and qp are all 
given and falls back to x^d mod n otherwise, so a key that only carries n, e 
and d is handled as well. Integers are big-endian byte strings; the working 
buffers come from the heap (2-3 KB for a 4096-bit operation, too large for 
work-queue stacks) and `CRYPTO_RSA_MAX_BITS` (512-4096, default 4096) bounds 
the modulus size.
   * **crypto/cryptosoft: compute RSA through rsa.c** - `swcr_mod_exp()` 
(CRK_MOD_EXP) and `swcr_rsa_verify()` (CRK_RSA_PKCS15_VERIFY), the two RSA 
handlers that went through the generic bignum code, now run through the new 
core. The public ioctl slot layout and the comparison semantics of the verify 
path are unchanged; the verify comparison becomes constant-time and gains 
length bounds checks. `crypto/bn.c` stays in the build for now and can be 
removed separately once nothing references it.
   
   ## Impact
   
   * New crypto/rsa.c and Kconfig option `CRYPTO_RSA_MAX_BITS`; cryptosoft RSA 
handlers rewritten internally. No ioctl or ABI change.
   * All RSA operations become constant-time and fast; keys given as n/e/d only 
are supported without the CRT components.
   
   ## Testing
   
   Board sim:sim/crypto (Make and CMake builds), nuttx master de5bfd40404:
   
   The headline number - the `rsa` test (nuttx-apps) on plain master needs 
**1481 s for case 1 and does not finish case 2 within 25 minutes**; with this 
branch all three cases complete in **0.07 s** total:
   
   ```
   rsa test case 1 success      (encrypt/decrypt round trip)
   rsa test case 2 success      (sign/verify)
   rsa test case 3 success      (2048-bit n/e/d-only key, PKCS#1 v1.5 signature,
                                 cross-checked against a python cryptography
                                 reference implementation; a tampered signature
                                 is rejected - the visible CIOCKEY: Error 22)
   ```
   
   The rsa.c core was additionally validated on the host with gcc 
`-fsanitize=address,undefined` against python `pow()`: 42/42 random vectors 
across 512/1024/2048/3072/4096-bit moduli, e=3 and 65537, p<q, CRT and d-only 
keys, leading zeros, and x==n / x>n rejection.
   
   Regression: ecdsa (p256 keygen/sign/verify), dhm, hmac (25 vectors), hash 
(md5/sha1/sha224/sha256/sha384/sha512/rmd160, huge-block, unaligned), aescbc, 
chachapoly all pass unchanged. The `aescmac` 16-byte case fails identically on 
plain master - pre-existing, fixed separately in #21930.
   
   `tools/checkpatch.sh -c -u -m -g <base>..HEAD` passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to