feat(core): add CipherOutput type

This commit is contained in:
2025-10-17 21:35:47 +03:00
parent 74dd8eb27d
commit 76a808e1b3
8 changed files with 140 additions and 15 deletions

View File

@@ -21,6 +21,16 @@ macro_rules! secret_block {
/// Mask to restrict the underlying integer to valid bits.
pub const MASK: $int = $mask;
/// Calculate the number of hex digits needed for the bit width
const fn hex_width() -> usize {
($bits as usize).div_ceil(4)
}
/// Calculate the number of octal digits needed for the bit width
const fn octal_width() -> usize {
($bits as usize).div_ceil(3)
}
#[inline]
#[must_use]
pub const fn new(value: $int) -> Self {
@@ -36,6 +46,37 @@ macro_rules! secret_block {
Self(v & Self::MASK)
}
}
impl From<$name> for $int {
fn from(value: $name) -> $int {
value.0
}
}
impl std::fmt::UpperHex for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$X}", self.0, width = Self::hex_width())
}
}
impl std::fmt::LowerHex for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$x}", self.0, width = Self::hex_width())
}
}
impl std::fmt::Octal for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$o}", self.0, width = Self::octal_width())
}
}
impl std::fmt::Binary for $name {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:0width$b}", self.0, width = $bits)
}
}
};
// Helper: generate conversions_as based on type
(@conversions_as u8) => {

View File

@@ -41,7 +41,7 @@ impl BlockCipher for Des {
&self,
block: &[u8],
action: cipher_core::CipherAction,
) -> cipher_core::CipherResult<Vec<u8>> {
) -> cipher_core::CipherResult<cipher_core::CipherOutput> {
let block_arr: [u8; Self::BLOCK_SIZE] = block
.try_into()
.map_err(|_| CipherError::invalid_block_size(Self::BLOCK_SIZE, block.len()))?;

View File

@@ -4,6 +4,7 @@ use zeroize::ZeroizeOnDrop;
/// 64-bit Key for DES
#[derive(ZeroizeOnDrop)]
pub struct Key([u8; 8]);
impl Key {
#[inline]
#[must_use]

View File

@@ -4,4 +4,4 @@ mod des;
mod key;
pub(crate) mod utils;
pub use {des::Des, key::Subkeys};
pub use des::Des;

View File

@@ -1,4 +1,4 @@
use cipher_core::BlockCipher;
use cipher_core::{BlockCipher, CipherOutput};
use claims::assert_ok;
use des::Des;
use rstest::rstest;
@@ -120,9 +120,9 @@ fn encrypt_decrypt_roundtrip(
let dectrypted = assert_ok!(des.decrypt(&ciphertext));
let re_ciphertext = assert_ok!(des.encrypt(&dectrypted));
let ciphertext_u64 = u64::from_be_bytes(ciphertext.try_into().expect("8 bytes"));
let decrypted_u64 = u64::from_be_bytes(dectrypted.try_into().expect("8 bytes"));
let re_ciphertext_u64 = u64::from_be_bytes(re_ciphertext.try_into().expect("8 bytes"));
let ciphertext_u64 = cipher_block_to_u64(ciphertext);
let decrypted_u64 = cipher_block_to_u64(dectrypted);
let re_ciphertext_u64 = cipher_block_to_u64(re_ciphertext);
assert_eq!(
ciphertext_u64, expected_ciphertext,
@@ -148,7 +148,7 @@ fn weak_keys(#[case] key: u64) {
let ciphertext = assert_ok!(des.encrypt(&plaintext.to_be_bytes()));
let decrypted = assert_ok!(des.decrypt(&ciphertext));
let decrypted_u64 = u64::from_be_bytes(decrypted.try_into().expect("8 bytes"));
let decrypted_u64 = cipher_block_to_u64(decrypted);
assert_eq!(
decrypted_u64, plaintext,
@@ -163,7 +163,7 @@ fn all_zero_paintext() {
let plain = 0u64;
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
let decrypted = assert_ok!(des.decrypt(&encrypted));
let decrypted_u64 = u64::from_be_bytes(decrypted.try_into().expect("8 bytes"));
let decrypted_u64 = cipher_block_to_u64(decrypted);
assert_eq!(decrypted_u64, plain, "All-zero plaintext failed");
}
@@ -174,7 +174,7 @@ fn all_one_paintext() {
let plain = u64::MAX;
let encrypted = assert_ok!(des.encrypt(&plain.to_be_bytes()));
let decrypted = assert_ok!(des.decrypt(&encrypted));
let decrypted_u64 = u64::from_be_bytes(decrypted.try_into().expect("8 bytes"));
let decrypted_u64 = cipher_block_to_u64(decrypted);
assert_eq!(decrypted_u64, plain, "All-one plaintext failed");
}
@@ -191,3 +191,8 @@ fn different_inputs() {
"Encryption not deterministic for different inputs"
);
}
fn cipher_block_to_u64(block: CipherOutput) -> u64 {
let bytes = block.as_slice().try_into().expect("8 bytes");
u64::from_be_bytes(bytes)
}