Skip to content

Commit 014653a

Browse files
committed
sqlite3: add sqlcipher support
Port SQLCipher support from mattn/go-sqlite3 PR mattn#1109 by Jonathan Giannuzzi onto current upstream master. Embed SQLCipher 4.16.0 and keep the fork module path as github.com/mattn/go-sqlite3. Downstream imports can stay unchanged behind a replace directive. Update the SQLCipher upgrade helper to build modern upstream releases without the removed userauth extension. The Homebrew path change and merge commit from the PR are intentionally omitted. Crypto provider: Use CommonCrypto on Darwin and LibTomCrypt on non-Darwin platforms so embedded SQLCipher builds no longer depend on OpenSSL/libcrypto at runtime, making static compilation work, avoiding the need to ship libcrypto or openssl shared libs / DLL files per platform. Add generated vendorable LibTomCrypt bindings from the submodule, document the provider split, and wire CI and upgrade tooling for the provider setup.
1 parent a40eeff commit 014653a

467 files changed

Lines changed: 361081 additions & 88 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/go.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jobs:
3636
echo "$(go env GOPATH)/bin" >> "$GITHUB_PATH"
3737
3838
- uses: actions/checkout@v6
39+
with:
40+
submodules: true
3941

4042
- name: 'Tags: default'
4143
run: go-acc . -- -race -v -tags ""
@@ -49,6 +51,9 @@ jobs:
4951
- name: 'Tags: vacuum'
5052
run: go-acc . -- -race -v -tags "sqlite_vacuum_full"
5153

54+
- name: 'Tags: sqlcipher'
55+
run: go-acc . -- -race -v -tags "sqlcipher"
56+
5257
- name: Upload coverage to Codecov
5358
uses: codecov/codecov-action@v5
5459
with:
@@ -87,6 +92,8 @@ jobs:
8792
shell: msys2 {0}
8893

8994
- uses: actions/checkout@v6
95+
with:
96+
submodules: true
9097

9198
- name: 'Tags: default'
9299
run: go build -race -v -tags ""
@@ -106,6 +113,10 @@ jobs:
106113
run: go build -race -v -tags "sqlite_vacuum_full"
107114
shell: msys2 {0}
108115

116+
- name: 'Tags: sqlcipher'
117+
run: go build -race -v -tags "sqlcipher"
118+
shell: msys2 {0}
119+
109120
- name: Upload coverage to Codecov
110121
uses: codecov/codecov-action@v5
111122
with:

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "internal/libtomcrypt/src"]
2+
path = internal/libtomcrypt/src
3+
url = https://github.com/sqlcipher/libtomcrypt.git

LICENSE.LIBTOMCRYPT

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
LibTomCrypt is licensed under DUAL licensing terms.
2+
3+
Choose and use the license of your needs.
4+
5+
[LICENSE #1]
6+
7+
LibTomCrypt is public domain. As should all quality software be.
8+
9+
Tom St Denis
10+
11+
[/LICENSE #1]
12+
13+
[LICENSE #2]
14+
15+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
16+
Version 2, December 2004
17+
18+
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
19+
20+
Everyone is permitted to copy and distribute verbatim or modified
21+
copies of this license document, and changing it is allowed as long
22+
as the name is changed.
23+
24+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
25+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
26+
27+
0. You just DO WHAT THE FUCK YOU WANT TO.
28+
29+
[/LICENSE #2]

LICENSE.SQLCIPHER

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
SQLCipher
2+
http://zetetic.net
3+
4+
Copyright (c) 2008-2024, ZETETIC LLC
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without
8+
modification, are permitted provided that the following conditions are met:
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright
12+
notice, this list of conditions and the following disclaimer in the
13+
documentation and/or other materials provided with the distribution.
14+
* Neither the name of the ZETETIC LLC nor the
15+
names of its contributors may be used to endorse or promote products
16+
derived from this software without specific prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
19+
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
22+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
go-sqlite3-sqlcipher fork
2+
=========================
3+
4+
This repository is a fork of [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
5+
that adds SQLCipher support. The Go module path remains
6+
`github.com/mattn/go-sqlite3` so existing imports continue to work with a
7+
`replace` directive.
8+
9+
Replace the module with this fork:
10+
11+
```go
12+
require github.com/mattn/go-sqlite3 v1.14.47
13+
14+
replace github.com/mattn/go-sqlite3 => github.com/BitBoxSwiss/go-sqlite3-sqlcipher <version>
15+
```
16+
17+
Build with the `sqlcipher` tag to use the embedded SQLCipher amalgamation:
18+
19+
```sh
20+
go build -tags sqlcipher ./...
21+
```
22+
23+
The embedded `sqlcipher` tag uses these crypto providers:
24+
25+
| Platform | Crypto provider | External runtime crypto dependency |
26+
| --- | --- | --- |
27+
| iOS/macOS | CommonCrypto via Apple frameworks | No |
28+
| Android | LibTomCrypt from the submodule source | No |
29+
| Linux | LibTomCrypt from the submodule source | No |
30+
| Windows | LibTomCrypt from the submodule source, with `advapi32` for RNG seeding | No OpenSSL DLL |
31+
| `libsqlcipher` tag | Determined by the linked external `libsqlcipher` | Depends on that library |
32+
33+
Initialize the LibTomCrypt submodule when working on the fork:
34+
35+
```sh
36+
git submodule update --init
37+
```
38+
39+
The fork checks in generated LibTomCrypt binding files under
40+
`internal/libtomcrypt` so downstream `go mod vendor` users can build with
41+
`-mod=vendor` without initializing this submodule.
42+
43+
This fork is based on the original work by Yasuhiro Matsumoto and the
44+
`mattn/go-sqlite3` contributors. SQLCipher support is based on
45+
[PR #1109](https://github.com/mattn/go-sqlite3/pull/1109) by
46+
[jgiannuzzi](https://github.com/jgiannuzzi).
47+
148
go-sqlite3
249
==========
350

@@ -41,9 +88,12 @@ This package follows the official [Golang Release Policy](https://golang.org/doc
4188
- [macOS](#mac-osx)
4289
- [Windows](#windows)
4390
- [Errors](#errors)
44-
- [User Authentication](#user-authentication)
91+
- [Encryption](#encryption)
4592
- [Compile](#compile)
4693
- [Usage](#usage-1)
94+
- [User Authentication](#user-authentication)
95+
- [Compile](#compile-1)
96+
- [Usage](#usage-2)
4797
- [Create protected database](#create-protected-database)
4898
- [Password Encoding](#password-encoding)
4999
- [Available Encoders](#available-encoders)
@@ -106,11 +156,17 @@ Boolean values can be one of:
106156
| Auto Vacuum | `_auto_vacuum` \| `_vacuum` | <ul><li>`0` \| `none`</li><li>`1` \| `full`</li><li>`2` \| `incremental`</li></ul> | For more information see [PRAGMA auto_vacuum](https://www.sqlite.org/pragma.html#pragma_auto_vacuum) |
107157
| Busy Timeout | `_busy_timeout` \| `_timeout` | `int` | Specify value for sqlite3_busy_timeout. For more information see [PRAGMA busy_timeout](https://www.sqlite.org/pragma.html#pragma_busy_timeout) |
108158
| Case Sensitive LIKE | `_case_sensitive_like` \| `_cslike` | `boolean` | For more information see [PRAGMA case_sensitive_like](https://www.sqlite.org/pragma.html#pragma_case_sensitive_like) |
159+
| Cipher Compatibility | `_cipher_compatibility` | `int` | For more information see [PRAGMA cipher_compatibility](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_compatibility) |
160+
| Cipher Migrate | `_cipher_migrate` | - | For more information see [PRAGMA cipher_migrate](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_migrate) |
161+
| Cipher Page Size | `_cipher_page_size` | `int` | For more information see [PRAGMA cipher_page_size](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_page_size) |
162+
| Cipher Plaintext Header Size | `_cipher_plaintext_header_size` | `int` | For more information see [PRAGMA cipher_plaintext_header_size](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_plaintext_header_size) |
163+
| Cipher Use HMAC | `_cipher_use_hmac` | `int` | For more information see [PRAGMA cipher_use_hmac](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_use_hmac) |
109164
| Defer Foreign Keys | `_defer_foreign_keys` \| `_defer_fk` | `boolean` | For more information see [PRAGMA defer_foreign_keys](https://www.sqlite.org/pragma.html#pragma_defer_foreign_keys) |
110165
| Foreign Keys | `_foreign_keys` \| `_fk` | `boolean` | For more information see [PRAGMA foreign_keys](https://www.sqlite.org/pragma.html#pragma_foreign_keys) |
111166
| Ignore CHECK Constraints | `_ignore_check_constraints` | `boolean` | For more information see [PRAGMA ignore_check_constraints](https://www.sqlite.org/pragma.html#pragma_ignore_check_constraints) |
112167
| Immutable | `immutable` | `boolean` | For more information see [Immutable](https://www.sqlite.org/c3ref/open.html) |
113168
| Journal Mode | `_journal_mode` \| `_journal` | <ul><li>DELETE</li><li>TRUNCATE</li><li>PERSIST</li><li>MEMORY</li><li>WAL</li><li>OFF</li></ul> | For more information see [PRAGMA journal_mode](https://www.sqlite.org/pragma.html#pragma_journal_mode) |
169+
| Encryption Key | `_key` | `string` | Sets the database encryption key to use with [SQLCipher](https://github.com/sqlcipher/sqlcipher). For more information see [PRAGMA key](https://www.zetetic.net/sqlcipher/sqlcipher-api/#PRAGMA_key)
114170
| Locking Mode | `_locking_mode` \| `_locking` | <ul><li>NORMAL</li><li>EXCLUSIVE</li></ul> | For more information see [PRAGMA locking_mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) |
115171
| Mode | `mode` | <ul><li>ro</li><li>rw</li><li>rwc</li><li>memory</li></ul> | Access Mode of the database. For more information see [SQLite Open](https://www.sqlite.org/c3ref/open.html) |
116172
| Mutex Locking | `_mutex` | <ul><li>no</li><li>full</li></ul> | Specify mutex mode. |
@@ -350,6 +406,18 @@ For example the TDM-GCC Toolchain can be found [here](https://jmeubank.github.io
350406
go install github.com/mattn/go-sqlite3
351407
```
352408
409+
# Encryption
410+
411+
## Compile
412+
413+
To use the database encryption feature, the package has to be compiled with the tags `sqlcipher` (to use the built-in implementation) or `libsqlcipher` (to link directly to libsqlcipher).
414+
415+
The built-in implementation requires OpenSSL to be installed (`libssl-dev` or `openssl-devel` on Linux). It is not required on macOS, where CommonCrypto gets used and is part of the system.
416+
417+
### Usage
418+
419+
Pass your encryption key via the `_key` argument in the connection string. See the [SQLCipher documentation](https://www.zetetic.net/sqlcipher/sqlcipher-api/#PRAGMA_key) for more details.
420+
353421
# User Authentication
354422
355423
***This is deprecated***
@@ -594,6 +662,16 @@ The -binding suffix was added to avoid build failures under gccgo.
594662

595663
In this repository, those files are an amalgamation of code that was copied from SQLite3. The license of that code is the same as the license of SQLite3.
596664

665+
sqlcipher-binding.c and sqlcipher-binding.h are an amalgamation of code that
666+
was copied from [SQLCipher](https://github.com/sqlcipher/sqlcipher). See
667+
[LICENSE.SQLCIPHER](./LICENSE.SQLCIPHER) for the SQLCipher copyright notice
668+
and license terms.
669+
670+
internal/libtomcrypt contains generated files copied from
671+
[LibTomCrypt](https://github.com/sqlcipher/libtomcrypt). See
672+
[LICENSE.LIBTOMCRYPT](./LICENSE.LIBTOMCRYPT) for the LibTomCrypt license
673+
terms.
674+
597675
# Author
598676

599677
Yasuhiro Matsumoto (a.k.a mattn)

backup.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
package sqlite3
77

88
/*
9-
#ifndef USE_LIBSQLITE3
10-
#include "sqlite3-binding.h"
11-
#else
9+
#if defined(USE_LIBSQLITE3)
1210
#include <sqlite3.h>
11+
#elif defined(USE_LIBSQLCIPHER)
12+
#include <sqlcipher/sqlite3.h>
13+
#elif defined(USE_SQLCIPHER)
14+
#include "sqlcipher-binding.h"
15+
#else
16+
#include "sqlite3-binding.h"
1317
#endif
1418
#include <stdlib.h>
1519
*/

callback.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ package sqlite3
1111
// code for SQLite custom functions is in here.
1212

1313
/*
14-
#ifndef USE_LIBSQLITE3
15-
#include "sqlite3-binding.h"
16-
#else
14+
#if defined(USE_LIBSQLITE3)
1715
#include <sqlite3.h>
16+
#elif defined(USE_LIBSQLCIPHER)
17+
#include <sqlcipher/sqlite3.h>
18+
#elif defined(USE_SQLCIPHER)
19+
#include "sqlcipher-binding.h"
20+
#else
21+
#include "sqlite3-binding.h"
1822
#endif
1923
#include <stdlib.h>
2024

error.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66
package sqlite3
77

88
/*
9-
#ifndef USE_LIBSQLITE3
10-
#include "sqlite3-binding.h"
11-
#else
9+
#if defined(USE_LIBSQLITE3)
1210
#include <sqlite3.h>
11+
#elif defined(USE_LIBSQLCIPHER)
12+
#include <sqlcipher/sqlite3.h>
13+
#elif defined(USE_SQLCIPHER)
14+
#include "sqlcipher-binding.h"
15+
#else
16+
#include "sqlite3-binding.h"
1317
#endif
1418
*/
1519
import "C"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (C) 2026 BitBoxSwiss.
2+
//
3+
// Use of this source code is governed by an MIT-style
4+
// license that can be found in the LICENSE file.
5+
6+
//go:build sqlcipher && !libsqlcipher && !darwin
7+
// +build sqlcipher,!libsqlcipher,!darwin
8+
9+
package libtomcrypt
10+
11+
/*
12+
#cgo CFLAGS: -DLTC_SOURCE
13+
#cgo CFLAGS: -I${SRCDIR}
14+
#cgo windows LDFLAGS: -ladvapi32
15+
*/
16+
import "C"

0 commit comments

Comments
 (0)