Skip to main content

Module crypto

Module crypto 

Source

Re-exports§

pub use chachapoly::wipe;
pub use chachapoly::wipe_vec;
pub use guarded_key::GuardedKey;
pub use crate::simd::image::nearest_neighbor_downsample_rgba;

Modules§

batch_verify
Event signatures checked many at a time.
chachapoly
ChaCha20-Poly1305 (RFC 8439), tuned for many short messages.
gate
Keeps every at-rest write on the key the store uses. A writer holds a Sealing ticket from before it reads the vault (or the enabled flag) until its row is written; a migration holds KeyChange from before its transaction until the vault and the flag show the result. A value sealed under one key can then never land after the store has moved to another.
gcm
AES-256-GCM with a 16-byte nonce and no associated data: the attachment cipher (0xChat-compatible). A stitched AES-NI/PCLMULQDQ engine where it has been measured to win, RustCrypto’s ctr + ghash everywhere else; both produce the same bytes.
guarded_key
Memory-hardened key storage: 32 arrays of random-looking words with decoy writes, and a shielded key.
nip44
NIP-44 v2 on Vector’s own ChaCha20 and SHA-256: the bytes of rust-nostr’s nip44::v2, decrypting in place in the buffer the payload was decoded into.
sha256
SHA-256 for bulk data: attachments, files and blobs.
stream
Attachment AES-256-GCM over a file, a chunk at a time: the cipher of super::decrypt_data (16-byte nonce, 16-byte tag appended) without the file ever sitting whole in memory.

Structs§

AtRestOpener
The per-string half of open_batch; the vault is read at the first ciphertext, so a batch of plaintext rows never touches it.
AtRestSealer
The per-string half of seal_batch.
EncryptionParams
Parameters for AES-256-GCM file encryption (hex-encoded key + nonce).
GuardedSigner
A signer backed by the MY_SECRET_KEY vault.

Enums§

Kdf
How an account’s at-rest key is derived from its PIN or password. Stored as the kdf settings row, in plaintext since it is needed before the key exists. No row is Legacy: the fixed salt every account used before per-account salts. A label names a whole parameter set, so a stored value can never ask for costs this build doesn’t know.

Constants§

RECOGNISED_MIMES
Detect MIME type from file magic bytes. Supports PNG, JPEG, GIF, WebP, TIFF, ICO, and SVG. Returns “application/octet-stream” for unrecognized formats. Every type mime_from_magic_bytes can name.
WRAPS_PER_VAULT_READ
Wraps opened per vault read, however many a batch holds. It bounds how long the key stays materialised: two ECDHs per wrap, about 1.5 ms per read on a desktop core.

Functions§

animated_format
If bytes is an animated image we must not re-encode, return its extension ("gif"/"webp"/"png"); otherwise None. Biased toward detecting animation: a false positive only skips stripping/compression, whereas a false negative would flatten the animation to a still.
attachment_identity_basis
Identity basis for a RECEIVED attachment: the sender’s ox (plaintext hash) when provided — it’s what enables honest cross-message dedup — else a digest of nonce+url. Never the raw nonce: senders can and do reuse nonces, which cross-bound DIFFERENT files to one identity (the “new image renders as an old one” class). The upload URL is unique per ciphertext, so the digest is collision-resistant even against nonce reuse. Nothing honest ever writes a file under the digest name, so existence of one is never proof of download — reuse decisions stay with the content-verified download path.
bounded_image_limits
Shared decode limits for decode_image_bounded and call sites that need their own ImageReader (fixed-format decodes).
decode_image_bounded
Decode an image with allocation limits. A tiny file declaring 60000×60000 would otherwise allocate ~14 GB before a single pixel decodes — every decode of bytes we didn’t author must go through this.
decrypt_blob_with_key
ChaCha20-Poly1305 decrypt nonce(12) || ct || tag(16) with an explicit key.
decrypt_data
Decrypt data with AES-256-GCM using a 16-byte nonce (0xChat-compatible). Input format: ciphertext || 16-byte auth tag.
decrypt_data_owned
decrypt_data in place, for a caller that owns the ciphertext: the plaintext comes back in the same allocation, so nothing the size of the file is copied.
decrypt_with_key
Decrypt a hex-encoded ChaCha20-Poly1305 ciphertext with an explicit key.
derive_key
Derive the 32-byte at-rest key from a PIN or password under kdf (Argon2id, 150 MB, 10 passes). Slow on purpose, so it runs off the async workers.
encrypt_blob_with_key
ChaCha20-Poly1305 encrypt raw bytes with an explicit key → nonce(12) || ct || tag(16).
encrypt_data
Encrypt data with AES-256-GCM using a 16-byte nonce (0xChat-compatible).
encrypt_with_key
Encrypt a string with an explicit key (ChaCha20-Poly1305), hex nonce || ct || tag.
extension_from_mime
Convert a MIME type to a file extension. Falls back to using the MIME subtype when unknown.
format_bytes
Format bytes into human-readable format (KB, MB, GB).
generate_encryption_params
Generate random AES-256-GCM encryption parameters.
generate_image_metadata
Generate image metadata (thumbhash + dimensions) from raw file bytes.
generate_thumbhash_from_rgba
Generate a thumbhash from RGBA8 pixel data.
install_unlocked_key
Install the key an unlock derived under kdf, once it proves against this account and the account still derives that way. Under a key change, so it can’t interleave with an upgrade another unlock is running: a key derived before that upgrade committed is refused, never installed over the one the store moved to.
is_encryption_enabled
Check if encryption is enabled in the database.
is_image_mime
Returns true if the provided MIME type is an image/*.
looks_encrypted
Check if a string looks like encrypted content (hex-encoded ChaCha20 output). Minimum (empty message): 12 + 0 + 16 = 28 bytes = 56 hex chars.
maybe_decrypt
Conditionally decrypt content. Handles crash recovery — if decryption fails on non-encrypted-looking content, returns it as-is.
maybe_decrypt_blob
Decrypt a secret key BLOB. Tolerant of the half-migrated DB: a 32-byte value is a raw (not-yet-wrapped / encryption-off) key, and anything that fails to authenticate is returned as-is, so a mixed store always reads back correctly.
maybe_decrypt_inner
Decrypt a hex-encoded ChaCha20-Poly1305 ciphertext using ENCRYPTION_KEY vault. If password is Some, derives a key from it instead.
maybe_decrypt_text
Decrypt a text field. A value that doesn’t look encrypted (or can’t be decrypted) is returned as-is — pre-migration rows and encryption-off rows.
maybe_encrypt_blob
Encrypt a secret key BLOB for at-rest storage. Off → unchanged. Enabled but the vault is empty → Err (never silently persists a secret in plaintext).
maybe_encrypt_text
Encrypt a text field for at-rest storage. Off → unchanged. Enabled but the vault is empty → Err.
mime_from_extension
Get MIME type from file extension.
mime_from_extension_safe
Convert a file extension to a MIME type, with an optional restriction to image/* types.
mime_from_magic_bytes
open_batch
Opens many at-rest strings for one vault read and one key schedule, where maybe_decrypt pays both per string (the vault read is nearly all of it). Sync, and the opener cannot leave the closure, so the key never crosses an await.
resolve_unique_filename
Resolve a unique filename in dir, appending -1, -2, etc. on collision.
sanitize_filename
Sanitize a filename for safe filesystem use. Strips path traversal, dangerous characters, and truncates to 64-char stem.
seal_batch
Seals many at-rest strings for one vault read, where maybe_encrypt_text pays it per string. Sync, so the caller’s at-rest ticket covers every seal and the write that follows.
sha256_hex
Calculate SHA-256 hash of data, returned as hex string.
still_png
An animation’s first frame as a PNG at its own size, so a page can swap one for the other without its layout moving.