s365b.storage.crypto

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

Exceptions

CryptoError

Raised when decryption fails: wrong passphrase, wrong key or tampered data.

Classes

KdfParams

Argon2id parameters used to derive the key-encryption key.

RepoKeys

The unwrapped repository key, split into its two uses.

Module Contents

s365b.storage.crypto.NONCE_LEN = 12[source]
s365b.storage.crypto.TAG_LEN = 16[source]
s365b.storage.crypto.BLOB_ID_LEN = 32[source]
exception s365b.storage.crypto.CryptoError[source]

Bases: Exception

Raised when decryption fails: wrong passphrase, wrong key or tampered data.

class s365b.storage.crypto.KdfParams[source]

Argon2id parameters used to derive the key-encryption key.

Variables:
  • time_cost – Iterations.

  • memory_kib – Memory in KiB.

  • parallelism – Lanes.

  • salt – Random salt, hex-encoded.

time_cost: int[source]
memory_kib: int[source]
parallelism: int[source]
salt: str[source]
classmethod new(*, time_cost: int = 3, memory_kib: int = 64 * 1024, parallelism: int = 4) → KdfParams[source]

Create parameters with a fresh random salt.

Parameters:
  • time_cost – Iterations.

  • memory_kib – Memory in KiB (default 64 MiB).

  • parallelism – Lanes.

Returns:

New parameters.

derive(passphrase: str) → bytes[source]

Derive the 32-byte key-encryption key from a passphrase.

Parameters:

passphrase – The repository passphrase.

Returns:

32-byte key.

class s365b.storage.crypto.RepoKeys[source]

The unwrapped repository key, split into its two uses.

Variables:
  • enc_key – AES-256-GCM key for blobs and index files.

  • id_key – BLAKE3 key for blob IDs.

enc_key: bytes[source]
id_key: bytes[source]
classmethod generate() → RepoKeys[source]

Create a new random repository key.

Returns:

New keys.

wrap(kek: bytes) → bytes[source]

Encrypt this key with a key-encryption key.

Parameters:

kek – Key from KdfParams.derive().

Returns:

nonce || ciphertext.

classmethod unwrap(wrapped: bytes, kek: bytes) → RepoKeys[source]

Decrypt a wrapped key.

Parameters:
  • wrapped – Output of wrap().

  • kek – Key-encryption key.

Returns:

The repository keys.

Raises:

CryptoError – If the passphrase (and so the KEK) is wrong.

blob_id(plaintext: bytes) → str[source]

Compute the content address of a blob.

Parameters:

plaintext – Uncompressed, unencrypted blob content.

Returns:

64-character hex ID.

encrypt(data: bytes, aad: bytes) → bytes[source]

Encrypt data with the repository key.

Parameters:
  • data – Plaintext.

  • aad – Associated data bound to the ciphertext (e.g. the blob ID).

Returns:

nonce || ciphertext || tag.

decrypt(data: bytes, aad: bytes) → bytes[source]

Decrypt data produced by encrypt().

Parameters:
  • data – nonce || ciphertext || tag.

  • aad – The associated data used when encrypting.

Returns:

Plaintext.

Raises:

CryptoError – If the data was modified or belongs elsewhere.