diff --git a/aes-kw/src/kw.rs b/aes-kw/src/kw.rs index d527ee2..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 { + 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,13 +171,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 `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; - if blocks_rem != 0 || blocks_len < 1 { + // The IV plus at least two semiblocks of key data. + if blocks_rem != 0 || blocks_len < 3 { return Err(Error::InvalidDataSize); } @@ -190,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 /// } /// @@ -215,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 79e7eb8..e51b8b4 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); } @@ -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, @@ -229,7 +238,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); } @@ -245,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 @@ -261,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 6506e42..f4f3d83 100644 --- a/aes-kw/tests/kw_tests.rs +++ b/aes-kw/tests/kw_tests.rs @@ -88,6 +88,19 @@ fn error_invalid_data_size() { let res = kek.unwrap_key(&output, &mut buf); assert_eq!(res, Err(Error::InvalidDataSize)); + // 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 0bb077d..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!( @@ -168,6 +188,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)); + + // 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); + assert_eq!(res, Err(Error::InvalidDataSize)); } #[test]