noise-ruby 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 224564f27800225c1ed1317359e630a06fe343b64db95db7456aece250b56026
4
- data.tar.gz: aef9533145ef39d8b9ca9ff7d31d2dd0b2e20808090b818601c3246e5c228eab
3
+ metadata.gz: 9731ebe9c0021aacc2dd916c8c37d42a4354a0aea6ef90c1f2983dc6cc200c48
4
+ data.tar.gz: 057bf0bf52963ba3f988f75ea0ed69d6c93df359ec0bd9017476960f492fef1c
5
5
  SHA512:
6
- metadata.gz: 7b18a8fbe1d077dab40e880dd6cea6920f788307365186eebe798a72bc5d337fb95caca237cbfaf5f12447bbec15b5499332ea86e0003d6d8bff9229e0d0e5f7
7
- data.tar.gz: 6a06a1687846b22e88a1634615b7cfdf633619e59773c1929c108c6ff69d742f53112f1cba24952689f473d2577fa25a614cebefe85e7021ea53237097c0775c
6
+ metadata.gz: 908ad0b81ab191a5f90b86a772b2af393ce82ef9df7fcda0deedfae345f449f5f997fa686e554139b1795fd29c74126eb12ef8c03543d362f9d2d1913a47b7b1
7
+ data.tar.gz: c11189bf83c51b1b4d80c054f5e0db0c2a43cad1304929816bc155b0cc38ef35e88e9b42f629b7c48de0b754ba87106d16d06b5c0b01776e9f1350a0b81e2273
data/CODE_OF_CONDUCT.md CHANGED
@@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
55
55
  ## Enforcement
56
56
 
57
57
  Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at h_yamaguchi@haw.co.jp. All
58
+ reported by contacting the project team at gen.yamaguchi0@gmail.com. All
59
59
  complaints will be reviewed and investigated and will result in a response that
60
60
  is deemed necessary and appropriate to the circumstances. The project team is
61
61
  obligated to maintain confidentiality with regard to the reporter of an incident.
data/Gemfile CHANGED
@@ -6,3 +6,6 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
6
 
7
7
  # Specify your gem's dependencies in noise.gemspec
8
8
  gemspec
9
+
10
+ # Use secp256k1 as hash function
11
+ # gem 'secp256k1-ruby'
data/README.md CHANGED
@@ -15,20 +15,9 @@ The followings are not supported yet.
15
15
 
16
16
  - DH Functions
17
17
  - Curve448
18
- - Hash Functions
19
- - Blake2s
20
18
 
21
19
  ## Installation
22
20
 
23
- This library requires [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
24
-
25
- $ git clone https://github.com/bitcoin-core/secp256k1
26
- $ cd secp256k1
27
- $ ./autogen.sh
28
- $ ./configure --enable-experimental --enable-module-ecdh --enable-module-recovery --enable-benchmark=false
29
- $ make
30
- $ sudo make install
31
-
32
21
  Add this line to your application's Gemfile:
33
22
 
34
23
  ```
@@ -43,6 +32,21 @@ Or install it yourself as:
43
32
 
44
33
  $ gem install noise-ruby
45
34
 
35
+ If you use Secp256k1, you must install [libsecp256k1](https://github.com/bitcoin-core/secp256k1).
36
+
37
+ $ git clone https://github.com/bitcoin-core/secp256k1
38
+ $ cd secp256k1
39
+ $ ./autogen.sh
40
+ $ ./configure --enable-module-recovery --enable-experimental --enable-module-ecdh
41
+ $ make
42
+ $ sudo make install
43
+
44
+ and, add this line to your Gemfile:
45
+
46
+ ```
47
+ gem 'secp256k1-ruby'
48
+ ```
49
+
46
50
  ## Usage
47
51
 
48
52
  TODO: Write usage instructions here
data/lib/noise.rb CHANGED
@@ -5,7 +5,6 @@ require 'noise/version'
5
5
  require 'ecdsa'
6
6
  require 'rbnacl'
7
7
  require 'ruby_hmac'
8
- require 'secp256k1'
9
8
  require 'securerandom'
10
9
 
11
10
  require 'noise/utils/hash'
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'secp256k1'
4
+
3
5
  module Noise
4
6
  module Functions
5
7
  module DH
@@ -9,11 +9,12 @@ module Noise
9
9
  autoload :Sha512, 'noise/functions/hash/sha512'
10
10
 
11
11
  def self.hmac_hash(key, data, digest)
12
- # TODO: support for blake2b, blake2s
13
12
  if digest.include?('SHA')
14
13
  OpenSSL::HMAC.digest(OpenSSL::Digest.new(digest), key, data)
15
14
  elsif digest.include?('BLAKE2b')
16
15
  Noise::Functions::Hash::Blake2bHMAC.new(key).update(data).digest
16
+ elsif digest.include?('BLAKE2s')
17
+ Noise::Functions::Hash::Blake2sHMAC.new(key).update(data).digest
17
18
  end
18
19
  end
19
20
 
@@ -7,7 +7,7 @@ module Noise
7
7
  HASHLEN = 32
8
8
  BLOCKLEN = 64
9
9
  def hash(data)
10
- throw NotImplementedError
10
+ Noise::Functions::Hash::Blake2sDigester.new.update(data).digest
11
11
  end
12
12
 
13
13
  def hashlen
@@ -18,6 +18,177 @@ module Noise
18
18
  BLOCKLEN
19
19
  end
20
20
  end
21
+
22
+ class Blake2sHMAC < HMAC::Base
23
+ def initialize(key = '')
24
+ super(Blake2sDigester, Blake2s::BLOCKLEN, Blake2s::HASHLEN, key)
25
+ end
26
+ public_class_method :new, :digest, :hexdigest
27
+ end
28
+
29
+ class Blake2sDigester
30
+ IV = [0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19]
31
+ SIGMA = [
32
+ [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 ],
33
+ [ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 ],
34
+ [ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 ],
35
+ [ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 ],
36
+ [ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 ],
37
+ [ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 ],
38
+ [ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 ],
39
+ [ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 ],
40
+ [ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 ],
41
+ [ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 ]
42
+ ]
43
+
44
+ def initialize(key: '')
45
+ @key = key
46
+ @ctx = init(Blake2s::HASHLEN, @key.unpack("C*"))
47
+ end
48
+
49
+ def update(data)
50
+ update_internal(@ctx, data.unpack("C*"))
51
+ self
52
+ end
53
+
54
+ def digest
55
+ out = []
56
+ final(@ctx, out)
57
+ out.pack("C*")
58
+ end
59
+
60
+ # @return context
61
+ def init(out_len, key)
62
+ raise ArgumentError if out_len == 0 || out_len > 32
63
+ h = IV.dup
64
+ h[0] ^= 0x01010000 ^ (key.size << 8) ^ out_len
65
+ t = [0, 0]
66
+ c = 0
67
+ b = Array.new(Blake2s::BLOCKLEN).fill(0, key.size)
68
+ ctx = Context.new(b, h, t, c, out_len)
69
+ if key.size > 0
70
+ update_internal(ctx, key)
71
+ ctx.c = 64
72
+ end
73
+ ctx
74
+ end
75
+
76
+ def update_internal(ctx, input)
77
+ input.size.times do |i|
78
+ if ctx.c == Blake2s::BLOCKLEN
79
+ ctx.t[0] += ctx.c
80
+ # if ctx.t[0] < ctx.c
81
+ if ctx.t[0] > 0xFFFFFFFF
82
+ ctx.t[0] = ctx.t[0] - 0xFFFFFFFF
83
+ ctx.t[1] += 1
84
+ end
85
+ compress(ctx, false)
86
+ ctx.c = 0
87
+ end
88
+
89
+ ctx.b[ctx.c] = input[i]
90
+ ctx.c += 1
91
+ end
92
+ end
93
+
94
+ def final(ctx, out)
95
+ ctx.t[0] += ctx.c
96
+ if ctx.t[0] > 0xFFFFFFFF
97
+ ctx.t[0] = ctx.t[0] - 0xFFFFFFFF
98
+ ctx.t[1] += 1
99
+ end
100
+
101
+ while ctx.c < Blake2s::BLOCKLEN
102
+ ctx.b[ctx.c] = 0
103
+ ctx.c += 1
104
+ end
105
+ compress(ctx, true)
106
+ ctx.out_len.times do |i|
107
+ out << ((ctx.h[i >> 2] >> (8 * (i & 3))) & 0xff)
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def to_int32(x)
114
+ x = x & 0xFFFFFFFF
115
+ x < 0x80000000 ? x : x - 2**32
116
+ end
117
+
118
+ def rshift(x, y, range=32)
119
+ (x + (x > 0 ? 0 : 2 ** range)) >> y
120
+ end
121
+
122
+ def rotr32(x, y)
123
+ to_int32(x << (32 - y) ^ rshift(x & 0xFFFFFFFF, y))
124
+ end
125
+
126
+ def get32(p0, p1, p2, p3)
127
+ (p0 & 0xFF) | ((p1 & 0xFF) << 8) | ((p2 & 0xFF) << 16) | ((p3 & 0xFF) << 24)
128
+ end
129
+
130
+ def mix_g(v, a, b, c, d, x, y)
131
+ v[a] = v[a] + v[b] + x
132
+ v[d] = v[d] ^ v[a]
133
+ v[d] = rotr32(v[d], 16)
134
+ v[c] = v[c] + v[d]
135
+ v[b] = v[b] ^ v[c]
136
+ v[b] = rotr32(v[b], 12)
137
+
138
+ v[a] = v[a] + v[b] + y
139
+ v[d] = v[d] ^ v[a]
140
+ v[d] = rotr32(v[d], 8)
141
+ v[c] = v[c] + v[d]
142
+ v[b] = v[b] ^ v[c]
143
+ v[b] = rotr32(v[b], 7)
144
+ end
145
+
146
+ def compress(ctx, last)
147
+ v = Array.new(16)
148
+ m = Array.new(16)
149
+ 8.times do |i|
150
+ v[i] = ctx.h[i]
151
+ v[i + 8] = IV[i]
152
+ end
153
+
154
+ v[12] ^= ctx.t[0]
155
+ v[13] ^= ctx.t[1]
156
+
157
+ if last
158
+ v[14] = ~v[14] & 0xFFFFFFFF
159
+ end
160
+
161
+ 16.times do |i|
162
+ m[i] = get32(ctx.b[4 * i], ctx.b[4 * i + 1], ctx.b[4 * i + 2], ctx.b[4 * i + 3])
163
+ end
164
+
165
+ 10.times do |i|
166
+ mix_g(v, 0, 4, 8, 12, m[SIGMA[i][ 0]], m[SIGMA[i][ 1]])
167
+ mix_g(v, 1, 5, 9, 13, m[SIGMA[i][ 2]], m[SIGMA[i][ 3]])
168
+ mix_g(v, 2, 6, 10, 14, m[SIGMA[i][ 4]], m[SIGMA[i][ 5]])
169
+ mix_g(v, 3, 7, 11, 15, m[SIGMA[i][ 6]], m[SIGMA[i][ 7]])
170
+ mix_g(v, 0, 5, 10, 15, m[SIGMA[i][ 8]], m[SIGMA[i][ 9]])
171
+ mix_g(v, 1, 6, 11, 12, m[SIGMA[i][10]], m[SIGMA[i][11]])
172
+ mix_g(v, 2, 7, 8, 13, m[SIGMA[i][12]], m[SIGMA[i][13]])
173
+ mix_g(v, 3, 4, 9, 14, m[SIGMA[i][14]], m[SIGMA[i][15]])
174
+ end
175
+
176
+ 8.times do |i|
177
+ ctx.h[i] ^= v[i] ^ v[i + 8]
178
+ end
179
+ end
180
+
181
+ class Context
182
+ attr_accessor :b, :h, :t, :c, :out_len
183
+ def initialize(b, h, t, c, out_len)
184
+ @b = b
185
+ @h = h
186
+ @t = t
187
+ @c = c
188
+ @out_len = out_len
189
+ end
190
+ end
191
+ end
21
192
  end
22
193
  end
23
194
  end
data/lib/noise/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Noise
4
- VERSION = '0.6.3'
4
+ VERSION = '0.7.0'
5
5
  end
data/noise.gemspec CHANGED
@@ -24,8 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency 'bundler', '~> 1.15'
25
25
  spec.add_development_dependency 'rake', '~> 10.0'
26
26
  spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'secp256k1-ruby'
27
28
 
28
- spec.add_runtime_dependency 'secp256k1-ruby'
29
29
  spec.add_runtime_dependency 'ecdsa'
30
30
  spec.add_runtime_dependency 'rbnacl'
31
31
  spec.add_runtime_dependency 'ruby-hmac'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noise-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hajime Yamaguchi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-11-15 00:00:00.000000000 Z
11
+ date: 2019-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -59,7 +59,7 @@ dependencies:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- type: :runtime
62
+ type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements: