allow files to be picked up in amalgamation
[sqlcipher.git] / README.md
blob7b392341913d66c32478a0f69f64385ab58e03b5
1 ## SQLCipher
3 SQLCipher 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.
5 SQLCipher is maintained by Zetetic, LLC, and additional information and documentation is available on the official [SQLCipher site](https://www.zetetic.net/sqlcipher/).
7 ## Features
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
16 ## Compatibility
18 SQLCipher maintains database format compatibility within the same major version number so an application on any platform can open databases created by any other application provided the major version of SQLCipher is the same between them. However, major version updates (e.g. from 3.x to 4.x) often include changes to default settings. This means that newer major versions of SQLCipher will not open databases created by older versions without using special settings. For example, SQLCipher 4 introduces many new performance and security enhancements. The new default algorithms, increased KDF iterations, and larger page size mean that SQLCipher 4 will not open databases created by SQLCipher 1.x, 2.x, or 3.x by default. Instead, an application would either need to migrate the older databases to use the new format or enable a special backwards-compatibility mode. The available options are described in SQLCipher's [upgrade documentation](https://discuss.zetetic.net/t/upgrading-to-sqlcipher-4/3283). 
20 SQLCipher is also compatible with standard SQLite databases. When a key is not provided, SQLCipher will behave just like the standard SQLite library. It is also possible to convert from a plaintext database (standard SQLite) to an encrypted SQLCipher database using [ATTACH and the sqlcipher_export() convenience function](https://discuss.zetetic.net/t/how-to-encrypt-a-plaintext-sqlite-database-to-use-sqlcipher-and-avoid-file-is-encrypted-or-is-not-a-database-errors/868).
22 ## Contributions
24 The SQLCipher team welcomes contributions to the core library. All contributions including pull requests and patches should be based on the `prerelease` branch, and must be accompanied by a [contributor agreement](https://www.zetetic.net/contributions/). For large changes we strongly encourage [discussion](https://discuss.zetetic.net/c/sqlcipher) of the proposed change prior to development and submission.
26 ## Compiling
28 Building SQLCipher is almost the same as compiling a regular version of 
29 SQLite with two small exceptions: 
31  1. You *must* define `SQLITE_HAS_CODEC` and `SQLITE_TEMP_STORE=2` when building sqlcipher. 
32  2. If compiling against the default OpenSSL crypto provider, you will need to link libcrypto
34 Example Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this 
35 example, `--enable-tempstore=yes` is setting `SQLITE_TEMP_STORE=2` for the build.
37         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
38                 LDFLAGS="/opt/local/lib/libcrypto.a"
39         $ make
41 Example Dynamic linking
43         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
44                 LDFLAGS="-lcrypto"
45         $ make
47 ## Encrypting a database
49 To specify an encryption passphrase for the database via the SQL interface you 
50 use a pragma. The passphrase you enter is passed through PBKDF2 key derivation to
51 obtain the encryption key for the database 
53         PRAGMA key = 'passphrase';
55 Alternately, you can specify an exact byte sequence using a blob literal. If you
56 use this method it is your responsibility to ensure that the data you provide is a
57 64 character hex string, which will be converted directly to 32 bytes (256 bits) of 
58 key data without key derivation.
60         PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
62 To encrypt a database programmatically you can use the `sqlite3_key` function. 
63 The data provided in `pKey` is converted to an encryption key according to the 
64 same rules as `PRAGMA key`. 
66         int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
68 `PRAGMA key` or `sqlite3_key` should be called as the first operation when a database is open.
70 ## Changing a database key
72 To change the encryption passphrase for an existing database you may use the rekey pragma
73 after you've supplied the correct database password;
75         PRAGMA key = 'passphrase'; -- start with the existing database passphrase
76         PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase
78 The hex rekey pragma may be used to rekey to a specific binary value
80         PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
82 This can be accomplished programmatically by using sqlite3_rekey;
83   
84         sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey)
86 ## Support
88 The primary avenue for support and discussions is the SQLCipher discuss site:
90 https://discuss.zetetic.net/c/sqlcipher
92 Issues or support questions on using SQLCipher should be entered into the 
93 GitHub Issue tracker:
95 https://github.com/sqlcipher/sqlcipher/issues
97 Please DO NOT post issues, support questions, or other problems to blog 
98 posts about SQLCipher as we do not monitor them frequently.
100 If you are using SQLCipher in your own software please let us know at 
101 support@zetetic.net!
103 ## License
105 Copyright (c) 2016, ZETETIC LLC
106 All rights reserved.
108 Redistribution and use in source and binary forms, with or without
109 modification, are permitted provided that the following conditions are met:
110     * Redistributions of source code must retain the above copyright
111       notice, this list of conditions and the following disclaimer.
112     * Redistributions in binary form must reproduce the above copyright
113       notice, this list of conditions and the following disclaimer in the
114       documentation and/or other materials provided with the distribution.
115     * Neither the name of the ZETETIC LLC nor the
116       names of its contributors may be used to endorse or promote products
117       derived from this software without specific prior written permission.
119 THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
120 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
121 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
122 DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
123 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
124 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
125 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
126 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
127 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
128 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
130 # Begin SQLite README.md
132 <h1 align="center">SQLite Source Repository</h1>
134 This repository contains the complete source code for the 
135 [SQLite database engine](https://sqlite.org/).  Some test scripts 
136 are also included.  However, many other test scripts
137 and most of the documentation are managed separately.
139 ## Version Control
141 SQLite sources are managed using the
142 [Fossil](https://www.fossil-scm.org/), a distributed version control system
143 that was specifically designed and written to support SQLite development.
144 The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext.
146 If you are reading this on GitHub or some other Git repository or service,
147 then you are looking at a mirror.  The names of check-ins and
148 other artifacts in a Git mirror are different from the official
149 names for those objects.  The offical names for check-ins are
150 found in a footer on the check-in comment for authorized mirrors.
151 The official check-in name can also be seen in the `manifest.uuid` file
152 in the root of the tree.  Always use the official name, not  the
153 Git-name, when communicating about an SQLite check-in.
155 If you pulled your SQLite source code from a secondary source and want to
156 verify its integrity, there are hints on how to do that in the
157 [Verifying Code Authenticity](#vauth) section below.
159 ## Obtaining The Code
161 If you do not want to use Fossil, you can download tarballs or ZIP
162 archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
164   *  Lastest trunk check-in as
165      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
166      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
167      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
169   *  Latest release as
170      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
171      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
172      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
174   *  For other check-ins, substitute an appropriate branch name or
175      tag or hash prefix in place of "release" in the URLs of the previous
176      bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
177      to locate the check-in desired, click on its information page link,
178      then click on the "Tarball" or "ZIP Archive" links on the information
179      page.
181 If you do want to use Fossil to check out the source tree, 
182 first install Fossil version 2.0 or later.
183 (Source tarballs and precompiled binaries available
184 [here](https://www.fossil-scm.org/fossil/uv/download.html).  Fossil is
185 a stand-alone program.  To install, simply download or build the single 
186 executable file and put that file someplace on your $PATH.)
187 Then run commands like this:
189         mkdir ~/sqlite
190         cd ~/sqlite
191         fossil clone https://www.sqlite.org/src sqlite.fossil
192         fossil open sqlite.fossil
193     
194 After setting up a repository using the steps above, you can always
195 update to the lastest version using:
197         fossil update trunk   ;# latest trunk check-in
198         fossil update release ;# latest official release
200 Or type "fossil ui" to get a web-based user interface.
202 ## Compiling
204 First create a directory in which to place
205 the build products.  It is recommended, but not required, that the
206 build directory be separate from the source directory.  Cd into the
207 build directory and then from the build directory run the configure
208 script found at the root of the source tree.  Then run "make".
210 For example:
212         tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
213         mkdir bld                ;#  Build will occur in a sibling directory
214         cd bld                   ;#  Change to the build directory
215         ../sqlite/configure      ;#  Run the configure script
216         make                     ;#  Run the makefile.
217         make sqlite3.c           ;#  Build the "amalgamation" source file
218         make test                ;#  Run some tests (requires Tcl)
220 See the makefile for additional targets.
222 The configure script uses autoconf 2.61 and libtool.  If the configure
223 script does not work out for you, there is a generic makefile named
224 "Makefile.linux-gcc" in the top directory of the source tree that you
225 can copy and edit to suit your needs.  Comments on the generic makefile
226 show what changes are needed.
228 ## Using MSVC
230 On Windows, all applicable build products can be compiled with MSVC.
231 First open the command prompt window associated with the desired compiler
232 version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
233 with the provided "Makefile.msc" to build one of the supported targets.
235 For example:
237         mkdir bld
238         cd bld
239         nmake /f Makefile.msc TOP=..\sqlite
240         nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
241         nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
242         nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
243         nmake /f Makefile.msc test TOP=..\sqlite
245 There are several build options that can be set via the NMAKE command
246 line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
247 to the "sqlite3.dll" command line above.  When debugging into the SQLite
248 code, adding the "DEBUG=1" argument to one of the above command lines is
249 recommended.
251 SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
252 is required by the makefiles (including those for MSVC).  SQLite contains
253 a lot of generated code and Tcl is used to do much of that code generation.
255 ## Source Code Tour
257 Most of the core source files are in the **src/** subdirectory.  The
258 **src/** folder also contains files used to build the "testfixture" test
259 harness. The names of the source files used by "testfixture" all begin
260 with "test".
261 The **src/** also contains the "shell.c" file
262 which is the main program for the "sqlite3.exe"
263 [command-line shell](https://sqlite.org/cli.html) and
264 the "tclsqlite.c" file which implements the
265 [Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite.
266 (Historical note:  SQLite began as a Tcl
267 extension and only later escaped to the wild as an independent library.)
269 Test scripts and programs are found in the **test/** subdirectory.
270 Addtional test code is found in other source repositories.
271 See [How SQLite Is Tested](http://www.sqlite.org/testing.html) for
272 additional information.
274 The **ext/** subdirectory contains code for extensions.  The
275 Full-text search engine is in **ext/fts3**.  The R-Tree engine is in
276 **ext/rtree**.  The **ext/misc** subdirectory contains a number of
277 smaller, single-file extensions, such as a REGEXP operator.
279 The **tool/** subdirectory contains various scripts and programs used
280 for building generated source code files or for testing or for generating
281 accessory programs such as "sqlite3_analyzer(.exe)".
283 ### Generated Source Code Files
285 Several of the C-language source files used by SQLite are generated from
286 other sources rather than being typed in manually by a programmer.  This
287 section will summarize those automatically-generated files.  To create all
288 of the automatically-generated files, simply run "make target&#95;source".
289 The "target&#95;source" make target will create a subdirectory "tsrc/" and
290 fill it with all the source files needed to build SQLite, both
291 manually-edited files and automatically-generated files.
293 The SQLite interface is defined by the **sqlite3.h** header file, which is
294 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION.  The
295 [Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
296 The manifest.uuid file contains the SHA3 hash of the particular check-in
297 and is used to generate the SQLITE\_SOURCE\_ID macro.  The VERSION file
298 contains the current SQLite version number.  The sqlite3.h header is really
299 just a copy of src/sqlite.h.in with the source-id and version number inserted
300 at just the right spots. Note that comment text in the sqlite3.h file is
301 used to generate much of the SQLite API documentation.  The Tcl scripts
302 used to generate that documentation are in a separate source repository.
304 The SQL language parser is **parse.c** which is generate from a grammar in
305 the src/parse.y file.  The conversion of "parse.y" into "parse.c" is done
306 by the [lemon](./doc/lemon.html) LALR(1) parser generator.  The source code
307 for lemon is at tool/lemon.c.  Lemon uses the tool/lempar.c file as a
308 template for generating its parser.
309 Lemon also generates the **parse.h** header file, at the same time it
310 generates parse.c.
312 The **opcodes.h** header file contains macros that define the numbers
313 corresponding to opcodes in the "VDBE" virtual machine.  The opcodes.h
314 file is generated by the scanning the src/vdbe.c source file.  The
315 Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
316 A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
317 the **opcodes.c** source file, which contains a reverse mapping from
318 opcode-number to opcode-name that is used for EXPLAIN output.
320 The **keywordhash.h** header file contains the definition of a hash table
321 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
322 the numeric codes used by the parse.c parser.  The keywordhash.h file is
323 generated by a C-language program at tool mkkeywordhash.c.
325 The **pragma.h** header file contains various definitions used to parse
326 and implement the PRAGMA statements.  The header is generated by a
327 script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
328 the **tool/mkpragmatab.tcl** file to insert the information needed by the
329 parser for your new PRAGMA, then run the script to regenerate the
330 **pragma.h** header file.
332 ### The Amalgamation
334 All of the individual C source code and header files (both manually-edited
335 and automatically-generated) can be combined into a single big source file
336 **sqlite3.c** called "the amalgamation".  The amalgamation is the recommended
337 way of using SQLite in a larger application.  Combining all individual
338 source code files into a single big source code file allows the C compiler
339 to perform more cross-procedure analysis and generate better code.  SQLite
340 runs about 5% faster when compiled from the amalgamation versus when compiled
341 from individual source files.
343 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
344 First, all of the individual source files must be gathered into the tsrc/
345 subdirectory (using the equivalent of "make target_source") then the
346 tool/mksqlite3c.tcl script is run to copy them all together in just the
347 right order while resolving internal "#include" references.
349 The amalgamation source file is more than 200K lines long.  Some symbolic
350 debuggers (most notably MSVC) are unable to deal with files longer than 64K
351 lines.  To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
352 can be run on the amalgamation to break it up into a single small C file
353 called **sqlite3-all.c** that does #include on about seven other files
354 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**.  In this way,
355 all of the source code is contained within a single translation unit so
356 that the compiler can do extra cross-procedure optimization, but no
357 individual source file exceeds 32K lines in length.
359 ## How It All Fits Together
361 SQLite is modular in design.
362 See the [architectural description](http://www.sqlite.org/arch.html)
363 for details. Other documents that are useful in
364 (helping to understand how SQLite works include the
365 [file format](http://www.sqlite.org/fileformat2.html) description,
366 the [virtual machine](http://www.sqlite.org/opcode.html) that runs
367 prepared statements, the description of
368 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
369 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
371 Years of effort have gone into optimizating SQLite, both
372 for small size and high performance.  And optimizations tend to result in
373 complex code.  So there is a lot of complexity in the current SQLite
374 implementation.  It will not be the easiest library in the world to hack.
376 Key files:
378   *  **sqlite.h.in** - This file defines the public interface to the SQLite
379      library.  Readers will need to be familiar with this interface before
380      trying to understand how the library works internally.
382   *  **sqliteInt.h** - this header file defines many of the data objects
383      used internally by SQLite.  In addition to "sqliteInt.h", some
384      subsystems have their own header files.
386   *  **parse.y** - This file describes the LALR(1) grammar that SQLite uses
387      to parse SQL statements, and the actions that are taken at each step
388      in the parsing process.
390   *  **vdbe.c** - This file implements the virtual machine that runs
391      prepared statements.  There are various helper files whose names
392      begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
393      which defines internal data objects.  The rest of SQLite interacts
394      with the VDBE through an interface defined by vdbe.h.
396   *  **where.c** - This file (together with its helper files named
397      by "where*.c") analyzes the WHERE clause and generates
398      virtual machine code to run queries efficiently.  This file is
399      sometimes called the "query optimizer".  It has its own private
400      header file, whereInt.h, that defines data objects used internally.
402   *  **btree.c** - This file contains the implementation of the B-Tree
403      storage engine used by SQLite.  The interface to the rest of the system
404      is defined by "btree.h".  The "btreeInt.h" header defines objects
405      used internally by btree.c and not published to the rest of the system.
407   *  **pager.c** - This file contains the "pager" implementation, the
408      module that implements transactions.  The "pager.h" header file
409      defines the interface between pager.c and the rest of the system.
411   *  **os_unix.c** and **os_win.c** - These two files implement the interface
412      between SQLite and the underlying operating system using the run-time
413      pluggable VFS interface.
415   *  **shell.c.in** - This file is not part of the core SQLite library.  This
416      is the file that, when linked against sqlite3.a, generates the
417      "sqlite3.exe" command-line shell.  The "shell.c.in" file is transformed
418      into "shell.c" as part of the build process.
420   *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
421      is not part of the core SQLite library.  But as most of the tests in this
422      repository are written in Tcl, the Tcl language bindings are important.
424   *  **test*.c** - Files in the src/ folder that begin with "test" go into
425      building the "testfixture.exe" program.  The testfixture.exe program is
426      an enhanced Tcl shell.  The testfixture.exe program runs scripts in the
427      test/ folder to validate the core SQLite code.  The testfixture program
428      (and some other test programs too) is build and run when you type
429      "make test".
431   *  **ext/misc/json1.c** - This file implements the various JSON functions
432      that are build into SQLite.
434 There are many other source files.  Each has a succinct header comment that
435 describes its purpose and role within the larger system.
437 <a name="vauth"></a>
438 ## Verifying Code Authenticity
440 If you obtained an SQLite source tree from a secondary source, such as a
441 GitHub mirror, and you want to verify that it has not been altered, there
442 are a couple of ways to do that.
444 If you have a release version of SQLite, and you are using the
445 `sqlite3.c` amalgamation, then SHA3-256 hashes for the amalgamation are
446 available in the [change log](https://www.sqlite.org/changes.html) on
447 the official website.  After building the `sqlite3.c` file, you can check
448 that it is authentic by comparing the hash.  This does not ensure that the
449 test scripts are unaltered, but it does validate the deliverable part of
450 the code and the verification process only involves computing and
451 comparing a single hash.
453 For versions other than an official release, or if you are building the
454 `sqlite3.c` amalgamation using non-standard build options, the verification
455 process is a little more involved.  The `manifest` file at the root directory
456 of the source tree
457 contains either a SHA3-256 hash (for newer files) or a SHA1 hash (for 
458 older files) for every source file in the repository.  You can write a script
459 to extracts hashes from `manifest` and verifies the hashes against the 
460 corresponding files in the source tree.  The SHA3-256 hash of the `manifest`
461 file itself is the official name of the version of the source tree that you
462 have.  The `manifest.uuid` file should contain the SHA3-256 hash of the
463 `manifest` file.  If all of the above hash comparisons are correct, then
464 you can be confident that your source tree is authentic and unadulterated.
466 The format of the `manifest` file should be mostly self-explanatory, but
467 if you want details, they are available
468 [here](https://fossil-scm.org/fossil/doc/trunk/www/fileformat.wiki#manifest).
470 ## Contacts
472 The main SQLite website is [http://www.sqlite.org/](http://www.sqlite.org/)
473 with geographically distributed backups at
474 [http://www2.sqlite.org/](http://www2.sqlite.org) and
475 [http://www3.sqlite.org/](http://www3.sqlite.org).