Permalink
Browse files

key schedule is implemented and fully tested

  • Loading branch information...
1 parent cbe720d commit 8b259526b91296ca370343b4743e1c51ecba2be7 Robert Sosinski committed Jul 30, 2008
Showing with 185 additions and 16 deletions.
  1. +1 −2 Rakefile
  2. +8 −4 lib/ruby-des/key_schedule.rb
  3. +0 −1 lib/rubydes.rb
  4. 0 test/feistel.yml
  5. +1 −1 test/feistel_test.rb
  6. +0 −4 test/helper.rb
  7. +120 −0 test/key_schedule.yml
  8. +35 −4 test/key_schedule_test.rb
  9. +20 −0 test/test_helper.rb
View
@@ -4,5 +4,4 @@ require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/*_test.rb']
- t.verbose = false
-end
+end
@@ -1,5 +1,6 @@
class KeySchedule
attr_accessor :sub_keys
+ attr_reader :key
PC_1_L = [0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09,
0x01, 0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12,
@@ -24,18 +25,20 @@ class KeySchedule
1, 2, 2, 2, 2, 2, 2, 1]
def initialize(key)
+ @key = key
+
c = [] # c[0] is the PC_1_L permutation of the key, c[1..16] are the results of each left shift.
d = [] # d[0] is the PC_1_R permutation of the key, d[1..16] are the results of each left shift.
k = [] # k[0..15] are the sub keys created by combining c[i] with d[i] and permuting with PC_2.
- # Get c[0] and d[0] by permuting the key with PC_1.
+ # Get c[0] and d[0] by permuting the key with PC_1_L and PC_1_R.
c << PC_1_L.collect{|p| key[p - 1]}
d << PC_1_R.collect{|p| key[p - 1]}
# Generate 16 sub keys with left-wise rotations and PC_2.
16.times do |i|
- # Create two new arrays of bits from the current arrays of bits specified by i.
+ # Create two new arrays of bits from the previous arrays of bits.
c << c[i]
d << d[i]
@@ -48,7 +51,8 @@ def initialize(key)
# Combine the new arrays (c and d) and permute the result with PC_2.
k << PC_2.collect{|p| (c[i + 1] + d[i + 1])[p - 1]}
end
-
- self.sub_keys = k
+
+ # 16 steps later, you have your 16 48 bit sub keys.
+ @sub_keys = k
end
end
View
@@ -1 +0,0 @@
-require 'ruby-des'
View
No changes.
View
@@ -1,4 +1,4 @@
-require 'helper'
+require 'test_helper'
class FeistelTest < Test::Unit::TestCase
def test_truth
View
@@ -1,4 +0,0 @@
-$:.unshift(File.dirname(__FILE__) + "../lib")
-
-require 'test/unit'
-require 'ruby-des'
View
@@ -0,0 +1,120 @@
+test_key: [1, 1, 1, 0, 0, 1, 0, 1,
+ 1, 1, 1, 0, 1, 0, 1, 0,
+ 1, 1, 0, 0, 0, 1, 0, 0,
+ 1, 1, 1, 1, 0, 0, 1, 0,
+ 0, 1, 0, 1, 1, 0, 1, 1,
+ 1, 1, 0, 0, 1, 0, 0, 0,
+ 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, 1,
+ 0, 1, 0, 1, 0, 0, 1, 0]
+
+sub_key_4: [0, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 0, 1, 1, 0, 0, 1,
+ 0, 1, 0, 1, 1, 0, 0, 1,
+ 1, 1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1, 0, 1, 0,
+ 1, 0, 1, 1, 1, 0, 0, 0,
+ 0, 1, 1, 1, 0, 0, 1, 1]
View
@@ -1,7 +1,38 @@
-require 'helper'
+require 'test_helper'
class KeyScheduleTest < Test::Unit::TestCase
- def test_truth
- assert true
+ fixtures :key_schedule
+
+ def setup
+ @key_schedule = KeySchedule.new(key_schedule(:test_key))
end
-end
+
+ def test_class
+ assert_kind_of KeySchedule, @key_schedule
+ end
+
+ def test_test_key
+ assert_kind_of Array, @key_schedule.key
+ assert_equal key_schedule(:test_key), @key_schedule.key
+ end
+
+ def test_sub_keys
+ assert_kind_of Array, @key_schedule.sub_keys
+ assert_equal key_schedule(:sub_key_1), @key_schedule.sub_keys[0]
+ assert_equal key_schedule(:sub_key_2), @key_schedule.sub_keys[1]
+ assert_equal key_schedule(:sub_key_3), @key_schedule.sub_keys[2]
+ assert_equal key_schedule(:sub_key_4), @key_schedule.sub_keys[3]
+ assert_equal key_schedule(:sub_key_5), @key_schedule.sub_keys[4]
+ assert_equal key_schedule(:sub_key_6), @key_schedule.sub_keys[5]
+ assert_equal key_schedule(:sub_key_7), @key_schedule.sub_keys[6]
+ assert_equal key_schedule(:sub_key_8), @key_schedule.sub_keys[7]
+ assert_equal key_schedule(:sub_key_9), @key_schedule.sub_keys[8]
+ assert_equal key_schedule(:sub_key_10), @key_schedule.sub_keys[9]
+ assert_equal key_schedule(:sub_key_11), @key_schedule.sub_keys[10]
+ assert_equal key_schedule(:sub_key_12), @key_schedule.sub_keys[11]
+ assert_equal key_schedule(:sub_key_13), @key_schedule.sub_keys[12]
+ assert_equal key_schedule(:sub_key_14), @key_schedule.sub_keys[13]
+ assert_equal key_schedule(:sub_key_15), @key_schedule.sub_keys[14]
+ assert_equal key_schedule(:sub_key_16), @key_schedule.sub_keys[15]
+ end
+end
View
@@ -0,0 +1,20 @@
+$:.unshift(File.dirname(__FILE__) + "../lib")
+
+require 'test/unit'
+require 'yaml'
+require 'ruby-des'
+
+class Test::Unit::TestCase
+ @@fixtures = {}
+ def self.fixtures(*list)
+ list.each do |fixture|
+ self.class_eval do
+ define_method(fixture) do |item|
+ @@fixtures[fixture] ||= File.exists?("#{fixture.to_s}.yml") ? YAML::load_file("#{fixture.to_s}.yml") : YAML::load_file("test/#{fixture.to_s}.yml")
+ @@fixtures[fixture][item.to_s]
+ end
+ end
+ end
+ end
+end
+

0 comments on commit 8b25952

Please sign in to comment.