import {createECDH, createHash, hkdfSync, createCipheriv} from 'node:crypto'; import {writeFileSync} from 'node:fs'; // Fixed synthetic keys; this vector is independent of the Go implementation. const scalar = n => { const b=Buffer.alloc(32); b[31]=n; return b; }; const recipient=createECDH('prime256v1'); recipient.setPrivateKey(scalar(1)); const ephemeral=createECDH('prime256v1'); ephemeral.setPrivateKey(scalar(2)); const binding={Version:1,Direction:'input',Account:'bds',App:'home',Host:'test.home.example',RequestID:'request-0001',AttemptID:'attempt-0001',Recipient:createHash('sha256').update(recipient.getPublicKey()).digest('hex')}; const length = n => {const b=Buffer.alloc(4);b.writeUInt32BE(n);return b;}; const fields=[Buffer.from('CME1'),length(1),Buffer.from('input'),...['Account','App','Host','RequestID','AttemptID','Recipient'].map(k=>Buffer.from(binding[k]))]; const aad=Buffer.concat(fields.flatMap(f=>[length(f.length),f])); const shared=ephemeral.computeSecret(recipient.getPublicKey()); const key=Buffer.from(hkdfSync('sha256',shared,Buffer.alloc(0),Buffer.from('carlos-maintenance-v1'),32)); const nonce=Buffer.from(Array.from({length:12},(_,i)=>i));const plaintext=Buffer.from('maintenance-secret-canary'); const cipher=createCipheriv('aes-256-gcm',key,nonce);cipher.setAAD(aad); const ciphertext=Buffer.concat([cipher.update(plaintext),cipher.final(),cipher.getAuthTag()]); const wire=Buffer.concat([Buffer.from('CME1'),ephemeral.getPublicKey(),nonce,ciphertext]); const vector={provenance:'Synthetic keys; independently generated with Node/OpenSSL P-256/ECDH/HKDF-SHA256/AES-GCM, not Go output.',recipient_private_hex:scalar(1).toString('hex'),ephemeral_private_hex:scalar(2).toString('hex'),nonce_hex:nonce.toString('hex'),plaintext:plaintext.toString(),binding,aad_hex:aad.toString('hex'),envelope:wire.toString('base64')}; writeFileSync(new URL('./envelope-vector.json',import.meta.url),JSON.stringify(vector,null,2)+'\n');