h2c 0.2.1 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3de2455d74c152fea54a81e553295ba16f3122e1af6e3d2196e52055eb179180
4
- data.tar.gz: 6bc78d7cbfa6110f5cee57aff3d099e1a86a25f3a511b7ea13ad4b366aa38bab
3
+ metadata.gz: 0a7b6c9428d078ca27c842f19c62bde2898392719ea38eba400ad7da05b42b14
4
+ data.tar.gz: 2f25543f2e98f350b200c2674639a49922f1a211dbe58d2bb2673bdc924cf384
5
5
  SHA512:
6
- metadata.gz: 7654446bc65c08a1eb073f4f4de149809e9b44c58d19f9eb420ff2a6c9f9277c5e2b7e7827cde92514e3807fa1ea7544e344be9441c9ac636d256dbdff522af6
7
- data.tar.gz: 8e4f8ee2aed37e8639869ea488a276f597dc0264cacd50bd8dc1a22ff2316da3ee2adc879af92a922f54e2f6cd7307d57368ec53be8a4d95a6e551fe38465239
6
+ metadata.gz: e76d02628e7938801beaf24275793d26e1811aca3adfc4b180951b9b9ea419c9eb7705e06b2eb76503c1955a100ec5e209a6ad8fdabbcf9634aaa8299d104e4b
7
+ data.tar.gz: a580f648bc69621849c7bb825e90cd2f3b58d27619e6a8a05f8b79b7396598682751d628e2ea6e257641ffab3f44cf47959966e4f495f97dd7ada0c9c15ddc97
data/.rubocop.yml CHANGED
@@ -1,4 +1,4 @@
1
- require:
1
+ plugins:
2
2
  - rubocop-rake
3
3
  - rubocop-rspec
4
4
  inherit_gem:
@@ -17,5 +17,7 @@ Style/WhileUntilModifier:
17
17
  Enabled: false
18
18
  Naming/MethodParameterName:
19
19
  Enabled: false
20
- RSpec/FilePath:
20
+ RSpec/SpecFilePathFormat:
21
+ Enabled: false
22
+ RSpec/SpecFilePathSuffix:
21
23
  Enabled: false
data/README.md CHANGED
@@ -4,6 +4,22 @@ This is a Ruby implementation of the [Hash to Curves](https://github.com/cfrg/dr
4
4
 
5
5
  It has been tested using the Test Vector provided, but the code has not been audited to ensure functional specification and safety. Also It is under development and is subject to change without backward compatibility.
6
6
 
7
+ ## Security considerations
8
+
9
+ **This library is not constant-time and must not be used with secret input messages.**
10
+
11
+ [RFC 9380 Section 4](https://www.rfc-editor.org/rfc/rfc9380.html#section-4) requires a constant-time implementation when the message being hashed is secret. This library does not meet that requirement:
12
+
13
+ * Field arithmetic is performed with Ruby's arbitrary-precision `Integer`, whose execution time depends on the operand values.
14
+ * The mapping in `H2C::M2C::SSWU#map` selects between candidates with ordinary `if`/ternary branches rather than constant-time conditional moves.
15
+ * Byte-string handling in `H2C::Expander` allocates and copies in ways that depend on the requested length.
16
+
17
+ An attacker able to measure timing or cache behaviour may therefore be able to recover information about the input. Hashing a **public** message to a curve point (the primary use case of RFC 9380) is unaffected.
18
+
19
+ In particular, do not use this library to derive nonces or key material directly from secret values — for example, FROST's `nonce_generate`, which hashes a secret share. Such protocols require a constant-time implementation.
20
+
21
+ Additionally, `H2C::HashToPoint` and the `H2C::Expander` instance it holds keep mutable digest state, so a single instance must not be shared across threads. Create one instance per thread instead.
22
+
7
23
  The following cipher suites are currently supported:
8
24
 
9
25
  * secp256k1_XMD:SHA-256_SSWU_NU_
@@ -46,7 +46,8 @@ module H2C
46
46
 
47
47
  bi = digest.digest
48
48
  pseudo = bi
49
- (2..(ell + 1)).each do |i|
49
+ # b_1 is already computed above, so this yields b_1, ..., b_ell in total.
50
+ (2..ell).each do |i|
50
51
  digest.reset
51
52
  digest.update(Expander.xor(b0, bi))
52
53
  digest.update([i].pack("C"))
data/lib/h2c/expander.rb CHANGED
@@ -32,12 +32,15 @@ module H2C
32
32
 
33
33
  # Get expander implementation
34
34
  # @param [String] func Hash function name. Currently supported by 'SHA-256' and 'SHA-512'.
35
- # @raise [H2C::Error] If invalid func specified.
35
+ # @param [String] dst Domain separation tag with binary format. Must not be empty.
36
+ # @raise [H2C::Error] If invalid func or empty dst specified.
36
37
  # @return [XMD] expander implementation, currently only XMD is supported.
37
38
  def get(func, dst, _k)
38
39
  unless HashFunc::XMD_FUNCS.include?(func)
39
40
  raise H2C::Error, "func #{func} is unsupported."
40
41
  end
42
+ # RFC 9380 3.1 requires domain separation tags to have nonzero length.
43
+ raise H2C::Error, "dst must not be empty." if dst.nil? || dst.empty?
41
44
  XMD.new(func, dst)
42
45
  # TODO: XOR
43
46
  end
@@ -42,7 +42,7 @@ module H2C
42
42
  v = []
43
43
  suite.m.times do |j|
44
44
  offset = suite.l * (j + i * suite.m)
45
- t = pseudo[offset, (offset + suite.l)]
45
+ t = pseudo[offset, suite.l]
46
46
  vj = t.unpack1("H*").to_i(16)
47
47
  v[j] = vj % modulo
48
48
  end
data/lib/h2c/m2c/sswu.rb CHANGED
@@ -25,8 +25,9 @@ module H2C
25
25
  t1 = f.mod(f.power(u, 2) * f.mod(z))
26
26
  t2 = f.power(t1, 2)
27
27
  x1 = f.mod(t1 + t2)
28
- x1 = f.inverse(x1)
29
28
  e1 = x1.zero?
29
+ # inv0(x) is the multiplicative inverse of x, with inv0(0) = 0.
30
+ x1 = e1 ? 0 : f.inverse(x1)
30
31
  x1 = f.mod(x1 + 1)
31
32
  x1 = e1 ? c2 : x1
32
33
  x1 = f.mod(x1 * c1)
@@ -47,7 +48,7 @@ module H2C
47
48
  end
48
49
 
49
50
  def square?(x)
50
- test = curve.field.power(x, ((curve.field.prime - 1) / 2))
51
+ test = curve.field.power(x, (curve.field.prime - 1) / 2)
51
52
  [0, 1].include?(test)
52
53
  end
53
54
 
data/lib/h2c/suite.rb CHANGED
@@ -56,7 +56,7 @@ module H2C
56
56
  @map = M2C::SSWU.new(ECDSA::Group::Nistp521, -4)
57
57
  @ro = (id == P521_XMDSHA512_SSWU_RO_)
58
58
  else
59
- raise H2C::Error, "suite #{curve} unsupported."
59
+ raise H2C::Error, "suite #{id} unsupported."
60
60
  end
61
61
  end
62
62
  end
data/lib/h2c/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module H2C
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: h2c
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - azuchi
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-02-06 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: ecdsa
@@ -69,7 +68,6 @@ metadata:
69
68
  homepage_uri: https://github.com/azuchi/h2c
70
69
  source_code_uri: https://github.com/azuchi/h2c
71
70
  changelog_uri: https://github.com/azuchi/h2c
72
- post_install_message:
73
71
  rdoc_options: []
74
72
  require_paths:
75
73
  - lib
@@ -84,8 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
82
  - !ruby/object:Gem::Version
85
83
  version: '0'
86
84
  requirements: []
87
- rubygems_version: 3.4.1
88
- signing_key:
85
+ rubygems_version: 3.6.9
89
86
  specification_version: 4
90
87
  summary: Ruby implementation of hash to curve.
91
88
  test_files: []