Skip to content

Commit

Permalink
implement KeyInit for aes-kw::Kek and BeltKwp
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Nov 7, 2024
1 parent 92cd9e1 commit 42891b9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 26 deletions.
41 changes: 26 additions & 15 deletions aes-kw/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ pub use error::{Error, Result};

use aes::cipher::{
array::Array,
typenum::{Unsigned, U16, U24, U32},
typenum::{U16, U24, U32},
Block, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt, BlockCipherEncBackend,
BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, KeyInit,
BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser,
};

pub use aes::cipher::{self, Key, KeyInit, KeySizeUser};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

Expand Down Expand Up @@ -138,26 +140,14 @@ where
type Error = Error;

fn try_from(value: &[u8]) -> Result<Self> {
if value.len() == Aes::KeySize::to_usize() {
Ok(Kek::new(
&Array::try_from(value).expect("size invariant violated"),
))
} else {
Err(Error::InvalidKekSize { size: value.len() })
}
Self::new_from_slice(value).map_err(|_| Error::InvalidKekSize { size: value.len() })
}
}

impl<Aes> Kek<Aes>
where
Aes: KeyInit + BlockSizeUser<BlockSize = U16> + BlockCipherEncrypt + BlockCipherDecrypt,
{
/// Constructs a new Kek based on the appropriate raw key material.
pub fn new(key: &Array<u8, Aes::KeySize>) -> Self {
let cipher = Aes::new(key);
Kek { cipher }
}

/// AES Key Wrap, as defined in RFC 3394.
///
/// The `out` buffer will be overwritten, and must be exactly [`IV_LEN`]
Expand Down Expand Up @@ -424,6 +414,27 @@ where
}
}

impl<Aes> KeyInit for Kek<Aes>
where
Aes: KeyInit + BlockSizeUser<BlockSize = U16> + BlockCipherEncrypt + BlockCipherDecrypt,
{
fn new(key: &Key<Self>) -> Self {
let cipher = Aes::new(key);
Kek { cipher }
}
}

impl<Aes> KeySizeUser for Kek<Aes>
where
Aes: KeyInit + BlockSizeUser<BlockSize = U16> + BlockCipherEncrypt + BlockCipherDecrypt,
{
type KeySize = Aes::KeySize;

fn key_size() -> usize {
Aes::key_size()
}
}

struct WCtx<'a> {
n: usize,
block: &'a mut Block<Self>,
Expand Down
33 changes: 22 additions & 11 deletions belt-kwp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#![forbid(unsafe_code)]
#![warn(missing_docs, rust_2018_idioms)]

use belt_block::{belt_wblock_dec, belt_wblock_enc};
use belt_block::{belt_wblock_dec, belt_wblock_enc, BeltBlock};
use core::fmt;

pub use belt_block::cipher::{self, Key, KeyInit, KeySizeUser};

/// Size of wrapping "header".
pub const IV_LEN: usize = 16;

Expand All @@ -27,16 +29,6 @@ impl fmt::Debug for BeltKwp {
}

impl BeltKwp {
/// Create new [`BeltKwp`] instance.
#[inline]
pub fn new(key: &[u8; 32]) -> Self {
let mut res = [0u32; 8];
res.iter_mut()
.zip(key.chunks_exact(4))
.for_each(|(dst, src)| *dst = u32::from_le_bytes(src.try_into().unwrap()));
Self { key: res }
}

/// Wrap key `x` with given `iv` and write result to `out`.
///
/// Size of `x` must be bigger than 16 bytes.
Expand Down Expand Up @@ -107,6 +99,25 @@ impl BeltKwp {
}
}

impl KeyInit for BeltKwp {
fn new(key: &Key<Self>) -> Self {
let mut res = [0u32; 8];
res.iter_mut()
.zip(key.chunks_exact(4))
.for_each(|(dst, src)| *dst = u32::from_le_bytes(src.try_into().unwrap()));

Self { key: res }
}
}

impl KeySizeUser for BeltKwp {
type KeySize = <BeltBlock as KeySizeUser>::KeySize;

fn key_size() -> usize {
BeltBlock::key_size()
}
}

/// Errors emitted from the wrap and unwrap operations.
#[derive(Debug)]
pub enum Error {
Expand Down

0 comments on commit 42891b9

Please sign in to comment.