@@ -128,6 +128,106 @@ fn initial_permutation_of_64bit_message(message : i64) -> i64 {
128128}
129129
130130
131+ const E_TABLE : [ u8 ; 48 ] = [
132+ 32 , 1 , 2 , 3 , 4 , 5 ,
133+ 4 , 5 , 6 , 7 , 8 , 9 ,
134+ 8 , 9 , 10 , 11 , 12 , 13 ,
135+ 12 , 13 , 14 , 15 , 16 , 17 ,
136+ 16 , 17 , 18 , 19 , 20 , 21 ,
137+ 20 , 21 , 22 , 23 , 24 , 25 ,
138+ 24 , 25 , 26 , 27 , 28 , 29 ,
139+ 28 , 29 , 30 , 31 , 32 , 1
140+ ] ;
141+
142+ fn encode_function ( block_32bit : i64 , block_48bit : i64 ) -> i64 {
143+ let expanded_block = expand_32bit_block_to_48bit_block_using_Etable ( block_32bit) ;
144+ let xored = block_48bit ^ expanded_block;
145+ 0
146+ }
147+
148+
149+ fn expand_32bit_block_to_48bit_block_using_Etable ( block : i64 ) -> i64 {
150+ let mut expanded = 0i64 ;
151+ for idx in 0 ..48 {
152+ let bit_at_index = ( block >> ( 32 - E_TABLE [ idx] ) ) & 1 ;
153+ expanded = expanded << 1 ;
154+ expanded = expanded | bit_at_index;
155+ }
156+ expanded
157+ }
158+
159+
160+ fn shrink_48bit_block_to_32bit_block_with_Stables ( block_48bit : i64 ) -> i64 {
161+ let mut shrinked = 0i64 ;
162+ let block_6bit_count = 8 ;
163+ for idx in 0 ..block_6bit_count {
164+ let ones_at_block_index = bit_pattern_ones ( 6 ) << ( 42 - 6 * idx) ;
165+ let only_6bit_block = ( ( ones_at_block_index) & block_48bit) ;
166+ let block_shited_left = only_6bit_block >> ( 42 - 6 * idx) ;
167+ let row_idx = ( block_shited_left & 0b00001 ) | ( ( block_shited_left & 0b100000 ) >> 4 ) ;
168+ let col_idx = ( block_shited_left & 0b011110 ) >> 1 ;
169+ let block_4bit = value_from_S_at_position ( ( idx + 1 ) as u8 , row_idx as u8 , col_idx as u8 ) as i64 ;
170+ shrinked = ( shrinked << 4 ) | block_4bit;
171+ }
172+ shrinked
173+ }
174+
175+
176+ fn value_from_S_at_position ( theS : u8 , row : u8 , col : u8 ) -> u8 {
177+ match theS {
178+ 1 if row < 4 && col < 16 => S1 [ ( row * 16 + col) as usize ] ,
179+ 2 if row < 4 && col < 16 => S2 [ ( row * 16 + col) as usize ] ,
180+ 3 if row < 4 && col < 16 => S3 [ ( row * 16 + col) as usize ] ,
181+ 4 if row < 4 && col < 16 => S4 [ ( row * 16 + col) as usize ] ,
182+ 5 if row < 4 && col < 16 => S5 [ ( row * 16 + col) as usize ] ,
183+ 6 if row < 4 && col < 16 => S6 [ ( row * 16 + col) as usize ] ,
184+ 7 if row < 4 && col < 16 => S7 [ ( row * 16 + col) as usize ] ,
185+ 8 if row < 4 && col < 16 => S8 [ ( row * 16 + col) as usize ] ,
186+ _ => 0
187+ }
188+ }
189+
190+
191+ //S-Boxes :
192+ const S1 : [ u8 ; 64 ] = [ 14 , 4 , 13 , 1 , 2 , 15 , 11 , 8 , 3 , 10 , 6 , 12 , 5 , 9 , 0 , 7 ,
193+ 0 , 15 , 7 , 4 , 14 , 2 , 13 , 1 , 10 , 6 , 12 , 11 , 9 , 5 , 3 , 8 ,
194+ 4 , 1 , 14 , 8 , 13 , 6 , 2 , 11 , 15 , 12 , 9 , 7 , 3 , 10 , 5 , 0 ,
195+ 15 , 12 , 8 , 2 , 4 , 9 , 1 , 7 , 5 , 11 , 3 , 14 , 10 , 0 , 6 , 13 ] ;
196+
197+ const S2 : [ u8 ; 64 ] = [ 15 , 1 , 8 , 14 , 6 , 11 , 3 , 4 , 9 , 7 , 2 , 13 , 12 , 0 , 5 , 10 ,
198+ 3 , 13 , 4 , 7 , 15 , 2 , 8 , 14 , 12 , 0 , 1 , 10 , 6 , 9 , 11 , 5 ,
199+ 0 , 14 , 7 , 11 , 10 , 4 , 13 , 1 , 5 , 8 , 12 , 6 , 9 , 3 , 2 , 15 ,
200+ 13 , 8 , 10 , 1 , 3 , 15 , 4 , 2 , 11 , 6 , 7 , 12 , 0 , 5 , 14 , 9 ] ;
201+
202+ const S3 : [ u8 ; 64 ] = [ 10 , 0 , 9 , 14 , 6 , 3 , 15 , 5 , 1 , 13 , 12 , 7 , 11 , 4 , 2 , 8 ,
203+ 13 , 7 , 0 , 9 , 3 , 4 , 6 , 10 , 2 , 8 , 5 , 14 , 12 , 11 , 15 , 1 ,
204+ 13 , 6 , 4 , 9 , 8 , 15 , 3 , 0 , 11 , 1 , 2 , 12 , 5 , 10 , 14 , 7 ,
205+ 1 , 10 , 13 , 0 , 6 , 9 , 8 , 7 , 4 , 15 , 14 , 3 , 11 , 5 , 2 , 12 ] ;
206+
207+ const S4 : [ u8 ; 64 ] = [ 7 , 13 , 14 , 3 , 0 , 6 , 9 , 10 , 1 , 2 , 8 , 5 , 11 , 12 , 4 , 15 ,
208+ 13 , 8 , 11 , 5 , 6 , 15 , 0 , 3 , 4 , 7 , 2 , 12 , 1 , 10 , 14 , 9 ,
209+ 10 , 6 , 9 , 0 , 12 , 11 , 7 , 13 , 15 , 1 , 3 , 14 , 5 , 2 , 8 , 4 ,
210+ 3 , 15 , 0 , 6 , 10 , 1 , 13 , 8 , 9 , 4 , 5 , 11 , 12 , 7 , 2 , 14 ] ;
211+
212+ const S5 : [ u8 ; 64 ] = [ 2 , 12 , 4 , 1 , 7 , 10 , 11 , 6 , 8 , 5 , 3 , 15 , 13 , 0 , 14 , 9 ,
213+ 14 , 11 , 2 , 12 , 4 , 7 , 13 , 1 , 5 , 0 , 15 , 10 , 3 , 9 , 8 , 6 ,
214+ 4 , 2 , 1 , 11 , 10 , 13 , 7 , 8 , 15 , 9 , 12 , 5 , 6 , 3 , 0 , 14 ,
215+ 11 , 8 , 12 , 7 , 1 , 14 , 2 , 13 , 6 , 15 , 0 , 9 , 10 , 4 , 5 , 3 ] ;
216+
217+ const S6 : [ u8 ; 64 ] = [ 12 , 1 , 10 , 15 , 9 , 2 , 6 , 8 , 0 , 13 , 3 , 4 , 14 , 7 , 5 , 11 ,
218+ 10 , 15 , 4 , 2 , 7 , 12 , 9 , 5 , 6 , 1 , 13 , 14 , 0 , 11 , 3 , 8 ,
219+ 9 , 14 , 15 , 5 , 2 , 8 , 12 , 3 , 7 , 0 , 4 , 10 , 1 , 13 , 11 , 6 ,
220+ 4 , 3 , 2 , 12 , 9 , 5 , 15 , 10 , 11 , 14 , 1 , 7 , 6 , 0 , 8 , 13 ] ;
221+
222+ const S7 : [ u8 ; 64 ] = [ 4 , 11 , 2 , 14 , 15 , 0 , 8 , 13 , 3 , 12 , 9 , 7 , 5 , 10 , 6 , 1 ,
223+ 13 , 0 , 11 , 7 , 4 , 9 , 1 , 10 , 14 , 3 , 5 , 12 , 2 , 15 , 8 , 6 ,
224+ 1 , 4 , 11 , 13 , 12 , 3 , 7 , 14 , 10 , 15 , 6 , 8 , 0 , 5 , 9 , 2 ,
225+ 6 , 11 , 13 , 8 , 1 , 4 , 10 , 7 , 9 , 5 , 0 , 15 , 14 , 2 , 3 , 12 ] ;
226+
227+ const S8 : [ u8 ; 64 ] = [ 13 , 2 , 8 , 4 , 6 , 15 , 11 , 1 , 10 , 9 , 3 , 14 , 5 , 0 , 12 , 7 ,
228+ 1 , 15 , 13 , 8 , 10 , 3 , 7 , 4 , 12 , 5 , 6 , 11 , 0 , 14 , 9 , 2 ,
229+ 7 , 11 , 4 , 1 , 9 , 12 , 14 , 2 , 0 , 6 , 10 , 13 , 15 , 3 , 5 , 8 ,
230+ 2 , 1 , 14 , 7 , 4 , 10 , 8 , 13 , 15 , 12 , 9 , 0 , 3 , 5 , 6 , 11 ] ;
131231
132232
133233//tests
@@ -157,7 +257,7 @@ fn ones_for_2_is_11() {
157257#[ cfg( test) ]
158258#[ test]
159259fn creating_vector_with_keys_returns_correct_subkeys ( ) {
160- let subkeys = create_16_subkeys ( 0b1111000011001100101010101111 , 0b0101010101100110011110001111 ) ;
260+ let subkeys = create_16_subkeys ( 0xf0ccaaf , 0x556678f ) ;
161261 assert_eq ! ( 16 , subkeys. len( ) ) ;
162262 //1
163263 assert_eq ! ( 0b1110000110011001010101011111 , subkeys[ 0 ] . 0 ) ;
@@ -267,6 +367,7 @@ fn get_48_bit_keys_from_array_of_28_bit_pairs() {
267367 0b011110011010111011011001110110111100100111100101 ,
268368 0b010101011111110010001010010000101100111110011001 ,
269369 0b011100101010110111010110110110110011010100011101 ,
370+
270371 0b011111001110110000000111111010110101001110101000 ,
271372 0b011000111010010100111110010100000111101100101111 ,
272373 0b111011001000010010110111111101100001100010111100 ,
@@ -293,3 +394,35 @@ fn splitting_key_of_64_bit_into_32_bit_pair() {
293394 let right = 0xf0aaf0aai64 ;
294395 assert_eq ! ( ( left, right) , split_key( key, 64 ) ) ;
295396}
397+
398+
399+ #[ cfg( test) ]
400+ #[ test]
401+ //1111 0000 1010 1010 1111 0000 1010 1010 ->
402+ //0111 1010 0001 0101 0101 0101 0111 1010 0001 0101 0101 0101
403+ fn expand_f0aaf0aa_using_Etable_will_result_7a15557a1555 ( ) {
404+ let block_32bit = 0b11110000101010101111000010101010 ;
405+ let expected_block = 0b011110100001010101010101011110100001010101010101 ;
406+ assert_eq ! ( expected_block, expand_32bit_block_to_48bit_block_using_Etable( block_32bit) ) ;
407+ }
408+
409+ #[ cfg( test) ]
410+ #[ test]
411+ //0110 0001 0001 0111 1011 1010 1000 0110 0110 0101 0010 0111 ->
412+ //0101 1100 1000 0010 1011 0101 1001 0111
413+ fn shirnk_6117ba866537_using_Stable_will_result_5c82b597 ( ) {
414+ let block_48bit = 0x6117ba866527 ;
415+ let expected_output = 0x5c82b597 ;
416+ assert_eq ! ( expected_output, shrink_48bit_block_to_32bit_block_with_Stables( block_48bit) ) ;
417+ }
418+
419+
420+ #[ cfg( test) ]
421+ #[ test]
422+ fn value_in_5th_S_position_2_10_is_12 ( ) {
423+ let Stable_index = 5u8 ;
424+ let row = 2u8 ;
425+ let col = 10u8 ;
426+ let expected = 12u8 ;
427+ assert_eq ! ( expected, value_from_S_at_position( Stable_index , row, col) ) ;
428+ }
0 commit comments