Skip to content

Commit

Permalink
s-boxes working, just steps away from making a full feistel network!
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Sosinski committed Jul 30, 2008
1 parent bf99757 commit df8bf36
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 119 deletions.
33 changes: 25 additions & 8 deletions lib/ruby-des/feistel.rb
Expand Up @@ -56,24 +56,41 @@ module Feistel
0x01, 0x0f, 0x0d, 0x08, 0x0a, 0x03, 0x07, 0x04, 0x0c, 0x05, 0x06, 0x0b, 0x00, 0x0e, 0x09, 0x02,
0x07, 0x0b, 0x04, 0x01, 0x09, 0x0c, 0x0e, 0x02, 0x00, 0x06, 0x0a, 0x0d, 0x0f, 0x03, 0x05, 0x08,
0x02, 0x01, 0x0e, 0x07, 0x04, 0x0a, 0x08, 0x0d, 0x0f, 0x0c, 0x09, 0x00, 0x03, 0x05, 0x06, 0x0b]

def self.run(half_block, k, round)
e = [] # e[0..47] is the expanded half block created with the E permutation.
e_xor_k = [] # e_xor_k[0..47] is the result of x-oring e with the current sub key.

S = [S1, S2, S3, S4, S5, S6, S7, S8]

# Some test data
#
# hb = [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1]
# k = [1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1]

def self.run(half_block, k)
b = [] # b[0..7] is e_xor_k prepped as 8 6-bit arrays for sbox substitution.
m = [] #
n = []

e << E.collect{|p| half_block[p - 1]}

e = E.collect{|p| half_block[p - 1]}
e_xor_k = XOR.run(e, k)

8.times do |i|
b << []
6.times do
b[i] << e_xor_k.shift
end

m << (b[i].first.to_s + b[i].last.to_s).to_i(2) * 16
n << b[i][1..4].to_s.to_i(2)
end

row = (e_xor_k.shift + e_xor_k.pop).to_i
col = e_xor_k.to_s.to_i(2)
b[0] = S1[m[0] + n[0]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[1] = S2[m[1] + n[1]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[2] = S3[m[2] + n[2]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[3] = S4[m[3] + n[3]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[4] = S5[m[4] + n[4]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[5] = S6[m[5] + n[5]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[6] = S7[m[6] + n[6]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}
b[7] = S8[m[7] + n[7]].to_s(2).rjust(4, '0').split('').collect{|bit| bit.to_i}

return b
end
end
254 changes: 143 additions & 111 deletions test/key_schedule.yml
Expand Up @@ -7,114 +7,146 @@ test_key: [1, 1, 1, 0, 0, 1, 0, 1,
1, 1, 0, 0, 1, 0, 1, 1,
1, 1, 1, 0, 0, 1, 1, 0]

sub_key_1: [1, 0, 1, 0, 1, 1, 0, 1,
1, 1, 0, 1, 1, 0, 1, 1,
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 1,
1, 0, 1, 0, 1, 1, 1, 1]

sub_key_2: [1, 0, 1, 0, 1, 1, 1, 1,
1, 1, 1, 1, 0, 1, 1, 1,
1, 1, 0, 1, 1, 0, 1, 0,
0, 0, 1, 0, 0, 1, 1, 1,
1, 1, 1, 0, 1, 1, 0, 1,
0, 0, 1, 1, 0, 0, 0, 0]

sub_key_3: [0, 1, 1, 1, 1, 1, 1, 0,
0, 1, 0, 1, 0, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
0, 0, 1, 0, 1, 0, 0, 1,
0, 0, 1, 0, 1, 1, 0, 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