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

@@ -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 } });
}