feat(media,android): Kotlin SMF1 reader/sealer and live channel, cross-language vectors

test-vectors/media.json pins SMF1 files (empty, partial, exact boundary,
multi-segment with full metadata) and SLF1 live frames in link mode across
a rotation. Kotlin reads them and reproduces every sealed file and every
signed frame byte for byte; TS locks the same file.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 21:07:45 +02:00
parent f359cbd9ad
commit d117397b36
12 changed files with 1514 additions and 4 deletions

View File

@@ -10,7 +10,8 @@
"./node": "./src/node.ts",
"./server": "./src/server.ts",
"./browser": "./src/browser/index.ts",
"./cli": "./src/cli.ts"
"./cli": "./src/cli.ts",
"./testing": "./src/testing.ts"
},
"dependencies": {
"@noble/curves": "^2.0.1",

View File

@@ -108,9 +108,17 @@ export interface SealResult {
segmentCount: number;
}
/**
* Pins the key and id instead of drawing them — for cross-language vectors
* ONLY, via `@shade/media/testing`. Not exported from the package root: SMF1
* nonces are deterministic, so a key used for two files breaks AES-GCM.
*/
export const VECTOR_KEYS: unique symbol = Symbol('shade-media/vector-keys');
export interface SealOptions {
meta?: MediaMeta;
segmentSize?: number;
[VECTOR_KEYS]?: { mediaId: Uint8Array; mediaKey: Uint8Array };
}
interface DerivedKeys {
@@ -118,6 +126,14 @@ interface DerivedKeys {
meta: AeadKey;
}
/** Raw segment and meta keys — exposed for vectors and ports, not for sealing. */
export function deriveMediaSubkeys(mediaKey: Uint8Array, mediaId: Uint8Array): { segmentKey: Uint8Array; metaKey: Uint8Array } {
return {
segmentKey: hkdf(nobleSha256, mediaKey, mediaId, INFO_SEGMENT, 32),
metaKey: hkdf(nobleSha256, mediaKey, mediaId, INFO_META, 32),
};
}
function deriveKeys(mediaKey: Uint8Array, mediaId: Uint8Array): DerivedKeys {
if (mediaKey.length !== MEDIA_KEY_BYTES) throw new MediaFormatError('mediaKey must be 32 bytes');
if (mediaId.length !== MEDIA_ID_BYTES) throw new MediaFormatError('mediaId must be 16 bytes');
@@ -288,8 +304,9 @@ export class MediaSealer {
this.segmentSize = opts.segmentSize ?? DEFAULT_SEGMENT_SIZE;
checkSegmentSize(this.segmentSize);
this.meta = validateMeta(opts.meta ?? {});
this.mediaId = randomBytes(MEDIA_ID_BYTES);
this.mediaKey = randomBytes(MEDIA_KEY_BYTES);
const pinned = opts[VECTOR_KEYS];
this.mediaId = pinned?.mediaId ?? randomBytes(MEDIA_ID_BYTES);
this.mediaKey = pinned?.mediaKey ?? randomBytes(MEDIA_KEY_BYTES);
this.keys = deriveKeys(this.mediaKey, this.mediaId);
}

View File

@@ -0,0 +1,15 @@
/**
* Vector helpers. NOT for production: these pin keys that the real API
* always draws fresh, and SMF1's nonces are deterministic per key.
*/
import { sealMediaBytes, VECTOR_KEYS } from './sealed.js';
import type { SealOptions } from './sealed.js';
export function sealWithPinnedKeys(
plaintext: Uint8Array,
mediaId: Uint8Array,
mediaKey: Uint8Array,
opts: Omit<SealOptions, typeof VECTOR_KEYS> = {},
): ReturnType<typeof sealMediaBytes> {
return sealMediaBytes(plaintext, { ...opts, [VECTOR_KEYS]: { mediaId, mediaKey } });
}

View File

@@ -0,0 +1,59 @@
/**
* `test-vectors/media.json` — the contract the Kotlin port is held to.
* Regenerate with `bun run scripts/generate-vectors.ts media.json`.
*/
import { describe, test, expect } from 'bun:test';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { MediaReader, deriveMediaSubkeys } from '../src/sealed.js';
import { sealWithPinnedKeys } from '../src/testing.js';
import { MemorySource } from '../src/source.js';
import { LiveSubscriber } from '../src/live/channel.js';
import { encodeContent } from '../src/live/frame.js';
import { parseGrant } from '../src/grant.js';
import type { LiveGrant } from '../src/grant.js';
const file = JSON.parse(readFileSync(join(import.meta.dir, '../../../test-vectors/media.json'), 'utf8'));
const vs: any[] = file.vectors;
const hex = (s: string) => Uint8Array.from(Buffer.from(s, 'hex'));
const toHex = (b: Uint8Array) => Buffer.from(b).toString('hex');
const by = (p: string) => vs.filter((v) => v.description.startsWith(p));
describe('media vectors', () => {
const keys = by('smf1 keys')[0];
test('subkeys', () => {
const k = deriveMediaSubkeys(hex(keys.mediaKey), hex(keys.mediaId));
expect(toHex(k.segmentKey)).toBe(keys.segmentKey);
expect(toHex(k.metaKey)).toBe(keys.metaKey);
});
for (const v of by('smf1 file')) {
test(v.description, async () => {
const { bytes, result } = await sealWithPinnedKeys(hex(v.plaintext), hex(keys.mediaId), hex(keys.mediaKey), {
segmentSize: v.segmentSize,
meta: v.meta,
});
expect(toHex(bytes)).toBe(v.sealed);
expect(toHex(result.rootDigest)).toBe(v.rootDigest);
const r = await MediaReader.open(new MemorySource(hex(v.sealed)), {
mediaId: hex(keys.mediaId),
mediaKey: hex(keys.mediaKey),
rootDigest: hex(v.rootDigest),
});
expect(toHex(await r.read(0, r.size))).toBe(v.plaintext);
expect(r.meta).toEqual(v.meta);
});
}
test('live frames open from the link grant, across a rotation', async () => {
const g = parseGrant(by('live grant')[0].json) as LiveGrant;
const sub = new LiveSubscriber(g);
for (const v of by('live frame')) {
const f = await sub.open(hex(v.wire));
expect(f).not.toBeNull();
expect(f!.seq).toBe(v.seq);
expect(toHex(encodeContent(f!.content))).toBe(v.payload);
}
});
});