feat(factory): add cipher/algorithm helper functions

This commit is contained in:
2025-11-24 12:02:22 +02:00
parent 051bba33a8
commit 46a47102b9
8 changed files with 120 additions and 41 deletions

View File

@@ -19,6 +19,12 @@ impl Aes {
}
}
#[inline]
#[must_use]
pub fn from_key(key: impl Into<Key>) -> Self {
Self::new(key)
}
#[cfg(test)]
#[inline]
#[must_use]
@@ -66,9 +72,8 @@ impl Aes {
}
impl BlockCipher for Aes {
const BLOCK_SIZE: usize = 16;
fn from_key(key: &[u8]) -> Self {
Self::new(key)
fn block_size(&self) -> usize {
16
}
fn transform_impl(
@@ -76,9 +81,10 @@ impl BlockCipher for Aes {
block: &[u8],
action: cipher_core::CipherAction,
) -> cipher_core::CipherResult<cipher_core::Output> {
let block_arr: [u8; Self::BLOCK_SIZE] = block
let block_size = self.block_size();
let block_arr: [u8; 16] = block
.try_into()
.map_err(|_| CipherError::invalid_block_size(Self::BLOCK_SIZE, block.len()))?;
.map_err(|_| CipherError::invalid_block_size(block_size, block.len()))?;
let block128 = Block128::from_be_bytes(block_arr);

View File

@@ -1,6 +1,8 @@
use std::fmt::Debug;
use zeroize::ZeroizeOnDrop;
use crate::Block128;
/// 128-bit Key for AES
#[derive(ZeroizeOnDrop)]
pub struct Key([u8; 16]);
@@ -67,6 +69,12 @@ impl From<u128> for Key {
}
}
impl From<Block128> for Key {
fn from(key: Block128) -> Self {
key.to_be_bytes().into()
}
}
impl From<Key> for [u8; 16] {
fn from(key: Key) -> Self {
key.0