ruby-aes-normal 1.0 → 1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -21,6 +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/AesShared.html">AesShared</a><br />
24
25
  </div>
25
26
  </div>
26
27
  </body>
@@ -24,6 +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_shared_rb.html">lib/ruby-aes/aes_shared.rb</a><br />
27
28
  </div>
28
29
  </div>
29
30
  </body>
@@ -20,19 +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/Aes.html#M000001">bs (Aes)</a><br />
24
- <a href="classes/Aes.html#M000002">bs= (Aes)</a><br />
25
- <a href="classes/Aes.html#M000004">check_iv (Aes)</a><br />
26
- <a href="classes/Aes.html#M000003">check_key (Aes)</a><br />
27
- <a href="classes/Aes.html#M000006">check_kl (Aes)</a><br />
28
- <a href="classes/Aes.html#M000005">check_mode (Aes)</a><br />
29
- <a href="classes/Aes.html#M000009">decrypt_block (Aes)</a><br />
30
- <a href="classes/Aes.html#M000011">decrypt_buffer (Aes)</a><br />
31
- <a href="classes/Aes.html#M000013">decrypt_stream (Aes)</a><br />
32
- <a href="classes/Aes.html#M000008">encrypt_block (Aes)</a><br />
33
- <a href="classes/Aes.html#M000010">encrypt_buffer (Aes)</a><br />
34
- <a href="classes/Aes.html#M000012">encrypt_stream (Aes)</a><br />
35
- <a href="classes/Aes.html#M000007">init (Aes)</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 />
36
40
  </div>
37
41
  </div>
38
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'
@@ -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)"
@@ -7,9 +7,11 @@
7
7
  =end
8
8
 
9
9
  require 'ruby-aes/aes_cons'
10
+ require 'ruby-aes/aes_shared'
10
11
 
11
12
  class AesAlg
12
13
  include AesCons
14
+ include AesShared
13
15
 
14
16
  def subBytes
15
17
  i = 0
@@ -181,64 +183,6 @@ class AesAlg
181
183
  end
182
184
  end
183
185
 
184
- def encrypt_blocks(buffer)
185
- raise "Bad block length" unless (buffer.length % 16).zero?
186
- ct = ""
187
- block = ""
188
- buffer.each_byte do |char|
189
- block << char
190
- if block.length == 16
191
- ct << encrypt_block(block)
192
- block = ""
193
- end
194
- end
195
- end
196
-
197
- def decrypt_blocks(buffer)
198
- raise "Bad block length" unless (buffer.length % 16).zero?
199
- pt = ""
200
- block = ""
201
- buffer.each_byte do |char|
202
- block << char
203
- if block.length == 16
204
- pt << decrypt_block(block)
205
- block = ""
206
- end
207
- end
208
- end
209
-
210
- def encrypt_buffer(buffer)
211
- ct = ""
212
- block = ""
213
- buffer.each_byte do |char|
214
- block << char
215
- if block.length == 16
216
- ct << encrypt_block(block)
217
- block = ""
218
- end
219
- end
220
- m = 16 - block.length % 16
221
- ct << (m == 16 ? 0 : encrypt_block(block << m.chr * m))
222
- end
223
-
224
- def decrypt_buffer(buffer)
225
- pt = ""
226
- block = ""
227
- buffer.each_byte do |char|
228
- block << char
229
- if block.length == 16
230
- pt << decrypt_block(block)
231
- block = ""
232
- end
233
- end
234
- if block.length == 0
235
- c = pt[-1]
236
- c.chr * c == pt[-c..-1] ? pt[0..-(c+1)] : (raise "Bad Block Padding")
237
- else
238
- pt
239
- end
240
- end
241
-
242
186
  def init(key_length, mode, key, iv = nil)
243
187
  @iv = "\000" * 16
244
188
  @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
@@ -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-normal
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,17 +49,14 @@ 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
61
  - test/test_ruby-aes.rb
58
62
  - test/test_helper.rb