Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

7.9 KB
Raw Blame
// @ts-check
// Sec-MS-GEC v2 token for Microsoft's readaloud (Edge TTS) endpoint.
//
// Port of rany2/edge-tts `drm.generate_sec_ms_gec_v2` (edge-tts 7.3.1). The v1
// token (a plain SHA-256 of the ticks and client token) is now rejected with 403.
// The v2 token is "<16 hex CS64>-<64 hex SHA-256>" over the same input string. The SHA-256
// half is a standard SHA-256 whose first 64-byte block (a fixed 57-byte prefix
// plus the first 7 bytes of the input) is supplied as a precomputed midstate,
// so the constants below are reproduced verbatim from the reference rather than
// derived. tests/api/tts-edge-gec.test.js pins the output against vectors
// generated by the reference implementation.

import { Buffer } from 'node:buffer';

const WIN_EPOCH = 11_644_473_600;

const SHA256_INITIAL = [
	0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
];
const SHA256_STATE = [
	0xfd9fbea2, 0x91243ce7, 0xbe499094, 0xcd2e1e0a, 0x6c006f49, 0x04bd35ab, 0xfeb0a7e4, 0x03effbd2,
];
const SHA256_SCHEDULE = [
	0xeb24bf9e, 0x1558691f, 0xa0c09ee2, 0x0cfa32ba, 0xa622ee8c, 0xd1dd9491, 0xc0dd25a0, 0xf9e70ab6,
	0xf0c31573, 0x7058ce4e, 0x7188e606, 0x00e5ab33, 0xd258b060, 0x72766564,
];
const K = [
	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
];
const SHA256_ADJUSTMENT = 0xc9dc06a7;
const K_BLOCK = K.slice();
K_BLOCK[14] = SHA256_ADJUSTMENT;
const SIGMA0_ADJUSTMENT = 0x059c0b80;
const SIGMA1_ADJUSTMENT = 0x000b92c0;

const CS64_COEFFICIENTS = [
	[0xe82e91b1, 0xb32700d0],
	[0x44de24c6, 0x88dff51d],
	[0x3beb65bc, 0x29514038],
	[0xc89c5933, 0xb328cbdf],
	[0x8d1ec211, 0xd405b4d5],
	[0x314ef5d5, 0x26e55f3c],
	[0xe92491b1, 0x607801c6],
	[0xf0a5b9d5, 0x82b2c1fb],
	[0x06064cf3, 0xfa51a3da],
	[0xda79fa60, 0x207cc464],
	[0xbed20560, 0x3c3c74f3],
	[0x89f9c9f8, 0xd2d14b61],
	[0x35fa38c6, 0xbb04a2c1],
	[0xcb49e3c6, 0xc5626167],
];
const CS64_FACTOR = 0x82893933;
const CS64_ADJUSTMENT0 = 0xf2f90b1a;
const CS64_ADJUSTMENT1 = 0x0b1a0000;

/** 32-bit unsigned multiply. */
const mul = (a, b) => Math.imul(a, b) >>> 0;
/** 32-bit unsigned add of any number of operands. */
const add = (...xs) => xs.reduce((s, x) => (s + x) >>> 0, 0);
const ror = (x, n) => ((x >>> n) | (x << (32 - n))) >>> 0;
const rol16 = (x) => ((x << 16) | (x >>> 16)) >>> 0;

// Python's `(v >> 7) ^ (v << 25) ^ ...` keeps high bits that are masked away
// later, so the masked result equals the textbook rotate-based sigma.
const sigma0 = (x) => (ror(x, 7) ^ ror(x, 18) ^ (x >>> 3)) >>> 0;
const sigma1 = (x) => (ror(x, 17) ^ ror(x, 19) ^ (x >>> 10)) >>> 0;

function expandSchedule(w, first = 16) {
	for (let i = first; i < 64; i++) {
		w[i] = add(sigma1(w[i - 2]), w[i - 7], sigma0(w[i - 15]), w[i - 16]);
	}
}

function rounds(state, w, firstRound = 0) {
	const k = firstRound === 14 ? K_BLOCK : K;
	let [a, b, c, d, e, f, g, h] = state;
	for (let i = firstRound; i < 64; i++) {
		const s1 = (ror(e, 6) ^ ror(e, 11) ^ ror(e, 25)) >>> 0;
		const ch = (g ^ (e & (f ^ g))) >>> 0;
		const t1 = add(h, s1, ch, k[i], w[i]);
		const s0 = (ror(a, 2) ^ ror(a, 13) ^ ror(a, 22)) >>> 0;
		const maj = ((a & b) | (c & (a | b))) >>> 0;
		const t2 = add(s0, maj);
		h = g;
		g = f;
		f = e;
		e = add(d, t1);
		d = c;
		c = b;
		b = a;
		a = add(t1, t2);
	}
	return [a, b, c, d, e, f, g, h];
}

/** Midstate after the hidden first block, parameterised by the first 7 input bytes. */
function sha256Init(data) {
	const w = new Array(14).fill(0);
	w.push(data.readUIntBE(0, 3));
	w.push(data.readUInt32BE(3));
	for (let i = 16; i < 30; i++) {
		let v = SHA256_SCHEDULE[i - 16];
		if (i - 2 >= 14) v += i === 16 ? (sigma1(w[i - 2]) ^ SIGMA1_ADJUSTMENT) >>> 0 : sigma1(w[i - 2]);
		if (i - 7 >= 14) v += w[i - 7];
		if (i - 15 >= 14) v += i === 29 ? (sigma0(w[i - 15]) ^ SIGMA0_ADJUSTMENT) >>> 0 : sigma0(w[i - 15]);
		w.push(v >>> 0);
	}
	w.push(add(sigma1(w[28]), w[23], sigma0(w[15]), w[14], SHA256_ADJUSTMENT, -K[14]));
	expandSchedule(w, 31);
	const working = rounds(SHA256_STATE, w, 14);
	return working.map((x, i) => add(x, SHA256_INITIAL[i]));
}

function sha256Digest(data) {
	const length = 57 + data.length;
	const padLen = (((55 - length) % 64) + 64) % 64;
	const tail = data.subarray(7);
	const padded = Buffer.concat([tail, Buffer.from([0x80]), Buffer.alloc(padLen), Buffer.alloc(8)]);
	padded.writeBigUInt64BE(BigInt(length) * 8n, padded.length - 8);
	let state = sha256Init(data.subarray(0, 7));
	for (let off = 0; off < padded.length; off += 64) {
		const w = [];
		for (let i = 0; i < 16; i++) w.push(padded.readUInt32BE(off + i * 4));
		expandSchedule(w);
		const working = rounds(state, w);
		state = working.map((x, i) => add(x, state[i]));
	}
	const out = Buffer.alloc(32);
	state.forEach((x, i) => out.writeUInt32BE(x, i * 4));
	return out;
}

function wordsLe4Even(data) {
	const padded = Buffer.concat([data, Buffer.alloc((4 - (data.length % 4)) % 4)]);
	const words = [];
	for (let i = 0; i < padded.length; i += 4) words.push(padded.readUInt32LE(i));
	if (words.length % 2) words.push(0);
	return words;
}

function cs64Digest(data, digest) {
	const keyA = (digest.readUInt32LE(0) | 1) >>> 0;
	const keyB = (digest.readUInt32LE(4) | 1) >>> 0;
	const factors = [mul(keyA, CS64_FACTOR), mul(keyB, CS64_FACTOR)];
	const terms = CS64_COEFFICIENTS.map(([coef], i) => mul(coef, factors[i % 2]));
	const offsets = CS64_COEFFICIENTS.map(([, off]) => off);
	wordsLe4Even(Buffer.concat([Buffer.from([0]), data])).forEach((word, i) => {
		let term = mul(word, i % 2 === 0 ? keyA : keyB);
		let offset = mul(rol16(word), i % 2 === 0 ? 0xef0569fb : 0xc31713db);
		if (i === 0) {
			term = add(term, mul(CS64_ADJUSTMENT0, factors[0]));
			offset = add(offset, CS64_ADJUSTMENT1);
		}
		terms.push(term);
		offsets.push(offset);
	});
	let swapAcc = 0;
	let swapLast = 0;
	let state = 0;
	let acc = 0;
	for (let i = 0; i < terms.length; i += 2) {
		const x = terms[i];
		const y = terms[i + 1];
		let t = rol16(add(offsets[i], x));
		let q = mul(t, 0x76f50f34);
		swapAcc = add(swapAcc, mul(rol16(q), 0xc3efea97), q);
		t = rol16(add(offsets[i + 1], y));
		q = mul(t, 0x1881ce3c);
		swapLast = add(mul(rol16(q), 0x35bd1ec9), q);
		swapAcc = add(swapAcc, swapLast);

		state = add(mul(state, keyA), x);
		state = mul(rol16(state), 0xcf98b111);
		state = mul(rol16(state), 0x87085b9f);
		state = mul(rol16(state), 0x12ceb96d);
		state = mul(rol16(state), 0x257e1d83);
		acc = add(acc, state);
		t = add(mul(state, keyB), y);
		t = mul(rol16(t), 0xa27416f5);
		t = mul(rol16(t), 0xd38396ff);
		state = mul(rol16(mul(rol16(t), 0x7c932b89)), 0xbfa49f69);
		acc = add(acc, state);
	}
	return [(swapLast ^ state) >>> 0, (swapAcc ^ acc) >>> 0];
}

/**
 * Sec-MS-GEC v2 token.
 * @param {string} trustedClientToken
 * @param {number} unixSeconds current unix time (clock-skew corrected by the caller)
 */
export function sec_ms_gec_v2(trustedClientToken, unixSeconds) {
	let secs = Math.floor(unixSeconds) + WIN_EPOCH;
	secs -= secs % 300;
	// seconds to 100ns ticks is x10^7, which overflows a double's exact range
	// only past 2^53; appending seven zeros keeps it exact.
	const data = Buffer.from(`${secs}0000000${trustedClientToken}`, 'ascii');
	const digest = sha256Digest(data);
	const [a, b] = cs64Digest(data, digest);
	const cs64 = Buffer.alloc(8);
	cs64.writeUInt32LE(a, 0);
	cs64.writeUInt32LE(b, 4);
	return `${cs64.toString('hex').toUpperCase()}-${digest.toString('hex').toUpperCase()}`;
}