Commit43ab8924Recorded8 Sep 2026Repositorysigil-noise

Implement transport-independent Noise XX sessions

Changed
 .gitignore                   |   2 ++
 LICENSE                      |  28 +++++++++++++++++
 README.md                    |  96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 THIRD-PARTY-NOTICES          |  22 +++++++++++++
 dev-redirects.sgl            |   6 ++++
 package.sgl                  |  15 +++++++++
 sigil.lock                   |  42 +++++++++++++++++++++++++
 src/sigil/noise.sgl          |  47 ++++++++++++++++++++++++++++
 src/sigil/noise/internal.sgl | 248 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 test/test-noise.sgl          | 281 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 787 insertions(+)
Diff
.gitignoreadded
@@ -0,0 +1,2 @@
+1
/build/
+2
/.sigil/
LICENSEadded
@@ -0,0 +1,28 @@
+1
BSD 3-Clause License
+2
+3
Copyright (c) 2025 David Wilson
+4
+5
Redistribution and use in source and binary forms, with or without
+6
modification, are permitted provided that the following conditions are met:
+7
+8
1. Redistributions of source code must retain the above copyright notice, this
+9
list of conditions and the following disclaimer.
+10
+11
2. Redistributions in binary form must reproduce the above copyright notice,
+12
this list of conditions and the following disclaimer in the documentation
+13
and/or other materials provided with the distribution.
+14
+15
3. Neither the name of the copyright holder nor the names of its
+16
contributors may be used to endorse or promote products derived from
+17
this software without specific prior written permission.
+18
+19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+22
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+23
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+24
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+25
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+26
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+27
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+28
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
README.mdadded
@@ -0,0 +1,96 @@
+1
# sigil-noise
+2
+3
Transport-independent [Noise XX](https://noiseprotocol.org/noise.html) sessions
+4
for Sigil, using `Noise_XX_25519_ChaChaPoly_SHA256`. The primitives come from
+5
`sigil-crypto` and its existing mbedTLS dependency.
+6
+7
This is an experimental implementation. It is not yet integrated with Slate's
+8
node transports. Native conformance tests do not establish browser/WASM entropy
+9
safety or authenticate a Noise key as an existing Slate node identity.
+10
+11
## Build and test
+12
+13
Use Sigil 0.22 and the sibling `sigil-crypto-noise-work` checkout containing the
+14
X25519 and ChaCha20-Poly1305 bindings. Until those bindings are released, the
+15
local redirect is required:
+16
+17
```sh
+18
sigil deps install --redirects dev-redirects.sgl
+19
sigil test --sgl --redirects dev-redirects.sgl
+20
```
+21
+22
Tests cover RFC 7748 and RFC 8439 in `sigil-crypto`, Noise-C handshake and
+23
transport vectors here, and rejection of tampering, replay, invalid state
+24
transitions, oversized messages, and the reserved final nonce.
+25
+26
## API
+27
+28
```scheme
+29
(import (sigil crypto)
+30
(sigil noise))
+31
+32
;; Provision/persist static keys in the application. Never reuse an ephemeral.
+33
(define i (make-noise-handshake 'initiator (random-bytes 32) #u8()))
+34
(define r (make-noise-handshake 'responder (random-bytes 32) #u8()))
+35
+36
(noise-read-message! r (noise-write-message! i #u8()))
+37
(noise-read-message! i (noise-write-message! r #u8()))
+38
(noise-read-message! r (noise-write-message! i #u8()))
+39
+40
;; Both peers now report completion. In a real application, authenticate
+41
;; noise-peer-static-key against a trusted identity here; handshake completion
+42
;; alone does not establish who the peer is. The handshake hash is available
+43
;; for binding application authentication to this particular session.
+44
(define send-side (noise-split! i))
+45
(define receive-side (noise-split! r))
+46
+47
(noise-decrypt! receive-side (noise-encrypt! send-side #u8(1 2 3)))
+48
;; => #u8(1 2 3)
+49
+50
(noise-close! send-side)
+51
(noise-close! receive-side)
+52
```
+53
+54
`noise-write-message!` returns one complete handshake message and
+55
`noise-read-message!` returns its payload. All payloads, prologues, keys and
+56
messages are bytevectors. `noise-handshake-complete?` becomes true after the
+57
third message. `noise-peer-static-key` and `noise-handshake-hash` return copies
+58
and are available only after completion. `noise-split!` works once and returns
+59
a transport owning independent send/receive keys and counters.
+60
+61
Handshake payloads must not trigger trusted application actions before peer
+62
authentication. In particular, the first XX message and its payload are sent in
+63
cleartext. XX does not authenticate an identity without an application trust
+64
check. Applications must also choose an unambiguous protocol-specific prologue.
+65
+66
Provide reliable, ordered message delivery and framing outside this library.
+67
Noise messages are at most 65,535 bytes; transport plaintext is at most 65,519
+68
bytes. Larger application frames need a bounded fragmentation protocol. Wire
+69
headers outside the encrypted record are not authenticated by this API; put
+70
security-relevant metadata inside the plaintext or bind it through the prologue.
+71
Do not share a session concurrently between threads, copy/serialize its state,
+72
or retry a write after losing the returned message. Reconnect with a fresh
+73
handshake if ordering or delivery is uncertain.
+74
+75
Operations raise on invalid input, incorrect phase, or authentication failure.
+76
A handshake/transport operation failure closes that session. Closing is
+77
idempotent. Closing a handshake after splitting does not close its separately
+78
owned transport. The caller must close each owner it retains.
+79
+80
Only XX with this exact algorithm suite is supported. No negotiation, PSKs,
+81
fallback, 0-RTT, rekey API, or socket integration is provided. Internal
+82
constructors accept fixed ephemerals/nonces solely for conformance testing;
+83
applications must use the public module.
+84
+85
## Key lifetime
+86
+87
Session state is held in closures so ordinary printing does not expose keys.
+88
Owned buffers are overwritten when replaced or closed, and handshake private
+89
keys are dropped on completion. Native scratch buffers are zeroized before
+90
being freed. Sigil's garbage collector may copy managed bytevectors, and HKDF
+91
creates managed intermediates: this does **not** guarantee secure erasure of
+92
all key material. Hardened native secret storage is a separate prerequisite
+93
for applications requiring that guarantee. Browser/WASM secure entropy must be
+94
validated before enabling this implementation there.
+95
+96
The test vector extracted from Noise-C is attributed in THIRD-PARTY-NOTICES.
THIRD-PARTY-NOTICESadded
@@ -0,0 +1,22 @@
+1
Noise-C test vector used in test/test-noise.sgl
+2
https://github.com/rweather/noise-c/blob/master/tests/vector/noise-c-basic.txt
+3
+4
Copyright (C) 2016 Southern Storm Software, Pty Ltd.
+5
+6
Permission is hereby granted, free of charge, to any person obtaining a
+7
copy of this software and associated documentation files (the "Software"),
+8
to deal in the Software without restriction, including without limitation
+9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
+10
and/or sell copies of the Software, and to permit persons to whom the
+11
Software is furnished to do so, subject to the following conditions:
+12
+13
The above copyright notice and this permission notice shall be included
+14
in all copies or substantial portions of the Software.
+15
+16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+17
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+19
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+21
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+22
DEALINGS IN THE SOFTWARE.
dev-redirects.sgladded
@@ -0,0 +1,6 @@
+1
;; Required until the Noise primitives are released in sigil-crypto.
+2
(redirects
+3
repos: (list
+4
(for-repo
+5
url: "codeberg:sigil/sigil-crypto"
+6
use: (from-path dir: "../sigil-crypto-noise-work"))))
package.sgladded
@@ -0,0 +1,15 @@
+1
(package
+2
name: "sigil-noise"
+3
version: "0.1.0"
+4
sigil: "0.22"
+5
description: "Transport-independent Noise XX sessions for Sigil"
+6
license: "BSD-3-Clause"
+7
authors: (list "David Wilson <david@daviwil.com>")
+8
+9
dependencies: (list
+10
(from-git url: "codeberg:sigil/sigil-crypto" version: "^0.16.6")
+11
(from-git url: "codeberg:sigil/sigil" package: "sigil-lib" version: "^0.22"))
+12
+13
dev-dependencies: (list
+14
(from-git url: "codeberg:sigil/sigil" package: "sigil-test" version: "^0.22")
+15
(from-git url: "codeberg:sigil/sigil" package: "sigil-test-runner" version: "^0.22")))
sigil.lockadded
@@ -0,0 +1,42 @@
+1
;; Auto-generated by sigil deps install. Do not edit.
+2
(lock
+3
(package name: "sigil-stdlib"
+4
url: "codeberg:sigil/sigil"
+5
ref: ">=0.22, <1.0"
+6
sha: "6cab4724e4512cb5ff9339ffe6964ed286759d8c"
+7
package-selector: "sigil-stdlib"
+8
version: "0.22.0")
+9
(package name: "sigil-test"
+10
url: "codeberg:sigil/sigil"
+11
ref: "^0.22"
+12
sha: "6cab4724e4512cb5ff9339ffe6964ed286759d8c"
+13
package-selector: "sigil-test"
+14
version: "0.22.0")
+15
(package name: "sigil-test-runner"
+16
url: "codeberg:sigil/sigil"
+17
ref: "^0.22"
+18
sha: "6cab4724e4512cb5ff9339ffe6964ed286759d8c"
+19
package-selector: "sigil-test-runner"
+20
version: "0.22.0")
+21
(package name: "sigil-lib"
+22
url: "codeberg:sigil/sigil"
+23
ref: "^0.22"
+24
sha: "6cab4724e4512cb5ff9339ffe6964ed286759d8c"
+25
package-selector: "sigil-lib"
+26
version: "0.22.0")
+27
(package name: "sigil-json"
+28
url: "codeberg:sigil/sigil-json"
+29
ref: "^0.16.0"
+30
sha: "55c9e74712b5f79b45d41c65cfe0aef2c8f7e155"
+31
version: "0.16.0")
+32
(package name: "sigil-crypto"
+33
url: "codeberg:sigil/sigil-crypto"
+34
ref: "^0.16.0"
+35
sha: "71362137db0596e0695791b32c4f6e8faf11b39e"
+36
version: "0.16.6")
+37
(package name: "sigil-sqlite"
+38
url: "codeberg:sigil/sigil-sqlite"
+39
ref: "^0.16.0"
+40
sha: "569ef1063dda8f5989968644af31fc09c64b4bfe"
+41
version: "0.16.3")
+42
)
src/sigil/noise.sgladded
@@ -0,0 +1,47 @@
+1
;;; Transport-independent, ordered Noise XX sessions.
+2
(define-library (sigil noise)
+3
(export make-noise-handshake
+4
noise-write-message! noise-read-message! noise-handshake-complete?
+5
noise-peer-static-key noise-handshake-hash noise-split!
+6
noise-encrypt! noise-decrypt! noise-close!)
+7
(import (sigil crypto)
+8
(sigil noise internal))
+9
(begin
+10
;;; role is 'initiator or 'responder; static-private is 32 X25519 bytes;
+11
;;; prologue is a bytevector shared by both participants. Each call
+12
;;; generates a fresh ephemeral private key using sigil-crypto's RNG.
+13
(define (make-noise-handshake role static-private prologue)
+14
(unless (memq role '(initiator responder)) (error "noise: invalid role"))
+15
(let ((ephemeral (random-bytes 32)))
+16
(dynamic-wind
+17
(lambda () #f)
+18
(lambda ()
+19
(make-noise-handshake/internal (eq? role 'initiator) static-private prologue ephemeral))
+20
(lambda ()
+21
(do ((i 0 (+ i 1))) ((= i 32))
+22
(bytevector-u8-set! ephemeral i 0))))))
+23
+24
(define (noise-write-message! handshake payload)
+25
(handshake 'write payload))
+26
(define (noise-read-message! handshake message)
+27
(handshake 'read message))
+28
(define (noise-handshake-complete? handshake)
+29
(handshake 'complete? #f))
+30
;;; Available only after completion. Possession of this key is proven;
+31
;;; its association with an identity must be checked by the application.
+32
(define (noise-peer-static-key handshake)
+33
(handshake 'peer-static #f))
+34
(define (noise-handshake-hash handshake)
+35
(handshake 'handshake-hash #f))
+36
;;; Returns one transport with independent sending and receiving keys.
+37
;;; The handshake can be split only once. Authenticate the peer before
+38
;;; allowing application traffic through this transport.
+39
(define (noise-split! handshake)
+40
(handshake 'split #f))
+41
(define (noise-encrypt! transport plaintext)
+42
(transport 'encrypt plaintext))
+43
(define (noise-decrypt! transport ciphertext)
+44
(transport 'decrypt ciphertext))
+45
;;; Idempotent. A split transport is owned separately from its handshake.
+46
(define (noise-close! session)
+47
(session 'close #f))))
src/sigil/noise/internal.sgladded
@@ -0,0 +1,248 @@
+1
;;; Implementation of Noise revision 34, XX / 25519 / ChaChaPoly / SHA256.
+2
;;; This module's deterministic constructors are for conformance tests.
+3
;;; Applications must use (sigil noise), which generates fresh ephemerals.
+4
(define-library (sigil noise internal)
+5
(export make-noise-handshake/internal make-noise-cipher/internal)
+6
(import (sigil crypto)
+7
(sigil io)
+8
(sigil math))
+9
(begin
+10
(define protocol-name "Noise_XX_25519_ChaChaPoly_SHA256")
+11
+12
(define (require-bytes bytes size)
+13
(unless (and (bytevector? bytes) (= (bytevector-length bytes) size))
+14
(error "noise: invalid key or nonce length")))
+15
+16
(define (wipe! bytes)
+17
(when (bytevector? bytes)
+18
(do ((i 0 (+ i 1))) ((= i (bytevector-length bytes)))
+19
(bytevector-u8-set! bytes i 0))))
+20
+21
(define (require-message bytes minimum maximum)
+22
(unless (and (bytevector? bytes)
+23
(<= minimum (bytevector-length bytes) maximum))
+24
(error "noise: invalid message length")))
+25
+26
;; The nonce is bytes, not a Sigil integer: every bit of the 64-bit
+27
;; counter must remain exact on native and WASM runtimes alike.
+28
(define (make-noise-cipher/internal key initial-nonce)
+29
(require-bytes key 32)
+30
(require-bytes initial-nonce 12)
+31
(do ((i 0 (+ i 1))) ((= i 4))
+32
(unless (= 0 (bytevector-u8-ref initial-nonce i))
+33
(error "noise: nonce prefix must be zero")))
+34
(let ((k (bytevector-copy key))
+35
(nonce (bytevector-copy initial-nonce))
+36
(closed? #f))
+37
(define (close!)
+38
(wipe! k)
+39
(set! k #f)
+40
(set! closed? #t))
+41
(define (exhausted?)
+42
(let loop ((i 4))
+43
(or (= i 12)
+44
(and (= 255 (bytevector-u8-ref nonce i)) (loop (+ i 1))))))
+45
(define (advance!)
+46
(let loop ((i 4))
+47
(let ((n (+ 1 (bytevector-u8-ref nonce i))))
+48
(bytevector-u8-set! nonce i (modulo n 256))
+49
(when (= n 256) (loop (+ i 1))))))
+50
(lambda (operation aad data)
+51
(if (eq? operation 'close)
+52
(close!)
+53
(begin
+54
(when closed? (error "noise: cipher is closed"))
+55
(when (exhausted?)
+56
(close!)
+57
(error "noise: nonce exhausted"))
+58
(let ((result
+59
(case operation
+60
((encrypt) (chacha20-poly1305-encrypt k nonce aad data))
+61
((decrypt) (chacha20-poly1305-decrypt k nonce aad data))
+62
(else (error "noise: unknown cipher operation")))))
+63
;; Authentication failure does not consume a nonce. The
+64
;; owning handshake/transport closes the whole session.
+65
(unless result (error "noise: authentication or cipher failure"))
+66
(advance!)
+67
result))))))
+68
+69
(define (make-transport send receive)
+70
(let ((closed? #f))
+71
(define (close!)
+72
(send 'close #f #f)
+73
(receive 'close #f #f)
+74
(set! closed? #t))
+75
(lambda (operation data)
+76
(if (eq? operation 'close)
+77
(close!)
+78
(begin
+79
(when closed? (error "noise: transport is closed"))
+80
(guard (ex (else (close!) (raise ex)))
+81
(case operation
+82
((encrypt)
+83
(require-message data 0 65519)
+84
(send 'encrypt #u8() data))
+85
((decrypt)
+86
(require-message data 16 65535)
+87
(receive 'decrypt #u8() data))
+88
(else (error "noise: unknown transport operation")))))))))
+89
+90
;; Cover failures during construction as well as normal session closure.
+91
(define (call-with-handshake-keys static-private ephemeral-private build)
+92
(let ((s #f) (e #f) (transferred? #f))
+93
(dynamic-wind
+94
(lambda () #f)
+95
(lambda ()
+96
(set! s (bytevector-copy static-private))
+97
(set! e (bytevector-copy ephemeral-private))
+98
(let ((session (build s e)))
+99
(set! transferred? #t)
+100
session))
+101
(lambda ()
+102
(unless transferred?
+103
(wipe! s)
+104
(wipe! e))))))
+105
+106
(define (make-noise-handshake/internal initiator? static-private prologue ephemeral-private)
+107
(unless (boolean? initiator?) (error "noise: role must be boolean"))
+108
(require-bytes static-private 32)
+109
(require-bytes ephemeral-private 32)
+110
(unless (bytevector? prologue) (error "noise: prologue must be bytes"))
+111
(call-with-handshake-keys static-private ephemeral-private
+112
(lambda (s e)
+113
(let* ((sp (x25519-public-key s))
+114
(ep (x25519-public-key e))
+115
(name (string->utf8 protocol-name))
+116
(initial-h (if (<= (bytevector-length name) 32)
+117
(bytevector-append name (make-bytevector (- 32 (bytevector-length name)) 0))
+118
(sha256-bytes name)))
+119
(h (sha256-bytes (bytevector-append initial-h prologue)))
+120
(ck (bytevector-copy initial-h))
+121
(cipher #f)
+122
(re #f)
+123
(rs #f)
+124
(step 0)
+125
(closed? #f)
+126
(split? #f))
+127
(unless (and sp ep) (error "noise: public key derivation failed"))
+128
(define (clear-secrets!)
+129
(wipe! s)
+130
(wipe! e)
+131
(wipe! ck)
+132
(set! s #f)
+133
(set! e #f)
+134
(set! ck #f)
+135
(when cipher (cipher 'close #f #f))
+136
(set! cipher #f))
+137
(define (close!)
+138
(clear-secrets!)
+139
(set! closed? #t))
+140
(define (advance-step!)
+141
(set! step (+ step 1))
+142
(when (= step 3)
+143
(wipe! s)
+144
(wipe! e)
+145
(set! s #f)
+146
(set! e #f)))
+147
(define (mix-hash! bytes)
+148
(set! h (sha256-bytes (bytevector-append h bytes))))
+149
(define (derive input)
+150
(hkdf-sha256 ck input #u8() 64))
+151
(define (mix-dh! private public)
+152
(let ((shared (x25519-shared-secret private public)))
+153
(unless shared (error "noise: invalid peer key"))
+154
(let ((keys (derive shared)))
+155
(wipe! shared)
+156
(wipe! ck)
+157
(set! ck (bytevector-copy keys 0 32))
+158
(when cipher (cipher 'close #f #f))
+159
(let ((key (bytevector-copy keys 32 64)))
+160
(set! cipher (make-noise-cipher/internal key (make-bytevector 12 0)))
+161
(wipe! key))
+162
(wipe! keys))))
+163
(define (encrypt-and-hash! bytes)
+164
(let ((result (if cipher (cipher 'encrypt h bytes) (bytevector-copy bytes))))
+165
(mix-hash! result)
+166
result))
+167
(define (decrypt-and-hash! bytes)
+168
(let ((result (if cipher (cipher 'decrypt h bytes) (bytevector-copy bytes))))
+169
(mix-hash! bytes)
+170
result))
+171
(define (write! payload)
+172
(unless (eq? initiator? (even? step)) (error "noise: expected read"))
+173
(let ((overhead (case step ((0) 32) ((1) 96) ((2) 64))))
+174
(require-message payload 0 (- 65535 overhead)))
+175
(let ((prefix
+176
(case step
+177
((0)
+178
(mix-hash! ep)
+179
ep)
+180
((1)
+181
(mix-hash! ep)
+182
(mix-dh! e re)
+183
(let ((encrypted-static (encrypt-and-hash! sp)))
+184
(mix-dh! s re)
+185
(bytevector-append ep encrypted-static)))
+186
((2)
+187
(let ((encrypted-static (encrypt-and-hash! sp)))
+188
(mix-dh! s re)
+189
encrypted-static)))))
+190
(let ((message (bytevector-append prefix (encrypt-and-hash! payload))))
+191
(advance-step!)
+192
message)))
+193
(define (read! message)
+194
(when (eq? initiator? (even? step)) (error "noise: expected write"))
+195
(require-message message (case step ((0) 32) ((1) 96) ((2) 64)) 65535)
+196
(let ((offset
+197
(case step
+198
((0)
+199
(set! re (bytevector-copy message 0 32))
+200
(mix-hash! re)
+201
32)
+202
((1)
+203
(set! re (bytevector-copy message 0 32))
+204
(mix-hash! re)
+205
(mix-dh! e re)
+206
(set! rs (decrypt-and-hash! (bytevector-copy message 32 80)))
+207
(mix-dh! e rs)
+208
80)
+209
((2)
+210
(set! rs (decrypt-and-hash! (bytevector-copy message 0 48)))
+211
(mix-dh! e rs)
+212
48))))
+213
(let ((payload (decrypt-and-hash! (bytevector-copy message offset))))
+214
(advance-step!)
+215
payload)))
+216
(define (split!)
+217
(when split? (error "noise: already split"))
+218
(let* ((keys (derive #u8()))
+219
(k1 (bytevector-copy keys 0 32))
+220
(k2 (bytevector-copy keys 32 64))
+221
(c1 (make-noise-cipher/internal k1 (make-bytevector 12 0)))
+222
(c2 (make-noise-cipher/internal k2 (make-bytevector 12 0))))
+223
(wipe! keys)
+224
(wipe! k1)
+225
(wipe! k2)
+226
(clear-secrets!)
+227
(set! split? #t)
+228
(if initiator? (make-transport c1 c2) (make-transport c2 c1))))
+229
;; A closure keeps key material out of ordinary printed values. It
+230
;; does not promise secure erasure of copies made by the runtime GC.
+231
(lambda (operation data)
+232
(case operation
+233
((close) (close!))
+234
((complete?) (and (not closed?) (= step 3)))
+235
(else
+236
(when closed? (error "noise: handshake is closed"))
+237
(guard (ex (else (close!) (raise ex)))
+238
(case operation
+239
((write read)
+240
(when (= step 3) (error "noise: handshake already complete"))
+241
(if (eq? operation 'write) (write! data) (read! data)))
+242
((peer-static handshake-hash split)
+243
(unless (= step 3) (error "noise: handshake incomplete"))
+244
(case operation
+245
((peer-static) (bytevector-copy rs))
+246
((handshake-hash) (bytevector-copy h))
+247
((split) (split!))))
+248
(else (error "noise: unknown handshake operation")))))))))))))
test/test-noise.sgladded
@@ -0,0 +1,281 @@
+1
(import (sigil test)
+2
(sigil crypto)
+3
(sigil math)
+4
(sigil noise)
+5
(sigil noise internal))
+6
+7
(define (hex text)
+8
(let* ((n (quotient (string-length text) 2))
+9
(out (make-bytevector n)))
+10
(do ((i 0 (+ i 1))) ((= i n) out)
+11
(bytevector-u8-set! out i
+12
(string->number (substring text (* i 2) (+ (* i 2) 2)) 16)))))
+13
+14
;; Noise-C basic vectors, Noise_XX_25519_ChaChaPoly_SHA256.
+15
;; https://github.com/rweather/noise-c/blob/master/tests/vector/noise-c-basic.txt
+16
;; See THIRD-PARTY-NOTICES for the vector license.
+17
(define init-static (hex "e61ef9919cde45dd5f82166404bd08e38bceb5dfdfded0a34c8df7ed542214d1"))
+18
(define init-ephemeral (hex "893e28b9dc6ca8d611ab664754b8ceb7bac5117349a4439a6b0569da977c464a"))
+19
(define resp-static (hex "4a3acbfdb163dec651dfa3194dece676d437029c62a408b4c5ea9114246e4893"))
+20
(define resp-ephemeral (hex "bbdb4cdbd309f1a1f2e1456967fe288cadd6f712d65dc7b7793d5e63da6b375b"))
+21
(define init-prologue (hex "50726f6c6f677565313233"))
+22
(define handshake-hash (hex "852a28d2146785c54bd8334f4e460c80d7fe4fd0cc5bc0abef2a24a3c4d44d5f"))
+23
(define payloads
+24
(list
+25
(hex "4c756477696720766f6e204d69736573")
+26
(hex "4d757272617920526f746862617264")
+27
(hex "462e20412e20486179656b")
+28
(hex "4361726c204d656e676572")
+29
(hex "4a65616e2d426170746973746520536179")
+30
(hex "457567656e2042f6686d20766f6e2042617765726b")))
+31
(define messages
+32
(list
+33
(hex "ca35def5ae56cec33dc2036731ab14896bc4c75dbb07a61f879f8e3afa4c79444c756477696720766f6e204d69736573")
+34
(hex "95ebc60d2b1fa672c1f46a8aa265ef51bfe38e7ccb39ec5be34069f14480884381cbad1f276e038c48378ffce2b65285e08d6b68aaa3629a5a8639392490e5b94a6d8798832d5372f220f161d9c2df035528f8982ffe09be9b5c412f8a0db5d21351f20af1370d0bf8ef1a8c59a30e")
+35
(hex "c7195ffacac1307ff99046f219750fc47693e23c3cb08b89c2af808b444850a8589981dfbd651e6ff4724a781cc2aa6158c9fea0d4ec82a286427484c5b8c8123a7a6002b1de9f9775fc97")
+36
(hex "96763ed773f8e47bb3712f0e29b3060ffc956ffc146cee53d5e1df")
+37
(hex "3e40f15f6f3a46ae446b253bf8b1d9ffb6ed9b174d272328ff91a7e2e5c79c07f5")
+38
(hex "eb3f3515110702e047a6c9da4478b6ead94873c11c0f2d710ddb3f09fce024b3a58502ae3f")))
+39
+40
(define (new-initiator)
+41
(make-noise-handshake/internal #t init-static init-prologue init-ephemeral))
+42
(define (new-responder)
+43
(make-noise-handshake/internal #f resp-static init-prologue resp-ephemeral))
+44
(define (exchange! i r)
+45
(noise-read-message! r (noise-write-message! i #u8()))
+46
(noise-read-message! i (noise-write-message! r #u8()))
+47
(noise-read-message! r (noise-write-message! i #u8())))
+48
(define (transports)
+49
(let ((i (new-initiator)) (r (new-responder)))
+50
(exchange! i r)
+51
(cons (noise-split! i) (noise-split! r))))
+52
+53
(test "Noise-C XX handshake, transcript hash, and transport vectors"
+54
(let ((i (new-initiator)) (r (new-responder)))
+55
(let loop ((n 0) (ps payloads) (ms messages))
+56
(when (< n 3)
+57
(let ((sender (if (even? n) i r))
+58
(receiver (if (even? n) r i)))
+59
(assert-equal (car ms) (noise-write-message! sender (car ps)))
+60
(assert-equal (car ps) (noise-read-message! receiver (car ms))))
+61
(loop (+ n 1) (cdr ps) (cdr ms))))
+62
(assert-true (noise-handshake-complete? i))
+63
(assert-true (noise-handshake-complete? r))
+64
(assert-equal handshake-hash (noise-handshake-hash i))
+65
(assert-equal handshake-hash (noise-handshake-hash r))
+66
(assert-equal (x25519-public-key resp-static) (noise-peer-static-key i))
+67
(assert-equal (x25519-public-key init-static) (noise-peer-static-key r))
+68
(let ((it (noise-split! i)) (rt (noise-split! r)))
+69
(let loop ((n 3) (ps (cdddr payloads)) (ms (cdddr messages)))
+70
(unless (null? ps)
+71
(let ((sender (if (even? n) it rt))
+72
(receiver (if (even? n) rt it)))
+73
(assert-equal (car ms) (noise-encrypt! sender (car ps)))
+74
(assert-equal (car ps) (noise-decrypt! receiver (car ms))))
+75
(loop (+ n 1) (cdr ps) (cdr ms)))))))
+76
+77
(test "public constructor produces fresh ephemeral keys and interoperates"
+78
(let ((i (make-noise-handshake 'initiator init-static #u8()))
+79
(r (make-noise-handshake 'responder resp-static #u8()))
+80
(other (make-noise-handshake 'initiator init-static #u8())))
+81
(let ((first (noise-write-message! i #u8())))
+82
(assert-false (equal? first (noise-write-message! other #u8())))
+83
(noise-read-message! r first))
+84
(noise-read-message! i (noise-write-message! r #u8()))
+85
(noise-read-message! r (noise-write-message! i #u8()))
+86
(assert-equal (noise-handshake-hash i) (noise-handshake-hash r))
+87
(noise-close! other)))
+88
+89
(test "invalid constructors reject role and key lengths"
+90
(assert-error (make-noise-handshake 'other init-static #u8()))
+91
(assert-error (make-noise-handshake 'initiator (make-bytevector 31) #u8()))
+92
(assert-error (make-noise-handshake 'initiator init-static "prologue")))
+93
+94
(test "caller-owned static keys remain unchanged"
+95
(let ((before (bytevector-copy init-static))
+96
(i (new-initiator)))
+97
(noise-close! i)
+98
(assert-equal before init-static)))
+99
+100
(test "wrong handshake direction closes the session"
+101
(let ((i (new-initiator)) (r (new-responder)))
+102
(assert-error (noise-read-message! i (car messages)))
+103
(assert-error (noise-write-message! r #u8()))
+104
(assert-error (noise-write-message! i #u8()))
+105
(assert-false (noise-handshake-complete? i))))
+106
+107
(test "metadata and transport are unavailable before completion"
+108
(assert-error (noise-peer-static-key (new-initiator)))
+109
(assert-error (noise-handshake-hash (new-initiator)))
+110
(assert-error (noise-split! (new-initiator))))
+111
+112
(test "split is one-shot and completed handshakes cannot be restarted"
+113
(let ((i (new-initiator)) (r (new-responder)))
+114
(exchange! i r)
+115
(noise-split! i)
+116
(assert-error (noise-split! i))
+117
(assert-error (noise-write-message! r #u8()))))
+118
+119
(test "handshake metadata is copied before returning"
+120
(let ((i (new-initiator)) (r (new-responder)))
+121
(exchange! i r)
+122
(let ((hash (noise-handshake-hash i))
+123
(peer (noise-peer-static-key i)))
+124
(bytevector-u8-set! hash 0 0)
+125
(bytevector-u8-set! peer 0 0)
+126
(assert-equal (noise-handshake-hash r) (noise-handshake-hash i))
+127
(assert-equal (x25519-public-key resp-static) (noise-peer-static-key i)))))
+128
+129
(test "prologue mismatch fails authentication and closes the handshake"
+130
(let ((i (new-initiator))
+131
(r (make-noise-handshake/internal #f resp-static #u8() resp-ephemeral)))
+132
(noise-read-message! r (noise-write-message! i #u8()))
+133
(assert-error (noise-read-message! i (noise-write-message! r #u8())))
+134
(assert-error (noise-write-message! i #u8()))))
+135
+136
(test "tampered handshake keys and payloads fail closed"
+137
(for-each
+138
(lambda (index)
+139
(let ((i (new-initiator)) (r (new-responder)))
+140
(noise-read-message! r (noise-write-message! i #u8()))
+141
(let ((message (noise-write-message! r #u8(42))))
+142
(bytevector-u8-set! message index
+143
(modulo (+ 1 (bytevector-u8-ref message index)) 256))
+144
(assert-error (noise-read-message! i message))
+145
(assert-error (noise-split! i)))))
+146
'(0 32 79 80 96)))
+147
+148
(test "low-order ephemeral key aborts before any encrypted response"
+149
(let ((r (new-responder)))
+150
(noise-read-message! r (make-bytevector 32 0))
+151
(assert-error (noise-write-message! r #u8()))
+152
(assert-error (noise-write-message! r #u8()))))
+153
+154
(test "truncated handshake messages are rejected at every step"
+155
(assert-error (noise-read-message! (new-responder) (make-bytevector 31)))
+156
(let ((i (new-initiator)) (r (new-responder)))
+157
(noise-read-message! r (noise-write-message! i #u8()))
+158
(assert-error (noise-read-message! i (make-bytevector 95))))
+159
(let ((i (new-initiator)) (r (new-responder)))
+160
(noise-read-message! r (noise-write-message! i #u8()))
+161
(noise-read-message! i (noise-write-message! r #u8()))
+162
(assert-error (noise-read-message! r (make-bytevector 63)))))
+163
+164
(test "maximum handshake messages work in all three flights"
+165
(let ((i (new-initiator)) (r (new-responder)))
+166
(for-each
+167
(lambda (n)
+168
(let* ((sender (if (= n 65439) r i))
+169
(receiver (if (= n 65439) i r))
+170
(payload (make-bytevector n 42))
+171
(message (noise-write-message! sender payload)))
+172
(assert-equal 65535 (bytevector-length message))
+173
(assert-equal payload (noise-read-message! receiver message))))
+174
'(65503 65439 65471))))
+175
+176
(test "oversized handshake messages fail closed"
+177
(assert-error (noise-write-message! (new-initiator) (make-bytevector 65504)))
+178
(assert-error (noise-read-message! (new-responder) (make-bytevector 65536))))
+179
+180
(test "bidirectional transports have independent nonce sequences"
+181
(let* ((pair (transports)) (i (car pair)) (r (cdr pair)))
+182
(do ((n 0 (+ n 1))) ((= n 260))
+183
(assert-equal #u8(1 2 3) (noise-decrypt! r (noise-encrypt! i #u8(1 2 3)))))
+184
(assert-equal #u8(4 5) (noise-decrypt! i (noise-encrypt! r #u8(4 5))))))
+185
+186
(test "transport supports empty and maximum-size messages"
+187
(let* ((pair (transports)) (i (car pair)) (r (cdr pair)))
+188
(assert-equal #u8() (noise-decrypt! r (noise-encrypt! i #u8())))
+189
(let ((payload (make-bytevector 65519 42)))
+190
(assert-equal payload (noise-decrypt! r (noise-encrypt! i payload))))))
+191
+192
(test "oversized or truncated transport records close both directions"
+193
(let* ((pair (transports)) (r (cdr pair)))
+194
(assert-error (noise-decrypt! r (make-bytevector 15)))
+195
(assert-error (noise-encrypt! r #u8())))
+196
(let* ((pair (transports)) (i (car pair)))
+197
(assert-error (noise-encrypt! i (make-bytevector 65520)))
+198
(assert-error (noise-decrypt! i (make-bytevector 16)))))
+199
+200
(test "replayed records are rejected and close both directions"
+201
(let* ((pair (transports)) (i (car pair)) (r (cdr pair))
+202
(message (noise-encrypt! i #u8(1))))
+203
(assert-equal #u8(1) (noise-decrypt! r message))
+204
(assert-error (noise-decrypt! r message))
+205
(assert-error (noise-encrypt! r #u8()))))
+206
+207
(test "out-of-order records fail authentication"
+208
(let* ((pair (transports)) (i (car pair)) (r (cdr pair)))
+209
(noise-encrypt! i #u8(1))
+210
(assert-error (noise-decrypt! r (noise-encrypt! i #u8(2))))))
+211
+212
(test "closing sessions is idempotent and disables further use"
+213
(let* ((pair (transports)) (i (car pair)))
+214
(noise-close! i)
+215
(noise-close! i)
+216
(assert-error (noise-encrypt! i #u8())))
+217
(let ((i (new-initiator)))
+218
(noise-close! i)
+219
(noise-close! i)
+220
(assert-error (noise-write-message! i #u8()))))
+221
+222
(test "reserved 64-bit nonce is never used"
+223
(let* ((nonce (hex "00000000feffffffffffffff"))
+224
(cipher (make-noise-cipher/internal (make-bytevector 32 7) nonce)))
+225
(assert-equal 16 (bytevector-length (cipher 'encrypt #u8() #u8())))
+226
(assert-error (cipher 'encrypt #u8() #u8()))))
+227
+228
(test "cipher authentication failure does not advance the nonce"
+229
(let* ((key (make-bytevector 32 7))
+230
(nonce (make-bytevector 12 0))
+231
(cipher (make-noise-cipher/internal key nonce))
+232
(valid (chacha20-poly1305-encrypt key nonce #u8() #u8(42))))
+233
(assert-error (cipher 'decrypt #u8() (make-bytevector 16 0)))
+234
(assert-equal #u8(42) (cipher 'decrypt #u8() valid))))
+235
+236
;; Expected tags generated independently with Node's OpenSSL-backed AEAD.
+237
(test "last permitted nonce matches independent AEAD output"
+238
(let ((cipher (make-noise-cipher/internal (make-bytevector 32 7)
+239
(hex "00000000feffffffffffffff"))))
+240
(assert-equal (hex "a2e54bf973e09c248d95674e10650387") (cipher 'encrypt #u8() #u8()))))
+241
+242
;; Expected tags generated independently with Node's OpenSSL-backed AEAD.
+243
(test "counter carry nonce matches independent AEAD output"
+244
(let ((cipher (make-noise-cipher/internal (make-bytevector 32 7)
+245
(hex "00000000ff00000000000000"))))
+246
(assert-equal (hex "c60b502347bfb05aa442d685267252bf") (cipher 'encrypt #u8() #u8()))
+247
(assert-equal (hex "05a7038f2016bfddd97166c7f0416492") (cipher 'encrypt #u8() #u8()))))
+248
+249
(test "tampered third-flight static key and payload fail closed"
+250
(for-each
+251
(lambda (index)
+252
(let ((i (new-initiator)) (r (new-responder)))
+253
(noise-read-message! r (noise-write-message! i #u8()))
+254
(noise-read-message! i (noise-write-message! r #u8()))
+255
(let ((message (noise-write-message! i #u8(42))))
+256
(bytevector-u8-set! message index
+257
(modulo (+ 1 (bytevector-u8-ref message index)) 256))
+258
(assert-error (noise-read-message! r message))
+259
(assert-false (noise-handshake-complete? r))
+260
(assert-error (noise-split! r)))))
+261
'(0 31 32 47 48 64)))
+262
+263
(test "second and third outbound flights enforce their payload limits"
+264
(let ((i (new-initiator)) (r (new-responder)))
+265
(noise-read-message! r (noise-write-message! i #u8()))
+266
(assert-error (noise-write-message! r (make-bytevector 65440))))
+267
(let ((i (new-initiator)) (r (new-responder)))
+268
(noise-read-message! r (noise-write-message! i #u8()))
+269
(noise-read-message! i (noise-write-message! r #u8()))
+270
(assert-error (noise-write-message! i (make-bytevector 65472)))))
+271
+272
(test "changed transport ciphertext and tag close both directions"
+273
(for-each
+274
(lambda (index)
+275
(let* ((pair (transports)) (i (car pair)) (r (cdr pair))
+276
(message (noise-encrypt! i #u8(42))))
+277
(bytevector-u8-set! message index
+278
(modulo (+ 1 (bytevector-u8-ref message index)) 256))
+279
(assert-error (noise-decrypt! r message))
+280
(assert-error (noise-encrypt! r #u8()))))
+281
'(0 1 16)))