3 ** crypto.h developed by Stephen Lombardo (Zetetic LLC)
4 ** sjlombardo at zetetic dot net
7 ** Copyright (c) 2008, ZETETIC LLC
8 ** All rights reserved.
10 ** Redistribution and use in source and binary forms, with or without
11 ** modification, are permitted provided that the following conditions are met:
12 ** * Redistributions of source code must retain the above copyright
13 ** notice, this list of conditions and the following disclaimer.
14 ** * Redistributions in binary form must reproduce the above copyright
15 ** notice, this list of conditions and the following disclaimer in the
16 ** documentation and/or other materials provided with the distribution.
17 ** * Neither the name of the ZETETIC LLC nor the
18 ** names of its contributors may be used to endorse or promote products
19 ** derived from this software without specific prior written permission.
21 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
22 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
25 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #ifdef SQLITE_HAS_CODEC
38 #define FILE_HEADER_SZ 16
41 #define CIPHER "aes-256-cbc"
44 #define CIPHER_DECRYPT 0
45 #define CIPHER_ENCRYPT 1
47 #define CIPHER_READ_CTX 0
48 #define CIPHER_WRITE_CTX 1
49 #define CIPHER_READWRITE_CTX 2
52 #define PBKDF2_ITER 4000
55 #ifndef DEFAULT_USE_HMAC
56 #define DEFAULT_USE_HMAC 1
59 /* by default, sqlcipher will use a reduced number of iterations to generate
60 the HMAC key / or transform a raw cipher key
62 #ifndef FAST_PBKDF2_ITER
63 #define FAST_PBKDF2_ITER 2
66 /* this if a fixed random array that will be xor'd with the database salt to ensure that the
67 salt passed to the HMAC key derivation function is not the same as that used to derive
68 the encryption key. This can be overridden at compile time but it will make the resulting
69 binary incompatible with the default builds when using HMAC. A future version of SQLcipher
70 will likely allow this to be defined at runtime via pragma */
71 #ifndef HMAC_SALT_MASK
72 #define HMAC_SALT_MASK 0x3a
76 #define CODEC_TRACE(X) {printf X;fflush(stdout);}
78 #define CODEC_TRACE(X)
82 /* extensions defined in pragma.c */
84 void sqlite3pager_get_codec(Pager
*pPager
, void **ctx
);
85 int sqlite3pager_is_mj_pgno(Pager
*pPager
, Pgno pgno
);
86 sqlite3_file
*sqlite3Pager_get_fd(Pager
*pPager
);
87 void sqlite3pager_sqlite3PagerSetCodec(
89 void *(*xCodec
)(void*,void*,Pgno
,int),
90 void (*xCodecSizeChng
)(void*,int,int),
91 void (*xCodecFree
)(void*),
94 /* end extensions defined in pragma.c */
97 ** Simple shared routines for converting hex char strings to binary data
99 static int cipher_hex2int(char c
) {
100 return (c
>='0' && c
<='9') ? (c
)-'0' :
101 (c
>='A' && c
<='F') ? (c
)-'A'+10 :
102 (c
>='a' && c
<='f') ? (c
)-'a'+10 : 0;
105 static void cipher_hex2bin(const char *hex
, int sz
, unsigned char *out
){
107 for(i
= 0; i
< sz
; i
+= 2){
108 out
[i
/2] = (cipher_hex2int(hex
[i
])<<4) | cipher_hex2int(hex
[i
+1]);
112 /* extensions defined in crypto_impl.c */
114 typedef struct codec_ctx codec_ctx
;
116 /* utility functions */
117 int sqlcipher_memcmp(const unsigned char *a0
, const unsigned char *a1
, int len
);
118 int sqlcipher_pseudorandom(void *, int);
119 void sqlcipher_free(void *, int);
121 /* activation and initialization */
122 void sqlcipher_activate();
123 int sqlcipher_codec_ctx_init(codec_ctx
**, Db
*, Pager
*, sqlite3_file
*, const void *, int);
124 void sqlcipher_codec_ctx_free(codec_ctx
**);
125 int sqlcipher_codec_key_derive(codec_ctx
*);
126 int sqlcipher_codec_key_copy(codec_ctx
*, int);
128 /* page cipher implementation */
129 int sqlcipher_page_cipher(codec_ctx
*, int, Pgno
, int, int, unsigned char *, unsigned char *);
131 /* context setters & getters */
132 void sqlcipher_codec_ctx_set_error(codec_ctx
*, int);
134 int sqlcipher_codec_ctx_set_pass(codec_ctx
*, const void *, int, int);
135 void sqlcipher_codec_get_pass(codec_ctx
*, void **zKey
, int *nKey
);
137 int sqlcipher_codec_ctx_set_pagesize(codec_ctx
*, int);
138 int sqlcipher_codec_ctx_get_pagesize(codec_ctx
*);
139 int sqlcipher_codec_ctx_get_reservesize(codec_ctx
*);
141 int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx
*, int, int);
142 void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx
*ctx
);
144 int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx
*, int, int);
146 int sqlcipher_codec_ctx_set_cipher(codec_ctx
*, const char *, int);
148 void* sqlcipher_codec_ctx_get_data(codec_ctx
*);
150 void sqlcipher_exportFunc(sqlite3_context
*, int, sqlite3_value
**);
152 void sqlcipher_set_default_use_hmac(int use
);
154 int sqlcipher_codec_ctx_set_use_hmac(codec_ctx
*ctx
, int use
);
155 /* end extensions defined in crypto_impl.c */