|
1 | 1 | # Go `xts` SQLite VFS
|
2 | 2 |
|
3 |
| -This package wraps an SQLite VFS to offer encryption at rest. |
| 3 | +This package wraps an SQLite VFS to offer encryption at rest. |
| 4 | + |
| 5 | +The `"xts"` VFS wraps the default SQLite VFS using the |
| 6 | +[AES-XTS](https://pkg.go.dev/golang.org/x/crypto/xts) |
| 7 | +tweakable and length-preserving encryption.\ |
| 8 | +In general, any XTS construction can be used to wrap any VFS. |
| 9 | + |
| 10 | +The default AES-XTS construction uses AES-128, AES-192 or AES-256 |
| 11 | +for its block cipher. |
| 12 | +Additionally, we use [PBKDF2-HMAC-SHA512](https://pkg.go.dev/golang.org/x/crypto/pbkdf2) |
| 13 | +to derive AES-128 keys from plain text where needed. |
| 14 | +File contents are encrypted in 512 byte sectors, matching the |
| 15 | +[minimum](https://sqlite.org/fileformat.html#pages) SQLite page size. |
| 16 | + |
| 17 | +The VFS encrypts all files _except_ |
| 18 | +[super journals](https://sqlite.org/tempfiles.html#super_journal_files): |
| 19 | +these _never_ contain database data, only filenames, |
| 20 | +and padding them to the sector size is problematic. |
| 21 | +Temporary files _are_ encrypted with **random** AES-128 keys, |
| 22 | +as they _may_ contain database data. |
| 23 | +To avoid the overhead of encrypting temporary files, |
| 24 | +keep them in memory: |
| 25 | + |
| 26 | + PRAGMA temp_store = memory; |
| 27 | + |
| 28 | +> [!IMPORTANT] |
| 29 | +> XTS is a cipher mode typically used for disk encryption. |
| 30 | +> The standard threat model for disk encryption considers an adversary |
| 31 | +> that can read multiple snapshots of a disk. |
| 32 | +> The only security property that disk encryption provides |
| 33 | +> is that all information such an adversary can obtain |
| 34 | +> is whether the data in a sector has or has not changed over time. |
| 35 | +
|
| 36 | +The encryption offered by this package is fully deterministic. |
| 37 | + |
| 38 | +This means that an adversary who can get ahold of multiple snapshots |
| 39 | +(e.g. backups) of a database file can learn precisely: |
| 40 | +which sectors changed, which ones didn't, which got reverted. |
| 41 | + |
| 42 | +This is slightly weaker than other forms of SQLite encryption |
| 43 | +that include *some* nondeterminism; with limited nondeterminism, |
| 44 | +an adversary can't distinguish between |
| 45 | +sectors that actually changed, and sectors that got reverted. |
| 46 | + |
| 47 | +> [!CAUTION] |
| 48 | +> This package does not claim protect databases against tampering or forgery. |
| 49 | +
|
| 50 | +The major practical consequence of the above point is that, |
| 51 | +if you're keeping `"xts"` encrypted backups of your database, |
| 52 | +and want to protect against forgery, you should sign your backups, |
| 53 | +and verify signatures before restoring them. |
| 54 | + |
| 55 | +This is slightly weaker than other forms of SQLite encryption |
| 56 | +that include page-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code). |
| 57 | +Page-level MACs can protect against forging individual pages, |
| 58 | +but can't prevent them from being reverted to former versions of themselves. |
| 59 | + |
| 60 | +> [!NOTICE] |
| 61 | +> The [`"adiantum"`](../adiantum/README.md) package also offers encryption at rest. |
| 62 | +
|
| 63 | +In general Adiantum performs significantly better, |
| 64 | +and is a "wide-block" cipher which _may_ offer improved security. |
0 commit comments