Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions des/benches/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#![feature(test)]
extern crate test;

use cipher::{block_decryptor_bench, block_encryptor_bench};
use des::{Des, TdesEde3};
macro_rules! benches {
($name:ident, $cipher:ty) => {
mod $name {
use cipher::{Key, KeyInit, block_decryptor_bench, block_encryptor_bench};

block_encryptor_bench!(Key: Des, des_encrypt_block, des_encrypt_blocks);
block_decryptor_bench!(Key: Des, des_decrypt_block, des_decrypt_blocks);
block_encryptor_bench!(Key: $cipher, encrypt_block, encrypt_blocks);
block_decryptor_bench!(Key: $cipher, decrypt_block, decrypt_blocks);

block_encryptor_bench!(
Key: TdesEde3,
tdes_ede3_encrypt_block,
tdes_ede3_encrypt_blocks,
);
block_decryptor_bench!(
Key: TdesEde3,
tdes_ede3_decrypt_block,
tdes_ede3_decrypt_blocks,
);
#[bench]
fn key_init(bh: &mut test::Bencher) {
let key = Key::<$cipher>::default();
bh.iter(|| <$cipher>::new(test::black_box(&key)));
}
}
};
}

benches!(des, ::des::Des);
benches!(tdes_ede2, ::des::TdesEde2);
benches!(tdes_ede3, ::des::TdesEde3);
benches!(tdes_eee2, ::des::TdesEee2);
benches!(tdes_eee3, ::des::TdesEee3);
38 changes: 23 additions & 15 deletions des/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn e(block: u64) -> u64 {
}

/// Swap bits using the P table
fn p(block: u64) -> u64 {
const fn p(block: u64) -> u64 {
let block = block.rotate_left(44);
let b1 = (block & 0x0000000000200000) << 32;
let b2 = (block & 0x0000000000480000) << 13;
Expand Down Expand Up @@ -132,21 +132,29 @@ pub(crate) fn round(input: u64, key: u64) -> u64 {
r | ((f(r, key) ^ l) >> 32)
}

fn f(input: u64, key: u64) -> u64 {
let mut val = e(input);
val ^= key;
val = apply_sboxes(val);
p(val)
}

/// Applies all eight sboxes to the input
fn apply_sboxes(input: u64) -> u64 {
let mut output: u64 = 0;
// Apply P to each possible S-box output at compile time. The eight contributions
// occupy disjoint bits, so ORing them is equivalent to permuting their union.
// Store only the significant 32 bits to keep the tables at 2 KiB.
const SPBOXES: [[u32; 64]; 8] = {
let mut tables = [[0; 64]; 8];
let mut i = 0;
while i < 8 {
let mut j = 0;
while j < 64 {
tables[i][j] = (p((SBOXES[i][j] as u64) << (60 - 4 * i)) >> 32) as u32;
j += 1;
}
i += 1;
}
tables
};

for (i, sbox) in SBOXES.iter().enumerate() {
fn f(input: u64, key: u64) -> u64 {
let input = e(input) ^ key;
let mut output = 0;
for (i, sbox) in SPBOXES.iter().enumerate() {
let val = (input >> (58 - (i * 6))) & 0x3F;
output |= u64::from(sbox[val as usize]) << (60 - (i * 4));
output |= sbox[val as usize];
}

output
u64::from(output) << 32
}
25 changes: 25 additions & 0 deletions des/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,28 @@
cipher::block_cipher_test!(des, des::Des);
cipher::block_cipher_test!(tdes, des::TdesEde3);
cipher::block_cipher_test!(tdes2, des::TdesEde2);

use cipher::dev::block_cipher::{TestVector, encrypt_decrypt_test};
use hex_literal::hex;

// Cross-checked with three successive OpenSSL DES-ECB encryptions.
#[test]
fn tdes_eee2() {
let tv = TestVector {
key: &hex!("133457799bbcdff10e329232ea6d0d73"),
plaintext: &hex!("0123456789abcdef"),
ciphertext: &hex!("bbf248468b8878ac"),
};
encrypt_decrypt_test::<des::TdesEee2>(&tv).unwrap();
}

// Cross-checked with three successive OpenSSL DES-ECB encryptions.
#[test]
fn tdes_eee3() {
let tv = TestVector {
key: &hex!("133457799bbcdff10e329232ea6d0d73aabb09182736ccdd"),
plaintext: &hex!("0123456789abcdef"),
ciphertext: &hex!("afc8f360c3c7d674"),
};
encrypt_decrypt_test::<des::TdesEee3>(&tv).unwrap();
}
Loading