Hybrid Public Key Encryption (HPKE) implementation for JavaScript runtimes.
Implements an authenticated encryption encapsulation format that combines a semi-static
asymmetric key exchange with a symmetric cipher. This was originally defined in an Informational
document on the IRTF stream as RFC 9180 and is now
being republished as a Standards Track document of the IETF as
draft-ietf-hpke-hpke.
HPKE provides a variant of public key encryption for arbitrary-sized plaintexts using a recipient
public key.
Getting started with CipherSuite
import * as HPKE from 'hpke'
// 1. Choose a cipher suite
const suite = new HPKE.CipherSuite(
HPKE.KEM_DHKEM_P256_HKDF_SHA256,
HPKE.KDF_HKDF_SHA256,
HPKE.AEAD_AES_128_GCM,
)
// 2. Generate recipient key pair
const recipient = await suite.GenerateKeyPair()
// 3. Encrypt a message
const plaintext = new TextEncoder().encode('Hello, World!')
const { encapsulatedSecret, ciphertext } = await suite.Seal(recipient.publicKey, plaintext)
// 4. Decrypt the message
const decrypted = await suite.Open(recipient.privateKey, encapsulatedSecret, ciphertext)
console.log(new TextDecoder().decode(decrypted)) // "Hello, World!"
| Name |
Description |
| CipherSuite |
Hybrid Public Key Encryption (HPKE) suite combining a KEM, KDF, and AEAD. |
| RecipientContext |
Context for decrypting multiple messages and exporting secrets on the recipient side. |
| SenderContext |
Context for encrypting multiple messages and exporting secrets on the sender side. |
| Interface |
Description |
| AEAD |
Authenticated Encryption with Associated Data (AEAD) implementation interface. |
| KDF |
Key Derivation Function (KDF) implementation interface. |
| KEM |
Key Encapsulation Mechanism (KEM) implementation interface. |
| Key |
A minimal key representation interface. |
| KeyPair |
Represents a cryptographic key pair consisting of a public key and private key. |
| Type Alias |
Description |
| AEADFactory |
Factory function that returns an AEAD implementation. |
| CryptoKey |
A Web Cryptography key as declared by the host runtime. |
| KDFFactory |
Factory function that returns a KDF implementation. |
| KEMFactory |
Factory function that returns a KEM implementation. |
| Mode |
Mode supported by this implementation. |
| Function |
Description |
| concat |
Concatenates multiple Uint8Array buffers into a single Uint8Array. It's exported for use in custom KEM, KDF, or AEAD implementations. |
| encode |
Encodes an ASCII string into a Uint8Array. |
| I2OSP |
Integer to Octet String Primitive (I2OSP) as defined in RFC 8017. Converts a non-negative integer into a byte string of specified length. It's exported for use in custom KEM, KDF, or AEAD implementations. |
| LabeledDerive |
Performs labeled key derivation for one-stage KDFs. |
| LabeledExpand |
Performs labeled expansion for two-stage KDFs. |
| LabeledExtract |
Performs labeled extraction for two-stage KDFs. |
| Variable |
Description |
| MODE_BASE |
Mode identifier for Base mode (0x00). |
| MODE_PSK |
Mode identifier for PSK mode (0x01). |