AtlatestRepositorysigil-quic
sigil-quic / tree / scriptsextract-vectors.mjs
1
// Extract code-component vectors from the authoritative RFC plain texts.2
// Usage: node scripts/extract-vectors.mjs rfc8448.txt rfc9001.txt3
import { readFileSync, writeFileSync } from 'node:fs';4
const clean = path => readFileSync(path, 'utf8').replace(/^Thomson.*\n|^RFC 8448.*\n|\f/gm, '').replace(/\n\s*\n/g, '\n');5
const tls = clean(process.argv[2]).split('3. Simple 1-RTT Handshake').at(-1).split('4. Resumed')[0];6
const quic = readFileSync(process.argv[3], 'utf8').split('Appendix A. Sample Packet Protection').at(-1);7
const data = new Map();8
function field(text, label, occurrence = 0) {9
const re = new RegExp(`${label.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')} \\((\\d+) octets\\):[ \\t]*((?:[0-9a-f]{2}(?:\\s+|$))+)`, 'g');10
const m = [...text.matchAll(re)][occurrence];11
if (!m) throw Error(`Missing ${label}`);12
const hex = m[2].replace(/\s/g, '');13
if (hex.length !== Number(m[1]) * 2) throw Error(`Wrong length ${label}: ${hex.length / 2}, expected ${m[1]}`);14
return hex;15
}16
function block(marker) {17
const start = tls.indexOf(marker);18
if (start < 0) throw Error(`Missing ${marker}`);19
return tls.slice(start).split(/\n \{(?:server|client)\}/)[0];20
}21
function add(name, value) { data.set(name, value); }22
for (const [side, index] of [['client', 0], ['server', 1]]) {23
add(`${side}-private`, field(tls, 'private key', index));24
add(`${side}-public`, field(tls, 'public key', index));25
}26
for (const name of ['early', 'handshake', 'master']) {27
const b = block(`{server} extract secret "${name}"`);28
add(name, field(b, 'secret'));29
add(`${name}-ikm`, field(b, 'IKM'));30
}31
for (const [name, label] of [['client-hs','c hs traffic'],['server-hs','s hs traffic'],['client-ap','c ap traffic'],['server-ap','s ap traffic'],['exporter','exp master'],['resumption','res master']]) {32
const b = block(`derive secret "tls13 ${label}"`);33
add(name, field(b, 'expanded'));34
add(`${name}-hash`, field(b, 'hash'));35
}36
for (const [name, marker] of [['derived-early','{server} derive secret for handshake'],['derived-handshake','{server} derive secret for master']]) {37
add(name, field(block(marker), 'expanded'));38
}39
for (const [name, marker] of [['server-hs','{server} derive write traffic keys for handshake'],['client-hs','{server} derive read traffic keys for handshake'],['server-ap','{server} derive write traffic keys for application'],['client-ap','{client} derive write traffic keys for application']]) {40
const b = block(marker);41
add(`${name}-key`, field(b, 'key expanded'));42
add(`${name}-iv`, field(b, 'iv expanded'));43
}44
for (const side of ['client', 'server']) {45
const b = block(`{${side}} calculate finished "tls13 finished":`);46
add(`${side}-finished-key`, field(b, 'expanded'));47
add(`${side}-verify`, field(b, 'finished'));48
}49
for (const name of ['ClientHello','ServerHello','EncryptedExtensions','Certificate','CertificateVerify']) {50
add(name, field(tls, name));51
}52
add('ServerFinished', field(tls, 'Finished', 0));53
add('ClientFinished', field(tls, 'Finished', 1));54
function hexBlocks(section) {55
return [...section.matchAll(/(?:^ [a-f0-9]+(?: [a-f0-9]+)*\n)+/gm)].map(m => m[0].replace(/\s/g, ''));56
}57
const client = quic.split('A.2. Client Initial')[1].split('A.3.')[0];58
const server = quic.split('A.3. Server Initial')[1].split('A.4.')[0];59
for (const [side, section] of [['client',client],['server',server]]) {60
const blocks = hexBlocks(section);61
if (blocks.length !== 3) throw Error(`Unexpected ${side} blocks: ${blocks.length}`);62
const sizes = side === 'client' ? [245, 22, 1200] : [99, 20, 135];63
blocks.forEach((hex, i) => {64
if (hex.length !== 2 * sizes[i]) throw Error(`Wrong ${side} block ${i} length: ${hex.length / 2}`);65
});66
add(`quic-${side}-payload`, blocks[0]);67
add(`quic-${side}-header`, blocks[1]);68
add(`quic-${side}-packet`, blocks[2]);69
}70
let output = `;;; RFC 8448 section 3 and RFC 9001 Appendix A code-component vectors.\n;;; Sources: https://www.rfc-editor.org/rfc/rfc8448.txt\n;;; https://www.rfc-editor.org/rfc/rfc9001.txt\n;;; Extract with scripts/extract-vectors.mjs; see LICENSE for RFC terms.\n(define-library (sigil quic vectors)\n (import (sigil quic bytes))\n (export fixture)\n (begin\n (define (fixture name)\n (hex->bytes\n (case name\n`;71
for (const [name, hex] of data) {72
output += ` ((${name})\n (string-append\n`;73
for (let i = 0; i < hex.length; i += 64) output += ` "${hex.slice(i,i+64)}"\n`;74
output += ' ))\n';75
}76
output += ' (else (error "unknown vector" name)))))))\n';77
writeFileSync('src/sigil/quic/vectors.sgl', output);78
console.log(`Extracted ${data.size} vectors with checked lengths`);