Merges crypto.h, crypto.c, and crypto_impl.c into sqlcipher.c
[sqlcipher.git] / src / sqlcipher.c
blobb7564b8bfa828a4020e76a2d263595049cf6338a
1 /*
2 ** SQLCipher
3 ** http://zetetic.net
4 **
5 ** Copyright (c) 2008-2024, ZETETIC LLC
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions are met:
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in the
14 ** documentation and/or other materials provided with the distribution.
15 ** * Neither the name of the ZETETIC LLC nor the
16 ** names of its contributors may be used to endorse or promote products
17 ** derived from this software without specific prior written permission.
18 **
19 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
20 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
23 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 **
31 /* BEGIN SQLCIPHER */
32 #ifdef SQLITE_HAS_CODEC
34 #include "sqliteInt.h"
35 #include "btreeInt.h"
36 #include "pager.h"
37 #include "vdbeInt.h"
39 #if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
40 #if defined(__ANDROID__)
41 #include <android/log.h>
42 #elif defined(__APPLE__)
43 #include <TargetConditionals.h>
44 #include <os/log.h>
45 #endif
46 #endif
48 #include <time.h>
50 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
51 #include <windows.h> /* amalgamator: dontcache */
52 #else
53 #include <sys/time.h> /* amalgamator: dontcache */
54 #endif
56 #ifndef OMIT_MEMLOCK
57 #if defined(__unix__) || defined(__APPLE__) || defined(_AIX)
58 #include <errno.h> /* amalgamator: dontcache */
59 #include <unistd.h> /* amalgamator: dontcache */
60 #include <sys/resource.h> /* amalgamator: dontcache */
61 #include <sys/mman.h> /* amalgamator: dontcache */
62 #endif
63 #endif
65 #include <assert.h>
66 #include "sqlcipher.h"
68 /* extensions defined in pager.c */
69 void *sqlcipherPagerGetCodec(Pager*);
70 void sqlcipherPagerSetCodec(Pager*, void *(*)(void*,void*,Pgno,int), void (*)(void*,int,int), void (*)(void*), void *);
71 int sqlite3pager_is_sj_pgno(Pager*, Pgno);
72 void sqlite3pager_error(Pager*, int);
73 void sqlite3pager_reset(Pager *pPager);
74 /* end extensions defined in pager.c */
76 #if !defined (SQLCIPHER_CRYPTO_CC) \
77 && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
78 && !defined (SQLCIPHER_CRYPTO_NSS) \
79 && !defined (SQLCIPHER_CRYPTO_OPENSSL)
80 #define SQLCIPHER_CRYPTO_OPENSSL
81 #endif
83 #define FILE_HEADER_SZ 16
85 #define CIPHER_XSTR(s) CIPHER_STR(s)
86 #define CIPHER_STR(s) #s
88 #ifndef CIPHER_VERSION_NUMBER
89 #define CIPHER_VERSION_NUMBER 4.6.1
90 #endif
92 #ifndef CIPHER_VERSION_BUILD
93 #define CIPHER_VERSION_BUILD community
94 #endif
96 #define CIPHER_DECRYPT 0
97 #define CIPHER_ENCRYPT 1
99 #define CIPHER_READ_CTX 0
100 #define CIPHER_WRITE_CTX 1
101 #define CIPHER_READWRITE_CTX 2
103 #ifndef PBKDF2_ITER
104 #define PBKDF2_ITER 256000
105 #endif
107 #define SQLCIPHER_FLAG_GET(FLAG,BIT) ((FLAG & BIT) != 0)
108 #define SQLCIPHER_FLAG_SET(FLAG,BIT) FLAG |= BIT
109 #define SQLCIPHER_FLAG_UNSET(FLAG,BIT) FLAG &= ~BIT
111 /* possible flags for codec_ctx->flags */
112 #define CIPHER_FLAG_HMAC (1 << 0)
113 #define CIPHER_FLAG_LE_PGNO (1 << 1)
114 #define CIPHER_FLAG_BE_PGNO (1 << 2)
115 #define CIPHER_FLAG_KEY_USED (1 << 3)
116 #define CIPHER_FLAG_HAS_KDF_SALT (1 << 4)
119 #ifndef DEFAULT_CIPHER_FLAGS
120 #define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
121 #endif
124 /* by default, sqlcipher will use a reduced number of iterations to generate
125 the HMAC key / or transform a raw cipher key
127 #ifndef FAST_PBKDF2_ITER
128 #define FAST_PBKDF2_ITER 2
129 #endif
131 /* this if a fixed random array that will be xor'd with the database salt to ensure that the
132 salt passed to the HMAC key derivation function is not the same as that used to derive
133 the encryption key. This can be overridden at compile time but it will make the resulting
134 binary incompatible with the default builds when using HMAC. A future version of SQLcipher
135 will likely allow this to be defined at runtime via pragma */
136 #ifndef HMAC_SALT_MASK
137 #define HMAC_SALT_MASK 0x3a
138 #endif
140 #ifndef CIPHER_MAX_IV_SZ
141 #define CIPHER_MAX_IV_SZ 16
142 #endif
144 #ifndef CIPHER_MAX_KEY_SZ
145 #define CIPHER_MAX_KEY_SZ 64
146 #endif
150 ** Simple shared routines for converting hex char strings to binary data
152 static int cipher_hex2int(char c) {
153 return (c>='0' && c<='9') ? (c)-'0' :
154 (c>='A' && c<='F') ? (c)-'A'+10 :
155 (c>='a' && c<='f') ? (c)-'a'+10 : 0;
158 static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){
159 int i;
160 for(i = 0; i < sz; i += 2){
161 out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]);
165 static void cipher_bin2hex(const unsigned char* in, int sz, char *out) {
166 int i;
167 for(i=0; i < sz; i++) {
168 sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]);
172 static int cipher_isHex(const unsigned char *hex, int sz){
173 int i;
174 for(i = 0; i < sz; i++) {
175 unsigned char c = hex[i];
176 if ((c < '0' || c > '9') &&
177 (c < 'A' || c > 'F') &&
178 (c < 'a' || c > 'f')) {
179 return 0;
182 return 1;
185 /* the default implementation of SQLCipher uses a cipher_ctx
186 to keep track of read / write state separately. The following
187 struct and associated functions are defined here */
188 typedef struct {
189 int derive_key;
190 int pass_sz;
191 unsigned char *key;
192 unsigned char *hmac_key;
193 unsigned char *pass;
194 char *keyspec;
195 } cipher_ctx;
198 typedef struct {
199 int store_pass;
200 int kdf_iter;
201 int fast_kdf_iter;
202 int kdf_salt_sz;
203 int key_sz;
204 int iv_sz;
205 int block_sz;
206 int page_sz;
207 int keyspec_sz;
208 int reserve_sz;
209 int hmac_sz;
210 int plaintext_header_sz;
211 int hmac_algorithm;
212 int kdf_algorithm;
213 unsigned int flags;
214 unsigned char *kdf_salt;
215 unsigned char *hmac_kdf_salt;
216 unsigned char *buffer;
217 Btree *pBt;
218 cipher_ctx *read_ctx;
219 cipher_ctx *write_ctx;
220 sqlcipher_provider *provider;
221 void *provider_ctx;
222 } codec_ctx ;
225 #ifdef SQLCIPHER_TEST
226 /* possible flags for simulating specific test conditions */
227 #define TEST_FAIL_ENCRYPT 0x01
228 #define TEST_FAIL_DECRYPT 0x02
229 #define TEST_FAIL_MIGRATE 0x04
231 static volatile unsigned int cipher_test_flags = 0;
232 static volatile int cipher_test_rand = 0;
234 static int sqlcipher_get_test_fail() {
235 int x;
237 /* if cipher_test_rand is not set to a non-zero value always fail (return true) */
238 if (cipher_test_rand == 0) return 1;
240 sqlite3_randomness(sizeof(x), &x);
241 return ((x % cipher_test_rand) == 0);
243 #endif
245 static volatile unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
246 static volatile unsigned char hmac_salt_mask = HMAC_SALT_MASK;
247 static volatile int default_kdf_iter = PBKDF2_ITER;
248 static volatile int default_page_size = 4096;
249 static volatile int default_plaintext_header_size = 0;
250 static volatile int default_hmac_algorithm = SQLCIPHER_HMAC_SHA512;
251 static volatile int default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512;
252 static volatile int sqlcipher_mem_security_on = 0;
253 static volatile int sqlcipher_mem_executed = 0;
254 static volatile int sqlcipher_mem_initialized = 0;
255 static volatile unsigned int sqlcipher_activate_count = 0;
256 static volatile sqlite3_mem_methods default_mem_methods;
257 static sqlcipher_provider *default_provider = NULL;
259 static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
260 static FILE* sqlcipher_log_file = NULL;
261 static volatile int sqlcipher_log_device = 0;
262 static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE;
263 static volatile unsigned int sqlcipher_log_source = SQLCIPHER_LOG_ALL;
264 static volatile int sqlcipher_log_set = 0;
266 sqlite3_mutex* sqlcipher_mutex(int mutex) {
267 if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
268 return sqlcipher_static_mutex[mutex];
271 static void sqlcipher_mlock(void *ptr, sqlite_uint64 sz) {
272 #ifndef OMIT_MEMLOCK
273 #if defined(__unix__) || defined(__APPLE__)
274 int rc;
275 unsigned long pagesize = sysconf(_SC_PAGESIZE);
276 unsigned long offset = (unsigned long) ptr % pagesize;
278 if(ptr == NULL || sz == 0) return;
280 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu", ptr - offset, sz + offset, pagesize);
281 rc = mlock(ptr - offset, sz + offset);
282 if(rc!=0) {
283 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock() returned %d errno=%d", rc, errno);
284 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: mlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno);
286 #elif defined(_WIN32)
287 #if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
288 int rc;
289 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: calling VirtualLock(%p,%d)", ptr, sz);
290 rc = VirtualLock(ptr, sz);
291 if(rc==0) {
292 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock() returned %d LastError=%d", rc, GetLastError());
293 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_mlock: VirtualLock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError());
295 #endif
296 #endif
297 #endif
300 static void sqlcipher_munlock(void *ptr, sqlite_uint64 sz) {
301 #ifndef OMIT_MEMLOCK
302 #if defined(__unix__) || defined(__APPLE__)
303 int rc;
304 unsigned long pagesize = sysconf(_SC_PAGESIZE);
305 unsigned long offset = (unsigned long) ptr % pagesize;
307 if(ptr == NULL || sz == 0) return;
309 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling munlock(%p,%lu)", ptr - offset, sz + offset);
310 rc = munlock(ptr - offset, sz + offset);
311 if(rc!=0) {
312 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: munlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno);
314 #elif defined(_WIN32)
315 #if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
316 int rc;
318 if(ptr == NULL || sz == 0) return;
320 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: calling VirtualUnlock(%p,%d)", ptr, sz);
321 rc = VirtualUnlock(ptr, sz);
323 /* because memory allocations may be made from the same individual page, it is possible for VirtualUnlock to be called
324 * multiple times for the same page. Subsequent calls will return an error, but this can be safely ignored (i.e. because
325 * the previous call for that page unlocked the memory already). Log an info level event only in that case. */
326 if(!rc) {
327 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_MEMORY, "sqlcipher_munlock: VirtualUnlock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError());
329 #endif
330 #endif
331 #endif
334 static int sqlcipher_mem_init(void *pAppData) {
335 return default_mem_methods.xInit(pAppData);
337 static void sqlcipher_mem_shutdown(void *pAppData) {
338 default_mem_methods.xShutdown(pAppData);
340 static void *sqlcipher_mem_malloc(int n) {
341 void *ptr = default_mem_methods.xMalloc(n);
342 if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
343 if(sqlcipher_mem_security_on) {
344 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)", ptr, n);
345 sqlcipher_mlock(ptr, n);
347 return ptr;
349 static int sqlcipher_mem_size(void *p) {
350 return default_mem_methods.xSize(p);
352 static void sqlcipher_mem_free(void *p) {
353 int sz;
354 if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
355 if(sqlcipher_mem_security_on) {
356 sz = sqlcipher_mem_size(p);
357 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_mem_free: calling sqlcipher_memset(%p,0,%d) and sqlcipher_munlock(%p, %d)", p, sz, p, sz);
358 sqlcipher_memset(p, 0, sz);
359 sqlcipher_munlock(p, sz);
361 default_mem_methods.xFree(p);
363 static void *sqlcipher_mem_realloc(void *p, int n) {
364 void *new = NULL;
365 int orig_sz = 0;
366 if(sqlcipher_mem_security_on) {
367 orig_sz = sqlcipher_mem_size(p);
368 if (n==0) {
369 sqlcipher_mem_free(p);
370 return NULL;
371 } else if (!p) {
372 return sqlcipher_mem_malloc(n);
373 } else if(n <= orig_sz) {
374 return p;
375 } else {
376 new = sqlcipher_mem_malloc(n);
377 if(new) {
378 memcpy(new, p, orig_sz);
379 sqlcipher_mem_free(p);
381 return new;
383 } else {
384 return default_mem_methods.xRealloc(p, n);
388 static int sqlcipher_mem_roundup(int n) {
389 return default_mem_methods.xRoundup(n);
392 static sqlite3_mem_methods sqlcipher_mem_methods = {
393 sqlcipher_mem_malloc,
394 sqlcipher_mem_free,
395 sqlcipher_mem_realloc,
396 sqlcipher_mem_size,
397 sqlcipher_mem_roundup,
398 sqlcipher_mem_init,
399 sqlcipher_mem_shutdown,
403 void sqlcipher_init_memmethods() {
404 if(sqlcipher_mem_initialized) return;
405 if(sqlite3_config(SQLITE_CONFIG_GETMALLOC, &default_mem_methods) != SQLITE_OK ||
406 sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlcipher_mem_methods) != SQLITE_OK) {
407 sqlcipher_mem_security_on = sqlcipher_mem_executed = sqlcipher_mem_initialized = 0;
408 } else {
409 sqlcipher_mem_initialized = 1;
413 int sqlcipher_register_provider(sqlcipher_provider *p) {
414 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_register_provider: entering SQLCIPHER_MUTEX_PROVIDER");
415 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
416 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_register_provider: entered SQLCIPHER_MUTEX_PROVIDER");
418 if(default_provider != NULL && default_provider != p) {
419 /* only free the current registerd provider if it has been initialized
420 and it isn't a pointer to the same provider passed to the function
421 (i.e. protect against a caller calling register twice for the same provider) */
422 sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
424 default_provider = p;
425 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_register_provider: leaving SQLCIPHER_MUTEX_PROVIDER");
426 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
427 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_register_provider: left SQLCIPHER_MUTEX_PROVIDER");
429 return SQLITE_OK;
432 /* return a pointer to the currently registered provider. This will
433 allow an application to fetch the current registered provider and
434 make minor changes to it */
435 sqlcipher_provider* sqlcipher_get_provider() {
436 return default_provider;
439 static void sqlcipher_activate() {
440 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_activate: entering static master mutex");
441 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
442 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_activate: entered static master mutex");
444 /* allocate new mutexes */
445 if(sqlcipher_activate_count == 0) {
446 int i;
447 for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) {
448 sqlcipher_static_mutex[i] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
450 #ifndef SQLCIPHER_OMIT_DEFAULT_LOGGING
451 /* when sqlcipher is first activated, set a default log target and level of WARN if the
452 logging settings have not yet been initialized. Use the "device log" for
453 android (logcat) or apple (console). Use stderr on all other platforms. */
454 if(!sqlcipher_log_set) {
456 /* set log level if it is different than the uninitalized default value of NONE */
457 if(sqlcipher_log_level == SQLCIPHER_LOG_NONE) {
458 sqlcipher_log_level = SQLCIPHER_LOG_WARN;
461 /* set the default file or device if neither is already set */
462 if(sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) {
463 #if defined(__ANDROID__) || defined(__APPLE_)
464 sqlcipher_log_device = 1;
465 #else
466 sqlcipher_log_file = stderr;
467 #endif
469 sqlcipher_log_set = 1;
471 #endif
474 /* check to see if there is a provider registered at this point
475 if there no provider registered at this point, register the
476 default provider */
477 if(sqlcipher_get_provider() == NULL) {
478 sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider));
479 #if defined (SQLCIPHER_CRYPTO_CC)
480 extern int sqlcipher_cc_setup(sqlcipher_provider *p);
481 sqlcipher_cc_setup(p);
482 #elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT)
483 extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
484 sqlcipher_ltc_setup(p);
485 #elif defined (SQLCIPHER_CRYPTO_NSS)
486 extern int sqlcipher_nss_setup(sqlcipher_provider *p);
487 sqlcipher_nss_setup(p);
488 #elif defined (SQLCIPHER_CRYPTO_OPENSSL)
489 extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
490 sqlcipher_openssl_setup(p);
491 #else
492 #error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
493 #endif
494 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_activate: calling sqlcipher_register_provider(%p)", p);
495 #ifdef SQLCIPHER_EXT
496 sqlcipher_ext_provider_setup(p);
497 #endif
498 sqlcipher_register_provider(p);
499 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_activate: called sqlcipher_register_provider(%p)",p);
502 sqlcipher_activate_count++; /* increment activation count */
504 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_activate: leaving static master mutex");
505 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
506 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_activate: left static master mutex");
509 static void sqlcipher_deactivate() {
510 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: entering static master mutex");
511 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
512 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: entered static master mutex");
514 sqlcipher_activate_count--;
515 /* if no connections are using sqlcipher, cleanup globals */
516 if(sqlcipher_activate_count < 1) {
517 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: entering SQLCIPHER_MUTEX_PROVIDER");
518 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
519 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: entered SQLCIPHER_MUTEX_PROVIDER");
521 if(default_provider != NULL) {
522 sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
523 default_provider = NULL;
526 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER");
527 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
528 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: left SQLCIPHER_MUTEX_PROVIDER");
530 #ifdef SQLCIPHER_EXT
531 sqlcipher_ext_provider_destroy();
532 #endif
534 /* last connection closed, free mutexes */
535 if(sqlcipher_activate_count == 0) {
536 int i;
537 for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) {
538 sqlite3_mutex_free(sqlcipher_static_mutex[i]);
541 sqlcipher_activate_count = 0; /* reset activation count */
544 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: leaving static master mutex");
545 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
546 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_deactivate: left static master mutex");
549 /* constant time memset using volitile to avoid having the memset
550 optimized out by the compiler.
551 Note: As suggested by Joachim Schipper (joachim.schipper@fox-it.com)
553 void* sqlcipher_memset(void *v, unsigned char value, sqlite_uint64 len) {
554 volatile sqlite_uint64 i = 0;
555 volatile unsigned char *a = v;
557 if (v == NULL) return v;
559 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_memset: setting %p[0-%llu]=%d)", a, len, value);
560 for(i = 0; i < len; i++) {
561 a[i] = value;
564 return v;
567 /* constant time memory check tests every position of a memory segement
568 matches a single value (i.e. the memory is all zeros)
569 returns 0 if match, 1 of no match */
570 int sqlcipher_ismemset(const void *v, unsigned char value, sqlite_uint64 len) {
571 const volatile unsigned char *a = v;
572 volatile sqlite_uint64 i = 0, result = 0;
574 for(i = 0; i < len; i++) {
575 result |= a[i] ^ value;
578 return (result != 0);
581 /* constant time memory comparison routine.
582 returns 0 if match, 1 if no match */
583 int sqlcipher_memcmp(const void *v0, const void *v1, int len) {
584 const volatile unsigned char *a0 = v0, *a1 = v1;
585 volatile int i = 0, result = 0;
587 for(i = 0; i < len; i++) {
588 result |= a0[i] ^ a1[i];
591 return (result != 0);
595 * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory
596 * can be countend and memory leak detection works in the test suite.
597 * If ptr is not null memory will be freed.
598 * If sz is greater than zero, the memory will be overwritten with zero before it is freed
599 * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the
600 * memory segment so it can be paged
602 void sqlcipher_free(void *ptr, sqlite_uint64 sz) {
603 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_free: calling sqlcipher_memset(%p,0,%llu)", ptr, sz);
604 sqlcipher_memset(ptr, 0, sz);
605 sqlcipher_munlock(ptr, sz);
606 sqlite3_free(ptr);
610 * allocate memory. Uses sqlite's internall malloc wrapper so memory can be
611 * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK
612 * attempts to lock the memory pages so sensitive information won't be swapped
614 void* sqlcipher_malloc(sqlite_uint64 sz) {
615 void *ptr;
616 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_malloc: calling sqlite3Malloc(%llu)", sz);
617 ptr = sqlite3Malloc(sz);
618 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MEMORY, "sqlcipher_malloc: calling sqlcipher_memset(%p,0,%llu)", ptr, sz);
619 sqlcipher_memset(ptr, 0, sz);
620 sqlcipher_mlock(ptr, sz);
621 return ptr;
624 char* sqlcipher_version() {
625 #ifdef CIPHER_VERSION_QUALIFIER
626 char *version = sqlite3_mprintf("%s %s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_QUALIFIER), CIPHER_XSTR(CIPHER_VERSION_BUILD));
627 #else
628 char *version = sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_BUILD));
629 #endif
630 return version;
634 * Initialize new cipher_ctx struct. This function will allocate memory
635 * for the cipher context and for the key
637 * returns SQLITE_OK if initialization was successful
638 * returns SQLITE_NOMEM if an error occured allocating memory
640 static int sqlcipher_cipher_ctx_init(codec_ctx *ctx, cipher_ctx **iCtx) {
641 cipher_ctx *c_ctx;
642 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating context");
643 *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx));
644 c_ctx = *iCtx;
645 if(c_ctx == NULL) return SQLITE_NOMEM;
647 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating key");
648 c_ctx->key = (unsigned char *) sqlcipher_malloc(ctx->key_sz);
650 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_cipher_ctx_init: allocating hmac_key");
651 c_ctx->hmac_key = (unsigned char *) sqlcipher_malloc(ctx->key_sz);
653 if(c_ctx->key == NULL) return SQLITE_NOMEM;
654 if(c_ctx->hmac_key == NULL) return SQLITE_NOMEM;
656 return SQLITE_OK;
660 * Free and wipe memory associated with a cipher_ctx
662 static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) {
663 cipher_ctx *c_ctx = *iCtx;
664 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "cipher_ctx_free: iCtx=%p", iCtx);
665 if(c_ctx->key) sqlcipher_free(c_ctx->key, ctx->key_sz);
666 if(c_ctx->hmac_key) sqlcipher_free(c_ctx->hmac_key, ctx->key_sz);
667 if(c_ctx->pass) sqlcipher_free(c_ctx->pass, c_ctx->pass_sz);
668 if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
669 sqlcipher_free(c_ctx, sizeof(cipher_ctx));
672 static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
673 int base_reserve = ctx->iv_sz; /* base reserve size will be IV only */
674 int reserve = base_reserve;
676 ctx->hmac_sz = ctx->provider->get_hmac_sz(ctx->provider_ctx, ctx->hmac_algorithm);
678 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC))
679 reserve += ctx->hmac_sz; /* if reserve will include hmac, update that size */
681 /* calculate the amount of reserve needed in even increments of the cipher block size */
682 if(ctx->block_sz > 0) {
683 reserve = ((reserve % ctx->block_sz) == 0) ? reserve :
684 ((reserve / ctx->block_sz) + 1) * ctx->block_sz;
687 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d",
688 base_reserve, ctx->block_sz, ctx->hmac_sz, reserve);
690 ctx->reserve_sz = reserve;
692 return SQLITE_OK;
696 * Compare one cipher_ctx to another.
698 * returns 0 if all the parameters (except the derived key data) are the same
699 * returns 1 otherwise
701 static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
702 int are_equal = (
703 c1->pass_sz == c2->pass_sz
704 && (
705 c1->pass == c2->pass
706 || !sqlcipher_memcmp((const unsigned char*)c1->pass,
707 (const unsigned char*)c2->pass,
708 c1->pass_sz)
711 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_cmp: c1=%p c2=%p sqlcipher_memcmp(c1->pass, c2_pass)=%d are_equal=%d",
712 c1, c2,
713 (c1->pass == NULL || c2->pass == NULL) ?
714 -1 :
715 sqlcipher_memcmp(
716 (const unsigned char*)c1->pass,
717 (const unsigned char*)c2->pass,
718 c1->pass_sz
720 are_equal
723 return !are_equal; /* return 0 if they are the same, 1 otherwise */
727 * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a
728 * fully initialized context, you could copy it to write_ctx and all yet data
729 * and pass information across
731 * returns SQLITE_OK if initialization was successful
732 * returns SQLITE_NOMEM if an error occured allocating memory
734 static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_ctx *source) {
735 void *key = target->key;
736 void *hmac_key = target->hmac_key;
738 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_copy: target=%p, source=%p", target, source);
739 if(target->pass) sqlcipher_free(target->pass, target->pass_sz);
740 if(target->keyspec) sqlcipher_free(target->keyspec, ctx->keyspec_sz);
741 memcpy(target, source, sizeof(cipher_ctx));
743 target->key = key; /* restore pointer to previously allocated key data */
744 memcpy(target->key, source->key, ctx->key_sz);
746 target->hmac_key = hmac_key; /* restore pointer to previously allocated hmac key data */
747 memcpy(target->hmac_key, source->hmac_key, ctx->key_sz);
749 if(source->pass && source->pass_sz) {
750 target->pass = sqlcipher_malloc(source->pass_sz);
751 if(target->pass == NULL) return SQLITE_NOMEM;
752 memcpy(target->pass, source->pass, source->pass_sz);
754 if(source->keyspec) {
755 target->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
756 if(target->keyspec == NULL) return SQLITE_NOMEM;
757 memcpy(target->keyspec, source->keyspec, ctx->keyspec_sz);
759 return SQLITE_OK;
763 * Set the keyspec for the cipher_ctx
765 * returns SQLITE_OK if assignment was successfull
766 * returns SQLITE_NOMEM if an error occured allocating memory
768 static int sqlcipher_cipher_ctx_set_keyspec(codec_ctx *ctx, cipher_ctx *c_ctx, const unsigned char *key) {
769 /* free, zero existing pointers and size */
770 if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
771 c_ctx->keyspec = NULL;
773 c_ctx->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
774 if(c_ctx->keyspec == NULL) return SQLITE_NOMEM;
776 c_ctx->keyspec[0] = 'x';
777 c_ctx->keyspec[1] = '\'';
778 cipher_bin2hex(key, ctx->key_sz, c_ctx->keyspec + 2);
779 cipher_bin2hex(ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->keyspec + (ctx->key_sz * 2) + 2);
780 c_ctx->keyspec[ctx->keyspec_sz - 1] = '\'';
781 return SQLITE_OK;
784 static void sqlcipher_set_derive_key(codec_ctx *ctx, int derive) {
785 if(ctx->read_ctx != NULL) ctx->read_ctx->derive_key = derive;
786 if(ctx->write_ctx != NULL) ctx->write_ctx->derive_key = derive;
790 * Set the passphrase for the cipher_ctx
792 * returns SQLITE_OK if assignment was successfull
793 * returns SQLITE_NOMEM if an error occured allocating memory
795 static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
796 /* free, zero existing pointers and size */
797 if(ctx->pass) sqlcipher_free(ctx->pass, ctx->pass_sz);
798 ctx->pass = NULL;
799 ctx->pass_sz = 0;
801 if(zKey && nKey) { /* if new password is provided, copy it */
802 ctx->pass_sz = nKey;
803 ctx->pass = sqlcipher_malloc(nKey);
804 if(ctx->pass == NULL) return SQLITE_NOMEM;
805 memcpy(ctx->pass, zKey, nKey);
807 return SQLITE_OK;
810 static int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) {
811 cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
812 int rc;
814 if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) {
815 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_set_pass", rc);
816 return rc;
819 c_ctx->derive_key = 1;
821 if(for_ctx == 2) {
822 if((rc = sqlcipher_cipher_ctx_copy(ctx, for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) {
823 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_copy", rc);
824 return rc;
828 return SQLITE_OK;
831 static int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter) {
832 ctx->kdf_iter = kdf_iter;
833 sqlcipher_set_derive_key(ctx, 1);
834 return SQLITE_OK;
837 static int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter) {
838 ctx->fast_kdf_iter = fast_kdf_iter;
839 sqlcipher_set_derive_key(ctx, 1);
840 return SQLITE_OK;
843 /* set the global default flag for HMAC */
844 static void sqlcipher_set_default_use_hmac(int use) {
845 if(use) SQLCIPHER_FLAG_SET(default_flags, CIPHER_FLAG_HMAC);
846 else SQLCIPHER_FLAG_UNSET(default_flags,CIPHER_FLAG_HMAC);
849 /* set the codec flag for whether this individual database should be using hmac */
850 static int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
851 if(use) {
852 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC);
853 } else {
854 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC);
857 return sqlcipher_codec_ctx_reserve_setup(ctx);
860 /* the length of plaintext header size must be:
861 * 1. greater than or equal to zero
862 * 2. a multiple of the cipher block size
863 * 3. less than the usable size of the first database page
865 static int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) {
866 if(size >= 0 && ctx->block_sz > 0 && (size % ctx->block_sz) == 0 && size < (ctx->page_sz - ctx->reserve_sz)) {
867 ctx->plaintext_header_sz = size;
868 return SQLITE_OK;
870 ctx->plaintext_header_sz = -1;
871 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_plaintext_header_size: attempt to set invalid plantext_header_size %d", size);
872 return SQLITE_ERROR;
875 static int sqlcipher_codec_ctx_set_hmac_algorithm(codec_ctx *ctx, int algorithm) {
876 ctx->hmac_algorithm = algorithm;
877 return sqlcipher_codec_ctx_reserve_setup(ctx);
880 static int sqlcipher_codec_ctx_set_kdf_algorithm(codec_ctx *ctx, int algorithm) {
881 ctx->kdf_algorithm = algorithm;
882 return SQLITE_OK;
885 static void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) {
886 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_error %d", error);
887 sqlite3pager_error(ctx->pBt->pBt->pPager, error);
888 ctx->pBt->pBt->db->errCode = error;
891 static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) {
892 sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
894 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
895 return SQLITE_OK; /* don't reload salt when not needed */
898 /* read salt from header, if present, otherwise generate a new random salt */
899 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: obtaining salt");
900 if(fd == NULL || fd->pMethods == 0 || sqlite3OsRead(fd, ctx->kdf_salt, ctx->kdf_salt_sz, 0) != SQLITE_OK) {
901 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: unable to read salt from file header, generating random");
902 if(ctx->provider->random(ctx->provider_ctx, ctx->kdf_salt, ctx->kdf_salt_sz) != SQLITE_OK) {
903 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init_kdf_salt: error retrieving random bytes from provider");
904 return SQLITE_ERROR;
907 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
908 return SQLITE_OK;
911 static int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int size) {
912 if(size >= ctx->kdf_salt_sz) {
913 memcpy(ctx->kdf_salt, salt, ctx->kdf_salt_sz);
914 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
915 return SQLITE_OK;
917 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size);
918 return SQLITE_ERROR;
921 static int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx, void** salt) {
922 int rc = SQLITE_OK;
923 if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
924 if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
925 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_get_kdf_salt: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
928 *salt = ctx->kdf_salt;
930 return rc;
933 static int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) {
934 if(!((size != 0) && ((size & (size - 1)) == 0)) || size < 512 || size > 65536) {
935 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "cipher_page_size not a power of 2 and between 512 and 65536 inclusive");
936 return SQLITE_ERROR;
938 /* attempt to free the existing page buffer */
939 if(ctx->buffer) sqlcipher_free(ctx->buffer,ctx->page_sz);
940 ctx->page_sz = size;
942 /* pre-allocate a page buffer of PageSize bytes. This will
943 be used as a persistent buffer for encryption and decryption
944 operations to avoid overhead of multiple memory allocations*/
945 ctx->buffer = sqlcipher_malloc(size);
946 if(ctx->buffer == NULL) return SQLITE_NOMEM;
948 return SQLITE_OK;
951 static int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const void *zKey, int nKey) {
952 int rc;
953 codec_ctx *ctx;
955 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating context");
957 *iCtx = sqlcipher_malloc(sizeof(codec_ctx));
958 ctx = *iCtx;
960 if(ctx == NULL) return SQLITE_NOMEM;
962 ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */
964 /* allocate space for salt data. Then read the first 16 bytes
965 directly off the database file. This is the salt for the
966 key derivation function. If we get a short read allocate
967 a new random salt value */
968 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating kdf_salt");
969 ctx->kdf_salt_sz = FILE_HEADER_SZ;
970 ctx->kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz);
971 if(ctx->kdf_salt == NULL) return SQLITE_NOMEM;
973 /* allocate space for separate hmac salt data. We want the
974 HMAC derivation salt to be different than the encryption
975 key derivation salt */
976 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating hmac_kdf_salt");
977 ctx->hmac_kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz);
978 if(ctx->hmac_kdf_salt == NULL) return SQLITE_NOMEM;
980 /* setup default flags */
981 ctx->flags = default_flags;
983 /* setup the crypto provider */
984 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "sqlcipher_codec_ctx_init: allocating provider");
985 ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider));
986 if(ctx->provider == NULL) return SQLITE_NOMEM;
988 /* make a copy of the provider to be used for the duration of the context */
989 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_codec_ctx_init: entering SQLCIPHER_MUTEX_PROVIDER");
990 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
991 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_codec_ctx_init: entered SQLCIPHER_MUTEX_PROVIDER");
993 memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider));
995 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_codec_ctx_init: leaving SQLCIPHER_MUTEX_PROVIDER");
996 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
997 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_codec_ctx_init: left SQLCIPHER_MUTEX_PROVIDER");
999 if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) {
1000 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from ctx_init", rc);
1001 return rc;
1004 ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx);
1005 ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx);
1006 ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx);
1008 /* establic the size for a hex-formated key specification, containing the
1009 raw encryption key and the salt used to generate it format. will be x'hexkey...hexsalt'
1010 so oversize by 3 bytes */
1011 ctx->keyspec_sz = ((ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3;
1014 Always overwrite page size and set to the default because the first page of the database
1015 in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in
1016 cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman
1018 if((rc = sqlcipher_codec_ctx_set_pagesize(ctx, default_page_size)) != SQLITE_OK) {
1019 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d returned from sqlcipher_codec_ctx_set_pagesize with %d", rc, default_page_size);
1020 return rc;
1023 /* establish settings for the KDF iterations and fast (HMAC) KDF iterations */
1024 if((rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, default_kdf_iter)) != SQLITE_OK) {
1025 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting default_kdf_iter %d", rc, default_kdf_iter);
1026 return rc;
1029 if((rc = sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, FAST_PBKDF2_ITER)) != SQLITE_OK) {
1030 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting fast_kdf_iter to %d", rc, FAST_PBKDF2_ITER);
1031 return rc;
1034 /* set the default HMAC and KDF algorithms which will determine the reserve size */
1035 if((rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, default_hmac_algorithm)) != SQLITE_OK) {
1036 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_hmac_algorithm with %d", rc, default_hmac_algorithm);
1037 return rc;
1040 /* Note that use_hmac is a special case that requires recalculation of page size
1041 so we call set_use_hmac to perform setup */
1042 if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC))) != SQLITE_OK) {
1043 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC));
1044 return rc;
1047 if((rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, default_kdf_algorithm)) != SQLITE_OK) {
1048 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_kdf_algorithm with %d", rc, default_kdf_algorithm);
1049 return rc;
1052 /* setup the default plaintext header size */
1053 if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, default_plaintext_header_size)) != SQLITE_OK) {
1054 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_plaintext_header_size with %d", rc, default_plaintext_header_size);
1055 return rc;
1058 /* initialize the read and write sub-contexts. this must happen after key_sz is established */
1059 if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->read_ctx)) != SQLITE_OK) {
1060 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing read_ctx", rc);
1061 return rc;
1064 if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->write_ctx)) != SQLITE_OK) {
1065 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d initializing write_ctx", rc);
1066 return rc;
1069 /* set the key material on one of the sub cipher contexts and sync them up */
1070 if((rc = sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, 0)) != SQLITE_OK) {
1071 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d setting pass key", rc);
1072 return rc;
1075 if((rc = sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx)) != SQLITE_OK) {
1076 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_init: error %d copying write_ctx to read_ctx", rc);
1077 return rc;
1080 return SQLITE_OK;
1084 * Free and wipe memory associated with a cipher_ctx, including the allocated
1085 * read_ctx and write_ctx.
1087 static void sqlcipher_codec_ctx_free(codec_ctx **iCtx) {
1088 codec_ctx *ctx = *iCtx;
1089 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_MEMORY, "codec_ctx_free: iCtx=%p", iCtx);
1090 if(ctx->kdf_salt) sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz);
1091 if(ctx->hmac_kdf_salt) sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz);
1092 if(ctx->buffer) sqlcipher_free(ctx->buffer, ctx->page_sz);
1094 if(ctx->provider) {
1095 ctx->provider->ctx_free(&ctx->provider_ctx);
1096 sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
1099 sqlcipher_cipher_ctx_free(ctx, &ctx->read_ctx);
1100 sqlcipher_cipher_ctx_free(ctx, &ctx->write_ctx);
1101 sqlcipher_free(ctx, sizeof(codec_ctx));
1104 /** convert a 32bit unsigned integer to little endian byte ordering */
1105 static void sqlcipher_put4byte_le(unsigned char *p, u32 v) {
1106 p[0] = (u8)v;
1107 p[1] = (u8)(v>>8);
1108 p[2] = (u8)(v>>16);
1109 p[3] = (u8)(v>>24);
1112 static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) {
1113 unsigned char pgno_raw[sizeof(pgno)];
1114 /* we may convert page number to consistent representation before calculating MAC for
1115 compatibility across big-endian and little-endian platforms.
1117 Note: The public release of sqlcipher 2.0.0 to 2.0.6 had a bug where the bytes of pgno
1118 were used directly in the MAC. SQLCipher convert's to little endian by default to preserve
1119 backwards compatibility on the most popular platforms, but can optionally be configured
1120 to use either big endian or native byte ordering via pragma. */
1122 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { /* compute hmac using little endian pgno*/
1123 sqlcipher_put4byte_le(pgno_raw, pgno);
1124 } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { /* compute hmac using big endian pgno */
1125 sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */
1126 } else { /* use native byte ordering */
1127 memcpy(pgno_raw, &pgno, sizeof(pgno));
1130 /* include the encrypted page data, initialization vector, and page number in HMAC. This will
1131 prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise
1132 valid pages out of order in a database */
1133 return ctx->provider->hmac(
1134 ctx->provider_ctx, ctx->hmac_algorithm, c_ctx->hmac_key,
1135 ctx->key_sz, in,
1136 in_sz, (unsigned char*) &pgno_raw,
1137 sizeof(pgno), out);
1141 * ctx - codec context
1142 * pgno - page number in database
1143 * size - size in bytes of input and output buffers
1144 * mode - 1 to encrypt, 0 to decrypt
1145 * in - pointer to input bytes
1146 * out - pouter to output bytes
1148 static int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) {
1149 cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
1150 unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start;
1151 int size;
1153 /* calculate some required positions into various buffers */
1154 size = page_sz - ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */
1155 iv_out = out + size;
1156 iv_in = in + size;
1158 /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain
1159 random bytes. note, these pointers are only valid when using hmac */
1160 hmac_in = in + size + ctx->iv_sz;
1161 hmac_out = out + size + ctx->iv_sz;
1162 out_start = out; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */
1164 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: pgno=%d, mode=%d, size=%d", pgno, mode, size);
1165 CODEC_HEXDUMP("sqlcipher_page_cipher: input page data", in, page_sz);
1167 /* the key size should never be zero. If it is, error out. */
1168 if(ctx->key_sz == 0) {
1169 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: error possible context corruption, key_sz is zero for pgno=%d", pgno);
1170 goto error;
1173 if(mode == CIPHER_ENCRYPT) {
1174 /* start at front of the reserve block, write random data to the end */
1175 if(ctx->provider->random(ctx->provider_ctx, iv_out, ctx->reserve_sz) != SQLITE_OK) goto error;
1176 } else { /* CIPHER_DECRYPT */
1177 memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */
1180 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) {
1181 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
1182 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: hmac operation on decrypt failed for pgno=%d", pgno);
1183 goto error;
1186 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: comparing hmac on in=%p out=%p hmac_sz=%d", hmac_in, hmac_out, ctx->hmac_sz);
1187 if(sqlcipher_memcmp(hmac_in, hmac_out, ctx->hmac_sz) != 0) { /* the hmac check failed */
1188 if(sqlite3BtreeGetAutoVacuum(ctx->pBt) != BTREE_AUTOVACUUM_NONE && sqlcipher_ismemset(in, 0, page_sz) == 0) {
1189 /* first check if the entire contents of the page is zeros. If so, this page
1190 resulted from a short read (i.e. sqlite attempted to pull a page after the end of the file. these
1191 short read failures must be ignored for autovaccum mode to work so wipe the output buffer
1192 and return SQLITE_OK to skip the decryption step. */
1193 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: zeroed page (short read) for pgno %d with autovacuum enabled", pgno);
1194 sqlcipher_memset(out, 0, page_sz);
1195 return SQLITE_OK;
1196 } else {
1197 /* if the page memory is not all zeros, it means the there was data and a hmac on the page.
1198 since the check failed, the page was either tampered with or corrupted. wipe the output buffer,
1199 and return SQLITE_ERROR to the caller */
1200 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: hmac check failed for pgno=%d", pgno);
1201 goto error;
1206 if(ctx->provider->cipher(ctx->provider_ctx, mode, c_ctx->key, ctx->key_sz, iv_out, in, size, out) != SQLITE_OK) {
1207 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: cipher operation mode=%d failed for pgno=%d", mode, pgno);
1208 goto error;
1211 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
1212 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
1213 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_page_cipher: hmac operation on encrypt failed for pgno=%d", pgno);
1214 goto error;
1218 CODEC_HEXDUMP("sqlcipher_page_cipher: output page data", out_start, page_sz);
1220 return SQLITE_OK;
1221 error:
1222 sqlcipher_memset(out, 0, page_sz);
1223 return SQLITE_ERROR;
1227 * Derive an encryption key for a cipher contex key based on the raw password.
1229 * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill
1230 * the key (i.e 64 hex chars for a 256 bit key) then the key data will be used directly.
1232 * Else, if the raw key data is formated as x'hex' and there are exactly enough hex chars to fill
1233 * the key and the salt (i.e 92 hex chars for a 256 bit key and 16 byte salt) then it will be unpacked
1234 * as the key followed by the salt.
1236 * Otherwise, a key data will be derived using PBKDF2
1238 * returns SQLITE_OK if initialization was successful
1239 * returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0)
1241 static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
1242 int rc;
1243 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: ctx->kdf_salt_sz=%d ctx->kdf_iter=%d ctx->fast_kdf_iter=%d ctx->key_sz=%d",
1244 ctx->kdf_salt_sz, ctx->kdf_iter, ctx->fast_kdf_iter, ctx->key_sz);
1246 if(c_ctx->pass && c_ctx->pass_sz) { /* if key material is present on the context for derivation */
1248 /* if necessary, initialize the salt from the header or random source */
1249 if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
1250 if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
1251 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
1252 return rc;
1256 if (c_ctx->pass_sz == ((ctx->key_sz * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 && cipher_isHex(c_ctx->pass + 2, ctx->key_sz * 2)) {
1257 int n = c_ctx->pass_sz - 3; /* adjust for leading x' and tailing ' */
1258 const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */
1259 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: using raw key from hex");
1260 cipher_hex2bin(z, n, c_ctx->key);
1261 } else if (c_ctx->pass_sz == (((ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0 && cipher_isHex(c_ctx->pass + 2, (ctx->key_sz + ctx->kdf_salt_sz) * 2)) {
1262 const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */
1263 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: using raw key from hex");
1264 cipher_hex2bin(z, (ctx->key_sz * 2), c_ctx->key);
1265 cipher_hex2bin(z + (ctx->key_sz * 2), (ctx->kdf_salt_sz * 2), ctx->kdf_salt);
1266 } else {
1267 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: deriving key using full PBKDF2 with %d iterations", ctx->kdf_iter);
1268 if(ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->pass, c_ctx->pass_sz,
1269 ctx->kdf_salt, ctx->kdf_salt_sz, ctx->kdf_iter,
1270 ctx->key_sz, c_ctx->key) != SQLITE_OK) {
1271 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: error occurred from provider kdf generating encryption key");
1272 return SQLITE_ERROR;
1276 /* set the context "keyspec" containing the hex-formatted key and salt to be used when attaching databases */
1277 if((rc = sqlcipher_cipher_ctx_set_keyspec(ctx, c_ctx, c_ctx->key)) != SQLITE_OK) {
1278 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: error %d from sqlcipher_cipher_ctx_set_keyspec", rc);
1279 return rc;
1282 /* if this context is setup to use hmac checks, generate a seperate and different
1283 key for HMAC. In this case, we use the output of the previous KDF as the input to
1284 this KDF run. This ensures a distinct but predictable HMAC key. */
1285 if(ctx->flags & CIPHER_FLAG_HMAC) {
1286 int i;
1288 /* start by copying the kdf key into the hmac salt slot
1289 then XOR it with the fixed hmac salt defined at compile time
1290 this ensures that the salt passed in to derive the hmac key, while
1291 easy to derive and publically known, is not the same as the salt used
1292 to generate the encryption key */
1293 memcpy(ctx->hmac_kdf_salt, ctx->kdf_salt, ctx->kdf_salt_sz);
1294 for(i = 0; i < ctx->kdf_salt_sz; i++) {
1295 ctx->hmac_kdf_salt[i] ^= hmac_salt_mask;
1298 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "cipher_ctx_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations",
1299 ctx->fast_kdf_iter);
1302 if(ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->key, ctx->key_sz,
1303 ctx->hmac_kdf_salt, ctx->kdf_salt_sz, ctx->fast_kdf_iter,
1304 ctx->key_sz, c_ctx->hmac_key) != SQLITE_OK) {
1305 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: error occurred from provider kdf generating HMAC key");
1306 return SQLITE_ERROR;
1310 c_ctx->derive_key = 0;
1311 return SQLITE_OK;
1313 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_cipher_ctx_key_derive: key material is not present on the context for key derivation");
1314 return SQLITE_ERROR;
1317 static int sqlcipher_codec_key_derive(codec_ctx *ctx) {
1318 /* derive key on first use if necessary */
1319 if(ctx->read_ctx->derive_key) {
1320 if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->read_ctx) != SQLITE_OK) {
1321 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving read_ctx key");
1322 return SQLITE_ERROR;
1326 if(ctx->write_ctx->derive_key) {
1327 if(sqlcipher_cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) {
1328 /* the relevant parameters are the same, just copy read key */
1329 if(sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx) != SQLITE_OK) {
1330 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred copying read_ctx to write_ctx");
1331 return SQLITE_ERROR;
1333 } else {
1334 if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->write_ctx) != SQLITE_OK) {
1335 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_key_derive: error occurred deriving write_ctx key");
1336 return SQLITE_ERROR;
1341 /* wipe and free passphrase after key derivation */
1342 if(ctx->store_pass != 1) {
1343 sqlcipher_cipher_ctx_set_pass(ctx->read_ctx, NULL, 0);
1344 sqlcipher_cipher_ctx_set_pass(ctx->write_ctx, NULL, 0);
1347 return SQLITE_OK;
1350 static int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) {
1351 if(source == CIPHER_READ_CTX) {
1352 return sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx);
1353 } else {
1354 return sqlcipher_cipher_ctx_copy(ctx, ctx->read_ctx, ctx->write_ctx);
1358 static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version, char** journal_mode) {
1359 int rc;
1360 sqlite3 *db = NULL;
1361 sqlite3_stmt *statement = NULL;
1362 char *query_journal_mode = "PRAGMA journal_mode;";
1363 char *query_user_version = "PRAGMA user_version;";
1365 rc = sqlite3_open(filename, &db);
1366 if(rc != SQLITE_OK) goto cleanup;
1368 rc = sqlite3_key(db, key, key_sz);
1369 if(rc != SQLITE_OK) goto cleanup;
1371 rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
1372 if(rc != SQLITE_OK) goto cleanup;
1374 /* start by querying the user version.
1375 this will fail if the key is incorrect */
1376 rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL);
1377 if(rc != SQLITE_OK) goto cleanup;
1379 rc = sqlite3_step(statement);
1380 if(rc == SQLITE_ROW) {
1381 *user_version = sqlite3_column_int(statement, 0);
1382 } else {
1383 goto cleanup;
1385 sqlite3_finalize(statement);
1387 rc = sqlite3_prepare(db, query_journal_mode, -1, &statement, NULL);
1388 if(rc != SQLITE_OK) goto cleanup;
1390 rc = sqlite3_step(statement);
1391 if(rc == SQLITE_ROW) {
1392 *journal_mode = sqlite3_mprintf("%s", sqlite3_column_text(statement, 0));
1393 } else {
1394 goto cleanup;
1396 rc = SQLITE_OK;
1397 /* cleanup will finalize open statement */
1399 cleanup:
1400 if(statement) sqlite3_finalize(statement);
1401 if(db) sqlite3_close(db);
1402 return rc;
1405 static int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *column) {
1406 Pgno page = 1;
1407 int rc = 0;
1408 char *result;
1409 unsigned char *hmac_out = NULL;
1410 sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
1411 i64 file_sz;
1413 Vdbe *v = sqlite3GetVdbe(pParse);
1414 sqlite3VdbeSetNumCols(v, 1);
1415 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, column, SQLITE_STATIC);
1417 if(fd == NULL || fd->pMethods == 0) {
1418 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "database file is undefined", P4_TRANSIENT);
1419 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1420 goto cleanup;
1423 if(!(ctx->flags & CIPHER_FLAG_HMAC)) {
1424 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "HMAC is not enabled, unable to integrity check", P4_TRANSIENT);
1425 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1426 goto cleanup;
1429 if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) {
1430 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "unable to derive keys", P4_TRANSIENT);
1431 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1432 goto cleanup;
1435 sqlite3OsFileSize(fd, &file_sz);
1436 hmac_out = sqlcipher_malloc(ctx->hmac_sz);
1438 for(page = 1; page <= file_sz / ctx->page_sz; page++) {
1439 i64 offset = (page - 1) * ctx->page_sz;
1440 int payload_sz = ctx->page_sz - ctx->reserve_sz + ctx->iv_sz;
1441 int read_sz = ctx->page_sz;
1443 /* skip integrity check on PAGER_SJ_PGNO since it will have no valid content */
1444 if(sqlite3pager_is_sj_pgno(ctx->pBt->pBt->pPager, page)) continue;
1446 if(page==1) {
1447 int page1_offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ;
1448 read_sz = read_sz - page1_offset;
1449 payload_sz = payload_sz - page1_offset;
1450 offset += page1_offset;
1453 sqlcipher_memset(ctx->buffer, 0, ctx->page_sz);
1454 sqlcipher_memset(hmac_out, 0, ctx->hmac_sz);
1455 if(sqlite3OsRead(fd, ctx->buffer, read_sz, offset) != SQLITE_OK) {
1456 result = sqlite3_mprintf("error reading %d bytes from file page %d at offset %d", read_sz, page, offset);
1457 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1458 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1459 } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out) != SQLITE_OK) {
1460 result = sqlite3_mprintf("HMAC operation failed for page %d", page);
1461 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1462 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1463 } else if(sqlcipher_memcmp(ctx->buffer + payload_sz, hmac_out, ctx->hmac_sz) != 0) {
1464 result = sqlite3_mprintf("HMAC verification failed for page %d", page);
1465 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1466 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1470 if(file_sz % ctx->page_sz != 0) {
1471 result = sqlite3_mprintf("page %d has an invalid size of %lld bytes (expected %d bytes)", page, file_sz - ((file_sz / ctx->page_sz) * ctx->page_sz), ctx->page_sz);
1472 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1473 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1476 cleanup:
1477 if(hmac_out != NULL) sqlcipher_free(hmac_out, ctx->hmac_sz);
1478 return SQLITE_OK;
1481 static int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
1482 int i, pass_sz, keyspec_sz, nRes, user_version, rc, oflags;
1483 Db *pDb = 0;
1484 sqlite3 *db = ctx->pBt->db;
1485 const char *db_filename = sqlite3_db_filename(db, "main");
1486 char *set_user_version = NULL, *pass = NULL, *attach_command = NULL, *migrated_db_filename = NULL, *keyspec = NULL, *temp = NULL, *journal_mode = NULL, *set_journal_mode = NULL, *pragma_compat = NULL;
1487 Btree *pDest = NULL, *pSrc = NULL;
1488 sqlite3_file *srcfile, *destfile;
1489 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1490 LPWSTR w_db_filename = NULL, w_migrated_db_filename = NULL;
1491 int w_db_filename_sz = 0, w_migrated_db_filename_sz = 0;
1492 #endif
1493 pass_sz = keyspec_sz = rc = user_version = 0;
1495 if(!db_filename || sqlite3Strlen30(db_filename) < 1)
1496 goto cleanup; /* exit immediately if this is an in memory database */
1498 /* pull the provided password / key material off the current codec context */
1499 pass_sz = ctx->read_ctx->pass_sz;
1500 pass = sqlcipher_malloc(pass_sz+1);
1501 memset(pass, 0, pass_sz+1);
1502 memcpy(pass, ctx->read_ctx->pass, pass_sz);
1504 /* Version 4 - current, no upgrade required, so exit immediately */
1505 rc = sqlcipher_check_connection(db_filename, pass, pass_sz, "", &user_version, &journal_mode);
1506 if(rc == SQLITE_OK){
1507 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: no upgrade required - exiting");
1508 goto cleanup;
1511 for(i = 3; i > 0; i--) {
1512 pragma_compat = sqlite3_mprintf("PRAGMA cipher_compatibility = %d;", i);
1513 rc = sqlcipher_check_connection(db_filename, pass, pass_sz, pragma_compat, &user_version, &journal_mode);
1514 if(rc == SQLITE_OK) {
1515 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: version %d format found", i);
1516 goto migrate;
1518 if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat));
1519 pragma_compat = NULL;
1522 /* if we exit the loop normally we failed to determine the version, this is an error */
1523 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: unable to determine format version for upgrade: this may indicate custom settings were used ");
1524 goto handle_error;
1526 migrate:
1528 temp = sqlite3_mprintf("%s-migrated", db_filename);
1529 /* overallocate migrated_db_filename, because sqlite3OsOpen will read past the null terminator
1530 * to determine whether the filename was URI formatted */
1531 migrated_db_filename = sqlcipher_malloc(sqlite3Strlen30(temp)+2);
1532 memcpy(migrated_db_filename, temp, sqlite3Strlen30(temp));
1533 sqlcipher_free(temp, sqlite3Strlen30(temp));
1535 attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate;", migrated_db_filename, pass);
1536 set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version);
1538 rc = sqlite3_exec(db, pragma_compat, NULL, NULL, NULL);
1539 if(rc != SQLITE_OK){
1540 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set compatibility mode failed, error code %d", rc);
1541 goto handle_error;
1544 /* force journal mode to DELETE, we will set it back later if different */
1545 rc = sqlite3_exec(db, "PRAGMA journal_mode = delete;", NULL, NULL, NULL);
1546 if(rc != SQLITE_OK){
1547 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: force journal mode DELETE failed, error code %d", rc);
1548 goto handle_error;
1551 rc = sqlite3_exec(db, attach_command, NULL, NULL, NULL);
1552 if(rc != SQLITE_OK){
1553 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: attach failed, error code %d", rc);
1554 goto handle_error;
1557 rc = sqlite3_key_v2(db, "migrate", pass, pass_sz);
1558 if(rc != SQLITE_OK){
1559 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: keying attached database failed, error code %d", rc);
1560 goto handle_error;
1563 rc = sqlite3_exec(db, "SELECT sqlcipher_export('migrate');", NULL, NULL, NULL);
1564 if(rc != SQLITE_OK){
1565 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: sqlcipher_export failed, error code %d", rc);
1566 goto handle_error;
1569 #ifdef SQLCIPHER_TEST
1570 if((cipher_test_flags & TEST_FAIL_MIGRATE) > 0) {
1571 rc = SQLITE_ERROR;
1572 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: simulated migrate failure, error code %d", rc);
1573 goto handle_error;
1575 #endif
1577 rc = sqlite3_exec(db, set_user_version, NULL, NULL, NULL);
1578 if(rc != SQLITE_OK){
1579 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: set user version failed, error code %d", rc);
1580 goto handle_error;
1583 if( !db->autoCommit ){
1584 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate from within a transaction");
1585 goto handle_error;
1587 if( db->nVdbeActive>1 ){
1588 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: cannot migrate - SQL statements in progress");
1589 goto handle_error;
1592 pDest = db->aDb[0].pBt;
1593 pDb = &(db->aDb[db->nDb-1]);
1594 pSrc = pDb->pBt;
1596 nRes = sqlite3BtreeGetRequestedReserve(pSrc);
1597 /* unset the BTS_PAGESIZE_FIXED flag to avoid SQLITE_READONLY */
1598 pDest->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
1599 rc = sqlite3BtreeSetPageSize(pDest, default_page_size, nRes, 0);
1600 if(rc != SQLITE_OK) {
1601 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to set btree page size to %d res %d rc %d", default_page_size, nRes, rc);
1602 goto handle_error;
1605 sqlcipherCodecGetKey(db, db->nDb - 1, (void**)&keyspec, &keyspec_sz);
1606 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_KEY_USED);
1607 sqlcipherCodecAttach(db, 0, keyspec, keyspec_sz);
1609 srcfile = sqlite3PagerFile(pSrc->pBt->pPager);
1610 destfile = sqlite3PagerFile(pDest->pBt->pPager);
1612 sqlite3OsClose(srcfile);
1613 sqlite3OsClose(destfile);
1615 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1616 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing windows MoveFileExA");
1618 w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, NULL, 0);
1619 w_db_filename = sqlcipher_malloc(w_db_filename_sz * sizeof(wchar_t));
1620 w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, (const LPWSTR) w_db_filename, w_db_filename_sz);
1622 w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, NULL, 0);
1623 w_migrated_db_filename = sqlcipher_malloc(w_migrated_db_filename_sz * sizeof(wchar_t));
1624 w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, (const LPWSTR) w_migrated_db_filename, w_migrated_db_filename_sz);
1626 if(!MoveFileExW(w_migrated_db_filename, w_db_filename, MOVEFILE_REPLACE_EXISTING)) {
1627 rc = SQLITE_ERROR;
1628 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc);
1629 goto handle_error;
1631 #else
1632 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: performing POSIX rename");
1633 if ((rc = rename(migrated_db_filename, db_filename)) != 0) {
1634 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %s to %s: %d", migrated_db_filename, db_filename, rc);
1635 goto handle_error;
1637 #endif
1638 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: renamed migration database %s to main database %s: %d", migrated_db_filename, db_filename, rc);
1640 rc = sqlite3OsOpen(db->pVfs, migrated_db_filename, srcfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags);
1641 if(rc != SQLITE_OK) {
1642 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen migration database %s: %d", migrated_db_filename, rc);
1643 goto handle_error;
1646 rc = sqlite3OsOpen(db->pVfs, db_filename, destfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags);
1647 if(rc != SQLITE_OK) {
1648 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to reopen main database %s: %d", db_filename, rc);
1649 goto handle_error;
1652 sqlite3pager_reset(pDest->pBt->pPager);
1653 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset pager");
1655 rc = sqlite3_exec(db, "DETACH DATABASE migrate;", NULL, NULL, NULL);
1656 if(rc != SQLITE_OK) {
1657 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: DETACH DATABASE migrate failed: %d", rc);
1660 sqlite3ResetAllSchemasOfConnection(db);
1661 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: reset all schemas");
1663 set_journal_mode = sqlite3_mprintf("PRAGMA journal_mode = %s;", journal_mode);
1664 rc = sqlite3_exec(db, set_journal_mode, NULL, NULL, NULL);
1665 if(rc != SQLITE_OK) {
1666 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to re-set journal mode via %s: %d", set_journal_mode, rc);
1667 goto handle_error;
1670 goto cleanup;
1672 handle_error:
1673 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: an error occurred attempting to migrate the database - last error %d", rc);
1675 cleanup:
1676 if(migrated_db_filename) {
1677 int del_rc = sqlite3OsDelete(db->pVfs, migrated_db_filename, 0);
1678 if(del_rc != SQLITE_OK) {
1679 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_ctx_migrate: failed to delete migration database %s: %d", migrated_db_filename, del_rc);
1683 if(pass) sqlcipher_free(pass, pass_sz);
1684 if(attach_command) sqlcipher_free(attach_command, sqlite3Strlen30(attach_command));
1685 if(migrated_db_filename) sqlcipher_free(migrated_db_filename, sqlite3Strlen30(migrated_db_filename));
1686 if(set_user_version) sqlcipher_free(set_user_version, sqlite3Strlen30(set_user_version));
1687 if(set_journal_mode) sqlcipher_free(set_journal_mode, sqlite3Strlen30(set_journal_mode));
1688 if(journal_mode) sqlcipher_free(journal_mode, sqlite3Strlen30(journal_mode));
1689 if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat));
1690 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1691 if(w_db_filename) sqlcipher_free(w_db_filename, w_db_filename_sz);
1692 if(w_migrated_db_filename) sqlcipher_free(w_migrated_db_filename, w_migrated_db_filename_sz);
1693 #endif
1694 return rc;
1697 static int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz){
1698 const char *suffix = &zRight[random_sz-1];
1699 int n = random_sz - 3; /* adjust for leading x' and tailing ' */
1700 if (n > 0 &&
1701 sqlite3StrNICmp((const char *)zRight ,"x'", 2) == 0 &&
1702 sqlite3StrNICmp(suffix, "'", 1) == 0 &&
1703 n % 2 == 0) {
1704 int rc = 0;
1705 int buffer_sz = n / 2;
1706 unsigned char *random;
1707 const unsigned char *z = (const unsigned char *)zRight + 2; /* adjust lead offset of x' */
1708 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: using raw random blob from hex");
1709 random = sqlcipher_malloc(buffer_sz);
1710 memset(random, 0, buffer_sz);
1711 cipher_hex2bin(z, n, random);
1712 rc = ctx->provider->add_random(ctx->provider_ctx, random, buffer_sz);
1713 sqlcipher_free(random, buffer_sz);
1714 return rc;
1716 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_add_random: attemt to add random with invalid format");
1717 return SQLITE_ERROR;
1720 #if !defined(SQLITE_OMIT_TRACE)
1722 #define SQLCIPHER_PROFILE_FMT "Elapsed time:%.3f ms - %s\n"
1723 #define SQLCIPHER_PROFILE_FMT_OSLOG "Elapsed time:%{public}.3f ms - %{public}s\n"
1725 static int sqlcipher_profile_callback(unsigned int trace, void *file, void *stmt, void *run_time){
1726 FILE *f = (FILE*) file;
1727 double elapsed = (*((sqlite3_uint64*)run_time))/1000000.0;
1728 if(f == NULL) {
1729 #if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
1730 #if defined(__ANDROID__)
1731 __android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1732 #elif defined(__APPLE__)
1733 os_log(OS_LOG_DEFAULT, SQLCIPHER_PROFILE_FMT_OSLOG, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1734 #endif
1735 #endif
1736 } else {
1737 fprintf(f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1739 return SQLITE_OK;
1741 #endif
1743 static int sqlcipher_cipher_profile(sqlite3 *db, const char *destination){
1744 #if defined(SQLITE_OMIT_TRACE)
1745 return SQLITE_ERROR;
1746 #else
1747 FILE *f = NULL;
1748 if(sqlite3_stricmp(destination, "off") == 0){
1749 sqlite3_trace_v2(db, 0, NULL, NULL); /* disable tracing */
1750 } else {
1751 if(sqlite3_stricmp(destination, "stdout") == 0){
1752 f = stdout;
1753 }else if(sqlite3_stricmp(destination, "stderr") == 0){
1754 f = stderr;
1755 }else if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){
1756 f = NULL; /* file pointer will be NULL indicating the device target (i.e. logcat or oslog). We will accept logcat for backwards compatibility */
1757 }else{
1758 #if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT))
1759 if(fopen_s(&f, destination, "a") != 0) return SQLITE_ERROR;
1760 #else
1761 if((f = fopen(destination, "a")) == 0) return SQLITE_ERROR;
1762 #endif
1764 sqlite3_trace_v2(db, SQLITE_TRACE_PROFILE, sqlcipher_profile_callback, f);
1766 return SQLITE_OK;
1767 #endif
1770 static char *sqlcipher_get_log_level_str(unsigned int level) {
1771 switch(level) {
1772 case SQLCIPHER_LOG_ERROR:
1773 return "ERROR";
1774 case SQLCIPHER_LOG_WARN:
1775 return "WARN";
1776 case SQLCIPHER_LOG_INFO:
1777 return "INFO";
1778 case SQLCIPHER_LOG_DEBUG:
1779 return "DEBUG";
1780 case SQLCIPHER_LOG_TRACE:
1781 return "TRACE";
1782 case SQLCIPHER_LOG_ALL:
1783 return "ALL";
1785 return "NONE";
1788 static char *sqlcipher_get_log_source_str(unsigned int source) {
1789 switch(source) {
1790 case SQLCIPHER_LOG_NONE:
1791 return "NONE";
1792 case SQLCIPHER_LOG_CORE:
1793 return "CORE";
1794 case SQLCIPHER_LOG_MEMORY:
1795 return "MEMORY";
1796 case SQLCIPHER_LOG_MUTEX:
1797 return "MUTEX";
1798 case SQLCIPHER_LOG_PROVIDER:
1799 return "PROVIDER";
1801 return "ALL";
1805 #ifndef SQLCIPHER_OMIT_LOG
1806 /* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */
1807 #define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
1808 #define HECTONANOSEC_PER_SEC 10000000ull
1809 #define MAX_LOG_LEN 8192
1810 void sqlcipher_log(unsigned int level, unsigned int source, const char *message, ...) {
1811 va_list params;
1812 va_start(params, message);
1813 char formatted[MAX_LOG_LEN];
1814 char *out = NULL;
1815 int len = 0;
1817 #ifdef CODEC_DEBUG
1818 #if defined(SQLCIPHER_OMIT_LOG_DEVICE)
1819 vfprintf(stderr, message, params);
1820 fprintf(stderr, "\n");
1821 goto end;
1822 #else
1823 #if defined(__ANDROID__)
1824 __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params);
1825 goto end;
1826 #elif defined(__APPLE__)
1827 formatted = sqlite3_vmprintf(message, params);
1828 os_log(OS_LOG_DEFAULT, "%s", formatted);
1829 sqlite3_free(formatted);
1830 goto end;
1831 #else
1832 vfprintf(stderr, message, params);
1833 fprintf(stderr, "\n");
1834 goto end;
1835 #endif
1836 #endif
1837 #endif
1839 level > sqlcipher_log_level /* log level is higher, e.g. level filter is at ERROR but this message is DEBUG */
1840 || (sqlcipher_log_source & source) == 0 /* source filter doesn't match this message source */
1841 || (sqlcipher_log_device == 0 && sqlcipher_log_file == NULL) /* no configured log target */
1843 /* skip logging this message */
1844 goto end;
1847 sqlite3_snprintf(MAX_LOG_LEN, formatted, "%s %s ", sqlcipher_get_log_level_str(level), sqlcipher_get_log_source_str(source));
1848 len = strlen(formatted);
1849 sqlite3_vsnprintf(MAX_LOG_LEN - len, formatted + len, message, params);
1851 #if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
1852 if(sqlcipher_log_device) {
1853 #if defined(__ANDROID__)
1854 __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", formatted);
1855 goto end;
1856 #elif defined(__APPLEformattes__)
1857 os_log(OS_LOG_DEFAULT, "%{public}s", formatted);
1858 goto end;
1859 #endif
1861 #endif
1863 if(sqlcipher_log_file != NULL){
1864 char buffer[24];
1865 struct tm tt;
1866 int ms;
1867 time_t sec;
1868 #ifdef _WIN32
1869 SYSTEMTIME st;
1870 FILETIME ft;
1871 GetSystemTime(&st);
1872 SystemTimeToFileTime(&st, &ft);
1873 sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC);
1874 ms = st.wMilliseconds;
1875 localtime_s(&tt, &sec);
1876 #else
1877 struct timeval tv;
1878 gettimeofday(&tv, NULL);
1879 sec = tv.tv_sec;
1880 ms = tv.tv_usec/1000.0;
1881 localtime_r(&sec, &tt);
1882 #endif
1883 if(strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt)) {
1884 fprintf((FILE*)sqlcipher_log_file, "%s.%03d: %s\n", buffer, ms, formatted);
1885 goto end;
1889 end:
1890 va_end(params);
1892 #endif
1894 static int sqlcipher_set_log(const char *destination){
1895 #ifdef SQLCIPHER_OMIT_LOG
1896 return SQLITE_ERROR;
1897 #else
1898 /* close open trace file if it is not stdout or stderr, then
1899 reset trace settings */
1900 if(sqlcipher_log_file != NULL && sqlcipher_log_file != stdout && sqlcipher_log_file != stderr) {
1901 fclose((FILE*)sqlcipher_log_file);
1903 sqlcipher_log_file = NULL;
1904 sqlcipher_log_device = 0;
1906 if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){
1907 /* use the appropriate device log. accept logcat for backwards compatibility */
1908 sqlcipher_log_device = 1;
1909 } else if(sqlite3_stricmp(destination, "stdout") == 0){
1910 sqlcipher_log_file = stdout;
1911 }else if(sqlite3_stricmp(destination, "stderr") == 0){
1912 sqlcipher_log_file = stderr;
1913 }else if(sqlite3_stricmp(destination, "off") != 0){
1914 #if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT))
1915 if(fopen_s(&sqlcipher_log_file, destination, "a") != 0) return SQLITE_ERROR;
1916 #else
1917 if((sqlcipher_log_file = fopen(destination, "a")) == 0) return SQLITE_ERROR;
1918 #endif
1920 sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_CORE, "sqlcipher_set_log: set log to %s", destination);
1921 return SQLITE_OK;
1922 #endif
1925 #ifdef SQLCIPHER_EXT
1926 #include "sqlcipher_ext.h"
1927 #endif
1929 static void sqlcipher_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){
1930 Vdbe *v = sqlite3GetVdbe(pParse);
1931 sqlite3VdbeSetNumCols(v, 1);
1932 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, zLabel, SQLITE_STATIC);
1933 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, value, value_type);
1934 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1937 static int codec_set_btree_to_codec_pagesize(sqlite3 *db, Db *pDb, codec_ctx *ctx) {
1938 int rc;
1940 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize() size=%d reserve=%d", ctx->page_sz, ctx->reserve_sz);
1942 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entering database mutex %p", db->mutex);
1943 sqlite3_mutex_enter(db->mutex);
1944 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: entered database mutex %p", db->mutex);
1945 db->nextPagesize = ctx->page_sz;
1947 /* before forcing the page size we need to unset the BTS_PAGESIZE_FIXED flag, else
1948 sqliteBtreeSetPageSize will block the change */
1949 pDb->pBt->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
1950 rc = sqlite3BtreeSetPageSize(pDb->pBt, ctx->page_sz, ctx->reserve_sz, 0);
1952 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_btree_to_codec_pagesize: sqlite3BtreeSetPageSize returned %d", rc);
1954 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: leaving database mutex %p", db->mutex);
1955 sqlite3_mutex_leave(db->mutex);
1956 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "codec_set_btree_to_codec_pagesize: left database mutex %p", db->mutex);
1958 return rc;
1961 static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey, int for_ctx) {
1962 struct Db *pDb = &db->aDb[nDb];
1963 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "codec_set_pass_key: db=%p nDb=%d for_ctx=%d", db, nDb, for_ctx);
1964 if(pDb->pBt) {
1965 codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
1967 if(ctx) {
1968 return sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, for_ctx);
1969 } else {
1970 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: error ocurred fetching codec from pager on db %d", nDb);
1971 return SQLITE_ERROR;
1974 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "codec_set_pass_key: no btree present on db %d", nDb);
1975 return SQLITE_ERROR;
1978 int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) {
1979 struct Db *pDb = &db->aDb[iDb];
1980 codec_ctx *ctx = NULL;
1981 int rc;
1983 if(pDb->pBt) {
1984 ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
1987 if(sqlite3_stricmp(zLeft, "key") !=0 && sqlite3_stricmp(zLeft, "rekey") != 0) {
1988 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p", db, iDb, pParse, zLeft, zRight, ctx);
1991 #ifdef SQLCIPHER_EXT
1992 if(sqlcipher_ext_pragma(db, iDb, pParse, zLeft, zRight)) {
1993 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: PRAGMA handled by sqlcipher_ext_pragma");
1994 } else
1995 #endif
1996 #ifdef SQLCIPHER_TEST
1997 if( sqlite3_stricmp(zLeft,"cipher_test_on")==0 ){
1998 if( zRight ) {
1999 if(sqlite3_stricmp(zRight, "fail_encrypt")==0) {
2000 SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_ENCRYPT);
2001 } else
2002 if(sqlite3_stricmp(zRight, "fail_decrypt")==0) {
2003 SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_DECRYPT);
2004 } else
2005 if(sqlite3_stricmp(zRight, "fail_migrate")==0) {
2006 SQLCIPHER_FLAG_SET(cipher_test_flags,TEST_FAIL_MIGRATE);
2009 } else
2010 if( sqlite3_stricmp(zLeft,"cipher_test_off")==0 ){
2011 if( zRight ) {
2012 if(sqlite3_stricmp(zRight, "fail_encrypt")==0) {
2013 SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_ENCRYPT);
2014 } else
2015 if(sqlite3_stricmp(zRight, "fail_decrypt")==0) {
2016 SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_DECRYPT);
2017 } else
2018 if(sqlite3_stricmp(zRight, "fail_migrate")==0) {
2019 SQLCIPHER_FLAG_UNSET(cipher_test_flags,TEST_FAIL_MIGRATE);
2022 } else
2023 if( sqlite3_stricmp(zLeft,"cipher_test")==0 ){
2024 char *flags = sqlite3_mprintf("%u", cipher_test_flags);
2025 sqlcipher_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC);
2026 }else
2027 if( sqlite3_stricmp(zLeft,"cipher_test_rand")==0 ){
2028 if( zRight ) {
2029 int rand = atoi(zRight);
2030 cipher_test_rand = rand;
2031 } else {
2032 char *rand = sqlite3_mprintf("%d", cipher_test_rand);
2033 sqlcipher_vdbe_return_string(pParse, "cipher_test_rand", rand, P4_DYNAMIC);
2035 } else
2036 #endif
2037 if( sqlite3_stricmp(zLeft, "cipher_fips_status")== 0 && !zRight ){
2038 if(ctx) {
2039 char *fips_mode_status = sqlite3_mprintf("%d", ctx->provider->fips_status(ctx->provider_ctx));
2040 sqlcipher_vdbe_return_string(pParse, "cipher_fips_status", fips_mode_status, P4_DYNAMIC);
2042 } else
2043 if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && zRight ) {
2044 if(ctx) {
2045 char *deprecation = "PRAGMA cipher_store_pass is deprecated, please remove from use";
2046 ctx->store_pass = sqlite3GetBoolean(zRight, 1);
2047 sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", deprecation, P4_TRANSIENT);
2048 sqlite3_log(SQLITE_WARNING, deprecation);
2050 } else
2051 if( sqlite3_stricmp(zLeft, "cipher_store_pass")==0 && !zRight ) {
2052 if(ctx){
2053 char *store_pass_value = sqlite3_mprintf("%d", ctx->store_pass);
2054 sqlcipher_vdbe_return_string(pParse, "cipher_store_pass", store_pass_value, P4_DYNAMIC);
2057 if( sqlite3_stricmp(zLeft, "cipher_profile")== 0 && zRight ){
2058 char *profile_status = sqlite3_mprintf("%d", sqlcipher_cipher_profile(db, zRight));
2059 sqlcipher_vdbe_return_string(pParse, "cipher_profile", profile_status, P4_DYNAMIC);
2060 } else
2061 if( sqlite3_stricmp(zLeft, "cipher_add_random")==0 && zRight ){
2062 if(ctx) {
2063 char *add_random_status = sqlite3_mprintf("%d", sqlcipher_codec_add_random(ctx, zRight, sqlite3Strlen30(zRight)));
2064 sqlcipher_vdbe_return_string(pParse, "cipher_add_random", add_random_status, P4_DYNAMIC);
2066 } else
2067 if( sqlite3_stricmp(zLeft, "cipher_migrate")==0 && !zRight ){
2068 if(ctx){
2069 int status = sqlcipher_codec_ctx_migrate(ctx);
2070 char *migrate_status = sqlite3_mprintf("%d", status);
2071 sqlcipher_vdbe_return_string(pParse, "cipher_migrate", migrate_status, P4_DYNAMIC);
2072 if(status != SQLITE_OK) {
2073 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipher_codec_pragma: error occurred during cipher_migrate: %d", status);
2074 sqlcipher_codec_ctx_set_error(ctx, status);
2077 } else
2078 if( sqlite3_stricmp(zLeft, "cipher_provider")==0 && !zRight ){
2079 if(ctx) {
2080 sqlcipher_vdbe_return_string(pParse, "cipher_provider",
2081 ctx->provider->get_provider_name(ctx->provider_ctx), P4_TRANSIENT);
2083 } else
2084 if( sqlite3_stricmp(zLeft, "cipher_provider_version")==0 && !zRight){
2085 if(ctx) {
2086 sqlcipher_vdbe_return_string(pParse, "cipher_provider_version",
2087 ctx->provider->get_provider_version(ctx->provider_ctx), P4_TRANSIENT);
2089 } else
2090 if( sqlite3_stricmp(zLeft, "cipher_version")==0 && !zRight ){
2091 sqlcipher_vdbe_return_string(pParse, "cipher_version", sqlcipher_version(), P4_DYNAMIC);
2092 }else
2093 if( sqlite3_stricmp(zLeft, "cipher")==0 ){
2094 if(ctx) {
2095 if( zRight ) {
2096 const char* message = "PRAGMA cipher is no longer supported.";
2097 sqlcipher_vdbe_return_string(pParse, "cipher", message, P4_TRANSIENT);
2098 sqlite3_log(SQLITE_WARNING, message);
2099 }else {
2100 sqlcipher_vdbe_return_string(pParse, "cipher",
2101 ctx->provider->get_cipher(ctx->provider_ctx), P4_TRANSIENT);
2104 }else
2105 if( sqlite3_stricmp(zLeft, "rekey_cipher")==0 && zRight ){
2106 const char* message = "PRAGMA rekey_cipher is no longer supported.";
2107 sqlcipher_vdbe_return_string(pParse, "rekey_cipher", message, P4_TRANSIENT);
2108 sqlite3_log(SQLITE_WARNING, message);
2109 }else
2110 if( sqlite3_stricmp(zLeft,"cipher_default_kdf_iter")==0 ){
2111 if( zRight ) {
2112 default_kdf_iter = atoi(zRight); /* change default KDF iterations */
2113 } else {
2114 char *kdf_iter = sqlite3_mprintf("%d", default_kdf_iter);
2115 sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_iter", kdf_iter, P4_DYNAMIC);
2117 }else
2118 if( sqlite3_stricmp(zLeft, "kdf_iter")==0 ){
2119 if(ctx) {
2120 if( zRight ) {
2121 sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */
2122 } else {
2123 char *kdf_iter = sqlite3_mprintf("%d", ctx->kdf_iter);
2124 sqlcipher_vdbe_return_string(pParse, "kdf_iter", kdf_iter, P4_DYNAMIC);
2127 }else
2128 if( sqlite3_stricmp(zLeft, "fast_kdf_iter")==0){
2129 if(ctx) {
2130 if( zRight ) {
2131 char *deprecation = "PRAGMA fast_kdf_iter is deprecated, please remove from use";
2132 sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, atoi(zRight)); /* change of RW PBKDF2 iteration */
2133 sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", deprecation, P4_TRANSIENT);
2134 sqlite3_log(SQLITE_WARNING, deprecation);
2135 } else {
2136 char *fast_kdf_iter = sqlite3_mprintf("%d", ctx->fast_kdf_iter);
2137 sqlcipher_vdbe_return_string(pParse, "fast_kdf_iter", fast_kdf_iter, P4_DYNAMIC);
2140 }else
2141 if( sqlite3_stricmp(zLeft, "rekey_kdf_iter")==0 && zRight ){
2142 const char* message = "PRAGMA rekey_kdf_iter is no longer supported.";
2143 sqlcipher_vdbe_return_string(pParse, "rekey_kdf_iter", message, P4_TRANSIENT);
2144 sqlite3_log(SQLITE_WARNING, message);
2145 }else
2146 if( sqlite3_stricmp(zLeft,"page_size")==0 || sqlite3_stricmp(zLeft,"cipher_page_size")==0 ){
2147 /* PRAGMA cipher_page_size will alter the size of the database pages while ensuring that the
2148 required reserve space is allocated at the end of each page. This will also override the
2149 standard SQLite PRAGMA page_size behavior if a codec context is attached to the database handle.
2150 If PRAGMA page_size is invoked but a codec context is not attached (i.e. dealing with a standard
2151 unencrypted database) then return early and allow the standard PRAGMA page_size logic to apply. */
2152 if(ctx) {
2153 if( zRight ) {
2154 int size = atoi(zRight);
2155 rc = sqlcipher_codec_ctx_set_pagesize(ctx, size);
2156 if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
2157 rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
2158 if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
2159 } else {
2160 char * page_size = sqlite3_mprintf("%d", ctx->page_sz);
2161 sqlcipher_vdbe_return_string(pParse, "cipher_page_size", page_size, P4_DYNAMIC);
2163 } else {
2164 return 0; /* return early so that the PragTyp_PAGE_SIZE case logic in pragma.c will take effect */
2166 }else
2167 if( sqlite3_stricmp(zLeft,"cipher_default_page_size")==0 ){
2168 if( zRight ) {
2169 default_page_size = atoi(zRight);
2170 } else {
2171 char *page_size = sqlite3_mprintf("%d", default_page_size);
2172 sqlcipher_vdbe_return_string(pParse, "cipher_default_page_size", page_size, P4_DYNAMIC);
2174 }else
2175 if( sqlite3_stricmp(zLeft,"cipher_default_use_hmac")==0 ){
2176 if( zRight ) {
2177 sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1));
2178 } else {
2179 char *default_use_hmac = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC));
2180 sqlcipher_vdbe_return_string(pParse, "cipher_default_use_hmac", default_use_hmac, P4_DYNAMIC);
2182 }else
2183 if( sqlite3_stricmp(zLeft,"cipher_use_hmac")==0 ){
2184 if(ctx) {
2185 if( zRight ) {
2186 rc = sqlcipher_codec_ctx_set_use_hmac(ctx, sqlite3GetBoolean(zRight,1));
2187 if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
2188 /* since the use of hmac has changed, the page size may also change */
2189 rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
2190 if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
2191 } else {
2192 char *hmac_flag = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC));
2193 sqlcipher_vdbe_return_string(pParse, "cipher_use_hmac", hmac_flag, P4_DYNAMIC);
2196 }else
2197 if( sqlite3_stricmp(zLeft,"cipher_hmac_pgno")==0 ){
2198 if(ctx) {
2199 if(zRight) {
2200 char *deprecation = "PRAGMA cipher_hmac_pgno is deprecated, please remove from use";
2201 /* clear both pgno endian flags */
2202 if(sqlite3_stricmp(zRight, "le") == 0) {
2203 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO);
2204 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_LE_PGNO);
2205 } else if(sqlite3_stricmp(zRight, "be") == 0) {
2206 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO);
2207 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_BE_PGNO);
2208 } else if(sqlite3_stricmp(zRight, "native") == 0) {
2209 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO);
2210 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO);
2212 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", deprecation, P4_TRANSIENT);
2213 sqlite3_log(SQLITE_WARNING, deprecation);
2215 } else {
2216 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) {
2217 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "le", P4_TRANSIENT);
2218 } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) {
2219 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "be", P4_TRANSIENT);
2220 } else {
2221 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "native", P4_TRANSIENT);
2225 }else
2226 if( sqlite3_stricmp(zLeft,"cipher_hmac_salt_mask")==0 ){
2227 if(ctx) {
2228 if(zRight) {
2229 char *deprecation = "PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use";
2230 if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == 5) {
2231 unsigned char mask = 0;
2232 const unsigned char *hex = (const unsigned char *)zRight+2;
2233 cipher_hex2bin(hex,2,&mask);
2234 hmac_salt_mask = mask;
2236 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", deprecation, P4_TRANSIENT);
2237 sqlite3_log(SQLITE_WARNING, deprecation);
2238 } else {
2239 char *mask = sqlite3_mprintf("%02x", hmac_salt_mask);
2240 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_salt_mask", mask, P4_DYNAMIC);
2243 }else
2244 if( sqlite3_stricmp(zLeft,"cipher_plaintext_header_size")==0 ){
2245 if(ctx) {
2246 if( zRight ) {
2247 int size = atoi(zRight);
2248 /* deliberately ignore result code, if size is invalid it will be set to -1
2249 and trip the error later in the codec */
2250 sqlcipher_codec_ctx_set_plaintext_header_size(ctx, size);
2251 } else {
2252 char *size = sqlite3_mprintf("%d", ctx->plaintext_header_sz);
2253 sqlcipher_vdbe_return_string(pParse, "cipher_plaintext_header_size", size, P4_DYNAMIC);
2256 }else
2257 if( sqlite3_stricmp(zLeft,"cipher_default_plaintext_header_size")==0 ){
2258 if( zRight ) {
2259 default_plaintext_header_size = atoi(zRight);
2260 } else {
2261 char *size = sqlite3_mprintf("%d", default_plaintext_header_size);
2262 sqlcipher_vdbe_return_string(pParse, "cipher_default_plaintext_header_size", size, P4_DYNAMIC);
2264 }else
2265 if( sqlite3_stricmp(zLeft,"cipher_salt")==0 ){
2266 if(ctx) {
2267 if(zRight) {
2268 if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == (FILE_HEADER_SZ*2)+3) {
2269 unsigned char *salt = (unsigned char*) sqlite3_malloc(FILE_HEADER_SZ);
2270 const unsigned char *hex = (const unsigned char *)zRight+2;
2271 cipher_hex2bin(hex,FILE_HEADER_SZ*2,salt);
2272 sqlcipher_codec_ctx_set_kdf_salt(ctx, salt, FILE_HEADER_SZ);
2273 sqlite3_free(salt);
2275 } else {
2276 void *salt;
2277 char *hexsalt = (char*) sqlite3_malloc((FILE_HEADER_SZ*2)+1);
2278 if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &salt)) == SQLITE_OK) {
2279 cipher_bin2hex(salt, FILE_HEADER_SZ, hexsalt);
2280 sqlcipher_vdbe_return_string(pParse, "cipher_salt", hexsalt, P4_DYNAMIC);
2281 } else {
2282 sqlite3_free(hexsalt);
2283 sqlcipher_codec_ctx_set_error(ctx, rc);
2287 }else
2288 if( sqlite3_stricmp(zLeft,"cipher_hmac_algorithm")==0 ){
2289 if(ctx) {
2290 if(zRight) {
2291 rc = SQLITE_ERROR;
2292 if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) {
2293 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1);
2294 } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) {
2295 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA256);
2296 } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) {
2297 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512);
2299 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2300 rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
2301 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2302 } else {
2303 int algorithm = ctx->hmac_algorithm;
2304 if(ctx->hmac_algorithm == SQLCIPHER_HMAC_SHA1) {
2305 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT);
2306 } else if(algorithm == SQLCIPHER_HMAC_SHA256) {
2307 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT);
2308 } else if(algorithm == SQLCIPHER_HMAC_SHA512) {
2309 sqlcipher_vdbe_return_string(pParse, "cipher_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT);
2313 }else
2314 if( sqlite3_stricmp(zLeft,"cipher_default_hmac_algorithm")==0 ){
2315 if(zRight) {
2316 rc = SQLITE_OK;
2317 if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA1_LABEL) == 0) {
2318 default_hmac_algorithm = SQLCIPHER_HMAC_SHA1;
2319 } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA256_LABEL) == 0) {
2320 default_hmac_algorithm = SQLCIPHER_HMAC_SHA256;
2321 } else if(sqlite3_stricmp(zRight, SQLCIPHER_HMAC_SHA512_LABEL) == 0) {
2322 default_hmac_algorithm = SQLCIPHER_HMAC_SHA512;
2324 } else {
2325 if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) {
2326 sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA1_LABEL, P4_TRANSIENT);
2327 } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) {
2328 sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA256_LABEL, P4_TRANSIENT);
2329 } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) {
2330 sqlcipher_vdbe_return_string(pParse, "cipher_default_hmac_algorithm", SQLCIPHER_HMAC_SHA512_LABEL, P4_TRANSIENT);
2333 }else
2334 if( sqlite3_stricmp(zLeft,"cipher_kdf_algorithm")==0 ){
2335 if(ctx) {
2336 if(zRight) {
2337 rc = SQLITE_ERROR;
2338 if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) {
2339 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1);
2340 } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) {
2341 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA256);
2342 } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) {
2343 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512);
2345 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2346 } else {
2347 if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
2348 sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT);
2349 } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
2350 sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT);
2351 } else if(ctx->kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
2352 sqlcipher_vdbe_return_string(pParse, "cipher_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT);
2356 }else
2357 if( sqlite3_stricmp(zLeft,"cipher_default_kdf_algorithm")==0 ){
2358 if(zRight) {
2359 rc = SQLITE_OK;
2360 if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL) == 0) {
2361 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1;
2362 } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL) == 0) {
2363 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA256;
2364 } else if(sqlite3_stricmp(zRight, SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL) == 0) {
2365 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512;
2367 } else {
2368 if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
2369 sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL, P4_TRANSIENT);
2370 } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
2371 sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL, P4_TRANSIENT);
2372 } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
2373 sqlcipher_vdbe_return_string(pParse, "cipher_default_kdf_algorithm", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL, P4_TRANSIENT);
2376 }else
2377 if( sqlite3_stricmp(zLeft,"cipher_compatibility")==0 ){
2378 if(ctx) {
2379 if(zRight) {
2380 int version = atoi(zRight);
2382 switch(version) {
2383 case 1:
2384 rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024);
2385 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2386 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1);
2387 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2388 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1);
2389 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2390 rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000);
2391 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2392 rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 0);
2393 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2394 break;
2396 case 2:
2397 rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024);
2398 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2399 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1);
2400 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2401 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1);
2402 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2403 rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 4000);
2404 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2405 rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1);
2406 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2407 break;
2409 case 3:
2410 rc = sqlcipher_codec_ctx_set_pagesize(ctx, 1024);
2411 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2412 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA1);
2413 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2414 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA1);
2415 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2416 rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 64000);
2417 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2418 rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1);
2419 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2420 break;
2422 default:
2423 rc = sqlcipher_codec_ctx_set_pagesize(ctx, 4096);
2424 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2425 rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, SQLCIPHER_HMAC_SHA512);
2426 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2427 rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, SQLCIPHER_PBKDF2_HMAC_SHA512);
2428 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2429 rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, 256000);
2430 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2431 rc = sqlcipher_codec_ctx_set_use_hmac(ctx, 1);
2432 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2433 break;
2436 rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
2437 if (rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2440 }else
2441 if( sqlite3_stricmp(zLeft,"cipher_default_compatibility")==0 ){
2442 if(zRight) {
2443 int version = atoi(zRight);
2444 switch(version) {
2445 case 1:
2446 default_page_size = 1024;
2447 default_hmac_algorithm = SQLCIPHER_HMAC_SHA1;
2448 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1;
2449 default_kdf_iter = 4000;
2450 sqlcipher_set_default_use_hmac(0);
2451 break;
2453 case 2:
2454 default_page_size = 1024;
2455 default_hmac_algorithm = SQLCIPHER_HMAC_SHA1;
2456 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1;
2457 default_kdf_iter = 4000;
2458 sqlcipher_set_default_use_hmac(1);
2459 break;
2461 case 3:
2462 default_page_size = 1024;
2463 default_hmac_algorithm = SQLCIPHER_HMAC_SHA1;
2464 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA1;
2465 default_kdf_iter = 64000;
2466 sqlcipher_set_default_use_hmac(1);
2467 break;
2469 default:
2470 default_page_size = 4096;
2471 default_hmac_algorithm = SQLCIPHER_HMAC_SHA512;
2472 default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512;
2473 default_kdf_iter = 256000;
2474 sqlcipher_set_default_use_hmac(1);
2475 break;
2478 }else
2479 if( sqlite3_stricmp(zLeft,"cipher_memory_security")==0 ){
2480 if( zRight ) {
2481 if(sqlite3GetBoolean(zRight,1)) {
2482 /* memory security can only be enabled, not disabled */
2483 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipher_set_mem_security: on");
2484 sqlcipher_mem_security_on = 1;
2486 } else {
2487 /* only report that memory security is enabled if pragma cipher_memory_security is ON and
2488 SQLCipher's allocator/deallocator was run at least one time */
2489 int state = sqlcipher_mem_security_on && sqlcipher_mem_executed;
2490 char *on = sqlite3_mprintf("%d", state);
2491 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE,
2492 "sqlcipher_get_mem_security: sqlcipher_mem_security_on = %d, sqlcipher_mem_executed = %d",
2493 sqlcipher_mem_security_on, sqlcipher_mem_executed);
2494 sqlcipher_vdbe_return_string(pParse, "cipher_memory_security", on, P4_DYNAMIC);
2496 }else
2497 if( sqlite3_stricmp(zLeft,"cipher_settings")==0 ){
2498 if(ctx) {
2499 int algorithm;
2500 char *pragma;
2502 pragma = sqlite3_mprintf("PRAGMA kdf_iter = %d;", ctx->kdf_iter);
2503 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2505 pragma = sqlite3_mprintf("PRAGMA cipher_page_size = %d;", ctx->page_sz);
2506 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2508 pragma = sqlite3_mprintf("PRAGMA cipher_use_hmac = %d;", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC));
2509 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2511 pragma = sqlite3_mprintf("PRAGMA cipher_plaintext_header_size = %d;", ctx->plaintext_header_sz);
2512 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2514 algorithm = ctx->hmac_algorithm;
2515 pragma = NULL;
2516 if(algorithm == SQLCIPHER_HMAC_SHA1) {
2517 pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL);
2518 } else if(algorithm == SQLCIPHER_HMAC_SHA256) {
2519 pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL);
2520 } else if(algorithm == SQLCIPHER_HMAC_SHA512) {
2521 pragma = sqlite3_mprintf("PRAGMA cipher_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL);
2523 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2525 algorithm = ctx->kdf_algorithm;
2526 pragma = NULL;
2527 if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
2528 pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL);
2529 } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
2530 pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL);
2531 } else if(algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
2532 pragma = sqlite3_mprintf("PRAGMA cipher_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL);
2534 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2537 }else
2538 if( sqlite3_stricmp(zLeft,"cipher_default_settings")==0 ){
2539 char *pragma;
2541 pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_iter = %d;", default_kdf_iter);
2542 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2544 pragma = sqlite3_mprintf("PRAGMA cipher_default_page_size = %d;", default_page_size);
2545 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2547 pragma = sqlite3_mprintf("PRAGMA cipher_default_use_hmac = %d;", SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC));
2548 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2550 pragma = sqlite3_mprintf("PRAGMA cipher_default_plaintext_header_size = %d;", default_plaintext_header_size);
2551 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2553 pragma = NULL;
2554 if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA1) {
2555 pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA1_LABEL);
2556 } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA256) {
2557 pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA256_LABEL);
2558 } else if(default_hmac_algorithm == SQLCIPHER_HMAC_SHA512) {
2559 pragma = sqlite3_mprintf("PRAGMA cipher_default_hmac_algorithm = %s;", SQLCIPHER_HMAC_SHA512_LABEL);
2561 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2563 pragma = NULL;
2564 if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA1) {
2565 pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA1_LABEL);
2566 } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA256) {
2567 pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA256_LABEL);
2568 } else if(default_kdf_algorithm == SQLCIPHER_PBKDF2_HMAC_SHA512) {
2569 pragma = sqlite3_mprintf("PRAGMA cipher_default_kdf_algorithm = %s;", SQLCIPHER_PBKDF2_HMAC_SHA512_LABEL);
2571 sqlcipher_vdbe_return_string(pParse, "pragma", pragma, P4_DYNAMIC);
2572 }else
2573 if( sqlite3_stricmp(zLeft,"cipher_integrity_check")==0 ){
2574 if(ctx) {
2575 sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check");
2577 } else
2578 if( sqlite3_stricmp(zLeft, "cipher_log_level")==0 ){
2579 if(zRight) {
2580 sqlcipher_log_level = SQLCIPHER_LOG_NONE;
2581 if(sqlite3_stricmp(zRight, "ERROR")==0) sqlcipher_log_level = SQLCIPHER_LOG_ERROR;
2582 else if(sqlite3_stricmp(zRight, "WARN" )==0) sqlcipher_log_level = SQLCIPHER_LOG_WARN;
2583 else if(sqlite3_stricmp(zRight, "INFO" )==0) sqlcipher_log_level = SQLCIPHER_LOG_INFO;
2584 else if(sqlite3_stricmp(zRight, "DEBUG")==0) sqlcipher_log_level = SQLCIPHER_LOG_DEBUG;
2585 else if(sqlite3_stricmp(zRight, "TRACE")==0) sqlcipher_log_level = SQLCIPHER_LOG_TRACE;
2587 sqlcipher_vdbe_return_string(pParse, "cipher_log_level", sqlcipher_get_log_level_str(sqlcipher_log_level), P4_TRANSIENT);
2588 } else
2589 if( sqlite3_stricmp(zLeft, "cipher_log_source")==0 ){
2590 if(zRight) {
2591 sqlcipher_log_source = SQLCIPHER_LOG_NONE;
2592 if(sqlite3_stricmp(zRight, "NONE" )==0) sqlcipher_log_source = SQLCIPHER_LOG_NONE;
2593 else if(sqlite3_stricmp(zRight, "ALL" )==0) sqlcipher_log_source = SQLCIPHER_LOG_ALL;
2594 else if(sqlite3_stricmp(zRight, "CORE" )==0) sqlcipher_log_source = SQLCIPHER_LOG_CORE;
2595 else if(sqlite3_stricmp(zRight, "MEMORY" )==0) sqlcipher_log_source = SQLCIPHER_LOG_MEMORY;
2596 else if(sqlite3_stricmp(zRight, "MUTEX" )==0) sqlcipher_log_source = SQLCIPHER_LOG_MUTEX;
2597 else if(sqlite3_stricmp(zRight, "PROVIDER")==0) sqlcipher_log_source = SQLCIPHER_LOG_PROVIDER;
2599 sqlcipher_vdbe_return_string(pParse, "cipher_log_source", sqlcipher_get_log_source_str(sqlcipher_log_source), P4_TRANSIENT);
2600 } else
2601 if( sqlite3_stricmp(zLeft, "cipher_log")== 0 && zRight ){
2602 char *status = sqlite3_mprintf("%d", sqlcipher_set_log(zRight));
2603 sqlcipher_vdbe_return_string(pParse, "cipher_log", status, P4_DYNAMIC);
2604 }else {
2605 return 0;
2607 return 1;
2610 /* these constants are used internally within SQLite's pager.c to differentiate between
2611 operations on the main database or journal pages. This is important in the context
2612 of a rekey operations, where the journal must be written using the original key
2613 material (to allow a transactional rollback), while the new database pages are being
2614 written with the new key material*/
2615 #define CODEC_READ_OP 3
2616 #define CODEC_WRITE_OP 6
2617 #define CODEC_JOURNAL_OP 7
2620 * sqlite3Codec can be called in multiple modes.
2621 * encrypt mode - expected to return a pointer to the
2622 * encrypted data without altering pData.
2623 * decrypt mode - expected to return a pointer to pData, with
2624 * the data decrypted in the input buffer
2626 static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
2627 codec_ctx *ctx = (codec_ctx *) iCtx;
2628 int offset = 0, rc = 0;
2629 unsigned char *pData = (unsigned char *) data;
2630 int cctx = CIPHER_READ_CTX;
2632 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: pgno=%d, mode=%d, ctx->page_sz=%d", pgno, mode, ctx->page_sz);
2634 #ifdef SQLCIPHER_EXT
2635 if(sqlcipher_license_check(ctx) != SQLITE_OK) return NULL;
2636 #endif
2638 /* call to derive keys if not present yet */
2639 if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) {
2640 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error occurred during key derivation: %d", rc);
2641 sqlcipher_codec_ctx_set_error(ctx, rc);
2642 return NULL;
2645 /* if the plaintext_header_size is negative that means an invalid size was set via
2646 PRAGMA. We can't set the error state on the pager at that point because the pager
2647 may not be open yet. However, this is a fatal error state, so abort the codec */
2648 if(ctx->plaintext_header_sz < 0) {
2649 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error invalid ctx->plaintext_header_sz: %d", ctx->plaintext_header_sz);
2650 sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR);
2651 return NULL;
2654 if(pgno == 1) /* adjust starting pointers in data page for header offset on first page*/
2655 offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ;
2658 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3Codec: switch mode=%d offset=%d", mode, offset);
2659 switch(mode) {
2660 case CODEC_READ_OP: /* decrypt */
2661 if(pgno == 1) /* copy initial part of file header or SQLite magic to buffer */
2662 memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset);
2664 rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
2665 #ifdef SQLCIPHER_TEST
2666 if((cipher_test_flags & TEST_FAIL_DECRYPT) > 0 && sqlcipher_get_test_fail()) {
2667 rc = SQLITE_ERROR;
2668 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating decryption failure for pgno=%d, mode=%d, ctx->page_sz=%d\n", pgno, mode, ctx->page_sz);
2670 #endif
2671 if(rc != SQLITE_OK) {
2672 /* failure to decrypt a page is considered a permanent error and will render the pager unusable
2673 in order to prevent inconsistent data being loaded into page cache */
2674 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error decrypting page %d data: %d", pgno, rc);
2675 sqlcipher_memset((unsigned char*) ctx->buffer+offset, 0, ctx->page_sz-offset);
2676 sqlcipher_codec_ctx_set_error(ctx, rc);
2677 } else {
2678 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED);
2680 memcpy(pData, ctx->buffer, ctx->page_sz); /* copy buffer data back to pData and return */
2681 return pData;
2682 break;
2684 case CODEC_WRITE_OP: /* encrypt database page, operate on write context and fall through to case 7, so the write context is used*/
2685 cctx = CIPHER_WRITE_CTX;
2687 case CODEC_JOURNAL_OP: /* encrypt journal page, operate on read context use to get the original page data from the database */
2688 if(pgno == 1) { /* copy initial part of file header or salt to buffer */
2689 void *kdf_salt = NULL;
2690 /* retrieve the kdf salt */
2691 if((rc = sqlcipher_codec_ctx_get_kdf_salt(ctx, &kdf_salt)) != SQLITE_OK) {
2692 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error retrieving salt: %d", rc);
2693 sqlcipher_codec_ctx_set_error(ctx, rc);
2694 return NULL;
2696 memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : kdf_salt, offset);
2698 rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
2699 #ifdef SQLCIPHER_TEST
2700 if((cipher_test_flags & TEST_FAIL_ENCRYPT) > 0 && sqlcipher_get_test_fail()) {
2701 rc = SQLITE_ERROR;
2702 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3Codec: simulating encryption failure for pgno=%d, mode=%d, ctx->page_sz=%d\n", pgno, mode, ctx->page_sz);
2704 #endif
2705 if(rc != SQLITE_OK) {
2706 /* failure to encrypt a page is considered a permanent error and will render the pager unusable
2707 in order to prevent corrupted pages from being written to the main databased when using WAL */
2708 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error encrypting page %d data: %d", pgno, rc);
2709 sqlcipher_memset((unsigned char*)ctx->buffer+offset, 0, ctx->page_sz-offset);
2710 sqlcipher_codec_ctx_set_error(ctx, rc);
2711 return NULL;
2713 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED);
2714 return ctx->buffer; /* return persistent buffer data, pData remains intact */
2715 break;
2717 default:
2718 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3Codec: error unsupported codec mode %d", mode);
2719 sqlcipher_codec_ctx_set_error(ctx, SQLITE_ERROR); /* unsupported mode, set error */
2720 return pData;
2721 break;
2725 static void sqlite3FreeCodecArg(void *pCodecArg) {
2726 codec_ctx *ctx = (codec_ctx *) pCodecArg;
2727 if(pCodecArg == NULL) return;
2728 sqlcipher_codec_ctx_free(&ctx); /* wipe and free allocated memory for the context */
2729 sqlcipher_deactivate(); /* cleanup related structures, OpenSSL etc, when codec is detatched */
2732 int sqlcipherCodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
2733 struct Db *pDb = &db->aDb[nDb];
2735 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: db=%p, nDb=%d", db, nDb);
2737 if(nKey && zKey && pDb->pBt) {
2738 int rc;
2739 Pager *pPager = pDb->pBt->pBt->pPager;
2740 sqlite3_file *fd;
2741 codec_ctx *ctx;
2743 ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
2745 if(ctx != NULL && SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) {
2746 /* there is already a codec attached to this database, so we should not proceed */
2747 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: no codec attached to db");
2748 return SQLITE_OK;
2751 /* check if the sqlite3_file is open, and if not force handle to NULL */
2752 if((fd = sqlite3PagerFile(pPager))->pMethods == 0) fd = NULL;
2754 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling sqlcipher_activate()");
2755 sqlcipher_activate(); /* perform internal initialization for sqlcipher */
2757 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: entering database mutex %p", db->mutex);
2758 sqlite3_mutex_enter(db->mutex);
2759 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: entered database mutex %p", db->mutex);
2761 #ifdef SQLCIPHER_EXT
2762 if((rc = sqlite3_set_authorizer(db, sqlcipher_license_authorizer, db)) != SQLITE_OK) {
2763 sqlite3_mutex_leave(db->mutex);
2764 return rc;
2766 #endif
2768 /* point the internal codec argument against the contet to be prepared */
2769 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling sqlcipher_codec_ctx_init()");
2770 rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, zKey, nKey);
2772 if(rc != SQLITE_OK) {
2773 /* initialization failed, do not attach potentially corrupted context */
2774 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: context initialization failed, forcing error state with rc=%d", rc);
2775 /* force an error at the pager level, such that even the upstream caller ignores the return code
2776 the pager will be in an error state and will process no further operations */
2777 sqlite3pager_error(pPager, rc);
2778 pDb->pBt->pBt->db->errCode = rc;
2779 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: leaving database mutex %p (early return on rc=%d)", db->mutex, rc);
2780 sqlite3_mutex_leave(db->mutex);
2781 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: left database mutex %p (early return on rc=%d)", db->mutex, rc);
2782 return rc;
2785 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling sqlcipherPagerSetCodec()");
2786 sqlcipherPagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
2788 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling codec_set_btree_to_codec_pagesize()");
2789 codec_set_btree_to_codec_pagesize(db, pDb, ctx);
2791 /* force secure delete. This has the benefit of wiping internal data when deleted
2792 and also ensures that all pages are written to disk (i.e. not skipped by
2793 sqlite3PagerDontWrite optimizations) */
2794 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling sqlite3BtreeSecureDelete()");
2795 sqlite3BtreeSecureDelete(pDb->pBt, 1);
2797 /* if fd is null, then this is an in-memory database and
2798 we dont' want to overwrite the AutoVacuum settings
2799 if not null, then set to the default */
2800 if(fd != NULL) {
2801 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecAttach: calling sqlite3BtreeSetAutoVacuum()");
2802 sqlite3BtreeSetAutoVacuum(pDb->pBt, SQLITE_DEFAULT_AUTOVACUUM);
2804 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: leaving database mutex %p", db->mutex);
2805 sqlite3_mutex_leave(db->mutex);
2806 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipherCodecAttach: left database mutex %p", db->mutex);
2808 return SQLITE_OK;
2811 int sqlcipher_find_db_index(sqlite3 *db, const char *zDb) {
2812 int db_index;
2813 if(zDb == NULL){
2814 return 0;
2816 for(db_index = 0; db_index < db->nDb; db_index++) {
2817 struct Db *pDb = &db->aDb[db_index];
2818 if(strcmp(pDb->zDbSName, zDb) == 0) {
2819 return db_index;
2822 return 0;
2825 void sqlite3_activate_see(const char* in) {
2826 /* do nothing, security enhancements are always active */
2829 int sqlite3_key(sqlite3 *db, const void *pKey, int nKey) {
2830 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_key: db=%p", db);
2831 return sqlite3_key_v2(db, "main", pKey, nKey);
2834 int sqlite3_key_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
2835 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_key_v2: db=%p zDb=%s", db, zDb);
2836 /* attach key if db and pKey are not null and nKey is > 0 */
2837 if(db && pKey && nKey) {
2838 int db_index = sqlcipher_find_db_index(db, zDb);
2839 return sqlcipherCodecAttach(db, db_index, pKey, nKey);
2841 sqlcipher_log(SQLCIPHER_LOG_WARN, SQLCIPHER_LOG_CORE, "sqlite3_key_v2: no key provided");
2842 return SQLITE_ERROR;
2845 int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey) {
2846 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey: db=%p", db);
2847 return sqlite3_rekey_v2(db, "main", pKey, nKey);
2850 /* sqlite3_rekey_v2
2851 ** Given a database, this will reencrypt the database using a new key.
2852 ** There is only one possible modes of operation - to encrypt a database
2853 ** that is already encrpyted. If the database is not already encrypted
2854 ** this should do nothing
2855 ** The proposed logic for this function follows:
2856 ** 1. Determine if the database is already encryptped
2857 ** 2. If there is NOT already a key present do nothing
2858 ** 3. If there is a key present, re-encrypt the database with the new key
2860 int sqlite3_rekey_v2(sqlite3 *db, const char *zDb, const void *pKey, int nKey) {
2861 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: db=%p zDb=%s", db, zDb);
2862 if(db && pKey && nKey) {
2863 int db_index = sqlcipher_find_db_index(db, zDb);
2864 struct Db *pDb = &db->aDb[db_index];
2865 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: database zDb=%p db_index:%d", zDb, db_index);
2866 if(pDb->pBt) {
2867 codec_ctx *ctx;
2868 int rc, page_count;
2869 Pgno pgno;
2870 PgHdr *page;
2871 Pager *pPager = pDb->pBt->pBt->pPager;
2873 ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
2875 if(ctx == NULL) {
2876 /* there was no codec attached to this database, so this should do nothing! */
2877 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no codec attached to db %s: rekey can't be used on an unencrypted database", zDb);
2878 return SQLITE_MISUSE;
2881 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entering database mutex %p", db->mutex);
2882 sqlite3_mutex_enter(db->mutex);
2883 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: entered database mutex %p", db->mutex);
2885 codec_set_pass_key(db, db_index, pKey, nKey, CIPHER_WRITE_CTX);
2887 /* do stuff here to rewrite the database
2888 ** 1. Create a transaction on the database
2889 ** 2. Iterate through each page, reading it and then writing it.
2890 ** 3. If that goes ok then commit and put ctx->rekey into ctx->key
2891 ** note: don't deallocate rekey since it may be used in a subsequent iteration
2893 rc = sqlite3BtreeBeginTrans(pDb->pBt, 1, 0); /* begin write transaction */
2894 sqlite3PagerPagecount(pPager, &page_count);
2895 for(pgno = 1; rc == SQLITE_OK && pgno <= (unsigned int)page_count; pgno++) { /* pgno's start at 1 see pager.c:pagerAcquire */
2896 if(!sqlite3pager_is_sj_pgno(pPager, pgno)) { /* skip this page (see pager.c:pagerAcquire for reasoning) */
2897 rc = sqlite3PagerGet(pPager, pgno, &page, 0);
2898 if(rc == SQLITE_OK) { /* write page see pager_incr_changecounter for example */
2899 rc = sqlite3PagerWrite(page);
2900 if(rc == SQLITE_OK) {
2901 sqlite3PagerUnref(page);
2902 } else {
2903 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred writing page %d", rc, pgno);
2905 } else {
2906 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: error %d occurred reading page %d", rc, pgno);
2911 /* if commit was successful commit and copy the rekey data to current key, else rollback to release locks */
2912 if(rc == SQLITE_OK) {
2913 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: committing");
2914 rc = sqlite3BtreeCommit(pDb->pBt);
2915 sqlcipher_codec_key_copy(ctx, CIPHER_WRITE_CTX);
2916 } else {
2917 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: rollback");
2918 sqlite3BtreeRollback(pDb->pBt, SQLITE_ABORT_ROLLBACK, 0);
2921 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: leaving database mutex %p", db->mutex);
2922 sqlite3_mutex_leave(db->mutex);
2923 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlite3_rekey_v2: left database mutex %p", db->mutex);
2925 return SQLITE_OK;
2927 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "sqlite3_rekey_v2: no key provided for db %s: rekey can't be used to decrypt an encrypted database", zDb);
2928 return SQLITE_ERROR;
2931 void sqlcipherCodecGetKey(sqlite3* db, int nDb, void **zKey, int *nKey) {
2932 struct Db *pDb = &db->aDb[nDb];
2933 sqlcipher_log(SQLCIPHER_LOG_DEBUG, SQLCIPHER_LOG_CORE, "sqlcipherCodecGetKey:db=%p, nDb=%d", db, nDb);
2934 if( pDb->pBt ) {
2935 codec_ctx *ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
2937 if(ctx) {
2938 /* pass back the keyspec from the codec, unless PRAGMA cipher_store_pass
2939 is set or keyspec has not yet been derived, in which case pass
2940 back the password key material */
2941 *zKey = ctx->read_ctx->keyspec;
2942 *nKey = ctx->keyspec_sz;
2943 if(ctx->store_pass == 1 || *zKey == NULL) {
2944 *zKey = ctx->read_ctx->pass;
2945 *nKey = ctx->read_ctx->pass_sz;
2947 } else {
2948 *zKey = NULL;
2949 *nKey = 0;
2955 * Implementation of an "export" function that allows a caller
2956 * to duplicate the main database to an attached database. This is intended
2957 * as a conveneince for users who need to:
2959 * 1. migrate from an non-encrypted database to an encrypted database
2960 * 2. move from an encrypted database to a non-encrypted database
2961 * 3. convert beween the various flavors of encrypted databases.
2963 * This implementation is based heavily on the procedure and code used
2964 * in vacuum.c, but is exposed as a function that allows export to any
2965 * named attached database.
2969 ** Finalize a prepared statement. If there was an error, store the
2970 ** text of the error message in *pzErrMsg. Return the result code.
2972 ** Based on vacuumFinalize from vacuum.c
2974 static int sqlcipher_finalize(sqlite3 *db, sqlite3_stmt *pStmt, char **pzErrMsg){
2975 int rc;
2976 rc = sqlite3VdbeFinalize((Vdbe*)pStmt);
2977 if( rc ){
2978 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
2980 return rc;
2984 ** Execute zSql on database db. Return an error code.
2986 ** Based on execSql from vacuum.c
2988 static int sqlcipher_execSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
2989 sqlite3_stmt *pStmt;
2990 VVA_ONLY( int rc; )
2991 if( !zSql ){
2992 return SQLITE_NOMEM;
2994 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
2995 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
2996 return sqlite3_errcode(db);
2998 VVA_ONLY( rc = ) sqlite3_step(pStmt);
2999 assert( rc!=SQLITE_ROW );
3000 return sqlcipher_finalize(db, pStmt, pzErrMsg);
3004 ** Execute zSql on database db. The statement returns exactly
3005 ** one column. Execute this as SQL on the same database.
3007 ** Based on execExecSql from vacuum.c
3009 static int sqlcipher_execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
3010 sqlite3_stmt *pStmt;
3011 int rc;
3013 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
3014 if( rc!=SQLITE_OK ) return rc;
3016 while( SQLITE_ROW==sqlite3_step(pStmt) ){
3017 rc = sqlcipher_execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
3018 if( rc!=SQLITE_OK ){
3019 sqlcipher_finalize(db, pStmt, pzErrMsg);
3020 return rc;
3024 return sqlcipher_finalize(db, pStmt, pzErrMsg);
3028 * copy database and schema from the main database to an attached database
3030 * Based on sqlite3RunVacuum from vacuum.c
3032 void sqlcipher_exportFunc(sqlite3_context *context, int argc, sqlite3_value **argv) {
3033 sqlite3 *db = sqlite3_context_db_handle(context);
3034 const char* targetDb, *sourceDb;
3035 int targetDb_idx = 0;
3036 u64 saved_flags = db->flags; /* Saved value of the db->flags */
3037 u32 saved_mDbFlags = db->mDbFlags; /* Saved value of the db->mDbFlags */
3038 int saved_nChange = db->nChange; /* Saved value of db->nChange */
3039 int saved_nTotalChange = db->nTotalChange; /* Saved value of db->nTotalChange */
3040 u8 saved_mTrace = db->mTrace; /* Saved value of db->mTrace */
3041 int rc = SQLITE_OK; /* Return code from service routines */
3042 char *zSql = NULL; /* SQL statements */
3043 char *pzErrMsg = NULL;
3045 if(argc != 1 && argc != 2) {
3046 rc = SQLITE_ERROR;
3047 pzErrMsg = sqlite3_mprintf("invalid number of arguments (%d) passed to sqlcipher_export", argc);
3048 goto end_of_export;
3051 if(sqlite3_value_type(argv[0]) == SQLITE_NULL) {
3052 rc = SQLITE_ERROR;
3053 pzErrMsg = sqlite3_mprintf("target database can't be NULL");
3054 goto end_of_export;
3057 targetDb = (const char*) sqlite3_value_text(argv[0]);
3058 sourceDb = "main";
3060 if(argc == 2) {
3061 if(sqlite3_value_type(argv[1]) == SQLITE_NULL) {
3062 rc = SQLITE_ERROR;
3063 pzErrMsg = sqlite3_mprintf("target database can't be NULL");
3064 goto end_of_export;
3066 sourceDb = (char *) sqlite3_value_text(argv[1]);
3070 /* if the name of the target is not main, but the index returned is zero
3071 there is a mismatch and we should not proceed */
3072 targetDb_idx = sqlcipher_find_db_index(db, targetDb);
3073 if(targetDb_idx == 0 && targetDb != NULL && sqlite3_stricmp("main", targetDb) != 0) {
3074 rc = SQLITE_ERROR;
3075 pzErrMsg = sqlite3_mprintf("unknown database %s", targetDb);
3076 goto end_of_export;
3078 db->init.iDb = targetDb_idx;
3080 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
3081 db->mDbFlags |= DBFLAG_PreferBuiltin | DBFLAG_Vacuum;
3082 db->flags &= ~(u64)(SQLITE_ForeignKeys | SQLITE_ReverseOrder | SQLITE_Defensive | SQLITE_CountRows);
3083 db->mTrace = 0;
3085 /* Query the schema of the main database. Create a mirror schema
3086 ** in the temporary database.
3088 zSql = sqlite3_mprintf(
3089 "SELECT sql "
3090 " FROM %s.sqlite_schema WHERE type='table' AND name!='sqlite_sequence'"
3091 " AND rootpage>0"
3092 , sourceDb);
3093 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
3094 if( rc!=SQLITE_OK ) goto end_of_export;
3095 sqlite3_free(zSql);
3097 zSql = sqlite3_mprintf(
3098 "SELECT sql "
3099 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE INDEX %%' "
3100 , sourceDb);
3101 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
3102 if( rc!=SQLITE_OK ) goto end_of_export;
3103 sqlite3_free(zSql);
3105 zSql = sqlite3_mprintf(
3106 "SELECT sql "
3107 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
3108 , sourceDb);
3109 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
3110 if( rc!=SQLITE_OK ) goto end_of_export;
3111 sqlite3_free(zSql);
3113 /* Loop through the tables in the main database. For each, do
3114 ** an "INSERT INTO rekey_db.xxx SELECT * FROM main.xxx;" to copy
3115 ** the contents to the temporary database.
3117 zSql = sqlite3_mprintf(
3118 "SELECT 'INSERT INTO %s.' || quote(name) "
3119 "|| ' SELECT * FROM %s.' || quote(name) || ';'"
3120 "FROM %s.sqlite_schema "
3121 "WHERE type = 'table' AND name!='sqlite_sequence' "
3122 " AND rootpage>0"
3123 , targetDb, sourceDb, sourceDb);
3124 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
3125 if( rc!=SQLITE_OK ) goto end_of_export;
3126 sqlite3_free(zSql);
3128 /* Copy over the contents of the sequence table
3130 zSql = sqlite3_mprintf(
3131 "SELECT 'INSERT INTO %s.' || quote(name) "
3132 "|| ' SELECT * FROM %s.' || quote(name) || ';' "
3133 "FROM %s.sqlite_schema WHERE name=='sqlite_sequence';"
3134 , targetDb, sourceDb, targetDb);
3135 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execExecSql(db, &pzErrMsg, zSql);
3136 if( rc!=SQLITE_OK ) goto end_of_export;
3137 sqlite3_free(zSql);
3139 /* Copy the triggers, views, and virtual tables from the main database
3140 ** over to the temporary database. None of these objects has any
3141 ** associated storage, so all we have to do is copy their entries
3142 ** from the SQLITE_MASTER table.
3144 zSql = sqlite3_mprintf(
3145 "INSERT INTO %s.sqlite_schema "
3146 " SELECT type, name, tbl_name, rootpage, sql"
3147 " FROM %s.sqlite_schema"
3148 " WHERE type='view' OR type='trigger'"
3149 " OR (type='table' AND rootpage=0)"
3150 , targetDb, sourceDb);
3151 rc = (zSql == NULL) ? SQLITE_NOMEM : sqlcipher_execSql(db, &pzErrMsg, zSql);
3152 if( rc!=SQLITE_OK ) goto end_of_export;
3153 sqlite3_free(zSql);
3155 zSql = NULL;
3156 end_of_export:
3157 db->init.iDb = 0;
3158 db->flags = saved_flags;
3159 db->mDbFlags = saved_mDbFlags;
3160 db->nChange = saved_nChange;
3161 db->nTotalChange = saved_nTotalChange;
3162 db->mTrace = saved_mTrace;
3164 if(zSql) sqlite3_free(zSql);
3166 if(rc) {
3167 if(pzErrMsg != NULL) {
3168 sqlite3_result_error(context, pzErrMsg, -1);
3169 sqlite3DbFree(db, pzErrMsg);
3170 } else {
3171 sqlite3_result_error(context, sqlite3ErrStr(rc), -1);
3175 #endif
3176 /* END SQLCIPHER */