Update from autoreconf
[sqlcipher.git] / README.md
bloba3abad40f486a736369668732d622b6285f89421
1 ## SQLCipher
3 SQLCipher is an SQLite extension that provides transparent 256-bit AES encryption of 
4 database files. Pages are encrypted before being written to disk and are decrypted 
5 when read back. Due to the small footprint and great performance it’s ideal for 
6 protecting embedded application databases and is well suited for mobile development.
8 The official SQLCipher software site is http://sqlcipher.net
10 SQLCipher was initially developed by Stephen Lombardo at Zetetic LLC 
11 (sjlombardo@zetetic.net) as the encrypted database layer for Strip, 
12 an iPhone data vault and password manager (http://getstrip.com).   
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, key derivation)
19 - Zero-configuration and application level cryptography
20 - Algorithms provided by the peer reviewed OpenSSL crypto library.
21 - Configurable crypto providers
23 ## Contributions
25 We 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.
27 If you are reading this on a Git mirror someplace, you are doing it wrong.
28 The [official repository](https://www.sqlite.org/src/) is better.  Go there
29 now.
31 ## Compiling
33 Building SQLCipher is almost the same as compiling a regular version of 
34 SQLite with two small exceptions: 
36  1. You *must* define SQLITE_HAS_CODEC and SQLITE_TEMP_STORE=2 when building sqlcipher. 
37  2. You need to link against a OpenSSL's libcrypto 
39 Example Static linking (replace /opt/local/lib with the path to libcrypto.a). Note in this 
40 example, --enable-tempstore=yes is setting SQLITE_TEMP_STORE=2 for the build.
42         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
43                 LDFLAGS="/opt/local/lib/libcrypto.a"
44         $ make
46 Example Dynamic linking
48         $ ./configure --enable-tempstore=yes CFLAGS="-DSQLITE_HAS_CODEC" \
49                 LDFLAGS="-lcrypto"
50         $ make
52 ## Encrypting a database
54 To specify an encryption passphrase for the database via the SQL interface you 
55 use a pragma. The passphrase you enter is passed through PBKDF2 key derivation to
56 obtain the encryption key for the database 
58         PRAGMA key = 'passphrase';
60 Alternately, you can specify an exact byte sequence using a blob literal. If you
61 use this method it is your responsibility to ensure that the data you provide a
62 64 character hex string, which will be converted directly to 32 bytes (256 bits) of 
63 key data without key derivation.
65         PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
67 To encrypt a database programatically you can use the sqlite3_key function. 
68 The data provided in pKey is converted to an encryption key according to the 
69 same rules as PRAGMA key. 
71         int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
73 PRAGMA key or sqlite3_key should be called as the first operation when a database is open.
75 ## Changing a database key
77 To change the encryption passphrase for an existing database you may use the rekey pragma
78 after you've supplied the correct database password;
80         PRAGMA key = 'passphrase'; -- start with the existing database passphrase
81         PRAGMA rekey = 'new-passphrase'; -- rekey will reencrypt with the new passphrase
83 The hexrekey pragma may be used to rekey to a specific binary value
85         PRAGMA rekey = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
87 This can be accomplished programtically by using sqlite3_rekey;
88   
89         sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey)
91 ## Support
93 The primary avenue for support and discussions is the SQLCipher users mailing list:
95 http://groups.google.com/group/sqlcipher
97 Issues or support questions on using SQLCipher should be entered into the 
98 GitHub Issue tracker:
100 https://github.com/sqlcipher/sqlcipher/issues
102 Please DO NOT post issues, support questions, or other problems to blog 
103 posts about SQLCipher as we do not monitor them frequently.
105 If you are using SQLCipher in your own software please let us know at 
106 support@zetetic.net!
108 ## License
110 Copyright (c) 2008, ZETETIC LLC
111 All rights reserved.
113 Redistribution and use in source and binary forms, with or without
114 modification, are permitted provided that the following conditions are met:
115     * Redistributions of source code must retain the above copyright
116       notice, this list of conditions and the following disclaimer.
117     * Redistributions in binary form must reproduce the above copyright
118       notice, this list of conditions and the following disclaimer in the
119       documentation and/or other materials provided with the distribution.
120     * Neither the name of the ZETETIC LLC nor the
121       names of its contributors may be used to endorse or promote products
122       derived from this software without specific prior written permission.
124 THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
125 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
126 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
127 DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
128 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
129 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
130 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
131 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
132 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
133 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 ## End SQLCipher
137 This directory contains source code to 
139   SQLite: An Embeddable SQL Database Engine
141 To compile the project, first create a directory in which to place
142 the build products.  It is recommended, but not required, that the
143 build directory be separate from the source directory.  Cd into the
144 build directory and then from the build directory run the configure
145 script found at the root of the source tree.  Then run "make".
147 For example:
149         tar xzf sqlite.tar.gz    ;#  Unpack the source tree into "sqlite"
150     mkdir bld                ;#  Build will occur in a sibling directory
151     cd bld                   ;#  Change to the build directory
152     ../sqlite/configure      ;#  Run the configure script
153     make                     ;#  Run the makefile.
154     make install             ;#  (Optional) Install the build products
156 The configure script uses autoconf 2.61 and libtool.  If the configure
157 script does not work out for you, there is a generic makefile named
158 "Makefile.linux-gcc" in the top directory of the source tree that you
159 can copy and edit to suit your needs.  Comments on the generic makefile
160 show what changes are needed.
162 The linux binaries on the website are created using the generic makefile,
163 not the configure script.  The windows binaries on the website are created
164 using MinGW32 configured as a cross-compiler running under Linux.  For 
165 details, see the ./publish.sh script at the top-level of the source tree.
166 The developers do not use teh configure script.
168 SQLite does not require TCL to run, but a TCL installation is required
169 by the makefiles.  SQLite contains a lot of generated code and TCL is
170 used to do much of that code generation.  The makefile also requires
171 AWK.
173 Contacts:
175   http://www.sqlite.org/
176 =======
177     mkdir bld
178     cd bld
179     nmake /f Makefile.msc TOP=..\sqlite
180     nmake /f Makefile.msc sqlite3.c TOP=..\sqlite
181     nmake /f Makefile.msc sqlite3.dll TOP=..\sqlite
182     nmake /f Makefile.msc sqlite3.exe TOP=..\sqlite
183     nmake /f Makefile.msc test TOP=..\sqlite
185 There are several build options that can be set via the NMAKE command
186 line.  For example, to build for WinRT, simply add "FOR_WINRT=1" argument
187 to the "sqlite3.dll" command line above.  When debugging into the SQLite
188 code, adding the "DEBUG=1" argument to one of the above command lines is
189 recommended.
191 SQLite does not require [Tcl](http://www.tcl.tk/) to run, but a Tcl installation
192 is required by the makefiles (including those for MSVC).  SQLite contains
193 a lot of generated code and Tcl is used to do much of that code generation.
194 The makefiles also require AWK.
196 ## Source Code Tour
198 Most of the core source files are in the **src/** subdirectory.  But
199 src/ also contains files used to build the "testfixture" test harness;
200 those file all begin with "test".  And src/ contains the "shell.c" file
201 which is the main program for the "sqlite3.exe" command-line shell and
202 the "tclsqlite.c" file which implements the bindings to SQLite from the
203 Tcl programming language.  (Historical note:  SQLite began as a Tcl
204 extension and only later escaped to the wild as an independent library.)
206 Test scripts and programs are found in the **test/** subdirectory.
207 There are other test suites for SQLite (see
208 [How SQLite Is Tested](http://www.sqlite.org/testing.html))
209 but those other test suites are
210 in separate source repositories.
212 The **ext/** subdirectory contains code for extensions.  The
213 Full-text search engine is in **ext/fts3**.  The R-Tree engine is in
214 **ext/rtree**.  The **ext/misc** subdirectory contains a number of
215 smaller, single-file extensions, such as a REGEXP operator.
217 The **tool/** subdirectory contains various scripts and programs used
218 for building generated source code files or for testing or for generating
219 accessory programs such as "sqlite3_analyzer(.exe)".
221 ### Generated Source Code Files
223 Several of the C-language source files used by SQLite are generated from
224 other sources rather than being typed in manually by a programmer.  This
225 section will summarize those automatically-generated files.  To create all
226 of the automatically-generated files, simply run "make target_source".
227 The "target_source" make target will create a subdirectory "tsrc/" and
228 fill it with all the source files needed to build SQLite, both
229 manually-edited files and automatically-generated files.
231 The SQLite interface is defined by the **sqlite3.h** header file, which is
232 generated from src/sqlite.h.in, ./manifest.uuid, and ./VERSION.  The
233 [Tcl script](http://www.tcl.tk) at tool/mksqlite3h.tcl does the conversion.
234 The manifest.uuid file contains the SHA1 hash of the particular check-in
235 and is used to generate the SQLITE\_SOURCE\_ID macro.  The VERSION file
236 contains the current SQLite version number.  The sqlite3.h header is really
237 just a copy of src/sqlite.h.in with the source-id and version number inserted
238 at just the right spots. Note that comment text in the sqlite3.h file is
239 used to generate much of the SQLite API documentation.  The Tcl scripts
240 used to generate that documentation are in a separate source repository.
242 The SQL language parser is **parse.c** which is generate from a grammar in
243 the src/parse.y file.  The conversion of "parse.y" into "parse.c" is done
244 by the [lemon](./doc/lemon.html) LALR(1) parser generator.  The source code
245 for lemon is at tool/lemon.c.  Lemon uses a
246 template for generating its parser.  A generic template is in tool/lempar.c,
247 but SQLite uses a slightly modified template found in src/lempar.c.
249 Lemon also generates the **parse.h** header file, at the same time it
250 generates parse.c. But the parse.h header file is
251 modified further (to add additional symbols) using the ./addopcodes.awk
252 AWK script.
254 The **opcodes.h** header file contains macros that define the numbers
255 corresponding to opcodes in the "VDBE" virtual machine.  The opcodes.h
256 file is generated by the scanning the src/vdbe.c source file.  The
257 AWK script at ./mkopcodeh.awk does this scan and generates opcodes.h.
258 A second AWK script, ./mkopcodec.awk, then scans opcodes.h to generate
259 the **opcodes.c** source file, which contains a reverse mapping from
260 opcode-number to opcode-name that is used for EXPLAIN output.
262 The **keywordhash.h** header file contains the definition of a hash table
263 that maps SQL language keywords (ex: "CREATE", "SELECT", "INDEX", etc.) into
264 the numeric codes used by the parse.c parser.  The keywordhash.h file is
265 generated by a C-language program at tool mkkeywordhash.c.
267 ### The Amalgamation
269 All of the individual C source code and header files (both manually-edited
270 and automatically-generated) can be combined into a single big source file
271 **sqlite3.c** called "the amalgamation".  The amalgamation is the recommended
272 way of using SQLite in a larger application.  Combining all individual
273 source code files into a single big source code file allows the C compiler
274 to perform more cross-procedure analysis and generate better code.  SQLite
275 runs about 5% faster when compiled from the amalgamation versus when compiled
276 from individual source files.
278 The amalgamation is generated from the tool/mksqlite3c.tcl Tcl script.
279 First, all of the individual source files must be gathered into the tsrc/
280 subdirectory (using the equivalent of "make target_source") then the
281 tool/mksqlite3c.tcl script is run to copy them all together in just the
282 right order while resolving internal "#include" references.
284 The amalgamation source file is more than 100K lines long.  Some symbolic
285 debuggers (most notably MSVC) are unable to deal with files longer than 64K
286 lines.  To work around this, a separate Tcl script, tool/split-sqlite3c.tcl,
287 can be run on the amalgamation to break it up into a single small C file
288 called **sqlite3-all.c** that does #include on about five other files
289 named **sqlite3-1.c**, **sqlite3-2.c**, ..., **sqlite3-5.c**.  In this way,
290 all of the source code is contained within a single translation unit so
291 that the compiler can do extra cross-procedure optimization, but no
292 individual source file exceeds 32K lines in length.
294 ## How It All Fits Together
296 SQLite is modular in design.
297 See the [architectural description](http://www.sqlite.org/arch.html)
298 for details. Other documents that are useful in
299 (helping to understand how SQLite works include the
300 [file format](http://www.sqlite.org/fileformat2.html) description,
301 the [virtual machine](http://www.sqlite.org/vdbe.html) that runs
302 prepared statements, the description of
303 [how transactions work](http://www.sqlite.org/atomiccommit.html), and
304 the [overview of the query planner](http://www.sqlite.org/optoverview.html).
306 Unfortunately, years of effort have gone into optimizating SQLite, both
307 for small size and high performance.  And optimizations tend to result in
308 complex code.  So there is a lot of complexity in the SQLite implementation.
310 Key files:
312   *  **sqlite.h.in** - This file defines the public interface to the SQLite
313      library.  Readers will need to be familiar with this interface before
314      trying to understand how the library works internally.
316   *  **sqliteInt.h** - this header file defines many of the data objects
317      used internally by SQLite.
319   *  **parse.y** - This file describes the LALR(1) grammer that SQLite uses
320      to parse SQL statements, and the actions that are taken at each step
321      in the parsing process.
323   *  **vdbe.c** - This file implements the virtual machine that runs
324      prepared statements.  There are various helper files whose names
325      begin with "vdbe".  The VDBE has access to the vdbeInt.h header file
326      which defines internal data objects.  The rest of SQLite interacts
327      with the VDBE through an interface defined by vdbe.h.
329   *  **where.c** - This file analyzes the WHERE clause and generates
330      virtual machine code to run queries efficiently.  This file is
331      sometimes called the "query optimizer".  It has its own private
332      header file, whereInt.h, that defines data objects used internally.
334   *  **btree.c** - This file contains the implementation of the B-Tree
335      storage engine used by SQLite.
337   *  **pager.c** - This file contains the "pager" implementation, the
338      module that implements transactions.
340   *  **os_unix.c** and **os_win.c** - These two files implement the interface
341      between SQLite and the underlying operating system using the run-time
342      pluggable VFS interface.
344   *  **shell.c** - This file is not part of the core SQLite library.  This
345      is the file that, when linked against sqlite3.a, generates the
346      "sqlite3.exe" command-line shell.
348   *  **tclsqlite.c** - This file implements the Tcl bindings for SQLite.  It
349      is not part of the core SQLite library.  But as most of the tests in this
350      repository are written in Tcl, the Tcl language bindings are important.
352 There are many other source files.  Each has a suscinct header comment that
353 describes its purpose and role within the larger system.
356 ## Contacts
358 The main SQLite webpage is [http://www.sqlite.org/](http://www.sqlite.org/)
359 with geographically distributed backup servers at
360 [http://www2.sqlite.org/](http://www2.sqlite.org) and
361 [http://www3.sqlite.org/](http://www3.sqlite.org).