From bf6ae712a7c048f4907ea79b85452bdba9a29af8 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Mon, 10 Nov 2025 12:03:11 +0200 Subject: [PATCH] feat(aes): generate subkeys --- aes/src/constants.rs | 79 +++++++++++++++++++++++++++++++++++++++ aes/src/key/expanded.rs | 5 +++ aes/src/key/mod.rs | 1 + aes/src/key/secret_key.rs | 2 +- aes/src/key/subkey.rs | 33 +++++++++++++++- aes/src/key/subkeys.rs | 46 +++++++++++++++++++++-- aes/src/lib.rs | 1 + aes/src/utils.rs | 28 ++++++++++++++ des/src/lib.rs | 4 +- 9 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 aes/src/key/expanded.rs create mode 100644 aes/src/utils.rs diff --git a/aes/src/constants.rs b/aes/src/constants.rs index 7ac3054..157ad6b 100644 --- a/aes/src/constants.rs +++ b/aes/src/constants.rs @@ -1 +1,80 @@ // AES Constants +pub const S_BOXES: [[u8; 16]; 16] = [ + [ + 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, + 0x76, + ], + [ + 0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, + 0xC0, + ], + [ + 0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, + 0x15, + ], + [ + 0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, + 0x75, + ], + [ + 0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, + 0x84, + ], + [ + 0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, + 0xCF, + ], + [ + 0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, + 0xA8, + ], + [ + 0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, + 0xD2, + ], + [ + 0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, + 0x73, + ], + [ + 0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, + 0xDB, + ], + [ + 0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, + 0x79, + ], + [ + 0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, + 0x08, + ], + [ + 0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, + 0x8A, + ], + [ + 0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, + 0x9E, + ], + [ + 0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, + 0xDF, + ], + [ + 0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, + 0x16, + ], +]; + +pub const RCON: [u32; 10] = [ + 0x0100_0000, + 0x0200_0000, + 0x0400_0000, + 0x0800_0000, + 0x1000_0000, + 0x2000_0000, + 0x4000_0000, + 0x8000_0000, + 0x1B00_0000, + 0x3600_0000, +]; diff --git a/aes/src/key/expanded.rs b/aes/src/key/expanded.rs new file mode 100644 index 0000000..bc047b7 --- /dev/null +++ b/aes/src/key/expanded.rs @@ -0,0 +1,5 @@ +use crate::secret_key; + +secret_key! { + pub struct ExpandedKey(u32, 32, 0xFFFF_FFFF); +} diff --git a/aes/src/key/mod.rs b/aes/src/key/mod.rs index b5aaac9..b2d4871 100644 --- a/aes/src/key/mod.rs +++ b/aes/src/key/mod.rs @@ -1,4 +1,5 @@ mod aes_key; +mod expanded; mod secret_key; mod subkey; mod subkeys; diff --git a/aes/src/key/secret_key.rs b/aes/src/key/secret_key.rs index 51b5f7f..1b6346f 100644 --- a/aes/src/key/secret_key.rs +++ b/aes/src/key/secret_key.rs @@ -15,7 +15,7 @@ macro_rules! secret_key { $vis:vis struct $name:ident ( $int:tt, $bits:expr, $mask:expr ); ) => { $(#[$meta])* - #[derive(::zeroize::ZeroizeOnDrop, Default)] + #[derive(Default, Clone, Copy)] $vis struct $name($int); impl $name { diff --git a/aes/src/key/subkey.rs b/aes/src/key/subkey.rs index 74c9a7a..53afa05 100644 --- a/aes/src/key/subkey.rs +++ b/aes/src/key/subkey.rs @@ -1,4 +1,5 @@ -use crate::key::secret_key; +use crate::key::{expanded::ExpandedKey, secret_key}; +use std::ops::BitXor; secret_key! { /// A single AES round subkey @@ -10,4 +11,34 @@ impl Subkey { pub const fn zero() -> Self { Self(0) } + + /// Shifts the bits to the left by a specified amount, `n`, + /// wrapping the truncated bits to the end of the resulting integer. + /// + /// Please note this isn't the same operation as the `<<` shifting operator! + pub const fn rotate_left(self, n: u32) -> Self { + Self(self.0.rotate_left(n)) + } + + /// Shifts the bits to the right by a specified amount, `n`, + /// wrapping the truncated bits to the beginning of the resulting integer. + /// + /// Please note this isn't the same operation as the `>>` shifting operator! + pub const fn rotate_right(self, n: u32) -> Self { + Self(self.0.rotate_right(n)) + } +} + +impl BitXor for Subkey { + type Output = Self; + fn bitxor(self, rhs: ExpandedKey) -> Self::Output { + Self(self.0 ^ rhs.as_u32()) + } +} + +impl BitXor for Subkey { + type Output = Self; + fn bitxor(self, rhs: Self) -> Self::Output { + Self(self.0 ^ rhs.0) + } } diff --git a/aes/src/key/subkeys.rs b/aes/src/key/subkeys.rs index 7b29ca8..a28d10c 100644 --- a/aes/src/key/subkeys.rs +++ b/aes/src/key/subkeys.rs @@ -1,4 +1,7 @@ -use crate::key::{Key, subkey::Subkey}; +use crate::{ + constants::{RCON, S_BOXES}, + key::{Key, expanded::ExpandedKey, subkey::Subkey}, +}; use std::{ fmt::Debug, iter::Rev, @@ -6,14 +9,32 @@ use std::{ slice::{Iter, IterMut}, }; +const SUBKEY_COUNT: usize = 44; + // #[derive(Default)] -pub struct Subkeys([Subkey; 44]); +pub struct Subkeys([Subkey; SUBKEY_COUNT]); impl Subkeys { /// Generates 44 round subkeys from the given key. #[must_use] pub fn from_key(key: &Key) -> Self { - todo!() + let mut subkeys = [const { Subkey::zero() }; 44]; + + // Load initial key + for (idx, &key) in key.as_2d().iter().enumerate() { + subkeys[idx] = Subkey::from_u32(u32::from_be_bytes(key)); + } + + for (round, &rcon) in RCON.iter().enumerate() { + let idx = round * 4 + 4; + + subkeys[idx + 0] = subkeys[idx - 4] ^ expand(&subkeys[idx - 1], rcon); + subkeys[idx + 1] = subkeys[idx] ^ subkeys[idx - 3]; + subkeys[idx + 2] = subkeys[idx + 1] ^ subkeys[idx - 2]; + subkeys[idx + 3] = subkeys[idx + 2] ^ subkeys[idx - 1]; + } + + Self(subkeys) } /// Returns an iterator over the subkeys. @@ -61,6 +82,25 @@ impl Debug for Subkeys { } } +const fn expand(subkey: &Subkey, rcon: u32) -> ExpandedKey { + let word = subkey.rotate_left(8).as_u32(); + + let b0 = sbox_lookup(word >> 24); + let b1 = sbox_lookup(word >> 16); + let b2 = sbox_lookup(word >> 8); + let b3 = sbox_lookup(word); + let substituted = (b0 << 24) | (b1 << 16) | (b2 << 8) | b3; + ExpandedKey::from_u32(substituted ^ rcon) +} + +const fn sbox_lookup(byte: u32) -> u32 { + const MASK: u32 = 0xFF; + let row = ((byte & MASK) as usize) >> 4; + let col = ((byte & MASK) as usize) & 0xF; + + S_BOXES[row][col] as u32 +} + #[cfg(test)] mod tests { use super::*; diff --git a/aes/src/lib.rs b/aes/src/lib.rs index 7730c8e..c390a0e 100644 --- a/aes/src/lib.rs +++ b/aes/src/lib.rs @@ -2,5 +2,6 @@ mod aes; mod block; mod constants; mod key; +mod utils; pub use {aes::Aes, block::Block128}; diff --git a/aes/src/utils.rs b/aes/src/utils.rs new file mode 100644 index 0000000..ae51a13 --- /dev/null +++ b/aes/src/utils.rs @@ -0,0 +1,28 @@ +/// Generic bit permutation for DES specification. +/// DES uses 1-based positioning where bit 1 is the leftmost (MSB) and bit 64 is the rightmost (LSB). +/// This function handles this correctly for arbitrary input/output sizes. +/// +/// # Arguments +/// - `input` - The input value (treated as a bitfield of `input_bits` size) +/// - `input_bits` - Number of meaningful bits in the input (1-64) +/// - `output_bits` - Number of bits in the output (1-64) +/// - `position_table` - Table of 1-based positions (1 to `input_bits`) where each output bit comes from. +/// The table should have `output_bits` entries. For each entry i (0-based), `position_table`[i] indicates +/// which input bit (1-based) should go to the i-th output bit position (from MSB to LSB). +#[must_use] +pub fn permutate( + input: u64, + input_bit_amount: u64, + output_bit_amount: u64, + position_table: &[u8], +) -> u64 { + position_table + .iter() + .enumerate() + .fold(0, |acc, (idx, &input_pos_1based)| { + let input_bit_pos = input_bit_amount - u64::from(input_pos_1based); + let bit_value = (input >> input_bit_pos) & 1; + let output_bit_pos = output_bit_amount - 1 - (idx as u64); + acc | (bit_value << output_bit_pos) + }) +} diff --git a/des/src/lib.rs b/des/src/lib.rs index a1a5cdb..2857a60 100644 --- a/des/src/lib.rs +++ b/des/src/lib.rs @@ -1,7 +1,7 @@ mod block; -pub(crate) mod constants; +pub mod constants; mod des; mod key; -pub(crate) mod utils; +pub mod utils; pub use {block::Block64, des::Des};