Key Encapsulation Mechanism (KEM) implementation interface.
This implementation interface defines the contract for additional KEM implementations to be usable with CipherSuite. While this module provides built-in KEM implementations based on Web Cryptography, this interface is exported to allow custom KEM implementations that may not rely on Web Cryptography (e.g., using native bindings, alternative crypto libraries, or specialized hardware).
Custom KEM implementations must conform to this interface to be compatible with CipherSuite and its APIs.
import * as HPKE from 'hpke'
// Using a built-in KEM
const suite = new HPKE.CipherSuite(
HPKE.KEM_DHKEM_P256_HKDF_SHA256,
HPKE.KDF_HKDF_SHA256,
HPKE.AEAD_AES_128_GCM,
)
// Creating and using a custom KEM implementation
const customKEM: HPKE.KEMFactory = (): HPKE.KEM => ({
id: 0x9999,
type: 'KEM',
name: 'Custom-KEM',
Nsecret: 32,
Nenc: 32,
Npk: 32,
Nsk: 32,
async DeriveKeyPair(ikm, extractable) {
// perform DeriveKeyPair
let kp!: HPKE.KeyPair
return kp
},
async GenerateKeyPair(extractable) {
// perform GenerateKeyPair
let kp!: HPKE.KeyPair
return kp
},
async SerializePublicKey(key) {
// perform SerializePublicKey
let publicKey!: Uint8Array
return publicKey
},
async DeserializePublicKey(key) {
// perform DeserializePublicKey
let publicKey!: HPKE.Key
return publicKey
},
async SerializePrivateKey(key) {
// perform SerializePrivateKey
let privateKey!: Uint8Array
return privateKey
},
async DeserializePrivateKey(key, extractable) {
// perform DeserializePrivateKey
let privateKey!: HPKE.Key
return privateKey
},
async Encap(pkR) {
// perform Encap
let shared_secret!: Uint8Array
let enc!: Uint8Array
return { shared_secret, enc }
},
async Decap(enc, skR, pkR) {
// perform Decap
let shared_secret!: Uint8Array
return shared_secret
},
})
const customSuite = new HPKE.CipherSuite(
customKEM,
HPKE.KDF_HKDF_SHA256,
HPKE.AEAD_AES_128_GCM,
)HPKE Key Encapsulation Mechanisms
Decap(
enc,skR,pkR):Promise<Uint8Array>
Decapsulates a shared secret using a recipient's private key.
This is the recipient-side operation that uses the private key to extract the shared secret from the encapsulated secret.
| Parameter | Type | Description |
|---|---|---|
enc |
Uint8Array |
The encapsulated secret of Nenc length |
skR |
Key |
The recipient's private key |
pkR |
Key ∣ undefined |
The recipient's public key (when user input to CipherSuite.SetupRecipient is a KeyPair) |
Promise<Uint8Array>
A promise resolving to the shared secret
DeriveKeyPair(
ikm,extractable):Promise<KeyPair>
Derives a key pair deterministically from input keying material.
| Parameter | Type | Description |
|---|---|---|
ikm |
Uint8Array |
Input keying material already validated to be at least Nsk bytes |
extractable |
boolean |
Whether the private key should be extractable |
Promise<KeyPair>
A promise resolving to a KeyPair
DeserializePrivateKey(
key,extractable):Promise<Key>
Deserializes a private key from bytes.
| Parameter | Type | Description |
|---|---|---|
key |
Uint8Array |
The serialized private key already validated to be exactly Nsk bytes |
extractable |
boolean |
Whether the private key should be extractable |
Promise<Key>
A promise resolving to a !Key or a Key interface-conforming object
DeserializePublicKey(
key):Promise<Key>
Deserializes a public key from bytes.
| Parameter | Type | Description |
|---|---|---|
key |
Uint8Array |
The serialized public key already validated to be exactly Npk bytes |
Promise<Key>
A promise resolving to a !Key or a Key interface-conforming object
Encap(
pkR):Promise<{enc:Uint8Array;shared_secret:Uint8Array; }>
Encapsulates a shared secret to a recipient's public key.
This is the sender-side operation that generates an ephemeral key pair, performs the KEM operation, and returns both the shared secret and the encapsulated secret to send to the recipient.
| Parameter | Type | Description |
|---|---|---|
pkR |
Key |
The recipient's public key |
Promise<{ enc: Uint8Array; shared_secret: Uint8Array; }>
A promise resolving to an object containing the shared secret and encapsulated secret
GenerateKeyPair(
extractable):Promise<KeyPair>
Generates a random key pair.
| Parameter | Type | Description |
|---|---|---|
extractable |
boolean |
Whether the private key should be extractable |
Promise<KeyPair>
A promise resolving to a KeyPair
SerializePrivateKey(
key):Promise<Uint8Array>
Serializes a private key to bytes.
| Parameter | Type | Description |
|---|---|---|
key |
Key |
The private Key to serialize |
Promise<Uint8Array>
A promise resolving to the serialized private key
SerializePublicKey(
key):Promise<Uint8Array>
Serializes a public key to bytes.
| Parameter | Type | Description |
|---|---|---|
key |
Key |
The public Key to serialize |
Promise<Uint8Array>
A promise resolving to the serialized public key
readonlyid:number
KEM algorithm identifier
readonlyname:string
Human-readable name of the KEM algorithm
readonlyNenc:number
Length in bytes of an encapsulated secret produced by this KEM
readonlyNpk:number
Length in bytes of a public key for this KEM
readonlyNsecret:number
Length in bytes of a KEM shared secret produced by this KEM
readonlyNsk:number
Length in bytes of a private key for this KEM
readonlytype:"KEM"
Type discriminator, always 'KEM'