gost_kuznyechik 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9acd134bce781a159a6702613170b746ef341d98
4
+ data.tar.gz: 25166fd7f79c1a9fa783750830e4fbc55df37610
5
+ SHA512:
6
+ metadata.gz: 87bfbf4b5d60966a38ee6f1c09705b7485aa6f1929067cc224ef221919a92fe5fc7b890bbb9a94bc9e015f35a4fe823430e5065504479bd03c08d9246bec5bfd
7
+ data.tar.gz: f00685df931f39e65b18b8977ccbdaf0e9a098a0a9a5a3235e555f5d8ac7d92a50bbda79e5e484941fa7dc8741f7125b07f9a74f18afe4f383d2aaae8d7635bd
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ before_install: gem install bundler -v 2.0.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in gost_kuznyechik.gemspec
4
+ gemspec
5
+
6
+ gem 'minitest-reporters', '1.2.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,31 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gost_kuznyechik (0.1.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ansi (1.5.0)
10
+ builder (3.2.3)
11
+ minitest (5.11.3)
12
+ minitest-reporters (1.2.0)
13
+ ansi
14
+ builder
15
+ minitest (>= 5.0)
16
+ ruby-progressbar
17
+ rake (10.5.0)
18
+ ruby-progressbar (1.10.0)
19
+
20
+ PLATFORMS
21
+ x64-mingw32
22
+
23
+ DEPENDENCIES
24
+ bundler (~> 2.0)
25
+ gost_kuznyechik!
26
+ minitest (~> 5.0)
27
+ minitest-reporters (= 1.2.0)
28
+ rake (~> 10.0)
29
+
30
+ BUNDLED WITH
31
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 vblazhnov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,167 @@
1
+ # GostKuznyechik
2
+
3
+ GOST R 34.12/13-2015 (Kuznyechik) block cipher algorithms for ECB, CBC, CTR, OFB, CFB, OMAC, CTR-ACPKM, OMAC-ACPKM modes and key export/import algorithms.
4
+
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'gost_kuznyechik'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install gost_kuznyechik
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require 'gost_kuznyechik'
26
+ include GostKuznyechik
27
+
28
+ BlockSize = Kuznyechik::BlockLengthInBytes
29
+
30
+ # GOST R 34.13-2015 Kuznyechik test data
31
+ SelfTestGostKMasterKeyData = [
32
+ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
33
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
34
+ 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
35
+ 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
36
+ ].pack('C*').freeze
37
+
38
+ SelfTestGostKPlainText = [
39
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
40
+ 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
41
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
42
+ 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a,
43
+ 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
44
+ 0x99, 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00,
45
+ 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
46
+ 0xaa, 0xbb, 0xcc, 0xee, 0xff, 0x0a, 0x00, 0x11
47
+ ].pack('C*').freeze
48
+
49
+ key = SelfTestGostKMasterKeyData
50
+ plain_text = SelfTestGostKPlainText
51
+
52
+ # ECB mode
53
+ SelfTestGostKEcbEncText = [
54
+ 0x7f, 0x67, 0x9d, 0x90, 0xbe, 0xbc, 0x24, 0x30,
55
+ 0x5a, 0x46, 0x8d, 0x42, 0xb9, 0xd4, 0xed, 0xcd,
56
+ 0xb4, 0x29, 0x91, 0x2c, 0x6e, 0x00, 0x32, 0xf9,
57
+ 0x28, 0x54, 0x52, 0xd7, 0x67, 0x18, 0xd0, 0x8b,
58
+ 0xf0, 0xca, 0x33, 0x54, 0x9d, 0x24, 0x7c, 0xee,
59
+ 0xf3, 0xf5, 0xa5, 0x31, 0x3b, 0xd4, 0xb1, 0x57,
60
+ 0xd0, 0xb0, 0x9c, 0xcd, 0xe8, 0x30, 0xb9, 0xeb,
61
+ 0x3a, 0x02, 0xc4, 0xc5, 0xaa, 0x8a, 0xda, 0x98
62
+ ].pack('C*').freeze
63
+ encrypted_test = SelfTestGostKEcbEncText
64
+
65
+ encrypted_text = KuznyechikEcb.new(key).encrypt(plain_text)
66
+ puts "ECB encrypted_text == encrypted_test: #{encrypted_text == encrypted_test}"
67
+
68
+ decrypted_text = KuznyechikEcb.new(key).decrypt(encrypted_test)
69
+ puts "ECB decrypted_text == plain_text: #{decrypted_text == plain_text}"
70
+
71
+ # OMAC mode
72
+ SelfTestGostKMacValue = [
73
+ 0x33, 0x6f, 0x4d, 0x29, 0x60, 0x59, 0xfb, 0xe3
74
+ ].pack('C*').freeze
75
+ mac_test = SelfTestGostKMacValue
76
+
77
+ mac = KuznyechikOmac.new(key, mac_test.length).update(plain_text).final
78
+ puts "OMAC mac == mac_test: #{mac == mac_test}"
79
+
80
+ # CTR mode
81
+ SelfTestGostKCtrSV = [
82
+ 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xce, 0xf0,
83
+ 0xa1, 0xb2, 0xc3, 0xd4, 0xe5, 0xf0, 0x01, 0x12
84
+ ].pack('C*').freeze
85
+ iv = SelfTestGostKCtrSV
86
+
87
+ SelfTestGostKCtrEncText = [
88
+ 0xf1, 0x95, 0xd8, 0xbe, 0xc1, 0x0e, 0xd1, 0xdb,
89
+ 0xd5, 0x7b, 0x5f, 0xa2, 0x40, 0xbd, 0xa1, 0xb8,
90
+ 0x85, 0xee, 0xe7, 0x33, 0xf6, 0xa1, 0x3e, 0x5d,
91
+ 0xf3, 0x3c, 0xe4, 0xb3, 0x3c, 0x45, 0xde, 0xe4,
92
+ 0xa5, 0xea, 0xe8, 0x8b, 0xe6, 0x35, 0x6e, 0xd3,
93
+ 0xd5, 0xe8, 0x77, 0xf1, 0x35, 0x64, 0xa3, 0xa5,
94
+ 0xcb, 0x91, 0xfa, 0xb1, 0xf2, 0x0c, 0xba, 0xb6,
95
+ 0xd1, 0xc6, 0xd1, 0x58, 0x20, 0xbd, 0xba, 0x73
96
+ ].pack('C*').freeze
97
+ encrypted_test = SelfTestGostKCtrEncText
98
+
99
+ encrypted_text = KuznyechikCtr.new(key, iv, BlockSize).encrypt(plain_text)
100
+ puts "CTR encrypted_text == encrypted_test: #{encrypted_text == encrypted_test}"
101
+
102
+ # CTR multi-part usage
103
+ text_len = plain_text.length
104
+ ctx = KuznyechikCtr.new(key, iv, BlockSize)
105
+ decrypted_text = ctx.decrypt(encrypted_test[0...text_len/3]) +
106
+ ctx.decrypt(encrypted_test[text_len/3..-1])
107
+ puts "CTR decrypted_text == plain_text: #{decrypted_text == plain_text}"
108
+
109
+ # Key export/import (TC 26 KExp15/KImp15)
110
+ TC26_K = [
111
+ 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
112
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
113
+ 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
114
+ 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
115
+ ].pack('C*').freeze
116
+ TC26_Kmac = [
117
+ 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
118
+ 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
119
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
120
+ 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F
121
+ ].pack('C*').freeze
122
+ TC26_Kenc = [
123
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
124
+ 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
125
+ 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
126
+ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
127
+ ].pack('C*').freeze
128
+ TC26_IV_K = [
129
+ 0x09, 0x09, 0x47, 0x2D, 0xD9, 0xF2, 0x6B, 0xE8
130
+ ].pack('C*').freeze
131
+ TC26_Kexp_K = [
132
+ 0xE3, 0x61, 0x84, 0xE8, 0x4E, 0x8D, 0x73, 0x6F,
133
+ 0xF3, 0x6C, 0xC2, 0xE5, 0xAE, 0x06, 0x5D, 0xC6,
134
+ 0x56, 0xB2, 0x3C, 0x20, 0xF5, 0x49, 0xB0, 0x2F,
135
+ 0xDF, 0xF8, 0x8E, 0x1F, 0x3F, 0x30, 0xD8, 0xC2,
136
+ 0x9A, 0x53, 0xF3, 0xCA, 0x55, 0x4D, 0xBA, 0xD8,
137
+ 0x0D, 0xE1, 0x52, 0xB9, 0xA4, 0x62, 0x5B, 0x32
138
+ ].pack('C*').freeze
139
+
140
+ key = TC26_K
141
+ key_mac = TC26_Kmac
142
+ key_enc = TC26_Kenc
143
+ iv = TC26_IV_K
144
+ key_exp_test = TC26_Kexp_K
145
+
146
+ key_exp = KuznyechikKeyExpImp::export(key, key_mac, key_enc, iv)
147
+ puts "Key Export key_exp == key_exp_test: #{key_exp == key_exp_test}"
148
+
149
+ imp_key = KuznyechikKeyExpImp::import(key_exp_test, key_mac, key_enc, iv)
150
+ puts "Key Import imp_key == key: #{imp_key == key}"
151
+ ```
152
+
153
+ For other cipher modes see test samples in /test/gost_kuznyechik_test.rb please.
154
+
155
+ ## Development
156
+
157
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
158
+
159
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
160
+
161
+ ## Contributing
162
+
163
+ Bug reports and pull requests are welcome on GitHub at https://github.com/vblazhnovgit/gost_kuznyechik.
164
+
165
+ ## License
166
+
167
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "gost_kuznyechik"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "gost_kuznyechik/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gost_kuznyechik"
8
+ spec.version = GostKuznyechik::VERSION
9
+ spec.authors = ["vblazhnovgit"]
10
+ spec.email = ["vblazhnov@yandex.ru"]
11
+
12
+ spec.summary = %q{Kuznyechik block ciphers}
13
+ spec.description = %q{GOST R 34.12/13-2015 (Kuznyechik) block cipher algorithms for ECB, CBC, CTR, OFB, CFB, OMAC, CTR-ACPKM and OMAC-ACPKM modes.}
14
+ spec.homepage = "https://github.com/vblazhnovgit/gost_kuznyechik.git"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org/"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 2.0"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "minitest", "~> 5.0"
38
+ end
@@ -0,0 +1,15 @@
1
+ require "gost_kuznyechik/version"
2
+
3
+ module GostKuznyechik
4
+ require "gost_kuznyechik/kuznyechik"
5
+ require "gost_kuznyechik/kuznyechik_tables"
6
+ require "gost_kuznyechik/kuznyechik_ecb"
7
+ require "gost_kuznyechik/kuznyechik_omac"
8
+ require "gost_kuznyechik/kuznyechik_ctr"
9
+ require "gost_kuznyechik/kuznyechik_ofb"
10
+ require "gost_kuznyechik/kuznyechik_cfb"
11
+ require "gost_kuznyechik/kuznyechik_cbc"
12
+ require "gost_kuznyechik/kuznyechik_ctr_acpkm"
13
+ require "gost_kuznyechik/kuznyechik_omac_acpkm"
14
+ require "gost_kuznyechik/kuznyechik_key_exp_imp"
15
+ end
@@ -0,0 +1,249 @@
1
+ module GostKuznyechik
2
+ # Base abstract class
3
+ class Kuznyechik
4
+ # class constants
5
+ BlockLengthInBytes = 16
6
+ NumberOfRounds = 10
7
+ NumberOfRoundsInKeySchedule = 8
8
+ KeyLengthInBytes = 32
9
+ MaxIvLength = BlockLengthInBytes*16
10
+
11
+ # 's' stands for native-endian byte order but 'n' stands for network (big-endian) byte order
12
+ BigEndian = [1].pack('s') == [1].pack('n')
13
+
14
+ protected
15
+
16
+ def self.printBytes(bytes, line_size = 16)
17
+ bytes.unpack('H*')[0].scan(/.{1,#{line_size}}/).each{|s| puts(s)}
18
+ end
19
+
20
+ def self.zeroBytes(n)
21
+ ("\x00"*n).force_encoding('BINARY')
22
+ end
23
+
24
+ def self.zeroBlock
25
+ ("\x00"*BlockLengthInBytes).force_encoding('BINARY')
26
+ end
27
+
28
+ # n - 64-bit number
29
+ def self.funcS(n)
30
+ uint64ToUint8(n).unpack('C*').map{|b| PiTable[b]}.pack('C*').unpack('Q*')[0]
31
+ end
32
+
33
+ def self.funcInvS(n)
34
+ uint64ToUint8(n).unpack('C*').map{|b| InvPiTable[b]}.pack('C*').unpack('Q*')[0]
35
+ end
36
+
37
+ def self.keyToNumbers(key)
38
+ inkey = key.scan(/.{8}/m).map{|k| k.unpack('Q*')[0]}
39
+ end
40
+
41
+ def self.expandEncryptKeys(inkey)
42
+ roundKeys = inkey.dup
43
+ constantIndex = 0
44
+ (2...NumberOfRounds).step(2) do |iNextKey|
45
+ roundKeys += roundKeys[-4..-1]
46
+ (0...NumberOfRoundsInKeySchedule).each do |iFeistel|
47
+ funcF(constantIndex, roundKeys, iNextKey * 2)
48
+ constantIndex += 1
49
+ end
50
+ end
51
+ roundKeys
52
+ end
53
+
54
+ def self.expandDecryptKeys(inkey)
55
+ roundKeys = expandEncryptKeys(inkey)
56
+ cache = [0, 0]
57
+ (1..8).each do |i|
58
+ cache[0] = roundKeys[2 * i]
59
+ cache[1] = roundKeys[2 * i + 1]
60
+ cache[0] = funcS(cache[0])
61
+ cache[1] = funcS(cache[1])
62
+ pair = funcInvLS(cache)
63
+ roundKeys[2 * i] = pair[0]
64
+ roundKeys[2 * i + 1] = pair[1]
65
+ end
66
+ roundKeys
67
+ end
68
+
69
+ def self.funcF(constantIndex, roundKeys, index)
70
+ temp1 = []
71
+ temp1 << (roundKeys[index] ^ RoundConstLeft[constantIndex]);
72
+ temp1 << (roundKeys[index+1] ^ RoundConstRight[constantIndex]);
73
+ temp2 = funcLS(temp1)
74
+ roundKeys[index+2] ^= temp2[0];
75
+ roundKeys[index+3] ^= temp2[1];
76
+ swapBlocks(roundKeys, index);
77
+ end
78
+
79
+ def self.funcLS(input)
80
+ # To little-endian
81
+ na = []
82
+ ns = uint64ToUint8BE(input[0]).reverse
83
+ na += ns.unpack('C*')
84
+ ns = uint64ToUint8BE(input[1]).reverse
85
+ na += ns.unpack('C*')
86
+ left = 0
87
+ right = 0
88
+ na.each_with_index do |v, i|
89
+ left ^= PrecLSTableLeft[i][v]
90
+ right ^= PrecLSTableRight[i][v]
91
+ end
92
+ [left, right]
93
+ end
94
+
95
+ def self.funcInvLS(input)
96
+ # To little-endian
97
+ na = []
98
+ ns = uint64ToUint8BE(input[0]).reverse
99
+ na += ns.unpack('C*')
100
+ ns = uint64ToUint8BE(input[1]).reverse
101
+ na += ns.unpack('C*')
102
+ left = 0
103
+ right = 0
104
+ na.each_with_index do |v, i|
105
+ left ^= PrecInvLSTableLeft[i][v]
106
+ right ^= PrecInvLSTableRight[i][v]
107
+ end
108
+ [left, right]
109
+ end
110
+
111
+ # roundKeys - array of 64-bit numbers
112
+ # index - points to left[0] number
113
+ # index+2 - points to right[0] number
114
+ # swaps left[0..1] block with right[0..1] block
115
+ def self.swapBlocks(roundKeys, index)
116
+ roundKeys[index] ^= roundKeys[index+2]
117
+ roundKeys[index+1] ^= roundKeys[index+3]
118
+
119
+ roundKeys[index+2] ^= roundKeys[index]
120
+ roundKeys[index+3] ^= roundKeys[index+1]
121
+
122
+ roundKeys[index] ^= roundKeys[index+2]
123
+ roundKeys[index+1] ^= roundKeys[index+3]
124
+ end
125
+
126
+ # Unload 64-bit number to 8-byte string
127
+ # (big-endian, adding leading zeroes)
128
+ def self.uint64ToUint8BE(n)
129
+ str = n.to_s(16) # big-endian
130
+ len = str.length
131
+ # add leading zeroes
132
+ str.insert(0, '0'*(16 - len)) if len < 16
133
+ # To byte string
134
+ bytes = [str].pack('H*')
135
+ end
136
+
137
+ # Unload 64-bit number to 8-byte string
138
+ # (native-endian, adding leading zeroes)
139
+ def self.uint64ToUint8(n)
140
+ bytes = uint64ToUint8BE(n)
141
+ bytes.reverse! unless BigEndian
142
+ bytes
143
+ end
144
+
145
+ # Unpacks 8-byte string to 64-bit number
146
+ # (native-endian)
147
+ def self.uint8ToUint64(bytes)
148
+ bytes.unpack('Q*')[0]
149
+ end
150
+
151
+ # block - 16-byte String
152
+ def self.encryptBlock(block, keys)
153
+ pair = block.scan(/.{8}/m)
154
+ data = []
155
+ # Format 'Q*' unpacks byte string using native byte order
156
+ data << pair[0].unpack('Q*')[0]
157
+ data << pair[1].unpack('Q*')[0]
158
+
159
+ cache = [0, 0]
160
+ (0...(NumberOfRounds - 1)).each do |round|
161
+ cache[0] = data[0] ^ keys[2 * round]
162
+ cache[1] = data[1] ^ keys[2 * round + 1]
163
+ data = funcLS(cache)
164
+ end
165
+
166
+ data[0] ^= keys[2 * (NumberOfRounds - 1)]
167
+ data[1] ^= keys[2 * (NumberOfRounds - 1) + 1];
168
+
169
+ output = uint64ToUint8(data[0]) + uint64ToUint8(data[1])
170
+ output
171
+ end
172
+
173
+ def self.decryptBlock(block, keys)
174
+ pair = block.scan(/.{8}/m)
175
+ data = []
176
+ # Format 'Q*' unpacks byte string using native byte order
177
+ data << pair[0].unpack('Q*')[0]
178
+ data << pair[1].unpack('Q*')[0]
179
+ round = NumberOfRounds - 1
180
+ # round == 9
181
+ data[0] ^= keys[2 * round]
182
+ data[1] ^= keys[2 * round + 1]
183
+ round -= 1
184
+ # round == 8
185
+ data[0] = funcS(data[0])
186
+ data[1] = funcS(data[1])
187
+ cache = funcInvLS(data)
188
+ data = funcInvLS(cache)
189
+ cache[0] = data[0] ^ keys[2 * round]
190
+ cache[1] = data[1] ^ keys[2 * round + 1]
191
+ round -= 1
192
+ # round = 7
193
+ (NumberOfRounds - 3).downto(1) do |i|
194
+ data = funcInvLS(cache)
195
+ cache[0] = data[0] ^ keys[2 * round]
196
+ cache[1] = data[1] ^ keys[2 * round + 1]
197
+ round -= 1
198
+ end
199
+ # round == 0
200
+ cache[0] = funcInvS(cache[0])
201
+ cache[1] = funcInvS(cache[1])
202
+ data[0] = cache[0] ^ keys[2 * round]
203
+ data[1] = cache[1] ^ keys[2 * round + 1]
204
+
205
+ output = uint64ToUint8(data[0]) + uint64ToUint8(data[1])
206
+ output
207
+ end
208
+
209
+ # Increment CTR counter
210
+ def self.incrementModulo(counter, size)
211
+ lastIndex = size - 1
212
+ (0...size).each do |i|
213
+ if counter[lastIndex - i].ord > 0xfe then
214
+ counter[lastIndex - i] = (counter[lastIndex - i].ord - 0xff).chr
215
+ else
216
+ counter[lastIndex - i] = (counter[lastIndex - i].ord + 1).chr
217
+ break
218
+ end
219
+ end
220
+ counter
221
+ end
222
+
223
+ # block - byte string
224
+ def self.shiftLeftOne(block)
225
+ (0...(BlockLengthInBytes-1)).each do |i|
226
+ ri1 = block[i+1].ord
227
+ ri = block[i].ord << 1
228
+ ri &= 0xfe
229
+ ri |= (ri1 >> 7) & 0x1
230
+ block[i] = ri.chr
231
+ end
232
+ ri = block[BlockLengthInBytes-1].ord << 1
233
+ block[BlockLengthInBytes-1] = (ri & 0xfe).chr
234
+ block
235
+ end
236
+
237
+ def self.padd(incomplete_block)
238
+ padding_len = BlockLengthInBytes - (incomplete_block.length % BlockLengthInBytes)
239
+ padded_block = incomplete_block.dup
240
+ padded_block += 0x80.chr
241
+ padding_len -= 1
242
+ if padding_len > 0 then
243
+ padded_block += 0.chr * padding_len
244
+ end
245
+ padded_block
246
+ end
247
+
248
+ end
249
+ end