Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always set IV length for AES CCM ciphers #2245

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

lwestlund
Copy link

This fixes an issue where the IV length would not be set if the length
was equal to the recommended length. The issue shows up at least when an
IV of length 12 (which is returned by t.iv_len()) is used with the
AES256 CCM cipher, as OpenSSL defaults the IV length to 7 bytes 1 and it
would not be correctly set to 12.

Closes #2244.

Footnotes

  1. https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption

@sfackler
Copy link
Owner

Yeah it unfortunately looks like we can't just unconditionally do this :(

@lwestlund
Copy link
Author

Yeah it unfortunately looks like we can't just unconditionally do this :(

No it looks like it ain't that easy 😞

Based on the OpenSSL wiki on AES with CCM, I still think that a change is warranted to make 12 byte IVs work so I'm gonna self review another proposal!

Comment on lines 629 to 630
if let (Some(iv), Some(_iv_len)) = (iv, t.iv_len()) {
ctx.set_iv_length(iv.len())?;
Copy link
Author

Choose a reason for hiding this comment

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

What do we think about this instead? I reintroduced the length check and added specific handling for the AES CCM ciphers, which are the ones with the IV length issue.

Suggested change
if let (Some(iv), Some(_iv_len)) = (iv, t.iv_len()) {
ctx.set_iv_length(iv.len())?;
if let (Some(iv), Some(iv_len)) = (iv, t.iv_len()) {
if iv.len() != iv_len
|| matches!(
t.nid(),
Nid::AES_128_CCM | Nid::AES_192_CCM | Nid::AES_256_CCM
)
{
ctx.set_iv_length(iv.len())?;
}

Copy link
Owner

Choose a reason for hiding this comment

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

I think I hate it, but that's just OpenSSL I guess :P

Copy link
Owner

Choose a reason for hiding this comment

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

Looks like boringssl's not happy with it though.

Copy link
Author

Choose a reason for hiding this comment

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

I waited for you to give your opinion before pushing and running all of the CI. Now I pushed in 20ce889!

Copy link
Author

Choose a reason for hiding this comment

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

Also, agree that it's not very pretty but it should (hopefully!) get the job done!

This fixes an issue where the IV length would not be set if the length
was equal to the recommended length. The issue shows up at least when an
IV of length 12 (which is returned by `t.iv_len()`) is used with the
AES256 CCM cipher, as OpenSSL defaults the IV length to 7 bytes [^1] and it
would not be correctly set to 12.

[^1]: https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption

Closes sfackler#2244.
@lwestlund lwestlund force-pushed the fix/always-set-cipher-iv-len branch from 20ce889 to 542b783 Compare June 7, 2024 07:48
@lwestlund lwestlund changed the title Always set IV length for ciphers that use an IV Always set IV length for AES CCM ciphers Jun 7, 2024
@lwestlund
Copy link
Author

@sfackler please have another look at this!

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.

Encrypt AES 256 CCM with 12 byte IV silently uses 7 byte IV
2 participants