ruby-aes-optimized 1.0 → 1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,8 +21,7 @@
21
21
  <h1 class="section-bar">Classes</h1>
22
22
  <div id="index-entries">
23
23
  <a href="classes/Aes.html">Aes</a><br />
24
- <a href="classes/AesAlg.html">AesAlg</a><br />
25
- <a href="classes/AesCons.html">AesCons</a><br />
24
+ <a href="classes/AesShared.html">AesShared</a><br />
26
25
  </div>
27
26
  </div>
28
27
  </body>
@@ -24,8 +24,7 @@
24
24
  <a href="files/COPYING.html">COPYING</a><br />
25
25
  <a href="files/README.html">README</a><br />
26
26
  <a href="files/lib/ruby-aes_rb.html">lib/ruby-aes.rb</a><br />
27
- <a href="files/lib/ruby-aes/aes_alg_rb.html">lib/ruby-aes/aes_alg.rb</a><br />
28
- <a href="files/lib/ruby-aes/aes_cons_rb.html">lib/ruby-aes/aes_cons.rb</a><br />
27
+ <a href="files/lib/ruby-aes/aes_shared_rb.html">lib/ruby-aes/aes_shared.rb</a><br />
29
28
  </div>
30
29
  </div>
31
30
  </body>
@@ -20,37 +20,23 @@
20
20
  <div id="index">
21
21
  <h1 class="section-bar">Methods</h1>
22
22
  <div id="index-entries">
23
- <a href="classes/AesAlg.html#M000009">_decrypt_block (AesAlg)</a><br />
24
- <a href="classes/AesAlg.html#M000008">_encrypt_block (AesAlg)</a><br />
25
- <a href="classes/AesAlg.html#M000006">addRoundKey (AesAlg)</a><br />
26
- <a href="classes/Aes.html#M000019">bs (Aes)</a><br />
27
- <a href="classes/Aes.html#M000020">bs= (Aes)</a><br />
28
- <a href="classes/Aes.html#M000022">check_iv (Aes)</a><br />
29
- <a href="classes/Aes.html#M000021">check_key (Aes)</a><br />
30
- <a href="classes/Aes.html#M000024">check_kl (Aes)</a><br />
31
- <a href="classes/Aes.html#M000023">check_mode (Aes)</a><br />
32
- <a href="classes/AesAlg.html#M000005">decryptSubRound (AesAlg)</a><br />
33
- <a href="classes/Aes.html#M000027">decrypt_block (Aes)</a><br />
34
- <a href="classes/AesAlg.html#M000012">decrypt_block (AesAlg)</a><br />
35
- <a href="classes/AesAlg.html#M000014">decrypt_blocks (AesAlg)</a><br />
36
- <a href="classes/Aes.html#M000029">decrypt_buffer (Aes)</a><br />
37
- <a href="classes/AesAlg.html#M000016">decrypt_buffer (AesAlg)</a><br />
38
- <a href="classes/Aes.html#M000031">decrypt_stream (Aes)</a><br />
39
- <a href="classes/Aes.html#M000026">encrypt_block (Aes)</a><br />
40
- <a href="classes/AesAlg.html#M000011">encrypt_block (AesAlg)</a><br />
41
- <a href="classes/AesAlg.html#M000013">encrypt_blocks (AesAlg)</a><br />
42
- <a href="classes/AesAlg.html#M000015">encrypt_buffer (AesAlg)</a><br />
43
- <a href="classes/Aes.html#M000028">encrypt_buffer (Aes)</a><br />
44
- <a href="classes/Aes.html#M000030">encrypt_stream (Aes)</a><br />
45
- <a href="classes/AesAlg.html#M000002">imixColumns (AesAlg)</a><br />
46
- <a href="classes/AesAlg.html#M000017">init (AesAlg)</a><br />
47
- <a href="classes/Aes.html#M000025">init (Aes)</a><br />
48
- <a href="classes/AesAlg.html#M000007">key_expansion (AesAlg)</a><br />
49
- <a href="classes/AesAlg.html#M000004">lastEncryptRound (AesAlg)</a><br />
50
- <a href="classes/AesAlg.html#M000001">mixColumns (AesAlg)</a><br />
51
- <a href="classes/AesAlg.html#M000018">new (AesAlg)</a><br />
52
- <a href="classes/AesAlg.html#M000003">subShiftRows (AesAlg)</a><br />
53
- <a href="classes/AesAlg.html#M000010">xor (AesAlg)</a><br />
23
+ <a href="classes/Aes.html#M000005">bs (Aes)</a><br />
24
+ <a href="classes/Aes.html#M000006">bs= (Aes)</a><br />
25
+ <a href="classes/Aes.html#M000008">check_iv (Aes)</a><br />
26
+ <a href="classes/Aes.html#M000007">check_key (Aes)</a><br />
27
+ <a href="classes/Aes.html#M000010">check_kl (Aes)</a><br />
28
+ <a href="classes/Aes.html#M000009">check_mode (Aes)</a><br />
29
+ <a href="classes/Aes.html#M000013">decrypt_block (Aes)</a><br />
30
+ <a href="classes/AesShared.html#M000002">decrypt_blocks (AesShared)</a><br />
31
+ <a href="classes/Aes.html#M000015">decrypt_buffer (Aes)</a><br />
32
+ <a href="classes/AesShared.html#M000004">decrypt_buffer (AesShared)</a><br />
33
+ <a href="classes/Aes.html#M000017">decrypt_stream (Aes)</a><br />
34
+ <a href="classes/Aes.html#M000012">encrypt_block (Aes)</a><br />
35
+ <a href="classes/AesShared.html#M000001">encrypt_blocks (AesShared)</a><br />
36
+ <a href="classes/Aes.html#M000014">encrypt_buffer (Aes)</a><br />
37
+ <a href="classes/AesShared.html#M000003">encrypt_buffer (AesShared)</a><br />
38
+ <a href="classes/Aes.html#M000016">encrypt_stream (Aes)</a><br />
39
+ <a href="classes/Aes.html#M000011">init (Aes)</a><br />
54
40
  </div>
55
41
  </div>
56
42
  </body>
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'rubygems'
3
4
  require 'ruby-aes'
4
5
  require 'example_helper'
5
6
 
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'rubygems'
3
4
  require 'ruby-aes'
4
5
  require 'example_helper'
5
6
 
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'rubygems'
3
4
  require 'ruby-aes'
4
5
  require 'example_helper'
5
6
  require 'fileutils'
data/lib/ruby-aes.rb CHANGED
@@ -47,7 +47,7 @@ module Aes
47
47
 
48
48
  def Aes.check_key(key_string, kl = 128)
49
49
  kl = Aes.check_kl(kl)
50
- k = key_string.length
50
+ k = key_string ? key_string.length : 0
51
51
  raise "Bad key string or bad key length" if (k != kl/8) && (k != kl/4)
52
52
  hex = (key_string =~ /[a-f0-9A-F]{#{k}}/) == 0 && (k == kl/4)
53
53
  bin = ! hex
@@ -131,8 +131,11 @@ module Aes
131
131
  sout << @@aes.encrypt_buffer(sin)
132
132
  when IO
133
133
  while buf = sin.read(@@bs)
134
- sout << ((buf.length % 16).zero? ? @@aes.encrypt_blocks(buf) :
135
- @@aes.encrypt_buffer(buf))
134
+ if buf.length == @@bs
135
+ sout << @@aes.encrypt_blocks(buf)
136
+ else
137
+ sout << @@aes.encrypt_buffer(buf)
138
+ end
136
139
  end
137
140
  else
138
141
  raise "Bad input stream (String, IO)"
@@ -150,9 +153,12 @@ module Aes
150
153
  when String
151
154
  sout << @@aes.decrypt_buffer(sin)
152
155
  when IO
153
- while buf = sin.read(@@bs)
154
- sout << (sin.eof? ? @@aes.decrypt_buffer(buf) :
155
- @@aes.decrypt_blocks(buf))
156
+ while buf = sin.read(@@bs)#+1)
157
+ if buf.length == @@bs
158
+ sout << @@aes.decrypt_blocks(buf)
159
+ else
160
+ sout << @@aes.decrypt_buffer(buf)
161
+ end
156
162
  end
157
163
  else
158
164
  raise "Bad input stream (String, IO)"
@@ -8,9 +8,11 @@
8
8
  =end
9
9
 
10
10
  require 'ruby-aes/aes_cons'
11
+ require 'ruby-aes/aes_shared'
11
12
 
12
13
  class AesAlg
13
14
  include AesCons
15
+ include AesShared
14
16
 
15
17
  def mixColumns
16
18
  t = ""
@@ -184,64 +186,6 @@ class AesAlg
184
186
  end
185
187
  end
186
188
 
187
- def encrypt_blocks(buffer)
188
- raise "Bad block length" unless (buffer.length % 16).zero?
189
- ct = ""
190
- block = ""
191
- buffer.each_byte do |char|
192
- block << char
193
- if block.length == 16
194
- ct << encrypt_block(block)
195
- block = ""
196
- end
197
- end
198
- end
199
-
200
- def decrypt_blocks(buffer)
201
- raise "Bad block length" unless (buffer.length % 16).zero?
202
- pt = ""
203
- block = ""
204
- buffer.each_byte do |char|
205
- block << char
206
- if block.length == 16
207
- pt << decrypt_block(block)
208
- block = ""
209
- end
210
- end
211
- end
212
-
213
- def encrypt_buffer(buffer)
214
- ct = ""
215
- block = ""
216
- buffer.each_byte do |char|
217
- block << char
218
- if block.length == 16
219
- ct << encrypt_block(block)
220
- block = ""
221
- end
222
- end
223
- m = 16 - block.length % 16
224
- ct << (m == 16 ? 0 : encrypt_block(block << m.chr * m))
225
- end
226
-
227
- def decrypt_buffer(buffer)
228
- pt = ""
229
- block = ""
230
- buffer.each_byte do |char|
231
- block << char
232
- if block.length == 16
233
- pt << decrypt_block(block)
234
- block = ""
235
- end
236
- end
237
- if block.length == 0
238
- c = pt[-1]
239
- c.chr * c == pt[-c..-1] ? pt[0..-(c+1)] : (raise "Bad Block Padding")
240
- else
241
- pt
242
- end
243
- end
244
-
245
189
  def init(key_length, mode, key, iv = nil)
246
190
  @iv = "\000" * 16
247
191
  @iv = iv if iv
@@ -0,0 +1,77 @@
1
+ =begin
2
+ This file is a part of ruby-aes <http://rubyforge.org/projects/ruby-aes>
3
+ Written by Alex Boussinet <alex.boussinet@gmail.com>
4
+
5
+ It contains the code shared by all the implementations
6
+ =end
7
+
8
+ module AesShared
9
+ def encrypt_blocks(buffer)
10
+ raise "Bad block length" unless (buffer.length % 16).zero?
11
+ ct = ""
12
+ block = ""
13
+ buffer.each_byte do |char|
14
+ block << char
15
+ if block.length == 16
16
+ ct << encrypt_block(block)
17
+ block = ""
18
+ end
19
+ end
20
+ ct
21
+ end
22
+
23
+ def decrypt_blocks(buffer)
24
+ raise "Bad block length" unless (buffer.length % 16).zero?
25
+ pt = ""
26
+ block = ""
27
+ buffer.each_byte do |char|
28
+ block << char
29
+ if block.length == 16
30
+ pt << decrypt_block(block)
31
+ block = ""
32
+ end
33
+ end
34
+ pt
35
+ end
36
+
37
+ def encrypt_buffer(buffer)
38
+ ct = ""
39
+ block = ""
40
+ buffer.each_byte do |char|
41
+ block << char
42
+ if block.length == 16
43
+ ct << encrypt_block(block)
44
+ block = ""
45
+ end
46
+ end
47
+ c = "\000"
48
+ if (m = 16 - block.length % 16) != 16
49
+ c = m.chr
50
+ ct << encrypt_block(block << c * m)
51
+ end
52
+ ct << c
53
+ end
54
+
55
+ def decrypt_buffer(buffer)
56
+ pt = ""
57
+ block = ""
58
+ buffer.each_byte do |char|
59
+ block << char
60
+ if block.length == 16
61
+ pt << decrypt_block(block)
62
+ block = ""
63
+ end
64
+ end
65
+ if block.length != 1
66
+ raise 'Bad Block Padding'
67
+ elsif (c = block[-1]).zero?
68
+ pt
69
+ else
70
+ if block * c == pt[-c..-1]
71
+ pt[0..-c-1]
72
+ else
73
+ raise "Bad Block Padding"
74
+ end
75
+ end
76
+ end
77
+ end
data/test/test_helper.rb CHANGED
@@ -3,8 +3,10 @@ require File.dirname(__FILE__) + '/../lib/ruby-aes'
3
3
 
4
4
  require 'fileutils'
5
5
 
6
- KEY_LENGTH = [128,192,256].freeze
7
- MODES = ['ECB','CBC','OFB','CFB'].freeze
6
+ unless defined? KEY_LENGTH
7
+ KEY_LENGTH = [128,192,256].freeze
8
+ MODES = ['ECB','CBC','OFB','CFB'].freeze
9
+ end
8
10
 
9
11
  def random_fill(n, buffer)
10
12
  n.times do
@@ -13,24 +13,28 @@ class TestRubyAES < Test::Unit::TestCase
13
13
  random_fill(16, @iv)
14
14
  @pt = ""
15
15
  random_fill(64, @pt)
16
- @kl = KEY_LENGTH[(rand * KEY_LENGTH).to_i]
16
+ @kl = KEY_LENGTH[(rand * KEY_LENGTH.length).to_i]
17
17
  @mode = MODES[(rand * MODES.length).to_i]
18
18
  end
19
19
 
20
20
  def test_modes_and_key_lengths
21
+ pt = @pt[0...16]
21
22
  MODES.each do |mode|
22
23
  KEY_LENGTH.each do |kl|
23
- ct = Aes.encrypt_block(kl, mode, @keys[kl], @iv, @pt)
24
+ ct = Aes.encrypt_block(kl, mode, @keys[kl], @iv, pt)
24
25
  npt = Aes.decrypt_block(kl, mode, @keys[kl], @iv, ct)
25
- assert_equal(@pt, npt, "Error in encryption/decryption (#{kl}-#{mode})")
26
+ assert_equal(pt, npt, "Error in encryption/decryption (#{kl}-#{mode})")
26
27
  end
27
28
  end
28
29
  end
29
30
 
30
- def test_encrypt_stream
31
+ def test_encrypt_decrypt_stream
31
32
  file = "_ruby-aes_test_encrypt_stream_"
32
33
  sin = File.open(file, "w+b")
33
34
  random_fill(4242, sin)
35
+ sin.close
36
+
37
+ sin = File.open(file, "rb")
34
38
  sout = File.open("#{file}.aes", "w+b")
35
39
  Aes.encrypt_stream(@kl, @mode, @keys[@kl], @iv, sin, sout)
36
40
  sin.close
@@ -42,18 +46,27 @@ class TestRubyAES < Test::Unit::TestCase
42
46
  sin.close
43
47
  sout.close
44
48
 
45
- assert_equal IO.read(file), IO.read("#{file}.plain"),
46
- "Error in encrypt_stream"
49
+ pt, npt = IO.read(file), IO.read("#{file}.plain")
50
+
51
+ assert_equal pt, npt, "Error in encrypt_decrypt_stream"
52
+ ensure
47
53
  FileUtils.rm_f [ file, "#{file}.aes", "#{file}.plain" ]
48
54
  end
49
55
 
50
- def test_encrypt_buffer
56
+ def test_encrypt_decrypt_buffer
57
+ MODES.each do |mode|
58
+ KEY_LENGTH.each do |kl|
59
+ ct = Aes.encrypt_buffer(kl, mode, @keys[kl], @iv, @pt)
60
+ npt = Aes.decrypt_buffer(kl, mode, @keys[kl], @iv, ct)
61
+ assert_equal(@pt, npt, "Error in encrypt_decrypt_buffer")
62
+ end
63
+ end
51
64
  pt = ""
52
65
  42.times do
53
66
  pt << random_fill(1, pt)
54
67
  ct = Aes.encrypt_buffer(@kl, @mode, @keys[@kl], @iv, pt)
55
68
  npt = Aes.decrypt_buffer(@kl, @mode, @keys[@kl], @iv, ct)
56
- assert_equal(pt, npt, "Error in encrypt buffer (padding)")
69
+ assert_equal(pt, npt, "Error in encrypt_decrypt_buffer")
57
70
  end
58
71
  end
59
72
 
@@ -79,19 +92,19 @@ class TestRubyAES < Test::Unit::TestCase
79
92
 
80
93
  def test_check_key
81
94
  assert_raise(RuntimeError) do
82
- Aes.check_key(@key, 64)
95
+ Aes.check_key(@keys[128], 64)
83
96
  end # bad key length
84
97
  assert_raise(RuntimeError) do
85
98
  Aes.check_key('123', 128)
86
99
  end # bad key string
87
100
  assert_raise(RuntimeError) do
88
- Aes.check_key(@key, 256)
101
+ Aes.check_key(nil, 256)
89
102
  end # bad key string
90
103
  assert_raise(RuntimeError) do
91
- Aes.check_key(@key.unpack('H*'), 128)
104
+ Aes.check_key(@keys[128].unpack('H*'), 128)
92
105
  end # bad key string
93
106
  assert_nothing_raised do
94
- Aes.check_key(@key, @kl)
107
+ Aes.check_key(@keys[@kl], @kl)
95
108
  end
96
109
  end
97
110
 
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.3
2
+ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: ruby-aes-optimized
5
5
  version: !ruby/object:Gem::Version
6
- version: "1.0"
7
- date: 2007-08-01 00:00:00 +02:00
6
+ version: "1.1"
7
+ date: 2008-01-05 00:00:00 +01:00
8
8
  summary: ruby-aes is an implementation of the Rijndael algorithm (AES)
9
9
  require_paths:
10
10
  - lib
@@ -35,6 +35,13 @@ files:
35
35
  - Rakefile
36
36
  - doc/rdoc
37
37
  - doc/rdoc/files
38
+ - doc/rdoc/files/lib
39
+ - doc/rdoc/files/lib/ruby-aes_rb.html
40
+ - doc/rdoc/files/lib/ruby-aes
41
+ - doc/rdoc/files/lib/ruby-aes/aes_shared_rb.html
42
+ - doc/rdoc/files/CHANGELOG.html
43
+ - doc/rdoc/files/README.html
44
+ - doc/rdoc/files/COPYING.html
38
45
  - doc/rdoc/index.html
39
46
  - doc/rdoc/rdoc-style.css
40
47
  - doc/rdoc/fr_method_index.html
@@ -42,20 +49,15 @@ files:
42
49
  - doc/rdoc/fr_file_index.html
43
50
  - doc/rdoc/created.rid
44
51
  - doc/rdoc/classes
45
- - doc/rdoc/files/lib
46
- - doc/rdoc/files/CHANGELOG.html
47
- - doc/rdoc/files/README.html
48
- - doc/rdoc/files/COPYING.html
49
- - doc/rdoc/files/lib/ruby-aes_rb.html
50
52
  - doc/rdoc/classes/Aes.html
53
+ - doc/rdoc/classes/AesShared.html
51
54
  - examples/encrypt_block.rb
52
55
  - examples/example_helper.rb
53
56
  - examples/encrypt_stream.rb
54
57
  - examples/encrypt_buffer.rb
55
58
  - lib/ruby-aes
59
+ - lib/ruby-aes/aes_shared.rb
56
60
  - lib/ruby-aes.rb
57
- - lib/ruby-aes/aes_alg.rb
58
- - lib/ruby-aes/aes_cons.rb
59
61
  - test/test_ruby-aes.rb
60
62
  - test/test_helper.rb
61
63
  - test/KAT_MCT
@@ -64,6 +66,8 @@ files:
64
66
  - test/KAT_MCT/table.192
65
67
  - test/KAT_MCT/table.256
66
68
  - test/KAT_MCT/aes_kat_mct.rb
69
+ - lib/ruby-aes/aes_alg.rb
70
+ - lib/ruby-aes/aes_cons.rb
67
71
  test_files:
68
72
  - test/test_ruby-aes.rb
69
73
  - test/test_helper.rb