nuckle 0.3.0 → 0.4.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: 545090d9c8b1c29a39666b5a6e036282a138a67ed06659aa9a3362e5cce147f7
4
- data.tar.gz: ec96d6f19f26142af27e438e75b0441d0d81dfec8c1d80250114c213380d440a
3
+ metadata.gz: c5e6f2dd3c188bdd913b913406835a4f1a316bbc67bb5df1f45abacea0e0a269
4
+ data.tar.gz: dfc1c52875126816b9d65d39a664340ff0e3cdb7b6bffc7c0634007e8b5eef40
5
5
  SHA512:
6
- metadata.gz: 8c943b18a056cf8a44cd264db3960b863168e3ec7ca694e1f8d50e28cf05cc3c478fc677fca88b42df355c9986221f7f59b6841f8ada0872a80deb3badada865
7
- data.tar.gz: 9527421844ba5513e96fa1e7ef69b230c550d0b049fc261622230d7a048e718b7f45aca2f19f08e25e6abb8f4b9b8f993468a3e0ab4992ee9014422796de83db
6
+ metadata.gz: 2a9780a5aae93b1eabbcc9917fa05930342712b56f2719c5205d5cbfbd8f232d9d0b912fa2c3d08fec4bbc9181f46727dc8678a8c1c18e5ecb5eeafa8641815e
7
+ data.tar.gz: af0a0dedac64cc08da2cdbd9ebcdaa478ea66dd9b876dac3f9f06f615883c1f0d7e29b3dabe1424a6638c0418d39c402d3fd3ce455b5e46bb1a496d219134fdc
data/README.md CHANGED
@@ -74,11 +74,37 @@ If you know what you're doing:
74
74
  RbNaCl = Nuckle
75
75
  ```
76
76
 
77
+ ## Performance
78
+
79
+ Pure Ruby versus the native equivalents (`rbnacl`/libsodium, `blake3-rb`,
80
+ `chacha20blake3`). Ruby 4.0.2 with YJIT, on a single core.
81
+ Numbers are slowdown factors (smaller = closer to native);
82
+ run `bundle exec ruby --yjit bench/primitives.rb` to reproduce.
83
+
84
+ | Primitive | 256 B | 1 KB | 4 KB | 16 KB |
85
+ | ------------------------------- | -----: | -----: | -----: | -----: |
86
+ | BLAKE3 hash | 46× | 118× | 340× | 716× |
87
+ | Poly1305 MAC | 31× | 83× | 161× | 221× |
88
+ | SecretBox (XSalsa20-Poly1305) | 40× | 65× | 123× | 166× |
89
+ | Box (Curve25519-XSalsa20-Poly.) | 46× | 73× | 125× | 162× |
90
+ | ChaCha20-BLAKE3 AEAD | 108× | 159× | 242× | 323× |
91
+
92
+ Size-independent: Curve25519 scalar multiplication ~42× slower; full
93
+ keypair generation ~45× slower.
94
+
95
+ XSalsa20 and ChaCha20 stream ciphers have no direct `rbnacl` counterpart,
96
+ so no comparison is shown; nuckle runs them at roughly 30 k ops/s at 256 B
97
+ and 550 ops/s at 16 KB.
98
+
77
99
  ## Verification
78
100
 
79
- All primitives are tested against rbnacl/libsodium test vectors
80
- (NaCl distribution, RFC 7748, RFC 8439) and cross-validated to
81
- produce byte-identical output.
101
+ Known-answer tests: the NaCl distribution vectors (Box, SecretBox),
102
+ RFC 7748 section 5.2 (X25519, including 1,000 iterations), RFC 8439
103
+ (ChaCha20 block, Poly1305 section 2.5.2 and appendix A.3), the official
104
+ BLAKE3 `test_vectors.json` (all three modes, extended output) and the
105
+ upstream chacha20-blake3 vectors. When the gems are installed, the
106
+ tests also check for byte-identical output against rbnacl/libsodium,
107
+ `blake3-rb` and `chacha20blake3`.
82
108
 
83
109
  ## License
84
110
 
data/lib/nuckle/box.rb CHANGED
@@ -15,12 +15,15 @@ module Nuckle
15
15
 
16
16
  # @param public_key [PublicKey, String] peer's 32-byte public key
17
17
  # @param private_key [PrivateKey, String] own 32-byte private key
18
+ # @raise [CryptoError] if +public_key+ is a low-order point. RbNaCl
19
+ # raises the same error, but only on the first encrypt or decrypt.
20
+ # @raise [TypeError] if a key is the wrong type, e.g. the two are swapped
18
21
  def initialize(public_key, private_key)
19
- pk = extract_bytes(public_key, PUBLICKEYBYTES, "public key")
20
- sk = extract_bytes(private_key, PRIVATEKEYBYTES, "private key")
22
+ public_key = PublicKey.new(public_key) unless public_key.is_a?(PublicKey)
23
+ private_key = PrivateKey.new(private_key) unless private_key.is_a?(PrivateKey)
21
24
 
22
25
  # Compute shared secret via Curve25519 DH
23
- shared = Internals::Curve25519.scalarmult(sk, pk)
26
+ shared = private_key.diffie_hellman(public_key)
24
27
 
25
28
  # Derive symmetric key via HSalsa20 (the "beforenm" step)
26
29
  key = Internals::Salsa20.hsalsa20(shared, "\x00" * 16)
@@ -45,21 +48,5 @@ module Nuckle
45
48
 
46
49
  alias box encrypt
47
50
  alias open decrypt
48
-
49
- private
50
-
51
- def extract_bytes(key, expected_size, name)
52
- bytes = case key
53
- when PublicKey, PrivateKey
54
- key.to_s
55
- when String
56
- key.b
57
- else
58
- raise ArgumentError, "#{name} must be a Key or String"
59
- end
60
- raise ArgumentError, "#{name} must be #{expected_size} bytes (got #{bytes.bytesize})" unless bytes.bytesize == expected_size
61
-
62
- bytes
63
- end
64
51
  end
65
52
  end
@@ -25,7 +25,7 @@ module Nuckle
25
25
  # @param key [String] 32-byte master key
26
26
  def initialize(key)
27
27
  key = key.b
28
- raise ArgumentError, "key must be #{KEYBYTES} bytes (got #{key.bytesize})" unless key.bytesize == KEYBYTES
28
+ raise LengthError, "key must be #{KEYBYTES} bytes (got #{key.bytesize})" unless key.bytesize == KEYBYTES
29
29
 
30
30
  @key = key
31
31
  end
@@ -76,7 +76,7 @@ module Nuckle
76
76
  # @raise [CryptoError] on authentication failure
77
77
  def decrypt_detached(nonce, ciphertext, tag, aad: "")
78
78
  tag = tag.b
79
- raise ArgumentError, "tag must be #{TAGBYTES} bytes" unless tag.bytesize == TAGBYTES
79
+ raise LengthError, "tag must be #{TAGBYTES} bytes" unless tag.bytesize == TAGBYTES
80
80
 
81
81
  ciphertext = ciphertext.b
82
82
  enc_key, auth_key, enc_nonce = derive_subkeys(nonce)
@@ -86,11 +86,19 @@ module Nuckle
86
86
  Internals::Chacha20.xor(enc_key, enc_nonce, ciphertext)
87
87
  end
88
88
 
89
+
90
+ # Hides the key bytes, so they stay out of logs and error output.
91
+ #
92
+ # @return [String]
93
+ def inspect
94
+ "#<#{self.class}>"
95
+ end
96
+
89
97
  private
90
98
 
91
99
  def derive_subkeys(nonce)
92
100
  nonce = nonce.b
93
- raise ArgumentError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
101
+ raise LengthError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
94
102
 
95
103
  kdf = Internals::Blake3.new_keyed(@key).update(nonce).finalize_xof.read(72)
96
104
  [kdf.byteslice(0, 32), kdf.byteslice(32, 32), kdf.byteslice(64, 8)]
@@ -252,7 +252,12 @@ module Nuckle
252
252
  end
253
253
 
254
254
  # One-shot unkeyed hash.
255
- def hash(input, length = OUT_LEN)
255
+ #
256
+ # Called with no arguments, this is Object#hash, so the module still
257
+ # works as a Hash key.
258
+ def hash(input = (no_input = true), length = OUT_LEN)
259
+ return super() if no_input
260
+
256
261
  Hasher.new(IV, 0).update(input).finalize(length)
257
262
  end
258
263
 
@@ -267,7 +272,7 @@ module Nuckle
267
272
  end
268
273
 
269
274
  def new_keyed(key)
270
- raise ArgumentError, "key must be 32 bytes" unless key.bytesize == KEY_LEN
275
+ raise LengthError, "key must be 32 bytes" unless key.bytesize == KEY_LEN
271
276
 
272
277
  Hasher.new(key.b.unpack("V8"), KEYED_HASH)
273
278
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nuckle
4
+ # Raised when a key, nonce or tag has the wrong length. An ArgumentError,
5
+ # like RbNaCl::LengthError.
6
+ class LengthError < ArgumentError
7
+ end
8
+ end
@@ -15,13 +15,16 @@ module Nuckle
15
15
  end
16
16
 
17
17
 
18
- # @param key [String] 32-byte raw private key (binary)
18
+ # @param key [String, PrivateKey] 32-byte raw private key (binary)
19
+ # @raise [TypeError] if +key+ is neither a PrivateKey nor a String
19
20
  def initialize(key)
20
- key = key.to_s if key.respond_to?(:to_s) && !key.is_a?(String)
21
- key = key.b
22
- raise ArgumentError, "private key must be #{BYTES} bytes (got #{key.bytesize})" unless key.bytesize == BYTES
21
+ key = key.to_s if key.is_a?(PrivateKey)
22
+ raise TypeError, "private key must be a String or #{PrivateKey} (got #{key.class})" unless key.respond_to?(:to_str)
23
23
 
24
- @key = key
24
+ key = key.to_str.b
25
+ raise LengthError, "private key must be #{BYTES} bytes (got #{key.bytesize})" unless key.bytesize == BYTES
26
+
27
+ @key = key.freeze
25
28
  end
26
29
 
27
30
 
@@ -40,26 +43,34 @@ module Nuckle
40
43
  # derivation (no HSalsa20). Callers are responsible for deriving
41
44
  # symmetric keys from the result (e.g. via HKDF or BLAKE3-derive-key).
42
45
  #
46
+ # Like libsodium's crypto_scalarmult, it refuses low-order public keys:
47
+ # those make the shared secret all zero.
48
+ #
43
49
  # @param peer_public_key [PublicKey, String] peer's 32-byte public key
44
50
  # @return [String] 32-byte shared secret (binary)
51
+ # @raise [CryptoError] if the shared secret is all zero
52
+ # @raise [TypeError] if +peer_public_key+ is neither a PublicKey nor a String
45
53
  def diffie_hellman(peer_public_key)
46
- pk = case peer_public_key
47
- when PublicKey
48
- peer_public_key.to_s
49
- when String
50
- peer_public_key.b
51
- else
52
- raise ArgumentError, "peer_public_key must be a PublicKey or String"
53
- end
54
- raise ArgumentError, "peer public key must be 32 bytes" unless pk.bytesize == BYTES
55
-
56
- Internals::Curve25519.scalarmult(@key, pk)
54
+ peer_public_key = PublicKey.new(peer_public_key) unless peer_public_key.is_a?(PublicKey)
55
+
56
+ shared = Internals::Curve25519.scalarmult(@key, peer_public_key.to_s)
57
+ raise CryptoError, "low-order public key (all-zero shared secret)" if Util.zero?(shared)
58
+
59
+ shared
57
60
  end
58
61
 
59
62
 
60
- # @return [String] raw 32-byte key (binary)
63
+ # @return [String] raw 32-byte key (binary, frozen)
61
64
  def to_bytes = @key
62
- # @return [String] raw 32-byte key (binary)
65
+ # @return [String] raw 32-byte key (binary, frozen)
63
66
  def to_s = @key
67
+
68
+
69
+ # Hides the key bytes, so they stay out of logs and error output.
70
+ #
71
+ # @return [String]
72
+ def inspect
73
+ "#<#{self.class}>"
74
+ end
64
75
  end
65
76
  end
@@ -7,22 +7,25 @@ module Nuckle
7
7
  BYTES = 32
8
8
 
9
9
 
10
- # @param key [String] 32-byte raw public key (binary)
10
+ # @param key [String, PublicKey] 32-byte raw public key (binary)
11
+ # @raise [TypeError] if +key+ is neither a PublicKey nor a String
11
12
  def initialize(key)
12
- key = key.to_s if key.respond_to?(:to_s) && !key.is_a?(String)
13
- key = key.b
14
- raise ArgumentError, "public key must be #{BYTES} bytes (got #{key.bytesize})" unless key.bytesize == BYTES
13
+ key = key.to_s if key.is_a?(PublicKey)
14
+ raise TypeError, "public key must be a String or #{PublicKey} (got #{key.class})" unless key.respond_to?(:to_str)
15
15
 
16
- @key = key
16
+ key = key.to_str.b
17
+ raise LengthError, "public key must be #{BYTES} bytes (got #{key.bytesize})" unless key.bytesize == BYTES
18
+
19
+ @key = key.freeze
17
20
  end
18
21
 
19
22
 
20
- # @return [String] raw 32-byte key (binary)
23
+ # @return [String] raw 32-byte key (binary, frozen)
21
24
  def to_bytes = @key
22
- # @return [String] raw 32-byte key (binary)
25
+ # @return [String] raw 32-byte key (binary, frozen)
23
26
  def to_s = @key
24
27
 
25
- # Constant-time equality comparison.
28
+ # Equality without an early exit (see {Util.verify}).
26
29
  #
27
30
  # @param other [PublicKey] key to compare
28
31
  # @return [Boolean]
@@ -16,7 +16,7 @@ module Nuckle
16
16
  # @param key [String] 32-byte symmetric key (binary)
17
17
  def initialize(key)
18
18
  key = key.b
19
- raise ArgumentError, "key must be #{KEYBYTES} bytes (got #{key.bytesize})" unless key.bytesize == KEYBYTES
19
+ raise LengthError, "key must be #{KEYBYTES} bytes (got #{key.bytesize})" unless key.bytesize == KEYBYTES
20
20
 
21
21
  @key = key
22
22
  end
@@ -36,7 +36,7 @@ module Nuckle
36
36
  def encrypt(nonce, plaintext)
37
37
  nonce = nonce.b
38
38
  plaintext = plaintext.b
39
- raise ArgumentError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
39
+ raise LengthError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
40
40
 
41
41
  # Pad with 32 zero bytes
42
42
  padded = ("\x00" * ZEROBYTES + plaintext).b
@@ -63,7 +63,7 @@ module Nuckle
63
63
  def decrypt(nonce, ciphertext)
64
64
  nonce = nonce.b
65
65
  ciphertext = ciphertext.b
66
- raise ArgumentError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
66
+ raise LengthError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
67
67
  raise CryptoError, "ciphertext too short" if ciphertext.bytesize < MACBYTES
68
68
 
69
69
  mac = ciphertext.byteslice(0, MACBYTES)
@@ -86,5 +86,13 @@ module Nuckle
86
86
  # Aliases matching rbnacl API
87
87
  alias box encrypt
88
88
  alias open decrypt
89
+
90
+
91
+ # Hides the key bytes, so they stay out of logs and error output.
92
+ #
93
+ # @return [String]
94
+ def inspect
95
+ "#<#{self.class}>"
96
+ end
89
97
  end
90
98
  end
data/lib/nuckle/util.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nuckle
4
- # Constant-time comparison utilities for cryptographic data.
4
+ # Comparison utilities for cryptographic data. They have no early exit,
5
+ # but Ruby gives no constant-time guarantee (see SECURITY.md).
5
6
  module Util
6
7
  # Compare two 16-byte strings.
7
8
  def self.verify16(a, b)
@@ -21,18 +22,33 @@ module Nuckle
21
22
  end
22
23
 
23
24
 
24
- # Compares two binary strings for equality.
25
+ # Compares two strings byte by byte. XORs every byte pair and ORs the
26
+ # results, so unlike String#== it does not stop at the first
27
+ # difference. Lengths are not treated as secret.
25
28
  #
26
29
  # @param a [String] first string
27
30
  # @param b [String] second string
28
31
  # @param expected_size [Integer, nil] required byte length, or nil to skip check
29
32
  # @return [Boolean]
30
33
  def self.verify(a, b, expected_size = nil)
31
- a = a.b if a.encoding != Encoding::BINARY
32
- b = b.b if b.encoding != Encoding::BINARY
33
34
  return false if expected_size && (a.bytesize != expected_size || b.bytesize != expected_size)
35
+ return false unless a.bytesize == b.bytesize
34
36
 
35
- a == b
37
+ diff = 0
38
+ a.bytesize.times { |i| diff |= a.getbyte(i) ^ b.getbyte(i) }
39
+ diff.zero?
40
+ end
41
+
42
+
43
+ # Checks whether every byte is zero. ORs all bytes together, with no
44
+ # early exit.
45
+ #
46
+ # @param bytes [String]
47
+ # @return [Boolean]
48
+ def self.zero?(bytes)
49
+ acc = 0
50
+ bytes.each_byte { |byte| acc |= byte }
51
+ acc.zero?
36
52
  end
37
53
  end
38
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nuckle
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/nuckle.rb CHANGED
@@ -6,6 +6,7 @@
6
6
 
7
7
  require_relative "nuckle/version"
8
8
  require_relative "nuckle/crypto_error"
9
+ require_relative "nuckle/length_error"
9
10
  require_relative "nuckle/random"
10
11
  require_relative "nuckle/util"
11
12
  require_relative "nuckle/internals/curve25519"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nuckle
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger
@@ -28,6 +28,7 @@ files:
28
28
  - lib/nuckle/internals/curve25519.rb
29
29
  - lib/nuckle/internals/poly1305.rb
30
30
  - lib/nuckle/internals/salsa20.rb
31
+ - lib/nuckle/length_error.rb
31
32
  - lib/nuckle/private_key.rb
32
33
  - lib/nuckle/public_key.rb
33
34
  - lib/nuckle/random.rb
@@ -52,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
53
  - !ruby/object:Gem::Version
53
54
  version: '0'
54
55
  requirements: []
55
- rubygems_version: 4.0.6
56
+ rubygems_version: 4.0.20
56
57
  specification_version: 4
57
58
  summary: Pure Ruby NaCl crypto primitives
58
59
  test_files: []