# pyca/cryptography architecture

> Architecture diagram of the GitHub repository pyca/cryptography, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pyca/cryptography
- Repository: https://github.com/pyca/cryptography

## Overview

#### 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: [`tests/hazmat/primitives/test_aead.py`](https://github.com/pyca/cryptography/blob/main/tests/hazmat/primitives/test_aead.py) (Python tests), [`src/rust/src/lib.rs`](https://github.com/pyca/cryptography/blob/main/src/rust/src/lib.rs) (Rust extension), [`src/_cffi_src/build_openssl.py`](https://github.com/pyca/cryptography/blob/main/src/_cffi_src/build_openssl.py) (CFFI OpenSSL generation), [`src/cryptography/hazmat/primitives/ciphers/aead.py`](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/ciphers/aead.py) (AEAD APIs)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_python["Python API"]
  node_package["Package surface<br/>Python package<br/>[__init__.py]"]
  node_fernet["Fernet recipe<br/>recipe API<br/>[fernet.py]"]
  node_aead["AEAD APIs<br/>primitive API<br/>[aead.py]"]
  node_rsa["RSA APIs<br/>primitive API<br/>[rsa.py]"]
  node_serialization["Serialization API<br/>key formats<br/>[base.py]"]
  node_x509_verify_api["X.509 verification API<br/>[verification.py]"]
end

subgraph group_native["Native backend"]
  node_openssl_binding["OpenSSL binding<br/>Python binding<br/>[binding.py]"]
  node_rust_aead_types["Rust AEAD interface<br/>extension types<br/>[aead.pyi]"]
  node_rust_extension{{"Rust extension<br/>native module<br/>[lib.rs]"}}
  node_rust_backend["Rust backend<br/>backend modules<br/>[mod.rs]"]
  node_openssl_crate["OpenSSL crate<br/>Rust crate<br/>[lib.rs]"]
  node_key_parsing["Key parsing crate<br/>Rust crate<br/>[lib.rs]"]
  node_x509_crate["X.509 crate<br/>Rust crate<br/>[lib.rs]"]
  node_x509_verification["X.509 verification backend<br/>Rust crate<br/>[lib.rs]"]
  node_x509_verify_modules["X.509 verify modules<br/>extension modules<br/>[mod.rs]"]
end

subgraph group_build["Build &amp; dependencies"]
  node_cargo_workspace["Cargo workspace<br/>Rust build manifest<br/>[Cargo.toml]"]
  node_rust_build["Rust build script<br/>native dependency setup<br/>[build.rs]"]
  node_cffi_build["CFFI OpenSSL generation<br/>binding generator<br/>[build_openssl.py]"]
  node_openssl_evp_decls["OpenSSL EVP declarations<br/>CFFI declarations<br/>[evp.py]"]
  node_openssl_x509_decls["OpenSSL X.509 declarations<br/>CFFI declarations<br/>[x509.py]"]
  node_openssl{{"OpenSSL<br/>native dependency"}}
end

subgraph group_quality["Verification &amp; delivery"]
  node_tests["Python tests<br/>test suites<br/>[test_aead.py]"]
  node_vectors[("Cryptographic vectors<br/>test data")]
  node_ci["CI<br/>GitHub Actions<br/>[ci.yml]"]
  node_wheel_builder["Wheel builder<br/>GitHub Actions<br/>[wheel-builder.yml]"]
  node_pypi["PyPI<br/>package registry"]
end

node_package -->|"exports recipes"| node_fernet
node_package -->|"exposes primitives"| node_aead
node_fernet -->|"uses native operations"| node_rust_extension
node_aead -->|"dispatches through"| node_rust_aead_types
node_rust_aead_types -->|"implemented by"| node_rust_extension
node_rsa -->|"uses native operations"| node_rust_extension
node_serialization -->|"parses keys"| node_key_parsing
node_x509_verify_api -->|"dispatches verification"| node_x509_verify_modules
node_x509_verify_modules -->|"uses"| node_x509_verification
node_x509_verification -->|"verifies parsed objects"| node_x509_crate
node_rust_extension -->|"routes operations"| node_rust_backend
node_rust_backend -->|"uses"| node_openssl_crate
node_openssl_crate -->|"links against"| node_openssl
node_openssl_binding -->|"constructed by"| node_cffi_build
node_cffi_build -->|"consumes"| node_openssl_evp_decls
node_cffi_build -->|"consumes"| node_openssl_x509_decls
node_cargo_workspace -->|"runs"| node_rust_build
node_rust_build -->|"configures"| node_openssl
node_tests -.->|"tests"| node_aead
node_tests -.->|"tests"| node_x509_verify_api
node_tests -->|"uses cases"| node_vectors
node_ci -->|"runs"| node_tests
node_wheel_builder -->|"builds wheels"| node_cargo_workspace
node_wheel_builder -->|"publishes"| node_pypi

click node_package "https://github.com/pyca/cryptography/blob/main/src/cryptography/__init__.py"
click node_fernet "https://github.com/pyca/cryptography/blob/main/src/cryptography/fernet.py"
click node_aead "https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/ciphers/aead.py"
click node_rsa "https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/asymmetric/rsa.py"
click node_serialization "https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/serialization/base.py"
click node_x509_verify_api "https://github.com/pyca/cryptography/blob/main/src/cryptography/x509/verification.py"
click node_openssl_binding "https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/bindings/openssl/binding.py"
click node_rust_aead_types "https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/bindings/_rust/openssl/aead.pyi"
click node_rust_extension "https://github.com/pyca/cryptography/blob/main/src/rust/src/lib.rs"
click node_rust_backend "https://github.com/pyca/cryptography/blob/main/src/rust/src/backend/mod.rs"
click node_openssl_crate "https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-openssl/src/lib.rs"
click node_key_parsing "https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-key-parsing/src/lib.rs"
click node_x509_crate "https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-x509/src/lib.rs"
click node_x509_verification "https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-x509-verification/src/lib.rs"
click node_x509_verify_modules "https://github.com/pyca/cryptography/blob/main/src/rust/src/x509/verify/mod.rs"
click node_cargo_workspace "https://github.com/pyca/cryptography/blob/main/Cargo.toml"
click node_rust_build "https://github.com/pyca/cryptography/blob/main/src/rust/build.rs"
click node_cffi_build "https://github.com/pyca/cryptography/blob/main/src/_cffi_src/build_openssl.py"
click node_openssl_evp_decls "https://github.com/pyca/cryptography/blob/main/src/_cffi_src/openssl/evp.py"
click node_openssl_x509_decls "https://github.com/pyca/cryptography/blob/main/src/_cffi_src/openssl/x509.py"
click node_tests "https://github.com/pyca/cryptography/blob/main/tests/hazmat/primitives/test_aead.py"
click node_vectors "https://github.com/pyca/cryptography/tree/main/vectors/cryptography_vectors"
click node_ci "https://github.com/pyca/cryptography/blob/main/.github/workflows/ci.yml"
click node_wheel_builder "https://github.com/pyca/cryptography/blob/main/.github/workflows/wheel-builder.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_package,node_fernet,node_aead,node_rsa,node_serialization,node_x509_verify_api toneBlue
class node_openssl_binding,node_rust_aead_types,node_rust_extension,node_rust_backend,node_openssl_crate,node_key_parsing,node_x509_crate,node_x509_verification,node_x509_verify_modules toneAmber
class node_cargo_workspace,node_rust_build,node_cffi_build,node_openssl_evp_decls,node_openssl_x509_decls,node_openssl toneMint
class node_tests,node_vectors,node_ci,node_wheel_builder,node_pypi toneRose
```

## Components

### Python API

Public recipes, primitives, serialization, and X.509 interfaces.

- [Package surface](https://github.com/pyca/cryptography/blob/main/src/cryptography/__init__.py): Python package · `src/cryptography/__init__.py`
- [Fernet recipe](https://github.com/pyca/cryptography/blob/main/src/cryptography/fernet.py): recipe API · Authenticated symmetric-encryption tokens. · `src/cryptography/fernet.py`
- [AEAD APIs](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/ciphers/aead.py): primitive API · `src/cryptography/hazmat/primitives/ciphers/aead.py`
- [RSA APIs](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/asymmetric/rsa.py): primitive API · `src/cryptography/hazmat/primitives/asymmetric/rsa.py`
- [Serialization API](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/primitives/serialization/base.py): key formats · Loads and emits PEM, DER, OpenSSH, and PKCS formats. · `src/cryptography/hazmat/primitives/serialization/base.py`
- [X.509 verification API](https://github.com/pyca/cryptography/blob/main/src/cryptography/x509/verification.py): 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](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/bindings/openssl/binding.py): Python binding · `src/cryptography/hazmat/bindings/openssl/binding.py`
- [Rust AEAD interface](https://github.com/pyca/cryptography/blob/main/src/cryptography/hazmat/bindings/_rust/openssl/aead.pyi): extension types · `src/cryptography/hazmat/bindings/_rust/openssl/aead.pyi`
- [Rust extension](https://github.com/pyca/cryptography/blob/main/src/rust/src/lib.rs): native module · Python-facing extension entry point and error boundary. · `src/rust/src/lib.rs`
- [Rust backend](https://github.com/pyca/cryptography/blob/main/src/rust/src/backend/mod.rs): backend modules · Dispatches cryptographic operations to native implementations. · `src/rust/src/backend/mod.rs`
- [OpenSSL crate](https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-openssl/src/lib.rs): Rust crate · `src/rust/cryptography-openssl/src/lib.rs`
- [Key parsing crate](https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-key-parsing/src/lib.rs): Rust crate · `src/rust/cryptography-key-parsing/src/lib.rs`
- [X.509 crate](https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-x509/src/lib.rs): Rust crate · `src/rust/cryptography-x509/src/lib.rs`
- [X.509 verification backend](https://github.com/pyca/cryptography/blob/main/src/rust/cryptography-x509-verification/src/lib.rs): Rust crate · `src/rust/cryptography-x509-verification/src/lib.rs`
- [X.509 verify modules](https://github.com/pyca/cryptography/blob/main/src/rust/src/x509/verify/mod.rs): extension modules · `src/rust/src/x509/verify/mod.rs`

### Build & dependencies

Mixed Python, Rust, CFFI, and OpenSSL build inputs.

- [Cargo workspace](https://github.com/pyca/cryptography/blob/main/Cargo.toml): Rust build manifest · `Cargo.toml`
- [Rust build script](https://github.com/pyca/cryptography/blob/main/src/rust/build.rs): native dependency setup · Discovers and configures native dependencies. · `src/rust/build.rs`
- [CFFI OpenSSL generation](https://github.com/pyca/cryptography/blob/main/src/_cffi_src/build_openssl.py): binding generator · Builds CFFI support from partitioned OpenSSL declarations. · `src/_cffi_src/build_openssl.py`
- [OpenSSL EVP declarations](https://github.com/pyca/cryptography/blob/main/src/_cffi_src/openssl/evp.py): CFFI declarations · `src/_cffi_src/openssl/evp.py`
- [OpenSSL X.509 declarations](https://github.com/pyca/cryptography/blob/main/src/_cffi_src/openssl/x509.py): CFFI declarations · `src/_cffi_src/openssl/x509.py`
- **OpenSSL**: native dependency · System-provided or wheel-bundled crypto library.

### Verification & delivery

Test vectors, CI, wheel builds, and publishing.

- [Python tests](https://github.com/pyca/cryptography/blob/main/tests/hazmat/primitives/test_aead.py): test suites · Exercises APIs, verification, and adversarial cases. · `tests/hazmat/primitives/test_aead.py`
- [Cryptographic vectors](https://github.com/pyca/cryptography/tree/main/vectors/cryptography_vectors): test data · NIST-style, Wycheproof, and PKITS materials. · `vectors/cryptography_vectors`
- [CI](https://github.com/pyca/cryptography/blob/main/.github/workflows/ci.yml): GitHub Actions · `.github/workflows/ci.yml`
- [Wheel builder](https://github.com/pyca/cryptography/blob/main/.github/workflows/wheel-builder.yml): GitHub Actions · `.github/workflows/wheel-builder.yml`
- **PyPI**: package registry

## Connections

- 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
