Repository navigation
wc_pkcs11: carry the AES-CTR counter across PKCS#11 calls - #12154
yosuke-wolfssl wants to merge 1 commit into
Conversation
- Pkcs11AesCtrEncrypt() uses the unused keystream in aes->tmp first, encrypts whole blocks on the token from aes->reg, then updates aes->reg, aes->tmp and aes->left. - Output is written only after the mechanism check and key lookup. - New helpers: Pkcs11AesCtrOp() and Pkcs11AesCtrAdd(). - tests: test_wc_Pkcs11_AesCtrStream Issue: F-18134
|
@claude review |
|
There was a problem hiding this comment.
I reviewed this PR and didn't find any bugs. It changes AES-CTR keystream and counter handling in the PKCS#11 crypto callback, so a human familiar with the PKCS#11 driver should still look at it.
What was reviewed:
Pkcs11AesCtrEncrypt: how leftover keystream is used, the split into whole blocks and a tail, and howreg,tmpandleftare written back. A tail only happens after the leftover bytes are used up, so settingleft = 16 - remis correct.- Error paths: the output is wiped only after the token operation has started. When the mechanism is unavailable or key creation fails, the output and state are left alone, so the software fallback still works. In-place buffers are handled correctly by
xorbufout. Pkcs11AesCtrAdd: the carry propagates and wraps at 2^128, matchingIncrementAesCounter. I only read the new mock-token test and did not run it.
Extended reasoning...
The change rewrites AES-CTR encryption in wolfcrypt/src/wc_pkcs11.c so the counter and partial-block keystream carry over between PKCS#11 calls. It adds two helpers and a mock-token test in tests/api/test_pkcs11.c. This is crypto code: a mistake in the keystream state could reuse keystream or produce wrong ciphertext. Reading the code turned up no defects. I deferred because the change is to cryptographic state handling, and the earlier automated reviewer's comment may still need a reply.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #12154
Scan targets checked: wolfcrypt-src, wolfcrypt-bugs, wolfssl-bugs
Not reviewed (no scan target covers them): tests/api/test_pkcs11.h
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Max
Problem
wc_AesCtrEncrypt()sends a PKCS#11-boundAesto the crypto callback and returns its result directly, so the software counter and partial-block bookkeeping (aes->reg,aes->tmp,aes->left) never runs.Fix (
wolfcrypt/src/wc_pkcs11.c)Pkcs11AesCtrEncrypt()now leavesaes->reg,aes->tmpandaes->leftexactly as the software path does, so any split of a stream gives the same bytes as one call.New helpers:
Pkcs11AesCtrOp()(one operation from a counter block, output length checked) andPkcs11AesCtrAdd()(128-bit big-endian add, wrapping likeIncrementAesCounter()).Closes F-18134.
Tests
test_wc_Pkcs11_AesCtrStream(tests/api/test_pkcs11.c) runswc_AesCtrEncrypt()against a mock token that does real single-part CTR, and compares with a one-shot software encryption.Verification
pkcs11group passes.-Werrorclean on Linux (--enable-pkcs11 --enable-aesctr,--enable-all --enable-pkcs11); 32-bit i386 build and tests pass.Not in this PR
Pkcs11AesCtrDecrypt()keeps the old behaviour. It is unreachable through wolfCrypt:wc_CryptoCb_AesCtrEncrypt()always setsenc = 1.