Skip to main content

encryption

Symmetric and asymmetric encryption functions.

Module​

Functions​

derive_aes_key​

def derive_aes_key(pod_key_path: Path, salt: bytes) ‑> bytes | None:

Derive a raw 32-byte AES key from a pod RSA/SSH key file and salt.

_FernetEncryption.derive_key_from_path returns a Fernet-formatted (base64-urlsafe) 32-byte key; this decodes it to the raw 32 bytes that _AESEncryption expects. The salt scopes the derivation so distinct call-sites (e.g. the cache-column key vs. the patient-API image-cache key) derive non-coinciding keys from the same pod key.

Arguments

  • pod_key_path: Path to the pod's RSA/SSH private key file.
  • salt: Domain-separation salt for the derivation.

Returns The raw 32-byte AES key, or None if the key could not be derived (e.g. the file is missing) — callers raise their own contextual error.

seal_aes_gcm​

def seal_aes_gcm(key: bytes, plaintext: bytes) ‑> bytes:

Encrypt plaintext with AES-GCM, returning nonce || ciphertext.

The raw envelope shared by the cache-column and image-cache encryption paths: a fresh 12-byte nonce is prepended to the ciphertext.

Arguments

  • key: The raw 32-byte AES key.
  • plaintext: The bytes to encrypt.

Returns nonce || ciphertext, with a 12-byte (_AES_NONCE_SIZE_BYTES) nonce.

unseal_aes_gcm​

def unseal_aes_gcm(key: bytes, blob: bytes) ‑> bytes:

Decrypt a nonce || ciphertext AES-GCM blob (12-byte nonce).

Inverse of seal_aes_gcm.

Arguments

  • key: The raw 32-byte AES key.
  • blob: nonce || ciphertext as produced by seal_aes_gcm.

Returns The decrypted plaintext bytes.

Raises

  • DecryptError: If the AES-GCM authentication tag is invalid.
  • ValueError: If the blob is too short to hold a valid nonce.

Classes​

AESGCM​

class AESGCM(key):

Static methods​


generate_key​

def generate_key(bit_length):

Methods​


decrypt​

def decrypt(self, /, nonce, data, associated_data):

decrypt_into​

def decrypt_into(self, /, nonce, data, associated_data, buf):

encrypt​

def encrypt(self, /, nonce, data, associated_data):

encrypt_into​

def encrypt_into(self, /, nonce, data, associated_data, buf):

AESSIV​

class AESSIV(key):

Static methods​


generate_key​

def generate_key(bit_length):

Methods​


decrypt​

def decrypt(self, /, data, associated_data):

decrypt_into​

def decrypt_into(self, /, data, associated_data, buf):

encrypt​

def encrypt(self, /, data, associated_data):

encrypt_into​

def encrypt_into(self, /, data, associated_data, buf):

KeySerializationFormat​

class KeySerializationFormat(*args, **kwds):

The format to serialize a key in.

Variables​

  • static PEM
  • static SSH