Skip to content

Validate key_to_wrap length in aes_key_wrap_with_padding#14665

Merged
reaperhulk merged 1 commit intopyca:mainfrom
Mrmaxmeier:fix/aes-kwp-empty-key-to-wrap
Apr 15, 2026
Merged

Validate key_to_wrap length in aes_key_wrap_with_padding#14665
reaperhulk merged 1 commit intopyca:mainfrom
Mrmaxmeier:fix/aes-kwp-empty-key-to-wrap

Conversation

@Mrmaxmeier
Copy link
Copy Markdown
Contributor

@Mrmaxmeier Mrmaxmeier commented Apr 15, 2026

Hi,

this PR resolves #14664:
RFC 5649 specifies that the plaintext must be between 1 and 2^32 octets. Previously, passing an empty key_to_wrap would cause _wrap_core to be called with r=[], which skips all AES operations and just returns an invalid wrapped key.

Wycheproof has a test for this that is marked as "invalid":

https://github.com/C2SP/wycheproof/blob/75ede73a39b8517b2a06c8115dfbcd364479796c/testvectors_v1/aes_kwp_test.json#L839-L849

I've updated the testing harness to allow aes_key_wrap_with_padding to return an error in this case. The initial CI run failed in that case. Unfortunately there doesn't seem to be a way to tell if unwrapping or wrapping the key should fail, which is why this wasn't caught by the wycheproof test and why the new logic is somewhat sloppy.

Thanks!

@Mrmaxmeier Mrmaxmeier force-pushed the fix/aes-kwp-empty-key-to-wrap branch from 1b1da40 to 97c6f47 Compare April 15, 2026 13:16
@gaynor-anthropic
Copy link
Copy Markdown

Can you add a direct unit test for this as well? (Sent from my $work github)

RFC 5649 specifies that the plaintext must be between 1 and 2^32
octets. Previously, passing an empty key_to_wrap would cause
_wrap_core to be called with r=[], which skips all AES operations
and returns an invalid wrapped key.
@Mrmaxmeier Mrmaxmeier force-pushed the fix/aes-kwp-empty-key-to-wrap branch from 97c6f47 to 33397a4 Compare April 15, 2026 14:31
Copy link
Copy Markdown
Member

@reaperhulk reaperhulk left a comment

Choose a reason for hiding this comment

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

Thanks!

@reaperhulk reaperhulk enabled auto-merge (squash) April 15, 2026 14:40
@reaperhulk reaperhulk merged commit 0397eee into pyca:main Apr 15, 2026
68 checks passed
@Mrmaxmeier Mrmaxmeier deleted the fix/aes-kwp-empty-key-to-wrap branch April 15, 2026 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

AES-KWP allows wrapping empty key_to_wrap, inconsistent with RFC 5649

3 participants