Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AES-XTS VFS #171

Merged
merged 6 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions vfs/adiantum/math.go → internal/util/math.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adiantum
package util

func abs(n int) int {
if n < 0 {
Expand All @@ -7,16 +7,16 @@ func abs(n int) int {
return n
}

func gcd(m, n int) int {
func GCD(m, n int) int {
for n != 0 {
m, n = n, m%n
}
return abs(m)
}

func lcm(m, n int) int {
func LCM(m, n int) int {
if n == 0 {
return 0
}
return abs(n) * (abs(m) / gcd(m, n))
return abs(n) * (abs(m) / GCD(m, n))
}
6 changes: 3 additions & 3 deletions vfs/adiantum/math_test.go → internal/util/math_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package adiantum
package util

ncruces marked this conversation as resolved.
Show resolved Hide resolved
import (
"math"
Expand Down Expand Up @@ -46,7 +46,7 @@ func Test_gcd(t *testing.T) {
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := gcd(tt.arg1, tt.arg2); got != tt.want {
if got := GCD(tt.arg1, tt.arg2); got != tt.want {
t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
}
})
Expand Down Expand Up @@ -74,7 +74,7 @@ func Test_lcm(t *testing.T) {
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := lcm(tt.arg1, tt.arg2); got != tt.want {
if got := LCM(tt.arg1, tt.arg2); got != tt.want {
t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
}
})
Expand Down
8 changes: 6 additions & 2 deletions vfs/adiantum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ and want to protect against forgery, you should sign your backups,
and verify signatures before restoring them.

This is slightly weaker than other forms of SQLite encryption
that include block-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code).
Block-level MACs can protect against forging individual blocks,
that include page-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code).
Page-level MACs can protect against forging individual pages,
but can't prevent them from being reverted to former versions of themselves.

> [!TIP]
> The [`"xts"`](../xts/README.md) package also offers encryption at rest.
> AES-XTS uses _only_ NIST and FIPS-140 approved cryptographic primitives.
5 changes: 5 additions & 0 deletions vfs/adiantum/adiantum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ func Test_fileformat(t *testing.T) {
if version != 0xBADDB {
t.Error(version)
}

_, err = db.Exec(`PRAGMA integrity_check`)
if err != nil {
t.Error(err)
}
}

func Benchmark_nokey(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion vfs/adiantum/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Register(name string, base vfs.VFS, cipher HBSHCreator) {
}
vfs.Register(name, &hbshVFS{
VFS: base,
hbsh: cipher,
init: cipher,
})
}

Expand Down
20 changes: 10 additions & 10 deletions vfs/adiantum/hbsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type hbshVFS struct {
vfs.VFS
hbsh HBSHCreator
init HBSHCreator
}

func (h *hbshVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, error) {
Expand All @@ -39,24 +39,24 @@ func (h *hbshVFS) OpenFilename(name *vfs.Filename, flags vfs.OpenFlag) (file vfs
} else {
var key []byte
if params := name.URIParameters(); name == nil {
key = h.hbsh.KDF("") // Temporary files get a random key.
key = h.init.KDF("") // Temporary files get a random key.
} else if t, ok := params["key"]; ok {
key = []byte(t[0])
} else if t, ok := params["hexkey"]; ok {
key, _ = hex.DecodeString(t[0])
} else if t, ok := params["textkey"]; ok {
key = h.hbsh.KDF(t[0])
key = h.init.KDF(t[0])
} else if flags&vfs.OPEN_MAIN_DB != 0 {
// Main datatabases may have their key specified as a PRAGMA.
return &hbshFile{File: file, reset: h.hbsh}, flags, nil
return &hbshFile{File: file, init: h.init}, flags, nil
}
hbsh = h.hbsh.HBSH(key)
hbsh = h.init.HBSH(key)
}

if hbsh == nil {
return nil, flags, sqlite3.CANTOPEN
}
return &hbshFile{File: file, hbsh: hbsh, reset: h.hbsh}, flags, nil
return &hbshFile{File: file, hbsh: hbsh, init: h.init}, flags, nil
}

const (
Expand All @@ -66,8 +66,8 @@ const (

type hbshFile struct {
vfs.File
init HBSHCreator
hbsh *hbsh.HBSH
reset HBSHCreator
tweak [tweakSize]byte
block [blockSize]byte
}
Expand All @@ -80,15 +80,15 @@ func (h *hbshFile) Pragma(name string, value string) (string, error) {
case "hexkey":
key, _ = hex.DecodeString(value)
case "textkey":
key = h.reset.KDF(value)
key = h.init.KDF(value)
default:
if f, ok := h.File.(vfs.FilePragma); ok {
return f.Pragma(name, value)
}
return "", sqlite3.NOTFOUND
}

if h.hbsh = h.reset.HBSH(key); h.hbsh != nil {
if h.hbsh = h.init.HBSH(key); h.hbsh != nil {
return "ok", nil
}
return "", sqlite3.CANTOPEN
Expand Down Expand Up @@ -187,7 +187,7 @@ func (h *hbshFile) Truncate(size int64) error {
}

func (h *hbshFile) SectorSize() int {
return lcm(h.File.SectorSize(), blockSize)
return util.LCM(h.File.SectorSize(), blockSize)
}

func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
Expand Down
Binary file modified vfs/adiantum/testdata/test.db
Binary file not shown.
47 changes: 47 additions & 0 deletions vfs/tests/mptest/mptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ncruces/go-sqlite3/vfs"
_ "github.com/ncruces/go-sqlite3/vfs/adiantum"
"github.com/ncruces/go-sqlite3/vfs/memdb"
_ "github.com/ncruces/go-sqlite3/vfs/xts"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
Expand Down Expand Up @@ -294,6 +295,52 @@ func Test_crash01_adiantum_wal(t *testing.T) {
mod.Close(ctx)
}

func Test_crash01_xts(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if os.Getenv("CI") != "" {
t.Skip("skipping in CI")
}
if !vfs.SupportsFileLocking {
t.Skip("skipping without locks")
}

ctx := util.NewContext(newContext(t))
name := "file:" + filepath.Join(t.TempDir(), "test.db") +
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
cfg := config(ctx).WithArgs("mptest", name, "crash01.test",
"--vfs", "xts")
mod, err := rt.InstantiateModule(ctx, module, cfg)
if err != nil {
t.Fatal(err)
}
mod.Close(ctx)
}

func Test_crash01_xts_wal(t *testing.T) {
if testing.Short() {
t.Skip("skipping in short mode")
}
if os.Getenv("CI") != "" {
t.Skip("skipping in CI")
}
if !vfs.SupportsSharedMemory {
t.Skip("skipping without shared memory")
}

ctx := util.NewContext(newContext(t))
name := "file:" + filepath.Join(t.TempDir(), "test.db") +
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
cfg := config(ctx).WithArgs("mptest", name, "crash01.test",
"--vfs", "xts", "--journalmode", "wal")
mod, err := rt.InstantiateModule(ctx, module, cfg)
if err != nil {
t.Fatal(err)
}
mod.Close(ctx)
}

func newContext(t *testing.T) context.Context {
return context.WithValue(context.Background(), logger{}, &testWriter{T: t})
}
Expand Down
20 changes: 20 additions & 0 deletions vfs/tests/speedtest1/speedtest1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/ncruces/go-sqlite3/vfs"
_ "github.com/ncruces/go-sqlite3/vfs/adiantum"
_ "github.com/ncruces/go-sqlite3/vfs/memdb"
_ "github.com/ncruces/go-sqlite3/vfs/xts"
)

//go:embed testdata/speedtest1.wasm.bz2
Expand Down Expand Up @@ -125,3 +126,22 @@ func Benchmark_adiantum(b *testing.B) {
}
mod.Close(ctx)
}

func Benchmark_xts(b *testing.B) {
output.Reset()
ctx := util.NewContext(context.Background())
name := "file:" + filepath.Join(b.TempDir(), "test.db") +
"?hexkey=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
args := append(options, "--vfs", "xts", "--size", strconv.Itoa(b.N), name)
cfg := wazero.NewModuleConfig().
WithArgs(args...).WithName("speedtest1").
WithStdout(&output).WithStderr(&output).
WithSysWalltime().WithSysNanotime().WithSysNanosleep().
WithOsyield(runtime.Gosched).
WithRandSource(rand.Reader)
mod, err := rt.InstantiateModule(ctx, module, cfg)
if err != nil {
b.Fatal(err)
}
mod.Close(ctx)
}
63 changes: 63 additions & 0 deletions vfs/xts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Go `xts` SQLite VFS

This package wraps an SQLite VFS to offer encryption at rest.

The `"xts"` VFS wraps the default SQLite VFS using the
[AES-XTS](https://pkg.go.dev/golang.org/x/crypto/xts)
tweakable and length-preserving encryption.\
In general, any XTS construction can be used to wrap any VFS.

The default AES-XTS construction uses AES-128, AES-192 or AES-256
for its block cipher.
Additionally, we use [PBKDF2-HMAC-SHA512](https://pkg.go.dev/golang.org/x/crypto/pbkdf2)
to derive AES-128 keys from plain text where needed.
File contents are encrypted in 512 byte sectors, matching the
[minimum](https://sqlite.org/fileformat.html#pages) SQLite page size.

The VFS encrypts all files _except_
[super journals](https://sqlite.org/tempfiles.html#super_journal_files):
these _never_ contain database data, only filenames,
and padding them to the sector size is problematic.
Temporary files _are_ encrypted with **random** AES-128 keys,
as they _may_ contain database data.
To avoid the overhead of encrypting temporary files,
keep them in memory:

PRAGMA temp_store = memory;

> [!IMPORTANT]
> XTS is a cipher mode typically used for disk encryption.
> The standard threat model for disk encryption considers an adversary
> that can read multiple snapshots of a disk.
> The only security property that disk encryption provides
> is that all information such an adversary can obtain
> is whether the data in a sector has or has not changed over time.

The encryption offered by this package is fully deterministic.

This means that an adversary who can get ahold of multiple snapshots
(e.g. backups) of a database file can learn precisely:
which sectors changed, which ones didn't, which got reverted.

This is slightly weaker than other forms of SQLite encryption
that include *some* nondeterminism; with limited nondeterminism,
an adversary can't distinguish between
sectors that actually changed, and sectors that got reverted.

> [!CAUTION]
> This package does not claim protect databases against tampering or forgery.

The major practical consequence of the above point is that,
if you're keeping `"xts"` encrypted backups of your database,
and want to protect against forgery, you should sign your backups,
and verify signatures before restoring them.

This is slightly weaker than other forms of SQLite encryption
that include page-level [MACs](https://en.wikipedia.org/wiki/Message_authentication_code).
Page-level MACs can protect against forging individual pages,
but can't prevent them from being reverted to former versions of themselves.

> [!TIP]
> The [`"adiantum"`](../adiantum/README.md) package also offers encryption at rest.
> In general Adiantum performs significantly better,
> and as a "wide-block" cipher, _may_ offer improved security.
34 changes: 34 additions & 0 deletions vfs/xts/aes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package xts

import (
"crypto/aes"
"crypto/rand"
"crypto/sha512"

"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/xts"
)

// This variable can be replaced with -ldflags:
//
// go build -ldflags="-X github.com/ncruces/go-sqlite3/vfs/xts.pepper=xts"
var pepper = "github.com/ncruces/go-sqlite3/vfs/xts"

type aesCreator struct{}

func (aesCreator) XTS(key []byte) *xts.Cipher {
c, err := xts.NewCipher(aes.NewCipher, key)
if err != nil {
return nil
}
return c
}

func (aesCreator) KDF(text string) []byte {
if text == "" {
key := make([]byte, 32)
n, _ := rand.Read(key)
return key[:n]
}
return pbkdf2.Key([]byte(text), []byte(pepper), 10_000, 32, sha512.New)
}
Loading
Loading