x25519 1.0.2 → 1.0.3

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
  SHA1:
3
- metadata.gz: fb37496fe14ef2bf58826e5b30805597b7031c5b
4
- data.tar.gz: 02e5d45c634da965b9d351da48c1213f16de2eb0
3
+ metadata.gz: 228a5fff8ae6b51e46c91ab6e0fd9fce4ec03fbf
4
+ data.tar.gz: e2562bda953f96a37e0188820f6139641b715c6b
5
5
  SHA512:
6
- metadata.gz: f8c40748f5937cba15380f71e729924764ee8e50f2992aff31ae91100d3efad634ee03ba51da02a62d4de5f243e56c7fc31af47d615b4b4a492df7f5ab937e43
7
- data.tar.gz: afae5c90642ae8eeec06c2e107d522f0da19b4b74f6c6b22cd386ffe6590ec8bcab665b1affd136505a38a0b1aed3011d6034a9f7ce392bd106f3f2321492858
6
+ metadata.gz: 2eb051180a4fdf3f4735bcd39aad32a13d117bd3aad7ee1597f2563b7ae3afb3f1056b52caf4ffc7efce335cdeeb79497ce848388ee5d2f663c499f6067ae01a
7
+ data.tar.gz: 4f850779d9401fcfdfe7bf27bb89637c7848eec13158f5f7a176c7ca98eb8a34f546c116f16a5378b12df179790f86384e292abfb765277eba87a2686ea59358
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [1.0.3] (2017-12-13)
2
+
3
+ [1.0.3]: https://github.com/cryptosphere/x25519/compare/v1.0.2...v1.0.3
4
+
5
+ * [#10](https://github.com/cryptosphere/x25519/pull/10)
6
+ Detect degenerate (i.e. all-zero) public keys (fixes #6)
7
+
1
8
  # [1.0.2] (2017-12-13)
2
9
 
3
10
  [1.0.2]: https://github.com/cryptosphere/x25519/compare/v1.0.1...v1.0.2
data/lib/x25519.rb CHANGED
@@ -19,6 +19,9 @@ module X25519
19
19
  # Size of an X25519 key (public or private) in bytes
20
20
  KEY_SIZE = 32
21
21
 
22
+ # Raised when we detect a degenerate (i.e. all-zero) public key
23
+ InvalidKeyError = Class.new(StandardError)
24
+
22
25
  # Raised when the built-in self-test fails
23
26
  SelfTestFailure = Class.new(StandardError)
24
27
 
@@ -35,6 +38,18 @@ module X25519
35
38
  # fall back to the ref10 portable C implementation.
36
39
  self.provider = X25519::Provider::Precomputed if X25519::Provider::Precomputed.available?
37
40
 
41
+ # Raw fixed-base scalar multiplication function that acts directly on
42
+ # bytestrings. Calculates the coordinate of the elliptic curve point that
43
+ # represents the public key for a given scalar.
44
+ #
45
+ # @param scalar_bytes [String] a serialized private scalar
46
+ #
47
+ # @return [String] compressed Montgomery-u coordinate of the resulting point
48
+ def calculate_public_key(scalar_bytes)
49
+ validate_key_bytes(scalar_bytes)
50
+ provider.scalarmult_base(scalar_bytes)
51
+ end
52
+
38
53
  # Raw Diffie-Hellman function that acts directly on bytestrings. An
39
54
  # alternative to the object-oriented API
40
55
  #
@@ -45,7 +60,12 @@ module X25519
45
60
  def diffie_hellman(scalar_bytes, montgomery_u_bytes)
46
61
  validate_key_bytes(scalar_bytes)
47
62
  validate_key_bytes(montgomery_u_bytes)
48
- X25519.provider.scalarmult(scalar_bytes, montgomery_u_bytes)
63
+
64
+ # The point located at a Montgomery-u coordinate of zero always returns
65
+ # the point at zero regardless of which scalar it's multiplied with
66
+ raise InvalidKeyError, "degenerate public key" if montgomery_u_bytes == ("\0" * KEY_SIZE)
67
+
68
+ provider.scalarmult(scalar_bytes, montgomery_u_bytes)
49
69
  end
50
70
 
51
71
  # Ensure a serialized key meets the requirements
@@ -11,6 +11,11 @@ module X25519
11
11
  # @param bytes [String] 32-byte compressed Montgomery-u coordinate
12
12
  def initialize(bytes)
13
13
  X25519.validate_key_bytes(bytes)
14
+
15
+ # The point located at a Montgomery-u coordinate of zero always returns
16
+ # the point at zero regardless of which scalar it's multiplied with
17
+ raise InvalidKeyError, "degenerate public key" if bytes == ("\0" * KEY_SIZE)
18
+
14
19
  @bytes = bytes
15
20
  end
16
21
 
data/lib/x25519/scalar.rb CHANGED
@@ -26,20 +26,20 @@ module X25519
26
26
  # @param montgomery_u [X25519::MontgomeryU] coordinate of the public key/point to perform D-H with
27
27
  #
28
28
  # @return [X25519::MontgomeryU] resulting point (i.e. D-H shared secret)
29
- def multiply(montgomery_u)
29
+ def diffie_hellman(montgomery_u)
30
30
  raise TypeError, "expected X25519::MontgomeryU, got #{montgomery_u}" unless montgomery_u.is_a?(MontgomeryU)
31
- MontgomeryU.new(X25519.provider.scalarmult(@scalar_bytes, montgomery_u.to_bytes))
31
+ MontgomeryU.new(X25519.diffie_hellman(@scalar_bytes, montgomery_u.to_bytes))
32
32
  end
33
- alias diffie_hellman multiply
33
+ alias multiply diffie_hellman
34
34
 
35
35
  # Fixed-base scalar multiplication. Calculates a public key from a
36
36
  # private scalar
37
37
  #
38
38
  # @return [X25519::MontgomeryU] resulting point (i.e. public key)
39
- def multiply_base
40
- MontgomeryU.new(X25519.provider.scalarmult_base(@scalar_bytes))
39
+ def public_key
40
+ MontgomeryU.new(X25519.calculate_public_key(@scalar_bytes))
41
41
  end
42
- alias public_key multiply_base
42
+ alias multiply_base public_key
43
43
 
44
44
  # Return a bytestring representation of this scalar
45
45
  #
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module X25519
4
- VERSION = "1.0.2"
4
+ VERSION = "1.0.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: x25519
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Arcieri
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-13 00:00:00.000000000 Z
11
+ date: 2017-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler