// Reference generator for the prekey variant of Noise IK message 1 (ticket 113), so that
// src/noise.c is not its own reference. Node's built-in crypto only. Prints JSON; the values are
// pinned in test/pic/test_noise.c.
//
// Protocol name Noise_IKprekey_P256_AESGCM_SHA256 (33 bytes, so h starts as its SHA-256).
// Message 1: e, es, s, ss, EncryptAndHash(empty), then with a prekey P: MixHash(bind),
// MixKey(DH(e, P)), MixKey(DH(s, P)); then EncryptAndHash(payload). Message 2 is IK's.
const crypto = require('node:crypto');
const cat = (...b) => Buffer.concat(b.map(x => typeof x === 'string' ? Buffer.from(x, 'utf8') : x));
const sha = (...b) => crypto.createHash('sha256').update(cat(...b)).digest();
const hmac = (k, ...d) => crypto.createHmac('sha256', k).update(cat(...d)).digest();
const seq = (a) => Buffer.from(Array.from({length: 32}, (_, i) => a + i));
const hex = (b) => Buffer.from(b).toString('hex');
function hkdf(ck, ikm) { const t = hmac(ck, ikm), o1 = hmac(t, Buffer.from([1])); return [o1, hmac(t, o1, Buffer.from([2]))]; }
function pub(priv) { const e = crypto.createECDH('prime256v1'); e.setPrivateKey(priv); return e.getPublicKey(); }
function dh(priv, pk) { const e = crypto.createECDH('prime256v1'); e.setPrivateKey(priv); return e.computeSecret(pk); }
function nonce(n) { const b = Buffer.alloc(12); b.writeBigUInt64BE(BigInt(n), 4); return b; }
function seal(k, n, ad, pt) {
const c = crypto.createCipheriv('aes-256-gcm', k, n); c.setAAD(ad);
return cat(c.update(pt), c.final(), c.getAuthTag());
}
class Sym {
constructor(name) {
const b = Buffer.from(name).length > 32 ? sha(name) : Buffer.alloc(32);
if (Buffer.from(name).length <= 32) Buffer.from(name).copy(b);
this.h = b; this.ck = b; this.k = null; this.n = 0;
}
mixHash(d) { this.h = sha(this.h, d); }
mixKey(ikm) { [this.ck, this.k] = hkdf(this.ck, ikm); this.n = 0; }
enc(pt) { const c = seal(this.k, nonce(this.n++), this.h, pt); this.mixHash(c); return c; }
}
const IKP = 'Noise_IKprekey_P256_AESGCM_SHA256';
// Message 1 as the initiator writes it. SS and SP are the ss and DH(s, P) outputs, so a forger
// can pass its own; ie and is are the initiator's ephemeral and static private keys.
function msg1(prologue, rsp, ie, isp, ss, prekey, bind, sp, p1) {
const I = new Sym(IKP), iep = pub(ie);
I.mixHash(prologue); I.mixHash(rsp);
I.mixHash(iep); I.mixKey(dh(ie, rsp));
const cs = I.enc(isp); I.mixKey(ss);
const tag = I.enc(Buffer.alloc(0));
if (prekey) { I.mixHash(bind); I.mixKey(dh(ie, prekey)); I.mixKey(sp); }
return { I, m1: cat(iep, cs, tag, I.enc(p1)) };
}
// Message 2, <- e, ee, se, from the initiator's symmetric state (both sides agree on it).
function msg2(I, re, iep, isp, p2) {
const R = new Sym(IKP); R.h = I.h; R.ck = I.ck;
const rep = pub(re);
R.mixHash(rep); R.mixKey(dh(re, iep)); R.mixKey(dh(re, isp));
return { m2: cat(rep, R.enc(p2)), h: R.h };
}
// The point (0, y) is on P-256, so a prekey P = e^-1 (0, y) makes DH(e, P) all zeros.
const p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffffn;
const q = 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551n;
const B = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604bn;
const md = (a, m) => ((a % m) + m) % m;
const pw = (a, e, m) => { let r = 1n; a = md(a, m); while (e) { if (e & 1n) r = r * a % m; a = a * a % m; e >>= 1n; } return r; };
const inv = (a, m) => pw(a, m - 2n, m);
function add(P, Q) {
if (!P) return Q; if (!Q) return P;
let l;
if (P[0] === Q[0]) { if (md(P[1] + Q[1], p) === 0n) return null; l = md(3n * P[0] * P[0] - 3n, p) * inv(2n * P[1], p); }
else l = md(Q[1] - P[1], p) * inv(md(Q[0] - P[0], p), p);
l = md(l, p); const x = md(l * l - P[0] - Q[0], p);
return [x, md(l * (P[0] - x) - P[1], p)];
}
function mul(k, P) { let R = null; for (; k; k >>= 1n, P = add(P, P)) if (k & 1n) R = add(R, P); return R; }
const big = (b) => BigInt('0x' + hex(b)), b32 = (x) => Buffer.from(x.toString(16).padStart(64, '0'), 'hex');
const hubS = seq(0x01), devS = seq(0x21), ie = seq(0x81), re = seq(0xa1), preP = seq(0xd1);
const hubP = pub(hubS), devP = pub(devS), preK = pub(preP), iep = pub(ie);
const id = Buffer.from('e1e2e3e4e5e6e7e8', 'hex');
const p1 = Buffer.from('set alpha beta'), p2 = Buffer.from('ok');
const out = { protocol: IKP, keys: { responder_static_private: hex(hubS), initiator_static_private: hex(devS), initiator_ephemeral_private: hex(ie),
responder_ephemeral_private: hex(re), prekey_private: hex(preP), prekey_public: hex(preK) }, payload1: p1.toString(), payload2: p2.toString() };
{
const prologue = cat('pic-noise-prekey/1', Buffer.from([0]));
const { I, m1 } = msg1(prologue, hubP, ie, devP, dh(devS, hubP), null, null, null, p1);
const h1 = I.h, r2 = msg2(I, re, iep, devP, p2);
out.plain = { prologue: hex(prologue), message1: hex(m1), h1: hex(h1), message2: hex(r2.m2), h2: hex(r2.h) };
}
{
const prologue = cat('pic-noise-prekey/1', Buffer.from([1])), bind = cat(Buffer.from([1]), id, preK);
const { I, m1 } = msg1(prologue, hubP, ie, devP, dh(devS, hubP), preK, bind, dh(devS, preK), p1);
const h1 = I.h, r2 = msg2(I, re, iep, devP, p2);
out.prekey = { prologue: hex(prologue), bind: hex(bind), message1: hex(m1), h1: hex(h1), message2: hex(r2.m2), h2: hex(r2.h) };
// A forger holding the responder's static private key but neither the initiator's nor P's:
// ss is DH(responder, initiator), which it can compute; DH(s, P) it cannot, and guesses.
const f = msg1(prologue, hubP, ie, devP, dh(hubS, devP), preK, bind, dh(ie, preK), p1);
out.prekey.forged_message1 = hex(f.m1);
}
{
// IKpsk1's message 1 head without the psk: e also mixed as a key, no MixKeyAndHash(psk), then the
// early tag and a payload. A responder started for IKpsk1 must refuse it at begin.
const prologue = cat('pic-noise-prekey/1', Buffer.from([0])), I = new Sym('Noise_IKpsk1_P256_AESGCM_SHA256');
I.mixHash(prologue); I.mixHash(hubP);
I.mixHash(iep); I.mixKey(iep); I.mixKey(dh(ie, hubP));
const cs = I.enc(devP); I.mixKey(dh(devS, hubP));
const tag = I.enc(Buffer.alloc(0));
out.psk_without_psk_message1 = hex(cat(iep, cs, tag, I.enc(p1)));
}
{
const Q0 = [0n, big(Buffer.from('66485c780e2f83d72433bd5d84a06bb6541c2af31dae871728bf856a174f93f4', 'hex'))];
const P = mul(inv(big(ie), q), Q0), Pb = cat(Buffer.from([4]), b32(P[0]), b32(P[1]));
if (!dh(ie, Pb).equals(Buffer.alloc(32))) throw new Error('zero dh');
out.zero_dh_prekey = hex(Pb);
}
console.log(JSON.stringify(out, null, 1));