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:
@@ -52,6 +52,13 @@ import {
|
||||
} from '../packages/shade-vault/src/testing.js';
|
||||
import type { VaultEntry, VaultManifest } from '../packages/shade-vault/src/types.js';
|
||||
import { canonicalizePayload } from '../packages/shade-server/src/auth.js';
|
||||
import { deriveMediaSubkeys } from '../packages/shade-media/src/sealed.js';
|
||||
import { sealWithPinnedKeys } from '../packages/shade-media/src/testing.js';
|
||||
import { LivePublisher, epochKeys, chainStep, linkEpochSeed } from '../packages/shade-media/src/live/channel.js';
|
||||
import { encodeContent, decodeFrame } from '../packages/shade-media/src/live/frame.js';
|
||||
import type { LiveContent } from '../packages/shade-media/src/live/frame.js';
|
||||
import { encodeGrant } from '../packages/shade-media/src/grant.js';
|
||||
import { ed25519PublicKeyFromSeed as edPub } from '../packages/shade-crypto-web/src/index.js';
|
||||
|
||||
const VECTOR_FILE_VERSION = 2;
|
||||
|
||||
@@ -975,6 +982,110 @@ async function generateVaultV2Vectors(): Promise<Vector[]> {
|
||||
return vectors;
|
||||
}
|
||||
|
||||
async function generateMediaVectors(): Promise<Vector[]> {
|
||||
// SMF1: everything is deterministic once the key and id are pinned —
|
||||
// segment nonces are the segment index. Small segments keep it readable.
|
||||
const mediaId = new Uint8Array(16).map((_, i) => 0xa0 + i);
|
||||
const mediaKey = new Uint8Array(32).map((_, i) => (i * 7 + 3) & 0xff);
|
||||
const sub = deriveMediaSubkeys(mediaKey, mediaId);
|
||||
const vectors: Vector[] = [
|
||||
{
|
||||
description: 'smf1 keys: segmentKey/metaKey = HKDF(mediaKey, salt = mediaId, "shade-media/v1\\0segment" | "…\\0meta")',
|
||||
mediaId: hex(mediaId),
|
||||
mediaKey: hex(mediaKey),
|
||||
segmentKey: hex(sub.segmentKey),
|
||||
metaKey: hex(sub.metaKey),
|
||||
},
|
||||
];
|
||||
const seg = 4096;
|
||||
const fill = (n: number, seed: number) => new Uint8Array(n).map((_, i) => (i * 131 + seed * 17 + (i >> 7)) & 0xff);
|
||||
const cases: Array<{ name: string; plaintext: Uint8Array; meta: Record<string, unknown> }> = [
|
||||
{ name: 'empty file — one empty last segment', plaintext: new Uint8Array(0), meta: {} },
|
||||
{ name: 'one partial segment', plaintext: new TextEncoder().encode('Shade media — æøå 🎬\n'), meta: { mime: 'text/plain', name: 'hei.txt' } },
|
||||
{ name: 'exact boundary — two full segments, the second is last', plaintext: fill(2 * seg, 1), meta: { mime: 'application/octet-stream' } },
|
||||
{
|
||||
name: 'three segments, last partial, full metadata',
|
||||
plaintext: fill(2 * seg + 1000, 2),
|
||||
meta: { mime: 'video/webm', name: 'Film.webm', durationMs: 5400000, width: 1920, height: 1080, createdAt: 1786600000000, extra: { series: 'S01E01' } },
|
||||
},
|
||||
];
|
||||
for (const c of cases) {
|
||||
const { bytes, result } = await sealWithPinnedKeys(c.plaintext, mediaId, mediaKey, { segmentSize: seg, meta: c.meta });
|
||||
vectors.push({
|
||||
description: `smf1 file: ${c.name}`,
|
||||
segmentSize: seg,
|
||||
meta: c.meta,
|
||||
metaJson: JSON.stringify(c.meta),
|
||||
plaintext: hex(c.plaintext),
|
||||
sealed: hex(bytes),
|
||||
rootDigest: hex(result.rootDigest),
|
||||
segmentCount: result.segmentCount,
|
||||
});
|
||||
}
|
||||
|
||||
// Live: link mode with pinned secrets and timestamps makes every frame
|
||||
// byte-for-byte reproducible (Ed25519 signatures are deterministic).
|
||||
const channelId = new Uint8Array(16).map((_, i) => 0x10 + i);
|
||||
const signingSecret = new Uint8Array(32).map((_, i) => 0x40 + i);
|
||||
const channelSecret = new Uint8Array(32).map((_, i) => 0x80 + i);
|
||||
const pub = new LivePublisher({ channelId, signingSecret, channelSecret, epoch: 0, meta: { name: 'kanal', mime: 'text/plain' } });
|
||||
const seed0 = linkEpochSeed(channelSecret, channelId, 0);
|
||||
const k0 = epochKeys(seed0, channelId, 0);
|
||||
const s0 = chainStep(k0.chainKey);
|
||||
const s1 = chainStep(s0.next);
|
||||
vectors.push({
|
||||
description: 'live keys: epochSeed = HKDF(channelSecret, salt = channelId, "shade-live/v1\\0epoch" || u32 epoch); chain/sticky = HKDF(epochSeed, channelId || u32 epoch, …); mk = HMAC(ck, 01), ck\' = HMAC(ck, 02)',
|
||||
channelId: hex(channelId),
|
||||
signingSecret: hex(signingSecret),
|
||||
signingKey: hex(await edPub(signingSecret)),
|
||||
channelSecret: hex(channelSecret),
|
||||
epoch: 0,
|
||||
epochSeed: hex(seed0),
|
||||
chainKey0: hex(k0.chainKey),
|
||||
stickyKey: hex(k0.stickyKey),
|
||||
messageKey0: hex(s0.messageKey),
|
||||
chainKey1: hex(s0.next),
|
||||
messageKey1: hex(s1.messageKey),
|
||||
epochSeed1: hex(linkEpochSeed(channelSecret, channelId, 1)),
|
||||
});
|
||||
const frames: Array<{ content: LiveContent; opts: { sticky?: boolean; keyframe?: boolean } }> = [
|
||||
{ content: { type: 'meta', meta: { name: 'kanal', mime: 'text/plain' } }, opts: { sticky: true } },
|
||||
{ content: { type: 'text', text: 'første linje\n' }, opts: { keyframe: true } },
|
||||
{ content: { type: 'init', mime: 'video/webm; codecs="vp8"', bytes: new Uint8Array([0x1a, 0x45, 0xdf, 0xa3]) }, opts: { sticky: true } },
|
||||
{ content: { type: 'chunk', bytes: fill(64, 3) }, opts: { keyframe: true } },
|
||||
{ content: { type: 'image', mime: 'image/jpeg', bytes: new Uint8Array([0xff, 0xd8, 0xff, 0xe0]) }, opts: {} },
|
||||
{ content: { type: 'binary', bytes: new Uint8Array([0, 1, 2, 255]) }, opts: {} },
|
||||
{ content: { type: 'end' }, opts: {} },
|
||||
];
|
||||
const grant = pub.grant({ from: 'now', link: true, url: 'wss://relay.example/v1/live/EBESExQVFhcYGRobHB0eHw', readToken: new Uint8Array(32).fill(0x55) });
|
||||
for (const [i, f] of frames.entries()) {
|
||||
const wire = await pub.frame(f.content, { ...f.opts, timestamp: 1_786_600_000_000 + i * 100 });
|
||||
vectors.push({
|
||||
description: `live frame: seq ${i} ${f.content.type}${f.opts.sticky ? ' (sticky — sealed under stickyKey)' : ''}`,
|
||||
seq: i,
|
||||
content: f.content.type,
|
||||
payload: hex(encodeContent(f.content)),
|
||||
wire: hex(wire),
|
||||
});
|
||||
}
|
||||
// One more epoch, so a port checks that link mode derives it unaided.
|
||||
const resealed = await pub.rotate();
|
||||
const after = await pub.frame({ type: 'text', text: 'etter rotasjon\n' }, { timestamp: 1_786_600_010_000 });
|
||||
vectors.push({
|
||||
description: `live frame: epoch 1 after rotate() — the ${resealed.length} sticky frames are resealed first (seq 0…${resealed.length - 1}); readable from the link grant alone`,
|
||||
epoch: 1,
|
||||
seq: decodeFrame(after).header.seq,
|
||||
content: 'text',
|
||||
payload: hex(encodeContent({ type: 'text', text: 'etter rotasjon\n' })),
|
||||
wire: hex(after),
|
||||
});
|
||||
vectors.push({
|
||||
description: 'live grant: wire JSON of a link grant (base64url fields, from: now at seq 0)',
|
||||
json: encodeGrant(grant),
|
||||
});
|
||||
return vectors;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('Generating cross-platform test vectors…');
|
||||
|
||||
@@ -992,6 +1103,7 @@ async function main() {
|
||||
['blob.json', { vectors: await generateBlobVectors() }],
|
||||
['approval.json', { vectors: await generateApprovalVectors() }],
|
||||
['vault-v2.json', { vectors: await generateVaultV2Vectors() }],
|
||||
['media.json', { vectors: await generateMediaVectors() }],
|
||||
];
|
||||
|
||||
const only = new Set(process.argv.slice(2));
|
||||
|
||||
Reference in New Issue
Block a user