Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rc6: RC6 Implementation #439

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
rc6: implement decryption algorithm
  • Loading branch information
ashWhiteHat committed Jul 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 115e04866c3f60f372ad95691ee6e6611774c24c
23 changes: 19 additions & 4 deletions rc6/src/core/backend.rs
Original file line number Diff line number Diff line change
@@ -170,14 +170,29 @@ where
pub fn decrypt(&self, mut block: InOut<'_, '_, Block<W>>) {
let (mut a, mut b) = Self::words_from_block(block.get_in());
let key = &self.key_table;
let log_w = W::from((W::Bytes::USIZE as f64 * 8 as f64).log2() as u8);

c = c.wrapping_sub(key[2 * R::USIZE + 3]);
a = a.wrapping_sub(key[2 * R::USIZE + 2]);

for i in (1..=R::USIZE).rev() {
b = b.wrapping_sub(key[2 * i + 1]).rotate_right(a).bitxor(a);
a = a.wrapping_sub(key[2 * i]).rotate_right(b).bitxor(b);
let (tmp_a, tmp_b, tmp_c, tmp_d) = (d, a, b, c);
a = tmp_a;
b = tmp_b;
c = tmp_c;
d = tmp_d;
let u = d
.wrapping_mul(d.wrapping_mul(W::from(2)).wrapping_add(W::from(1)))
.rotate_left(log_w);
let t = b
.wrapping_mul(b.wrapping_mul(W::from(2)).wrapping_add(W::from(1)))
.rotate_left(log_w);
c = c.wrapping_sub(key[2 * i + 1]).rotate_right(t).bitxor(u);
a = a.wrapping_sub(key[2 * i]).rotate_right(u).bitxor(t);
}

b = b.wrapping_sub(key[1]);
a = a.wrapping_sub(key[0]);
d = d.wrapping_sub(key[1]);
b = b.wrapping_sub(key[0]);

Self::block_from_words(a, b, block.get_out())
}
14 changes: 13 additions & 1 deletion rc6/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// generated using the code in: https://www.ietf.org/archive/id/draft-krovetz-rc6-rc5-vectors-00.txt
#[cfg(test)]
mod tests {
use cipher::{generic_array::GenericArray, BlockEncrypt, KeyInit};
use cipher::{generic_array::GenericArray, BlockDecrypt, BlockEncrypt, KeyInit};
use rc6::{RC6_16_16_8, RC6_32_20_16, RC6_64_24_24, RC6_8_12_4};

#[test]
@@ -16,6 +16,9 @@ mod tests {
rc6.encrypt_block(&mut block);

assert_eq!(ct, block[..]);

rc6.decrypt_block(&mut block);
assert_eq!(pt, block[..]);
}

#[test]
@@ -30,6 +33,9 @@ mod tests {
rc6.encrypt_block(&mut block);

assert_eq!(ct, block[..]);

rc6.decrypt_block(&mut block);
assert_eq!(pt, block[..]);
}

#[test]
@@ -53,6 +59,9 @@ mod tests {
rc6.encrypt_block(&mut block);

assert_eq!(ct, block[..]);

rc6.decrypt_block(&mut block);
assert_eq!(pt, block[..]);
}

#[test]
@@ -78,5 +87,8 @@ mod tests {
rc6.encrypt_block(&mut block);

assert_eq!(ct, block[..]);

rc6.decrypt_block(&mut block);
assert_eq!(pt, block[..]);
}
}