<!-- Copyright (c) 2026 defconxt. All rights reserved. -->
<!-- Licensed under AGPL-3.0 — see LICENSE file for details. -->
---
name: implementing-post-quantum-cryptography
description: >-
  Implement NIST post-quantum cryptographic standards (ML-KEM, ML-DSA, SLH-DSA)
  for quantum-resistant key encapsulation and digital signatures. Covers
  algorithm selection, hybrid mode deployment, crypto agility planning,
  migration strategies, and testing with liboqs and oqs-provider.
domain: cybersecurity
subdomain: cryptography
tags:
  - post-quantum
  - pqc
  - ml-kem
  - ml-dsa
  - slh-dsa
  - crystals-kyber
  - crystals-dilithium
  - crypto-agility
  - nist-pqc
  - quantum-readiness
version: "1.0"
author: defconxt
license: AGPL-3.0
metadata:
  mitre-attack: []
---

# Implementing Post-Quantum Cryptography

## Overview

Quantum computers threaten RSA and ECC through Shor's algorithm. NIST
finalized post-quantum standards in August 2024: ML-KEM (FIPS 203) for key
encapsulation and ML-DSA (FIPS 204) / SLH-DSA (FIPS 205) for signatures.
This skill covers practical implementation using liboqs and hybrid deployment
strategies.

## Prerequisites

| Requirement | Install |
|---|---|
| liboqs | `apt install liboqs-dev` or build from source |
| oqs-provider | OpenSSL 3.x provider for PQC algorithms |
| Python 3.10+ | For agent tooling |
| liboqs-python | `pip install liboqs-python` |

## Key Concepts

### NIST PQC Standards

| Standard | Algorithm | Purpose | Key Size | Sig/CT Size |
|---|---|---|---|---|
| FIPS 203 (ML-KEM-768) | CRYSTALS-Kyber | Key encapsulation | 1,184 B pub | 1,088 B CT |
| FIPS 203 (ML-KEM-1024) | CRYSTALS-Kyber | Key encapsulation | 1,568 B pub | 1,568 B CT |
| FIPS 204 (ML-DSA-65) | CRYSTALS-Dilithium | Signatures | 1,952 B pub | 3,293 B sig |
| FIPS 204 (ML-DSA-87) | CRYSTALS-Dilithium | Signatures | 2,592 B pub | 4,595 B sig |
| FIPS 205 (SLH-DSA-SHA2-256f) | SPHINCS+ | Signatures (stateless) | 64 B pub | 29,792 B sig |

### Algorithm Selection Guide

```
Key Encapsulation (replacing RSA/ECDH key exchange):
├── ML-KEM-768 — Recommended for most applications
├── ML-KEM-1024 — High security (256-bit quantum)
└── Hybrid: ML-KEM-768 + X25519 — Transition period

Digital Signatures (replacing RSA/ECDSA signatures):
├── ML-DSA-65 — General purpose signatures
├── ML-DSA-87 — High security signatures
├── SLH-DSA — When stateless hash-based preferred
└── Hybrid: ML-DSA-65 + Ed25519 — Transition period
```

### Using liboqs-python

```python
import oqs

# Key Encapsulation with ML-KEM-768
kem = oqs.KeyEncapsulation("ML-KEM-768")
public_key = kem.generate_keypair()
ciphertext, shared_secret_enc = kem.encap_secret(public_key)
shared_secret_dec = kem.decap_secret(ciphertext)
assert shared_secret_enc == shared_secret_dec

# Digital Signature with ML-DSA-65
sig = oqs.Signature("ML-DSA-65")
public_key = sig.generate_keypair()
message = b"Document to sign"
signature = sig.sign(message)
is_valid = sig.verify(message, signature, public_key)
```

### Hybrid Mode with OpenSSL + oqs-provider

```bash
# Generate hybrid key (X25519 + ML-KEM-768)
openssl genpkey -provider oqsprovider -algorithm x25519_mlkem768 \
  -out hybrid-key.pem

# Create hybrid certificate
openssl req -new -x509 -provider oqsprovider \
  -key hybrid-key.pem -out hybrid-cert.pem \
  -subj "/CN=PQC Hybrid Test" -days 365

# Test TLS with hybrid key exchange
openssl s_server -provider oqsprovider \
  -cert hybrid-cert.pem -key hybrid-key.pem -port 4433 &
openssl s_client -provider oqsprovider \
  -connect localhost:4433 -groups x25519_mlkem768
```

### Migration Strategy

```
Phase 1 — Inventory (Now)
├── Catalog all cryptographic algorithms in use
├── Identify harvest-now-decrypt-later risk data
├── Map dependencies on RSA/ECC
└── Establish crypto agility requirement

Phase 2 — Hybrid Deployment (2025-2027)
├── Deploy hybrid TLS (classical + PQC combined)
├── Test PQC in non-production environments
├── Update libraries to PQC-capable versions
└── Validate performance impact

Phase 3 — PQC Primary (2027-2030)
├── PQC as default for new deployments
├── Classical as fallback only
├── Certificate infrastructure supports PQC
└── HSMs updated for PQC algorithms

Phase 4 — Classical Retirement (2030+)
├── Remove classical-only endpoints
├── Audit for residual classical dependencies
└── Full PQC for all cryptographic operations
```

## Workflow

1. **Inventory** — Catalog all cryptographic usage across the organization
2. **Assess** — Identify data at risk from harvest-now-decrypt-later attacks
3. **Test** — Deploy liboqs in test environment, benchmark performance
4. **Hybrid** — Implement hybrid classical + PQC for critical systems
5. **Migrate** — Transition to PQC-primary as ecosystem matures
6. **Validate** — Continuous testing against updated NIST guidance

## Verification

| Check | Method |
|---|---|
| liboqs installed | `python3 -c "import oqs; print(oqs.get_enabled_kem_mechanisms())"` |
| ML-KEM functional | KEM keygen/encap/decap round-trip succeeds |
| ML-DSA functional | Sign/verify round-trip succeeds |
| Hybrid TLS working | `openssl s_client` negotiates hybrid group |
| Inventory complete | All crypto assets cataloged with algorithm and key size |

## References

- [NIST Post-Quantum Cryptography](https://csrc.nist.gov/projects/post-quantum-cryptography)
- [liboqs — Open Quantum Safe](https://github.com/open-quantum-safe/liboqs)
- [oqs-provider for OpenSSL](https://github.com/open-quantum-safe/oqs-provider)
- [FIPS 203 (ML-KEM)](https://csrc.nist.gov/publications/detail/fips/203/final)
- [FIPS 204 (ML-DSA)](https://csrc.nist.gov/publications/detail/fips/204/final)
