nuckle 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: b6a034f12d9a0dd8326bfbc7595b593cd7a0089ab7fe0c8563c7d7eecce791e9
4
- data.tar.gz: 6161d94c79b134ffe0dd014dba44c65345910bde408ab5c618fccba456cc5356
3
+ metadata.gz: 545090d9c8b1c29a39666b5a6e036282a138a67ed06659aa9a3362e5cce147f7
4
+ data.tar.gz: ec96d6f19f26142af27e438e75b0441d0d81dfec8c1d80250114c213380d440a
5
5
  SHA512:
6
- metadata.gz: e513c94f45a9bf21e6a432c419868e8c5817bd82c7727debf402394dfd09d28fa488f8622ba69182e44d30e7262caa8b5ac6263e06ccddb6f7e96864da535063
7
- data.tar.gz: dc9dbc2a4f101c1bc842037b88f3db9dd765020c5e3d855f6ad4c45c26847aa79e278ebcb0a410f4a4a216fd551d063b37ea5cf432996b867c031e478e9b5a00
6
+ metadata.gz: 8c943b18a056cf8a44cd264db3960b863168e3ec7ca694e1f8d50e28cf05cc3c478fc677fca88b42df355c9986221f7f59b6841f8ada0872a80deb3badada865
7
+ data.tar.gz: 9527421844ba5513e96fa1e7ef69b230c550d0b049fc261622230d7a048e718b7f45aca2f19f08e25e6abb8f4b9b8f993468a3e0ab4992ee9014422796de83db
data/README.md CHANGED
@@ -28,9 +28,12 @@ through timing side channels. Use
28
28
  |-----------|-------------|
29
29
  | **Curve25519** | Elliptic-curve Diffie-Hellman (key agreement) |
30
30
  | **XSalsa20** | Extended-nonce stream cipher (includes HSalsa20, Salsa20) |
31
+ | **ChaCha20** | Stream cipher (DJB's 8-byte-nonce variant) |
31
32
  | **Poly1305** | One-time message authenticator |
33
+ | **BLAKE3** | Cryptographic hash — unkeyed, keyed, `derive_key`, XOF |
32
34
  | **Box** | Public-key authenticated encryption (Curve25519-XSalsa20-Poly1305) |
33
35
  | **SecretBox** | Symmetric authenticated encryption (XSalsa20-Poly1305) |
36
+ | **ChaCha20-BLAKE3** | Authenticated encryption AEAD, wire-compatible with [skerkour/chacha20-blake3](https://github.com/skerkour/chacha20-blake3) |
34
37
 
35
38
  ## Usage
36
39
 
@@ -0,0 +1,110 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nuckle
4
+ # ChaCha20-BLAKE3 authenticated encryption with associated data (AEAD).
5
+ #
6
+ # Wire-compatible with the `chacha20-blake3` Rust crate
7
+ # (https://github.com/skerkour/chacha20-blake3). Given a 32-byte master key
8
+ # and a 24-byte nonce:
9
+ #
10
+ # 1. BLAKE3-keyed(master_key) absorbing the nonce, then XOF-extracting 72
11
+ # bytes, splits into (encryption_key, authentication_key, chacha20_nonce)
12
+ # of sizes (32, 32, 8).
13
+ # 2. Encrypt plaintext with ChaCha20(encryption_key, chacha20_nonce).
14
+ # 3. Tag = BLAKE3-keyed(authentication_key) over
15
+ # aad || u64_le(aad.bytesize) || ciphertext || u64_le(ciphertext.bytesize).
16
+ # 4. Output is `ciphertext || tag`.
17
+ #
18
+ module Chacha20Blake3
19
+ KEYBYTES = 32
20
+ NONCEBYTES = 24
21
+ TAGBYTES = 32
22
+
23
+ # Authenticated encryption cipher.
24
+ class Cipher
25
+ # @param key [String] 32-byte master key
26
+ def initialize(key)
27
+ key = key.b
28
+ raise ArgumentError, "key must be #{KEYBYTES} bytes (got #{key.bytesize})" unless key.bytesize == KEYBYTES
29
+
30
+ @key = key
31
+ end
32
+
33
+ # Encrypt plaintext with the given 24-byte nonce.
34
+ #
35
+ # @param nonce [String] 24-byte nonce
36
+ # @param plaintext [String]
37
+ # @param aad [String] associated data (authenticated but not encrypted)
38
+ # @return [String] ciphertext || 32-byte tag
39
+ #
40
+ def encrypt(nonce, plaintext, aad: "")
41
+ enc_key, auth_key, enc_nonce = derive_subkeys(nonce)
42
+ ct = Internals::Chacha20.xor(enc_key, enc_nonce, plaintext)
43
+ tag = compute_tag(auth_key, aad, ct)
44
+ ct + tag
45
+ end
46
+
47
+ # Decrypt a ciphertext||tag blob with the given 24-byte nonce.
48
+ #
49
+ # @param nonce [String] 24-byte nonce
50
+ # @param ciphertext [String] ciphertext || 32-byte tag
51
+ # @param aad [String]
52
+ # @return [String] plaintext
53
+ # @raise [CryptoError] on authentication failure
54
+ #
55
+ def decrypt(nonce, ciphertext, aad: "")
56
+ ciphertext = ciphertext.b
57
+ raise CryptoError, "ciphertext too short" if ciphertext.bytesize < TAGBYTES
58
+
59
+ tag = ciphertext.byteslice(ciphertext.bytesize - TAGBYTES, TAGBYTES)
60
+ ct = ciphertext.byteslice(0, ciphertext.bytesize - TAGBYTES)
61
+ decrypt_detached(nonce, ct, tag, aad: aad)
62
+ end
63
+
64
+ # Encrypt, returning ciphertext and tag separately.
65
+ #
66
+ # @return [Array(String, String)] [ciphertext, tag]
67
+ def encrypt_detached(nonce, plaintext, aad: "")
68
+ enc_key, auth_key, enc_nonce = derive_subkeys(nonce)
69
+ ct = Internals::Chacha20.xor(enc_key, enc_nonce, plaintext)
70
+ tag = compute_tag(auth_key, aad, ct)
71
+ [ct, tag]
72
+ end
73
+
74
+ # Decrypt from a detached ciphertext + tag.
75
+ #
76
+ # @raise [CryptoError] on authentication failure
77
+ def decrypt_detached(nonce, ciphertext, tag, aad: "")
78
+ tag = tag.b
79
+ raise ArgumentError, "tag must be #{TAGBYTES} bytes" unless tag.bytesize == TAGBYTES
80
+
81
+ ciphertext = ciphertext.b
82
+ enc_key, auth_key, enc_nonce = derive_subkeys(nonce)
83
+ expected = compute_tag(auth_key, aad, ciphertext)
84
+ raise CryptoError, "decryption failed" unless Util.verify32(tag, expected)
85
+
86
+ Internals::Chacha20.xor(enc_key, enc_nonce, ciphertext)
87
+ end
88
+
89
+ private
90
+
91
+ def derive_subkeys(nonce)
92
+ nonce = nonce.b
93
+ raise ArgumentError, "nonce must be #{NONCEBYTES} bytes" unless nonce.bytesize == NONCEBYTES
94
+
95
+ kdf = Internals::Blake3.new_keyed(@key).update(nonce).finalize_xof.read(72)
96
+ [kdf.byteslice(0, 32), kdf.byteslice(32, 32), kdf.byteslice(64, 8)]
97
+ end
98
+
99
+ def compute_tag(auth_key, aad, ct)
100
+ aad = aad.b
101
+ Internals::Blake3.new_keyed(auth_key).
102
+ update(aad).
103
+ update([aad.bytesize].pack("Q<")).
104
+ update(ct).
105
+ update([ct.bytesize].pack("Q<")).
106
+ finalize(TAGBYTES)
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,286 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nuckle
4
+ module Internals
5
+ # BLAKE3 cryptographic hash (unkeyed, keyed, and derive_key modes) with XOF.
6
+ #
7
+ # References:
8
+ # - https://github.com/BLAKE3-team/BLAKE3-specs/blob/master/blake3.pdf
9
+ # - https://github.com/BLAKE3-team/BLAKE3 (reference impl)
10
+ #
11
+ module Blake3
12
+ MASK32 = 0xFFFFFFFF
13
+ OUT_LEN = 32
14
+ KEY_LEN = 32
15
+ BLOCK_LEN = 64
16
+ CHUNK_LEN = 1024
17
+
18
+ # Domain-separation flags (per-compression, not global).
19
+ CHUNK_START = 1
20
+ CHUNK_END = 2
21
+ PARENT = 4
22
+ ROOT = 8
23
+ KEYED_HASH = 16
24
+ DERIVE_KEY_CONTEXT = 32
25
+ DERIVE_KEY_MATERIAL = 64
26
+
27
+ # Same constants as SHA-256 IV.
28
+ IV = [
29
+ 0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
30
+ 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
31
+ ].freeze
32
+
33
+ # Message-word permutation applied between rounds.
34
+ MSG_PERMUTATION = [2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8].freeze
35
+
36
+ module_function
37
+
38
+ # Quarter-round (G) on a 16-word state.
39
+ def g(state, a, b, c, d, mx, my)
40
+ state[a] = (state[a] + state[b] + mx) & MASK32
41
+ t = state[d] ^ state[a]
42
+ state[d] = ((t >> 16) | (t << 16)) & MASK32
43
+ state[c] = (state[c] + state[d]) & MASK32
44
+ t = state[b] ^ state[c]
45
+ state[b] = ((t >> 12) | (t << 20)) & MASK32
46
+ state[a] = (state[a] + state[b] + my) & MASK32
47
+ t = state[d] ^ state[a]
48
+ state[d] = ((t >> 8) | (t << 24)) & MASK32
49
+ state[c] = (state[c] + state[d]) & MASK32
50
+ t = state[b] ^ state[c]
51
+ state[b] = ((t >> 7) | (t << 25)) & MASK32
52
+ end
53
+
54
+ def round(state, m)
55
+ # Columns
56
+ g(state, 0, 4, 8, 12, m[0], m[1])
57
+ g(state, 1, 5, 9, 13, m[2], m[3])
58
+ g(state, 2, 6, 10, 14, m[4], m[5])
59
+ g(state, 3, 7, 11, 15, m[6], m[7])
60
+ # Diagonals
61
+ g(state, 0, 5, 10, 15, m[8], m[9])
62
+ g(state, 1, 6, 11, 12, m[10], m[11])
63
+ g(state, 2, 7, 8, 13, m[12], m[13])
64
+ g(state, 3, 4, 9, 14, m[14], m[15])
65
+ end
66
+
67
+ def permute(m)
68
+ MSG_PERMUTATION.map { |i| m[i] }
69
+ end
70
+
71
+ # BLAKE3 compression function.
72
+ #
73
+ # @param cv [Array<Integer>] 8-word chaining value
74
+ # @param block_words [Array<Integer>] 16-word message block
75
+ # @param counter [Integer] 64-bit counter (chunk index, XOF block index, or 0 for parents)
76
+ # @param block_len [Integer] number of valid bytes in the block (1..64, or 0 for empty)
77
+ # @param flags [Integer] domain-separation flags
78
+ # @return [Array<Integer>] 16-word output (first 8 = new CV for non-XOF)
79
+ #
80
+ def compress(cv, block_words, counter, block_len, flags)
81
+ state = [
82
+ cv[0], cv[1], cv[2], cv[3],
83
+ cv[4], cv[5], cv[6], cv[7],
84
+ IV[0], IV[1], IV[2], IV[3],
85
+ counter & MASK32, (counter >> 32) & MASK32, block_len, flags
86
+ ]
87
+ m = block_words.dup
88
+
89
+ round(state, m)
90
+ 6.times do
91
+ m = permute(m)
92
+ round(state, m)
93
+ end
94
+
95
+ # Final xor: state[0..8] ^= state[8..16]; state[8..16] ^= cv[0..8]
96
+ 8.times do |i|
97
+ state[i] ^= state[i + 8]
98
+ state[i + 8] ^= cv[i]
99
+ end
100
+ state
101
+ end
102
+
103
+ # Parse a 64-byte string (or zero-padded short string) into 16 LE u32 words.
104
+ def words_from_block(block)
105
+ block = block + ("\x00".b * (BLOCK_LEN - block.bytesize)) if block.bytesize < BLOCK_LEN
106
+ block.unpack("V16")
107
+ end
108
+
109
+ # Deferred final compression. Supports variable-length output via XOF.
110
+ class Output
111
+ def initialize(input_cv, block_words, counter, block_len, flags)
112
+ @cv = input_cv
113
+ @block = block_words
114
+ @counter = counter
115
+ @block_len = block_len
116
+ @flags = flags
117
+ @xof_ctr = 0
118
+ @buffer = String.new(encoding: Encoding::BINARY)
119
+ end
120
+
121
+ # 8-word CV (non-root). Used when folding intermediate parents.
122
+ def chaining_value
123
+ Blake3.compress(@cv, @block, @counter, @block_len, @flags)[0, 8]
124
+ end
125
+
126
+ # Read the next n bytes from the root XOF stream.
127
+ def read(n)
128
+ out = String.new(capacity: n, encoding: Encoding::BINARY)
129
+ while out.bytesize < n
130
+ if @buffer.empty?
131
+ words = Blake3.compress(@cv, @block, @xof_ctr, @block_len, @flags | ROOT)
132
+ @buffer = words.pack("V16")
133
+ @xof_ctr += 1
134
+ end
135
+ take = [@buffer.bytesize, n - out.bytesize].min
136
+ out << @buffer.byteslice(0, take)
137
+ @buffer = @buffer.byteslice(take..) || "".b
138
+ end
139
+ out
140
+ end
141
+ end
142
+
143
+ # Streaming hasher. Supports update/finalize/finalize_xof.
144
+ class Hasher
145
+ # @param key_words [Array<Integer>] 8-word base key (IV or actual key)
146
+ # @param flags [Integer] base flags (0, KEYED_HASH, or DERIVE_KEY_MATERIAL)
147
+ def initialize(key_words, flags)
148
+ @key_words = key_words.freeze
149
+ @flags = flags
150
+ @cv_stack = []
151
+ @chunk_counter = 0
152
+ @chunk_cv = key_words.dup
153
+ @chunk_block = String.new(encoding: Encoding::BINARY)
154
+ @blocks_compressed = 0
155
+ end
156
+
157
+ # Feed more input. Returns self for chaining.
158
+ def update(input)
159
+ input = input.b
160
+ len = input.bytesize
161
+ offset = 0
162
+
163
+ while offset < len
164
+ if @chunk_block.bytesize == BLOCK_LEN
165
+ if @blocks_compressed < 15
166
+ flush_intermediate_block
167
+ else
168
+ flush_final_chunk_block
169
+ push_and_merge_chunk
170
+ start_new_chunk
171
+ end
172
+ end
173
+
174
+ need = BLOCK_LEN - @chunk_block.bytesize
175
+ take = [need, len - offset].min
176
+ @chunk_block << input.byteslice(offset, take)
177
+ offset += take
178
+ end
179
+ self
180
+ end
181
+
182
+ # Return `out_len` bytes of output (default 32).
183
+ def finalize(out_len = OUT_LEN)
184
+ build_root_output.read(out_len)
185
+ end
186
+
187
+ # Return an Output that can be read incrementally via `read(n)`.
188
+ def finalize_xof
189
+ build_root_output
190
+ end
191
+
192
+ private
193
+
194
+ def flush_intermediate_block
195
+ words = @chunk_block.unpack("V16")
196
+ flags = @flags
197
+ flags |= CHUNK_START if @blocks_compressed == 0
198
+ @chunk_cv = Blake3.compress(@chunk_cv, words, @chunk_counter, BLOCK_LEN, flags)[0, 8]
199
+ @chunk_block = String.new(encoding: Encoding::BINARY)
200
+ @blocks_compressed += 1
201
+ end
202
+
203
+ # Finalize the current (full-size, 16th) block of a chunk and produce its CV.
204
+ def flush_final_chunk_block
205
+ flags = @flags | CHUNK_END
206
+ flags |= CHUNK_START if @blocks_compressed == 0
207
+ words = @chunk_block.unpack("V16")
208
+ @chunk_cv = Blake3.compress(@chunk_cv, words, @chunk_counter, BLOCK_LEN, flags)[0, 8]
209
+ end
210
+
211
+ def push_and_merge_chunk
212
+ completed = @chunk_counter
213
+ @cv_stack.push(@chunk_cv)
214
+ total = completed + 1
215
+ while (total & 1).zero?
216
+ right = @cv_stack.pop
217
+ left = @cv_stack.pop
218
+ block = left + right
219
+ @cv_stack.push(Blake3.compress(@key_words, block, 0, BLOCK_LEN, @flags | PARENT)[0, 8])
220
+ total >>= 1
221
+ end
222
+ end
223
+
224
+ def start_new_chunk
225
+ @chunk_counter += 1
226
+ @chunk_cv = @key_words.dup
227
+ @chunk_block = String.new(encoding: Encoding::BINARY)
228
+ @blocks_compressed = 0
229
+ end
230
+
231
+ # Build the final (deferred) Output — the last compression is not
232
+ # actually performed until root_output#read / chaining_value.
233
+ def build_root_output
234
+ # Current chunk (possibly partial) becomes the right-most subtree.
235
+ flags = @flags | CHUNK_END
236
+ flags |= CHUNK_START if @blocks_compressed == 0
237
+ block_len = @chunk_block.bytesize
238
+ padded = block_len == BLOCK_LEN ? @chunk_block : @chunk_block + ("\x00".b * (BLOCK_LEN - block_len))
239
+ block_words = padded.unpack("V16")
240
+
241
+ output = Output.new(@chunk_cv, block_words, @chunk_counter, block_len, flags)
242
+
243
+ # Fold stack right-to-left; each fold creates a new PARENT Output.
244
+ # The last one (leftmost stack entry) is the root — ROOT flag is
245
+ # applied inside Output#read.
246
+ @cv_stack.reverse_each do |left_cv|
247
+ right_cv = output.chaining_value
248
+ output = Output.new(@key_words, left_cv + right_cv, 0, BLOCK_LEN, @flags | PARENT)
249
+ end
250
+ output
251
+ end
252
+ end
253
+
254
+ # One-shot unkeyed hash.
255
+ def hash(input, length = OUT_LEN)
256
+ Hasher.new(IV, 0).update(input).finalize(length)
257
+ end
258
+
259
+ # Build a Hasher for the unkeyed mode (for streaming use).
260
+ def new_hasher
261
+ Hasher.new(IV, 0)
262
+ end
263
+
264
+ # One-shot keyed hash (32-byte key).
265
+ def keyed_hash(key, input, length = OUT_LEN)
266
+ new_keyed(key).update(input).finalize(length)
267
+ end
268
+
269
+ def new_keyed(key)
270
+ raise ArgumentError, "key must be 32 bytes" unless key.bytesize == KEY_LEN
271
+
272
+ Hasher.new(key.b.unpack("V8"), KEYED_HASH)
273
+ end
274
+
275
+ # Derive a subkey from a context string and key material.
276
+ def derive_key(context, material, length = OUT_LEN)
277
+ new_derive_key(context).update(material).finalize(length)
278
+ end
279
+
280
+ def new_derive_key(context)
281
+ ctx_key_bytes = Hasher.new(IV, DERIVE_KEY_CONTEXT).update(context).finalize(KEY_LEN)
282
+ Hasher.new(ctx_key_bytes.unpack("V8"), DERIVE_KEY_MATERIAL)
283
+ end
284
+ end
285
+ end
286
+ end
@@ -0,0 +1,154 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nuckle
4
+ module Internals
5
+ # ChaCha20 stream cipher (DJB's original 8-byte-nonce variant).
6
+ #
7
+ # State layout (16 x 32-bit little-endian words):
8
+ # [ 0.. 3] "expand 32-byte k" constants
9
+ # [ 4..11] 32-byte key
10
+ # [12..13] 64-bit block counter (low word first)
11
+ # [14..15] 8-byte nonce
12
+ #
13
+ # Quarter-round rotations: 16, 12, 8, 7.
14
+ #
15
+ # References:
16
+ # - https://cr.yp.to/chacha/chacha-20080128.pdf (ChaCha family)
17
+ # - RFC 8439 uses the 12-byte-nonce variant; this module implements the
18
+ # 8-byte-nonce variant required by ChaCha20-BLAKE3.
19
+ #
20
+ module Chacha20
21
+ MASK32 = 0xFFFFFFFF
22
+ SIGMA = "expand 32-byte k".b.freeze
23
+ SIGMA_V4 = SIGMA.unpack("V4").freeze
24
+
25
+ module_function
26
+
27
+ # ChaCha20 block function: 20 rounds on 16 x 32-bit words.
28
+ #
29
+ # @param key [String] 32-byte key
30
+ # @param nonce [String] 8-byte nonce
31
+ # @param counter [Integer] 64-bit block counter
32
+ # @return [String] 64-byte keystream block
33
+ #
34
+ def block(key, nonce, counter)
35
+ k0, k1, k2, k3, k4, k5, k6, k7 = key.unpack("V8")
36
+ n0, n1 = nonce.unpack("V2")
37
+ s0, s1, s2, s3 = SIGMA_V4
38
+ c0 = counter & MASK32
39
+ c1 = (counter >> 32) & MASK32
40
+
41
+ x0 = s0; x1 = s1; x2 = s2; x3 = s3
42
+ x4 = k0; x5 = k1; x6 = k2; x7 = k3
43
+ x8 = k4; x9 = k5; x10 = k6; x11 = k7
44
+ x12 = c0; x13 = c1; x14 = n0; x15 = n1
45
+
46
+ z0 = x0; z1 = x1; z2 = x2; z3 = x3
47
+ z4 = x4; z5 = x5; z6 = x6; z7 = x7
48
+ z8 = x8; z9 = x9; z10 = x10; z11 = x11
49
+ z12 = x12; z13 = x13; z14 = x14; z15 = x15
50
+
51
+ 10.times do
52
+ # Column round: QR(0,4,8,12), QR(1,5,9,13), QR(2,6,10,14), QR(3,7,11,15)
53
+ z0 = (z0 + z4) & MASK32; z12 ^= z0; z12 = ((z12 << 16) | (z12 >> 16)) & MASK32
54
+ z8 = (z8 + z12) & MASK32; z4 ^= z8; z4 = ((z4 << 12) | (z4 >> 20)) & MASK32
55
+ z0 = (z0 + z4) & MASK32; z12 ^= z0; z12 = ((z12 << 8) | (z12 >> 24)) & MASK32
56
+ z8 = (z8 + z12) & MASK32; z4 ^= z8; z4 = ((z4 << 7) | (z4 >> 25)) & MASK32
57
+
58
+ z1 = (z1 + z5) & MASK32; z13 ^= z1; z13 = ((z13 << 16) | (z13 >> 16)) & MASK32
59
+ z9 = (z9 + z13) & MASK32; z5 ^= z9; z5 = ((z5 << 12) | (z5 >> 20)) & MASK32
60
+ z1 = (z1 + z5) & MASK32; z13 ^= z1; z13 = ((z13 << 8) | (z13 >> 24)) & MASK32
61
+ z9 = (z9 + z13) & MASK32; z5 ^= z9; z5 = ((z5 << 7) | (z5 >> 25)) & MASK32
62
+
63
+ z2 = (z2 + z6) & MASK32; z14 ^= z2; z14 = ((z14 << 16) | (z14 >> 16)) & MASK32
64
+ z10 = (z10 + z14) & MASK32; z6 ^= z10; z6 = ((z6 << 12) | (z6 >> 20)) & MASK32
65
+ z2 = (z2 + z6) & MASK32; z14 ^= z2; z14 = ((z14 << 8) | (z14 >> 24)) & MASK32
66
+ z10 = (z10 + z14) & MASK32; z6 ^= z10; z6 = ((z6 << 7) | (z6 >> 25)) & MASK32
67
+
68
+ z3 = (z3 + z7) & MASK32; z15 ^= z3; z15 = ((z15 << 16) | (z15 >> 16)) & MASK32
69
+ z11 = (z11 + z15) & MASK32; z7 ^= z11; z7 = ((z7 << 12) | (z7 >> 20)) & MASK32
70
+ z3 = (z3 + z7) & MASK32; z15 ^= z3; z15 = ((z15 << 8) | (z15 >> 24)) & MASK32
71
+ z11 = (z11 + z15) & MASK32; z7 ^= z11; z7 = ((z7 << 7) | (z7 >> 25)) & MASK32
72
+
73
+ # Diagonal round: QR(0,5,10,15), QR(1,6,11,12), QR(2,7,8,13), QR(3,4,9,14)
74
+ z0 = (z0 + z5) & MASK32; z15 ^= z0; z15 = ((z15 << 16) | (z15 >> 16)) & MASK32
75
+ z10 = (z10 + z15) & MASK32; z5 ^= z10; z5 = ((z5 << 12) | (z5 >> 20)) & MASK32
76
+ z0 = (z0 + z5) & MASK32; z15 ^= z0; z15 = ((z15 << 8) | (z15 >> 24)) & MASK32
77
+ z10 = (z10 + z15) & MASK32; z5 ^= z10; z5 = ((z5 << 7) | (z5 >> 25)) & MASK32
78
+
79
+ z1 = (z1 + z6) & MASK32; z12 ^= z1; z12 = ((z12 << 16) | (z12 >> 16)) & MASK32
80
+ z11 = (z11 + z12) & MASK32; z6 ^= z11; z6 = ((z6 << 12) | (z6 >> 20)) & MASK32
81
+ z1 = (z1 + z6) & MASK32; z12 ^= z1; z12 = ((z12 << 8) | (z12 >> 24)) & MASK32
82
+ z11 = (z11 + z12) & MASK32; z6 ^= z11; z6 = ((z6 << 7) | (z6 >> 25)) & MASK32
83
+
84
+ z2 = (z2 + z7) & MASK32; z13 ^= z2; z13 = ((z13 << 16) | (z13 >> 16)) & MASK32
85
+ z8 = (z8 + z13) & MASK32; z7 ^= z8; z7 = ((z7 << 12) | (z7 >> 20)) & MASK32
86
+ z2 = (z2 + z7) & MASK32; z13 ^= z2; z13 = ((z13 << 8) | (z13 >> 24)) & MASK32
87
+ z8 = (z8 + z13) & MASK32; z7 ^= z8; z7 = ((z7 << 7) | (z7 >> 25)) & MASK32
88
+
89
+ z3 = (z3 + z4) & MASK32; z14 ^= z3; z14 = ((z14 << 16) | (z14 >> 16)) & MASK32
90
+ z9 = (z9 + z14) & MASK32; z4 ^= z9; z4 = ((z4 << 12) | (z4 >> 20)) & MASK32
91
+ z3 = (z3 + z4) & MASK32; z14 ^= z3; z14 = ((z14 << 8) | (z14 >> 24)) & MASK32
92
+ z9 = (z9 + z14) & MASK32; z4 ^= z9; z4 = ((z4 << 7) | (z4 >> 25)) & MASK32
93
+ end
94
+
95
+ [
96
+ (z0 + x0) & MASK32, (z1 + x1) & MASK32, (z2 + x2) & MASK32, (z3 + x3) & MASK32,
97
+ (z4 + x4) & MASK32, (z5 + x5) & MASK32, (z6 + x6) & MASK32, (z7 + x7) & MASK32,
98
+ (z8 + x8) & MASK32, (z9 + x9) & MASK32, (z10 + x10) & MASK32, (z11 + x11) & MASK32,
99
+ (z12 + x12) & MASK32, (z13 + x13) & MASK32, (z14 + x14) & MASK32, (z15 + x15) & MASK32,
100
+ ].pack("V16")
101
+ end
102
+
103
+ # XOR message with ChaCha20 keystream.
104
+ #
105
+ # @param key [String] 32-byte key
106
+ # @param nonce [String] 8-byte nonce
107
+ # @param message [String] plaintext/ciphertext
108
+ # @param counter [Integer] initial 64-bit block counter (default 0)
109
+ # @return [String] XOR'd output (same length as message)
110
+ #
111
+ def xor(key, nonce, message, counter = 0)
112
+ msg = message.b
113
+ len = msg.bytesize
114
+ out = String.new(capacity: len, encoding: Encoding::BINARY)
115
+ offset = 0
116
+
117
+ while offset < len
118
+ ks = block(key, nonce, counter)
119
+ remaining = len - offset
120
+ take = remaining < 64 ? remaining : 64
121
+
122
+ if take == 64
123
+ m = msg.byteslice(offset, 64).unpack("Q<8")
124
+ b = ks.unpack("Q<8")
125
+ out << [m[0] ^ b[0], m[1] ^ b[1], m[2] ^ b[2], m[3] ^ b[3],
126
+ m[4] ^ b[4], m[5] ^ b[5], m[6] ^ b[6], m[7] ^ b[7]].pack("Q<8")
127
+ else
128
+ m = msg.byteslice(offset, take).unpack("C*")
129
+ b = ks.unpack("C*")
130
+ take.times { |i| m[i] ^= b[i] }
131
+ out << m.pack("C*")
132
+ end
133
+
134
+ offset += take
135
+ counter += 1
136
+ end
137
+
138
+ out
139
+ end
140
+
141
+ # Generate ChaCha20 keystream bytes (equivalent to XOR against zeros).
142
+ #
143
+ # @param key [String] 32-byte key
144
+ # @param nonce [String] 8-byte nonce
145
+ # @param length [Integer] number of bytes to generate
146
+ # @param counter [Integer] initial 64-bit block counter (default 0)
147
+ # @return [String] keystream bytes
148
+ #
149
+ def stream(key, nonce, length, counter = 0)
150
+ xor(key, nonce, ("\x00".b * length), counter)
151
+ end
152
+ end
153
+ end
154
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nuckle
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
data/lib/nuckle.rb CHANGED
@@ -10,8 +10,11 @@ require_relative "nuckle/random"
10
10
  require_relative "nuckle/util"
11
11
  require_relative "nuckle/internals/curve25519"
12
12
  require_relative "nuckle/internals/salsa20"
13
+ require_relative "nuckle/internals/chacha20"
13
14
  require_relative "nuckle/internals/poly1305"
15
+ require_relative "nuckle/internals/blake3"
14
16
  require_relative "nuckle/public_key"
15
17
  require_relative "nuckle/private_key"
16
18
  require_relative "nuckle/box"
17
19
  require_relative "nuckle/secret_box"
20
+ require_relative "nuckle/chacha20_blake3"
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.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrik Wenger
@@ -21,7 +21,10 @@ files:
21
21
  - README.md
22
22
  - lib/nuckle.rb
23
23
  - lib/nuckle/box.rb
24
+ - lib/nuckle/chacha20_blake3.rb
24
25
  - lib/nuckle/crypto_error.rb
26
+ - lib/nuckle/internals/blake3.rb
27
+ - lib/nuckle/internals/chacha20.rb
25
28
  - lib/nuckle/internals/curve25519.rb
26
29
  - lib/nuckle/internals/poly1305.rb
27
30
  - lib/nuckle/internals/salsa20.rb