update podspec version
[sqlcipher.git] / README.md
blob7593fabf359f27068e915323628dfcf133fe9c93
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 like:
5 - on-the-fly encryption
6 - tamper detection
7 - memory sanitization
8 - strong key derivation
10 SQLCipher is based on SQLite and stable upstream release features are periodically integrated. 
12 SQLCipher is maintained by Zetetic, LLC, and additional information and documentation is available on the official [SQLCipher site](https://www.zetetic.net/sqlcipher/).
14 ## Features
16 - Fast performance with as little as 5-15% overhead for encryption on many operations
17 - 100% of data in the database file is encrypted
18 - Good security practices (CBC mode, HMAC, key derivation)
19 - Zero-configuration and application level cryptography
20 - Algorithms provided by the peer reviewed OpenSSL crypto library.
21 - Configurable crypto providers
23 ## Compatibility
25 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). 
27 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).
29 ## Contributions
31 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/). We strongly encourage [discussion](https://discuss.zetetic.net/c/sqlcipher) of the proposed change prior to development and submission.
33 ## Compiling
35 Building SQLCipher is similar to compiling a regular version of SQLite from source a couple small exceptions:  
37  1. You *must* define `SQLITE_HAS_CODEC` and either `SQLITE_TEMP_STORE=2` or SQLITE_TEMP_STORE=3` 
38  2. You will need to link against a support cryptograpic provider (OpenSSL, LibTomCrypt, CommonCrypto/Security.framework, or NSS)
40 The following examples demonstrate linking against OpenSSL, which is a readily available provider on most Unix-like systems. 
42 Example 1. Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this 
43 example, `--enable-tempstore=yes` is setting `SQLITE_TEMP_STORE=2` for the build.
45 ```
46         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
47                 LDFLAGS="/opt/local/lib/libcrypto.a"
48         $ make
49 ```
51 Example 2. Dynamic linking
53 ```
54         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
55                 LDFLAGS="-lcrypto"
56         $ make
57 ```
59 ## Testing
61 The full SQLite test suite will not complete successfully when using SQLCipher. In some cases encryption interferes with low-level tests that require access to database file data or features which are unsupported by SQLCipher. Those tests that are intended to support encryption are intended for non-SQLCipher implementations. In addition, because SQLite tests are not always isolated, if one test fails it can trigger a domino effect with other failures in later steps.
63 As a result, the SQLCipher package includes it's own independent tests that exercise and verify the core functionality of the SQLCipher extensions. This test suite is intended to provide an abbreviated verification of SQLCipher's internal logic; it does not perform an exhaustive test of the SQLite database system as a whole or verify functionality on specific platforms. Because SQLCipher is based on stable upstream builds of SQLite, it is consider a basic assumption that the core SQLite library code is operating properly (the SQLite core is almost untouched in SQLCipher). Thus, the additional SQLCipher-specific test provide the requisite verification that the library is operating as expected with SQLCipher's security features enabled.
65 To run SQLCipher specific tests, configure as described above and run the following to execute the tests and recieve a report of the results:
67 ```
68   $ make testfixture
69   $ ./testfixture test/sqlcipher.test
70 ```
72 ## Encrypting a database
74 To specify an encryption passphrase for the database via the SQL interface you 
75 use a PRAGMA. The passphrase you enter is passed through PBKDF2 key derivation to
76 obtain the encryption key for the database 
78         PRAGMA key = 'passphrase';
80 Alternately, you can specify an exact byte sequence using a blob literal. If you
81 use this method it is your responsibility to ensure that the data you provide is a
82 64 character hex string, which will be converted directly to 32 bytes (256 bits) of 
83 key data without key derivation.
85         PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
87 To encrypt a database programmatically you can use the `sqlite3_key` function. 
88 The data provided in `pKey` is converted to an encryption key according to the 
89 same rules as `PRAGMA key`. 
91         int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
93 `PRAGMA key` or `sqlite3_key` should be called as the first operation when a database is open.
95 ## Changing a database key
97 To change the encryption passphrase for an existing database you may use the rekey PRAGMA
98 after you've supplied the correct database password;
100         PRAGMA key = 'passphrase'; -- start with the existing database passphrase
101         PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase
103 The hex rekey pragma may be used to rekey to a specific binary value
105         PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
107 This can be accomplished programmatically by using sqlite3_rekey;
108   
109         sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey)
111 ## Support
113 The primary source for complete documentation (desing, API, platforms, usage) is the SQLCipher website:
115 https://www.zetetic.net/sqlcipher/documentation
117 The primary avenue for support and discussions is the SQLCipher discuss site:
119 https://discuss.zetetic.net/c/sqlcipher
121 Issues or support questions on using SQLCipher should be entered into the 
122 GitHub Issue tracker:
124 https://github.com/sqlcipher/sqlcipher/issues
126 Please DO NOT post issues, support questions, or other problems to blog 
127 posts about SQLCipher as we do not monitor them frequently.
129 If you are using SQLCipher in your own software please let us know at 
130 support@zetetic.net!
132 ## Community Edition Open Source License
134 Copyright (c) 2020, ZETETIC LLC
135 All rights reserved.
137 Redistribution and use in source and binary forms, with or without
138 modification, are permitted provided that the following conditions are met:
139     * Redistributions of source code must retain the above copyright
140       notice, this list of conditions and the following disclaimer.
141     * Redistributions in binary form must reproduce the above copyright
142       notice, this list of conditions and the following disclaimer in the
143       documentation and/or other materials provided with the distribution.
144     * Neither the name of the ZETETIC LLC nor the
145       names of its contributors may be used to endorse or promote products
146       derived from this software without specific prior written permission.
148 THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
149 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
150 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
151 DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
152 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
153 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
154 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
155 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
156 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
157 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
159 # Begin SQLite README.md
161 <h1 align="center">SQLite Source Repository</h1>
163 This repository contains the complete source code for the 
164 [SQLite database engine](https://sqlite.org/).  Some test scripts 
165 are also included.  However, many other test scripts
166 and most of the documentation are managed separately.
168 ## Version Control
170 SQLite sources are managed using the
171 [Fossil](https://www.fossil-scm.org/), a distributed version control system
172 that was specifically designed and written to support SQLite development.
173 The [Fossil repository](https://sqlite.org/src/timeline) contains the urtext.
175 If you are reading this on GitHub or some other Git repository or service,
176 then you are looking at a mirror.  The names of check-ins and
177 other artifacts in a Git mirror are different from the official
178 names for those objects.  The offical names for check-ins are
179 found in a footer on the check-in comment for authorized mirrors.
180 The official check-in name can also be seen in the `manifest.uuid` file
181 in the root of the tree.  Always use the official name, not  the
182 Git-name, when communicating about an SQLite check-in.
184 If you pulled your SQLite source code from a secondary source and want to
185 verify its integrity, there are hints on how to do that in the
186 [Verifying Code Authenticity](#vauth) section below.
188 ## Obtaining The Code
190 If you do not want to use Fossil, you can download tarballs or ZIP
191 archives or [SQLite archives](https://sqlite.org/cli.html#sqlar) as follows:
193   *  Lastest trunk check-in as
194      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz),
195      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip), or
196      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar).
198   *  Latest release as
199      [Tarball](https://www.sqlite.org/src/tarball/sqlite.tar.gz?r=release),
200      [ZIP-archive](https://www.sqlite.org/src/zip/sqlite.zip?r=release), or
201      [SQLite-archive](https://www.sqlite.org/src/sqlar/sqlite.sqlar?r=release).
203   *  For other check-ins, substitute an appropriate branch name or
204      tag or hash prefix in place of "release" in the URLs of the previous
205      bullet.  Or browse the [timeline](https://www.sqlite.org/src/timeline)
206      to locate the check-in desired, click on its information page link,
207      then click on the "Tarball" or "ZIP Archive" links on the information
208      page.
210 If you do want to use Fossil to check out the source tree, 
211 first install Fossil version 2.0 or later.
212 (Source tarballs and precompiled binaries available
213 [here](https://www.fossil-scm.org/fossil/uv/download.html).  Fossil is
214 a stand-alone program.  To install, simply download or build the single 
215 executable file and put that file someplace on your $PATH.)
216 Then run commands like this:
218         mkdir ~/sqlite
219         cd ~/sqlite
220         fossil clone https://www.sqlite.org/src sqlite.fossil
221         fossil open sqlite.fossil
222     
223 After setting up a repository using the steps above, you can always
224 update to the lastest version using:
226         fossil update trunk   ;# latest trunk check-in
227         fossil update release ;# latest official release
229 Or type "fossil ui" to get a web-based user interface.
231 ## Compiling
233 First create a directory in which to place
234 the build products.  It is recommended, but not required, that the
235 build directory be separate from the source directory.  Cd into the
236 build directory and then from the build directory run the configure
237 script found at the root of the source tree.  Then run "make".
239 For example:
241         tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
242         mkdir bld                ;#  Build will occur in a sibling directory
243         cd bld                   ;#  Change to the build directory
244         ../sqlite/configure      ;#  Run the configure script
245         make                     ;#  Run the makefile.
246         make sqlite3.c           ;#  Build the "amalgamation" source file
247         make test                ;#  Run some tests (requires Tcl)
249 See the makefile for additional targets.
251 The configure script uses autoconf 2.61 and libtool.  If the configure
252 script does not work out for you, there is a generic makefile named
253 "Makefile.linux-gcc" in the top directory of the source tree that you
254 can copy and edit to suit your needs.  Comments on the generic makefile
255 show what changes are needed.
257 ## Using MSVC
259 On Windows, all applicable build products can be compiled with MSVC.
260 First open the command prompt window associated with the desired compiler
261 version (e.g. "Developer Command Prompt for VS2013").  Next, use NMAKE
262 with the provided "Makefile.msc" to build one of the supported targets.
264 For example:
266         mkdir bld
267         cd bld
268         nmake /f Makefile.msc TOP=..\sqlite
269         nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
270         nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
271         nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
272         nmake /f Makefile.msc test TOP=..\sqlite
274 There are several build options that can be set via the NMAKE command
275 line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
276 to the "sqlite3.dll" command line above.  When debugging into the SQLite
277 code, adding the "DEBUG=1" argument to one of the above command lines is
278 recommended.
280 SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
281 is required by the makefiles (including those for MSVC).  SQLite contains
282 a lot of generated code and Tcl is used to do much of that code generation.
284 ## Source Code Tour
286 Most of the core source files are in the **src/** subdirectory.  The
287 **src/** folder also contains files used to build the "testfixture" test
288 harness. The names of the source files used by "testfixture" all begin
289 with "test".
290 The **src/** also contains the "shell.c" file
291 which is the main program for the "sqlite3.exe"
292 [command-line shell](https://sqlite.org/cli.html) and
293 the "tclsqlite.c" file which implements the
294 [Tcl bindings](https://sqlite.org/tclsqlite.html) for SQLite.
295 (Historical note:  SQLite began as a Tcl
296 extension and only later escaped to the wild as an independent library.)
298 Test scripts and programs are found in the **test/** subdirectory.
299 Addtional test code is found in other source repositories.
300 See [How SQLite Is Tested](http://www.sqlite.org/testing.html) for
301 additional information.
303 The **ext/** subdirectory contains code for extensions.  The
304 Full-text search engine is in **ext/fts3**.  The R-Tree engine is in
305 **ext/rtree**.  The **ext/misc** subdirectory contains a number of
306 smaller, single-file extensions, such as a REGEXP operator.
308 The **tool/** subdirectory contains various scripts and programs used
309 for building generated source code files or for testing or for generating
310 accessory programs such as "sqlite3_analyzer(.exe)".
312 ### Generated Source Code Files
314 Several of the C-language source files used by SQLite are generated from
315 other sources rather than being typed in manually by a programmer.  This
316 section will summarize those automatically-generated files.  To create all
317 of the automatically-generated files, simply run "make target&#95;source".
318 The "target&#95;source" make target will create a subdirectory "tsrc/" and
319 fill it with all the source files needed to build SQLite, both
320 manually-edited files and automatically-generated files.
322 The SQLite interface is defined by the **sqlite3.h** header file, which is
323 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION.  The
324 [Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
325 The manifest.uuid file contains the SHA3 hash of the particular check-in
326 and is used to generate the SQLITE\_SOURCE\_ID macro.  The VERSION file
327 contains the current SQLite version number.  The sqlite3.h header is really
328 just a copy of src/sqlite.h.in with the source-id and version number inserted
329 at just the right spots. Note that comment text in the sqlite3.h file is
330 used to generate much of the SQLite API documentation.  The Tcl scripts
331 used to generate that documentation are in a separate source repository.
333 The SQL language parser is **parse.c** which is generate from a grammar in
334 the src/parse.y file.  The conversion of "parse.y" into "parse.c" is done
335 by the [lemon](./doc/lemon.html) LALR(1) parser generator.  The source code
336 for lemon is at tool/lemon.c.  Lemon uses the tool/lempar.c file as a
337 template for generating its parser.
338 Lemon also generates the **parse.h** header file, at the same time it
339 generates parse.c.
341 The **opcodes.h** header file contains macros that define the numbers
342 corresponding to opcodes in the "VDBE" virtual machine.  The opcodes.h
343 file is generated by the scanning the src/vdbe.c source file.  The
344 Tcl script at ./mkopcodeh.tcl does this scan and generates opcodes.h.
345 A second Tcl script, ./mkopcodec.tcl, then scans opcodes.h to generate
346 the **opcodes.c** source file, which contains a reverse mapping from
347 opcode-number to opcode-name that is used for EXPLAIN output.
349 The **keywordhash.h** header file contains the definition of a hash table
350 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
351 the numeric codes used by the parse.c parser.  The keywordhash.h file is
352 generated by a C-language program at tool mkkeywordhash.c.
354 The **pragma.h** header file contains various definitions used to parse
355 and implement the PRAGMA statements.  The header is generated by a
356 script **tool/mkpragmatab.tcl**. If you want to add a new PRAGMA, edit
357 the **tool/mkpragmatab.tcl** file to insert the information needed by the
358 parser for your new PRAGMA, then run the script to regenerate the
359 **pragma.h** header file.
361 ### The Amalgamation
363 All of the individual C source code and header files (both manually-edited
364 and automatically-generated) can be combined into a single big source file
365 **sqlite3.c** called "the amalgamation".  The amalgamation is the recommended
366 way of using SQLite in a larger application.  Combining all individual
367 source code files into a single big source code file allows the C compiler
368 to perform more cross-procedure analysis and generate better code.  SQLite
369 runs about 5% faster when compiled from the amalgamation versus when compiled
370 from individual source files.
372 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
373 First, all of the individual source files must be gathered into the tsrc/
374 subdirectory (using the equivalent of "make target_source") then the
375 tool/mksqlite3c.tcl script is run to copy them all together in just the
376 right order while resolving internal "#include" references.
378 The amalgamation source file is more than 200K lines long.  Some symbolic
379 debuggers (most notably MSVC) are unable to deal with files longer than 64K
380 lines.  To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
381 can be run on the amalgamation to break it up into a single small C file
382 called **sqlite3-all.c** that does #include on about seven other files
383 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-7.c**.  In this way,
384 all of the source code is contained within a single translation unit so
385 that the compiler can do extra cross-procedure optimization, but no
386 individual source file exceeds 32K lines in length.
388 ## How It All Fits Together
390 SQLite is modular in design.
391 See the [architectural description](http://www.sqlite.org/arch.html)
392 for details. Other documents that are useful in
393 (helping to understand how SQLite works include the
394 [file format](http://www.sqlite.org/fileformat2.html) description,
395 the [virtual machine](http://www.sqlite.org/opcode.html) that runs
396 prepared statements, the description of
397 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
398 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
400 Years of effort have gone into optimizating SQLite, both
401 for small size and high performance.  And optimizations tend to result in
402 complex code.  So there is a lot of complexity in the current SQLite
403 implementation.  It will not be the easiest library in the world to hack.
405 Key files:
407   *  **sqlite.h.in** - This file defines the public interface to the SQLite
408      library.  Readers will need to be familiar with this interface before
409      trying to understand how the library works internally.
411   *  **sqliteInt.h** - this header file defines many of the data objects
412      used internally by SQLite.  In addition to "sqliteInt.h", some
413      subsystems have their own header files.
415   *  **parse.y** - This file describes the LALR(1) grammar that SQLite uses
416      to parse SQL statements, and the actions that are taken at each step
417      in the parsing process.
419   *  **vdbe.c** - This file implements the virtual machine that runs
420      prepared statements.  There are various helper files whose names
421      begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
422      which defines internal data objects.  The rest of SQLite interacts
423      with the VDBE through an interface defined by vdbe.h.
425   *  **where.c** - This file (together with its helper files named
426      by "where*.c") analyzes the WHERE clause and generates
427      virtual machine code to run queries efficiently.  This file is
428      sometimes called the "query optimizer".  It has its own private
429      header file, whereInt.h, that defines data objects used internally.
431   *  **btree.c** - This file contains the implementation of the B-Tree
432      storage engine used by SQLite.  The interface to the rest of the system
433      is defined by "btree.h".  The "btreeInt.h" header defines objects
434      used internally by btree.c and not published to the rest of the system.
436   *  **pager.c** - This file contains the "pager" implementation, the
437      module that implements transactions.  The "pager.h" header file
438      defines the interface between pager.c and the rest of the system.
440   *  **os_unix.c** and **os_win.c** - These two files implement the interface
441      between SQLite and the underlying operating system using the run-time
442      pluggable VFS interface.
444   *  **shell.c.in** - This file is not part of the core SQLite library.  This
445      is the file that, when linked against sqlite3.a, generates the
446      "sqlite3.exe" command-line shell.  The "shell.c.in" file is transformed
447      into "shell.c" as part of the build process.
449   *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
450      is not part of the core SQLite library.  But as most of the tests in this
451      repository are written in Tcl, the Tcl language bindings are important.
453   *  **test*.c** - Files in the src/ folder that begin with "test" go into
454      building the "testfixture.exe" program.  The testfixture.exe program is
455      an enhanced Tcl shell.  The testfixture.exe program runs scripts in the
456      test/ folder to validate the core SQLite code.  The testfixture program
457      (and some other test programs too) is build and run when you type
458      "make test".
460   *  **ext/misc/json1.c** - This file implements the various JSON functions
461      that are build into SQLite.
463 There are many other source files.  Each has a succinct header comment that
464 describes its purpose and role within the larger system.
466 <a name="vauth"></a>
467 ## Verifying Code Authenticity
469 The `manifest` file at the root directory of the source tree
470 contains either a SHA3-256 hash (for newer files) or a SHA1 hash (for 
471 older files) for every source file in the repository.
472 The SHA3-256 hash of the `manifest`
473 file itself is the official name of the version of the source tree that you
474 have. The `manifest.uuid` file should contain the SHA3-256 hash of the
475 `manifest` file. If all of the above hash comparisons are correct, then
476 you can be confident that your source tree is authentic and unadulterated.
478 The format of the `manifest` file should be mostly self-explanatory, but
479 if you want details, they are available
480 [here](https://fossil-scm.org/fossil/doc/trunk/www/fileformat.wiki#manifest).
482 ## Contacts
484 The main SQLite website is [http://www.sqlite.org/](http://www.sqlite.org/)
485 with geographically distributed backups at
486 [http://www2.sqlite.org/](http://www2.sqlite.org) and
487 [http://www3.sqlite.org/](http://www3.sqlite.org).