Skip to main content

Module encryption

Module encryption 

Source
Expand description

Asset and metadata encryption — the only place AES-256-GCM is invoked for user data.

Three constructions (SSoT: Cryptography — Encryption):

  • stream — AES-256-GCM STREAM for asset bytes (originals + derivatives), supporting streaming, ranged reads, and per-chunk authentication.
  • blob — standalone AES-256-GCM with a fixed wire format for small metadata blobs.
  • keywrap — asset-keywrap/v1 seal/unseal for an externally-chosen file key carried under key_mode = wrapped (an adopted web-upload drop).
  • rekey — the re-keying writers that fold a fresh nonce into the key salt and refuse to reuse the nonce they supersede (a same-epoch replace / metadata-update).

Structs§

AssetEncryption
Result of encrypting an asset: everything the signed manifest commits to.
CiphertextReader
Random-access re-derivation of a STREAM ciphertext from its plaintext.
StreamDecryptor
Incremental STREAM decryption of a ciphertext that arrives in pieces of any size.

Enums§

RekeyStreamError
Why a streaming re-keyed write failed: the reuse refusal, or the reader or writer.
StreamError
Errors from streaming encrypt/decrypt.

Constants§

CIPHERTEXT_CHUNK
Ciphertext bytes per full STREAM chunk (plaintext chunk + 16-byte GCM tag = 64 KiB).
MIN_BLOB_LEN
Minimum valid blob length: header + nonce + empty ciphertext + tag.
NONCE_LEN
Length of the per-blob AES-256-GCM nonce (fresh CSPRNG draw per seal).
NONCE_PREFIX_LEN
STREAM nonce prefix length (12-byte nonce − 4-byte counter − 1-byte last flag).
PLAINTEXT_CHUNK
Plaintext bytes per STREAM chunk.
TAG_LEN
GCM authentication tag length.
WRAPPED_FILE_KEY_LEN
Length of the wrapped file key: wrap_nonce(12) ‖ ciphertext(32) ‖ tag(16).
WRAP_NONCE_LEN
Length of the fresh per-wrap nonce (the AES-256-GCM nonce for the wrap).

Functions§

blob_ciphertext_hash
The content hash of a metadata blob: SHA-256 over the full wire byte string (header ‖ nonce ‖ ciphertext ‖ tag). This is the value the manifest commits to.
blob_nonce
The 12-byte nonce a blob carries in its header, needed to re-derive the folded blob_key before opening. None if the wire is shorter than the fixed prefix.
blob_suite_id
The crypto_suite_id declared in a blob’s header, without decrypting.
chunk_count
The number of STREAM chunks a plaintext_size-byte plaintext encrypts to. An empty plaintext is still one (empty, last) chunk: its ciphertext is a bare tag.
ciphertext_len
The exact ciphertext length a plaintext_size-byte plaintext encrypts to: the plaintext plus one TAG_LEN tag per chunk.
decrypt_asset
Decrypt a STREAM ciphertext from reader to writer. Every chunk’s tag is verified; any tamper/truncation/reorder yields StreamError::Auth.
decrypt_asset_vec
Convenience: decrypt a whole ciphertext slice in memory.
decrypt_chunk
Decrypt a single ciphertext chunk in isolation (ranged read). index is the 0-based chunk number; is_last must be true only for the final chunk of the file (it carries the STREAM last-block flag). Returns crate::crypto::CryptoError::Auth on tamper.
encrypt_asset
Encrypt reader to writer under file_key, returning the content metadata. Computes the ciphertext hash incrementally — the whole file is never buffered. Draws a fresh random nonce prefix.
encrypt_asset_rekey
Encrypt plaintext as a first or re-rolled asset write under amk scoped to file_id: draw a fresh nonce_prefix, fold it into the file-key salt, and STREAM-encrypt.
encrypt_asset_rekey_stream
encrypt_asset_rekey from a reader to a writer, one STREAM chunk at a time: the same prefix draw, key fold and reuse refusal, without the plaintext or the ciphertext ever being whole in memory. Returns the AssetEncryption metadata (whose ciphertext_hash is computed as the chunks pass) and the derived file key.
encrypt_asset_rekey_stream_with_prefix
encrypt_asset_rekey_stream with an explicit nonce_prefix — the one implementation of the fold and the reuse refusal, which encrypt_asset_rekey_with_prefix drives over a slice, so the two cannot produce different bytes.
encrypt_asset_rekey_with_prefix
encrypt_asset_rekey with an explicit nonce_prefix — the deterministic, testable core of the fold and the reuse refusal.
encrypt_asset_vec
Convenience: encrypt a whole slice in memory, returning (metadata, ciphertext).
encrypt_asset_vec_full
Convenience: encrypt a whole slice in memory and also return the ciphertext bytes.
encrypt_asset_vec_with_prefix
Convenience: encrypt a whole slice in memory with an explicit prefix (deterministic).
encrypt_asset_with_prefix
As encrypt_asset but with an explicit nonce prefix, so a client can regenerate the exact ciphertext from the plaintext + file key + the prefix recorded in the manifest (the client stores plaintext locally; ciphertext is derived for upload/backup).
encrypt_chunk
Encrypt a single plaintext chunk in isolation: the inverse of decrypt_chunk, and byte-identical to chunk index of encrypt_asset_with_prefix’s output for the same key and prefix. pt_chunk must be PLAINTEXT_CHUNK bytes unless is_last.
open_blob
Open a metadata-blob wire string under blob_key. Rejects an unknown crypto_suite_id (fail-closed) before attempting decryption, and rejects tampered ciphertext.
recover_file_key
Recover the file key of the original a manifest describes, honouring its key_mode.
recover_file_key_parts
recover_file_key over the manifest fields it reads, for a reader that holds those fields but not the manifest: the share viewer, which is served an asset’s key-free crypto parameters and never its signed manifest (that would carry the device and account ids the export strip removes). Same dispatch and the same fail-closed errors.
seal_blob
Seal plaintext (canonical CBOR) into the metadata-blob wire format under blob_key, tagging it with the current crypto_suite_id. A fresh nonce is drawn per call.
seal_blob_with_nonce
seal_blob with an explicit nonce, so a metadata-blob writer can fold that same nonce into the derived blob_key salt before sealing (the salt depends on the nonce).
seal_file_key
Seal an externally-chosen 32-byte file key K under amk, scoped to file_id.
seal_metadata_blob
Seal plaintext (canonical CBOR sidecar) into a metadata blob under amk scoped to blob_id: draw a fresh blob nonce, fold it into the blob-key salt, and AEAD-seal.
seal_metadata_blob_with_nonce
seal_metadata_blob with an explicit nonce — the deterministic, testable core.
unseal_file_key
Recover the file key K from a wrapped_file_key under amk, scoped to file_id.