@hpke/dhkem-x25519
Version:
A Hybrid Public Key Encryption (HPKE) module extension for X25519
84 lines (83 loc) • 2.42 kB
JavaScript
import { Dhkem, KemId, SerializeError, XCurveDhkemPrimitives, } from "@hpke/common";
import { x25519 } from "./primitives/x25519.js";
import { HkdfSha256 } from "./hkdfSha256.js";
export class X25519 extends XCurveDhkemPrimitives {
constructor(hkdf) {
super("X25519", 32, x25519, hkdf);
}
derive(sk, pk) {
try {
return Promise.resolve(this._curve.getSharedSecret(sk, pk));
}
catch (e) {
return Promise.reject(new SerializeError(e));
}
}
}
/**
* The DHKEM(X25519, HKDF-SHA256) for HPKE KEM implementing {@link KemInterface}.
*
* This class is implemented using
* {@link https://github.com/paulmillr/noble-curves | @noble/curves}.
*
* The instance of this class can be specified to the
* {@link https://jsr.io/@hpke/core/doc/~/CipherSuiteParams | CipherSuiteParams} as follows:
*
* @example Use with `@hpke/core`:
*
* ```ts
* import {
* Aes128Gcm,
* CipherSuite,
* HkdfSha256,
* } from "@hpke/core";
* import { DhkemX25519HkdfSha256 } from "@hpke/dhkem-x25519";
*
* const suite = new CipherSuite({
* kem: new DhkemX25519HkdfSha256(),
* kdf: new HkdfSha256(),
* aead: new Aes128Gcm(),
* });
* ```
*/
export class DhkemX25519HkdfSha256 extends Dhkem {
constructor() {
const kdf = new HkdfSha256();
super(KemId.DhkemX25519HkdfSha256, new X25519(kdf), kdf);
/** KemId.DhkemX25519HkdfSha256 (0x0020) */
Object.defineProperty(this, "id", {
enumerable: true,
configurable: true,
writable: true,
value: KemId.DhkemX25519HkdfSha256
});
/** 32 */
Object.defineProperty(this, "secretSize", {
enumerable: true,
configurable: true,
writable: true,
value: 32
});
/** 32 */
Object.defineProperty(this, "encSize", {
enumerable: true,
configurable: true,
writable: true,
value: 32
});
/** 32 */
Object.defineProperty(this, "publicKeySize", {
enumerable: true,
configurable: true,
writable: true,
value: 32
});
/** 32 */
Object.defineProperty(this, "privateKeySize", {
enumerable: true,
configurable: true,
writable: true,
value: 32
});
}
}