xmldsign 0.2.0.beta → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +9 -9
- data/Rakefile +0 -11
- data/lib/xmldsign/digests/gost.rb +16 -14
- data/lib/xmldsign/version.rb +1 -1
- data/spec/assets/signed-act-birth.xml +2 -2
- data/spec/spec_helper.rb +0 -1
- data/spec/xmldsign/sign_spec.rb +8 -0
- data/xmldsign.gemspec +2 -5
- metadata +27 -48
- data/ext/xmldsign/extconf.rb +0 -3
- data/ext/xmldsign/gost89.c +0 -421
- data/ext/xmldsign/gost89.h +0 -96
- data/ext/xmldsign/gosthash.c +0 -255
- data/ext/xmldsign/gosthash.h +0 -48
- data/ext/xmldsign/xmldsign_ext.c +0 -54
- data/ext/xmldsign/xmldsign_ext.h +0 -18
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTczMWIyN2I3YjYwNjc1ZDkyYjFlMGYyOTkwMTAzNTg3OTRlODBmNQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
7
|
-
|
6
|
+
N2NmMTZiNDIzNzdmZWEyNmIwYzI5ZDM4ZmRlOTZkOTg2YTAyYmI3Yg==
|
7
|
+
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MjlkMWU1OTlkZjY1ZWE0ZTg0YWE5ZTEyZjQ3ZmE5NGQ0MjBmOTc0M2IzMjMw
|
10
|
+
NmUzNjczNzBkMDNkNDBjNGNhODA0ODQyYTFmNDhmMDM0NjU3MDQ3ZGRlMzQ5
|
11
|
+
MmJjYTVlMDZkYzcxNzcwZWI5NWJjMjA5ZTM3ZmVlMzdiODJiMmM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjZjOWNlMWM0OTFhMDQ2YjAxNmFkNGRiYzUxMDM5YmQxYmRmNTU3MDgwNzhm
|
14
|
+
OWQ1NWE2OTZkZDQyMWIxY2IyOWFkYjQ0YTViYmEzMzAwMzA3NjJjNGQ1NTUw
|
15
|
+
ZDAxYWE0MDk2YmZjYTQ1YmVjNWU4YmNjODNiYTkyMzQzOTk0YjA=
|
data/Rakefile
CHANGED
@@ -3,15 +3,4 @@ require 'rspec/core/rake_task'
|
|
3
3
|
|
4
4
|
RSpec::Core::RakeTask.new('spec')
|
5
5
|
|
6
|
-
task :build_native_extension do
|
7
|
-
Dir.chdir('ext/xmldsign') do
|
8
|
-
output = `ruby extconf.rb`
|
9
|
-
raise output unless $? == 0
|
10
|
-
output = `make`
|
11
|
-
raise output unless $? == 0
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
task :spec => :build_native_extension
|
16
|
-
|
17
6
|
task :default => :spec
|
@@ -1,33 +1,35 @@
|
|
1
|
-
require "
|
1
|
+
require "anyhash"
|
2
2
|
|
3
3
|
module Xmldsign
|
4
4
|
module Digests
|
5
5
|
class Gost
|
6
|
+
|
6
7
|
attr_reader :data
|
7
8
|
|
9
|
+
def initialize(data)
|
10
|
+
@data = data.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
def base64
|
14
|
+
[[hex].pack('H*')].pack('m0')
|
15
|
+
end
|
16
|
+
|
17
|
+
def hex
|
18
|
+
Anyhash.gost_cryptopro(data)
|
19
|
+
end
|
20
|
+
|
8
21
|
class << self
|
22
|
+
|
9
23
|
def base64(data)
|
10
24
|
new(data).base64
|
11
25
|
end
|
26
|
+
|
12
27
|
def hex(data)
|
13
28
|
new(data).hex
|
14
29
|
end
|
15
|
-
def binary(data)
|
16
|
-
new(data).binary
|
17
|
-
end
|
18
|
-
end
|
19
30
|
|
20
|
-
def initialize(data)
|
21
|
-
@data = data.to_s
|
22
31
|
end
|
23
32
|
|
24
|
-
def base64
|
25
|
-
Base64.strict_encode64(binary(data))
|
26
|
-
end
|
27
|
-
|
28
|
-
def hex
|
29
|
-
binary(data).bytes.inject("") { |hex, b| hex << b.to_s(16) }
|
30
|
-
end
|
31
33
|
end
|
32
34
|
end
|
33
35
|
end
|
data/lib/xmldsign/version.rb
CHANGED
@@ -105,10 +105,10 @@
|
|
105
105
|
<ds:DigestValue>ZtonhLsw3V9Sr14iQUvwMJoTo0RbnXMFca2z29xIPxw=</ds:DigestValue>
|
106
106
|
</ds:Reference>
|
107
107
|
</ds:SignedInfo>
|
108
|
-
<ds:SignatureValue
|
108
|
+
<ds:SignatureValue/>
|
109
109
|
<ds:KeyInfo>
|
110
110
|
<ds:X509Data>
|
111
|
-
<ds:X509Certificate
|
111
|
+
<ds:X509Certificate/>
|
112
112
|
</ds:X509Data>
|
113
113
|
</ds:KeyInfo>
|
114
114
|
</ds:Signature>
|
data/spec/spec_helper.rb
CHANGED
data/spec/xmldsign/sign_spec.rb
CHANGED
@@ -5,6 +5,14 @@ describe Xmldsign do
|
|
5
5
|
LibXML::XML.indent_tree_output = false
|
6
6
|
subject { Xmldsign::Document.string asset('act-birth-for-sign.xml') }
|
7
7
|
|
8
|
+
it "should calculate GOST digest hex (CryptoPro parameters) " do
|
9
|
+
Xmldsign::Digests::Gost.hex('a').should eql 'e74c52dd282183bf37af0079c9f78055715a103f17e3133ceff1aacf2f403011'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should calculate GOST digest base64 (CryptoPro parameters) " do
|
13
|
+
Xmldsign::Digests::Gost.base64('a').should eql '50xS3Sghg783rwB5yfeAVXFaED8X4xM87/Gqzy9AMBE='
|
14
|
+
end
|
15
|
+
|
8
16
|
it "should execute transformations" do
|
9
17
|
subject.signature.sign.to_s.should eql asset('signed-act-birth.xml')
|
10
18
|
end
|
data/xmldsign.gemspec
CHANGED
@@ -12,18 +12,15 @@ Gem::Specification.new do |gem|
|
|
12
12
|
gem.summary = %q{Xmldsign library for ruby with GOST algorithms. Work in progress...}
|
13
13
|
gem.homepage = ""
|
14
14
|
|
15
|
-
gem.extensions = ["ext/xmldsign/extconf.rb"]
|
16
|
-
|
17
15
|
gem.files = `git ls-files`.split($/)
|
18
16
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
18
|
gem.require_paths = ["lib"]
|
21
19
|
|
20
|
+
gem.add_dependency 'anyhash', '~> 0.0.1'
|
22
21
|
gem.add_dependency 'libxml-ruby'
|
23
22
|
gem.add_dependency 'libxslt-ruby'
|
24
|
-
gem.add_development_dependency 'rake
|
25
|
-
gem.add_development_dependency 'rake', '0.8.7' # NB: 0.8.7 required by rake-compiler 0.7.9
|
23
|
+
gem.add_development_dependency 'rake'
|
26
24
|
gem.add_development_dependency 'rspec'
|
27
25
|
gem.add_development_dependency 'pry'
|
28
|
-
gem.add_development_dependency 'pry-debugger'
|
29
26
|
end
|
metadata
CHANGED
@@ -1,37 +1,37 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xmldsign
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.0
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Sozontov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-05-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
prerelease: false
|
15
|
-
name: libxml-ruby
|
16
14
|
version_requirements: !ruby/object:Gem::Requirement
|
17
15
|
requirements:
|
18
|
-
- -
|
16
|
+
- - ~>
|
19
17
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
18
|
+
version: 0.0.1
|
19
|
+
prerelease: false
|
20
|
+
name: anyhash
|
21
21
|
requirement: !ruby/object:Gem::Requirement
|
22
22
|
requirements:
|
23
|
-
- -
|
23
|
+
- - ~>
|
24
24
|
- !ruby/object:Gem::Version
|
25
|
-
version:
|
25
|
+
version: 0.0.1
|
26
26
|
type: :runtime
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
prerelease: false
|
29
|
-
name: libxslt-ruby
|
30
28
|
version_requirements: !ruby/object:Gem::Requirement
|
31
29
|
requirements:
|
32
30
|
- - ! '>='
|
33
31
|
- !ruby/object:Gem::Version
|
34
32
|
version: '0'
|
33
|
+
prerelease: false
|
34
|
+
name: libxml-ruby
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
37
37
|
- - ! '>='
|
@@ -39,41 +39,27 @@ dependencies:
|
|
39
39
|
version: '0'
|
40
40
|
type: :runtime
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
prerelease: false
|
43
|
-
name: rake-compiler
|
44
42
|
version_requirements: !ruby/object:Gem::Requirement
|
45
43
|
requirements:
|
46
|
-
- -
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version: 0.7.7
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - ~>
|
44
|
+
- - ! '>='
|
52
45
|
- !ruby/object:Gem::Version
|
53
|
-
version: 0
|
54
|
-
type: :development
|
55
|
-
- !ruby/object:Gem::Dependency
|
46
|
+
version: '0'
|
56
47
|
prerelease: false
|
57
|
-
name:
|
58
|
-
version_requirements: !ruby/object:Gem::Requirement
|
59
|
-
requirements:
|
60
|
-
- - '='
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: 0.8.7
|
48
|
+
name: libxslt-ruby
|
63
49
|
requirement: !ruby/object:Gem::Requirement
|
64
50
|
requirements:
|
65
|
-
- - '
|
51
|
+
- - ! '>='
|
66
52
|
- !ruby/object:Gem::Version
|
67
|
-
version: 0
|
68
|
-
type: :
|
53
|
+
version: '0'
|
54
|
+
type: :runtime
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
|
-
prerelease: false
|
71
|
-
name: rspec
|
72
56
|
version_requirements: !ruby/object:Gem::Requirement
|
73
57
|
requirements:
|
74
58
|
- - ! '>='
|
75
59
|
- !ruby/object:Gem::Version
|
76
60
|
version: '0'
|
61
|
+
prerelease: false
|
62
|
+
name: rake
|
77
63
|
requirement: !ruby/object:Gem::Requirement
|
78
64
|
requirements:
|
79
65
|
- - ! '>='
|
@@ -81,13 +67,13 @@ dependencies:
|
|
81
67
|
version: '0'
|
82
68
|
type: :development
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
|
-
prerelease: false
|
85
|
-
name: pry
|
86
70
|
version_requirements: !ruby/object:Gem::Requirement
|
87
71
|
requirements:
|
88
72
|
- - ! '>='
|
89
73
|
- !ruby/object:Gem::Version
|
90
74
|
version: '0'
|
75
|
+
prerelease: false
|
76
|
+
name: rspec
|
91
77
|
requirement: !ruby/object:Gem::Requirement
|
92
78
|
requirements:
|
93
79
|
- - ! '>='
|
@@ -95,13 +81,13 @@ dependencies:
|
|
95
81
|
version: '0'
|
96
82
|
type: :development
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
|
-
prerelease: false
|
99
|
-
name: pry-debugger
|
100
84
|
version_requirements: !ruby/object:Gem::Requirement
|
101
85
|
requirements:
|
102
86
|
- - ! '>='
|
103
87
|
- !ruby/object:Gem::Version
|
104
88
|
version: '0'
|
89
|
+
prerelease: false
|
90
|
+
name: pry
|
105
91
|
requirement: !ruby/object:Gem::Requirement
|
106
92
|
requirements:
|
107
93
|
- - ! '>='
|
@@ -112,8 +98,7 @@ description: Xmldsign library for ruby with GOST algorithms
|
|
112
98
|
email:
|
113
99
|
- a.sozontov@gmail.com
|
114
100
|
executables: []
|
115
|
-
extensions:
|
116
|
-
- ext/xmldsign/extconf.rb
|
101
|
+
extensions: []
|
117
102
|
extra_rdoc_files: []
|
118
103
|
files:
|
119
104
|
- .gitignore
|
@@ -128,13 +113,6 @@ files:
|
|
128
113
|
- LICENSE.txt
|
129
114
|
- README.md
|
130
115
|
- Rakefile
|
131
|
-
- ext/xmldsign/extconf.rb
|
132
|
-
- ext/xmldsign/gost89.c
|
133
|
-
- ext/xmldsign/gost89.h
|
134
|
-
- ext/xmldsign/gosthash.c
|
135
|
-
- ext/xmldsign/gosthash.h
|
136
|
-
- ext/xmldsign/xmldsign_ext.c
|
137
|
-
- ext/xmldsign/xmldsign_ext.h
|
138
116
|
- lib/xmldsign.rb
|
139
117
|
- lib/xmldsign/algorithms.rb
|
140
118
|
- lib/xmldsign/digests/gost.rb
|
@@ -191,12 +169,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
191
169
|
version: '0'
|
192
170
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
193
171
|
requirements:
|
194
|
-
- - ! '
|
172
|
+
- - ! '>='
|
195
173
|
- !ruby/object:Gem::Version
|
196
|
-
version:
|
174
|
+
version: '0'
|
197
175
|
requirements: []
|
198
176
|
rubyforge_project:
|
199
|
-
rubygems_version: 2.
|
177
|
+
rubygems_version: 2.2.2
|
200
178
|
signing_key:
|
201
179
|
specification_version: 4
|
202
180
|
summary: Xmldsign library for ruby with GOST algorithms. Work in progress...
|
@@ -233,3 +211,4 @@ test_files:
|
|
233
211
|
- spec/assets/x509.crt
|
234
212
|
- spec/spec_helper.rb
|
235
213
|
- spec/xmldsign/sign_spec.rb
|
214
|
+
has_rdoc:
|
data/ext/xmldsign/extconf.rb
DELETED
data/ext/xmldsign/gost89.c
DELETED
@@ -1,421 +0,0 @@
|
|
1
|
-
/**********************************************************************
|
2
|
-
* gost89.c *
|
3
|
-
* Copyright (c) 2005-2006 Cryptocom LTD *
|
4
|
-
* This file is distributed under the same license as OpenSSL *
|
5
|
-
* *
|
6
|
-
* Implementation of GOST 28147-89 encryption algorithm *
|
7
|
-
* No OpenSSL libraries required to compile and use *
|
8
|
-
* this code *
|
9
|
-
**********************************************************************/
|
10
|
-
#include <string.h>
|
11
|
-
#include "gost89.h"
|
12
|
-
/* Substitution blocks from RFC 4357
|
13
|
-
|
14
|
-
Note: our implementation of gost 28147-89 algorithm
|
15
|
-
uses S-box matrix rotated 90 degrees counterclockwise, relative to
|
16
|
-
examples given in RFC.
|
17
|
-
|
18
|
-
|
19
|
-
*/
|
20
|
-
|
21
|
-
/* Substitution blocks from test examples for GOST R 34.11-94*/
|
22
|
-
gost_subst_block GostR3411_94_TestParamSet = {
|
23
|
-
{0X1,0XF,0XD,0X0,0X5,0X7,0XA,0X4,0X9,0X2,0X3,0XE,0X6,0XB,0X8,0XC},
|
24
|
-
{0XD,0XB,0X4,0X1,0X3,0XF,0X5,0X9,0X0,0XA,0XE,0X7,0X6,0X8,0X2,0XC},
|
25
|
-
{0X4,0XB,0XA,0X0,0X7,0X2,0X1,0XD,0X3,0X6,0X8,0X5,0X9,0XC,0XF,0XE},
|
26
|
-
{0X6,0XC,0X7,0X1,0X5,0XF,0XD,0X8,0X4,0XA,0X9,0XE,0X0,0X3,0XB,0X2},
|
27
|
-
{0X7,0XD,0XA,0X1,0X0,0X8,0X9,0XF,0XE,0X4,0X6,0XC,0XB,0X2,0X5,0X3},
|
28
|
-
{0X5,0X8,0X1,0XD,0XA,0X3,0X4,0X2,0XE,0XF,0XC,0X7,0X6,0X0,0X9,0XB},
|
29
|
-
{0XE,0XB,0X4,0XC,0X6,0XD,0XF,0XA,0X2,0X3,0X8,0X1,0X0,0X7,0X5,0X9},
|
30
|
-
{0X4,0XA,0X9,0X2,0XD,0X8,0X0,0XE,0X6,0XB,0X1,0XC,0X7,0XF,0X5,0X3}
|
31
|
-
};
|
32
|
-
/* Substitution blocks for hash function 1.2.643.2.9.1.6.1 */
|
33
|
-
gost_subst_block GostR3411_94_CryptoProParamSet= {
|
34
|
-
{0x1,0x3,0xA,0x9,0x5,0xB,0x4,0xF,0x8,0x6,0x7,0xE,0xD,0x0,0x2,0xC},
|
35
|
-
{0xD,0xE,0x4,0x1,0x7,0x0,0x5,0xA,0x3,0xC,0x8,0xF,0x6,0x2,0x9,0xB},
|
36
|
-
{0x7,0x6,0x2,0x4,0xD,0x9,0xF,0x0,0xA,0x1,0x5,0xB,0x8,0xE,0xC,0x3},
|
37
|
-
{0x7,0x6,0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
-
};
|
95
|
-
|
96
|
-
/* 1.2.643.2.2.31.4 */
|
97
|
-
gost_subst_block Gost28147_CryptoProParamSetD=
|
98
|
-
{
|
99
|
-
{0x1,0xA,0x6,0x8,0xF,0xB,0x0,0x4,0xC,0x3,0x5,0x9,0x7,0xD,0x2,0xE},
|
100
|
-
{0x3,0x0,0x6,0xF,0x1,0xE,0x9,0x2,0xD,0x8,0xC,0x4,0xB,0xA,0x5,0x7},
|
101
|
-
{0x8,0x0,0xF,0x3,0x2,0x5,0xE,0xB,0x1,0xA,0x4,0x7,0xC,0x9,0xD,0x6},
|
102
|
-
{0x0,0xC,0x8,0x9,0xD,0x2,0xA,0xB,0x7,0x3,0x6,0x5,0x4,0xE,0xF,0x1},
|
103
|
-
{0x1,0x5,0xE,0xC,0xA,0x7,0x0,0xD,0x6,0x2,0xB,0x4,0x9,0x3,0xF,0x8},
|
104
|
-
{0x1,0xC,0xB,0x0,0xF,0xE,0x6,0x5,0xA,0xD,0x4,0x8,0x9,0x3,0x7,0x2},
|
105
|
-
{0xB,0x6,0x3,0x4,0xC,0xF,0xE,0x2,0x7,0xD,0x8,0x0,0x5,0xA,0x9,0x1},
|
106
|
-
{0xF,0xC,0x2,0xA,0x6,0x4,0x5,0x0,0x7,0x9,0xE,0xD,0x1,0xB,0x8,0x3}
|
107
|
-
};
|
108
|
-
|
109
|
-
|
110
|
-
const byte CryptoProKeyMeshingKey[]={
|
111
|
-
0x69, 0x00, 0x72, 0x22, 0x64, 0xC9, 0x04, 0x23,
|
112
|
-
0x8D, 0x3A, 0xDB, 0x96, 0x46, 0xE9, 0x2A, 0xC4,
|
113
|
-
0x18, 0xFE, 0xAC, 0x94, 0x00, 0xED, 0x07, 0x12,
|
114
|
-
0xC0, 0x86, 0xDC, 0xC2, 0xEF, 0x4C, 0xA9, 0x2B
|
115
|
-
};
|
116
|
-
/* Initialization of gost_ctx subst blocks*/
|
117
|
-
static void kboxinit(gost_ctx *c, const gost_subst_block *b)
|
118
|
-
{
|
119
|
-
int i;
|
120
|
-
|
121
|
-
for (i = 0; i < 256; i++)
|
122
|
-
{
|
123
|
-
c->k87[i] = (b->k8[i>>4] <<4 | b->k7 [i &15])<<24;
|
124
|
-
c->k65[i] = (b->k6[i>>4] << 4 | b->k5 [i &15])<<16;
|
125
|
-
c->k43[i] = (b->k4[i>>4] <<4 | b->k3 [i &15])<<8;
|
126
|
-
c->k21[i] = b->k2[i>>4] <<4 | b->k1 [i &15];
|
127
|
-
|
128
|
-
}
|
129
|
-
}
|
130
|
-
|
131
|
-
/* Part of GOST 28147 algorithm moved into separate function */
|
132
|
-
static word32 f(gost_ctx *c,word32 x)
|
133
|
-
{
|
134
|
-
x = c->k87[x>>24 & 255] | c->k65[x>>16 & 255]|
|
135
|
-
c->k43[x>> 8 & 255] | c->k21[x & 255];
|
136
|
-
/* Rotate left 11 bits */
|
137
|
-
return x<<11 | x>>(32-11);
|
138
|
-
}
|
139
|
-
/* Low-level encryption routine - encrypts one 64 bit block*/
|
140
|
-
void gostcrypt(gost_ctx *c, const byte *in, byte *out)
|
141
|
-
{
|
142
|
-
register word32 n1, n2; /* As named in the GOST */
|
143
|
-
n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24);
|
144
|
-
n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24);
|
145
|
-
/* Instead of swapping halves, swap names each round */
|
146
|
-
|
147
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
148
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
149
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
150
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
151
|
-
|
152
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
153
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
154
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
155
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
156
|
-
|
157
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
158
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
159
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
160
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
161
|
-
|
162
|
-
n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]);
|
163
|
-
n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]);
|
164
|
-
n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]);
|
165
|
-
n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]);
|
166
|
-
|
167
|
-
out[0] = (byte)(n2&0xff); out[1] = (byte)((n2>>8)&0xff);
|
168
|
-
out[2] = (byte)((n2>>16)&0xff); out[3]=(byte)(n2>>24);
|
169
|
-
out[4] = (byte)(n1&0xff); out[5] = (byte)((n1>>8)&0xff);
|
170
|
-
out[6] = (byte)((n1>>16)&0xff); out[7] = (byte)(n1>>24);
|
171
|
-
}
|
172
|
-
/* Low-level decryption routine. Decrypts one 64-bit block */
|
173
|
-
void gostdecrypt(gost_ctx *c, const byte *in,byte *out)
|
174
|
-
{
|
175
|
-
register word32 n1, n2; /* As named in the GOST */
|
176
|
-
n1 = in[0]|(in[1]<<8)|(in[2]<<16)|(in[3]<<24);
|
177
|
-
n2 = in[4]|(in[5]<<8)|(in[6]<<16)|(in[7]<<24);
|
178
|
-
|
179
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
180
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
181
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
182
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
183
|
-
|
184
|
-
n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]);
|
185
|
-
n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]);
|
186
|
-
n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]);
|
187
|
-
n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]);
|
188
|
-
|
189
|
-
n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]);
|
190
|
-
n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]);
|
191
|
-
n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]);
|
192
|
-
n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]);
|
193
|
-
|
194
|
-
n2 ^= f(c,n1+c->k[7]); n1 ^= f(c,n2+c->k[6]);
|
195
|
-
n2 ^= f(c,n1+c->k[5]); n1 ^= f(c,n2+c->k[4]);
|
196
|
-
n2 ^= f(c,n1+c->k[3]); n1 ^= f(c,n2+c->k[2]);
|
197
|
-
n2 ^= f(c,n1+c->k[1]); n1 ^= f(c,n2+c->k[0]);
|
198
|
-
|
199
|
-
out[0] = (byte)(n2&0xff); out[1] = (byte)((n2>>8)&0xff);
|
200
|
-
out[2] = (byte)((n2>>16)&0xff); out[3]=(byte)(n2>>24);
|
201
|
-
out[4] = (byte)(n1&0xff); out[5] = (byte)((n1>>8)&0xff);
|
202
|
-
out[6] = (byte)((n1>>16)&0xff); out[7] = (byte)(n1>>24);
|
203
|
-
}
|
204
|
-
|
205
|
-
/* Encrypts several blocks in ECB mode */
|
206
|
-
void gost_enc(gost_ctx *c,const byte *clear,byte *cipher, int blocks)
|
207
|
-
{
|
208
|
-
int i;
|
209
|
-
for(i=0;i<blocks;i++)
|
210
|
-
{
|
211
|
-
gostcrypt(c,clear,cipher);
|
212
|
-
clear+=8;
|
213
|
-
cipher+=8;
|
214
|
-
}
|
215
|
-
}
|
216
|
-
/* Decrypts several blocks in ECB mode */
|
217
|
-
void gost_dec(gost_ctx *c, const byte *cipher,byte *clear, int blocks)
|
218
|
-
{
|
219
|
-
int i;
|
220
|
-
for(i=0;i<blocks;i++)
|
221
|
-
{
|
222
|
-
gostdecrypt(c,cipher,clear);
|
223
|
-
clear+=8;
|
224
|
-
cipher+=8;
|
225
|
-
}
|
226
|
-
}
|
227
|
-
|
228
|
-
/* Encrypts several full blocks in CFB mode using 8byte IV */
|
229
|
-
void gost_enc_cfb(gost_ctx *ctx,const byte *iv,const byte *clear,byte *cipher, int blocks)
|
230
|
-
{
|
231
|
-
byte cur_iv[8];
|
232
|
-
byte gamma[8];
|
233
|
-
int i,j;
|
234
|
-
const byte *in;
|
235
|
-
byte *out;
|
236
|
-
memcpy(cur_iv,iv,8);
|
237
|
-
for(i=0,in=clear,out=cipher;i<blocks;i++,in+=8,out+=8)
|
238
|
-
{
|
239
|
-
gostcrypt(ctx,cur_iv,gamma);
|
240
|
-
for (j=0;j<8;j++)
|
241
|
-
{
|
242
|
-
cur_iv[j]=out[j]=in[j]^gamma[j];
|
243
|
-
}
|
244
|
-
}
|
245
|
-
}
|
246
|
-
/* Decrypts several full blocks in CFB mode using 8byte IV */
|
247
|
-
void gost_dec_cfb(gost_ctx *ctx,const byte *iv,const byte *cipher,byte *clear, int blocks)
|
248
|
-
{
|
249
|
-
byte cur_iv[8];
|
250
|
-
byte gamma[8];
|
251
|
-
int i,j;
|
252
|
-
const byte *in;
|
253
|
-
byte *out;
|
254
|
-
memcpy(cur_iv,iv,8);
|
255
|
-
for(i=0,in=cipher,out=clear;i<blocks;i++,in+=8,out+=8)
|
256
|
-
{
|
257
|
-
gostcrypt(ctx,cur_iv,gamma);
|
258
|
-
for (j=0;j<8;j++)
|
259
|
-
{
|
260
|
-
out[j]=(cur_iv[j]=in[j])^gamma[j];
|
261
|
-
}
|
262
|
-
}
|
263
|
-
}
|
264
|
-
|
265
|
-
/* Encrypts one block using specified key */
|
266
|
-
void gost_enc_with_key(gost_ctx *c,byte *key,byte *inblock,byte *outblock)
|
267
|
-
{
|
268
|
-
gost_key(c,key);
|
269
|
-
gostcrypt(c,inblock,outblock);
|
270
|
-
}
|
271
|
-
|
272
|
-
/* Set 256 bit key into context */
|
273
|
-
void gost_key(gost_ctx *c, const byte *k)
|
274
|
-
{
|
275
|
-
int i,j;
|
276
|
-
for(i=0,j=0;i<8;i++,j+=4)
|
277
|
-
{
|
278
|
-
c->k[i]=k[j]|(k[j+1]<<8)|(k[j+2]<<16)|(k[j+3]<<24);
|
279
|
-
}
|
280
|
-
}
|
281
|
-
|
282
|
-
/* Retrieve 256-bit key from context */
|
283
|
-
void gost_get_key(gost_ctx *c, byte *k)
|
284
|
-
{
|
285
|
-
int i,j;
|
286
|
-
for(i=0,j=0;i<8;i++,j+=4)
|
287
|
-
{
|
288
|
-
k[j]=(byte)(c->k[i]& 0xFF);
|
289
|
-
k[j+1]=(byte)((c->k[i]>>8 )&0xFF);
|
290
|
-
k[j+2]=(byte)((c->k[i]>>16) &0xFF);
|
291
|
-
k[j+3]=(byte)((c->k[i]>>24) &0xFF);
|
292
|
-
}
|
293
|
-
}
|
294
|
-
|
295
|
-
/* Initalize context. Provides default value for subst_block */
|
296
|
-
void gost_init(gost_ctx *c, const gost_subst_block *b)
|
297
|
-
{
|
298
|
-
if(!b)
|
299
|
-
{
|
300
|
-
b=&GostR3411_94_TestParamSet;
|
301
|
-
}
|
302
|
-
kboxinit(c,b);
|
303
|
-
}
|
304
|
-
|
305
|
-
/* Cleans up key from context */
|
306
|
-
void gost_destroy(gost_ctx *c)
|
307
|
-
{
|
308
|
-
int i; for(i=0;i<8;i++) c->k[i]=0;
|
309
|
-
}
|
310
|
-
|
311
|
-
/* Compute GOST 28147 mac block
|
312
|
-
*
|
313
|
-
* Parameters
|
314
|
-
* gost_ctx *c - context initalized with substitution blocks and key
|
315
|
-
* buffer - 8-byte mac state buffer
|
316
|
-
* block 8-byte block to process.
|
317
|
-
* */
|
318
|
-
void mac_block(gost_ctx *c,byte *buffer,const byte *block)
|
319
|
-
{
|
320
|
-
register word32 n1, n2; /* As named in the GOST */
|
321
|
-
int i;
|
322
|
-
for (i=0; i<8; i++)
|
323
|
-
{
|
324
|
-
buffer[i]^=block[i];
|
325
|
-
}
|
326
|
-
n1 = buffer[0]|(buffer[1]<<8)|(buffer[2]<<16)|(buffer[3]<<24);
|
327
|
-
n2 = buffer[4]|(buffer[5]<<8)|(buffer[6]<<16)|(buffer[7]<<24);
|
328
|
-
/* Instead of swapping halves, swap names each round */
|
329
|
-
|
330
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
331
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
332
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
333
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
334
|
-
|
335
|
-
n2 ^= f(c,n1+c->k[0]); n1 ^= f(c,n2+c->k[1]);
|
336
|
-
n2 ^= f(c,n1+c->k[2]); n1 ^= f(c,n2+c->k[3]);
|
337
|
-
n2 ^= f(c,n1+c->k[4]); n1 ^= f(c,n2+c->k[5]);
|
338
|
-
n2 ^= f(c,n1+c->k[6]); n1 ^= f(c,n2+c->k[7]);
|
339
|
-
|
340
|
-
buffer[0] = (byte)(n1&0xff); buffer[1] = (byte)((n1>>8)&0xff);
|
341
|
-
buffer[2] = (byte)((n1>>16)&0xff); buffer[3] = (byte)(n1>>24);
|
342
|
-
buffer[4] = (byte)(n2&0xff); buffer[5] = (byte)((n2>>8)&0xff);
|
343
|
-
buffer[6] = (byte)((n2>>16)&0xff); buffer[7] = (byte)(n2>>24);
|
344
|
-
}
|
345
|
-
|
346
|
-
/* Get mac with specified number of bits from MAC state buffer */
|
347
|
-
void get_mac(byte *buffer,int nbits,byte *out)
|
348
|
-
{
|
349
|
-
int nbytes= nbits >> 3;
|
350
|
-
int rembits = nbits & 7;
|
351
|
-
int mask =rembits?((1<rembits)-1):0;
|
352
|
-
int i;
|
353
|
-
for (i=0;i<nbytes;i++) out[i]=buffer[i];
|
354
|
-
if (rembits) out[i]=buffer[i]&mask;
|
355
|
-
}
|
356
|
-
|
357
|
-
/* Compute mac of specified length (in bits) from data.
|
358
|
-
* Context should be initialized with key and subst blocks */
|
359
|
-
int gost_mac(gost_ctx *ctx,int mac_len,const unsigned char *data,
|
360
|
-
unsigned int data_len,unsigned char *mac)
|
361
|
-
{
|
362
|
-
byte buffer[8]={0,0,0,0,0,0,0,0};
|
363
|
-
byte buf2[8];
|
364
|
-
unsigned int i;
|
365
|
-
for (i=0;i+8<=data_len;i+=8)
|
366
|
-
mac_block(ctx,buffer,data+i);
|
367
|
-
if (i<data_len)
|
368
|
-
{
|
369
|
-
memset(buf2,0,8);
|
370
|
-
memcpy(buf2,data+i,data_len-i);
|
371
|
-
mac_block(ctx,buffer,buf2);
|
372
|
-
i+=8;
|
373
|
-
}
|
374
|
-
if (i==8)
|
375
|
-
{
|
376
|
-
memset(buf2,0,8);
|
377
|
-
mac_block(ctx,buffer,buf2);
|
378
|
-
}
|
379
|
-
get_mac(buffer,mac_len,mac);
|
380
|
-
return 1;
|
381
|
-
}
|
382
|
-
|
383
|
-
/* Compute MAC with non-zero IV. Used in some RFC 4357 algorithms */
|
384
|
-
int gost_mac_iv(gost_ctx *ctx,int mac_len,const unsigned char *iv,const unsigned char *data,
|
385
|
-
unsigned int data_len,unsigned char *mac)
|
386
|
-
{
|
387
|
-
byte buffer[8];
|
388
|
-
byte buf2[8];
|
389
|
-
unsigned int i;
|
390
|
-
memcpy (buffer,iv,8);
|
391
|
-
for (i=0;i+8<=data_len;i+=8)
|
392
|
-
mac_block(ctx,buffer,data+i);
|
393
|
-
if (i<data_len)
|
394
|
-
{
|
395
|
-
memset(buf2,0,8);
|
396
|
-
memcpy(buf2,data+i,data_len-i);
|
397
|
-
mac_block(ctx,buffer,buf2);
|
398
|
-
i+=8;
|
399
|
-
}
|
400
|
-
if (i==8)
|
401
|
-
{
|
402
|
-
memset(buf2,0,8);
|
403
|
-
mac_block(ctx,buffer,buf2);
|
404
|
-
}
|
405
|
-
get_mac(buffer,mac_len,mac);
|
406
|
-
return 1;
|
407
|
-
}
|
408
|
-
|
409
|
-
/* Implements key meshing algorithm by modifing ctx and IV in place */
|
410
|
-
void cryptopro_key_meshing(gost_ctx *ctx, unsigned char *iv)
|
411
|
-
{
|
412
|
-
unsigned char newkey[32],newiv[8];
|
413
|
-
/* Set static keymeshing key */
|
414
|
-
/* "Decrypt" key with keymeshing key */
|
415
|
-
gost_dec(ctx,CryptoProKeyMeshingKey,newkey,4);
|
416
|
-
/* set new key */
|
417
|
-
gost_key(ctx,newkey);
|
418
|
-
/* Encrypt iv with new key */
|
419
|
-
gostcrypt(ctx,iv,newiv);
|
420
|
-
memcpy(iv,newiv,8);
|
421
|
-
}
|
data/ext/xmldsign/gost89.h
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
/**********************************************************************
|
2
|
-
* gost89.h *
|
3
|
-
* Copyright (c) 2005-2006 Cryptocom LTD *
|
4
|
-
* This file is distributed under the same license as OpenSSL *
|
5
|
-
* *
|
6
|
-
* Declarations for GOST 28147-89 encryption algorithm *
|
7
|
-
* No OpenSSL libraries required to compile and use *
|
8
|
-
* this code *
|
9
|
-
**********************************************************************/
|
10
|
-
#ifndef GOST89_H
|
11
|
-
#define GOST89_H
|
12
|
-
|
13
|
-
/* Typedef for unsigned 32-bit integer */
|
14
|
-
#if __LONG_MAX__ > 2147483647L
|
15
|
-
typedef unsigned int u4;
|
16
|
-
#else
|
17
|
-
typedef unsigned long u4;
|
18
|
-
#endif
|
19
|
-
/* Typedef for unsigned 8-bit integer */
|
20
|
-
typedef unsigned char byte;
|
21
|
-
|
22
|
-
/* Internal representation of GOST substitution blocks */
|
23
|
-
typedef struct {
|
24
|
-
byte k8[16];
|
25
|
-
byte k7[16];
|
26
|
-
byte k6[16];
|
27
|
-
byte k5[16];
|
28
|
-
byte k4[16];
|
29
|
-
byte k3[16];
|
30
|
-
byte k2[16];
|
31
|
-
byte k1[16];
|
32
|
-
} gost_subst_block;
|
33
|
-
|
34
|
-
|
35
|
-
/* Cipher context includes key and preprocessed substitution block */
|
36
|
-
typedef struct {
|
37
|
-
u4 k[8];
|
38
|
-
/* Constant s-boxes -- set up in gost_init(). */
|
39
|
-
u4 k87[256],k65[256],k43[256],k21[256];
|
40
|
-
} gost_ctx;
|
41
|
-
/* Note: encrypt and decrypt expect full blocks--padding blocks is
|
42
|
-
caller's responsibility. All bulk encryption is done in
|
43
|
-
ECB mode by these calls. Other modes may be added easily
|
44
|
-
enough. */
|
45
|
-
/* Encrypt several full blocks in ECB mode */
|
46
|
-
void gost_enc(gost_ctx *ctx, const byte *clear,byte *cipher, int blocks);
|
47
|
-
/* Decrypt several full blocks in ECB mode */
|
48
|
-
void gost_dec(gost_ctx *ctx, const byte *cipher,byte *clear, int blocks);
|
49
|
-
/* Encrypts several full blocks in CFB mode using 8byte IV */
|
50
|
-
void gost_enc_cfb(gost_ctx *ctx,const byte *iv,const byte *clear,byte *cipher,int blocks);
|
51
|
-
/* Decrypts several full blocks in CFB mode using 8byte IV */
|
52
|
-
void gost_dec_cfb(gost_ctx *ctx,const byte *iv,const byte *cipher,byte *clear,int blocks);
|
53
|
-
|
54
|
-
/* Encrypt one block */
|
55
|
-
void gostcrypt(gost_ctx *c, const byte *in, byte *out);
|
56
|
-
/* Decrypt one block */
|
57
|
-
void gostdecrypt(gost_ctx *c, const byte *in,byte *out);
|
58
|
-
/* Set key into context */
|
59
|
-
void gost_key(gost_ctx *ctx, const byte *key);
|
60
|
-
/* Get key from context */
|
61
|
-
void gost_get_key(gost_ctx *ctx, byte *key);
|
62
|
-
/* Set S-blocks into context */
|
63
|
-
void gost_init(gost_ctx *ctx, const gost_subst_block *subst_block);
|
64
|
-
/* Clean up context */
|
65
|
-
void gost_destroy(gost_ctx *ctx);
|
66
|
-
/* Intermediate function used for calculate hash */
|
67
|
-
void gost_enc_with_key(gost_ctx *,byte *key,byte *inblock,byte *outblock);
|
68
|
-
/* Compute MAC of given length in bits from data */
|
69
|
-
int gost_mac(gost_ctx *ctx,int hmac_len,const unsigned char *data,
|
70
|
-
unsigned int data_len,unsigned char *hmac) ;
|
71
|
-
/* Compute MAC of given length in bits from data, using non-zero 8-byte
|
72
|
-
* IV (non-standard, for use in CryptoPro key transport only */
|
73
|
-
int gost_mac_iv(gost_ctx *ctx,int hmac_len,const unsigned char *iv,const unsigned char *data,
|
74
|
-
unsigned int data_len,unsigned char *hmac) ;
|
75
|
-
/* Perform one step of MAC calculation like gostcrypt */
|
76
|
-
void mac_block(gost_ctx *c,byte *buffer,const byte *block);
|
77
|
-
/* Extracts MAC value from mac state buffer */
|
78
|
-
void get_mac(byte *buffer,int nbits,byte *out);
|
79
|
-
/* Implements cryptopro key meshing algorithm. Expect IV to be 8-byte size*/
|
80
|
-
void cryptopro_key_meshing(gost_ctx *ctx, unsigned char *iv);
|
81
|
-
/* Parameter sets specified in RFC 4357 */
|
82
|
-
extern gost_subst_block GostR3411_94_TestParamSet;
|
83
|
-
extern gost_subst_block GostR3411_94_CryptoProParamSet;
|
84
|
-
extern gost_subst_block Gost28147_TestParamSet;
|
85
|
-
extern gost_subst_block Gost28147_CryptoProParamSetA;
|
86
|
-
extern gost_subst_block Gost28147_CryptoProParamSetB;
|
87
|
-
extern gost_subst_block Gost28147_CryptoProParamSetC;
|
88
|
-
extern gost_subst_block Gost28147_CryptoProParamSetD;
|
89
|
-
extern const byte CryptoProKeyMeshingKey[];
|
90
|
-
#if __LONG_MAX__ > 2147483647L
|
91
|
-
typedef unsigned int word32;
|
92
|
-
#else
|
93
|
-
typedef unsigned long word32;
|
94
|
-
#endif
|
95
|
-
|
96
|
-
#endif
|
data/ext/xmldsign/gosthash.c
DELETED
@@ -1,255 +0,0 @@
|
|
1
|
-
/**********************************************************************
|
2
|
-
* gosthash.c *
|
3
|
-
* Copyright (c) 2005-2006 Cryptocom LTD *
|
4
|
-
* This file is distributed under the same license as OpenSSL *
|
5
|
-
* *
|
6
|
-
* Implementation of GOST R 34.11-94 hash function *
|
7
|
-
* uses on gost89.c and gost89.h Doesn't need OpenSSL *
|
8
|
-
**********************************************************************/
|
9
|
-
#include <string.h>
|
10
|
-
|
11
|
-
#include "gost89.h"
|
12
|
-
#include "gosthash.h"
|
13
|
-
|
14
|
-
|
15
|
-
/* Use OPENSSL_malloc for memory allocation if compiled with
|
16
|
-
* -DOPENSSL_BUILD, and libc malloc otherwise
|
17
|
-
*/
|
18
|
-
#ifndef MYALLOC
|
19
|
-
# ifdef OPENSSL_BUILD
|
20
|
-
# include <openssl/crypto.h>
|
21
|
-
# define MYALLOC(size) OPENSSL_malloc(size)
|
22
|
-
# define MYFREE(ptr) OPENSSL_free(ptr)
|
23
|
-
# else
|
24
|
-
# define MYALLOC(size) malloc(size)
|
25
|
-
# define MYFREE(ptr) free(ptr)
|
26
|
-
# endif
|
27
|
-
#endif
|
28
|
-
/* Following functions are various bit meshing routines used in
|
29
|
-
* GOST R 34.11-94 algorithms */
|
30
|
-
static void swap_bytes (byte *w, byte *k)
|
31
|
-
{
|
32
|
-
int i,j;
|
33
|
-
for (i=0;i<4;i++)
|
34
|
-
for (j=0;j<8;j++)
|
35
|
-
k[i+4*j]=w[8*i+j];
|
36
|
-
|
37
|
-
}
|
38
|
-
|
39
|
-
/* was A_A */
|
40
|
-
static void circle_xor8 (const byte *w, byte *k)
|
41
|
-
{
|
42
|
-
byte buf[8];
|
43
|
-
int i;
|
44
|
-
memcpy(buf,w,8);
|
45
|
-
memmove(k,w+8,24);
|
46
|
-
for(i=0;i<8;i++)
|
47
|
-
k[i+24]=buf[i]^k[i];
|
48
|
-
}
|
49
|
-
|
50
|
-
/* was R_R */
|
51
|
-
static void transform_3 (byte *data)
|
52
|
-
{
|
53
|
-
unsigned short int acc;
|
54
|
-
acc=(data[0]^data[2]^data[4]^data[6]^data[24]^data[30])|
|
55
|
-
((data[1]^data[3]^data[5]^data[7]^data[25]^data[31])<<8);
|
56
|
-
memmove(data,data+2,30);
|
57
|
-
data[30]=acc&0xff;
|
58
|
-
data[31]=acc>>8;
|
59
|
-
}
|
60
|
-
|
61
|
-
/* Adds blocks of N bytes modulo 2**(8*n). Returns carry*/
|
62
|
-
static int add_blocks(int n,byte *left, const byte *right)
|
63
|
-
{
|
64
|
-
int i;
|
65
|
-
int carry=0;
|
66
|
-
int sum;
|
67
|
-
for (i=0;i<n;i++)
|
68
|
-
{
|
69
|
-
sum=(int)left[i]+(int)right[i]+carry;
|
70
|
-
left[i]=sum & 0xff;
|
71
|
-
carry=sum>>8;
|
72
|
-
}
|
73
|
-
return carry;
|
74
|
-
}
|
75
|
-
|
76
|
-
/* Xor two sequences of bytes */
|
77
|
-
static void xor_blocks (byte *result,const byte *a,const byte *b,size_t len)
|
78
|
-
{
|
79
|
-
size_t i;
|
80
|
-
for (i=0;i<len;i++) result[i]=a[i]^b[i];
|
81
|
-
}
|
82
|
-
|
83
|
-
/*
|
84
|
-
* Calculate H(i+1) = Hash(Hi,Mi)
|
85
|
-
* Where H and M are 32 bytes long
|
86
|
-
*/
|
87
|
-
static int hash_step(gost_ctx *c,byte *H,const byte *M)
|
88
|
-
{
|
89
|
-
byte U[32],W[32],V[32],S[32],Key[32];
|
90
|
-
int i;
|
91
|
-
/* Compute first key */
|
92
|
-
xor_blocks(W,H,M,32);
|
93
|
-
swap_bytes(W,Key);
|
94
|
-
/* Encrypt first 8 bytes of H with first key*/
|
95
|
-
gost_enc_with_key(c,Key,H,S);
|
96
|
-
/* Compute second key*/
|
97
|
-
circle_xor8(H,U);
|
98
|
-
circle_xor8(M,V);
|
99
|
-
circle_xor8(V,V);
|
100
|
-
xor_blocks(W,U,V,32);
|
101
|
-
swap_bytes(W,Key);
|
102
|
-
/* encrypt second 8 bytes of H with second key*/
|
103
|
-
gost_enc_with_key(c,Key,H+8,S+8);
|
104
|
-
/* compute third key */
|
105
|
-
circle_xor8(U,U);
|
106
|
-
U[31]=~U[31]; U[29]=~U[29]; U[28]=~U[28]; U[24]=~U[24];
|
107
|
-
U[23]=~U[23]; U[20]=~U[20]; U[18]=~U[18]; U[17]=~U[17];
|
108
|
-
U[14]=~U[14]; U[12]=~U[12]; U[10]=~U[10]; U[ 8]=~U[ 8];
|
109
|
-
U[ 7]=~U[ 7]; U[ 5]=~U[ 5]; U[ 3]=~U[ 3]; U[ 1]=~U[ 1];
|
110
|
-
circle_xor8(V,V);
|
111
|
-
circle_xor8(V,V);
|
112
|
-
xor_blocks(W,U,V,32);
|
113
|
-
swap_bytes(W,Key);
|
114
|
-
/* encrypt third 8 bytes of H with third key*/
|
115
|
-
gost_enc_with_key(c,Key,H+16,S+16);
|
116
|
-
/* Compute fourth key */
|
117
|
-
circle_xor8(U,U);
|
118
|
-
circle_xor8(V,V);
|
119
|
-
circle_xor8(V,V);
|
120
|
-
xor_blocks(W,U,V,32);
|
121
|
-
swap_bytes(W,Key);
|
122
|
-
/* Encrypt last 8 bytes with fourth key */
|
123
|
-
gost_enc_with_key(c,Key,H+24,S+24);
|
124
|
-
for (i=0;i<12;i++)
|
125
|
-
transform_3(S);
|
126
|
-
xor_blocks(S,S,M,32);
|
127
|
-
transform_3(S);
|
128
|
-
xor_blocks(S,S,H,32);
|
129
|
-
for (i=0;i<61;i++)
|
130
|
-
transform_3(S);
|
131
|
-
memcpy(H,S,32);
|
132
|
-
return 1;
|
133
|
-
}
|
134
|
-
|
135
|
-
/* Initialize gost_hash ctx - cleans up temporary structures and
|
136
|
-
* set up substitution blocks
|
137
|
-
*/
|
138
|
-
int init_gost_hash_ctx(gost_hash_ctx *ctx, const gost_subst_block *subst_block)
|
139
|
-
{
|
140
|
-
memset(ctx,0,sizeof(gost_hash_ctx));
|
141
|
-
ctx->cipher_ctx = (gost_ctx *)MYALLOC(sizeof(gost_ctx));
|
142
|
-
if (!ctx->cipher_ctx)
|
143
|
-
{
|
144
|
-
return 0;
|
145
|
-
}
|
146
|
-
gost_init(ctx->cipher_ctx,subst_block);
|
147
|
-
return 1;
|
148
|
-
}
|
149
|
-
|
150
|
-
/*
|
151
|
-
* Free cipher CTX if it is dynamically allocated. Do not use
|
152
|
-
* if cipher ctx is statically allocated as in OpenSSL implementation of
|
153
|
-
* GOST hash algroritm
|
154
|
-
*
|
155
|
-
*/
|
156
|
-
void done_gost_hash_ctx(gost_hash_ctx *ctx)
|
157
|
-
{
|
158
|
-
/* No need to use gost_destroy, because cipher keys are not really
|
159
|
-
* secret when hashing */
|
160
|
-
MYFREE(ctx->cipher_ctx);
|
161
|
-
}
|
162
|
-
|
163
|
-
/*
|
164
|
-
* reset state of hash context to begin hashing new message
|
165
|
-
*/
|
166
|
-
int start_hash(gost_hash_ctx *ctx)
|
167
|
-
{
|
168
|
-
if (!ctx->cipher_ctx) return 0;
|
169
|
-
memset(&(ctx->H),0,32);
|
170
|
-
memset(&(ctx->S),0,32);
|
171
|
-
ctx->len = 0L;
|
172
|
-
ctx->left=0;
|
173
|
-
return 1;
|
174
|
-
}
|
175
|
-
|
176
|
-
/*
|
177
|
-
* Hash block of arbitrary length
|
178
|
-
*
|
179
|
-
*
|
180
|
-
*/
|
181
|
-
int hash_block(gost_hash_ctx *ctx,const byte *block, size_t length)
|
182
|
-
{
|
183
|
-
const byte *curptr=block;
|
184
|
-
const byte *barrier=block+(length-32);/* Last byte we can safely hash*/
|
185
|
-
if (ctx->left)
|
186
|
-
{
|
187
|
-
/*There are some bytes from previous step*/
|
188
|
-
unsigned int add_bytes = 32-ctx->left;
|
189
|
-
if (add_bytes>length)
|
190
|
-
{
|
191
|
-
add_bytes = length;
|
192
|
-
}
|
193
|
-
memcpy(&(ctx->remainder[ctx->left]),block,add_bytes);
|
194
|
-
ctx->left+=add_bytes;
|
195
|
-
if (ctx->left<32)
|
196
|
-
{
|
197
|
-
return 1;
|
198
|
-
}
|
199
|
-
curptr=block+add_bytes;
|
200
|
-
hash_step(ctx->cipher_ctx,ctx->H,ctx->remainder);
|
201
|
-
add_blocks(32,ctx->S,ctx->remainder);
|
202
|
-
ctx->len+=32;
|
203
|
-
ctx->left=0;
|
204
|
-
}
|
205
|
-
while (curptr<=barrier)
|
206
|
-
{
|
207
|
-
hash_step(ctx->cipher_ctx,ctx->H,curptr);
|
208
|
-
|
209
|
-
add_blocks(32,ctx->S,curptr);
|
210
|
-
ctx->len+=32;
|
211
|
-
curptr+=32;
|
212
|
-
}
|
213
|
-
if (curptr!=block+length)
|
214
|
-
{
|
215
|
-
ctx->left=block+length-curptr;
|
216
|
-
memcpy(ctx->remainder,curptr,ctx->left);
|
217
|
-
}
|
218
|
-
return 1;
|
219
|
-
}
|
220
|
-
|
221
|
-
/*
|
222
|
-
* Compute hash value from current state of ctx
|
223
|
-
* state of hash ctx becomes invalid and cannot be used for further
|
224
|
-
* hashing.
|
225
|
-
*/
|
226
|
-
int finish_hash(gost_hash_ctx *ctx,byte *hashval)
|
227
|
-
{
|
228
|
-
byte buf[32];
|
229
|
-
byte H[32];
|
230
|
-
byte S[32];
|
231
|
-
ghosthash_len fin_len=ctx->len;
|
232
|
-
byte *bptr;
|
233
|
-
memcpy(H,ctx->H,32);
|
234
|
-
memcpy(S,ctx->S,32);
|
235
|
-
if (ctx->left)
|
236
|
-
{
|
237
|
-
memset(buf,0,32);
|
238
|
-
memcpy(buf,ctx->remainder,ctx->left);
|
239
|
-
hash_step(ctx->cipher_ctx,H,buf);
|
240
|
-
add_blocks(32,S,buf);
|
241
|
-
fin_len+=ctx->left;
|
242
|
-
}
|
243
|
-
memset(buf,0,32);
|
244
|
-
bptr=buf;
|
245
|
-
fin_len<<=3; /* Hash length in BITS!!*/
|
246
|
-
while(fin_len>0)
|
247
|
-
{
|
248
|
-
*(bptr++)=(byte)(fin_len&0xFF);
|
249
|
-
fin_len>>=8;
|
250
|
-
};
|
251
|
-
hash_step(ctx->cipher_ctx,H,buf);
|
252
|
-
hash_step(ctx->cipher_ctx,H,S);
|
253
|
-
memcpy(hashval,H,32);
|
254
|
-
return 1;
|
255
|
-
}
|
data/ext/xmldsign/gosthash.h
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
/**********************************************************************
|
2
|
-
* gosthash.h *
|
3
|
-
* Copyright (c) 2005-2006 Cryptocom LTD *
|
4
|
-
* This file is distributed under the same license as OpenSSL *
|
5
|
-
* *
|
6
|
-
* Declaration of GOST R 34.11-94 hash functions *
|
7
|
-
* uses and gost89.h Doesn't need OpenSSL *
|
8
|
-
**********************************************************************/
|
9
|
-
#ifndef GOSTHASH_H
|
10
|
-
#define GOSTHASH_H
|
11
|
-
#include "gost89.h"
|
12
|
-
#include <stdlib.h>
|
13
|
-
|
14
|
-
#if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
|
15
|
-
typedef __int64 ghosthash_len;
|
16
|
-
#elif defined(__arch64__)
|
17
|
-
typedef long ghosthash_len;
|
18
|
-
#else
|
19
|
-
typedef long long ghosthash_len;
|
20
|
-
#endif
|
21
|
-
|
22
|
-
typedef struct gost_hash_ctx {
|
23
|
-
ghosthash_len len;
|
24
|
-
gost_ctx *cipher_ctx;
|
25
|
-
int left;
|
26
|
-
byte H[32];
|
27
|
-
byte S[32];
|
28
|
-
byte remainder[32];
|
29
|
-
} gost_hash_ctx;
|
30
|
-
|
31
|
-
|
32
|
-
/* Initalizes gost hash ctx, including creation of gost cipher ctx */
|
33
|
-
|
34
|
-
int init_gost_hash_ctx(gost_hash_ctx *ctx, const gost_subst_block *subst_block);
|
35
|
-
void done_gost_hash_ctx(gost_hash_ctx *ctx);
|
36
|
-
|
37
|
-
/* Cleans up all fields, except cipher ctx preparing ctx for computing
|
38
|
-
* of new hash value */
|
39
|
-
int start_hash(gost_hash_ctx *ctx);
|
40
|
-
|
41
|
-
/* Hashes block of data */
|
42
|
-
int hash_block(gost_hash_ctx *ctx, const byte *block, size_t length);
|
43
|
-
|
44
|
-
/* Finalizes computation of hash and fills buffer (which should be at
|
45
|
-
* least 32 bytes long) with value of computed hash. */
|
46
|
-
int finish_hash(gost_hash_ctx *ctx, byte *hashval);
|
47
|
-
|
48
|
-
#endif
|
data/ext/xmldsign/xmldsign_ext.c
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
#include <xmldsign_ext.h>
|
2
|
-
|
3
|
-
#ifndef O_BINARY
|
4
|
-
#define O_BINARY 0
|
5
|
-
#endif
|
6
|
-
|
7
|
-
int digest(char * data, byte * sum)
|
8
|
-
{
|
9
|
-
gost_subst_block *b= &GostR3411_94_CryptoProParamSet;
|
10
|
-
gost_hash_ctx ctx;
|
11
|
-
|
12
|
-
init_gost_hash_ctx(&ctx, b);
|
13
|
-
|
14
|
-
if (hash_data(&ctx, data, sum))
|
15
|
-
{
|
16
|
-
return 1;
|
17
|
-
}
|
18
|
-
else
|
19
|
-
return 0;
|
20
|
-
}
|
21
|
-
|
22
|
-
int hash_data(gost_hash_ctx *ctx, const char *data, byte *sum)
|
23
|
-
{
|
24
|
-
int i;
|
25
|
-
size_t bytes = strlen(data);
|
26
|
-
|
27
|
-
start_hash(ctx);
|
28
|
-
|
29
|
-
if(bytes > 0)
|
30
|
-
hash_block(ctx, data, bytes);
|
31
|
-
|
32
|
-
finish_hash(ctx, sum);
|
33
|
-
|
34
|
-
return 1;
|
35
|
-
}
|
36
|
-
|
37
|
-
static VALUE rb_gost_digest(VALUE self, VALUE data)
|
38
|
-
{
|
39
|
-
byte sum[32];
|
40
|
-
|
41
|
-
if( digest(StringValuePtr(data), sum) )
|
42
|
-
return rb_str_new( sum, 32 );
|
43
|
-
else
|
44
|
-
return Qfalse;
|
45
|
-
}
|
46
|
-
|
47
|
-
/* Ruby Extension initializer */
|
48
|
-
void Init_xmldsign_ext() {
|
49
|
-
mXmldsign = rb_define_module("Xmldsign");
|
50
|
-
mDigests = rb_define_module_under(mXmldsign, "Digests");
|
51
|
-
cGost = rb_define_class_under(mDigests, "Gost", rb_cObject);
|
52
|
-
|
53
|
-
rb_define_method(cGost, "binary", rb_gost_digest, 1);
|
54
|
-
}
|
data/ext/xmldsign/xmldsign_ext.h
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
#ifndef XMLDSIGN_EXT_H
|
2
|
-
#define XMLDSIGN_EXT_H
|
3
|
-
|
4
|
-
#include <ruby.h>
|
5
|
-
|
6
|
-
#include <stdio.h>
|
7
|
-
#include <stdlib.h>
|
8
|
-
#include <unistd.h>
|
9
|
-
#include <limits.h>
|
10
|
-
#include <string.h>
|
11
|
-
|
12
|
-
#include "gost89.h"
|
13
|
-
#include "gosthash.h"
|
14
|
-
|
15
|
-
VALUE mXmldsign, mDigests, cGost;
|
16
|
-
|
17
|
-
|
18
|
-
#endif /* ifndef XMLDSIGN_EXT_H */
|