File size: 3,895 Bytes
72fc0ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
"""
Symmetric encryption for sensitive data at rest.

Uses Fernet (AES-128-CBC + HMAC-SHA256) from the ``cryptography`` package.
Key derivation follows PBKDF2-HMAC-SHA256 with a configurable iteration count.

Environment variables
---------------------
QUANTLIB_MASTER_KEY   Hex-encoded 32-byte master key.  If absent, a random
                      ephemeral key is generated (development mode).
QUANTLIB_SALT         Hex-encoded 16-byte salt for PBKDF2.
"""

from __future__ import annotations

import base64
import logging
import os
from functools import lru_cache

log = logging.getLogger(__name__)

_MASTER_KEY_ENV = "QUANTLIB_MASTER_KEY"
_SALT_ENV = "QUANTLIB_SALT"
_PBKDF2_ITERATIONS = 390_000       # OWASP 2023 recommendation


class EncryptionError(RuntimeError):
    pass


# ─── Key management ───────────────────────────────────────────────────────────

@lru_cache(maxsize=None)
def _get_fernet():
    """Return a Fernet instance, derived from the master key via PBKDF2."""
    try:
        from cryptography.fernet import Fernet
        from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
        from cryptography.hazmat.primitives import hashes
    except ImportError as exc:
        raise EncryptionError(
            "Install 'cryptography' to enable encryption: pip install cryptography"
        ) from exc

    raw_key_hex = os.environ.get(_MASTER_KEY_ENV, "")
    salt_hex = os.environ.get(_SALT_ENV, "")

    if raw_key_hex:
        try:
            password = bytes.fromhex(raw_key_hex)
        except ValueError as exc:
            raise EncryptionError(
                f"{_MASTER_KEY_ENV} must be a hex-encoded string"
            ) from exc
    else:
        password = os.urandom(32)
        log.warning(
            "No %s set β€” using ephemeral encryption key. "
            "Data encrypted now cannot be decrypted after restart.",
            _MASTER_KEY_ENV,
        )

    if salt_hex:
        try:
            salt = bytes.fromhex(salt_hex)
        except ValueError as exc:
            raise EncryptionError(f"{_SALT_ENV} must be a hex-encoded string") from exc
    else:
        salt = b"quantlibpro_dev_"    # 16 bytes β€” deterministic for dev

    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),
        length=32,
        salt=salt,
        iterations=_PBKDF2_ITERATIONS,
    )
    derived = base64.urlsafe_b64encode(kdf.derive(password))
    return Fernet(derived)


# ─── Public API ───────────────────────────────────────────────────────────────

def encrypt(plaintext: str | bytes) -> bytes:
    """Encrypt *plaintext* and return ciphertext bytes."""
    if isinstance(plaintext, str):
        plaintext = plaintext.encode()
    try:
        return _get_fernet().encrypt(plaintext)
    except Exception as exc:
        raise EncryptionError(f"Encryption failed: {exc}") from exc


def decrypt(ciphertext: bytes) -> bytes:
    """Decrypt *ciphertext* and return plaintext bytes."""
    try:
        return _get_fernet().decrypt(ciphertext)
    except Exception as exc:
        raise EncryptionError(f"Decryption failed β€” wrong key or corrupted data") from exc


def decrypt_str(ciphertext: bytes) -> str:
    """Decrypt *ciphertext* and decode as UTF-8 string."""
    return decrypt(ciphertext).decode()


def generate_key_hex() -> str:
    """
    Generate a cryptographically secure random 32-byte key as hex string.

    Print and store this in ``QUANTLIB_MASTER_KEY`` for production use.
    """
    return os.urandom(32).hex()


def generate_salt_hex() -> str:
    """Generate a fresh 16-byte salt as hex string for ``QUANTLIB_SALT``."""
    return os.urandom(16).hex()