UNPKG

@hpke/chacha20poly1305

Version:

A Hybrid Public Key Encryption (HPKE) module extension for ChaCha20/Poly1305

111 lines (110 loc) 3.99 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", "./chacha/chacha.js", "@hpke/common"], factory); } })(function (require, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Chacha20Poly1305 = exports.Chacha20Poly1305Context = void 0; const chacha_js_1 = require("./chacha/chacha.js"); const common_1 = require("@hpke/common"); class Chacha20Poly1305Context { constructor(key) { Object.defineProperty(this, "_key", { enumerable: true, configurable: true, writable: true, value: void 0 }); this._key = new Uint8Array((0, common_1.toArrayBuffer)(key)); } async seal(iv, data, aad) { return await this._seal((0, common_1.toArrayBuffer)(iv), (0, common_1.toArrayBuffer)(data), (0, common_1.toArrayBuffer)(aad)); } async open(iv, data, aad) { return await this._open((0, common_1.toArrayBuffer)(iv), (0, common_1.toArrayBuffer)(data), (0, common_1.toArrayBuffer)(aad)); } _seal(iv, data, aad) { return new Promise((resolve) => { const ret = (0, chacha_js_1.chacha20poly1305)(this._key, new Uint8Array(iv), new Uint8Array(aad)).encrypt(new Uint8Array(data)); resolve(ret.buffer); }); } _open(iv, data, aad) { return new Promise((resolve) => { const ret = (0, chacha_js_1.chacha20poly1305)(this._key, new Uint8Array(iv), new Uint8Array(aad)).decrypt(new Uint8Array(data)); resolve(ret.buffer); }); } } exports.Chacha20Poly1305Context = Chacha20Poly1305Context; /** * The ChaCha20Poly1305 for HPKE AEAD implementing {@link AeadInterface}. * * When using `@hpke/core`, the instance of this class can be specified * to the `aead` parameter of {@link CipherSuiteParams} instead of `AeadId.Chacha20Poly1305` * as follows: * * @example * * ```ts * import { * CipherSuite, * DhkemP256HkdfSha256, * HkdfSha256, * } from "@hpke/core"; * import { * Chacha20Poly1305, * } from "@hpke/chacha20poly1305"; * * const suite = new CipherSuite({ * kem: new DhkemP256HkdfSha256(), * kdf: new HkdfSha256(), * aead: new Chacha20Poly1305(), * }); * ``` * * This class is implemented using * {@link https://github.com/paulmillr/noble-ciphers | @noble/ciphers}. */ class Chacha20Poly1305 { constructor() { /** AeadId.Chacha20Poly1305 (0x0003) */ Object.defineProperty(this, "id", { enumerable: true, configurable: true, writable: true, value: common_1.AeadId.Chacha20Poly1305 }); /** 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 }); } createEncryptionContext(key) { return new Chacha20Poly1305Context(key); } } exports.Chacha20Poly1305 = Chacha20Poly1305; });