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