Repository navigation
aes-kw: reject empty keys and single-semiblock unwrap inputs #103
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
Merged
+81
−16
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,7 +98,7 @@ impl<C: BlockCipherEncrypt<BlockSize = U16>> AesKwp<C> { | |
| /// bytes (i.e. 8 bytes) longer than the length of `data`. | ||
| #[inline] | ||
| pub fn wrap_key<'a>(&self, key: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { | ||
| if key.len() > KWP_MAX_LEN { | ||
| if key.is_empty() || key.len() > KWP_MAX_LEN { | ||
| return Err(Error::InvalidDataSize); | ||
| } | ||
|
|
||
|
|
@@ -119,20 +119,29 @@ impl<C: BlockCipherEncrypt<BlockSize = U16>> AesKwp<C> { | |
|
|
||
| /// Wrap fixed-size key `key` and return wrapped key. | ||
| /// | ||
| /// The key must be non-empty; keys of 1 to 8 bytes use the single-block path. | ||
| /// | ||
| /// This method is roughly equivalent to: | ||
| /// ```ignore | ||
| /// const fn check_key_size(n: usize) -> usize { | ||
| /// assert!(n != 0); | ||
| /// 0 | ||
| /// } | ||
| /// | ||
| /// pub fn wrap_fixed_key<const N: usize>( | ||
| /// &self, | ||
| /// key: &[u8; N], | ||
| /// ) -> [u8; IV_LEN * (N.div_ceil(IV_LEN) + 1)] | ||
| /// where | ||
| /// [(); check_key_size(N)]: Sized, | ||
| /// { ... } | ||
| /// ``` | ||
| /// but uses [`hybrid_array::Array`][Array] instead of built-in arrays | ||
| /// to work around current limitations of the const generics system. | ||
| #[inline] | ||
| 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>, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| Le<N, KwpMaxLen>: NonZero, | ||
| Sum<N, IvLenM1>: Div<IvLen>, | ||
| Quot<Sum<N, IvLenM1>, IvLen>: Add<B1>, | ||
|
|
@@ -229,7 +238,8 @@ impl<C: BlockCipherDecrypt<BlockSize = U16>> AesKwp<C> { | |
| pub fn unwrap_key<'a>(&self, data: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { | ||
| let blocks_len = data.len() / IV_LEN; | ||
| let blocks_rem = data.len() % IV_LEN; | ||
| if blocks_rem != 0 || blocks_len < 1 || data.len() > KWP_MAX_LEN { | ||
| // The AIV plus at least one semiblock of padded key data. | ||
| if blocks_rem != 0 || blocks_len < 2 || data.len() > KWP_MAX_LEN { | ||
| return Err(Error::InvalidDataSize); | ||
| } | ||
|
|
||
|
|
@@ -245,12 +255,21 @@ impl<C: BlockCipherDecrypt<BlockSize = U16>> AesKwp<C> { | |
|
|
||
| /// Unwrap fixed-size wrapped key `wkey` and return resulting key. | ||
| /// | ||
| /// The resulting key must be non-empty. | ||
| /// | ||
| /// This method is roughly equivalent to: | ||
| /// ```ignore | ||
| /// const fn check_key_size(n: usize) -> usize { | ||
| /// assert!(n != 0); | ||
| /// 0 | ||
| /// } | ||
| /// | ||
| /// pub fn unwrap_fixed_key<const N: usize>( | ||
| /// &self, | ||
| /// wkey: &[u8; IV_LEN * (N.div_ceil(IV_LEN) + 1)], | ||
| /// ) -> [u8; N] | ||
| /// where | ||
| /// [(); check_key_size(N)]: Sized, | ||
| /// { ... } | ||
| /// ``` | ||
| /// but uses [`hybrid_array::Array`][Array] instead of built-in arrays | ||
|
|
@@ -261,7 +280,7 @@ impl<C: BlockCipherDecrypt<BlockSize = U16>> AesKwp<C> { | |
| wkey: &KwpWrappedKey<N>, | ||
| ) -> Result<Array<u8, N>, IntegrityCheckFailed> | ||
| where | ||
| N: ArraySize + Add<IvLenM1> + IsLess<KwpMaxLen>, | ||
| N: ArraySize + NonZero + Add<IvLenM1> + IsLess<KwpMaxLen>, | ||
| Le<N, KwpMaxLen>: NonZero, | ||
| Sum<N, IvLenM1>: Div<IvLen>, | ||
| Quot<Sum<N, IvLenM1>, IvLen>: Add<B1> + Mul<IvLen>, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.