Keyboard shortcuts

Press ← or → to navigate between chapters

Press ? to show this help

Press Esc to hide this help

Use the SDK

The Rust quantaegis-sdk crate and the Python quantaegis package provide application APIs for envelope encryption, signing, algorithm policies, and key rotation. Use them when your application needs to keep data readable across a change in keys or algorithms.

What do you want to do?API to use
Encrypt a message or stored record for a recipient and bind it to an order IDUse Envelope.seal and Envelope.open for authenticated encryption and associated data (AAD)
Switch to a new key or algorithm while keeping existing data readableUse Envelope.open_with_keyring and SecurityPolicy to control the transition and cutover
Replace a release signing key and verify signatures from both periodsUse Python’s AgileSignature or Rust’s AgileSigningKey and AgileVerifyingKey
Import a public key from an existing toolDecode its SPKI with the codec for that algorithm and convert the result to an SDK key
Handle capsules or signatures in a format defined by an existing protocolUse that protocol’s implementation and the raw-key or raw-signature APIs for the required algorithm

The high-level envelope and keyring APIs are available in Rust and Python. See binding support for the individual algorithms exposed through C and JS/WASM and the supported standard key formats.

Basic workflow

For encryption, the recipient generates a key pair and shares the public key through a trusted channel. The sender passes the public key, plaintext, and optional AAD to seal. The recipient uses their private key and the same AAD to open the result. For example, a different order ID causes the receiver to reject the message. The application keeps track of the AAD. Basic encryption example

Envelope uses QuantAegis’s QAEN v1 format. Use it where both sides can handle this format. inspect reads the algorithm from the envelope header. Use header metadata for routing and use the plaintext only after authenticated decryption succeeds. Python errors and policies · Envelope format in Rustdoc

Migrate stored envelopes to X-Wing

Suppose your service has existing orders encrypted with ML-KEM-768. By choosing policies separately for reads and writes, you can switch new writes to X-Wing while continuing to read stored data.

  1. Deploy both recipient keys to the readers first. During the transition, use CRYPTREC_CATEGORY3_PLUS to allow both ML-KEM-768 and X-Wing.
  2. Write new records with the X-Wing public key and HYBRID_REQUIRED.
  3. Authenticate and decrypt each stored record with its original AAD, then re-encrypt it with X-Wing. Check that you can open the replacement before writing it in place.
  4. Once you have accounted for the records and any backups you need, switch reads to HYBRID_REQUIRED. The original ML-KEM envelope now raises PolicyViolationError, even if its key is still in the ring.

Set up Python, then run the example from the repository root. It creates records in memory. Your application handles writes and replacement in its own data store.

python docs/examples/migrate_envelopes.py
from quantaegis import Envelope, PolicyViolationError, SecurityPolicy

transition = SecurityPolicy.CRYPTREC_CATEGORY3_PLUS
hybrid_only = SecurityPolicy.HYBRID_REQUIRED

# Stand in for an existing stored record and its recipient key.
old_public, old_secret = Envelope.generate_keypair("ml-kem-768", policy=transition)
old_aad = b"order=42"
old_packet = Envelope.seal(old_public, b"ship on Friday", aad=old_aad, policy=transition)

# Write new records with X-Wing. Keep both keys available for reads.
new_public, new_secret = Envelope.generate_keypair("x-wing", policy=hybrid_only)
keyring = [new_secret, old_secret]
new_aad = b"order=43"
new_packet = Envelope.seal(
    new_public, b"ship on Monday", aad=new_aad, policy=hybrid_only
)

old_plaintext = Envelope.open_with_keyring(
    keyring, old_packet, aad=old_aad, policy=transition
)
assert old_plaintext == b"ship on Friday"
assert Envelope.open_with_keyring(
    keyring, new_packet, aad=new_aad, policy=transition
) == b"ship on Monday"
print("Read both records during migration")

# Re-encrypt the authenticated old record and check the replacement before saving it.
replacement = Envelope.seal(new_public, old_plaintext, aad=old_aad, policy=hybrid_only)
assert Envelope.open(
    new_secret, replacement, aad=old_aad, policy=hybrid_only
) == old_plaintext
print("Re-encrypted the old record with X-Wing")

try:
    Envelope.open_with_keyring(keyring, old_packet, aad=old_aad, policy=hybrid_only)
except PolicyViolationError:
    print("Rejected the original ML-KEM envelope after cutover")
else:
    raise AssertionError("Expected the hybrid-only policy to reject the old envelope")

Run the same migration in Rust

The Rust example uses AgileKeyPair, SecurityPolicy, and quantaegis_core::Error.

cargo run --locked -p quantaegis-sdk --example envelope_migration
//! Migrate an existing ML-KEM envelope to X-Wing and enforce the new policy.

use quantaegis_core::{Error, Result};
use quantaegis_sdk::{AeadAlgorithm, AgileKeyPair, Envelope, KemAlgorithm, SecurityPolicy};

fn main() -> Result<()> {
    let transition = SecurityPolicy::CryptrecCategory3Plus;
    let hybrid_only = SecurityPolicy::HybridRequired;
    let old = AgileKeyPair::generate(transition, KemAlgorithm::MlKem768)?;
    let new = AgileKeyPair::generate(hybrid_only, KemAlgorithm::XWing)?;

    let aad = b"order=42";
    let old_packet = Envelope::seal(
        transition,
        &old.public,
        AeadAlgorithm::Aes256Gcm,
        b"ship on Friday",
        aad,
    )?;

    let keyring = [&new.secret, &old.secret];
    let plaintext = Envelope::open_with_keyring(transition, &keyring, &old_packet, aad)?;
    assert_eq!(plaintext, b"ship on Friday");
    let replacement = Envelope::seal(
        hybrid_only,
        &new.public,
        AeadAlgorithm::Aes256Gcm,
        &plaintext,
        aad,
    )?;

    assert_eq!(
        Envelope::open_with_keyring(hybrid_only, &keyring, &replacement, aad)?,
        plaintext
    );
    assert_eq!(
        Envelope::open_with_keyring(hybrid_only, &keyring, &old_packet, aad),
        Err(Error::PolicyViolation)
    );
    println!("Opened the X-Wing replacement and rejected the old envelope");
    Ok(())
}

Rotate a release signing key

This example switches a publisher from ML-DSA-65 to MLDSA65-Ed25519-SHA512. Distribute the new verification key through a trusted channel before publishing releases signed with it. During the transition, verifiers keep both verification keys and pass the same ctx when checking either signature. When signing an actual file, the publisher and verifier use the same file bytes.

python docs/examples/rotate_signing_keys.py
from quantaegis import AgileSignature, PolicyViolationError, SecurityPolicy

transition = SecurityPolicy.CRYPTREC_CATEGORY3_PLUS
hybrid_only = SecurityPolicy.HYBRID_REQUIRED
context = b"release-manifest-v1"

old_verify, old_sign = AgileSignature.generate_keypair("ml-dsa-65", policy=transition)
old_manifest = b"release=1.0"
old_signature = AgileSignature.sign(
    old_sign, old_manifest, ctx=context, policy=transition
)

new_verify, new_sign = AgileSignature.generate_keypair(
    "mldsa65-ed25519-sha512", policy=hybrid_only
)
new_manifest = b"release=2.0"
new_signature = AgileSignature.sign(
    new_sign, new_manifest, ctx=context, policy=hybrid_only
)

# Distribute the updated set of trusted verification keys before the cutover.
trusted_keys = [new_verify, old_verify]
for manifest, signature in (
    (old_manifest, old_signature),
    (new_manifest, new_signature),
):
    algorithm = AgileSignature.verify_with_keyring(
        trusted_keys, manifest, signature, ctx=context, policy=transition
    )
    print(f"Verified with {algorithm}")

AgileSignature.verify_with_keyring(
    trusted_keys, new_manifest, new_signature, ctx=context, policy=hybrid_only
)
try:
    AgileSignature.verify_with_keyring(
        trusted_keys, old_manifest, old_signature, ctx=context, policy=hybrid_only
    )
except PolicyViolationError:
    print("Rejected the earlier ML-DSA-only signature after cutover")
else:
    raise AssertionError("Expected the hybrid-only policy to reject the old signature")

HYBRID_REQUIRED restricts algorithms. To retire an earlier key for the same algorithm, remove it from the trusted keyring. If you still need to verify archived releases, retain the appropriate verification keys and policy for those releases. In this example, AgileSignature.sign returns a QuantAegis signature with an algorithm identifier. For raw ML-DSA signatures, see the individual signing API example. Rust signing API

Import an existing public key

When you receive an ML-KEM recipient key in SPKI PEM, first decode it into an algorithm name and raw public key. Then use AgileKem.public_from_raw to create the key representation accepted by Envelope.seal. This example generates a sample recipient key locally and exports the PEM that the sender would receive.

python docs/examples/import_recipient_key.py
from quantaegis import (
    AgileKem,
    Envelope,
    MlKem768,
    ml_kem_public_from_spki_pem,
    ml_kem_public_to_spki_pem,
)

# The recipient exports a raw public key for distribution as SPKI PEM.
raw_public, raw_secret = MlKem768.keygen()
recipient_pem = ml_kem_public_to_spki_pem("ml-kem-768", raw_public)

# The sender loads a trusted copy of the PEM and adapts the key for Envelope.
algorithm, raw_public = ml_kem_public_from_spki_pem(recipient_pem)
recipient = AgileKem.public_from_raw(algorithm, raw_public)
packet = Envelope.seal(recipient, b"ship on Friday", aad=b"order=42")

# The recipient adapts the private key locally and recovers the message.
secret = AgileKem.secret_from_raw("ml-kem-768", raw_secret)
assert Envelope.open(secret, packet, aad=b"order=42") == b"ship on Friday"
print("Opened an envelope addressed to an imported SPKI key")

This example exchanges a public key. For private-key migration and the ML-DSA and SLH-DSA codecs, see supported algorithms and key formats.

This version is a pre-1.0 evaluation release. See the security policy for API maturity, independent audit status, and verification coverage.