11#[ cfg( test) ]
22mod main_tests;
33
4- // const MESS : i64 = 0x0123456789ABCDEF;
4+ const MESS : i64 = 0x0123456789ABCDEF ;
55const KEY : i64 = 0x133457799BBCDFF1 ;
66
77
@@ -10,7 +10,10 @@ fn main() {
1010 let key_plus = generate_key_plus ( KEY ) ;
1111 let ( left, right) = split_key ( key_plus, 56 ) ;
1212 let subkey_pairs = create_16_subkeys ( left, right) ;
13- let subkeys_48_bit = convert_pairs_to_encrypted_48_bit_keys ( subkey_pairs) ;
13+ let _subkeys_48_bit = convert_pairs_to_encrypted_48_bit_keys ( subkey_pairs) ;
14+
15+ let message_permutation = initial_permutation_of_64bit_message ( MESS ) ;
16+ let ( left_message, right_message) = split_key ( message_permutation, 64 ) ;
1417 //println!("{:b}\n{:b}", left, right);
1518}
1619
@@ -39,8 +42,8 @@ pub fn generate_key_plus(key: i64) -> i64 {
3942
4043pub fn split_key ( key : i64 , key_len : u8 ) -> ( i64 , i64 ) {
4144 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) ;
45+ let left_half = ( key >> half_size) & bit_pattern_ones ( half_size) ;
46+ let right_half = key & bit_pattern_ones ( half_size) ;
4447 ( left_half, right_half)
4548}
4649
@@ -51,21 +54,22 @@ pub fn create_16_subkeys(left_half: i64, right_half: i64) -> Vec<(i64, i64)> {
5154 let mut subkeys : Vec < ( i64 , i64 ) > = Vec :: new ( ) ;
5255 subkeys. push ( ( left_half, right_half) ) ;
5356 for idx in 0 ..16 {
54- let next_left = bit_rotate_left ( subkeys[ idx] . 0 , LEFT_SHIFTS [ idx] ) ;
55- let next_right = bit_rotate_left ( subkeys[ idx] . 1 , LEFT_SHIFTS [ idx] ) ;
57+ let next_left = bit_rotate_left ( subkeys[ idx] . 0 , LEFT_SHIFTS [ idx] , 28 ) ;
58+ let next_right = bit_rotate_left ( subkeys[ idx] . 1 , LEFT_SHIFTS [ idx] , 28 ) ;
5659 subkeys. push ( ( next_left, next_right) ) ;
5760 }
5861 subkeys. remove ( 0 ) ;
5962 subkeys
6063}
6164
62- pub fn bit_rotate_left ( bit_array : i64 , rol_count : u8 ) -> i64 {
63- let rotated_bit = ( ( bit_pattern_containing_ones ( rol_count) << 28 - rol_count) & bit_array) >> 28 - rol_count;
64- ( ( bit_array << rol_count) & 0xffff_fff ) | rotated_bit
65+ pub fn bit_rotate_left ( bit_pattern : i64 , rol_count : u8 , pattern_len : u8 ) -> i64 {
66+ let ones_for_rolled_bits = bit_pattern_ones ( rol_count) << pattern_len - rol_count;
67+ let rotated_bits = ( ( ones_for_rolled_bits) & bit_pattern) >> pattern_len - rol_count;
68+ ( ( bit_pattern << rol_count) & bit_pattern_ones ( pattern_len) ) | rotated_bits
6569}
6670
6771
68- pub fn bit_pattern_containing_ones ( how_much : u8 ) -> i64 {
72+ pub fn bit_pattern_ones ( how_much : u8 ) -> i64 {
6973 ( ( 2u64 ) . pow ( how_much as u32 ) - 1 ) as i64
7074}
7175
0 commit comments