How pyca/cryptography works
Purpose and entry points
cryptography is a Python package providing high-level cryptographic recipes plus lower-level hazardous-material primitives. Public import surfaces begin at src/cryptography/__init__.py; the primary recipe is src/cryptography/fernet.py, while most APIs live under src/cryptography/hazmat/primitives/ and src/cryptography/x509/. Packaging/build metadata is in pyproject.toml.
Public API layers
The top-level API separates ergonomic recipes from composable primitives. Fernet provides authenticated symmetric-encryption tokens; primitives cover ciphers/AEAD, hashes, MACs, KDFs, asymmetric keys, serialization, HPKE, and deprecated algorithms.
• src/cryptography/fernet.py
• src/cryptography/hazmat/primitives/ciphers/aead.py
• src/cryptography/hazmat/primitives/asymmetric/rsa.py
Control flow generally starts in Python API objects, validates Python inputs and lifecycle constraints, then dispatches to compiled Rust/OpenSSL-backed implementations.
Compiled backend boundary
The primary execution boundary is Python ↔ native extension. Python exposes typed Rust-extension modules and an OpenSSL binding; the Rust workspace implements backend operations and owns much parsing/encoding and native error translation.
• src/cryptography/hazmat/bindings/openssl/binding.py
• src/cryptography/hazmat/bindings/_rust/openssl/aead.pyi
• src/rust/src/lib.rs
OpenSSL operations are implemented through Rust backend modules and the OpenSSL-facing crate, with CFFI retained for binding construction/support.
• src/rust/src/backend/mod.rs
• src/rust/cryptography-openssl/src/lib.rs
• src/rust/cryptography-cffi/src/lib.rs
Key, ASN.1, and certificate pipeline
Serialization inputs (PEM/DER/OpenSSH/PKCS#8/PKCS#12/PKCS#7) are parsed into Python-facing key or certificate objects, using Rust key-parsing and X.509 crates where applicable. X.509 APIs model certificates, extensions, OCSP, and verification; verification applies policy and trust-store logic rather than merely parsing objects.
• src/cryptography/hazmat/primitives/serialization/base.py
• src/rust/cryptography-key-parsing/src/lib.rs
• src/cryptography/x509/verification.py
• src/rust/cryptography-x509/src/lib.rs
• src/rust/cryptography-x509-verification/src/lib.rs
• src/rust/src/x509/verify/mod.rs
Native dependency and build architecture
The project is a mixed Python/Rust/CFFI distribution. Cargo workspace manifests organize Rust crates; build scripts discover/configure native dependencies. OpenSSL C declarations are partitioned by API domain and feed CFFI binding generation.
• Cargo.toml
• src/rust/Cargo.toml
• src/rust/build.rs
• src/_cffi_src/build_openssl.py
• src/_cffi_src/openssl/evp.py
• src/_cffi_src/openssl/x509.py
Material external runtime dependencies are CPython or PyPy, Rust/Cargo during source builds, and OpenSSL (system-provided or wheel-bundled depending on distribution). CI also explicitly tracks OpenSSL-family variants through dedicated workflows.
Verification, delivery, and infrastructure
Correctness is driven by Python tests plus extensive checked-in cryptographic vectors, including NIST-style cipher/hash data, Wycheproof cases, and PKITS X.509 materials. This is a library, not a network service; external infrastructure is GitHub Actions for CI/wheel production and PyPI for publishing.
• tests/hazmat/primitives/test_aead.py
• tests/wycheproof/test_rsa.py
• tests/x509/verification/test_verification.py
• vectors/cryptography_vectors/
• .github/workflows/ci.yml
• .github/workflows/wheel-builder.yml
Main entry points: test_aead.py Python tests · lib.rs Rust extension · build_openssl.py CFFI OpenSSL generation · aead.py AEAD APIs
Components 26
Python API
Public recipes, primitives, serialization, and X.509 interfaces.
- Package surface
src/cryptography/__init__.py - Fernet recipe · Authenticated symmetric-encryption tokens.
src/cryptography/fernet.py - AEAD APIs
src/cryptography/hazmat/primitives/ciphers/aead.py - RSA APIs
src/cryptography/hazmat/primitives/asymmetric/rsa.py - Serialization API · Loads and emits PEM, DER, OpenSSH, and PKCS formats.
src/cryptography/hazmat/primitives/serialization/base.py - X.509 verification API · Applies certificate policy and trust-store logic.
src/cryptography/x509/verification.py
Native backend
Rust extensions, OpenSSL integration, and parsing crates.
- OpenSSL binding
src/cryptography/hazmat/bindings/openssl/binding.py - Rust AEAD interface
src/cryptography/hazmat/bindings/_rust/openssl/aead.pyi - Rust extension · Python-facing extension entry point and error boundary.
src/rust/src/lib.rs - Rust backend · Dispatches cryptographic operations to native implementations.
src/rust/src/backend/mod.rs - OpenSSL crate
src/rust/cryptography-openssl/src/lib.rs - Key parsing crate
src/rust/cryptography-key-parsing/src/lib.rs - X.509 crate
src/rust/cryptography-x509/src/lib.rs - X.509 verification backend
src/rust/cryptography-x509-verification/src/lib.rs - X.509 verify modules
src/rust/src/x509/verify/mod.rs
Build & dependencies
Mixed Python, Rust, CFFI, and OpenSSL build inputs.
- Cargo workspace
Cargo.toml - Rust build script · Discovers and configures native dependencies.
src/rust/build.rs - CFFI OpenSSL generation · Builds CFFI support from partitioned OpenSSL declarations.
src/_cffi_src/build_openssl.py - OpenSSL EVP declarations
src/_cffi_src/openssl/evp.py - OpenSSL X.509 declarations
src/_cffi_src/openssl/x509.py - OpenSSL · System-provided or wheel-bundled crypto library.
Verification & delivery
Test vectors, CI, wheel builds, and publishing.
- Python tests · Exercises APIs, verification, and adversarial cases.
tests/hazmat/primitives/test_aead.py - Cryptographic vectors · NIST-style, Wycheproof, and PKITS materials.
vectors/cryptography_vectors - CI
.github/workflows/ci.yml - Wheel builder
.github/workflows/wheel-builder.yml - PyPI
Connections 24
- Package surface → Fernet recipe: exports recipes
- Package surface → AEAD APIs: exposes primitives
- Fernet recipe → Rust extension: uses native operations
- AEAD APIs → Rust AEAD interface: dispatches through
- Rust AEAD interface → Rust extension: implemented by
- RSA APIs → Rust extension: uses native operations
- Serialization API → Key parsing crate: parses keys
- X.509 verification API → X.509 verify modules: dispatches verification
- X.509 verify modules → X.509 verification backend: uses
- X.509 verification backend → X.509 crate: verifies parsed objects
- Rust extension → Rust backend: routes operations
- Rust backend → OpenSSL crate: uses
- OpenSSL crate → OpenSSL: links against
- OpenSSL binding → CFFI OpenSSL generation: constructed by
- CFFI OpenSSL generation → OpenSSL EVP declarations: consumes
- CFFI OpenSSL generation → OpenSSL X.509 declarations: consumes
- Cargo workspace → Rust build script: runs
- Rust build script → OpenSSL: configures
- Python tests → AEAD APIs: tests
- Python tests → X.509 verification API: tests
- Python tests → Cryptographic vectors: uses cases
- CI → Python tests: runs
- Wheel builder → Cargo workspace: builds wheels
- Wheel builder → PyPI: publishes