// Sealing a secret to one runner in the browser (Connections spec ยง2.4): // libsodium's crypto_box_seal, so the runner opens it with its own key // and the instance, which only carries it, cannot. The box is // // ephemeral_pk || crypto_box(message, nonce, recipient_pk, ephemeral_sk) // nonce = BLAKE2b-192(ephemeral_pk || recipient_pk) // // X25519 and XSalsa20-Poly1305 come from the vendored tweetnacl // (vendor/tweetnacl/nacl-fast.js, public domain); BLAKE2b is below, // adapted from Paul's contra vault (RFC 7693, unkeyed). The Go side is // internal/sealbox, and a browser test opens this page's box with // x/crypto's box.OpenAnonymous. (function () { "use strict"; var MASK = (1n << 64n) - 1n; var IV = [ 0x6a09e667f3bcc908n, 0xbb67ae8584caa73bn, 0x3c6ef372fe94f82bn, 0xa54ff53a5f1d36f1n, 0x510e527fade682d1n, 0x9b05688c2b3e6c1fn, 0x1f83d9abfb41bd6bn, 0x5be0cd19137e2179n ]; var SIGMA = [ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3], [11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4], [7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8], [9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13], [2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9], [12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11], [13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10], [6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5], [10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3] ]; function rotr(x, n) { return ((x >> n) | (x << (64n - n))) & MASK; } function mix(v, a, b, c, d, x, y) { v[a] = (v[a] + v[b] + x) & MASK; v[d] = rotr(v[d] ^ v[a], 32n); v[c] = (v[c] + v[d]) & MASK; v[b] = rotr(v[b] ^ v[c], 24n); v[a] = (v[a] + v[b] + y) & MASK; v[d] = rotr(v[d] ^ v[a], 16n); v[c] = (v[c] + v[d]) & MASK; v[b] = rotr(v[b] ^ v[c], 63n); } function compress(h, block, counter, last) { var v = h.concat(IV); v[12] ^= counter & MASK; v[13] ^= (counter >> 64n) & MASK; if (last) v[14] ^= MASK; var m = new Array(16); for (var i = 0; i < 16; i++) { var w = 0n; for (var j = 7; j >= 0; j--) w = (w << 8n) | BigInt(block[i * 8 + j]); m[i] = w; } for (var r = 0; r < 12; r++) { var s = SIGMA[r]; mix(v, 0, 4, 8, 12, m[s[0]], m[s[1]]); mix(v, 1, 5, 9, 13, m[s[2]], m[s[3]]); mix(v, 2, 6, 10, 14, m[s[4]], m[s[5]]); mix(v, 3, 7, 11, 15, m[s[6]], m[s[7]]); mix(v, 0, 5, 10, 15, m[s[8]], m[s[9]]); mix(v, 1, 6, 11, 12, m[s[10]], m[s[11]]); mix(v, 2, 7, 8, 13, m[s[12]], m[s[13]]); mix(v, 3, 4, 9, 14, m[s[14]], m[s[15]]); } for (var k = 0; k < 8; k++) h[k] = h[k] ^ v[k] ^ v[k + 8]; } // blake2b hashes input (a Uint8Array) to outLen bytes (1 to 64), // unkeyed. function blake2b(outLen, input) { if (outLen < 1 || outLen > 64) throw new Error("blake2b output length"); var h = IV.slice(); h[0] ^= 0x01010000n ^ BigInt(outLen); var counter = 0n, off = 0; while (input.length - off > 128) { counter += 128n; compress(h, input.subarray(off, off + 128), counter, false); off += 128; } var tail = new Uint8Array(128); tail.set(input.subarray(off)); counter += BigInt(input.length - off); compress(h, tail, counter, true); var out = new Uint8Array(outLen); for (var i = 0; i < outLen; i++) out[i] = Number((h[i >> 3] >> BigInt(8 * (i & 7))) & 0xffn); return out; } function toBase64(bytes) { var s = ""; for (var i = 0; i < bytes.length; i++) s += String.fromCharCode(bytes[i]); return btoa(s); } function fromBase64(text) { var s = atob(text), out = new Uint8Array(s.length); for (var i = 0; i < s.length; i++) out[i] = s.charCodeAt(i); return out; } // seal seals message (a Uint8Array) to the recipient's X25519 public // key (standard base64) and returns the box in standard base64. The // ephemeral secret key is wiped before it returns. function seal(publicKey, message) { var nacl = window.nacl; var pk = fromBase64(publicKey); if (!nacl || pk.length !== nacl.box.publicKeyLength) throw new Error("seal key"); var eph = nacl.box.keyPair(); var both = new Uint8Array(64); both.set(eph.publicKey); both.set(pk, 32); var boxed = nacl.box(message, blake2b(nacl.box.nonceLength, both), pk, eph.secretKey); eph.secretKey.fill(0); var out = new Uint8Array(32 + boxed.length); out.set(eph.publicKey); out.set(boxed, 32); return toBase64(out); } window.EquipoSeal = { seal: seal, blake2b: blake2b }; })();