Skip to content

SE050Sim: accept 0x81 TLV lengths in commands - #28

Merged
LinuxJedi merged 1 commit into
wolfSSL:mainfrom
LinuxJedi:se050sim-tlv-81-length
Nov 30, 2026
Merged

LinuxJedi merged 1 commit into
wolfSSL:mainfrom
LinuxJedi:se050sim-tlv-81-length

Conversation

@LinuxJedi

Copy link
Copy Markdown
Member

SE050Sim rejected command TLVs that use the one-byte extended length form (81 LL) with SW 0x6A80. Plug&Trust's tlvSet_u8buf() uses that form for every 128..255 byte value, so wolfSSL's SE050 hash offload (WOLFSSL_SE050_HASH) failed on its first 128-byte DigestUpdate chunk. Found while testing wolfSSL/wolfssl#12318.

How it was validated

  • Silicon first. Read-only raw-APDU probes on an SE051 (applet 7.2.0):
    • The applet accepts 81 LL in commands: DigestOneShot with 42 81 C8 and 200 bytes returns 0x9000 and the correct SHA-256.
    • It never sent 81 LL in a response. GetRandom (64 to 300 bytes), DigestOneShot, ReadIDList and ReadObject (16 to 869 bytes) all answered 41 82 HH LL, including at 128..255 bytes. Only a 1-byte flag (ReadIDList "more") used the short form.
  • cargo test passes (109 unit, 14 integration, 14 SCP03).
  • Dockerfile.wolfcrypt against wolfSSL with the PR above:
    • default flags: PASS.
    • -DWOLFSSL_SE050_HASH -DWOLFSSL_SE050_CRYPT: PASS. 795 commands carried a 43 81 TAG_3, for example DigestUpdate 42 02 00 01 43 81 80 plus 128 bytes, answered 90 00. Before this change that run needed a locally patched simulator. (Built with --disable-rng-autolock to avoid an unrelated fork-test deadlock in that wolfSSL branch.)

Changes

  • parse_tlvs() decodes lengths through a new parse_length() helper that takes the short, 81 LL and 82 HH LL forms, including longer than needed ones such as 81 00, which the real applet accepts. Truncated input is still TooShort; 0x80 and 0x83 and up are still InvalidLength.
  • The response encoder is unchanged (short below 128 bytes, else 82 HH LL) and now documents why it never emits 81 LL: silicon doesn't, and the Rust se050 crate used by the integration tests (LinuxJedi/nxp-se050 sim-compat) only parses short and 0x82 and loops forever on any other length byte.
  • Unit tests for the short, 0x81 and 0x82 forms, non-minimal lengths, mixed TLVs, truncated input, invalid length bytes and the encoder's 127/128/255/256 boundaries.

No other place in the simulator decodes TLV lengths; the T=1, policy and TCP framing lengths are separate formats.

parse_tlvs() only took the short and 0x82 length forms and answered a
0x81 LL length with 0x6A80. Plug&Trust's tlvSet_u8buf() uses 0x81 for
every 128..255 byte value, so wolfSSL's SE050 hash offload failed on
its first 128-byte DigestUpdate chunk.

Decode the length in a parse_length() helper that takes the short,
0x81 and 0x82 forms, including longer than needed ones such as 81 00,
which the real applet accepts.

The response encoder stays on short / 0x82. On an SE051 (applet 7.2.0)
GetRandom, ReadObject and DigestOneShot answer with 0x82 HH LL at every
value size tested (16 to 869 bytes, including 128 to 255), so the part
never sends 0x81 there. Plug&Trust parses all three forms; the Rust
se050 crate used by the integration tests only parses short and 0x82.

Add unit tests for the short, 0x81 and 0x82 forms, non-minimal
lengths, mixed TLVs, truncated input, invalid length bytes and the
encoder's 127/128/255/256 boundaries.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 14:21

Copilot AI 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.

🟢 Approval recommended

No active review issues were identified for the TLV parser’s expanded length support.

No open issues 🎉

What changed in this PR

This PR updates SE050Sim’s command TLV parser to accept 0x81 lengths, enabling Plug&Trust’s 128–255-byte values and resolving the reported wolfSSL hash-offload failure.

Changes:

  • Adds a length-decoding helper supporting short, 0x81, and 0x82 forms, including non-minimal encodings, while preserving malformed-input errors.
  • Adds tests for length boundaries, mixed encodings, truncation, and unsupported length forms.
  • Documents the existing response-encoding behavior without changing its output.
File Description
SE050Sim/​se050-sim/​src/​tlv.rs Adds command-length decoding support and regression tests; documents unchanged response encoding.

Review effort: Balanced

@LinuxJedi
LinuxJedi merged commit 3b71f78 into wolfSSL:main Nov 30, 2026
26 checks passed
@LinuxJedi
LinuxJedi deleted the se050sim-tlv-81-length branch November 30, 2026 14:53
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.

2 participants