|
| 1 | +# Copyright 2026 Quantum Pipes Technologies, LLC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +"""Hybrid encryption: ML-KEM-768 key exchange + AES-256-GCM data encryption. |
| 5 | +
|
| 6 | +Combines post-quantum key encapsulation (FIPS 203) with classical |
| 7 | +symmetric encryption (FIPS 197) for defense-in-depth: |
| 8 | +
|
| 9 | +1. ML-KEM-768 encapsulates a shared secret (32 bytes) |
| 10 | +2. Shared secret is used as AES-256-GCM key |
| 11 | +3. Data is encrypted with AES-256-GCM |
| 12 | +
|
| 13 | +Format: kem_ciphertext_len (4 bytes) || kem_ciphertext || aes_nonce (12) || aes_ciphertext || aes_tag (16) |
| 14 | +
|
| 15 | +Requires: pip install qp-vault[pq,encryption] |
| 16 | +""" |
| 17 | + |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +import struct |
| 21 | + |
| 22 | +from qp_vault.encryption.aes_gcm import AESGCMEncryptor |
| 23 | +from qp_vault.encryption.ml_kem import MLKEMKeyManager |
| 24 | + |
| 25 | + |
| 26 | +class HybridEncryptor: |
| 27 | + """ML-KEM-768 + AES-256-GCM hybrid encryption. |
| 28 | +
|
| 29 | + Provides quantum-resistant data encryption by wrapping AES keys |
| 30 | + with ML-KEM-768 key encapsulation. |
| 31 | +
|
| 32 | + Usage: |
| 33 | + enc = HybridEncryptor() |
| 34 | + pub, sec = enc.generate_keypair() |
| 35 | + ciphertext = enc.encrypt(b"secret data", pub) |
| 36 | + plaintext = enc.decrypt(ciphertext, sec) |
| 37 | + """ |
| 38 | + |
| 39 | + def __init__(self) -> None: |
| 40 | + self._kem = MLKEMKeyManager() |
| 41 | + |
| 42 | + def generate_keypair(self) -> tuple[bytes, bytes]: |
| 43 | + """Generate an ML-KEM-768 keypair for hybrid encryption. |
| 44 | +
|
| 45 | + Returns: |
| 46 | + (public_key, secret_key) — store secret_key securely. |
| 47 | + """ |
| 48 | + return self._kem.generate_keypair() |
| 49 | + |
| 50 | + def encrypt(self, plaintext: bytes, public_key: bytes) -> bytes: |
| 51 | + """Encrypt data with hybrid ML-KEM-768 + AES-256-GCM. |
| 52 | +
|
| 53 | + Args: |
| 54 | + plaintext: Data to encrypt. |
| 55 | + public_key: ML-KEM-768 public key. |
| 56 | +
|
| 57 | + Returns: |
| 58 | + Hybrid ciphertext: kem_ct_len(4) || kem_ct || aes_encrypted |
| 59 | + """ |
| 60 | + # Step 1: ML-KEM-768 key encapsulation -> shared secret (32 bytes) |
| 61 | + kem_ciphertext, shared_secret = self._kem.encapsulate(public_key) |
| 62 | + |
| 63 | + # Step 2: AES-256-GCM encrypt with the shared secret as key |
| 64 | + aes = AESGCMEncryptor(key=shared_secret[:32]) |
| 65 | + aes_encrypted = aes.encrypt(plaintext) |
| 66 | + |
| 67 | + # Step 3: Pack: kem_ct_len || kem_ct || aes_encrypted |
| 68 | + return struct.pack(">I", len(kem_ciphertext)) + kem_ciphertext + aes_encrypted |
| 69 | + |
| 70 | + def decrypt(self, data: bytes, secret_key: bytes) -> bytes: |
| 71 | + """Decrypt hybrid ML-KEM-768 + AES-256-GCM ciphertext. |
| 72 | +
|
| 73 | + Args: |
| 74 | + data: Hybrid ciphertext from encrypt(). |
| 75 | + secret_key: ML-KEM-768 secret key. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + Decrypted plaintext. |
| 79 | + """ |
| 80 | + # Step 1: Unpack kem_ct_len |
| 81 | + if len(data) < 4: |
| 82 | + raise ValueError("Hybrid ciphertext too short") |
| 83 | + kem_ct_len = struct.unpack(">I", data[:4])[0] |
| 84 | + |
| 85 | + # Step 2: Extract KEM ciphertext and AES ciphertext |
| 86 | + kem_ciphertext = data[4 : 4 + kem_ct_len] |
| 87 | + aes_encrypted = data[4 + kem_ct_len :] |
| 88 | + |
| 89 | + # Step 3: ML-KEM-768 decapsulation -> shared secret |
| 90 | + shared_secret = self._kem.decapsulate(kem_ciphertext, secret_key) |
| 91 | + |
| 92 | + # Step 4: AES-256-GCM decrypt |
| 93 | + aes = AESGCMEncryptor(key=shared_secret[:32]) |
| 94 | + return aes.decrypt(aes_encrypted) |
0 commit comments