s365b.storage.crypto ==================== .. py:module:: s365b.storage.crypto .. autoapi-nested-parse:: Repository cryptography: keys, blob IDs and blob encryption. Key hierarchy: * A random 64-byte **repository key** is created when the repository is initialised. Its first half encrypts blobs (AES-256-GCM); its second half keys the BLAKE3 hash that names blobs. * The repository key is stored in the repository config, **wrapped** (AES-256-GCM) with a key-encryption key derived from the admin's passphrase using Argon2id. Blob IDs are a *keyed* hash of the plaintext, so identical content deduplicates, but someone holding only the repository files cannot test whether a known file is present. Attributes ---------- .. autoapisummary:: s365b.storage.crypto.NONCE_LEN s365b.storage.crypto.TAG_LEN s365b.storage.crypto.BLOB_ID_LEN Exceptions ---------- .. autoapisummary:: s365b.storage.crypto.CryptoError Classes ------- .. autoapisummary:: s365b.storage.crypto.KdfParams s365b.storage.crypto.RepoKeys Module Contents --------------- .. py:data:: NONCE_LEN :value: 12 .. py:data:: TAG_LEN :value: 16 .. py:data:: BLOB_ID_LEN :value: 32 .. py:exception:: CryptoError Bases: :py:obj:`Exception` Raised when decryption fails: wrong passphrase, wrong key or tampered data. .. py:class:: KdfParams Argon2id parameters used to derive the key-encryption key. :ivar time_cost: Iterations. :ivar memory_kib: Memory in KiB. :ivar parallelism: Lanes. :ivar salt: Random salt, hex-encoded. .. py:attribute:: time_cost :type: int .. py:attribute:: memory_kib :type: int .. py:attribute:: parallelism :type: int .. py:attribute:: salt :type: str .. py:method:: new(*, time_cost: int = 3, memory_kib: int = 64 * 1024, parallelism: int = 4) -> KdfParams :classmethod: Create parameters with a fresh random salt. :param time_cost: Iterations. :param memory_kib: Memory in KiB (default 64 MiB). :param parallelism: Lanes. :returns: New parameters. .. py:method:: derive(passphrase: str) -> bytes Derive the 32-byte key-encryption key from a passphrase. :param passphrase: The repository passphrase. :returns: 32-byte key. .. py:class:: RepoKeys The unwrapped repository key, split into its two uses. :ivar enc_key: AES-256-GCM key for blobs and index files. :ivar id_key: BLAKE3 key for blob IDs. .. py:attribute:: enc_key :type: bytes .. py:attribute:: id_key :type: bytes .. py:method:: generate() -> RepoKeys :classmethod: Create a new random repository key. :returns: New keys. .. py:method:: wrap(kek: bytes) -> bytes Encrypt this key with a key-encryption key. :param kek: Key from :meth:`KdfParams.derive`. :returns: ``nonce || ciphertext``. .. py:method:: unwrap(wrapped: bytes, kek: bytes) -> RepoKeys :classmethod: Decrypt a wrapped key. :param wrapped: Output of :meth:`wrap`. :param kek: Key-encryption key. :returns: The repository keys. :raises CryptoError: If the passphrase (and so the KEK) is wrong. .. py:method:: blob_id(plaintext: bytes) -> str Compute the content address of a blob. :param plaintext: Uncompressed, unencrypted blob content. :returns: 64-character hex ID. .. py:method:: encrypt(data: bytes, aad: bytes) -> bytes Encrypt data with the repository key. :param data: Plaintext. :param aad: Associated data bound to the ciphertext (e.g. the blob ID). :returns: ``nonce || ciphertext || tag``. .. py:method:: decrypt(data: bytes, aad: bytes) -> bytes Decrypt data produced by :meth:`encrypt`. :param data: ``nonce || ciphertext || tag``. :param aad: The associated data used when encrypting. :returns: Plaintext. :raises CryptoError: If the data was modified or belongs elsewhere.