1## SQLCipher
2
3SQLCipher extends the [SQLite](https://www.sqlite.org) database library to add security enhancements that make it more suitable for encrypted local data storage such as on-the-fly encryption, tamper evidence, and key derivation. Based on SQLite, SQLCipher closely tracks SQLite and periodically integrates stable SQLite release features.
4
5SQLCipher is maintained by Zetetic, LLC, the official site can be found [here](https://www.zetetic.net/sqlcipher/).
6
7## Features
8
9- Fast performance with as little as 5-15% overhead for encryption on many operations
10- 100% of data in the database file is encrypted
11- Good security practices (CBC mode, HMAC, key derivation)
12- Zero-configuration and application level cryptography
13- Algorithms provided by the peer reviewed OpenSSL crypto library.
14- Configurable crypto providers
15
16## Contributions
17
18We welcome contributions, to contribute to SQLCipher, a [contributor agreement](https://www.zetetic.net/contributions/) needs to be submitted.  All submissions should be based on the `prerelease` branch.
19
20## Compiling
21
22Building SQLCipher is almost the same as compiling a regular version of
23SQLite with two small exceptions:
24
25 1. You *must* define `SQLITE_HAS_CODEC` and `SQLITE_TEMP_STORE=2` when building sqlcipher.
26 2. If compiling against the default OpenSSL crypto provider, you will need to link libcrypto
27
28Example Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this
29example, `--enable-tempstore=yes` is setting `SQLITE_TEMP_STORE=2` for the build.
30
31	$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
32		LDFLAGS="/opt/local/lib/libcrypto.a"
33	$ make
34
35Example Dynamic linking
36
37	$ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
38		LDFLAGS="-lcrypto"
39	$ make
40
41## Encrypting a database
42
43To specify an encryption passphrase for the database via the SQL interface you
44use a pragma. The passphrase you enter is passed through PBKDF2 key derivation to
45obtain the encryption key for the database
46
47	PRAGMA key = 'passphrase';
48
49Alternately, you can specify an exact byte sequence using a blob literal. If you
50use this method it is your responsibility to ensure that the data you provide is a
5164 character hex string, which will be converted directly to 32 bytes (256 bits) of
52key data without key derivation.
53
54	PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
55
56To encrypt a database programatically you can use the `sqlite3_key` function.
57The data provided in `pKey` is converted to an encryption key according to the
58same rules as `PRAGMA key`.
59
60	int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
61
62`PRAGMA key` or `sqlite3_key` should be called as the first operation when a database is open.
63
64## Changing a database key
65
66To change the encryption passphrase for an existing database you may use the rekey pragma
67after you've supplied the correct database password;
68
69	PRAGMA key = 'passphrase'; -- start with the existing database passphrase
70	PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase
71
72The hex rekey pragma may be used to rekey to a specific binary value
73
74	PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
75
76This can be accomplished programtically by using sqlite3_rekey;
77
78	sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey)
79
80## Support
81
82The primary avenue for support and discussions is the SQLCipher discuss site:
83
84https://discuss.zetetic.net/c/sqlcipher
85
86Issues or support questions on using SQLCipher should be entered into the
87GitHub Issue tracker:
88
89https://github.com/sqlcipher/sqlcipher/issues
90
91Please DO NOT post issues, support questions, or other problems to blog
92posts about SQLCipher as we do not monitor them frequently.
93
94If you are using SQLCipher in your own software please let us know at
95support@zetetic.net!
96
97## License
98
99Copyright (c) 2016, ZETETIC LLC
100All rights reserved.
101
102Redistribution and use in source and binary forms, with or without
103modification, are permitted provided that the following conditions are met:
104    * Redistributions of source code must retain the above copyright
105      notice, this list of conditions and the following disclaimer.
106    * Redistributions in binary form must reproduce the above copyright
107      notice, this list of conditions and the following disclaimer in the
108      documentation and/or other materials provided with the distribution.
109    * Neither the name of the ZETETIC LLC nor the
110      names of its contributors may be used to endorse or promote products
111      derived from this software without specific prior written permission.
112
113THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
114EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
115WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
116DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
117DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
118(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
119LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
120ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
121(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
122SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
123