Hybrid PKE
The Hybrid Public Key Encryption (HPKE) standard in Python.
hybrid_pke
= hpke-rs
:heavy_plus_sign: PyO3
This library provides Python bindings to the hpke-rs
crate, which supports primitives from either Rust Crypto or EverCrypt.
Table of Contents
- Usage
- Features
- Installation
- Development
- Related Projects
Usage
Basic
The single-shot API is intended for single message encryption/decryption. The default HPKE configuration uses the unauthenticated Base mode, an X25519 DH key encapsulation mechanism, a SHA256 key derivation mechanism, and a ChaCha20Poly1305 AEAD function.
import hybrid_pke
hpke = hybrid_pke.default()
info = b""
aad = b""
secret_key_r, public_key_r = hpke.generate_key_pair()
message = b"hello from the other side!"
encap, ciphertext = hpke.seal(public_key_r, info, aad, message)
plaintext = hpke.open(encap, secret_key_r, info, aad, ciphertext)
print(plaintext.decode("utf-8"))
Advanced
Sender & Receiver Contexts
The Sender Context and Receiver Context APIs allow for setting up a context for repeated encryptions and decryptions. It's recommended whenever you intend to perform several encryptions or decryptions in quick succession.
info = b"quotes from your favorite aphorists"
aads = [
b"Szasz",
b"Nietzsche",
b"Morandotti",
b"Brudzinski",
b"Hubbard",
]
messages = [
b"Two wrongs don't make a right, but they make a good excuse.",
b"Become who you are!",
b"Only those who aren't hungry are able to judge the quality of a meal.",
b"Under certain circumstances a wanted poster is a letter of recommendation.",
b"Nobody ever forgets where he buried the hatchet.",
]
encap, sender_context = hpke.setup_sender(public_key_r, info)
ciphertexts = []
for aad, msg in zip(aads, messages):
ciphertext = sender_context.seal(aad, msg)
ciphertexts.append(ciphertext)
receiver_context = hpke.setup_receiver(encap, secret_key_r, info)
plaintexts = []
for aad, ctxt in zip(aads, ciphertexts):
plaintext = receiver_context.open(aad, ctxt)
plaintexts.append(plaintext)
print(f"\"{plaintexts[0].decode()}\" - {aad[0].decode()}")
print(f"\"{plaintexts[1].decode()}\" - {aad[1].decode()}")
Mode.AUTH: Authenticated Sender
Auth mode allows for signing and verifying encryptions with a previously authenticated sender key-pair.
hpke = hybrid_pke.default(mode=hybrid_pke.Mode.AUTH)
secret_key_r, public_key_r = hpke.generate_key_pair()
secret_key_s, public_key_s = hpke.generate_key_pair()
encap, ciphertext = hpke.seal(public_key_r, info, aad, message, sk_s=secret_key_s)
plaintext = hpke.open(encap, secret_key_r, info, aad, ciphertext, pk_s=public_key_s)
Mode.PSK: Pre-shared Key Authentication
PSK mode allows for signing and verifying encryptions with a previously shared key held by both the sender and recipient.
hpke = hybrid_pke.default(mode=hybrid_pke.Mode.PSK)
psk = bytes.fromhex("0247fd33b913760fa1fa51e1892d9f307fbe65eb171e8132c2af18555a738b82")
psk_id = bytes.fromhex("456e6e796e20447572696e206172616e204d6f726961")
encap, ciphertext = hpke.seal(public_key_r, info, aad, message, psk=psk, psk_id=psk_id)
plaintext = hpke.open(encap, secret_key_r, info, aad, ciphertext, psk=psk, psk_id=psk_id)
Mode.AUTH_PSK: Combining AUTH and PSK.
PSK mode allows for signing and verifying encryptions with a previously shared key held by both the sender and recipient.
hpke = hybrid_pke.default(mode=hybrid_pke.Mode.PSK)
secret_key_r, public_key_r = hpke.generate_key_pair()
secret_key_s, public_key_s = hpke.generate_key_pair()
psk = bytes.fromhex("0247fd33b913760fa1fa51e1892d9f307fbe65eb171e8132c2af18555a738b82")
psk_id = bytes.fromhex("456e6e796e20447572696e206172616e204d6f726961")
encap, ciphertext = hpke.seal(
public_key_r, info, aad, message,
psk=psk, psk_id=psk_id, sk_s=secret_key_s,
)
plaintext = hpke.open(
encap, secret_key_r, info, aad, ciphertext,
psk=psk, psk_id=psk_id, pk_s=public_key_s,
)
(back to top)
Features
The features available match those supported by hpke-rs
.
HPKE Modes
KEMs: (Diffie-Hellman) Key Encapsulation Mechanisms
KDFs: Key Derivation Functions
AEADs: Authenticated Encryption with Additional Data functions
(back to top)
Installation
Wheels for various platforms and architectures can be found on PyPI or in the wheelhouse.zip
archive from the latest Github release.
The library can also be installed from source with maturin
-- see below.
(back to top)
Development
We use maturin
to build and distribute the PyO3 extension module as a Python wheel.
For users of cmake
, we provide a Makefile
that includes some helpful development commands.
Some useful tips
maturin develop
builds & installs the Python package into your Python environment (venv
or conda
recommended)pytest .
tests the resulting Python package.pytest -n auto .
runs the full test suite in parallel.maturin build --release -o dist --sdist
builds the extension module in release-mode and produces a wheel for your environment's OS and architecture.- The
-i
/--interpreter
flag for maturin
can be used to swap out different Python interpreters, if you have multiple Python installations.
(back to top)
Releasing
We use cargo-release
to manage release commits and git tags. Our versioning follows SemVer, and after every release we immediately bump to a prerelease version with the -dev0
suffix.
Example release flow
$ git checkout main
$ cargo release patch --execute
Upgrading hybrid_pke from X.X.X-dev0 to X.X.X
Replacing in pyproject.toml
--- pyproject.toml original
+++ pyproject.toml replaced
@@ -8 +8 @@
-version = "X.X.X-dev0" # NOTE: auto-updated during release
+version = "X.X.X" # NOTE: auto-updated during release
$ cargo release X.X.Y-dev0 --no-tag
Upgrading hybrid_pke from X.X.X to X.X.Y-dev0
Replacing in pyproject.toml
--- pyproject.toml original
+++ pyproject.toml replaced
@@ -8 +8 @@
-version = "X.X.X" # NOTE: auto-updated during release
+version = "X.X.Y-dev0" # NOTE: auto-updated during release
$ git push origin main
$ git push origin vX.X.X
(back to top)
Related Projects
(back to top)