Skip to content

Commit

Permalink
rc6: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashWhiteHat committed Jul 31, 2024
1 parent e8e7360 commit 342a69e
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rc6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRVs
- stable
target:
- thumbv7em-none-eabi
Expand All @@ -45,7 +45,7 @@ jobs:
strategy:
matrix:
rust:
- 1.56.0 # MSRV
- 1.65.0 # MSRVs
- stable
steps:
- uses: actions/checkout@v3
Expand Down
114 changes: 114 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rc6/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cipher = { version = "0.4.3", features = ["zeroize"] }

[dev-dependencies]
cipher = { version = "0.4.3", features = ["dev"] }
rand = { version = "0.8.5" }

[features]
zeroize = []
68 changes: 57 additions & 11 deletions rc6/src/core/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ where
ExpandedKeyTableSize<R>: ArrayLength<W>,
{
pub fn encrypt(&self, mut block: InOut<'_, '_, Block<W>>) {
let (mut a, mut b) = Self::words_from_block(block.get_in());
let (mut a, mut b, mut c, mut d) = Self::words_from_block(block.get_in());
let key = &self.key_table;
let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8);

Expand Down Expand Up @@ -168,7 +168,7 @@ where
}

pub fn decrypt(&self, mut block: InOut<'_, '_, Block<W>>) {
let (mut a, mut b) = Self::words_from_block(block.get_in());
let (mut a, mut b, mut c, mut d) = Self::words_from_block(block.get_in());
let key = &self.key_table;
let log_w = W::from((usize::BITS - 1 - (W::Bytes::USIZE * 8).leading_zeros()) as u8);

Expand All @@ -194,21 +194,67 @@ where
d = d.wrapping_sub(key[1]);
b = b.wrapping_sub(key[0]);

Self::block_from_words(a, b, block.get_out())
Self::block_from_words(a, b, c, d, block.get_out())
}

fn words_from_block(block: &Block<W>) -> (W, W) {
// Block size is 2 * word::BYTES so the unwrap is safe
fn words_from_block(block: &Block<W>) -> (W, W, W, W) {
// Block size is 4 * word::BYTES so the unwrap is safe
let a = W::from_le_bytes(block[..W::Bytes::USIZE].try_into().unwrap());
let b = W::from_le_bytes(block[W::Bytes::USIZE..].try_into().unwrap());
let b = W::from_le_bytes(
block[W::Bytes::USIZE..W::Bytes::USIZE * 2]
.try_into()
.unwrap(),
);
let c = W::from_le_bytes(
block[W::Bytes::USIZE * 2..W::Bytes::USIZE * 3]
.try_into()
.unwrap(),
);
let d = W::from_le_bytes(
block[W::Bytes::USIZE * 3..W::Bytes::USIZE * 4]
.try_into()
.unwrap(),
);

(a, b, c, d)
}

fn block_from_words(a: W, b: W, c: W, d: W, out_block: &mut Block<W>) {
let (left, right) = out_block.split_at_mut(W::Bytes::USIZE * 2);
let (l_l, l_h) = left.split_at_mut(W::Bytes::USIZE);
let (r_l, r_h) = right.split_at_mut(W::Bytes::USIZE);

(a, b)
l_l.copy_from_slice(&a.to_le_bytes());
l_h.copy_from_slice(&b.to_le_bytes());
r_l.copy_from_slice(&c.to_le_bytes());
r_h.copy_from_slice(&d.to_le_bytes());
}
}

fn block_from_words(a: W, b: W, out_block: &mut Block<W>) {
let (left, right) = out_block.split_at_mut(W::Bytes::USIZE);
#[cfg(test)]
mod tests {
use crate::block_cipher::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4};
use crate::core::backend::GenericArray;
use rand::{thread_rng, Rng};

#[macro_export]
macro_rules! words_block_conv {
($rc_tyoe:ident, $key_size:expr) => {
let mut pt = [0u8; $key_size];
thread_rng().fill(&mut pt[..]);
let block = GenericArray::clone_from_slice(&pt);
let mut after_block = block.clone();
let (a, b, c, d) = $rc_tyoe::words_from_block(&block);
$rc_tyoe::block_from_words(a, b, c, d, &mut after_block);
assert_eq!(block, after_block);
};
}

left.copy_from_slice(&a.to_le_bytes());
right.copy_from_slice(&b.to_le_bytes());
#[test]
fn words_block_test() {
words_block_conv!(RC6_16_16_8, 8);
words_block_conv!(RC6_32_20_16, 16);
words_block_conv!(RC6_64_24_24, 32);
words_block_conv!(RC6_8_12_4, 4);
}
}

0 comments on commit 342a69e

Please sign in to comment.