Skip to content

crypto: add AES-192/256-CBC, cipher streaming, arbitrary GCM IV and AES-256-CMAC - #21957

Open
ThePassionate wants to merge 5 commits into
apache:masterfrom
ThePassionate:crypto-sym-capabilities
Open

ThePassionate wants to merge 5 commits into
apache:masterfrom
ThePassionate:crypto-sym-capabilities

Conversation

@ThePassionate

Copy link
Copy Markdown
Contributor

Summary

Four symmetric-crypto capabilities that the software crypto driver is missing, plus a style cleanup:

  • crypto/cryptosoft: add AES-192/256-CBC support - the CBC session selects the CRYPTO_AES_192_CBC / CRYPTO_AES_256_CBC variant by key length, so 24/32-byte keys stop being created as AES-128-CBC sessions.
  • crypto: make COP_FLAG_UPDATE reach the cipher descriptor - cryptodev_op() only set CRD_F_UPDATE on the hash descriptor, so a cipher session re-initialized its key stream/IV on every request and streaming a cipher through /dev/crypto was impossible. The flag now reaches the cipher descriptor, AES-CTR keeps a byte-level keystream tail across requests (8-byte chunks of a block work), and CBC/OFB/CFB carry the IV in the key schedule between update steps. Split requests must keep passing the IV, as the existing blfcbc test already does.
  • crypto: support any GCM IV length and split AAD/payload - CRYPTO_AES_GCM_16 was never accepted by CIOCGSESSION (the CTR session ABI carves a nonce out of the key). GCM now takes a plain 16/24/32-byte key, derives the pre-counter block through GHASH for non-96-bit IVs, and the authenc path is reworked to the three-step UPDATE_AAD / UPDATE / final flow with the MAC context kept in the session. The old prepended-IV adjustment in swcr_authenc() (the region guarded in crypto/cryptosoft: do not skip an IV byte in the CMAC authenc path #21930) is removed entirely; if crypto/cryptosoft: do not skip an IV byte in the CMAC authenc path #21930 lands first the rebase here is trivial. A zero-length update is a no-op instead of faulting.
  • crypto/cryptosoft: add AES-256-CMAC - new CRYPTO_AES_256_CMAC id (45) with the 256-bit CMAC variant; the 128/256 selection follows the key length.
  • crypto: fix nxstyle errors in chachapoly.c and xform.c - long-standing issues in the two files this series touches (required by the whole-file style check).

Impact

  • crypto/, include/crypto/ only. New algorithm id 45 (CRYPTO_ALGORITHM_MAX 44 -> 45); no existing id or ABI changes. CTR keeps its nonce-in-key session ABI, so existing users (aes_alt, aesctr test) are unaffected.
  • The GCM-16 key format changes from "key + nonce" to a plain key - GCM-16 sessions could not be created before, so there is no existing user to break.

Testing

Board sim:sim/crypto (nuttx master e9eefaa), with the companion test PR in nuttx-apps:

  • aescbc: 6 vectors (4x RFC 3602 AES-128 + NIST SP 800-38A 192/256), each as one-shot and as a 16-byte-block update chain - aescbc test vector 0..5 ok
  • aesctr: 9 vectors, each as one-shot and as an 8-byte-chunk update chain (keystream tail across block boundaries) - all OK
  • aesgcm: round trip plus known-answer check of ciphertext and tag for a 96-bit and an 8-byte IV (GCM spec test case 4 parameters; the 8-byte IV exercises the GHASH pre-counter block). The expected values match an independent host implementation (python cryptography) and the published spec values for the 96-bit case.
  • aescmac: 8/8 - the four 128-bit and four 256-bit NIST vectors (the 128-bit 16-byte case also passes here because the authenc rework removes the one-byte truncation that crypto/cryptosoft: do not skip an IV byte in the CMAC authenc path #21930 fixes)

Regression: des3cbc, aesxts, chacha20, chachapoly, hmac (35 vectors), hash (incl. huge-block and unaligned), crc32, pbkdf2, poly1305, castcbc, blfcbc, ecdsa (p256 keygen/sign/verify), dhm all pass unchanged. The rsa test does not finish on sim within 25 minutes - that is pre-existing master behavior (bn-based modular exponentiation), unchanged by this series and fixed by #21939.

tools/checkpatch.sh -c -u -m -g <base>..HEAD passes; tools/refresh.sh --silent sim:sim/crypto reports no defconfig change.

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


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

GUIDINGLI
GUIDINGLI previously approved these changes Nov 17, 2026
CRYPTO_AES_192_CBC and CRYPTO_AES_256_CBC are accepted by cryptodev and
the mbedtls aes_alt picks them for 24/32 byte keys, but cryptosoft only
registered the 128-bit variant, so such a session matched no driver and
failed with -EINVAL. enc_xform_aes already keys 128/192/256, so wire the
two ids through newsession, freesession, process and the advertised
algorithm table.

Signed-off-by: makejian <makejian@xiaomi.com>
cryptodev_op() only propagated COP_FLAG_UPDATE to the hash descriptor,
never to the cipher descriptor, so a cipher session restarted its
IV/counter through exf->reinit() on every chunked request and a split
operation never matched the one-shot result. Set CRD_F_UPDATE on crde
as well.

Give the plain AES xform a reinit method that keeps the running CBC IV
inside AES_CTX, keep the unused CTR keystream tail in the key schedule
so a request ending mid-block is continued by the next one, and copy the
OFB/CFB IV into the key schedule instead of storing a pointer to the
per-request descriptor, which goes stale once the request returns.
swcr_encdec() no longer writes the chain IV back through crp_iv for
ciphers with a reinit method: for those ivp still points at the input
IV, so the copy was a no-op that would fault on a read-only IV buffer.

Signed-off-by: makejian <makejian@xiaomi.com>
cryptodev rejected CRYPTO_AES_GCM_16, so GCM was unreachable through
/dev/crypto, and the GMAC session keys carried a salt convention that no
in-tree user exercises. Export it with plain 16/24/32-byte AES keys:

1) enc_xform reinit takes the IV length, so the cipher side derives its
   own counter block instead of swcr hard-coding the 96-bit layout.
2) aes_gcm_j0() derives J0 per NIST SP 800-38D 7.1, IV || 0^31 || 1 for
   a 96-bit IV and GHASH_H(IV || 0^s || 0^64 || [len(IV)]64) otherwise.
   aes_gmac_reinit() and aes_gcm_reinit() share it, and aes_gmac_final()
   no longer forces J0[15] = 1.
3) swcr_authenc() passes crp_ivlen to both reinit hooks and hands AAD to
   the MAC unpadded, so it may be split over several requests; an empty
   update closes the AAD before the first payload block, and the length
   block is computed from the accumulated aad/payload lengths. The MAC
   context is kept in the session across requests.
4) COP_FLAG_UPDATE_AAD is propagated to the hash descriptor, matching
   the userspace contract of an AAD-only first request.

The keystream tail keeping in aes_ctr_crypt() from the previous commit
makes a payload request ending mid-block continuable on the same
session.

Signed-off-by: makejian <makejian@xiaomi.com>
CRYPTO_AES_256_CMAC is a defined cryptodev algorithm id, but the
software backend only registered the 128-bit variant and
aes_cmac_setkey() hard-coded a 16-byte key schedule, so a 256-bit
session failed with -EINVAL and any 32-byte key would have been
truncated. Give aes_cmac_setkey() the key length, add
auth_hash_cmac_aes_256 and wire CRYPTO_AES_256_CMAC (new id 45)
through cryptodev, swcr newsession/freesession/authenc/process and the
advertised algorithm table.

Signed-off-by: makejian <makejian@xiaomi.com>
Blank line after declarations and brace placement only, in functions
touched by the nearby commits; no semantic change.

Signed-off-by: makejian <makejian@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Crypto Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants