@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 155 lines 7.58 KB
Raw
1 import * as NodeCrypto from "@effect/platform-node/NodeCrypto"
2 import { assert, describe, it } from "@effect/vitest"
3 import * as Aead from "effect/crypto/Aead"
4 import * as Digest from "effect/crypto/Digest"
5 import * as Kdf from "effect/crypto/Kdf"
6 import * as Mac from "effect/crypto/Mac"
7 import * as SecureRandom from "effect/crypto/SecureRandom"
8 import * as Effect from "effect/Effect"
9 import * as Hex from "effect/encoding/Hex"
10 import * as Layer from "effect/Layer"
11 import * as Result from "effect/Result"
12 import * as NodeCryptoModule from "node:crypto"
13
14 const encode = (text: string): Uint8Array => new TextEncoder().encode(text)
15 const bytes = (hex: string): Uint8Array => Result.getOrThrow(Hex.decode(hex))
16
17 // RFC 8439, section 2.8.2, and draft-irtf-cfrg-xchacha-03, Appendix A.3.1.
18 const sunscreen = encode(
19 "Ladies and Gentlemen of the class of '99: If I could offer you only one tip for the future, sunscreen would be it."
20 )
21 const chachaKey = "808182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9f"
22 const aeadVectors: ReadonlyArray<readonly [Aead.Algorithm, string, string, string]> = [
23 [
24 Aead.chacha20Poly1305,
25 chachaKey,
26 "070000004041424344454647",
27 "d31a8d34648e60db7b86afbc53ef7ec2a4aded51296e08fea9e2b5a736ee62d63dbea45e8ca9671282fafb69da92728b" +
28 "1a71de0a9e060b2905d6a5b67ecd3b3692ddbd7f2d778b8c9803aee328091b58fab324e4fad675945585808b4831d7bc" +
29 "3ff4def08e4b7a9de576d26586cec64b61161ae10b594f09e26a7e902ecbd0600691"
30 ],
31 [
32 Aead.xchacha20Poly1305,
33 chachaKey,
34 "404142434445464748494a4b4c4d4e4f5051525354555657",
35 "bd6d179d3e83d43b9576579493c0e939572a1700252bfaccbed2902c21396cbb731c7f1b0b4aa6440bf3a82f4eda7e39" +
36 "ae64c6708c54c216cb96b72e1213b4522f8c9ba40db5d945b11b69b982c1bb9e3f3fac2bc369488f76b2383565d3fff9" +
37 "21f9664c97637da9768812f615c68b13b52ec0875924c1c7987947deafd8780acf49"
38 ]
39 ]
40
41 describe("NodeCrypto", () => {
42 it.effect("layerDigest computes MD5 and SHA digests", () =>
43 Effect.gen(function*() {
44 const digest = yield* Digest.Digest
45 const abc = new TextEncoder().encode("abc")
46 // RFC 1321 and FIPS 180-2 "abc" vectors.
47 assert.strictEqual(yield* digest.digestHex(Digest.md5, abc), "900150983cd24fb0d6963f7d28e17f72")
48 assert.strictEqual(yield* digest.digestHex(Digest.sha1, abc), "a9993e364706816aba3e25717850c26c9cd0d89d")
49 assert.strictEqual(
50 yield* digest.digestHex(Digest.sha256, abc),
51 "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
52 )
53 assert.strictEqual((yield* digest.digest(Digest.sha512, abc)).length, 64)
54 }).pipe(Effect.provide(NodeCrypto.layerDigest)))
55
56 it.effect("layerMac signs with byte-backed and CryptoKey-backed keys", () =>
57 Effect.gen(function*() {
58 const mac = yield* Mac.Mac
59 // RFC 4231, test case 2.
60 const secret = encode("Jefe")
61 const message = encode("what do ya want for nothing?")
62 const expected = "5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843"
63 const cryptoKey = yield* Effect.promise(() =>
64 globalThis.crypto.subtle.importKey("raw", secret as BufferSource, { name: "HMAC", hash: "SHA-256" }, false, [
65 "sign",
66 "verify"
67 ])
68 )
69 for (const key of [yield* Mac.key(Mac.hmacSha256, secret), yield* Mac.fromCryptoKey(Mac.hmacSha256, cryptoKey)]) {
70 const tag = yield* mac.sign(key, message)
71 assert.strictEqual(Hex.encode(tag), expected)
72 assert.isTrue(yield* mac.verify(key, tag, message))
73 assert.isFalse(yield* mac.verify(key, tag.subarray(1), message))
74 }
75 }).pipe(Effect.provide(NodeCrypto.layerMac.pipe(Layer.provide(SecureRandom.layerWebCrypto)))))
76
77 it.effect("layerKdf derives Argon2id where node:crypto has it, and HKDF through the core implementation", () =>
78 Effect.gen(function*() {
79 const kdf = yield* Kdf.Kdf
80 const password = encode("password")
81 const salt = encode("somesalt")
82 // From the Argon2 reference CLI: `argon2 somesalt -id -t 2 -k 64 -p 1 -l 32`.
83 const argon2id = Kdf.argon2id({ memory: 64, passes: 2, parallelism: 1 })
84 if (typeof NodeCryptoModule.argon2 === "function") {
85 assert.strictEqual(
86 Hex.encode(yield* kdf.derive(argon2id, password, { salt, length: 32 })),
87 "16a1a498734609dd01456da406de9f3d9da93e6c86c300a12fc1465214ce4922"
88 )
89 } else {
90 const error = yield* Effect.flip(kdf.derive(argon2id, password, { salt, length: 32 }))
91 assert.strictEqual(error.reason._tag, "Unsupported")
92 }
93 // RFC 5869, test case 3.
94 assert.strictEqual(
95 Hex.encode(
96 yield* kdf.derive(Kdf.hkdfSha256, new Uint8Array(22).fill(0x0b), { salt: new Uint8Array(), length: 42 })
97 ),
98 "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8"
99 )
100 }).pipe(Effect.provide(NodeCrypto.layerKdf)))
101
102 it.effect("layerAead encrypts ChaCha20-Poly1305 on node:crypto and the other algorithms through the core implementation", () =>
103 Effect.gen(function*() {
104 const aead = yield* Aead.Aead
105 const aad = bytes("50515253c0c1c2c3c4c5c6c7")
106 for (const [algorithm, keyHex, nonceHex, expected] of aeadVectors) {
107 const key = yield* Aead.key(algorithm, bytes(keyHex))
108 const nonce = bytes(nonceHex)
109 const sealed = yield* aead.encrypt(key, sunscreen, { nonce, aad })
110 assert.strictEqual(Hex.encode(sealed), expected)
111 assert.deepStrictEqual(yield* aead.decrypt(key, sealed, { nonce, aad }), sunscreen)
112 sealed[sealed.length - 1] ^= 1
113 for (const data of [sealed, sealed.subarray(0, 15)]) {
114 const error = yield* Effect.flip(aead.decrypt(key, data, { nonce, aad }))
115 assert.strictEqual(error.reason._tag, "DecryptionFailed")
116 assert.strictEqual(error.message, "Aead.decrypt: Could not decrypt the data")
117 }
118 // node:crypto would throw its own error for a wrong nonce length.
119 const error = yield* Effect.flip(aead.encrypt(key, sunscreen, { nonce: new Uint8Array(16) }))
120 assert.strictEqual(error.reason._tag, "InvalidArgument")
121 }
122
123 const cryptoKey = yield* Effect.promise(() =>
124 globalThis.crypto.subtle.importKey("raw", new Uint8Array(32), "AES-GCM", false, ["encrypt", "decrypt"])
125 )
126 for (
127 const key of [
128 yield* Aead.key(Aead.aes256Gcm, new Uint8Array(32)),
129 yield* Aead.fromCryptoKey(Aead.aes256Gcm, cryptoKey)
130 ]
131 ) {
132 const nonce = new Uint8Array(12)
133 // The GCM specification, test case 13.
134 assert.strictEqual(
135 Hex.encode(yield* aead.encrypt(key, new Uint8Array(), { nonce })),
136 "530f8afbc74536b9a963b4f1c4cb738b"
137 )
138 }
139 }).pipe(Effect.provide(NodeCrypto.layerAead.pipe(Layer.provide(SecureRandom.layerWebCrypto)))))
140
141 // An effect may run later than it is built, so node:crypto must not see a
142 // nonce that was valid only when the effect was built.
143 it.effect("layerAead checks the ChaCha20-Poly1305 nonce length when the effect runs", () =>
144 Effect.gen(function*() {
145 const aead = yield* Aead.Aead
146 const key = yield* Aead.key(Aead.chacha20Poly1305, bytes(chachaKey))
147 const options = { nonce: new Uint8Array(12) }
148 const data = new Uint8Array(32)
149 const encrypt = aead.encrypt(key, data, options)
150 const decrypt = aead.decrypt(key, data, options)
151 options.nonce = new Uint8Array(16)
152 assert.strictEqual((yield* Effect.flip(encrypt)).reason._tag, "InvalidArgument")
153 assert.strictEqual((yield* Effect.flip(decrypt)).reason._tag, "InvalidArgument")
154 }).pipe(Effect.provide(NodeCrypto.layerAead.pipe(Layer.provide(SecureRandom.layerWebCrypto)))))
155 })