Skip to content

Optimized KWP implementation. - #4

Open
90meter wants to merge 3 commits into
dogtagpki:COOLKEY_GP211_BRANCH_JCARD_3.0.4from
90meter:COOLKEY_GP211_BRANCH_JCARD_3.0.4
Open

Optimized KWP implementation.#4
90meter wants to merge 3 commits into
dogtagpki:COOLKEY_GP211_BRANCH_JCARD_3.0.4from
90meter:COOLKEY_GP211_BRANCH_JCARD_3.0.4

Conversation

@90meter

@90meter 90meter commented Apr 23, 2026

Copy link
Copy Markdown

This change optimizes the KWP unwrapping implementation to do as much work as possible in transient memory. Because of this change, the iobuf buffer was increased to 2800 to support 4096 keys since they have to now fit entirely in iobuf during the KWP process.

The core operation is as the following:

  1. First do some simple validation on arguments.
  2. For the special case where N==1, do a single round of ECB. Skip to step 5.
  3. Copy the entire input object (from EEPROM) into the iobuf (transient memory).
  4. Do the entire KWP decryption operations manually, without using the Unsigned64 class, since those are also not in transient memory.
  5. If nothing went wrong, validate the now-decrypted AIV and check for valid padding.
  6. Copy the decrypted buffer from iobuf to the output buffer.
  7. Wipe the iobuf.

The main optimizations come from doing all the work in transient memory, as well as not using the Unsigned64 classes.

One downside is that DecryptAESKWP will not work if iobuf is passed as either the input buffer or the output buffer, however, the TPS currently does not do that, so I don't believe it is a major concern. If it is an issue, a separate transient buffer can be allocated just for the KWP work.

Performance Comparison (4096 keys)

Operation 1.5.65cbf5a6 Proposed Change
SecureImportKeyEncrypted APDU (1 key) ~162 seconds ~7 seconds
Full RHCS Enrollment (3 keys, all server-side keygen) ~529 seconds ~65 seconds

Benchmarks were done on a SC230.

@coderabbitai

coderabbitai Bot commented Apr 23, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 977dfa9d-9054-4786-88d8-2a28a377f03a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the applet's build ID and replaces the existing AES Key Wrap with Padding (KWP) implementation with a new DecryptAESKWP method, supported by an increased iobuf allocation. Review feedback highlights that DecryptAESKWP should return the decrypted plaintext length rather than a boolean to ensure keybuf_size is correctly updated and padding is not processed as key data. Additionally, the implementation is missing a lower bound check for plaintext length required by RFC 5649 and lacks safeguards against data corruption if iobuf is passed as an input or output buffer.

Comment thread applet/src/com/redhat/ckey/applet/CardEdge.java Outdated
Comment thread applet/src/com/redhat/ckey/applet/CardEdge.java Outdated
Comment thread applet/src/com/redhat/ckey/applet/CardEdge.java Outdated
90meter and others added 2 commits April 23, 2026 09:41
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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.

1 participant