1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193 | ;; ported-from: src/lib/selftest.ts
;;
;; Crypto self-test: roundtrip, cross-key and AAD-mismatch must fail. Runs in
;; dev at boot (room.cljs) and as a first-class node test (test/selftest.test.mjs).
(ns sueta.lib.selftest
(:require [ardegazu.id.identity :refer (Identity verify-assertion)]
[ardegazu.rooms.js :as j]
[ardegazu.rooms.lib.access :as access]
[ardegazu.rooms.lib.crypto :as rc]
[shadow.cljs.modern :refer (js-await)]))
(declare at-rest-part authorship-part)
(defn- to-hex [b]
(.join (.map (js/Array.from ^js b) (fn [x] (.padStart (.toString x 16) 2 "0"))) ""))
(defn- assert! [ok what]
;; NOT named `cond`: a local named after a core MACRO silently shadows it
;; inside its own body (test/source-hygiene.test.mjs)
(when-not (j/truthy? ok)
(throw (js/Error. (str "crypto self-test FAILED: " what))))
js/undefined)
(defn- bytes-eq [a b]
(and (j/truthy? a)
(.every (js/Array.from ^js a) (fn [v i] (identical? v (aget b i))))))
(defn run-self-test []
(-> (js/Promise.resolve nil)
(.then
(fn [_]
(let [secret (rc/new-room-secret)]
(js-await [a (rc/create secret "selftest/v1")]
(js-await [b (rc/create secret "selftest/v1")]
(js-await [other (rc/create (rc/new-room-secret) "selftest/v1")]
(js-await [other-app (rc/create secret "selftest/v2")]
(do
(assert! (identical? (unchecked-get a "roomId") (unchecked-get b "roomId"))
"same secret ⇒ same roomId")
(assert! (not (identical? (unchecked-get a "roomId") (unchecked-get other "roomId")))
"different secret ⇒ different roomId")
(assert! (not (identical? (unchecked-get a "roomId") (unchecked-get other-app "roomId")))
"different app salt ⇒ different roomId")
;; signaling roundtrip a → b
(js-await [sig (rc/seal-signal a (unchecked-get b "peerId")
(j/ordered "kind" "offer" "sdp" "x" "name" "n"))]
(js-await [opened (rc/open-signal b (unchecked-get a "peerId") sig)]
(do
(assert! (identical? (unchecked-get opened "sdp") "x") "signal roundtrip")
(js-await [wrong-from (rc/open-signal b (unchecked-get b "peerId") sig)]
(do
(assert! (nil? wrong-from) "wrong `from` must fail (AAD)")
(js-await [c (rc/create secret "selftest/v1")]
(js-await [wrong-to (rc/open-signal c (unchecked-get a "peerId") sig)]
(do
(assert! (nil? wrong-to) "wrong `to` must fail (AAD)")
(js-await [wrong-room (rc/open-signal other (unchecked-get a "peerId") sig)]
(do
(assert! (nil? wrong-room) "wrong room must fail")
;; datachannel JSON roundtrip + binary
(js-await [msg (rc/seal-msg a (j/ordered "kind" "chat" "text" "hi"))]
(js-await [m1 (rc/open-msg b (unchecked-get a "peerId") msg)]
(do
(assert! (identical? (unchecked-get m1 "text") "hi") "msg roundtrip")
(js-await [m2 (rc/open-msg b (unchecked-get b "peerId") msg)]
(do
(assert! (nil? m2) "msg wrong sender must fail")
(let [data (js/crypto.getRandomValues (js/Uint8Array. 1000))]
(js-await [bin (rc/seal-msg-binary a data)]
(js-await [opened-bin (rc/open-msg-binary b (unchecked-get a "peerId") bin)]
(do
(assert! (bytes-eq opened-bin data) "binary roundtrip")
(js-await [bad-bin (rc/open-msg-binary b (unchecked-get b "peerId") bin)]
(do
(assert! (nil? bad-bin) "binary wrong sender must fail")
;; IVs must never repeat for one sealer
(let [ivs (js/Set.)]
(-> ((fn step [i]
(if (>= i 200)
(js/Promise.resolve nil)
(js-await [e (rc/seal-msg a (j/ordered "i" i))]
(do (.add ivs (unchecked-get e "iv"))
(step (inc i))))))
0)
(.then
(fn [_]
(assert! (identical? 200 (.-size ivs)) "IV uniqueness")
;; identity: deterministic from seed, assertion
;; verifies, wrong context fails
(let [seed (.newSeed Identity)]
(js-await [id1 (.fromSeed Identity seed)]
(js-await [id2 (.fromSeed Identity seed)]
(do
(assert! (identical? (unchecked-get id1 "publicKeyB64")
(unchecked-get id2 "publicKeyB64"))
"identity deterministic from seed")
(assert! (and (pos? (.-length (unchecked-get (unchecked-get id1 "fingerprint") "emoji")))
(identical? 64 (.-length (unchecked-get (unchecked-get id1 "fingerprint") "hex"))))
"fingerprint shape")
(js-await [id-sig (.assert ^js id1 "selftest/v1"
(unchecked-get a "roomId")
(unchecked-get a "peerId"))]
(js-await [v1 (verify-assertion "selftest/v1" (unchecked-get a "roomId")
(unchecked-get a "peerId")
(unchecked-get id1 "publicKeyB64") id-sig)]
(do
(assert! (not (nil? v1)) "assertion verifies")
(js-await [v2 (verify-assertion "selftest/v1" (unchecked-get a "roomId")
(unchecked-get b "peerId")
(unchecked-get id1 "publicKeyB64") id-sig)]
(do
(assert! (nil? v2) "assertion bound to peerId")
(js-await [v3 (verify-assertion "selftest/v1" (unchecked-get other "roomId")
(unchecked-get a "peerId")
(unchecked-get id1 "publicKeyB64") id-sig)]
(do
(assert! (nil? v3) "assertion bound to room")
(js-await [_ (at-rest-part a b other)]
(js-await [_ (authorship-part)]
(do (js/console.info "✅ crypto self-test passed")
js/undefined)))))))))))))))))))))))))))))))))))))))))))))))))
;; The v2 at-rest layer: dbName must be identical across instances (this is what
;; makes every member open the SAME OrbitDB address), ciphers must roundtrip
;; across instances of the same room and reject tampering + foreign rooms.
(defn- at-rest-part [a b other]
(js-await [na (rc/db-name a)]
(js-await [nb (rc/db-name b)]
(js-await [no (rc/db-name other)]
(do
(assert! (identical? na nb) "same secret ⇒ same dbName")
(assert! (not (identical? na no)) "different secret ⇒ different dbName")
(js-await [ea (rc/log-entry-cipher a)]
(js-await [eb (rc/log-entry-cipher b)]
(js-await [e-other (rc/log-entry-cipher other)]
(let [plain (js/crypto.getRandomValues (js/Uint8Array. 500))]
(js-await [sealed (rc/at-rest-seal ea plain)]
(js-await [opened (rc/at-rest-open eb sealed)]
(do
(assert! (bytes-eq opened plain) "at-rest cross-instance roundtrip")
(js-await [foreign (rc/at-rest-open e-other sealed)]
(do
(assert! (nil? foreign) "at-rest foreign room must fail")
(let [tampered (.slice ^js sealed)]
(aset tampered (dec (.-length tampered))
(bit-xor (aget tampered (dec (.-length tampered))) 0x01))
(js-await [tam (rc/at-rest-open eb tampered)]
(do
(assert! (nil? tam) "at-rest tamper must fail")
(js-await [pc (rc/log-payload-cipher a)]
(js-await [cross (rc/at-rest-open pc sealed)]
(do
(assert! (nil? cross) "entry/payload key domains must differ")
(let [iv2 (js/Set.)]
(-> ((fn step [i]
(if (>= i 100)
(js/Promise.resolve nil)
(js-await [s (rc/at-rest-seal ea plain)]
(do (.add iv2 (to-hex (.subarray ^js s 1 13)))
(step (inc i))))))
0)
(.then (fn [_]
(assert! (identical? 100 (.-size iv2))
"at-rest IV uniqueness (random)")
js/undefined))))))))))))))))))))))))
;; Authorship binding (lib/access): the entry's signing key must BE the
;; referenced identity's key — a member signing with their own key while
;; referencing a victim's identity must be rejected.
(defn- authorship-part []
(let [identities (j/ordered
"getIdentity"
(fn [hash]
(js/Promise.resolve
(if (identical? hash "victim-ref")
(j/ordered "publicKey" "victim-pub")
(j/ordered "publicKey" "attacker-pub")))))]
(js-await [ok (access/signer-matches-identity identities (j/ordered "key" "victim-pub" "identity" "victim-ref"))]
(do
(assert! ok "matching signer/identity must pass")
(js-await [forged (access/signer-matches-identity identities (j/ordered "key" "attacker-pub" "identity" "victim-ref"))]
(do
(assert! (not forged) "forged authorship (foreign identity ref) must fail")
(js-await [no-key (access/signer-matches-identity identities (j/ordered "identity" "victim-ref"))]
(do
(assert! (not no-key) "missing key must fail")
(js-await [no-ref (access/signer-matches-identity identities (j/ordered "key" "victim-pub"))]
(do
(assert! (not no-ref) "missing identity ref must fail")
js/undefined))))))))))
|