gost_hmac 0.2.0
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 +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +33 -0
- data/LICENSE.txt +21 -0
- data/README.md +201 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gost_hmac.gemspec +39 -0
- data/lib/gost_hmac.rb +8 -0
- data/lib/gost_hmac/hmac.rb +55 -0
- data/lib/gost_hmac/kdf_tree_256.rb +18 -0
- data/lib/gost_hmac/pkcs5.rb +44 -0
- data/lib/gost_hmac/version.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b0e180cd39c6b76ee7ee1ab7e16ea29886bd2990
|
4
|
+
data.tar.gz: 28ff4d4b57725fa3ab1418a37b1a67fa0c930c78
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0703674daf019681d2eb5e83bffb5ee2bec211379a4f700a0b2effd5f977c5fc28fefe994885303ac0732c1dcb00a90cbbb3755bcf5dd7b168e238a76ed14948
|
7
|
+
data.tar.gz: e6b10e028c30d9cb69e5b3e1e9f2a75594c411521b439227c770d0ab119c1644534c64adb45b3e385640efcfa77bd989ba8cf5522a608ab31b5d9374d1ce55b2
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gost_hmac (0.2.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
11
|
+
crypto_gost3411 (0.1.6)
|
12
|
+
minitest (5.11.3)
|
13
|
+
minitest-reporters (1.2.0)
|
14
|
+
ansi
|
15
|
+
builder
|
16
|
+
minitest (>= 5.0)
|
17
|
+
ruby-progressbar
|
18
|
+
rake (10.5.0)
|
19
|
+
ruby-progressbar (1.10.0)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
x64-mingw32
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler (~> 2.0)
|
26
|
+
crypto_gost3411
|
27
|
+
gost_hmac!
|
28
|
+
minitest (~> 5.0)
|
29
|
+
minitest-reporters (= 1.2.0)
|
30
|
+
rake (~> 10.0)
|
31
|
+
|
32
|
+
BUNDLED WITH
|
33
|
+
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,201 @@
|
|
1
|
+
# GostHmac
|
2
|
+
|
3
|
+
HMAC algorithm for GOST R 34.11-2012 digest
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gost_hmac'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gost_hmac
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Depends on crypto_gost3411 gem.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'gost_hmac'
|
27
|
+
include GostHmac
|
28
|
+
|
29
|
+
# TC 26 HMAC test
|
30
|
+
HmacKey = [
|
31
|
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
32
|
+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
33
|
+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
34
|
+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
|
35
|
+
].pack('C*').freeze
|
36
|
+
|
37
|
+
HmacData = [
|
38
|
+
0x01, 0x26, 0xbd, 0xb8, 0x78, 0x00, 0xaf, 0x21,
|
39
|
+
0x43, 0x41, 0x45, 0x65, 0x63, 0x78, 0x01, 0x00
|
40
|
+
].pack('C*').freeze
|
41
|
+
|
42
|
+
Hmac_32 = [
|
43
|
+
0xa1, 0xaa, 0x5f, 0x7d, 0xe4, 0x02, 0xd7, 0xb3,
|
44
|
+
0xd3, 0x23, 0xf2, 0x99, 0x1c, 0x8d, 0x45, 0x34,
|
45
|
+
0x01, 0x31, 0x37, 0x01, 0x0a, 0x83, 0x75, 0x4f,
|
46
|
+
0xd0, 0xaf, 0x6d, 0x7c, 0xd4, 0x92, 0x2e, 0xd9
|
47
|
+
].pack('C*').freeze
|
48
|
+
|
49
|
+
Hmac_64 = [
|
50
|
+
0xa5, 0x9b, 0xab, 0x22, 0xec, 0xae, 0x19, 0xc6,
|
51
|
+
0x5f, 0xbd, 0xe6, 0xe5, 0xf4, 0xe9, 0xf5, 0xd8,
|
52
|
+
0x54, 0x9d, 0x31, 0xf0, 0x37, 0xf9, 0xdf, 0x9b,
|
53
|
+
0x90, 0x55, 0x00, 0xe1, 0x71, 0x92, 0x3a, 0x77,
|
54
|
+
0x3d, 0x5f, 0x15, 0x30, 0xf2, 0xed, 0x7e, 0x96,
|
55
|
+
0x4c, 0xb2, 0xee, 0xdc, 0x29, 0xe9, 0xad, 0x2f,
|
56
|
+
0x3a, 0xfe, 0x93, 0xb2, 0x81, 0x4f, 0x79, 0xf5,
|
57
|
+
0x00, 0x0f, 0xfc, 0x03, 0x66, 0xc2, 0x51, 0xe6
|
58
|
+
].pack('C*').freeze
|
59
|
+
|
60
|
+
hmac = Hmac.new(HmacKey, 32).update(HmacData).final
|
61
|
+
puts "hmac == Hmac_32: #{hmac == Hmac_32}"
|
62
|
+
|
63
|
+
hmac = Hmac.new(HmacKey, 64).update(HmacData).final
|
64
|
+
puts "hmac == Hmac_64: #{hmac == Hmac_64}"
|
65
|
+
```
|
66
|
+
|
67
|
+
Additionally implemented Pkcs5 static class for password-based key generation:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
require 'gost_hmac'
|
71
|
+
include GostHmac
|
72
|
+
|
73
|
+
# Password-based GOST PKCS#5 key generation with iter_count=2000 takes a few seconds.
|
74
|
+
# To make it more fast use C implementation, please.
|
75
|
+
|
76
|
+
Password7 = "01234567" # (8 octets)
|
77
|
+
Password12 = "Пароль для PFX".force_encoding('BINARY') # Cyrillic UTF-8 password
|
78
|
+
|
79
|
+
Salt7 = [
|
80
|
+
0x8D, 0x47, 0x81, 0x05, 0x26, 0x6D, 0xF5, 0x94,
|
81
|
+
0x09, 0x6E, 0x26, 0xC6, 0x0D, 0x2B, 0x93, 0x89,
|
82
|
+
0xFC, 0x41, 0xCB, 0x22, 0x5D, 0xE6, 0x68, 0x7D,
|
83
|
+
0x6F, 0xE4, 0xF8, 0x72, 0xFF, 0x60, 0x08, 0xCA
|
84
|
+
].pack('C*').freeze
|
85
|
+
Salt12 = [
|
86
|
+
0xA9, 0xCF, 0x20, 0x90, 0x04, 0x8F, 0xAB, 0xCD,
|
87
|
+
0xF2, 0x12, 0x78, 0xAB, 0xCF, 0x57, 0x54, 0x4E,
|
88
|
+
0x7D, 0xC5, 0xE2, 0x61, 0x4F, 0x77, 0x9B, 0x07,
|
89
|
+
0x25, 0xD7, 0x14, 0x15, 0xD8, 0x6E, 0x7F, 0x7E
|
90
|
+
].pack('C*').freeze
|
91
|
+
|
92
|
+
Ic7 = 2000
|
93
|
+
|
94
|
+
KeyLen7 = 32
|
95
|
+
KeyLen12 = 96
|
96
|
+
|
97
|
+
Et7 = [
|
98
|
+
0xe5, 0xc5, 0x8a, 0x6e, 0xf8, 0x81, 0xcd, 0x27,
|
99
|
+
0x0e, 0x63, 0x69, 0x43, 0xc4, 0xf3, 0x31, 0x99,
|
100
|
+
0x9f, 0x33, 0x46, 0x40, 0xf0, 0x55, 0x24, 0xb7,
|
101
|
+
0x40, 0x30, 0xbf, 0x50, 0xeb, 0x4f, 0xec, 0x6d
|
102
|
+
].pack('C*').freeze
|
103
|
+
Et12 = [
|
104
|
+
0xca, 0xdb, 0xfb, 0xf3, 0xbc, 0xea, 0xa9, 0xb7,
|
105
|
+
0x9f, 0x65, 0x15, 0x08, 0xfa, 0xc5, 0xab, 0xbe,
|
106
|
+
0xb4, 0xa1, 0x3d, 0x0b, 0xd0, 0xe1, 0x87, 0x6b,
|
107
|
+
0xd3, 0xc3, 0xef, 0xb2, 0x11, 0x21, 0x28, 0xa5
|
108
|
+
].pack('C*').freeze
|
109
|
+
|
110
|
+
key7 = Pkcs5::generateKey(Password7, Salt7, Ic7, KeyLen7)
|
111
|
+
puts "key7 == Et7: #{key7 == Et7}"
|
112
|
+
|
113
|
+
# TC 26 PKCS#12 HMAC key generation example
|
114
|
+
# First generate 96 bytes
|
115
|
+
key96 = Pkcs5::generateKey(Password12, Salt12, Ic7, KeyLen12)
|
116
|
+
# Then get last 32 bytes
|
117
|
+
key12 = key96[-32..-1]
|
118
|
+
puts "key12 == Et12: #{key12 == Et12}"
|
119
|
+
```
|
120
|
+
|
121
|
+
Additionally implemented KdfTree256 static class for key tree generation:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
require 'gost_hmac'
|
125
|
+
include GostHmac
|
126
|
+
|
127
|
+
Key_in = [
|
128
|
+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
129
|
+
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
|
130
|
+
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
|
131
|
+
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
|
132
|
+
].pack('C*').freeze
|
133
|
+
|
134
|
+
Seed = [
|
135
|
+
0xaf, 0x21, 0x43, 0x41, 0x45, 0x65, 0x63, 0x78
|
136
|
+
].pack('C*').freeze
|
137
|
+
|
138
|
+
Label = [
|
139
|
+
0x26, 0xBD, 0xB8, 0x78
|
140
|
+
].pack('C*').freeze
|
141
|
+
|
142
|
+
# R=1, L=32
|
143
|
+
Kdf_32 = [
|
144
|
+
0xa1, 0xaa, 0x5f, 0x7d, 0xe4, 0x02, 0xd7, 0xb3,
|
145
|
+
0xd3, 0x23, 0xf2, 0x99, 0x1c, 0x8d, 0x45, 0x34,
|
146
|
+
0x01, 0x31, 0x37, 0x01, 0x0a, 0x83, 0x75, 0x4f,
|
147
|
+
0xd0, 0xaf, 0x6d, 0x7c, 0xd4, 0x92, 0x2e, 0xd9
|
148
|
+
].pack('C*').freeze
|
149
|
+
|
150
|
+
# R = 1, L = 64
|
151
|
+
Kdf_64 = [
|
152
|
+
0x22, 0xb6, 0x83, 0x78, 0x45, 0xc6, 0xbe, 0xf6,
|
153
|
+
0x5e, 0xa7, 0x16, 0x72, 0xb2, 0x65, 0x83, 0x10,
|
154
|
+
0x86, 0xd3, 0xc7, 0x6a, 0xeb, 0xe6, 0xda, 0xe9,
|
155
|
+
0x1c, 0xad, 0x51, 0xd8, 0x3f, 0x79, 0xd1, 0x6b,
|
156
|
+
0x07, 0x4c, 0x93, 0x30, 0x59, 0x9d, 0x7f, 0x8d,
|
157
|
+
0x71, 0x2f, 0xca, 0x54, 0x39, 0x2f, 0x4d, 0xdd,
|
158
|
+
0xe9, 0x37, 0x51, 0x20, 0x6b, 0x35, 0x84, 0xc8,
|
159
|
+
0xf4, 0x3f, 0x9e, 0x6d, 0xc5, 0x15, 0x31, 0xf9
|
160
|
+
].pack('C*').freeze
|
161
|
+
|
162
|
+
# R = 2, L = 64
|
163
|
+
Kdf_2_64 = [
|
164
|
+
0xb7, 0x4e, 0xea, 0x99, 0x7c, 0x9d, 0xa9, 0x16,
|
165
|
+
0x0c, 0xe1, 0xa3, 0x3d, 0xdd, 0xb2, 0xd7, 0x52,
|
166
|
+
0x89, 0xfe, 0xe7, 0xd4, 0x79, 0x67, 0x06, 0x87,
|
167
|
+
0x85, 0x1d, 0x9c, 0xf9, 0xca, 0x9f, 0xed, 0x32,
|
168
|
+
0xdd, 0x5b, 0x85, 0x2e, 0x3f, 0x82, 0x6d, 0xb5,
|
169
|
+
0x0e, 0x7c, 0xbe, 0xb0, 0x48, 0xd4, 0x9e, 0x19,
|
170
|
+
0xdc, 0xa7, 0x2d, 0x4f, 0x8b, 0x99, 0x49, 0x11,
|
171
|
+
0x29, 0xc7, 0x5c, 0xd5, 0x1a, 0x08, 0x62, 0x91
|
172
|
+
].pack('C*').freeze
|
173
|
+
|
174
|
+
puts 'Testing GOST R 34.11-2012 KDF TREE'
|
175
|
+
|
176
|
+
puts 'R = 1, L = 32'
|
177
|
+
kdf = KdfTree256.generateKey(Key_in, Label, Seed, 1, 32)
|
178
|
+
puts "kdf == Kdf_32: #{kdf == Kdf_32}"
|
179
|
+
|
180
|
+
puts 'R = 1, L = 64'
|
181
|
+
kdf = KdfTree256.generateKey(Key_in, Label, Seed, 1, 64)
|
182
|
+
puts "kdf == Kdf_64: #{kdf == Kdf_64}"
|
183
|
+
|
184
|
+
puts 'R = 2, L = 64'
|
185
|
+
kdf = KdfTree256.generateKey(Key_in, Label, Seed, 2, 64)
|
186
|
+
puts "kdf == Kdf_2_64: #{kdf == Kdf_2_64}"
|
187
|
+
```
|
188
|
+
|
189
|
+
## Development
|
190
|
+
|
191
|
+
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.
|
192
|
+
|
193
|
+
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).
|
194
|
+
|
195
|
+
## Contributing
|
196
|
+
|
197
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vblazhnovgit/gost_hmac.
|
198
|
+
|
199
|
+
## License
|
200
|
+
|
201
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gost_hmac"
|
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
data/gost_hmac.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "gost_hmac/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "gost_hmac"
|
8
|
+
spec.version = GostHmac::VERSION
|
9
|
+
spec.authors = ["vblazhnovgit"]
|
10
|
+
spec.email = ["vblazhnov@yandex.ru"]
|
11
|
+
|
12
|
+
spec.summary = %q{GOST R 34.11-2012 HMAC}
|
13
|
+
spec.description = %q{GOST R 34.11-2012 HMAC using crypto_gost3411 gem}
|
14
|
+
spec.homepage = "https://github.com/vblazhnovgit/gost_hmac.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
|
+
|
39
|
+
end
|
data/lib/gost_hmac.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
module GostHmac
|
2
|
+
|
3
|
+
require 'crypto_gost3411'
|
4
|
+
include CryptoGost3411
|
5
|
+
|
6
|
+
class Hmac
|
7
|
+
|
8
|
+
def initialize(key, hmac_size)
|
9
|
+
@dgst_size = hmac_size
|
10
|
+
key_len = key.length
|
11
|
+
if key_len <= 64 then
|
12
|
+
standard_key = self.class.zeroBytes(64)
|
13
|
+
standard_key[0...key_len] = key
|
14
|
+
else
|
15
|
+
standard_key = Gost3411.new(64).update(key).final
|
16
|
+
end
|
17
|
+
@ipad = standard_key.dup
|
18
|
+
@opad = standard_key.dup
|
19
|
+
(0...64).each do |i|
|
20
|
+
@ipad[i] = (@ipad[i].ord ^ 0x36).chr
|
21
|
+
@opad[i] = (@opad[i].ord ^ 0x5c).chr
|
22
|
+
end
|
23
|
+
@dgst_ctx = Gost3411.new(@dgst_size)
|
24
|
+
@dgst_ctx.update(@ipad)
|
25
|
+
end
|
26
|
+
|
27
|
+
def update(data)
|
28
|
+
@dgst_ctx.update(data)
|
29
|
+
return self
|
30
|
+
end
|
31
|
+
|
32
|
+
def final
|
33
|
+
hmac_buf = @dgst_ctx.final
|
34
|
+
hmac = Gost3411.new(@dgst_size).update(@opad).update(hmac_buf).final
|
35
|
+
hmac
|
36
|
+
end
|
37
|
+
|
38
|
+
def reset
|
39
|
+
@dgst_ctx = Gost3411.new(@dgst_size)
|
40
|
+
@dgst_ctx.update(@ipad)
|
41
|
+
return self
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
|
46
|
+
def self.printBytes(bytes, line_size = 16)
|
47
|
+
bytes.unpack('H*')[0].scan(/.{1,#{line_size}}/).each{|s| puts(s)}
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.zeroBytes(n)
|
51
|
+
("\x00"*n).force_encoding('BINARY')
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module GostHmac
|
2
|
+
class KdfTree256
|
3
|
+
def self.generateKey(key_in, label, seed, num_R, out_len)
|
4
|
+
n = out_len / 32
|
5
|
+
if out_len % 32 > 0 then
|
6
|
+
n += 1
|
7
|
+
end
|
8
|
+
buf = ''
|
9
|
+
data = 0.chr * num_R + label + 0.chr + seed + (out_len / 32).chr + (out_len % 32).chr
|
10
|
+
(0...n).each do |i|
|
11
|
+
data[num_R-1] = (i+1).chr
|
12
|
+
hmac = Hmac.new(key_in, 32).update(data).final
|
13
|
+
buf += hmac
|
14
|
+
end
|
15
|
+
return buf[0...out_len]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module GostHmac
|
2
|
+
# Password-based GOST PKCS#5 key generation with iter_count=2000 takes a few seconds.
|
3
|
+
# To make it more fast use C implementation, please.
|
4
|
+
class Pkcs5
|
5
|
+
def self.generateKey(pswd_utf8, salt, iter_count, key_len)
|
6
|
+
keyLen = key_len
|
7
|
+
hmacLen = 64
|
8
|
+
hmac_ctx = Hmac.new(pswd_utf8, hmacLen)
|
9
|
+
count = 1
|
10
|
+
resPos = 0
|
11
|
+
if keyLen > hmacLen then
|
12
|
+
keyBufLen = keyLen
|
13
|
+
else
|
14
|
+
keyBufLen = hmacLen
|
15
|
+
end
|
16
|
+
keyBuf = 0.chr * keyBufLen
|
17
|
+
while keyLen > 0 do
|
18
|
+
if keyLen > hmacLen then
|
19
|
+
outLen = hmacLen
|
20
|
+
else
|
21
|
+
outLen = keyLen
|
22
|
+
end
|
23
|
+
itmp = [(count>>24)&0xff, (count>>16)&0xff, (count>>8)&0xff, count&0xff].pack('C*')
|
24
|
+
buf = hmac_ctx.reset.update(salt).update(itmp).final
|
25
|
+
keyBuf[resPos...resPos+outLen] = buf[0...outLen]
|
26
|
+
(1...iter_count).each do |i|
|
27
|
+
buf = hmac_ctx.reset.update(buf).final
|
28
|
+
(0...outLen).each do |j|
|
29
|
+
keyBuf[resPos+j] = (keyBuf[resPos+j].ord ^ buf[j].ord).chr
|
30
|
+
end
|
31
|
+
end
|
32
|
+
keyLen -= outLen
|
33
|
+
count += 1
|
34
|
+
resPos += outLen
|
35
|
+
end
|
36
|
+
key = keyBuf[0...key_len].dup
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.printBytes(bytes, line_size = 16)
|
40
|
+
bytes.unpack('H*')[0].scan(/.{1,#{line_size}}/).each{|s| puts(s)}
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gost_hmac
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- vblazhnovgit
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: GOST R 34.11-2012 HMAC using crypto_gost3411 gem
|
56
|
+
email:
|
57
|
+
- vblazhnov@yandex.ru
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- gost_hmac.gemspec
|
72
|
+
- lib/gost_hmac.rb
|
73
|
+
- lib/gost_hmac/hmac.rb
|
74
|
+
- lib/gost_hmac/kdf_tree_256.rb
|
75
|
+
- lib/gost_hmac/pkcs5.rb
|
76
|
+
- lib/gost_hmac/version.rb
|
77
|
+
homepage: https://github.com/vblazhnovgit/gost_hmac.git
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata:
|
81
|
+
allowed_push_host: https://rubygems.org/
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.6.14.3
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: GOST R 34.11-2012 HMAC
|
102
|
+
test_files: []
|