@hpke/core
Version:
A Hybrid Public Key Encryption (HPKE) core module for various JavaScript runtimes
168 lines (167 loc) • 4.39 kB
JavaScript
import { AEAD_USAGES, AeadId, NativeAlgorithm } from "@hpke/common";
export class AesGcmContext extends 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, AEAD_USAGES);
}
}
/**
* 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(),
* });
* ```
*/
export class Aes128Gcm {
constructor() {
/** AeadId.Aes128Gcm (0x0001) */
Object.defineProperty(this, "id", {
enumerable: true,
configurable: true,
writable: true,
value: 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);
}
}
/**
* 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(),
* });
* ```
*/
export class Aes256Gcm extends Aes128Gcm {
constructor() {
super(...arguments);
/** AeadId.Aes256Gcm (0x0002) */
Object.defineProperty(this, "id", {
enumerable: true,
configurable: true,
writable: true,
value: 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
});
}
}