5 ** Copyright (c) 2008-2024, ZETETIC LLC
6 ** All rights reserved.
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.
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.
32 #ifdef SQLITE_HAS_CODEC
34 #include "sqliteInt.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>
50 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
51 #include <windows.h> /* amalgamator: dontcache */
53 #include <sys/time.h> /* amalgamator: dontcache */
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 */
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
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
92 #ifndef CIPHER_VERSION_BUILD
93 #define CIPHER_VERSION_BUILD community
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
104 #define PBKDF2_ITER 256000
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
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
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
140 #ifndef CIPHER_MAX_IV_SZ
141 #define CIPHER_MAX_IV_SZ 16
144 #ifndef CIPHER_MAX_KEY_SZ
145 #define CIPHER_MAX_KEY_SZ 64
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
){
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
) {
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
){
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')) {
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 */
192 unsigned char *hmac_key
;
210 int plaintext_header_sz
;
214 unsigned char *kdf_salt
;
215 unsigned char *hmac_kdf_salt
;
216 unsigned char *buffer
;
218 cipher_ctx
*read_ctx
;
219 cipher_ctx
*write_ctx
;
220 sqlcipher_provider
*provider
;
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() {
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);
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
) {
273 #if defined(__unix__) || defined(__APPLE__)
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
);
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))
289 sqlcipher_log(SQLCIPHER_LOG_TRACE
, SQLCIPHER_LOG_MEMORY
, "sqlcipher_mlock: calling VirtualLock(%p,%d)", ptr
, sz
);
290 rc
= VirtualLock(ptr
, sz
);
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());
300 static void sqlcipher_munlock(void *ptr
, sqlite_uint64 sz
) {
302 #if defined(__unix__) || defined(__APPLE__)
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
);
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))
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. */
327 sqlcipher_log(SQLCIPHER_LOG_INFO
, SQLCIPHER_LOG_MEMORY
, "sqlcipher_munlock: VirtualUnlock(%p,%d) returned %d LastError=%d", ptr
, sz
, rc
, GetLastError());
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
);
349 static int sqlcipher_mem_size(void *p
) {
350 return default_mem_methods
.xSize(p
);
352 static void sqlcipher_mem_free(void *p
) {
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
) {
366 if(sqlcipher_mem_security_on
) {
367 orig_sz
= sqlcipher_mem_size(p
);
369 sqlcipher_mem_free(p
);
372 return sqlcipher_mem_malloc(n
);
373 } else if(n
<= orig_sz
) {
376 new = sqlcipher_mem_malloc(n
);
378 memcpy(new, p
, orig_sz
);
379 sqlcipher_mem_free(p
);
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
,
395 sqlcipher_mem_realloc
,
397 sqlcipher_mem_roundup
,
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;
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");
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) {
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;
466 sqlcipher_log_file
= stderr
;
469 sqlcipher_log_set
= 1;
474 /* check to see if there is a provider registered at this point
475 if there no provider registered at this point, register the
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
);
492 #error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
494 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_CORE
, "sqlcipher_activate: calling sqlcipher_register_provider(%p)", p
);
496 sqlcipher_ext_provider_setup(p
);
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");
531 sqlcipher_ext_provider_destroy();
534 /* last connection closed, free mutexes */
535 if(sqlcipher_activate_count
== 0) {
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
++) {
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
);
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
) {
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
);
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
));
628 char *version
= sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER
), CIPHER_XSTR(CIPHER_VERSION_BUILD
));
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
) {
642 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_MEMORY
, "sqlcipher_cipher_ctx_init: allocating context");
643 *iCtx
= (cipher_ctx
*) sqlcipher_malloc(sizeof(cipher_ctx
));
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
;
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
;
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
) {
703 c1
->pass_sz
== c2
->pass_sz
706 || !sqlcipher_memcmp((const unsigned char*)c1
->pass
,
707 (const unsigned char*)c2
->pass
,
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",
713 (c1
->pass
== NULL
|| c2
->pass
== NULL
) ?
716 (const unsigned char*)c1
->pass
,
717 (const unsigned char*)c2
->pass
,
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
);
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] = '\'';
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
);
801 if(zKey
&& nKey
) { /* if new password is provided, copy it */
803 ctx
->pass
= sqlcipher_malloc(nKey
);
804 if(ctx
->pass
== NULL
) return SQLITE_NOMEM
;
805 memcpy(ctx
->pass
, zKey
, nKey
);
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
;
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
);
819 c_ctx
->derive_key
= 1;
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
);
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);
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);
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
) {
852 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_HMAC
);
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
;
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
);
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
;
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");
907 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_HAS_KDF_SALT
);
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
);
917 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size
);
921 static int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx
*ctx
, void** salt
) {
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
;
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");
938 /* attempt to free the existing page buffer */
939 if(ctx
->buffer
) sqlcipher_free(ctx
->buffer
,ctx
->page_sz
);
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
;
951 static int sqlcipher_codec_ctx_init(codec_ctx
**iCtx
, Db
*pDb
, Pager
*pPager
, const void *zKey
, int nKey
) {
955 sqlcipher_log(SQLCIPHER_LOG_DEBUG
, SQLCIPHER_LOG_MEMORY
, "sqlcipher_codec_ctx_init: allocating context");
957 *iCtx
= sqlcipher_malloc(sizeof(codec_ctx
));
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
);
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
);
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
);
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
);
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
);
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
));
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
);
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
);
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
);
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
);
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
);
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
);
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
);
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
) {
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
,
1136 in_sz
, (unsigned char*) &pgno_raw
,
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
;
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
;
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
);
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
);
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
);
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
);
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
);
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
);
1218 CODEC_HEXDUMP("sqlcipher_page_cipher: output page data", out_start
, page_sz
);
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
) {
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
);
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
);
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
);
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
) {
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;
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
;
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);
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
);
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
) {
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);
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));
1397 /* cleanup will finalize open statement */
1400 if(statement
) sqlite3_finalize(statement
);
1401 if(db
) sqlite3_close(db
);
1405 static int sqlcipher_codec_ctx_integrity_check(codec_ctx
*ctx
, Parse
*pParse
, char *column
) {
1409 unsigned char *hmac_out
= NULL
;
1410 sqlite3_file
*fd
= sqlite3PagerFile(ctx
->pBt
->pBt
->pPager
);
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);
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);
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);
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;
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);
1477 if(hmac_out
!= NULL
) sqlcipher_free(hmac_out
, ctx
->hmac_sz
);
1481 static int sqlcipher_codec_ctx_migrate(codec_ctx
*ctx
) {
1482 int i
, pass_sz
, keyspec_sz
, nRes
, user_version
, rc
, oflags
;
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;
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");
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
);
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 ");
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
);
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
);
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
);
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
);
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
);
1569 #ifdef SQLCIPHER_TEST
1570 if((cipher_test_flags
& TEST_FAIL_MIGRATE
) > 0) {
1572 sqlcipher_log(SQLCIPHER_LOG_WARN
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_ctx_migrate: simulated migrate failure, error code %d", rc
);
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
);
1583 if( !db
->autoCommit
){
1584 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_ctx_migrate: cannot migrate from within a transaction");
1587 if( db
->nVdbeActive
>1 ){
1588 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_ctx_migrate: cannot migrate - SQL statements in progress");
1592 pDest
= db
->aDb
[0].pBt
;
1593 pDb
= &(db
->aDb
[db
->nDb
-1]);
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
);
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
)) {
1628 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc
);
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
);
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
);
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
);
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
);
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
);
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
);
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 ' */
1701 sqlite3StrNICmp((const char *)zRight
,"x'", 2) == 0 &&
1702 sqlite3StrNICmp(suffix
, "'", 1) == 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
);
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;
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
));
1737 fprintf(f
, SQLCIPHER_PROFILE_FMT
, elapsed
, sqlite3_sql((sqlite3_stmt
*)stmt
));
1743 static int sqlcipher_cipher_profile(sqlite3
*db
, const char *destination
){
1744 #if defined(SQLITE_OMIT_TRACE)
1745 return SQLITE_ERROR
;
1748 if(sqlite3_stricmp(destination
, "off") == 0){
1749 sqlite3_trace_v2(db
, 0, NULL
, NULL
); /* disable tracing */
1751 if(sqlite3_stricmp(destination
, "stdout") == 0){
1753 }else if(sqlite3_stricmp(destination
, "stderr") == 0){
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 */
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
;
1761 if((f
= fopen(destination
, "a")) == 0) return SQLITE_ERROR
;
1764 sqlite3_trace_v2(db
, SQLITE_TRACE_PROFILE
, sqlcipher_profile_callback
, f
);
1770 static char *sqlcipher_get_log_level_str(unsigned int level
) {
1772 case SQLCIPHER_LOG_ERROR
:
1774 case SQLCIPHER_LOG_WARN
:
1776 case SQLCIPHER_LOG_INFO
:
1778 case SQLCIPHER_LOG_DEBUG
:
1780 case SQLCIPHER_LOG_TRACE
:
1782 case SQLCIPHER_LOG_ALL
:
1788 static char *sqlcipher_get_log_source_str(unsigned int source
) {
1790 case SQLCIPHER_LOG_NONE
:
1792 case SQLCIPHER_LOG_CORE
:
1794 case SQLCIPHER_LOG_MEMORY
:
1796 case SQLCIPHER_LOG_MUTEX
:
1798 case SQLCIPHER_LOG_PROVIDER
:
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
, ...) {
1812 va_start(params
, message
);
1813 char formatted
[MAX_LOG_LEN
];
1818 #if defined(SQLCIPHER_OMIT_LOG_DEVICE)
1819 vfprintf(stderr
, message
, params
);
1820 fprintf(stderr
, "\n");
1823 #if defined(__ANDROID__)
1824 __android_log_vprint(ANDROID_LOG_DEBUG
, "sqlcipher", message
, params
);
1826 #elif defined(__APPLE__)
1827 formatted
= sqlite3_vmprintf(message
, params
);
1828 os_log(OS_LOG_DEFAULT
, "%s", formatted
);
1829 sqlite3_free(formatted
);
1832 vfprintf(stderr
, message
, params
);
1833 fprintf(stderr
, "\n");
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 */
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
);
1856 #elif defined(__APPLEformattes__)
1857 os_log(OS_LOG_DEFAULT
, "%{public}s", formatted
);
1863 if(sqlcipher_log_file
!= NULL
){
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
);
1878 gettimeofday(&tv
, NULL
);
1880 ms
= tv
.tv_usec
/1000.0;
1881 localtime_r(&sec
, &tt
);
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
);
1894 static int sqlcipher_set_log(const char *destination
){
1895 #ifdef SQLCIPHER_OMIT_LOG
1896 return SQLITE_ERROR
;
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
;
1917 if((sqlcipher_log_file
= fopen(destination
, "a")) == 0) return SQLITE_ERROR
;
1920 sqlcipher_log(SQLCIPHER_LOG_INFO
, SQLCIPHER_LOG_CORE
, "sqlcipher_set_log: set log to %s", destination
);
1925 #ifdef SQLCIPHER_EXT
1926 #include "sqlcipher_ext.h"
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
) {
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
);
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
);
1965 codec_ctx
*ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
1968 return sqlcipher_codec_ctx_set_pass(ctx
, zKey
, nKey
, for_ctx
);
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
;
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");
1996 #ifdef SQLCIPHER_TEST
1997 if( sqlite3_stricmp(zLeft
,"cipher_test_on")==0 ){
1999 if(sqlite3_stricmp(zRight
, "fail_encrypt")==0) {
2000 SQLCIPHER_FLAG_SET(cipher_test_flags
,TEST_FAIL_ENCRYPT
);
2002 if(sqlite3_stricmp(zRight
, "fail_decrypt")==0) {
2003 SQLCIPHER_FLAG_SET(cipher_test_flags
,TEST_FAIL_DECRYPT
);
2005 if(sqlite3_stricmp(zRight
, "fail_migrate")==0) {
2006 SQLCIPHER_FLAG_SET(cipher_test_flags
,TEST_FAIL_MIGRATE
);
2010 if( sqlite3_stricmp(zLeft
,"cipher_test_off")==0 ){
2012 if(sqlite3_stricmp(zRight
, "fail_encrypt")==0) {
2013 SQLCIPHER_FLAG_UNSET(cipher_test_flags
,TEST_FAIL_ENCRYPT
);
2015 if(sqlite3_stricmp(zRight
, "fail_decrypt")==0) {
2016 SQLCIPHER_FLAG_UNSET(cipher_test_flags
,TEST_FAIL_DECRYPT
);
2018 if(sqlite3_stricmp(zRight
, "fail_migrate")==0) {
2019 SQLCIPHER_FLAG_UNSET(cipher_test_flags
,TEST_FAIL_MIGRATE
);
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
);
2027 if( sqlite3_stricmp(zLeft
,"cipher_test_rand")==0 ){
2029 int rand
= atoi(zRight
);
2030 cipher_test_rand
= rand
;
2032 char *rand
= sqlite3_mprintf("%d", cipher_test_rand
);
2033 sqlcipher_vdbe_return_string(pParse
, "cipher_test_rand", rand
, P4_DYNAMIC
);
2037 if( sqlite3_stricmp(zLeft
, "cipher_fips_status")== 0 && !zRight
){
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
);
2043 if( sqlite3_stricmp(zLeft
, "cipher_store_pass")==0 && zRight
) {
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
);
2051 if( sqlite3_stricmp(zLeft
, "cipher_store_pass")==0 && !zRight
) {
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
);
2061 if( sqlite3_stricmp(zLeft
, "cipher_add_random")==0 && zRight
){
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
);
2067 if( sqlite3_stricmp(zLeft
, "cipher_migrate")==0 && !zRight
){
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
);
2078 if( sqlite3_stricmp(zLeft
, "cipher_provider")==0 && !zRight
){
2080 sqlcipher_vdbe_return_string(pParse
, "cipher_provider",
2081 ctx
->provider
->get_provider_name(ctx
->provider_ctx
), P4_TRANSIENT
);
2084 if( sqlite3_stricmp(zLeft
, "cipher_provider_version")==0 && !zRight
){
2086 sqlcipher_vdbe_return_string(pParse
, "cipher_provider_version",
2087 ctx
->provider
->get_provider_version(ctx
->provider_ctx
), P4_TRANSIENT
);
2090 if( sqlite3_stricmp(zLeft
, "cipher_version")==0 && !zRight
){
2091 sqlcipher_vdbe_return_string(pParse
, "cipher_version", sqlcipher_version(), P4_DYNAMIC
);
2093 if( sqlite3_stricmp(zLeft
, "cipher")==0 ){
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
);
2100 sqlcipher_vdbe_return_string(pParse
, "cipher",
2101 ctx
->provider
->get_cipher(ctx
->provider_ctx
), P4_TRANSIENT
);
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
);
2110 if( sqlite3_stricmp(zLeft
,"cipher_default_kdf_iter")==0 ){
2112 default_kdf_iter
= atoi(zRight
); /* change default KDF iterations */
2114 char *kdf_iter
= sqlite3_mprintf("%d", default_kdf_iter
);
2115 sqlcipher_vdbe_return_string(pParse
, "cipher_default_kdf_iter", kdf_iter
, P4_DYNAMIC
);
2118 if( sqlite3_stricmp(zLeft
, "kdf_iter")==0 ){
2121 sqlcipher_codec_ctx_set_kdf_iter(ctx
, atoi(zRight
)); /* change of RW PBKDF2 iteration */
2123 char *kdf_iter
= sqlite3_mprintf("%d", ctx
->kdf_iter
);
2124 sqlcipher_vdbe_return_string(pParse
, "kdf_iter", kdf_iter
, P4_DYNAMIC
);
2128 if( sqlite3_stricmp(zLeft
, "fast_kdf_iter")==0){
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
);
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
);
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
);
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. */
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
);
2160 char * page_size
= sqlite3_mprintf("%d", ctx
->page_sz
);
2161 sqlcipher_vdbe_return_string(pParse
, "cipher_page_size", page_size
, P4_DYNAMIC
);
2164 return 0; /* return early so that the PragTyp_PAGE_SIZE case logic in pragma.c will take effect */
2167 if( sqlite3_stricmp(zLeft
,"cipher_default_page_size")==0 ){
2169 default_page_size
= atoi(zRight
);
2171 char *page_size
= sqlite3_mprintf("%d", default_page_size
);
2172 sqlcipher_vdbe_return_string(pParse
, "cipher_default_page_size", page_size
, P4_DYNAMIC
);
2175 if( sqlite3_stricmp(zLeft
,"cipher_default_use_hmac")==0 ){
2177 sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight
,1));
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
);
2183 if( sqlite3_stricmp(zLeft
,"cipher_use_hmac")==0 ){
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
);
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
);
2197 if( sqlite3_stricmp(zLeft
,"cipher_hmac_pgno")==0 ){
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
);
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
);
2221 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_pgno", "native", P4_TRANSIENT
);
2226 if( sqlite3_stricmp(zLeft
,"cipher_hmac_salt_mask")==0 ){
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
);
2239 char *mask
= sqlite3_mprintf("%02x", hmac_salt_mask
);
2240 sqlcipher_vdbe_return_string(pParse
, "cipher_hmac_salt_mask", mask
, P4_DYNAMIC
);
2244 if( sqlite3_stricmp(zLeft
,"cipher_plaintext_header_size")==0 ){
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
);
2252 char *size
= sqlite3_mprintf("%d", ctx
->plaintext_header_sz
);
2253 sqlcipher_vdbe_return_string(pParse
, "cipher_plaintext_header_size", size
, P4_DYNAMIC
);
2257 if( sqlite3_stricmp(zLeft
,"cipher_default_plaintext_header_size")==0 ){
2259 default_plaintext_header_size
= atoi(zRight
);
2261 char *size
= sqlite3_mprintf("%d", default_plaintext_header_size
);
2262 sqlcipher_vdbe_return_string(pParse
, "cipher_default_plaintext_header_size", size
, P4_DYNAMIC
);
2265 if( sqlite3_stricmp(zLeft
,"cipher_salt")==0 ){
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
);
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
);
2282 sqlite3_free(hexsalt
);
2283 sqlcipher_codec_ctx_set_error(ctx
, rc
);
2288 if( sqlite3_stricmp(zLeft
,"cipher_hmac_algorithm")==0 ){
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
);
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
);
2314 if( sqlite3_stricmp(zLeft
,"cipher_default_hmac_algorithm")==0 ){
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
;
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
);
2334 if( sqlite3_stricmp(zLeft
,"cipher_kdf_algorithm")==0 ){
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
);
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
);
2357 if( sqlite3_stricmp(zLeft
,"cipher_default_kdf_algorithm")==0 ){
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
;
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
);
2377 if( sqlite3_stricmp(zLeft
,"cipher_compatibility")==0 ){
2380 int version
= atoi(zRight
);
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
);
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
);
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
);
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
);
2436 rc
= codec_set_btree_to_codec_pagesize(db
, pDb
, ctx
);
2437 if (rc
!= SQLITE_OK
) sqlcipher_codec_ctx_set_error(ctx
, SQLITE_ERROR
);
2441 if( sqlite3_stricmp(zLeft
,"cipher_default_compatibility")==0 ){
2443 int version
= atoi(zRight
);
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);
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);
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);
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);
2479 if( sqlite3_stricmp(zLeft
,"cipher_memory_security")==0 ){
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;
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
);
2497 if( sqlite3_stricmp(zLeft
,"cipher_settings")==0 ){
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
;
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
;
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
);
2538 if( sqlite3_stricmp(zLeft
,"cipher_default_settings")==0 ){
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
);
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
);
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
);
2573 if( sqlite3_stricmp(zLeft
,"cipher_integrity_check")==0 ){
2575 sqlcipher_codec_ctx_integrity_check(ctx
, pParse
, "cipher_integrity_check");
2578 if( sqlite3_stricmp(zLeft
, "cipher_log_level")==0 ){
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
);
2589 if( sqlite3_stricmp(zLeft
, "cipher_log_source")==0 ){
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
);
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
);
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
;
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
);
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
);
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
);
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()) {
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
);
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
);
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 */
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
);
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()) {
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
);
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
);
2713 SQLCIPHER_FLAG_SET(ctx
->flags
, CIPHER_FLAG_KEY_USED
);
2714 return ctx
->buffer
; /* return persistent buffer data, pData remains intact */
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 */
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
) {
2739 Pager
*pPager
= pDb
->pBt
->pBt
->pPager
;
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");
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
);
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
);
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 */
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
);
2811 int sqlcipher_find_db_index(sqlite3
*db
, const char *zDb
) {
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) {
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
);
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
);
2871 Pager
*pPager
= pDb
->pBt
->pBt
->pPager
;
2873 ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
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
);
2903 sqlcipher_log(SQLCIPHER_LOG_ERROR
, SQLCIPHER_LOG_CORE
, "sqlite3_rekey_v2: error %d occurred writing page %d", rc
, pgno
);
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
);
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
);
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
);
2935 codec_ctx
*ctx
= (codec_ctx
*) sqlcipherPagerGetCodec(pDb
->pBt
->pBt
->pPager
);
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
;
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
){
2976 rc
= sqlite3VdbeFinalize((Vdbe
*)pStmt
);
2978 sqlite3SetString(pzErrMsg
, db
, sqlite3_errmsg(db
));
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
;
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
;
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
);
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) {
3047 pzErrMsg
= sqlite3_mprintf("invalid number of arguments (%d) passed to sqlcipher_export", argc
);
3051 if(sqlite3_value_type(argv
[0]) == SQLITE_NULL
) {
3053 pzErrMsg
= sqlite3_mprintf("target database can't be NULL");
3057 targetDb
= (const char*) sqlite3_value_text(argv
[0]);
3061 if(sqlite3_value_type(argv
[1]) == SQLITE_NULL
) {
3063 pzErrMsg
= sqlite3_mprintf("target database can't be NULL");
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) {
3075 pzErrMsg
= sqlite3_mprintf("unknown database %s", targetDb
);
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
);
3085 /* Query the schema of the main database. Create a mirror schema
3086 ** in the temporary database.
3088 zSql
= sqlite3_mprintf(
3090 " FROM %s.sqlite_schema WHERE type='table' AND name!='sqlite_sequence'"
3093 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
3094 if( rc
!=SQLITE_OK
) goto end_of_export
;
3097 zSql
= sqlite3_mprintf(
3099 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE INDEX %%' "
3101 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
3102 if( rc
!=SQLITE_OK
) goto end_of_export
;
3105 zSql
= sqlite3_mprintf(
3107 " FROM %s.sqlite_schema WHERE sql LIKE 'CREATE UNIQUE INDEX %%'"
3109 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
3110 if( rc
!=SQLITE_OK
) goto end_of_export
;
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' "
3123 , targetDb
, sourceDb
, sourceDb
);
3124 rc
= (zSql
== NULL
) ? SQLITE_NOMEM
: sqlcipher_execExecSql(db
, &pzErrMsg
, zSql
);
3125 if( rc
!=SQLITE_OK
) goto end_of_export
;
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
;
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
;
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
);
3167 if(pzErrMsg
!= NULL
) {
3168 sqlite3_result_error(context
, pzErrMsg
, -1);
3169 sqlite3DbFree(db
, pzErrMsg
);
3171 sqlite3_result_error(context
, sqlite3ErrStr(rc
), -1);