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 #include "sqliteInt.h"
43 #include <android/log.h>
46 /* extensions defined in pager.c */
47 void *sqlite3PagerGetCodec(Pager
*);
48 void sqlite3PagerSetCodec(Pager
*, void *(*)(void*,void*,Pgno
,int), void (*)(void*,int,int), void (*)(void*), void *);
49 int sqlite3pager_is_mj_pgno(Pager
*, Pgno
);
50 void sqlite3pager_error(Pager
*, int);
51 void sqlite3pager_reset(Pager
*pPager
);
52 /* end extensions defined in pager.c */
54 #if !defined (SQLCIPHER_CRYPTO_CC) \
55 && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
56 && !defined (SQLCIPHER_CRYPTO_NSS) \
57 && !defined (SQLCIPHER_CRYPTO_OPENSSL)
58 #define SQLCIPHER_CRYPTO_OPENSSL
61 #define FILE_HEADER_SZ 16
63 #define CIPHER_XSTR(s) CIPHER_STR(s)
64 #define CIPHER_STR(s) #s
66 #ifndef CIPHER_VERSION_NUMBER
67 #define CIPHER_VERSION_NUMBER 4.5.1
70 #ifndef CIPHER_VERSION_BUILD
71 #define CIPHER_VERSION_BUILD community
74 #define CIPHER_DECRYPT 0
75 #define CIPHER_ENCRYPT 1
77 #define CIPHER_READ_CTX 0
78 #define CIPHER_WRITE_CTX 1
79 #define CIPHER_READWRITE_CTX 2
82 #define PBKDF2_ITER 256000
85 /* possible flags for cipher_ctx->flags */
86 #define CIPHER_FLAG_HMAC 0x01
87 #define CIPHER_FLAG_LE_PGNO 0x02
88 #define CIPHER_FLAG_BE_PGNO 0x04
90 #ifndef DEFAULT_CIPHER_FLAGS
91 #define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
95 /* by default, sqlcipher will use a reduced number of iterations to generate
96 the HMAC key / or transform a raw cipher key
98 #ifndef FAST_PBKDF2_ITER
99 #define FAST_PBKDF2_ITER 2
102 /* this if a fixed random array that will be xor'd with the database salt to ensure that the
103 salt passed to the HMAC key derivation function is not the same as that used to derive
104 the encryption key. This can be overridden at compile time but it will make the resulting
105 binary incompatible with the default builds when using HMAC. A future version of SQLcipher
106 will likely allow this to be defined at runtime via pragma */
107 #ifndef HMAC_SALT_MASK
108 #define HMAC_SALT_MASK 0x3a
111 #ifndef CIPHER_MAX_IV_SZ
112 #define CIPHER_MAX_IV_SZ 16
115 #ifndef CIPHER_MAX_KEY_SZ
116 #define CIPHER_MAX_KEY_SZ 64
121 ** Simple shared routines for converting hex char strings to binary data
123 static int cipher_hex2int(char c
) {
124 return (c
>='0' && c
<='9') ? (c
)-'0' :
125 (c
>='A' && c
<='F') ? (c
)-'A'+10 :
126 (c
>='a' && c
<='f') ? (c
)-'a'+10 : 0;
129 static void cipher_hex2bin(const unsigned char *hex
, int sz
, unsigned char *out
){
131 for(i
= 0; i
< sz
; i
+= 2){
132 out
[i
/2] = (cipher_hex2int(hex
[i
])<<4) | cipher_hex2int(hex
[i
+1]);
136 static void cipher_bin2hex(const unsigned char* in
, int sz
, char *out
) {
138 for(i
=0; i
< sz
; i
++) {
139 sqlite3_snprintf(3, out
+ (i
*2), "%02x ", in
[i
]);
143 static int cipher_isHex(const unsigned char *hex
, int sz
){
145 for(i
= 0; i
< sz
; i
++) {
146 unsigned char c
= hex
[i
];
147 if ((c
< '0' || c
> '9') &&
148 (c
< 'A' || c
> 'F') &&
149 (c
< 'a' || c
> 'f')) {
156 /* possible flags for simulating specific test conditions */
157 #ifdef SQLCIPHER_TEST
158 #define TEST_FAIL_ENCRYPT 0x01
159 #define TEST_FAIL_DECRYPT 0x02
160 #define TEST_FAIL_MIGRATE 0x04
161 unsigned int sqlcipher_get_test_flags(void);
162 void sqlcipher_set_test_flags(unsigned int);
163 int sqlcipher_get_test_rand(void);
164 void sqlcipher_set_test_rand(int);
165 int sqlcipher_get_test_fail(void);
168 /* extensions defined in crypto_impl.c */
169 /* the default implementation of SQLCipher uses a cipher_ctx
170 to keep track of read / write state separately. The following
171 struct and associated functions are defined here */
176 unsigned char *hmac_key
;
194 int plaintext_header_sz
;
197 unsigned int skip_read_hmac
;
198 unsigned int need_kdf_salt
;
200 unsigned char *kdf_salt
;
201 unsigned char *hmac_kdf_salt
;
202 unsigned char *buffer
;
204 cipher_ctx
*read_ctx
;
205 cipher_ctx
*write_ctx
;
206 sqlcipher_provider
*provider
;
210 /* crypto.c functions */
211 int sqlcipher_codec_pragma(sqlite3
*, int, Parse
*, const char *, const char*);
212 int sqlite3CodecAttach(sqlite3
*, int, const void *, int);
213 void sqlite3CodecGetKey(sqlite3
*, int, void**, int*);
214 void sqlcipher_exportFunc(sqlite3_context
*, int, sqlite3_value
**);
216 /* crypto_impl.c functions */
218 void sqlcipher_init_memmethods(void);
220 /* activation and initialization */
221 void sqlcipher_activate(void);
222 void sqlcipher_deactivate(void);
224 int sqlcipher_codec_ctx_init(codec_ctx
**, Db
*, Pager
*, const void *, int);
225 void sqlcipher_codec_ctx_free(codec_ctx
**);
226 int sqlcipher_codec_key_derive(codec_ctx
*);
227 int sqlcipher_codec_key_copy(codec_ctx
*, int);
229 /* page cipher implementation */
230 int sqlcipher_page_cipher(codec_ctx
*, int, Pgno
, int, int, unsigned char *, unsigned char *);
232 /* context setters & getters */
233 void sqlcipher_codec_ctx_set_error(codec_ctx
*, int);
235 void sqlcipher_codec_get_pass(codec_ctx
*, void **, int *);
236 int sqlcipher_codec_ctx_set_pass(codec_ctx
*, const void *, int, int);
237 void sqlcipher_codec_get_keyspec(codec_ctx
*, void **zKey
, int *nKey
);
239 int sqlcipher_codec_ctx_set_pagesize(codec_ctx
*, int);
240 int sqlcipher_codec_ctx_get_pagesize(codec_ctx
*);
241 int sqlcipher_codec_ctx_get_reservesize(codec_ctx
*);
243 void sqlcipher_set_default_pagesize(int page_size
);
244 int sqlcipher_get_default_pagesize(void);
246 void sqlcipher_set_default_kdf_iter(int iter
);
247 int sqlcipher_get_default_kdf_iter(void);
248 int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx
*, int);
249 int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx
*ctx
);
251 int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx
*ctx
, unsigned char *salt
, int sz
);
252 int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx
*ctx
, void **salt
);
254 int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx
*, int);
255 int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx
*);
257 const char* sqlcipher_codec_ctx_get_cipher(codec_ctx
*ctx
);
259 void* sqlcipher_codec_ctx_get_data(codec_ctx
*);
261 void sqlcipher_set_default_use_hmac(int use
);
262 int sqlcipher_get_default_use_hmac(void);
264 void sqlcipher_set_hmac_salt_mask(unsigned char mask
);
265 unsigned char sqlcipher_get_hmac_salt_mask(void);
267 int sqlcipher_codec_ctx_set_use_hmac(codec_ctx
*ctx
, int use
);
268 int sqlcipher_codec_ctx_get_use_hmac(codec_ctx
*ctx
);
270 int sqlcipher_codec_ctx_set_flag(codec_ctx
*ctx
, unsigned int flag
);
271 int sqlcipher_codec_ctx_unset_flag(codec_ctx
*ctx
, unsigned int flag
);
272 int sqlcipher_codec_ctx_get_flag(codec_ctx
*ctx
, unsigned int flag
);
274 const char* sqlcipher_codec_get_cipher_provider(codec_ctx
*ctx
);
275 int sqlcipher_codec_ctx_migrate(codec_ctx
*ctx
);
276 int sqlcipher_codec_add_random(codec_ctx
*ctx
, const char *data
, int random_sz
);
277 int sqlcipher_cipher_profile(sqlite3
*db
, const char *destination
);
278 int sqlcipher_codec_get_store_pass(codec_ctx
*ctx
);
279 void sqlcipher_codec_get_pass(codec_ctx
*ctx
, void **zKey
, int *nKey
);
280 void sqlcipher_codec_set_store_pass(codec_ctx
*ctx
, int value
);
281 int sqlcipher_codec_fips_status(codec_ctx
*ctx
);
282 const char* sqlcipher_codec_get_provider_version(codec_ctx
*ctx
);
284 int sqlcipher_set_default_plaintext_header_size(int size
);
285 int sqlcipher_get_default_plaintext_header_size(void);
286 int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx
*ctx
, int size
);
287 int sqlcipher_codec_ctx_get_plaintext_header_size(codec_ctx
*ctx
);
289 int sqlcipher_set_default_hmac_algorithm(int algorithm
);
290 int sqlcipher_get_default_hmac_algorithm(void);
291 int sqlcipher_codec_ctx_set_hmac_algorithm(codec_ctx
*ctx
, int algorithm
);
292 int sqlcipher_codec_ctx_get_hmac_algorithm(codec_ctx
*ctx
);
294 int sqlcipher_set_default_kdf_algorithm(int algorithm
);
295 int sqlcipher_get_default_kdf_algorithm(void);
296 int sqlcipher_codec_ctx_set_kdf_algorithm(codec_ctx
*ctx
, int algorithm
);
297 int sqlcipher_codec_ctx_get_kdf_algorithm(codec_ctx
*ctx
);
299 void sqlcipher_set_mem_security(int);
300 int sqlcipher_get_mem_security(void);
302 int sqlcipher_find_db_index(sqlite3
*db
, const char *zDb
);
304 int sqlcipher_codec_ctx_integrity_check(codec_ctx
*, Parse
*, char *);
306 int sqlcipher_set_log(const char *destination
);
307 int sqlcipher_set_log_level(unsigned int level
);
308 void sqlcipher_log(unsigned int tag
, const char *message
, ...);
310 #define SQLCIPHER_LOG_NONE 0x00
311 #define SQLCIPHER_LOG_ERROR 0x01
312 #define SQLCIPHER_LOG_WARN 0x02
313 #define SQLCIPHER_LOG_INFO 0x04
314 #define SQLCIPHER_LOG_DEBUG 0x08
315 #define SQLCIPHER_LOG_TRACE 0xffffffff
317 #ifdef CODEC_DEBUG_PAGEDATA
318 #define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
322 for(__pctr=0; __pctr < LEN; __pctr++) { \
323 if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \
324 printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \
330 #define CODEC_HEXDUMP(DESC,BUFFER,LEN)