Expand description
sqlite3mc encryption configuration.
§Encryption flow
This crate uses sqlite3mc (SQLite3 Multiple Ciphers) to encrypt
SQLite databases at rest. The encryption is transparent to SQL – once a
database is opened and keyed, all reads and writes are automatically
encrypted/decrypted by the SQLite pager layer.
The flow when opening a database is:
-
Open –
sqlite3_open_v2creates or opens the database file. At this point the file is opaque (encrypted) and no data can be read. -
Configure cipher –
PRAGMA cipher = 'chacha20'fixes the on-disk cipher before the key activates it. -
Detect and encrypt or unlock – A read from
sqlite_mastersucceeds for a plaintext (or new) database. Such a database is moved out of WAL mode and atomically encrypted withPRAGMA rekey. Existing databases with a fully encrypted header are unlocked using their old settings and migrated in place. Bothkeyandrekeyreceive the 32-byteK_intermediateas a raw hex key, bypassing the passphrase KDF. -
Verify – We read from
sqlite_masterafter rekeying or keying. A wrong key returnsSQLITE_NOTADBbecause the decrypted page header does not match the expectedSQLitemagic bytes. -
Configure connection – The target-specific journal mode and every connection-level invariant are set and verified.
The default cipher is ChaCha20-Poly1305 (authenticated encryption).
All crypto is built into the sqlite3mc amalgamation – no OpenSSL or
other external crypto library is needed on any platform.
The first 32 bytes of every database header remain plaintext. This gives all targets one on-disk format and lets iOS recognize shared-container databases in WAL mode. Existing databases with fully encrypted headers are migrated in place on their first successful open.
Functions§
- export_
plaintext_ copy - Creates a plaintext (unencrypted) copy of an already-open encrypted database.
- import_
plaintext_ copy - Imports data from a plaintext (unencrypted) database into an already-open encrypted database.
- integrity_
check - Runs
PRAGMA integrity_checkand returns whether the database is healthy. - open_
encrypted - Opens a writable database, applies the encryption key, and configures the connection.