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.
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.
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.
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.
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_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 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.
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.