mirror of
https://github.com/kristoferssolo/des-rs.git
synced 2025-12-20 11:04:38 +00:00
372 lines
11 KiB
Rust
372 lines
11 KiB
Rust
#[derive(Debug)]
|
|
pub struct DES {
|
|
pub subkeys: [u64; 16],
|
|
_s_boxes: Vec<Vec<Vec<u8>>>,
|
|
}
|
|
|
|
impl DES {
|
|
#[must_use]
|
|
pub fn new(key: u64) -> Self {
|
|
let mut des = Self {
|
|
subkeys: [0; 16],
|
|
_s_boxes: Vec::new(),
|
|
};
|
|
des.generate_subkeys(key);
|
|
des
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn encrypt(&self, _plaintext: u64) -> u64 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn decrypt(&self, _plaintext: u64) -> u64 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn ip(&self, _input: u64) -> u64 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn pc1(&self, _key: u64) -> u64 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn expand(&self, _right_half: u32) -> u64 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn permutate_output(&self, _input: u32) -> u32 {
|
|
todo!()
|
|
}
|
|
|
|
#[must_use]
|
|
pub fn feistel(&self, _right: u32, _subkey: u64) -> u32 {
|
|
todo!()
|
|
}
|
|
|
|
fn generate_subkeys(&mut self, _key: u64) {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
/// Encrypts data using ECB mode.
|
|
///
|
|
/// # Arguments
|
|
/// - `data` - Plaintext bytes (must be multiple of 8 for ECB)
|
|
/// - `key` - 8-byte DES key
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// Ciphertext as Vec<u8>, same length as input
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// If data length is not multiple of 8 bytes
|
|
#[must_use]
|
|
pub fn encrypt_ecb(_data: &[u8], _key: &[u8; 8]) -> Vec<u8> {
|
|
todo!()
|
|
}
|
|
|
|
/// Decrypts ECB-encrypted data.
|
|
///
|
|
/// # Arguments
|
|
/// - `data` - Plaintext bytes (must be multiple of 8 for ECB)
|
|
/// - `key` - 8-byte DES key
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// Ciphertext as Vec<u8>, same length as input
|
|
///
|
|
/// # Panics
|
|
///
|
|
/// If data length is not multiple of 8 bytes
|
|
#[must_use]
|
|
pub fn decrypt_ecb(_data: &[u8], _key: &[u8; 8]) -> Vec<u8> {
|
|
todo!()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use claims::assert_le;
|
|
use rand::random;
|
|
use std::time::Instant;
|
|
|
|
const TEST_KEY: u64 = 0x133457799BBCDFF1;
|
|
const RIGHT_KEY: u32 = 0x12345678;
|
|
const TEST_PLAINTEXT: u64 = 0x0123456789ABCDEF;
|
|
const TEST_CIPHERTEXT: u64 = 0x85E813540F0AB405;
|
|
|
|
impl DES {
|
|
fn apply_sboxes(&self, _input: u64) -> u32 {
|
|
// Implementation for testing S-boxes in isolation
|
|
// Return 32-bit result after 8 S-boxes
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
/// Helper to create a test Des instance (use your actual key schedule)
|
|
fn des_instance() -> DES {
|
|
DES::new(TEST_KEY)
|
|
}
|
|
|
|
#[test]
|
|
fn encrypt_decrypt_roundtrip() {
|
|
let des = des_instance();
|
|
let plaintext = TEST_PLAINTEXT;
|
|
let ciphertext = des.encrypt(plaintext);
|
|
let dectrypted = des.decrypt(plaintext);
|
|
let re_ciphertext = des.encrypt(dectrypted);
|
|
|
|
assert_eq!(ciphertext, TEST_CIPHERTEXT, "Encyption failed");
|
|
assert_eq!(re_ciphertext, TEST_CIPHERTEXT, "Re-Encyption failed");
|
|
}
|
|
|
|
#[test]
|
|
fn weak_keys_rejected() {
|
|
let weak_keys = [0x0101010101010101, 0xFEFEFEFEFEFEFEFE, 0xE001E001E001E001];
|
|
|
|
for key in weak_keys {
|
|
let des = DES::new(key);
|
|
let plaintext = TEST_PLAINTEXT;
|
|
let encrypted = des.encrypt(plaintext);
|
|
let dectrypted = des.decrypt(encrypted);
|
|
assert_eq!(dectrypted, plaintext, "Weak key {key} failed roundtrip");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn multiple_blocks() {
|
|
let des = des_instance();
|
|
let blocks = [
|
|
(0x0123456789ABCDEFu64, 0x85E813540F0AB405u64),
|
|
(0xFEDCBA9876543210u64, 0xC08BF0FF627D3E6Fu64), // Another test vector
|
|
(0x0000000000000000u64, 0x474D5E3B6F8A07F8u64), // Zero block
|
|
];
|
|
for (plaintext, expected) in blocks {
|
|
let encrypted = des.encrypt(plaintext);
|
|
assert_eq!(encrypted, expected, "Failed on plaintext: {plaintext:016X}");
|
|
|
|
let dectrypted = des.decrypt(encrypted);
|
|
assert_eq!(dectrypted, plaintext, "Roundtrip failed on block");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn key_schedule_generates_correct_subkeys() {
|
|
let expected_subkeys = [
|
|
0xF3FDFBF373848CF5u64,
|
|
0xF3738CF548C4F3F5u64,
|
|
0x848C4F3F5F373848u64,
|
|
];
|
|
|
|
let des = des_instance();
|
|
let generated = des.subkeys;
|
|
|
|
for (idx, &expected) in expected_subkeys.iter().enumerate() {
|
|
let masked_gen = generated[idx];
|
|
let masked_exp = expected;
|
|
assert_eq!(
|
|
masked_gen, masked_exp,
|
|
"Subkey {idx} mismatch: expected {masked_exp:012X}, got {masked_gen:012X}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn initial_permutation() {
|
|
let input = TEST_KEY;
|
|
let expected_ip = 0xC2B093C7A3A7C24A;
|
|
let result = des_instance().ip(input);
|
|
assert_eq!(result, expected_ip, "Initial permulation failed");
|
|
}
|
|
|
|
#[test]
|
|
fn pc1_permutaion_correct() {
|
|
let des = des_instance();
|
|
let key = TEST_KEY;
|
|
let expected_pc1 = 0x0A2B3C4D5E6F789A; // Truncated 56 bits from spec
|
|
let result = des.pc1(key);
|
|
let masked_result = result & 0x00FF_FFFF_FFFF_FFFF; // 56 bits
|
|
let masked_expected = expected_pc1 & 0x00FF_FFFF_FFFF_FFFF;
|
|
assert_eq!(masked_result, masked_expected, "PC1 permutation failed");
|
|
}
|
|
|
|
#[test]
|
|
fn expansion_permutation() {
|
|
let des = des_instance();
|
|
let right_half = RIGHT_KEY;
|
|
let expanded = des.expand(right_half);
|
|
|
|
// Expansion should produce 48 bits from 32
|
|
assert_eq!(expanded >> 48, 0, "Expandsion exceeds 48 bits");
|
|
|
|
// Test that expansion duplicates bits correctly
|
|
// Bit 0 of expanded should match bit 31 of input (EXPANSION[0]=32)
|
|
assert_eq!(
|
|
(expanded >> 47) & 1,
|
|
((right_half as u64) >> 31) & 1,
|
|
"Expansion bit 0 failed"
|
|
);
|
|
// Bit 1 should match bit 0 (EXPANSION[1]=1)
|
|
assert_eq!(
|
|
(expanded >> 46) & 1,
|
|
(right_half as u64) & 1,
|
|
"Expansion bit 1 failed"
|
|
);
|
|
// Test wraparound: bit 47 should match bit 0 again (EXPANSION[47]=1)
|
|
assert_eq!(
|
|
expanded & 1,
|
|
(right_half as u64) & 1,
|
|
"Expansion wraparound failed"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn sbox_subsitution() {
|
|
let des = des_instance();
|
|
let sbox_tests = [
|
|
// (box_idx, 6-bit input, expected 4-bit output)
|
|
(0, 0b000000, 14), // S1: 00 0000 -> row 0, col 0 -> 14
|
|
(0, 0b011111, 9), // S1: 01 1111 -> row 1, col 15 -> 9
|
|
(1, 0b100000, 0), // S2: 10 0000 -> row 2, col 0 -> 0
|
|
(2, 0b001010, 2), // S3: 00 1010 -> row 0, col 10 -> 2
|
|
];
|
|
|
|
for (box_idx, input, expected) in sbox_tests {
|
|
let row = (input & 1) | ((input >> 4) & 0x2);
|
|
let col = (input >> 1) & 0xF;
|
|
let val = des._s_boxes[box_idx][row as usize][col as usize];
|
|
|
|
assert_eq!(
|
|
val,
|
|
expected as u8,
|
|
"S{} failed: input {input:06b} (row {row}, col {col}) expected {expected}, got {val}",
|
|
box_idx + 1
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn permuation_pbox() {
|
|
let des = des_instance();
|
|
let input = RIGHT_KEY;
|
|
let result = des.permutate_output(input);
|
|
|
|
// P-box should preserve all bits (32 in, 32 out), just reorder
|
|
let bit_count = input.count_ones();
|
|
let result_bit_count = result.count_ones();
|
|
assert_eq!(bit_count, result_bit_count, "P-box changes bit count");
|
|
|
|
// Test specific bit mapping: PERMUTATION[0]=16 means bit 15 (0-based) of output = bit 15 of input
|
|
let input_bit_15 = (input >> 15) & 1;
|
|
let output_bit_0 = (result >> 31) & 1; // MSB first
|
|
assert_eq!(input_bit_15, output_bit_0, "P-box bit mapping failed");
|
|
}
|
|
|
|
#[test]
|
|
fn feistel_function_properties() {
|
|
let des = des_instance();
|
|
let right = RIGHT_KEY;
|
|
let subkey = 0xFEDCBA9876543210 & 0xFFFF_FFFF_FFFF;
|
|
|
|
let feistel_result = des.feistel(right, subkey);
|
|
|
|
// Feistel output should always be 32 bits
|
|
assert_le!(feistel_result, u32::MAX, "Feistel output exceeds 32 bits");
|
|
|
|
// Test that zero subkey produces deterministic result
|
|
let zero_subkey_result = des.feistel(right, 0);
|
|
let zero_expanded = des.expand(right);
|
|
let sbox_result = des.apply_sboxes(zero_expanded);
|
|
let expected = des.permutate_output(sbox_result as u32);
|
|
assert_eq!(zero_subkey_result, expected, "Feistel with zero key failed");
|
|
}
|
|
|
|
#[test]
|
|
fn all_zero_paintext() {
|
|
let des = des_instance();
|
|
|
|
let plain = 0;
|
|
let encrypted = des.encrypt(plain);
|
|
let decrypted = des.decrypt(encrypted);
|
|
assert_eq!(decrypted, plain, "All-zero plaintext failed");
|
|
}
|
|
|
|
#[test]
|
|
fn all_one_paintext() {
|
|
let des = des_instance();
|
|
|
|
let plain = 1;
|
|
let encrypted = des.encrypt(plain);
|
|
let decrypted = des.decrypt(encrypted);
|
|
assert_eq!(decrypted, plain, "All-one plaintext failed");
|
|
}
|
|
|
|
#[test]
|
|
fn different_inputs() {
|
|
let des = des_instance();
|
|
|
|
let plain1 = 0x0000000000000001;
|
|
let plain2 = 0x0000000000000002;
|
|
let enc1 = des.encrypt(plain1);
|
|
let enc2 = des.encrypt(plain2);
|
|
assert_ne!(
|
|
enc1, enc2,
|
|
"Encryption not deterministic for different inputs"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[should_panic(expected = "Invalid key size")]
|
|
fn invalid_key_size() {
|
|
DES::new(0);
|
|
}
|
|
|
|
#[test]
|
|
fn performance() {
|
|
let des = des_instance();
|
|
let plaintext = TEST_PLAINTEXT;
|
|
|
|
let start = Instant::now();
|
|
for _ in 0..10000 {
|
|
des.encrypt(plaintext);
|
|
}
|
|
let duration = start.elapsed();
|
|
|
|
println!("10k encryption took: {duration:?}");
|
|
// Reasonable benchmark: should be under 1ms on modern hardware
|
|
assert!(duration.as_millis() < 100, "Performance degraded");
|
|
}
|
|
|
|
#[test]
|
|
fn fuzz_properties() {
|
|
let des = des_instance();
|
|
|
|
for _ in 0..100 {
|
|
let plaintext = random();
|
|
let encrypted = des.encrypt(plaintext);
|
|
let decrypted = des.decrypt(plaintext);
|
|
|
|
assert_eq!(decrypted, encrypted, "Fuzz roundtrip failed");
|
|
assert_ne!(encrypted, plaintext, "Encryption is identity function");
|
|
|
|
let key2 = random();
|
|
if key2 != TEST_KEY {
|
|
let des2 = DES::new(key2);
|
|
let encrypted2 = des2.encrypt(plaintext);
|
|
assert_ne!(
|
|
encrypted, encrypted2,
|
|
"Different keys produced same encryption"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|