Skip to content

crypto: fix the RSA parameter mismatch and the out-of-bounds key parameter - #22862

Open
ThePassionate wants to merge 2 commits into
apache:masterfrom
ThePassionate:crypto-rsa-abi-fixes
Open

ThePassionate wants to merge 2 commits into
apache:masterfrom
ThePassionate:crypto-rsa-abi-fixes

Conversation

@ThePassionate

@ThePassionate ThePassionate commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two defects in the RSA path of the software crypto backend, both reachable
only through the rpmsg and vhost transports, which is why the in-tree tests
have not caught them.

1. The modulus and the exponent are read from each other's parameter.
swcr_rsa_load() took a component number and a parameter number separately,
so a caller could read a component from a parameter other than its own slot,
and swcr_mod_exp() used that to read the exponent from param[1] and the
modulus from param[2]. The rpmsg crypto server and the vhost-crypto backend
both fill param[1] with n and param[2] with e, so a CRK_MOD_EXP request
that arrives over either transport is computed with the two swapped.

The fix numbers a component by the parameter that carries it, which removes
the mismatch by construction: param[1] is always n, param[2] is always
the exponent. swcr_rsa_verify() read its own third layout and is aligned
with the transports as well.

2. CRK_MAXPARAM is too small for an RSA private key, and the transports
already write past the array.
A private key spans nine input parameters
(the operand, n, e, d, p, q, dp, dq, qp) and a request returning a result
needs a tenth. With CRK_MAXPARAM at 8 the valid indices are 0..7, yet
rpmsg_crypto_server.c, rpmsg_crypto.c and vhost-crypto.c all assign
krp_param[8]. In struct cryptkop that write lands exactly on
krp_callback, the function pointer the framework calls on completion:

sizeof(crparam)     = 16
krp_param offset    = 40
krp_param end       = 168
krp_callback offset = 168      <-- krp_param[8] starts here

Impact

CRK_MOD_EXP over rpmsg or vhost produced a wrong result; a private-key
request over either transport corrupted a function pointer. Local requests
through /dev/crypto were not affected by the first defect as long as the
caller matched what cryptosoft read, which the in-tree tests did.

The parameter order is now the one the transports use, so two tests that
passed the exponent first need the matching change; that goes through
apache/nuttx-apps#4291 and the two should land together.

Testing

sim:crypto, built and run on Linux:

  • rsa — 3/3 cases pass, including the tampered-signature rejection
  • aescbc, aesctr, aesxts, des3cbc, hmac, chachapoly — all pass
  • each commit builds on its own, 0 warnings and 0 errors
  • tools/checkpatch.sh -c -u -m -g clean over the series

Number the RSA components by the cryptkop parameter that carries them
and load a component always from krp_param[slot] into comp[slot].
The old two-argument swcr_rsa_load() allowed a caller to read a
component from a parameter other than its own slot, and swcr_mod_exp()
used that freedom to read the exponent from param[1] and the modulus
from param[2]; the rpmsg and vhost transports fill param[1] with n and
param[2] with e, so their CRK_MOD_EXP requests reached the software
backend with the modulus and the exponent swapped.

swcr_rsa_verify() read the same slots from yet another layout on top of
that; align it with the transports: params 0 = signature, 1 = n,
2 = e, 3 = padding, 4 = hash.

Signed-off-by: makejian <makejian@xiaomi.com>
An RSA private key spans nine input parameters - the operand, n, e, d,
p, q, dp, dq and qp - and a request that returns a result needs a tenth
for the output.  The rpmsg crypto server and the vhost-crypto backend
already fill krp_param[8] with qp, which is one past the end of an
eight-entry array: the write lands on krp_callback, the function
pointer the framework calls when the request completes.

Signed-off-by: makejian <makejian@xiaomi.com>
@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Crypto Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants