From 45065355cd189b03016d3e89e823cd742b00082e Mon Sep 17 00:00:00 2001 From: Dickson Wu Date: Sun, 15 Nov 2026 13:58:24 -0500 Subject: [PATCH 1/2] aes-kw: reject empty keys and single-semiblock unwrap inputs 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. --- aes-kw/src/kw.rs | 7 ++++--- aes-kw/src/kwp.rs | 5 +++-- aes-kw/tests/kw_tests.rs | 7 +++++++ aes-kw/tests/kwp_tests.rs | 6 ++++++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/aes-kw/src/kw.rs b/aes-kw/src/kw.rs index d527ee2..0db9a17 100644 --- a/aes-kw/src/kw.rs +++ b/aes-kw/src/kw.rs @@ -81,7 +81,7 @@ impl> AesKw { #[inline] pub fn wrap_key<'a>(&self, key: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { let blocks_rem = key.len() % IV_LEN; - if blocks_rem != 0 { + if blocks_rem != 0 || key.is_empty() { return Err(Error::InvalidDataSize); } @@ -168,13 +168,14 @@ impl> AesKw { /// /// Returns slice which points to `buf` and contains unwrapped data. /// - /// Length of `data` must be multiple of [`IV_LEN`] and bigger than zero. + /// Length of `data` must be multiple of [`IV_LEN`] and at least `2 * IV_LEN`. /// Length of `buf` must be bigger or equal to `data.len()`. #[inline] pub fn unwrap_key<'a>(&self, wkey: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { let blocks_len = wkey.len() / IV_LEN; let blocks_rem = wkey.len() % IV_LEN; - if blocks_rem != 0 || blocks_len < 1 { + // The IV plus at least one semiblock of key data. + if blocks_rem != 0 || blocks_len < 2 { return Err(Error::InvalidDataSize); } diff --git a/aes-kw/src/kwp.rs b/aes-kw/src/kwp.rs index 79e7eb8..1101443 100644 --- a/aes-kw/src/kwp.rs +++ b/aes-kw/src/kwp.rs @@ -98,7 +98,7 @@ impl> AesKwp { /// 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); } @@ -229,7 +229,8 @@ impl> AesKwp { 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); } diff --git a/aes-kw/tests/kw_tests.rs b/aes-kw/tests/kw_tests.rs index 6506e42..a4e1712 100644 --- a/aes-kw/tests/kw_tests.rs +++ b/aes-kw/tests/kw_tests.rs @@ -88,6 +88,13 @@ fn error_invalid_data_size() { let res = kek.unwrap_key(&output, &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); + // An empty key has nothing to wrap, and a single semiblock has no key data. + let res = kek.wrap_key(&[], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); + + let res = kek.unwrap_key(&[0u8; 8], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kek.unwrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); } diff --git a/aes-kw/tests/kwp_tests.rs b/aes-kw/tests/kwp_tests.rs index 0bb077d..c048804 100644 --- a/aes-kw/tests/kwp_tests.rs +++ b/aes-kw/tests/kwp_tests.rs @@ -168,6 +168,12 @@ fn error_invalid_data_size() { assert_eq!(res, Err(Error::InvalidDataSize)); let res = kwp.unwrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); + + // An empty key has nothing to wrap, and a single semiblock has no key data. + let res = kwp.wrap_key(&[], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kwp.unwrap_key(&[0u8; 8], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); } #[test] From a7f6fea2983fef137db8575344f1da33b9f364bf Mon Sep 17 00:00:00 2001 From: Dickson Wu Date: Mon, 16 Nov 2026 16:02:08 +0000 Subject: [PATCH 2/2] aes-kw: enforce minimum key lengths for KW and KWP --- aes-kw/src/kw.rs | 32 +++++++++++++++++++------------- aes-kw/src/kwp.rs | 22 ++++++++++++++++++++-- aes-kw/tests/kw_tests.rs | 8 +++++++- aes-kw/tests/kwp_tests.rs | 22 +++++++++++++++++++++- 4 files changed, 67 insertions(+), 17 deletions(-) diff --git a/aes-kw/src/kw.rs b/aes-kw/src/kw.rs index 0db9a17..0347228 100644 --- a/aes-kw/src/kw.rs +++ b/aes-kw/src/kw.rs @@ -5,7 +5,7 @@ use aes::cipher::{ Array, Block, BlockCipherDecrypt, BlockCipherEncrypt, array::ArraySize, common::{InnerInit, InnerUser}, - typenum::{Mod, NonZero, Sum, U16, Zero}, + typenum::{GrEq, IsGreaterOrEqual, Mod, NonZero, Sum, U16, Zero}, }; /// Default Initial Value for AES-KW as defined in RFC3394 ยง 2.2.3.1. @@ -76,12 +76,12 @@ impl> AesKw { /// /// Returns slice which points to `buf` and contains wrapped data. /// - /// Length of `data` must be multiple of [`IV_LEN`] and bigger than zero. - /// Length of `buf` must be bigger or equal to `data.len() + IV_LEN`. + /// Length of `key` must be a multiple of [`IV_LEN`] and at least 16 bytes. + /// Length of `buf` must be at least `key.len() + IV_LEN`. #[inline] pub fn wrap_key<'a>(&self, key: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { let blocks_rem = key.len() % IV_LEN; - if blocks_rem != 0 || key.is_empty() { + if blocks_rem != 0 || key.len() < 2 * IV_LEN { return Err(Error::InvalidDataSize); } @@ -97,10 +97,12 @@ impl> AesKw { /// Wrap fixed-size key `key` and return wrapped key. /// + /// The key must be a multiple of 8 bytes and at least 16 bytes long. + /// /// This method is roughly equivalent to: /// ```ignore /// const fn check_key_size(n: usize) -> usize { - /// assert!(n != 0 && n % IV_LEN == 0); + /// assert!(n >= 16 && n % IV_LEN == 0); /// 0 /// } /// @@ -117,9 +119,10 @@ impl> AesKw { #[inline] pub fn wrap_fixed_key(&self, key: &Array) -> KwWrappedKey where - N: ArraySize + NonZero + Add + Rem, + N: ArraySize + NonZero + Add + Rem + IsGreaterOrEqual, Sum: ArraySize, Mod: Zero, + GrEq: NonZero, { let mut buf = KwWrappedKey::::default(); self.wrap_key_trusted(key, &mut buf); @@ -168,14 +171,14 @@ impl> AesKw { /// /// Returns slice which points to `buf` and contains unwrapped data. /// - /// Length of `data` must be multiple of [`IV_LEN`] and at least `2 * IV_LEN`. - /// Length of `buf` must be bigger or equal to `data.len()`. + /// Length of `wkey` must be a multiple of [`IV_LEN`] and at least 24 bytes. + /// Length of `buf` must be at least `wkey.len() - IV_LEN`. #[inline] pub fn unwrap_key<'a>(&self, wkey: &[u8], buf: &'a mut [u8]) -> Result<&'a [u8], Error> { let blocks_len = wkey.len() / IV_LEN; let blocks_rem = wkey.len() % IV_LEN; - // The IV plus at least one semiblock of key data. - if blocks_rem != 0 || blocks_len < 2 { + // The IV plus at least two semiblocks of key data. + if blocks_rem != 0 || blocks_len < 3 { return Err(Error::InvalidDataSize); } @@ -191,12 +194,14 @@ impl> AesKw { Ok(buf) } - /// Unwrap key in `data` and return unwrapped key. + /// Unwrap key in `wkey` and return unwrapped key. + /// + /// The wrapped key must be at least 24 bytes long. /// /// This method is roughly equivalent to: /// ```ignore /// const fn check_key_size(n: usize) -> usize { - /// assert!(n != 0 && n % IV_LEN == 0); + /// assert!(n >= 16 && n % IV_LEN == 0); /// 0 /// } /// @@ -216,9 +221,10 @@ impl> AesKw { wkey: &KwWrappedKey, ) -> Result, IntegrityCheckFailed> where - N: ArraySize + NonZero + Add + Rem, + N: ArraySize + NonZero + Add + Rem + IsGreaterOrEqual, Sum: ArraySize, Mod: Zero, + GrEq: NonZero, { let mut buf = Array::::default(); self.unwrap_key_trusted(wkey, &mut buf)?; diff --git a/aes-kw/src/kwp.rs b/aes-kw/src/kwp.rs index 1101443..e51b8b4 100644 --- a/aes-kw/src/kwp.rs +++ b/aes-kw/src/kwp.rs @@ -119,12 +119,21 @@ impl> AesKwp { /// 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( /// &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 @@ -132,7 +141,7 @@ impl> AesKwp { #[inline] pub fn wrap_fixed_key(&self, key: &Array) -> KwpWrappedKey where - N: ArraySize + Add + IsLess, + N: ArraySize + NonZero + Add + IsLess, Le: NonZero, Sum: Div, Quot, IvLen>: Add, @@ -246,12 +255,21 @@ impl> AesKwp { /// 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( /// &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 @@ -262,7 +280,7 @@ impl> AesKwp { wkey: &KwpWrappedKey, ) -> Result, IntegrityCheckFailed> where - N: ArraySize + Add + IsLess, + N: ArraySize + NonZero + Add + IsLess, Le: NonZero, Sum: Div, Quot, IvLen>: Add + Mul, diff --git a/aes-kw/tests/kw_tests.rs b/aes-kw/tests/kw_tests.rs index a4e1712..f4f3d83 100644 --- a/aes-kw/tests/kw_tests.rs +++ b/aes-kw/tests/kw_tests.rs @@ -88,13 +88,19 @@ fn error_invalid_data_size() { let res = kek.unwrap_key(&output, &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); - // An empty key has nothing to wrap, and a single semiblock has no key data. + // KW requires two semiblocks of key data and one semiblock for the IV. let res = kek.wrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kek.wrap_key(&[0u8; 8], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kek.unwrap_key(&[0u8; 8], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kek.unwrap_key(&[0u8; 16], &mut buf); + assert_eq!(res, Err(Error::InvalidDataSize)); + let res = kek.unwrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); } diff --git a/aes-kw/tests/kwp_tests.rs b/aes-kw/tests/kwp_tests.rs index c048804..8fe2c5a 100644 --- a/aes-kw/tests/kwp_tests.rs +++ b/aes-kw/tests/kwp_tests.rs @@ -38,6 +38,26 @@ test_aes_kwp!( hex!("afbeb0f07dfbf5419200f2ccb50bb24f") ); +#[test] +fn wrap_unwrap_one_byte_key() { + let key = hex!("000102030405060708090A0B0C0D0E0F"); + let kwp = KwpAes128::new(&key.into()); + let input = [0x42]; + let mut wrapped_buf = [0u8; 16]; + let wrapped = kwp.wrap_key(&input, &mut wrapped_buf).unwrap(); + let mut unwrapped_buf = [0u8; 8]; + assert_eq!(kwp.unwrap_key(wrapped, &mut unwrapped_buf).unwrap(), input); + + let wrapped_fixed = kwp.wrap_fixed_key::((&input).into()); + assert_eq!(wrapped_fixed.as_slice(), wrapped); + assert_eq!( + kwp.unwrap_fixed_key::(&wrapped_fixed) + .unwrap() + .as_slice(), + input + ); +} + // These test vectors were obtained using NIST's CAVP system test_aes_kwp!( @@ -169,7 +189,7 @@ fn error_invalid_data_size() { let res = kwp.unwrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); - // An empty key has nothing to wrap, and a single semiblock has no key data. + // Empty plaintext is invalid, and a single ciphertext semiblock is too short. let res = kwp.wrap_key(&[], &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); let res = kwp.unwrap_key(&[0u8; 8], &mut buf);