Skip to content

Commit 9c966ed

Browse files
committed
Docs.
1 parent 3c4283e commit 9c966ed

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

vfs/adiantum/README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ and want to protect against forgery, you should sign your backups,
5353
and verify signatures before restoring them.
5454

5555
This is slightly weaker than other forms of SQLite encryption
56-
that include block-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code).
57-
Block-level MACs can protect against forging individual blocks,
56+
that include page-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code).
57+
Page-level MACs can protect against forging individual pages,
5858
but can't prevent them from being reverted to former versions of themselves.
59+
60+
> [!NOTICE]
61+
> The [`"xts"`](../xts/README.md) package also offers encryption at rest.
62+
63+
AES-XTS uses _only_ NIST and FIPS-140 approved cryptographic primitives.

vfs/xts/README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,64 @@
11
# Go `xts` SQLite VFS
22

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.

vfs/xts/api.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
11
// Package xts wraps an SQLite VFS to offer encryption at rest.
2+
//
3+
// The "xts" [vfs.VFS] wraps the default VFS using the
4+
// AES-XTS tweakable, length-preserving encryption.
5+
//
6+
// Importing package xts registers that VFS:
7+
//
8+
// import _ "github.com/ncruces/go-sqlite3/vfs/xts"
9+
//
10+
// To open an encrypted database you need to provide key material.
11+
//
12+
// The simplest way to do that is to specify the key through an [URI] parameter:
13+
//
14+
// - key: key material in binary (32, 48 or 64 bytes)
15+
// - hexkey: key material in hex (64, 96 or 128 hex digits)
16+
// - textkey: key material in text (any length)
17+
//
18+
// However, this makes your key easily accessible to other parts of
19+
// your application (e.g. through [vfs.Filename.URIParameters]).
20+
//
21+
// To avoid this, invoke any of the following PRAGMAs
22+
// immediately after opening a connection:
23+
//
24+
// PRAGMA key='D41d8cD98f00b204e9800998eCf8427e';
25+
// PRAGMA hexkey='e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855';
26+
// PRAGMA textkey='your-secret-key';
27+
//
28+
// For an ATTACH-ed database, you must specify the schema name:
29+
//
30+
// ATTACH DATABASE 'demo.db' AS demo;
31+
// PRAGMA demo.textkey='your-secret-key';
32+
//
33+
// [URI]: https://sqlite.org/uri.html
234
package xts
335

436
import (

0 commit comments

Comments
 (0)