Skip to main content

ECDH-ES+A128KW crypto provider for Swarmauri

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_crypto_ecdh_es_a128kw


swarmauri_crypto_ecdh_es_a128kw

ECDH-ES+A128KW key wrapping provider for Swarmauri.

Highlights

  • Implements the JSON Web Encryption ECDH-ES key agreement combined with AES Key Wrap using a 128-bit KEK (ECDH-ES+A128KW).
  • Accepts KeyRef objects whose public attribute carries an EC public key in PEM format for wrapping and whose material attribute provides the corresponding private key for unwrapping.
  • Derives a one-time key-encryption key via Concat KDF with SHA-256 and serializes results as JSON containing the ephemeral public key (epk) and wrapped DEK (kw), both Base64URL encoded.
  • Generates a fresh 16-byte DEK when one is not provided so you can delegate symmetric key generation to the provider.

Installation

Choose the tool that matches your workflow:

# pip
pip install swarmauri_crypto_ecdh_es_a128kw

# Poetry
poetry add swarmauri_crypto_ecdh_es_a128kw

# uv
uv add swarmauri_crypto_ecdh_es_a128kw

Quickstart

The example below creates a recipient EC key pair, wraps a deterministic 128-bit DEK, and then unwraps it again to demonstrate the round trip. Run it with python quickstart.py or paste it into a REPL.

import asyncio
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec

from swarmauri_core.crypto.types import ExportPolicy, KeyRef, KeyType, KeyUse
from swarmauri_crypto_ecdh_es_a128kw import ECDHESA128KWCrypto


def make_recipient_key() -> KeyRef:
    private_key = ec.generate_private_key(ec.SECP256R1())
    public_key = private_key.public_key()

    return KeyRef(
        kid="recipient-key",
        version=1,
        type=KeyType.EC,
        uses=(KeyUse.WRAP, KeyUse.UNWRAP),
        export_policy=ExportPolicy.SECRET_WHEN_ALLOWED,
        material=private_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption(),
        ),
        public=public_key.public_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PublicFormat.SubjectPublicKeyInfo,
        ),
    )


async def main() -> None:
    crypto = ECDHESA128KWCrypto()
    recipient = make_recipient_key()
    dek = b"0123456789ABCDEF"  # 16 byte content encryption key

    wrapped = await crypto.wrap(recipient, dek=dek)
    recovered = await crypto.unwrap(recipient, wrapped)

    print("Wrapped payload:", wrapped.wrapped.decode("utf-8"))
    assert recovered == dek


if __name__ == "__main__":
    asyncio.run(main())

What to expect

  • wrap derives an ephemeral ECDH shared secret with the recipient public key, hashes it with Concat KDF (SHA-256) to produce a 128-bit KEK, and AES-KW wraps the provided DEK.
  • The returned WrappedKey stores a JSON document containing the ephemeral public key (epk) and the wrapped DEK (kw), both Base64URL encoded.
  • unwrap repeats the derivation using the recipient private key (KeyRef.material) and returns the original DEK bytes.

License

swarmauri_crypto_ecdh_es_a128kw is licensed under the Apache License 2.0. See the LICENSE file for details.

Entry point

The provider is registered under the swarmauri.cryptos entry point as ECDHESA128KWCrypto.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5.tar.gz.

File metadata

  • Download URL: swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5.tar.gz
Algorithm Hash digest
SHA256 59ded4038d53d28c0157369c17469de64eb6f415d0891133bb4aa2c6163d2d57
MD5 10ee14edcccc8d51241d0c078a888720
BLAKE2b-256 8fa6ae8ba4ef2eed595e28d8723c3358a6ffc8bec96058a9b173f9d6d4a52327

See more details on using hashes here.

File details

Details for the file swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for swarmauri_crypto_ecdh_es_a128kw-0.4.0.dev5-py3-none-any.whl
Algorithm Hash digest
SHA256 0db0241bd73fdbf6cf1b74a2fac51a3b76e6c7b3d0b1ef76423aef13ee3c72b3
MD5 d76b802c79dfaf3aacf53f7d01f4ae64
BLAKE2b-256 303e477dd116fd2607148dafd878d7b0958a22c55bdc90c6b0bcacc76b9ab81d

See more details on using hashes here.

Supported by

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