Skip to content

Commit 4c3384b

Browse files
author
Ion Ostafi
committed
Added function to make initial permutation of the 64 bit message and also modified the split_keys function to support 64 bit keys.
1 parent a6c5929 commit 4c3384b

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/main.rs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ const KEY : i64 = 0x133457799BBCDFF1;
99
fn main() {
1010
let key_plus = generate_key_plus(KEY);
1111
let (left, right) = split_key(key_plus, 56);
12-
let subkeys = create_16_subkeys(left, right);
13-
for idx in 0..subkeys.len() {
14-
println!("[{0}]{1:b}\n[{0}]{2:b}\n\n",idx, subkeys[idx].0, subkeys[idx].1);
15-
}
12+
let subkey_pairs = create_16_subkeys(left, right);
13+
let subkeys_48_bit = convert_pairs_to_encrypted_48_bit_keys(subkey_pairs);
1614
//println!("{:b}\n{:b}", left, right);
1715
}
1816

@@ -39,9 +37,10 @@ pub fn generate_key_plus(key: i64) -> i64 {
3937
}
4038

4139

42-
pub fn split_key(key : i64, key_len: i8) -> (i64, i64) {
43-
let left_half = key >> key_len / 2;
44-
let right_half = (key << 64 - key_len + key_len / 2) >> 64 - key_len + key_len / 2;
40+
pub fn split_key(key : i64, key_len: u8) -> (i64, i64) {
41+
let half_size = key_len / 2;
42+
let left_half = (key >> half_size) & bit_pattern_containing_ones(half_size);
43+
let right_half = key & bit_pattern_containing_ones(half_size);
4544
(left_half,right_half)
4645
}
4746

@@ -95,5 +94,33 @@ pub fn key_kn_from_pair(left: i64, right: i64) -> i64 {
9594

9695

9796
pub fn convert_pairs_to_encrypted_48_bit_keys(pairs: Vec<(i64, i64)>) -> Vec<i64> {
98-
Vec::new()
97+
let mut keys_48_bit : Vec<i64> = Vec::new();
98+
for idx in 0..pairs.len() {
99+
keys_48_bit.push(key_kn_from_pair(pairs[idx].0, pairs[idx].1));
100+
}
101+
keys_48_bit
102+
}
103+
104+
105+
106+
//Step 2: Encode each 64-bit block of data.
107+
const IP : [u8; 64] = [
108+
58, 50,42, 34, 26,18, 10, 2,
109+
60, 52,44, 36, 28,20, 12, 4,
110+
62, 54,46, 38, 30,22, 14, 6,
111+
64, 56,48, 40, 32,24, 16, 8,
112+
57, 49,41, 33, 25,17, 9, 1,
113+
59, 51,43, 35, 27,19, 11, 3,
114+
61, 53,45, 37, 29,21, 13, 5,
115+
63, 55,47, 39, 31,23, 15, 7
116+
];
117+
//b = bit
118+
pub fn initial_permutation_of_64bit_message(message : i64) -> i64 {
119+
let mut permutation = 0i64;
120+
for idx in 0..64 {
121+
let bit_at_index_in_message = (message >> (64 - IP[idx])) & 1;
122+
permutation = permutation << 1;
123+
permutation = permutation | bit_at_index_in_message;
124+
}
125+
permutation
99126
}

src/main_tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,46 @@ fn creating_48_bit_key_based_on_maximum_pairs() {
109109
let expected = 0b1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111_1111;
110110
assert_eq!(expected, key_kn_from_pair(left, right));
111111
}
112+
113+
114+
#[test]
115+
fn get_48_bit_keys_from_array_of_28_bit_pairs() {
116+
let pairs_28_bit = vec![(0b1110000110011001010101011111,0b1010101011001100111100011110),
117+
(0b1100001100110010101010111111,0b0101010110011001111000111101),
118+
(0b0000110011001010101011111111,0b0101011001100111100011110101),
119+
(0b0011001100101010101111111100,0b0101100110011110001111010101),
120+
121+
(0b1100110010101010111111110000,0b0110011001111000111101010101),
122+
(0b0011001010101011111111000011,0b1001100111100011110101010101),
123+
(0b1100101010101111111100001100,0b0110011110001111010101010110),
124+
(0b0010101010111111110000110011,0b1001111000111101010101011001)];
125+
126+
let expected = vec![0b000110110000001011101111111111000111000001110010,
127+
0b011110011010111011011001110110111100100111100101,
128+
0b010101011111110010001010010000101100111110011001,
129+
0b011100101010110111010110110110110011010100011101,
130+
0b011111001110110000000111111010110101001110101000,
131+
0b011000111010010100111110010100000111101100101111,
132+
0b111011001000010010110111111101100001100010111100,
133+
0b111101111000101000111010110000010011101111111011];
134+
135+
assert_eq!(expected, convert_pairs_to_encrypted_48_bit_keys(pairs_28_bit));
136+
}
137+
138+
139+
140+
#[test]
141+
fn permutation_of_64bit_integer_gives_58_bit() {
142+
let message_64bit = 0x123456789abcdef;
143+
let intial_permutation = 0xcc00ccfff0aaf0aa;
144+
assert_eq!(intial_permutation, initial_permutation_of_64bit_message(message_64bit));
145+
}
146+
147+
148+
#[test]
149+
fn splitting_key_of_64_bit_into_32_bit_pair() {
150+
let key = 0xcc00ccfff0aaf0aa;
151+
let left = 0xcc00ccffi64;
152+
let right = 0xf0aaf0aai64;
153+
assert_eq!((left, right), split_key(key, 64));
154+
}

0 commit comments

Comments
 (0)