Skip to content

wc_pkcs11: carry the AES-CTR counter across PKCS#11 calls - #12154

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-18134
Open

yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f-18134

Conversation

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor

Problem

wc_AesCtrEncrypt() sends a PKCS#11-bound Aes to 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 leaves aes->reg, aes->tmp and aes->left exactly 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) and Pkcs11AesCtrAdd() (128-bit big-endian add, wrapping like IncrementAesCounter()).

Closes F-18134.

Tests

test_wc_Pkcs11_AesCtrStream (tests/api/test_pkcs11.c) runs wc_AesCtrEncrypt() against a mock token that does real single-part CTR, and compares with a one-shot software encryption.

Verification

  • New test fails before the fix (32+32 split), passes after; pkcs11 group passes.
  • ASan + UBSan clean; GCC -Werror clean on Linux (--enable-pkcs11 --enable-aesctr, --enable-all --enable-pkcs11); 32-bit i386 build and tests pass.
  • Mutation check: removing the wipe or committing the counter on failure each fails the test.

Not in this PR

  • Pkcs11AesCtrDecrypt() keeps the old behaviour. It is unreachable through wolfCrypt: wc_CryptoCb_AesCtrEncrypt() always sets enc = 1.
  • AES-CBC IV chaining in the PKCS#11 driver is handled separately.

- 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
@yosuke-wolfssl yosuke-wolfssl self-assigned this Nov 17, 2026
Copilot AI balanced review requested due to automatic review settings November 17, 2026 10:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@yosuke-wolfssl

Copy link
Copy Markdown
Contributor Author

@claude review

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +104 B, text +12,192 B (+4.4%, 294,768 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


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

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 how reg, tmp and left are written back. A tail only happens after the leftover bytes are used up, so setting left = 16 - rem is 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, matching IncrementAesCounter. 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 wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants