ruby-aes-cext 1.0-i686-linux

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,6 @@
1
+ = 0.1
2
+
3
+ === 2007-07-30
4
+ * Initial release: RAA project import
5
+ * Added a C extension for speed along the 6 other versions (see README)
6
+ * Minor changes in the API
data/COPYING ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Alex Boussinet
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,77 @@
1
+ README for ruby-aes
2
+ ===================
3
+
4
+ Ruby AES <http://rubyforge.org/projects/ruby-aes> is an implementation of the Rijndael algorithm.
5
+
6
+ Written by Alex Boussinet <mailto:alex.boussinet@gmail.com>
7
+
8
+ This release is mainly an import from the Ruby Application Archive (RAA).
9
+ I've added all the versions I was working on (algorithm variations) and a new
10
+ C extension for improved performance.
11
+ 6 variations are available:
12
+
13
+ * "Normal":
14
+ Pure Ruby implementation of the Rijndael algorithm specifications.
15
+ Useful for understanding the algorithm.
16
+
17
+ * "Optimized":
18
+ Pure Ruby implementation based on the "Normal" code but optimized for speed.
19
+ The SubBytes and ShiftRows methods have been combined.
20
+
21
+ * "Table Optimized 1":
22
+ Pure Ruby implementation based on the C code from the Rijndael website.
23
+ The arrays of constants are bigger because all the operations are
24
+ already computed so it's mainly based on table look ups.
25
+
26
+ * "Table Optimized 2":
27
+ Pure Ruby implementation based on the "Table Optimized 1" code.
28
+ The arrays of constants are bigger because all the operations are
29
+ already computed and table look ups are also combined.
30
+
31
+ * "Table Unroll Optimized 1":
32
+ Pure Ruby implementation based on the "Table Optimized 1" code.
33
+ The change here is that the loops are unrolled.
34
+
35
+ * "Table Unroll Optimized 2":
36
+ Pure Ruby implementation based on the "Table Optimized 2" code.
37
+ The change here is that the loops are unrolled.
38
+
39
+ * "EXT Table Unroll Optimized 2":
40
+ C extension based on the "Table Unroll Optimized 2" code.
41
+ This extension is provided for major speed improvement.
42
+
43
+ All those variations share the same API:
44
+ Default key_length: 128
45
+ Default mode: 'ECB'
46
+ Default IV: 16 null chars ("00" * 16 in hex format)
47
+ Default key: 16 null chars ("00" * 16 in hex format)
48
+ Default input text: "PLAINTEXT"
49
+
50
+ Aes.check_key(key_string, key_length)
51
+ Aes.check_iv(iv_string)
52
+ Aes.check_kl(key_length)
53
+ Aes.check_mode(mode)
54
+ Aes.init(key_length, mode, key, iv)
55
+ Aes.encrypt_block(key_length, mode, key, iv, block) # no padding
56
+ Aes.decrypt_block(key_length, mode, key, iv, block) # no padding
57
+ Aes.encrypt_buffer(key_length, mode, key, iv, block) # padding
58
+ Aes.decrypt_buffer(key_length, mode, key, iv, block) # padding
59
+ Aes.encrypt_stream(key_length, mode, key, iv, sin, sout)
60
+ Aes.decrypt_stream(key_length, mode, key, iv, sin, sout)
61
+ Aes.bs() # block size for read operations (stream)
62
+ Aes.bs=(bs)
63
+
64
+ Valid modes are:
65
+ * ECB (Electronic Code Book)
66
+ * CBC (Cipher Block Chaining)
67
+ * OFB (Output Feedback)
68
+ * CFB (Cipher Feedback)
69
+
70
+ Valid key length:
71
+ * 128 bits
72
+ * 192 bits
73
+ * 256 bits
74
+
75
+ For a really good encryption, 256 bits CBC is recommanded.
76
+
77
+ For more information on AES-Rijndael, see: <http://csrc.nist.gov/encryption/aes/rijndael/>
data/Rakefile ADDED
@@ -0,0 +1,154 @@
1
+ require 'rake'
2
+ require 'rake/clean'
3
+ require 'rake/testtask'
4
+ require 'rake/gempackagetask'
5
+ require 'rake/rdoctask'
6
+ require 'fileutils'
7
+
8
+ include FileUtils
9
+
10
+ @name = 'ruby-aes'
11
+ @version = '1.0'
12
+ @native = false
13
+
14
+ @lib = "lib/#{@name}"
15
+ @ext = "ext/#{@name}"
16
+ @ext_o = 'aes_alg.o'
17
+ @ext_so = "aes_alg.#{Config::CONFIG['DLEXT']}"
18
+
19
+ RDOC_OPTS = ['--quiet',
20
+ '--title', 'ruby-aes reference',
21
+ '--opname', 'index.html',
22
+ '--exclude', 'ext',
23
+ '--line-numbers',
24
+ '--main', 'README',
25
+ '--inline-source']
26
+
27
+ CLEAN.include [
28
+ '**/.*.sw?', '*.gem', '.config', '**/.DS_Store',
29
+ "#{@ext}/#{@ext_so}", "#{@ext}/#{@ext_o}",
30
+ "#{@ext}/Makefile", "#{@ext}/aes_cons.h", "#{@ext}/mkmf.log",
31
+ "#{@lib}/aes_alg.rb", "#{@lib}/aes_cons.rb", "#{@lib}/aes_gencons.rb"
32
+ ]
33
+
34
+ SPEC = Gem::Specification.new do |s|
35
+ s.name = @name
36
+ s.version = @version
37
+ s.platform = Gem::Platform::RUBY
38
+ s.has_rdoc = true
39
+ s.rdoc_options += RDOC_OPTS
40
+ s.extra_rdoc_files = ['README', 'CHANGELOG', 'COPYING']
41
+ s.summary = 'ruby-aes is an implementation of the Rijndael algorithm (AES)'
42
+ s.description = s.summary
43
+ s.author = 'Alex Boussinet'
44
+ s.email = 'alex.boussinet@gmail.com'
45
+ s.homepage = "http://#{@name}.rubyforge.org"
46
+ s.rubyforge_project = @name
47
+ s.test_files = FileList['test/test_*.rb']
48
+ s.require_paths = ['lib']
49
+ # s.bindir = 'bin'
50
+ s.files = %w(CHANGELOG COPYING README Rakefile) +
51
+ Dir.glob('{doc,examples,lib,test}/**/*')
52
+ end
53
+
54
+ def task_gem
55
+ desc 'Build the gem'
56
+ Rake::GemPackageTask.new(SPEC) do |p|
57
+ p.need_tar = true
58
+ p.gem_spec = SPEC
59
+ end
60
+ end
61
+
62
+ Dir.glob('extras/*').each do |project|
63
+ desc "Specify the project to use"
64
+ task File.basename(project).to_sym do |t|
65
+ @type = t.name
66
+
67
+ @gem_name = "#{@name}-#{@type}"
68
+ SPEC.name = @gem_name
69
+ SPEC.files += [ "#{@lib}/aes_alg.rb", "#{@lib}/aes_cons.rb" ]
70
+ task_gem
71
+ end
72
+ end
73
+
74
+ desc "Specify the project to use"
75
+ task :cext do |t|
76
+ @type = t.name
77
+
78
+ @gem_name = "#{@name}-#{@type}"
79
+ SPEC.name = @gem_name
80
+ SPEC.require_paths += ['ext']
81
+ if @native
82
+ SPEC.files += ["#{@ext}/#{@ext_so}"]
83
+ SPEC.platform = Gem::Platform::CURRENT
84
+ else
85
+ SPEC.files += Dir.glob("#{@ext}/*")
86
+ SPEC.extensions = FileList["#{@ext}/extconf.rb"].to_a
87
+ end
88
+ task_gem
89
+ end
90
+ desc "Use the native version of cext"
91
+ task :native do
92
+ @native = true
93
+ Rake::Task[:cext].invoke
94
+ end
95
+
96
+ task :prepare do
97
+ if @type == 'cext'
98
+ Dir.chdir(@ext) do
99
+ ruby 'aes_gencons.rb'
100
+ if @native
101
+ ruby 'extconf.rb'
102
+ sh(PLATFORM =~ /win32/ ? 'nmake' : 'make')
103
+ end
104
+ end
105
+ else
106
+ cp "extras/#{@type}/aes_alg.rb", "#{@lib}/"
107
+ cp "extras/#{@type}/aes_gencons.rb", "#{@lib}/"
108
+ Dir.chdir(@lib) do
109
+ ruby 'aes_gencons.rb'
110
+ rm_f 'aes_gencons.rb'
111
+ end
112
+ end
113
+ end
114
+
115
+ task :package => [:clean, :prepare, :rerdoc]
116
+
117
+ task :default do
118
+ STDERR.puts <<-EOM
119
+ You must call rake with one of this task as first param:
120
+ normal
121
+ optimized
122
+ table1
123
+ table2
124
+ unroll1
125
+ unroll2
126
+ cext
127
+ native (imply cext)
128
+ EOM
129
+ end
130
+
131
+ desc 'Run all the tests'
132
+ Rake::TestTask.new do |t|
133
+ t.libs << "test"
134
+ t.test_files = FileList['test/test_*.rb']
135
+ t.verbose = true
136
+ end
137
+
138
+ desc 'Build the documentation'
139
+ Rake::RDocTask.new do |rdoc|
140
+ rdoc.rdoc_dir = 'doc/rdoc'
141
+ rdoc.options += RDOC_OPTS
142
+ rdoc.main = 'README'
143
+ rdoc.rdoc_files.add ['README', 'CHANGELOG', 'COPYING', 'lib/**/*.rb']
144
+ end
145
+
146
+ desc 'Install the package'
147
+ task :install do |t|
148
+ sh %{sudo gem install pkg/#{@gem_name}}
149
+ end
150
+
151
+ desc 'Uninstall the package'
152
+ task :uninstall do
153
+ sh %{sudo gem uninstall #{@gem_name}}
154
+ end
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ruby-aes'
4
+ require 'example_helper'
5
+
6
+ class RubyAES_block
7
+
8
+ include RubyAES_helper
9
+
10
+ def initialize
11
+ setup
12
+ pt = "0123467890ABCDEF"
13
+ puts "Using #{@kl}-#{@mode} encryption/decryption"
14
+ puts "Plaintext is: #{pt} (a block should be 16 octets)"
15
+ ct = Aes.encrypt_block(@kl, @mode, @keys[@kl], @iv, pt)
16
+ puts "Ciphertext (unpacked) is: #{ct.unpack("H*").first}"
17
+ npt = Aes.decrypt_block(@kl, @mode, @keys[@kl], @iv, ct)
18
+ puts "Decrypted ciphertext is: #{npt} (should be: #{pt})"
19
+ end
20
+
21
+ end
22
+ RubyAES_block.new
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ruby-aes'
4
+ require 'example_helper'
5
+
6
+ class RubyAES_buffer
7
+
8
+ include RubyAES_helper
9
+
10
+ def initialize
11
+ setup
12
+ puts "Using #{@kl}-#{@mode} encryption/decryption"
13
+ pt = "The quick brown fox jumps over the lazy dog"
14
+ puts "Plaintext is: '#{pt}'"
15
+ puts "(a buffer will be padded so that its length will be a multiple of 16)"
16
+ ct = Aes.encrypt_buffer(@kl, @mode, @keys[@kl], @iv, pt)
17
+ puts "Ciphertext (unpacked) is: #{ct.unpack("H*").first}"
18
+ npt = Aes.decrypt_buffer(@kl, @mode, @keys[@kl], @iv, ct)
19
+ puts "Decrypted ciphertext is: '#{npt}'"
20
+ puts "(should be: '#{pt}')"
21
+ end
22
+
23
+ end
24
+ RubyAES_buffer.new
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ruby-aes'
4
+ require 'example_helper'
5
+ require 'fileutils'
6
+
7
+ class RubyAES_stream
8
+
9
+ include RubyAES_helper
10
+
11
+ def initialize
12
+ setup
13
+ puts "Using #{@kl}-#{@mode} encryption/decryption"
14
+ file = "_ruby-aes_encrypt_stream_"
15
+
16
+ sin = File.open(file, "w+b")
17
+ sin.puts "The quick brown fox jumps over the lazy dog"
18
+ sin.rewind
19
+ sout = File.open("#{file}.aes", "w+b")
20
+ Aes.encrypt_stream(@kl, @mode, @keys[@kl], @iv, sin, sout)
21
+ sin.close
22
+ sout.close
23
+
24
+ sin = File.open("#{file}.aes", "rb")
25
+ sout = File.open("#{file}.plain", "w+b")
26
+ Aes.decrypt_stream(@kl, @mode, @keys[@kl], @iv, sin, sout)
27
+ sin.close
28
+ sout.close
29
+
30
+ if IO.read(file) == IO.read("#{file}.plain")
31
+ puts "The decrypted file is exactly the same as the original one"
32
+ else
33
+ puts "The decrypted file differs from the orginal one"
34
+ end
35
+ FileUtils.rm_f [ file, "#{file}.aes", "#{file}.plain" ]
36
+ end
37
+
38
+ end
39
+ RubyAES_stream.new
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ module RubyAES_helper
4
+
5
+ KEY_LENGTH = [128,192,256].freeze
6
+ MODES = ['ECB','CBC','OFB','CFB'].freeze
7
+
8
+ def random_fill(n, buffer)
9
+ n.times do
10
+ buffer << rand(256).chr
11
+ end
12
+ end
13
+
14
+ def setup
15
+ @keys = {}
16
+ KEY_LENGTH.each do |kl|
17
+ @keys[kl] = ""
18
+ random_fill(kl/8, @keys[kl])
19
+ end
20
+
21
+ @iv = ""; random_fill(16, @iv)
22
+ @pt = ""; random_fill(64, @pt)
23
+ @kl = KEY_LENGTH[(rand * KEY_LENGTH.length).to_i]
24
+ @mode = MODES[(rand * MODES.length).to_i]
25
+ end
26
+
27
+ end
Binary file
data/lib/ruby-aes.rb ADDED
@@ -0,0 +1,162 @@
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
+ ==Valid modes are:
6
+ * ECB (Electronic Code Book)
7
+ * CBC (Cipher Block Chaining)
8
+ * OFB (Output Feedback)
9
+ * CFB (Cipher Feedback)
10
+
11
+ ==Valid key length:
12
+ * 128 bits
13
+ * 192 bits
14
+ * 256 bits
15
+
16
+ ==API calls:
17
+ Default key_length: 128
18
+ Default mode: 'ECB'
19
+ Default IV: 16 null chars ("00" * 16 in hex format)
20
+ Default key: 16 null chars ("00" * 16 in hex format)
21
+ Default input text: "PLAINTEXT"
22
+
23
+ Aes.check_key(key_string, key_length)
24
+ Aes.check_iv(iv_string)
25
+ Aes.check_kl(key_length)
26
+ Aes.check_mode(mode)
27
+ Aes.init(key_length, mode, key, iv)
28
+ Aes.encrypt_block(key_length, mode, key, iv, block) # no padding
29
+ Aes.decrypt_block(key_length, mode, key, iv, block) # no padding
30
+ Aes.encrypt_buffer(key_length, mode, key, iv, block) # padding
31
+ Aes.decrypt_buffer(key_length, mode, key, iv, block) # padding
32
+ Aes.encrypt_stream(key_length, mode, key, iv, sin, sout)
33
+ Aes.decrypt_stream(key_length, mode, key, iv, sin, sout)
34
+ Aes.bs() # block size for read operations (stream)
35
+ Aes.bs=(bs)
36
+ =end
37
+
38
+ module Aes
39
+
40
+ require 'ruby-aes/aes_alg'
41
+
42
+ @@aes = nil
43
+ @@bs = 4096
44
+
45
+ def Aes.bs(); return @@bs end
46
+ def Aes.bs=(bs); @@bs = bs.to_i; @@bs==0 ? 4096 : @@bs = @@bs - @@bs%16 end
47
+
48
+ def Aes.check_key(key_string, kl = 128)
49
+ kl = Aes.check_kl(kl)
50
+ k = key_string.length
51
+ raise "Bad key string or bad key length" if (k != kl/8) && (k != kl/4)
52
+ hex = (key_string =~ /[a-f0-9A-F]{#{k}}/) == 0 && (k == kl/4)
53
+ bin = ! hex
54
+ if ! (([32, 48, 64].include?(k) && hex) ||
55
+ ([16, 24, 32].include?(k) && bin))
56
+ raise "Bad key string"
57
+ end
58
+ hex ? [key_string].pack("H*") : key_string
59
+ end
60
+
61
+ def Aes.check_iv(iv_string)
62
+ k = iv_string.length
63
+ hex = (iv_string =~ /[a-f0-9A-F]{#{k}}/) == 0
64
+ bin = ! hex
65
+ if k == 32 && hex
66
+ return [iv_string].pack("H*")
67
+ elsif k == 16 && bin
68
+ return iv_string
69
+ else
70
+ raise "Bad IV string"
71
+ end
72
+ end
73
+
74
+ def Aes.check_mode (mode)
75
+ case mode
76
+ when 'ECB', 'CBC', 'OFB', 'CFB'
77
+ else raise "Bad cipher mode"
78
+ end
79
+ mode
80
+ end
81
+
82
+ def Aes.check_kl(key_length)
83
+ case key_length
84
+ when 128, 192, 256
85
+ else raise "Bad key length"
86
+ end
87
+ key_length
88
+ end
89
+
90
+ def Aes.init(keyl, mode, key, iv)
91
+ unless @@aes
92
+ @@aes = AesAlg.new(Aes.check_kl(keyl), Aes.check_mode(mode),
93
+ Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil)
94
+ else
95
+ @@aes.init(Aes.check_kl(keyl), Aes.check_mode(mode),
96
+ Aes.check_key(key, keyl), iv ? Aes.check_iv(iv) : nil)
97
+ end
98
+ end
99
+
100
+ def Aes.encrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT")
101
+ raise "Bad Block size" if block.length < 16 || block.length > 16
102
+ Aes.init(keyl, mode, key, iv)
103
+ @@aes.encrypt_block(block)
104
+ end
105
+
106
+ def Aes.decrypt_block(keyl, mode, key, iv, block = "DEFAULT PLAINTXT")
107
+ Aes.init(keyl, mode, key, iv)
108
+ @@aes.decrypt_block(block)
109
+ end
110
+
111
+ def Aes.encrypt_buffer(keyl, mode, key, iv, buffer = "PLAINTEXT")
112
+ Aes.init(keyl, mode, key, iv)
113
+ @@aes.encrypt_buffer(buffer)
114
+ end
115
+
116
+ def Aes.decrypt_buffer(keyl, mode, key, iv, buffer = "DEFAULT PLAINTXT")
117
+ raise "Bad Block size" if buffer.length < 16
118
+ Aes.init(keyl, mode, key, iv)
119
+ @@aes.decrypt_buffer(buffer)
120
+ end
121
+
122
+ def Aes.encrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)
123
+ Aes.init(keyl, mode, key, iv)
124
+ case sout
125
+ when String, Array, IO
126
+ else
127
+ raise "Bad output stream (String, Array, IO)"
128
+ end
129
+ case sin
130
+ when String
131
+ sout << @@aes.encrypt_buffer(sin)
132
+ when IO
133
+ while buf = sin.read(@@bs)
134
+ sout << ((buf.length % 16).zero? ? @@aes.encrypt_blocks(buf) :
135
+ @@aes.encrypt_buffer(buf))
136
+ end
137
+ else
138
+ raise "Bad input stream (String, IO)"
139
+ end
140
+ end
141
+
142
+ def Aes.decrypt_stream(keyl, mode, key, iv, sin = STDIN, sout = STDOUT)
143
+ Aes.init(keyl, mode, key, iv)
144
+ case sout
145
+ when String, Array, IO
146
+ else
147
+ raise "Bad output stream (String, Array, IO)"
148
+ end
149
+ case sin
150
+ when String
151
+ sout << @@aes.decrypt_buffer(sin)
152
+ when IO
153
+ while buf = sin.read(@@bs)
154
+ sout << (sin.eof? ? @@aes.decrypt_buffer(buf) :
155
+ @@aes.decrypt_blocks(buf))
156
+ end
157
+ else
158
+ raise "Bad input stream (String, IO)"
159
+ end
160
+ end
161
+
162
+ end # end Aes