UNPKG

@hpke/core

Version:

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

184 lines (183 loc) 5.68 kB
(function (factory) { if (typeof module === "object" && typeof module.exports === "object") { var v = factory(require, exports); if (v !== undefined) module.exports = v; } else if (typeof define === "function" && define.amd) { define(["require", "exports", "@hpke/common"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Aes256Gcm = exports.Aes128Gcm = exports.AesGcmContext = void 0; const common_1 = require("@hpke/common"); class AesGcmContext extends common_1.NativeAlgorithm { constructor(key) { super(); Object.defineProperty(this, "_rawKey", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "_key", { enumerable: true, configurable: true, writable: true, value: undefined }); this._rawKey = key; } async seal(iv, data, aad) { await this._setupKey(); const alg = { name: "AES-GCM", iv: iv, additionalData: aad, }; const ct = await this._api.encrypt(alg, this._key, data); return ct; } async open(iv, data, aad) { await this._setupKey(); const alg = { name: "AES-GCM", iv: iv, additionalData: aad, }; const pt = await this._api.decrypt(alg, this._key, data); return pt; } async _setupKey() { if (this._key !== undefined) { return; } await this._setup(); const key = await this._importKey(this._rawKey); (new Uint8Array(this._rawKey)).fill(0); this._key = key; return; } async _importKey(key) { return await this._api.importKey("raw", key, { name: "AES-GCM" }, true, common_1.AEAD_USAGES); } } exports.AesGcmContext = AesGcmContext; /** * The AES-128-GCM for HPKE AEAD implementing {@link AeadInterface}. * * When using `@hpke/core`, the instance of this class must be specified * to the `aead` parameter of {@link CipherSuiteParams} instead of `AeadId.Aes128Gcm`. * * @example * * ```ts * import { * Aes128Gcm, * CipherSuite, * DhkemP256HkdfSha256, * HkdfSha256, * } from "@hpke/core"; * * const suite = new CipherSuite({ * kem: new DhkemP256HkdfSha256(), * kdf: new HkdfSha256(), * aead: new Aes128Gcm(), * }); * ``` */ class Aes128Gcm { constructor() { /** AeadId.Aes128Gcm (0x0001) */ Object.defineProperty(this, "id", { enumerable: true, configurable: true, writable: true, value: common_1.AeadId.Aes128Gcm }); /** 16 */ Object.defineProperty(this, "keySize", { enumerable: true, configurable: true, writable: true, value: 16 }); /** 12 */ Object.defineProperty(this, "nonceSize", { enumerable: true, configurable: true, writable: true, value: 12 }); /** 16 */ Object.defineProperty(this, "tagSize", { enumerable: true, configurable: true, writable: true, value: 16 }); } createEncryptionContext(key) { return new AesGcmContext(key); } } exports.Aes128Gcm = Aes128Gcm; /** * The AES-256-GCM for HPKE AEAD implementing {@link AeadInterface}. * * When using `@hpke/core`, the instance of this class must be specified * to the `aead` parameter of {@link CipherSuiteParams} instead of `AeadId.Aes256Gcm` * as follows: * * @example * * ```ts * import { * Aes256Gcm, * CipherSuite, * DhkemP256HkdfSha256, * HkdfSha256, * } from "@hpke/core"; * * const suite = new CipherSuite({ * kem: new DhkemP256HkdfSha256(), * kdf: new HkdfSha256(), * aead: new Aes256Gcm(), * }); * ``` */ class Aes256Gcm extends Aes128Gcm { constructor() { super(...arguments); /** AeadId.Aes256Gcm (0x0002) */ Object.defineProperty(this, "id", { enumerable: true, configurable: true, writable: true, value: common_1.AeadId.Aes256Gcm }); /** 32 */ Object.defineProperty(this, "keySize", { enumerable: true, configurable: true, writable: true, value: 32 }); /** 12 */ Object.defineProperty(this, "nonceSize", { enumerable: true, configurable: true, writable: true, value: 12 }); /** 16 */ Object.defineProperty(this, "tagSize", { enumerable: true, configurable: true, writable: true, value: 16 }); } } exports.Aes256Gcm = Aes256Gcm; });