Skip to main content

A Python implementation of HPKE.

Project description

PyHPKE - A Python implementation of HPKE

PyPI version PyPI - Python Version Documentation Status Github CI codecov

PyHPKE is a HPKE (Hybrid Public Key Encryption) implementation written in Python.

You can install PyHPKE with pip:

$ pip install pyhpke

And then, you can use it as follows:

from pyhpke import AEADId, CipherSuite, KDFId, KEMId, KEMKey

# The sender side:
suite_s = CipherSuite.new(
    KEMId.DHKEM_P256_HKDF_SHA256, KDFId.HKDF_SHA256, AEADId.AES128_GCM
)
pkr = KEMKey.from_jwk(  # from_pem is also available.
    {
        "kid": "01",
        "kty": "EC",
        "crv": "P-256",
        "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0",
        "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw",
    }
)
enc, sender = suite_s.create_sender_context(pkr)
ct = sender.seal(b"Hello world!")

# The recipient side:
suite_r = CipherSuite.new(
    KEMId.DHKEM_P256_HKDF_SHA256, KDFId.HKDF_SHA256, AEADId.AES128_GCM
)
skr = KEMKey.from_jwk(
    {
        "kid": "01",
        "kty": "EC",
        "crv": "P-256",
        "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0",
        "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw",
        "d": "r_kHyZ-a06rmxM3yESK84r1otSg-aQcVStkRhA-iCM8",
    }
)
recipient = suite_r.create_recipient_context(enc, skr)
pt = recipient.open(ct)

assert pt == b"Hello world!"


# deriving a KEMKeyPair
keypair = suite_s.kem.derive_key_pair(b"some_ikm_bytes_used_for_key_derivation")

Index

Installation

You can install PyHPKE with pip:

$ pip install pyhpke

Supported HPKE Modes and Cipher Suites

PyHPKE supports all of the HPKE modes and cipher suites defined in RFC9180 below.

  • modes
    • ✅ Base
    • ✅ PSK
    • ✅ Auth
    • ✅ AuthPSK
  • KEMs (Key Encapsulation Machanisms)
    • ✅ DHKEM (P-256, HKDF-SHA256)
    • ✅ DHKEM (P-384, HKDF-SHA384)
    • ✅ DHKEM (P-521, HKDF-SHA512)
    • ✅ DHKEM (X25519, HKDF-SHA256)
    • ✅ DHKEM (X448, HKDF-SHA512)
  • KDFs (Key Derivation Functions)
    • ✅ HKDF-SHA256
    • ✅ HKDF-SHA384
    • ✅ HKDF-SHA512
  • AEADs (Authenticated Encryption with Associated Data)
    • ✅ AES-128-GCM
    • ✅ AES-256-GCM
    • ✅ ChaCha20Poly1305
    • ✅ Export Only

Warnings and Restrictions

Although this library has been passed all of the following official test vectors, it has not been formally audited.

Usage

from pyhpke import AEADId, CipherSuite, KDFId, KEMId, KEMKey

# The sender side:
suite_s = CipherSuite.new(
    KEMId.DHKEM_P256_HKDF_SHA256, KDFId.HKDF_SHA256, AEADId.AES128_GCM
)
pkr = KEMKey.from_jwk(
    {
        "kid": "01",
        "kty": "EC",
        "crv": "P-256",
        "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0",
        "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw",
    }
)
enc, sender = suite_s.create_sender_context(pkr)
ct = sender.seal(b"Hello world!")

# The recipient side:
suite_r = CipherSuite.new(
    KEMId.DHKEM_P256_HKDF_SHA256, KDFId.HKDF_SHA256, AEADId.AES128_GCM
)
skr = KEMKey.from_jwk(
    {
        "kid": "01",
        "kty": "EC",
        "crv": "P-256",
        "x": "Ze2loSV3wrroKUN_4zhwGhCqo3Xhu1td4QjeQ5wIVR0",
        "y": "HlLtdXARY_f55A3fnzQbPcm6hgr34Mp8p-nuzQCE0Zw",
        "d": "r_kHyZ-a06rmxM3yESK84r1otSg-aQcVStkRhA-iCM8",
    }
)
recipient = suite_r.create_recipient_context(enc, skr)
pt = recipient.open(ct)

assert pt == b"Hello world!"

API Reference

See Documentation.

Test

You can run tests from the project root after cloning with:

$ tox

Contributing

We welcome all kind of contributions, filing issues, suggesting new features or sending PRs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyhpke-0.5.4.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

pyhpke-0.5.4-py3-none-any.whl (51.4 kB view details)

Uploaded Python 3

File details

Details for the file pyhpke-0.5.4.tar.gz.

File metadata

  • Download URL: pyhpke-0.5.4.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1025-azure

File hashes

Hashes for pyhpke-0.5.4.tar.gz
Algorithm Hash digest
SHA256 738ce16d5be68766f9d8726ae62328abc8c7f09b9935990dfa1b7f5e20d7e7cc
MD5 a3f7b0f1f065ebbab6862095f9980fee
BLAKE2b-256 d19edf04f7575e693223d3523da7426ee43734ed5f62f553a525b70b56fd820f

See more details on using hashes here.

File details

Details for the file pyhpke-0.5.4-py3-none-any.whl.

File metadata

  • Download URL: pyhpke-0.5.4-py3-none-any.whl
  • Upload date:
  • Size: 51.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1025-azure

File hashes

Hashes for pyhpke-0.5.4-py3-none-any.whl
Algorithm Hash digest
SHA256 429ed8339df7a1a51de6493a638a7dd2831c1a21065ffa8344d8851df3918826
MD5 1cd1f107e519ea62db3af40eb06182e5
BLAKE2b-256 89d137f87c5c5625c4985c190be1696c7ed8bc04259a96dbe082bf7654cac6fe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page