UNPKG

@hpke/core

Version:

A Hybrid Public Key Encryption (HPKE) core module for various JavaScript runtimes

63 lines (62 loc) 2.02 kB
import { ExportError, INPUT_LENGTH_LIMIT, InvalidParamError, toArrayBuffer, } from "@hpke/common"; import { emitNotSupported } from "./utils/emitNotSupported.js"; // b"sec" const LABEL_SEC = new Uint8Array([115, 101, 99]); export class ExporterContextImpl { constructor(api, kdf, exporterSecret) { Object.defineProperty(this, "_api", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "exporterSecret", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_kdf", { enumerable: true, configurable: true, writable: true, value: void 0 }); this._api = api; this._kdf = kdf; this.exporterSecret = exporterSecret; } async seal(_data, _aad) { return await emitNotSupported(); } async open(_data, _aad) { return await emitNotSupported(); } async export(exporterContext, len) { const rawExporterContext = toArrayBuffer(exporterContext); if (rawExporterContext.byteLength > INPUT_LENGTH_LIMIT) { throw new InvalidParamError("Too long exporter context"); } try { return await this._kdf.labeledExpand(this.exporterSecret, LABEL_SEC, new Uint8Array(rawExporterContext), len); } catch (e) { throw new ExportError(e); } } } export class RecipientExporterContextImpl extends ExporterContextImpl { } export class SenderExporterContextImpl extends ExporterContextImpl { constructor(api, kdf, exporterSecret, enc) { super(api, kdf, exporterSecret); Object.defineProperty(this, "enc", { enumerable: true, configurable: true, writable: true, value: void 0 }); this.enc = enc; return; } }