Skip to content

aes-kw: reject empty keys and single-semiblock unwrap inputs - #103

Merged
tarcieri merged 2 commits into
RustCrypto:masterfrom
DicksonWu654:aes-kw-min-length
Nov 17, 2026
Merged

tarcieri merged 2 commits into
RustCrypto:masterfrom
DicksonWu654:aes-kw-min-length

Conversation

@DicksonWu654

Copy link
Copy Markdown
Contributor

KW and KWP unwrap_key accept an 8-byte input. With no key data the unwrap loop runs zero rounds, so the integrity check compares the input with the default IV directly and returns Ok with an empty key without using the KEK. Wrapping an empty key also produces just the IV.

This requires at least one semiblock of key data on wrap and two semiblocks (IV plus data) on unwrap, and adds the cases to the existing error_invalid_data_size tests in kw_tests.rs and kwp_tests.rs. The KW test fails without the change and all aes-kw tests pass with it (cargo test --all-features), and cargo fmt --check is clean.

Reported privately as GHSA-38fr-39m3-52f8; @tarcieri said it doesn't need to stay embargoed.

KW and KWP unwrap accepted an 8-byte input. With no key data the
unwrap loop runs zero rounds, so the integrity check compares the
input against the default IV directly and returns Ok with an empty
key without using the KEK. Wrapping an empty key likewise produced
just the IV.

Require at least one semiblock of key data on wrap and two semiblocks
(IV plus data) on unwrap, and add regression cases to the existing
InvalidDataSize tests.
@tarcieri

Copy link
Copy Markdown
Member

It looks like this doesn't cover (un)wrap_fixed_key and probably should.

Also it seems like there's a disparity between plaintexts and ciphertexts. Based on my reading of RFC3394 the minimum plaintext key size is 128-bits or two blocks.

@DicksonWu654

Copy link
Copy Markdown
Contributor Author

Thanks Tony, good catches on both! I pushed an update:

  • KW now needs at least 16 bytes of plaintext (two semiblocks, per RFC 3394) and at least 24 bytes of wrapped input, in both the slice and the fixed-size APIs. The fixed-size ones get it as an N >= U16 bound.
  • KWP keeps the 1 to 8 byte single-block path, since RFC 5649 allows it, but the fixed-size APIs now require N: NonZero to match the runtime check on empty keys.
  • I added boundary tests for both, plus a one-byte KWP round trip. Tests, clippy and docs pass.

Comment thread aes-kw/src/kw.rs
pub fn wrap_fixed_key<N>(&self, key: &Array<u8, N>) -> KwWrappedKey<N>
where
N: ArraySize + NonZero + Add<IvLen> + Rem<IvLen>,
N: ArraySize + NonZero + Add<IvLen> + Rem<IvLen> + IsGreaterOrEqual<U16>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hmm, the bounds change is technically breaking. It may not actually matter in practice, though.

We could also just bump the minor version to v0.4.

Comment thread aes-kw/src/kwp.rs
pub fn wrap_fixed_key<N>(&self, key: &Array<u8, N>) -> KwpWrappedKey<N>
where
N: ArraySize + Add<IvLenM1> + IsLess<KwpMaxLen>,
N: ArraySize + NonZero + Add<IvLenM1> + IsLess<KwpMaxLen>,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same here re: bounds changes, though I'm not sure it matters if the thing it breaks is buggy anyway.

@tarcieri tarcieri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Going to go ahead and merge this despite the breaking changes. I think we can just bump the minor version to be safe.

@tarcieri
tarcieri merged commit 061a910 into RustCrypto:master Nov 17, 2026
9 checks passed
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