ruby-aes-cext 1.0 → 1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +7 -1
- data/Rakefile +3 -3
- data/doc/rdoc/classes/Aes.html +112 -106
- data/doc/rdoc/classes/AesShared.html +265 -0
- data/doc/rdoc/created.rid +1 -1
- data/doc/rdoc/files/CHANGELOG.html +12 -2
- data/doc/rdoc/files/COPYING.html +2 -2
- data/doc/rdoc/files/README.html +3 -3
- data/doc/rdoc/files/lib/ruby-aes/aes_shared_rb.html +101 -0
- data/doc/rdoc/files/lib/ruby-aes_rb.html +1 -1
- data/doc/rdoc/fr_class_index.html +1 -0
- data/doc/rdoc/fr_file_index.html +1 -0
- data/doc/rdoc/fr_method_index.html +17 -13
- data/examples/encrypt_block.rb +1 -0
- data/examples/encrypt_buffer.rb +1 -0
- data/examples/encrypt_stream.rb +1 -0
- data/ext/ruby-aes/aes_cons.h +752 -0
- data/lib/ruby-aes.rb +12 -6
- data/lib/ruby-aes/aes_shared.rb +77 -0
- data/test/test_helper.rb +4 -2
- data/test/test_ruby-aes.rb +25 -12
- metadata +14 -10
- data/ext/ruby-aes/aes_gencons.rb +0 -118
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
|
-
|
135
|
-
|
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
|
-
|
155
|
-
|
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)"
|
@@ -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
|
-
|
7
|
-
|
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
|
data/test/test_ruby-aes.rb
CHANGED
@@ -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,
|
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(
|
26
|
+
assert_equal(pt, npt, "Error in encryption/decryption (#{kl}-#{mode})")
|
26
27
|
end
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
30
|
-
def
|
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
|
-
|
46
|
-
|
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
|
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
|
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(@
|
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(
|
101
|
+
Aes.check_key(nil, 256)
|
89
102
|
end # bad key string
|
90
103
|
assert_raise(RuntimeError) do
|
91
|
-
Aes.check_key(@
|
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(@
|
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.
|
2
|
+
rubygems_version: 0.9.4
|
3
3
|
specification_version: 1
|
4
4
|
name: ruby-aes-cext
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: "1.
|
7
|
-
date:
|
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
|
@@ -36,6 +36,13 @@ files:
|
|
36
36
|
- Rakefile
|
37
37
|
- doc/rdoc
|
38
38
|
- doc/rdoc/files
|
39
|
+
- doc/rdoc/files/lib
|
40
|
+
- doc/rdoc/files/lib/ruby-aes_rb.html
|
41
|
+
- doc/rdoc/files/lib/ruby-aes
|
42
|
+
- doc/rdoc/files/lib/ruby-aes/aes_shared_rb.html
|
43
|
+
- doc/rdoc/files/CHANGELOG.html
|
44
|
+
- doc/rdoc/files/README.html
|
45
|
+
- doc/rdoc/files/COPYING.html
|
39
46
|
- doc/rdoc/index.html
|
40
47
|
- doc/rdoc/rdoc-style.css
|
41
48
|
- doc/rdoc/fr_method_index.html
|
@@ -43,17 +50,14 @@ files:
|
|
43
50
|
- doc/rdoc/fr_file_index.html
|
44
51
|
- doc/rdoc/created.rid
|
45
52
|
- doc/rdoc/classes
|
46
|
-
- doc/rdoc/files/lib
|
47
|
-
- doc/rdoc/files/CHANGELOG.html
|
48
|
-
- doc/rdoc/files/README.html
|
49
|
-
- doc/rdoc/files/COPYING.html
|
50
|
-
- doc/rdoc/files/lib/ruby-aes_rb.html
|
51
53
|
- doc/rdoc/classes/Aes.html
|
54
|
+
- doc/rdoc/classes/AesShared.html
|
52
55
|
- examples/encrypt_block.rb
|
53
56
|
- examples/example_helper.rb
|
54
57
|
- examples/encrypt_stream.rb
|
55
58
|
- examples/encrypt_buffer.rb
|
56
59
|
- lib/ruby-aes
|
60
|
+
- lib/ruby-aes/aes_shared.rb
|
57
61
|
- lib/ruby-aes.rb
|
58
62
|
- test/test_ruby-aes.rb
|
59
63
|
- test/test_helper.rb
|
@@ -63,9 +67,9 @@ files:
|
|
63
67
|
- test/KAT_MCT/table.192
|
64
68
|
- test/KAT_MCT/table.256
|
65
69
|
- test/KAT_MCT/aes_kat_mct.rb
|
66
|
-
- ext/ruby-aes/aes_gencons.rb
|
67
|
-
- ext/ruby-aes/extconf.rb
|
68
70
|
- ext/ruby-aes/aes_alg.c
|
71
|
+
- ext/ruby-aes/extconf.rb
|
72
|
+
- ext/ruby-aes/aes_cons.h
|
69
73
|
test_files:
|
70
74
|
- test/test_ruby-aes.rb
|
71
75
|
- test/test_helper.rb
|
data/ext/ruby-aes/aes_gencons.rb
DELETED
@@ -1,118 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
=begin
|
4
|
-
This file is a part of ruby-aes <http://rubyforge.org/projects/ruby-aes>
|
5
|
-
Written by Alex Boussinet <alex.boussinet@gmail.com>
|
6
|
-
|
7
|
-
This script generates the constant arrays needed by ruby-aes
|
8
|
-
=end
|
9
|
-
|
10
|
-
module AesCons
|
11
|
-
|
12
|
-
ALOG = []
|
13
|
-
LOG = []
|
14
|
-
j = 1
|
15
|
-
256.times do |i|
|
16
|
-
ALOG[i] = ALOG[i+255] = j
|
17
|
-
LOG[j] = i
|
18
|
-
j = (j ^ (j << 1) ^ (j & 0x80 != 0 ? 0x01b : 0)) & 0xff
|
19
|
-
end
|
20
|
-
LOG[1] = 0
|
21
|
-
|
22
|
-
def self.mul(a, b)
|
23
|
-
(a.zero? || b.zero?) ? 0 : ALOG[LOG[a] + LOG[b]]
|
24
|
-
end
|
25
|
-
|
26
|
-
RCON = []
|
27
|
-
j = 1
|
28
|
-
10.times do |i|
|
29
|
-
RCON[i] = j << 24 & 0xff000000
|
30
|
-
j = mul(2, j)
|
31
|
-
end
|
32
|
-
|
33
|
-
S = []
|
34
|
-
Si = []
|
35
|
-
256.times do |i|
|
36
|
-
x = (i != 0) ? ALOG[255 - LOG[i]] : 0
|
37
|
-
x ^= (x << 1) ^ (x << 2) ^ (x << 3) ^ (x << 4)
|
38
|
-
x = 0x63 ^ (x ^ (x >> 8))
|
39
|
-
S[i] = x & 0xff
|
40
|
-
Si[x & 0xff] = i
|
41
|
-
end
|
42
|
-
|
43
|
-
def self.mul4(a, b)
|
44
|
-
return 0 if (a.zero?)
|
45
|
-
a = LOG[a & 0xFF]
|
46
|
-
a0 = (b[0] != 0) ? ALOG[(a + LOG[b[0] & 0xff]) % 255] & 0xff : 0
|
47
|
-
a1 = (b[1] != 0) ? ALOG[(a + LOG[b[1] & 0xff]) % 255] & 0xff : 0
|
48
|
-
a2 = (b[2] != 0) ? ALOG[(a + LOG[b[2] & 0xff]) % 255] & 0xff : 0
|
49
|
-
a3 = (b[3] != 0) ? ALOG[(a + LOG[b[3] & 0xff]) % 255] & 0xff : 0
|
50
|
-
return a0 << 24 | a1 << 16 | a2 << 8 | a3
|
51
|
-
end
|
52
|
-
|
53
|
-
G = [
|
54
|
-
[2, 1, 1, 3],[3, 2, 1, 1],[1, 3, 2, 1],[1, 1, 3, 2],
|
55
|
-
[0, 0, 0, 1],[0, 0, 1, 0],[0, 1, 0, 0],[1, 0, 0, 0]
|
56
|
-
]
|
57
|
-
Gi = [
|
58
|
-
[14, 9, 13, 11], [11, 14, 9, 13], [13, 11, 14, 9],[9, 13, 11, 14],
|
59
|
-
[0, 0, 0, 1],[0, 0, 1, 0],[0, 1, 0, 0],[1, 0, 0, 0]
|
60
|
-
]
|
61
|
-
Te0, Te1, Te2, Te3 = [], [], [], []
|
62
|
-
S0, S1, S2, S3 = [], [], [], []
|
63
|
-
Td0, Td1, Td2, Td3 = [], [], [], []
|
64
|
-
Si0, Si1, Si2, Si3 = [], [], [], []
|
65
|
-
256.times do |t|
|
66
|
-
s = S[t]
|
67
|
-
Te0[t] = mul4(s, G[0])
|
68
|
-
Te1[t] = mul4(s, G[1])
|
69
|
-
Te2[t] = mul4(s, G[2])
|
70
|
-
Te3[t] = mul4(s, G[3])
|
71
|
-
S0[t] = mul4(s, G[4])
|
72
|
-
S1[t] = mul4(s, G[5])
|
73
|
-
S2[t] = mul4(s, G[6])
|
74
|
-
S3[t] = mul4(s, G[7])
|
75
|
-
s = Si[t]
|
76
|
-
Td0[t] = mul4(s, Gi[0])
|
77
|
-
Td1[t] = mul4(s, Gi[1])
|
78
|
-
Td2[t] = mul4(s, Gi[2])
|
79
|
-
Td3[t] = mul4(s, Gi[3])
|
80
|
-
Si0[t] = mul4(s, Gi[4])
|
81
|
-
Si1[t] = mul4(s, Gi[5])
|
82
|
-
Si2[t] = mul4(s, Gi[6])
|
83
|
-
Si3[t] = mul4(s, Gi[7])
|
84
|
-
end
|
85
|
-
|
86
|
-
File.open("aes_cons.h" , "w+") do |f|
|
87
|
-
f.write <<-STOP
|
88
|
-
/*
|
89
|
-
* This file is a part of ruby-aes <http://rubyforge.org/projects/ruby-aes>
|
90
|
-
* Written by Alex Boussinet <alex.boussinet@gmail.com>
|
91
|
-
*
|
92
|
-
* aes_cons.h - AES Constant Arrays for ruby-aes
|
93
|
-
*/
|
94
|
-
|
95
|
-
#ifndef __AES_CONS__
|
96
|
-
#define __AES_CONS__
|
97
|
-
|
98
|
-
STOP
|
99
|
-
["RCON", "Te0", "Te1", "Te2", "Te3", "S0", "S1", "S2", "S3",
|
100
|
-
"Td0", "Td1", "Td2", "Td3", "Si0", "Si1", "Si2", "Si3"].each do |x|
|
101
|
-
f.write "uint " + x + "[] = {\n"
|
102
|
-
line = " " * 4
|
103
|
-
module_eval(x).each do |y|
|
104
|
-
z = "0x%08x" % y
|
105
|
-
line << ", " if line.length > 4
|
106
|
-
if (line.length + z.length) > 79
|
107
|
-
f.write line.chop + "\n"
|
108
|
-
line = " " * 4
|
109
|
-
end
|
110
|
-
line << z
|
111
|
-
end
|
112
|
-
f.write line unless line.length == 4
|
113
|
-
f.write "\n};\n\n"
|
114
|
-
end
|
115
|
-
f.write "#endif\n"
|
116
|
-
end
|
117
|
-
|
118
|
-
end # AesCons
|