CryptGuard v3.0.1
CryptGuard is a pure-Rust post-quantum cryptography library. Version 3 uses a revision-pinned PQ HPKE protocol as its default encryption transport.
ML-KEM (FIPS 203) -> HPKE KEM and key schedule -> AEAD
The default suite is ML-KEM-1024/P-384 with SHAKE256 and ChaCha20-Poly1305. It prioritizes conservative security and explicit protocol boundaries over compact ciphertexts.
Contents
- Installation
- Choose a transport form
- Default encrypted envelope
- Raw Base-mode HPKE
- PSK-mode HPKE
- Suite selection
- Private AEAD extensions
- Envelope format and metadata
- Failure handling and security rules
- CGv2 migration
- Signatures and legacy support
- Feature flags
Installation
[]
= "3.0.1"
The default feature set includes the FIPS ML-KEM and ML-DSA backends. No
feature is necessary for the v3 pq_hpke API.
Choose a transport form
CryptGuard provides two deliberately separate transport forms.
| Form | Use it when | Contents |
|---|---|---|
| Raw HPKE | The protocol already negotiates suite and info out of band |
Separate enc and ciphertext |
HpkeEnvelope |
You need a crypt_guard self-describing record | Protocol magic, version, suite, enc, ciphertext |
Use raw transport only with RFC-style AEAD identifiers: AES-128-GCM,
AES-256-GCM, and ChaCha20-Poly1305. Use HpkeEnvelope for either the
standardized suites or CryptGuard private AEAD extensions.
info and AAD are intentionally not serialized inside HpkeEnvelope. The
sender and receiver must have the same application contract for both values.
Default encrypted envelope
This is the normal v3 starting point. DEFAULT_SUITE is
ML-KEM-1024/P-384 with SHAKE256 and ChaCha20-Poly1305.
use ;
Generate recipient key material once, protect the private key seed using your
key-management boundary, and distribute only the public key. A sender gets a
fresh encapsulation and a fresh HPKE context for every call to seal.
Raw Base-mode HPKE
Raw HPKE is appropriate when your protocol carries enc and ciphertext in
separate fields. The suite and info are negotiated or persisted by that
protocol, not guessed during decryption.
use ;
Sender and recipient contexts are stateful and intentionally not cloneable.
Each successful seal or open advances the HPKE message sequence and derives
the next nonce internally. Do not serialize a live context for later reuse.
PSK-mode HPKE
PSK mode binds an additional symmetric secret to HPKE setup. Both PSK bytes and their identifier are required. A PSK is not a replacement for recipient public key validation or authenticated application identity.
use ;
Base and PSK modes are supported. PQ authenticated KEM modes are deliberately not exposed by this API.
Suite selection
Select a suite explicitly whenever the default profile does not match your deployment. Persist the selected KEM, KDF, and AEAD identifiers with raw transport records.
use ;
let default_suite = DEFAULT_SUITE;
let pure_pq = new;
let hybrid_p256 = new;
let hybrid_x25519 = new;
let hybrid_p384 = new;
assert_eq!;
assert_eq!;
Supported KEM choices are ML-KEM-512, ML-KEM-768, ML-KEM-1024,
ML-KEM-768/P-256, ML-KEM-768/X25519, and ML-KEM-1024/P-384. The ML-KEM and
hybrid mappings are revision-pinned to draft-ietf-hpke-pq-05 and are not
final IANA assignments.
Private AEAD extensions
AES-256-GCM-SIV and XChaCha20-Poly1305 are available only in the CryptGuard envelope namespace. They use private AEAD identifiers and must not be sent to an implementation expecting an RFC 9180 or IANA suite identifier.
use ;
| AEAD | Identifier | Raw transport | HpkeEnvelope |
|---|---|---|---|
| AES-128-GCM | 0x0001 |
yes | yes |
| AES-256-GCM | 0x0002 |
yes | yes |
| ChaCha20-Poly1305 | 0x0003 |
yes | yes |
| AES-256-GCM-SIV | 0xff01 |
no | yes |
| XChaCha20-Poly1305 | 0xff02 |
no | yes |
Envelope format and metadata
HpkeEnvelope serializes a binary CGH3 record with envelope version 1.
It contains the protocol magic, version, KEM identifier, KDF identifier, AEAD
identifier, encapsulation length, ciphertext length, encapsulation, and
ciphertext. It does not contain plaintext, info, AAD, recipient private key
material, or a shared secret.
HpkeEnvelope::from_bytes validates framing and suite identifiers before a
receiver context is constructed. Parsing a CGv2 record as a CGH3 envelope
fails before decryption.
Failure handling and security rules
open returns an opaque Error::AuthenticationFailed for wrong AAD, wrong
info, modified ciphertext, and same-size modified encapsulations that reach
ML-KEM implicit rejection. Do not branch application behavior on which of those
conditions occurred.
use ;
Treat info as a stable setup context such as protocol version or service
name. Treat AAD as authenticated but unencrypted metadata such as tenant,
record type, object identifier, or sender routing data. Do not reuse a sender
context after its sequence is exhausted. Do not put secrets in AAD.
CGv2 migration
CGv2 is compatibility-only in v3. Default builds do not expose the legacy builders or accept CGv2 as a v3 envelope. Existing stored data requires an explicit migration build.
[]
= { = "3.0.1", = ["cgv2-compat"] }
Migration procedure:
- Deploy a migration worker with
cgv2-compatenabled. - Read and authenticate the existing CGv2 envelope using the compatibility API.
- Re-encrypt the recovered plaintext with
pq_hpke::HpkeEnvelope. - Store the
CGH3bytes and preserve the application contract forinfoand AAD. - Remove
cgv2-compatafter all stored data has been migrated.
Never trial-decrypt unknown records with both formats. Store or transmit a transport discriminator and select the reader directly.
Signatures and legacy support
ML-DSA remains available in the default feature set.
use OsRng;
use ;
let mut rng = OsRng;
let = keypair?;
let signature = sign?;
verify?;
# Ok::
The legacy-pqclean feature retains the historical Kyber, Falcon, and
Dilithium path for explicitly managed legacy data. It is separate from
cgv2-compat, which controls the v2 envelope and builder compatibility path.
Feature flags
| Feature | Default | Purpose |
|---|---|---|
ml-kem-backend |
yes | FIPS ML-KEM-512, ML-KEM-768, ML-KEM-1024 |
ml-dsa-backend |
yes | FIPS ML-DSA-44, ML-DSA-65, ML-DSA-87 |
sign-slhdsa |
no | SLH-DSA signatures |
cgv2-compat |
no | CGv2 envelope, builders, and compatibility helpers |
legacy-pqclean |
no | Historical Kyber, Falcon, and Dilithium compatibility |
archive |
no | Archive helpers |
zip |
no | ZIP helpers |
aes-ctr |
no | Legacy AES-CTR support |
aes-xts |
no | Legacy AES-XTS support |