| 1 | // Sealing a secret to one runner in the browser (Connections spec §2.4): |
| 2 | // libsodium's crypto_box_seal, so the runner opens it with its own key |
| 3 | // and the instance, which only carries it, cannot. The box is |
| 4 | // |
| 5 | // ephemeral_pk || crypto_box(message, nonce, recipient_pk, ephemeral_sk) |
| 6 | // nonce = BLAKE2b-192(ephemeral_pk || recipient_pk) |
| 7 | // |
| 8 | // X25519 and XSalsa20-Poly1305 come from the vendored tweetnacl |
| 9 | // (vendor/tweetnacl/nacl-fast.js, public domain); BLAKE2b is below, |
| 10 | // adapted from Paul's contra vault (RFC 7693, unkeyed). The Go side is |
| 11 | // internal/sealbox, and a browser test opens this page's box with |
| 12 | // x/crypto's box.OpenAnonymous. |
| 13 | (function () { |
| 14 | "use strict"; |
| 15 | |
| 16 | var MASK = (1n << 64n) - 1n; |
| 17 | var IV = [ |
| 18 | 0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n, |
| 19 | 0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n |
| 20 | ]; |
| 21 | var SIGMA = [ |
| 22 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], |
| 23 | [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], |
| 24 | [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], |
| 25 | [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], |
| 26 | [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], |
| 27 | [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], |
| 28 | [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], |
| 29 | [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], |
| 30 | [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], |
| 31 | [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0], |
| 32 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], |
| 33 | [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3] |
| 34 | ]; |
| 35 | |
| 36 | function rotr(x, n) { return ((x >> n) | (x << (64n - n))) & MASK; } |
| 37 | |
| 38 | function mix(v, a, b, c, d, x, y) { |
| 39 | v[a] = (v[a] + v[b] + x) & MASK; |
| 40 | v[d] = rotr(v[d] ^ v[a], 32n); |
| 41 | v[c] = (v[c] + v[d]) & MASK; |
| 42 | v[b] = rotr(v[b] ^ v[c], 24n); |
| 43 | v[a] = (v[a] + v[b] + y) & MASK; |
| 44 | v[d] = rotr(v[d] ^ v[a], 16n); |
| 45 | v[c] = (v[c] + v[d]) & MASK; |
| 46 | v[b] = rotr(v[b] ^ v[c], 63n); |
| 47 | } |
| 48 | |
| 49 | function compress(h, block, counter, last) { |
| 50 | var v = h.concat(IV); |
| 51 | v[12] ^= counter & MASK; |
| 52 | v[13] ^= (counter >> 64n) & MASK; |
| 53 | if (last) v[14] ^= MASK; |
| 54 | var m = new Array(16); |
| 55 | for (var i = 0; i < 16; i++) { |
| 56 | var w = 0n; |
| 57 | for (var j = 7; j >= 0; j--) w = (w << 8n) | BigInt(block[i * 8 + j]); |
| 58 | m[i] = w; |
| 59 | } |
| 60 | for (var r = 0; r < 12; r++) { |
| 61 | var s = SIGMA[r]; |
| 62 | mix(v, 0, 4, 8, 12, m[s[0]], m[s[1]]); |
| 63 | mix(v, 1, 5, 9, 13, m[s[2]], m[s[3]]); |
| 64 | mix(v, 2, 6, 10, 14, m[s[4]], m[s[5]]); |
| 65 | mix(v, 3, 7, 11, 15, m[s[6]], m[s[7]]); |
| 66 | mix(v, 0, 5, 10, 15, m[s[8]], m[s[9]]); |
| 67 | mix(v, 1, 6, 11, 12, m[s[10]], m[s[11]]); |
| 68 | mix(v, 2, 7, 8, 13, m[s[12]], m[s[13]]); |
| 69 | mix(v, 3, 4, 9, 14, m[s[14]], m[s[15]]); |
| 70 | } |
| 71 | for (var k = 0; k < 8; k++) h[k] = h[k] ^ v[k] ^ v[k + 8]; |
| 72 | } |
| 73 | |
| 74 | // blake2b hashes input (a Uint8Array) to outLen bytes (1 to 64), |
| 75 | // unkeyed. |
| 76 | function blake2b(outLen, input) { |
| 77 | if (outLen < 1 || outLen > 64) throw new Error("blake2b output length"); |
| 78 | var h = IV.slice(); |
| 79 | h[0] ^= 0x01010000n ^ BigInt(outLen); |
| 80 | var counter = 0n, off = 0; |
| 81 | while (input.length - off > 128) { |
| 82 | counter += 128n; |
| 83 | compress(h, input.subarray(off, off + 128), counter, false); |
| 84 | off += 128; |
| 85 | } |
| 86 | var tail = new Uint8Array(128); |
| 87 | tail.set(input.subarray(off)); |
| 88 | counter += BigInt(input.length - off); |
| 89 | compress(h, tail, counter, true); |
| 90 | var out = new Uint8Array(outLen); |
| 91 | for (var i = 0; i < outLen; i++) out[i] = Number((h[i >> 3] >> BigInt(8 * (i & 7))) & 0xffn); |
| 92 | return out; |
| 93 | } |
| 94 | |
| 95 | function toBase64(bytes) { |
| 96 | var s = ""; |
| 97 | for (var i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]); |
| 98 | return btoa(s); |
| 99 | } |
| 100 | |
| 101 | function fromBase64(text) { |
| 102 | var s = atob(text), out = new Uint8Array(s.length); |
| 103 | for (var i = 0; i < s.length; i++) out[i] = s.charCodeAt(i); |
| 104 | return out; |
| 105 | } |
| 106 | |
| 107 | // seal seals message (a Uint8Array) to the recipient's X25519 public |
| 108 | // key (standard base64) and returns the box in standard base64. The |
| 109 | // ephemeral secret key is wiped before it returns. |
| 110 | function seal(publicKey, message) { |
| 111 | var nacl = window.nacl; |
| 112 | var pk = fromBase64(publicKey); |
| 113 | if (!nacl || pk.length !== nacl.box.publicKeyLength) throw new Error("seal key"); |
| 114 | var eph = nacl.box.keyPair(); |
| 115 | var both = new Uint8Array(64); |
| 116 | both.set(eph.publicKey); |
| 117 | both.set(pk, 32); |
| 118 | var boxed = nacl.box(message, blake2b(nacl.box.nonceLength, both), pk, eph.secretKey); |
| 119 | eph.secretKey.fill(0); |
| 120 | var out = new Uint8Array(32 + boxed.length); |
| 121 | out.set(eph.publicKey); |
| 122 | out.set(boxed, 32); |
| 123 | return toBase64(out); |
| 124 | } |
| 125 | |
| 126 | window.EquipoSeal = { seal: seal, blake2b: blake2b }; |
| 127 | })(); |
| 128 | |