Skip to content

Commit

Permalink
Key cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
eightfilms committed Jun 29, 2024
1 parent b6f90f3 commit b5d929c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
21 changes: 15 additions & 6 deletions src/encryption/symmetric/aes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains the implementation for the Advanced Encryption Standard (AES) encryption
//! and decryption.
#![doc = include_str!("./README.md")]
#![cfg_attr(not(doctest), doc = include_str!("./README.md"))]

use itertools::Itertools;

Expand All @@ -23,6 +23,13 @@ where [(); N / 8]: {
inner: [u8; N / 8],
}

impl<const N: usize> Key<N>
where [(); N / 8]:
{
/// Creates a new `Key` of size `N` bits.
pub fn new(key_bytes: [u8; N / 8]) -> Self { Self { inner: key_bytes } }
}

impl<const N: usize> std::ops::Deref for Key<N>
where [(); N / 8]:
{
Expand All @@ -41,11 +48,16 @@ where [(); K / 8]:
///
/// ## Example
/// ```rust
/// #![feature(generic_const_exprs)]
///
/// use rand::{thread_rng, Rng};
/// use ronkathon::encryption::symmetric::{aes::AES, SymmetricEncryption};
/// use ronkathon::encryption::symmetric::{
/// aes::{Key, AES},
/// SymmetricEncryption,
/// };
///
/// let mut rng = thread_rng();
/// let key = Key::<128> { inner: rng.gen() };
/// let key = Key::<128>::new(rng.gen());
/// let plaintext = rng.gen();
/// let encrypted = AES::encrypt(&key, &plaintext);
/// ```
Expand Down Expand Up @@ -229,9 +241,6 @@ where [(); K / 8]:
let key_words: Vec<Word> = key.chunks(4).map(|c| c.try_into().unwrap()).collect();
expanded_key.extend(key_words);

// key len (Nk words)
// block size (Nb words)
// num rounds (Nr)
for i in key_len..(block_num_words * (num_rounds + 1)) {
let mut last = *expanded_key.last().unwrap();

Expand Down
30 changes: 11 additions & 19 deletions src/encryption/symmetric/aes/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ use super::*;
#[test]
fn test_aes_128() {
const KEY_LEN: usize = 128;
let key = Key::<KEY_LEN> {
inner: [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f,
],
};
let key = Key::<KEY_LEN>::new([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
]);

let plaintext = [
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
Expand All @@ -27,12 +24,10 @@ fn test_aes_128() {
#[test]
fn test_aes_192() {
const KEY_LEN: usize = 192;
let key = Key::<KEY_LEN> {
inner: [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
],
};
let key = Key::<KEY_LEN>::new([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
]);

let plaintext = [
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
Expand All @@ -50,13 +45,10 @@ fn test_aes_192() {
#[test]
fn test_aes_256() {
const KEY_LEN: usize = 256;
let key = Key::<KEY_LEN> {
inner: [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
0x1e, 0x1f,
],
};
let key = Key::<KEY_LEN>::new([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
]);

let plaintext = [
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
Expand Down

0 comments on commit b5d929c

Please sign in to comment.