correct function name in sqlcipher_mlock log output
[sqlcipher.git] / src / crypto_impl.c
blob1c25ed3d625d68851abfa54ef5373006b2ad70aa
1 /*
2 ** SQLCipher
3 ** http://sqlcipher.net
4 **
5 ** Copyright (c) 2008 - 2013, ZETETIC LLC
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions are met:
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in the
14 ** documentation and/or other materials provided with the distribution.
15 ** * Neither the name of the ZETETIC LLC nor the
16 ** names of its contributors may be used to endorse or promote products
17 ** derived from this software without specific prior written permission.
18 **
19 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
20 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
23 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 **
31 /* BEGIN SQLCIPHER */
32 #ifdef SQLITE_HAS_CODEC
34 #include "sqlcipher.h"
35 #include "crypto.h"
37 #ifdef SQLCIPHER_TEST
38 static volatile unsigned int cipher_test_flags = 0;
39 unsigned int sqlcipher_get_test_flags() {
40 return cipher_test_flags;
42 void sqlcipher_set_test_flags(unsigned int flags) {
43 cipher_test_flags = flags;
46 static volatile int cipher_test_rand = 0;
47 int sqlcipher_get_test_rand() {
48 return cipher_test_rand;
50 void sqlcipher_set_test_rand(int rand) {
51 cipher_test_rand = rand;
53 int sqlcipher_get_test_fail() {
54 int x;
56 /* if cipher_test_rand is not set to a non-zero value always fail (return true) */
57 if (cipher_test_rand == 0) return 1;
59 sqlite3_randomness(sizeof(x), &x);
60 return ((x % cipher_test_rand) == 0);
62 #endif
64 /* Generate code to return a string value */
66 static volatile unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
67 static volatile unsigned char hmac_salt_mask = HMAC_SALT_MASK;
68 static volatile int default_kdf_iter = PBKDF2_ITER;
69 static volatile int default_page_size = 4096;
70 static volatile int default_plaintext_header_sz = 0;
71 static volatile int default_hmac_algorithm = SQLCIPHER_HMAC_SHA512;
72 static volatile int default_kdf_algorithm = SQLCIPHER_PBKDF2_HMAC_SHA512;
73 static volatile int sqlcipher_mem_security_on = 0;
74 static volatile int sqlcipher_mem_executed = 0;
75 static volatile int sqlcipher_mem_initialized = 0;
76 static volatile unsigned int sqlcipher_activate_count = 0;
77 static volatile sqlite3_mem_methods default_mem_methods;
78 static sqlcipher_provider *default_provider = NULL;
80 static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
81 static FILE* sqlcipher_log_file = NULL;
82 static volatile int sqlcipher_log_device = 0;
83 static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE;
84 static volatile int sqlcipher_log_set = 0;
86 sqlite3_mutex* sqlcipher_mutex(int mutex) {
87 if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
88 return sqlcipher_static_mutex[mutex];
91 static int sqlcipher_mem_init(void *pAppData) {
92 return default_mem_methods.xInit(pAppData);
94 static void sqlcipher_mem_shutdown(void *pAppData) {
95 default_mem_methods.xShutdown(pAppData);
97 static void *sqlcipher_mem_malloc(int n) {
98 void *ptr = default_mem_methods.xMalloc(n);
99 if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
100 if(sqlcipher_mem_security_on) {
101 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_malloc: calling sqlcipher_mlock(%p,%d)", ptr, n);
102 sqlcipher_mlock(ptr, n);
104 return ptr;
106 static int sqlcipher_mem_size(void *p) {
107 return default_mem_methods.xSize(p);
109 static void sqlcipher_mem_free(void *p) {
110 int sz;
111 if(!sqlcipher_mem_executed) sqlcipher_mem_executed = 1;
112 if(sqlcipher_mem_security_on) {
113 sz = sqlcipher_mem_size(p);
114 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_free: calling sqlcipher_memset(%p,0,%d) and sqlcipher_munlock(%p, %d)", p, sz, p, sz);
115 sqlcipher_memset(p, 0, sz);
116 sqlcipher_munlock(p, sz);
118 default_mem_methods.xFree(p);
120 static void *sqlcipher_mem_realloc(void *p, int n) {
121 void *new = NULL;
122 int orig_sz = 0;
123 if(sqlcipher_mem_security_on) {
124 orig_sz = sqlcipher_mem_size(p);
125 if (n==0) {
126 sqlcipher_mem_free(p);
127 return NULL;
128 } else if (!p) {
129 return sqlcipher_mem_malloc(n);
130 } else if(n <= orig_sz) {
131 return p;
132 } else {
133 new = sqlcipher_mem_malloc(n);
134 if(new) {
135 memcpy(new, p, orig_sz);
136 sqlcipher_mem_free(p);
138 return new;
140 } else {
141 return default_mem_methods.xRealloc(p, n);
145 static int sqlcipher_mem_roundup(int n) {
146 return default_mem_methods.xRoundup(n);
149 static sqlite3_mem_methods sqlcipher_mem_methods = {
150 sqlcipher_mem_malloc,
151 sqlcipher_mem_free,
152 sqlcipher_mem_realloc,
153 sqlcipher_mem_size,
154 sqlcipher_mem_roundup,
155 sqlcipher_mem_init,
156 sqlcipher_mem_shutdown,
160 void sqlcipher_init_memmethods() {
161 if(sqlcipher_mem_initialized) return;
162 if(sqlite3_config(SQLITE_CONFIG_GETMALLOC, &default_mem_methods) != SQLITE_OK ||
163 sqlite3_config(SQLITE_CONFIG_MALLOC, &sqlcipher_mem_methods) != SQLITE_OK) {
164 sqlcipher_mem_security_on = sqlcipher_mem_executed = sqlcipher_mem_initialized = 0;
165 } else {
166 sqlcipher_mem_initialized = 1;
170 int sqlcipher_register_provider(sqlcipher_provider *p) {
171 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_register_provider: entering SQLCIPHER_MUTEX_PROVIDER");
172 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
173 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_register_provider: entered SQLCIPHER_MUTEX_PROVIDER");
175 if(default_provider != NULL && default_provider != p) {
176 /* only free the current registerd provider if it has been initialized
177 and it isn't a pointer to the same provider passed to the function
178 (i.e. protect against a caller calling register twice for the same provider) */
179 sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
181 default_provider = p;
182 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_register_provider: leaving SQLCIPHER_MUTEX_PROVIDER");
183 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
184 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_register_provider: left SQLCIPHER_MUTEX_PROVIDER");
186 return SQLITE_OK;
189 /* return a pointer to the currently registered provider. This will
190 allow an application to fetch the current registered provider and
191 make minor changes to it */
192 sqlcipher_provider* sqlcipher_get_provider() {
193 return default_provider;
196 void sqlcipher_activate() {
197 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_activate: entering static master mutex");
198 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
199 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_activate: entered static master mutex");
201 /* allocate new mutexes */
202 if(sqlcipher_activate_count == 0) {
203 int i;
204 for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) {
205 sqlcipher_static_mutex[i] = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
207 #ifndef SQLCIPHER_OMIT_DEFAULT_LOGGING
208 /* when sqlcipher is first activated, set a default log target and level of WARN. Use the "device log"
209 for android (logcat) or apple (console). Use stderr on all other platforms. */
210 if(!sqlcipher_log_set) {
211 sqlcipher_log_level = SQLCIPHER_LOG_WARN;
212 #if defined(__ANDROID__) || defined(__APPLE_)
213 sqlcipher_log_device = 1;
214 #else
215 sqlcipher_log_file = stderr;
216 #endif
217 sqlcipher_log_set = 1;
219 #endif
222 /* check to see if there is a provider registered at this point
223 if there no provider registered at this point, register the
224 default provider */
225 if(sqlcipher_get_provider() == NULL) {
226 sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider));
227 #if defined (SQLCIPHER_CRYPTO_CC)
228 extern int sqlcipher_cc_setup(sqlcipher_provider *p);
229 sqlcipher_cc_setup(p);
230 #elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT)
231 extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
232 sqlcipher_ltc_setup(p);
233 #elif defined (SQLCIPHER_CRYPTO_NSS)
234 extern int sqlcipher_nss_setup(sqlcipher_provider *p);
235 sqlcipher_nss_setup(p);
236 #elif defined (SQLCIPHER_CRYPTO_OPENSSL)
237 extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
238 sqlcipher_openssl_setup(p);
239 #else
240 #error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
241 #endif
242 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_activate: calling sqlcipher_register_provider(%p)", p);
243 #ifdef SQLCIPHER_EXT
244 sqlcipher_ext_provider_setup(p);
245 #endif
246 sqlcipher_register_provider(p);
247 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_activate: called sqlcipher_register_provider(%p)",p);
250 sqlcipher_activate_count++; /* increment activation count */
252 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_activate: leaving static master mutex");
253 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
254 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_activate: left static master mutex");
257 void sqlcipher_deactivate() {
258 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: entering static master mutex");
259 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
260 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: entered static master mutex");
262 sqlcipher_activate_count--;
263 /* if no connections are using sqlcipher, cleanup globals */
264 if(sqlcipher_activate_count < 1) {
265 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: entering SQLCIPHER_MUTEX_PROVIDER");
266 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
267 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: entered SQLCIPHER_MUTEX_PROVIDER");
269 if(default_provider != NULL) {
270 sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
271 default_provider = NULL;
274 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER");
275 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
276 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: left SQLCIPHER_MUTEX_PROVIDER");
278 #ifdef SQLCIPHER_EXT
279 sqlcipher_ext_provider_destroy();
280 #endif
282 /* last connection closed, free mutexes */
283 if(sqlcipher_activate_count == 0) {
284 int i;
285 for(i = 0; i < SQLCIPHER_MUTEX_COUNT; i++) {
286 sqlite3_mutex_free(sqlcipher_static_mutex[i]);
289 sqlcipher_activate_count = 0; /* reset activation count */
292 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: leaving static master mutex");
293 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
294 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_deactivate: left static master mutex");
297 /* constant time memset using volitile to avoid having the memset
298 optimized out by the compiler.
299 Note: As suggested by Joachim Schipper (joachim.schipper@fox-it.com)
301 void* sqlcipher_memset(void *v, unsigned char value, sqlite_uint64 len) {
302 volatile sqlite_uint64 i = 0;
303 volatile unsigned char *a = v;
305 if (v == NULL) return v;
307 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_memset: setting %p[0-%llu]=%d)", a, len, value);
308 for(i = 0; i < len; i++) {
309 a[i] = value;
312 return v;
315 /* constant time memory check tests every position of a memory segement
316 matches a single value (i.e. the memory is all zeros)
317 returns 0 if match, 1 of no match */
318 int sqlcipher_ismemset(const void *v, unsigned char value, sqlite_uint64 len) {
319 const volatile unsigned char *a = v;
320 volatile sqlite_uint64 i = 0, result = 0;
322 for(i = 0; i < len; i++) {
323 result |= a[i] ^ value;
326 return (result != 0);
329 /* constant time memory comparison routine.
330 returns 0 if match, 1 if no match */
331 int sqlcipher_memcmp(const void *v0, const void *v1, int len) {
332 const volatile unsigned char *a0 = v0, *a1 = v1;
333 volatile int i = 0, result = 0;
335 for(i = 0; i < len; i++) {
336 result |= a0[i] ^ a1[i];
339 return (result != 0);
342 void sqlcipher_mlock(void *ptr, sqlite_uint64 sz) {
343 #ifndef OMIT_MEMLOCK
344 #if defined(__unix__) || defined(__APPLE__)
345 int rc;
346 unsigned long pagesize = sysconf(_SC_PAGESIZE);
347 unsigned long offset = (unsigned long) ptr % pagesize;
349 if(ptr == NULL || sz == 0) return;
351 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mlock: calling mlock(%p,%lu); _SC_PAGESIZE=%lu", ptr - offset, sz + offset, pagesize);
352 rc = mlock(ptr - offset, sz + offset);
353 if(rc!=0) {
354 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_mlock: mlock() returned %d errno=%d", rc, errno);
355 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_mlock: mlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno);
357 #elif defined(_WIN32)
358 #if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
359 int rc;
360 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mlock: calling VirtualLock(%p,%d)", ptr, sz);
361 rc = VirtualLock(ptr, sz);
362 if(rc==0) {
363 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_mlock: VirtualLock() returned %d LastError=%d", rc, GetLastError());
364 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_mlock: VirtualLock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError());
366 #endif
367 #endif
368 #endif
371 void sqlcipher_munlock(void *ptr, sqlite_uint64 sz) {
372 #ifndef OMIT_MEMLOCK
373 #if defined(__unix__) || defined(__APPLE__)
374 int rc;
375 unsigned long pagesize = sysconf(_SC_PAGESIZE);
376 unsigned long offset = (unsigned long) ptr % pagesize;
378 if(ptr == NULL || sz == 0) return;
380 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_unlock: calling munlock(%p,%lu)", ptr - offset, sz + offset);
381 rc = munlock(ptr - offset, sz + offset);
382 if(rc!=0) {
383 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_mem_unlock: munlock() returned %d errno=%d", rc, errno);
384 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_mem_unlock: munlock(%p,%lu) returned %d errno=%d", ptr - offset, sz + offset, rc, errno);
386 #elif defined(_WIN32)
387 #if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
388 int rc;
389 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_mem_lock: calling VirtualUnlock(%p,%d)", ptr, sz);
390 rc = VirtualUnlock(ptr, sz);
391 if(!rc) {
392 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_mem_unlock: VirtualUnlock() returned %d LastError=%d", rc, GetLastError());
393 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_mem_unlock: VirtualUnlock(%p,%d) returned %d LastError=%d", ptr, sz, rc, GetLastError());
395 #endif
396 #endif
397 #endif
401 * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory
402 * can be countend and memory leak detection works in the test suite.
403 * If ptr is not null memory will be freed.
404 * If sz is greater than zero, the memory will be overwritten with zero before it is freed
405 * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the
406 * memory segment so it can be paged
408 void sqlcipher_free(void *ptr, sqlite_uint64 sz) {
409 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_free: calling sqlcipher_memset(%p,0,%llu)", ptr, sz);
410 sqlcipher_memset(ptr, 0, sz);
411 sqlcipher_munlock(ptr, sz);
412 sqlite3_free(ptr);
416 * allocate memory. Uses sqlite's internall malloc wrapper so memory can be
417 * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK
418 * attempts to lock the memory pages so sensitive information won't be swapped
420 void* sqlcipher_malloc(sqlite_uint64 sz) {
421 void *ptr;
422 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_malloc: calling sqlite3Malloc(%llu)", sz);
423 ptr = sqlite3Malloc(sz);
424 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_malloc: calling sqlcipher_memset(%p,0,%llu)", ptr, sz);
425 sqlcipher_memset(ptr, 0, sz);
426 sqlcipher_mlock(ptr, sz);
427 return ptr;
430 char* sqlcipher_version() {
431 #ifdef CIPHER_VERSION_QUALIFIER
432 char *version = sqlite3_mprintf("%s %s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_QUALIFIER), CIPHER_XSTR(CIPHER_VERSION_BUILD));
433 #else
434 char *version = sqlite3_mprintf("%s %s", CIPHER_XSTR(CIPHER_VERSION_NUMBER), CIPHER_XSTR(CIPHER_VERSION_BUILD));
435 #endif
436 return version;
440 * Initialize new cipher_ctx struct. This function will allocate memory
441 * for the cipher context and for the key
443 * returns SQLITE_OK if initialization was successful
444 * returns SQLITE_NOMEM if an error occured allocating memory
446 static int sqlcipher_cipher_ctx_init(codec_ctx *ctx, cipher_ctx **iCtx) {
447 cipher_ctx *c_ctx;
448 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_init: allocating context");
449 *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx));
450 c_ctx = *iCtx;
451 if(c_ctx == NULL) return SQLITE_NOMEM;
453 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_init: allocating key");
454 c_ctx->key = (unsigned char *) sqlcipher_malloc(ctx->key_sz);
456 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_init: allocating hmac_key");
457 c_ctx->hmac_key = (unsigned char *) sqlcipher_malloc(ctx->key_sz);
459 if(c_ctx->key == NULL) return SQLITE_NOMEM;
460 if(c_ctx->hmac_key == NULL) return SQLITE_NOMEM;
462 return SQLITE_OK;
466 * Free and wipe memory associated with a cipher_ctx
468 static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) {
469 cipher_ctx *c_ctx = *iCtx;
470 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "cipher_ctx_free: iCtx=%p", iCtx);
471 if(c_ctx->key) sqlcipher_free(c_ctx->key, ctx->key_sz);
472 if(c_ctx->hmac_key) sqlcipher_free(c_ctx->hmac_key, ctx->key_sz);
473 if(c_ctx->pass) sqlcipher_free(c_ctx->pass, c_ctx->pass_sz);
474 if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
475 sqlcipher_free(c_ctx, sizeof(cipher_ctx));
478 static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
479 int base_reserve = ctx->iv_sz; /* base reserve size will be IV only */
480 int reserve = base_reserve;
482 ctx->hmac_sz = ctx->provider->get_hmac_sz(ctx->provider_ctx, ctx->hmac_algorithm);
484 if(sqlcipher_codec_ctx_get_use_hmac(ctx))
485 reserve += ctx->hmac_sz; /* if reserve will include hmac, update that size */
487 /* calculate the amount of reserve needed in even increments of the cipher block size */
488 if(ctx->block_sz > 0) {
489 reserve = ((reserve % ctx->block_sz) == 0) ? reserve :
490 ((reserve / ctx->block_sz) + 1) * ctx->block_sz;
493 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d",
494 base_reserve, ctx->block_sz, ctx->hmac_sz, reserve);
496 ctx->reserve_sz = reserve;
498 return SQLITE_OK;
502 * Compare one cipher_ctx to another.
504 * returns 0 if all the parameters (except the derived key data) are the same
505 * returns 1 otherwise
507 static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
508 int are_equal = (
509 c1->pass_sz == c2->pass_sz
510 && (
511 c1->pass == c2->pass
512 || !sqlcipher_memcmp((const unsigned char*)c1->pass,
513 (const unsigned char*)c2->pass,
514 c1->pass_sz)
517 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_cmp: c1=%p c2=%p sqlcipher_memcmp(c1->pass, c2_pass)=%d are_equal=%d",
518 c1, c2,
519 (c1->pass == NULL || c2->pass == NULL) ?
520 -1 :
521 sqlcipher_memcmp(
522 (const unsigned char*)c1->pass,
523 (const unsigned char*)c2->pass,
524 c1->pass_sz
526 are_equal
529 return !are_equal; /* return 0 if they are the same, 1 otherwise */
533 * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a
534 * fully initialized context, you could copy it to write_ctx and all yet data
535 * and pass information across
537 * returns SQLITE_OK if initialization was successful
538 * returns SQLITE_NOMEM if an error occured allocating memory
540 static int sqlcipher_cipher_ctx_copy(codec_ctx *ctx, cipher_ctx *target, cipher_ctx *source) {
541 void *key = target->key;
542 void *hmac_key = target->hmac_key;
544 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_copy: target=%p, source=%p", target, source);
545 if(target->pass) sqlcipher_free(target->pass, target->pass_sz);
546 if(target->keyspec) sqlcipher_free(target->keyspec, ctx->keyspec_sz);
547 memcpy(target, source, sizeof(cipher_ctx));
549 target->key = key; /* restore pointer to previously allocated key data */
550 memcpy(target->key, source->key, ctx->key_sz);
552 target->hmac_key = hmac_key; /* restore pointer to previously allocated hmac key data */
553 memcpy(target->hmac_key, source->hmac_key, ctx->key_sz);
555 if(source->pass && source->pass_sz) {
556 target->pass = sqlcipher_malloc(source->pass_sz);
557 if(target->pass == NULL) return SQLITE_NOMEM;
558 memcpy(target->pass, source->pass, source->pass_sz);
560 if(source->keyspec) {
561 target->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
562 if(target->keyspec == NULL) return SQLITE_NOMEM;
563 memcpy(target->keyspec, source->keyspec, ctx->keyspec_sz);
565 return SQLITE_OK;
569 * Set the keyspec for the cipher_ctx
571 * returns SQLITE_OK if assignment was successfull
572 * returns SQLITE_NOMEM if an error occured allocating memory
574 static int sqlcipher_cipher_ctx_set_keyspec(codec_ctx *ctx, cipher_ctx *c_ctx, const unsigned char *key) {
575 /* free, zero existing pointers and size */
576 if(c_ctx->keyspec) sqlcipher_free(c_ctx->keyspec, ctx->keyspec_sz);
577 c_ctx->keyspec = NULL;
579 c_ctx->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
580 if(c_ctx->keyspec == NULL) return SQLITE_NOMEM;
582 c_ctx->keyspec[0] = 'x';
583 c_ctx->keyspec[1] = '\'';
584 cipher_bin2hex(key, ctx->key_sz, c_ctx->keyspec + 2);
585 cipher_bin2hex(ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->keyspec + (ctx->key_sz * 2) + 2);
586 c_ctx->keyspec[ctx->keyspec_sz - 1] = '\'';
587 return SQLITE_OK;
590 int sqlcipher_codec_get_store_pass(codec_ctx *ctx) {
591 return ctx->store_pass;
594 void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value) {
595 ctx->store_pass = value;
598 void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey) {
599 *zKey = ctx->read_ctx->pass;
600 *nKey = ctx->read_ctx->pass_sz;
603 static void sqlcipher_set_derive_key(codec_ctx *ctx, int derive) {
604 if(ctx->read_ctx != NULL) ctx->read_ctx->derive_key = derive;
605 if(ctx->write_ctx != NULL) ctx->write_ctx->derive_key = derive;
609 * Set the passphrase for the cipher_ctx
611 * returns SQLITE_OK if assignment was successfull
612 * returns SQLITE_NOMEM if an error occured allocating memory
614 static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
615 /* free, zero existing pointers and size */
616 if(ctx->pass) sqlcipher_free(ctx->pass, ctx->pass_sz);
617 ctx->pass = NULL;
618 ctx->pass_sz = 0;
620 if(zKey && nKey) { /* if new password is provided, copy it */
621 ctx->pass_sz = nKey;
622 ctx->pass = sqlcipher_malloc(nKey);
623 if(ctx->pass == NULL) return SQLITE_NOMEM;
624 memcpy(ctx->pass, zKey, nKey);
626 return SQLITE_OK;
629 int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) {
630 cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
631 int rc;
633 if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) {
634 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_set_pass", rc);
635 return rc;
638 c_ctx->derive_key = 1;
640 if(for_ctx == 2) {
641 if((rc = sqlcipher_cipher_ctx_copy(ctx, for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK) {
642 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_pass: error %d from sqlcipher_cipher_ctx_copy", rc);
643 return rc;
647 return SQLITE_OK;
650 const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx) {
651 return ctx->provider->get_cipher(ctx->provider_ctx);
654 /* set the global default KDF iteration */
655 void sqlcipher_set_default_kdf_iter(int iter) {
656 default_kdf_iter = iter;
659 int sqlcipher_get_default_kdf_iter() {
660 return default_kdf_iter;
663 int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter) {
664 ctx->kdf_iter = kdf_iter;
665 sqlcipher_set_derive_key(ctx, 1);
666 return SQLITE_OK;
669 int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx) {
670 return ctx->kdf_iter;
673 int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter) {
674 ctx->fast_kdf_iter = fast_kdf_iter;
675 sqlcipher_set_derive_key(ctx, 1);
676 return SQLITE_OK;
679 int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *ctx) {
680 return ctx->fast_kdf_iter;
683 /* set the global default flag for HMAC */
684 void sqlcipher_set_default_use_hmac(int use) {
685 if(use) SQLCIPHER_FLAG_SET(default_flags, CIPHER_FLAG_HMAC);
686 else SQLCIPHER_FLAG_UNSET(default_flags,CIPHER_FLAG_HMAC);
689 int sqlcipher_get_default_use_hmac() {
690 return SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC);
693 void sqlcipher_set_hmac_salt_mask(unsigned char mask) {
694 hmac_salt_mask = mask;
697 unsigned char sqlcipher_get_hmac_salt_mask() {
698 return hmac_salt_mask;
701 /* set the codec flag for whether this individual database should be using hmac */
702 int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
703 if(use) {
704 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC);
705 } else {
706 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC);
709 return sqlcipher_codec_ctx_reserve_setup(ctx);
712 int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx) {
713 return SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC);
716 /* the length of plaintext header size must be:
717 * 1. greater than or equal to zero
718 * 2. a multiple of the cipher block size
719 * 3. less than the usable size of the first database page
721 int sqlcipher_set_default_plaintext_header_size(int size) {
722 default_plaintext_header_sz = size;
723 return SQLITE_OK;
726 int sqlcipher_codec_ctx_set_plaintext_header_size(codec_ctx *ctx, int size) {
727 if(size >= 0 && ctx->block_sz > 0 && (size % ctx->block_sz) == 0 && size < (ctx->page_sz - ctx->reserve_sz)) {
728 ctx->plaintext_header_sz = size;
729 return SQLITE_OK;
731 ctx->plaintext_header_sz = -1;
732 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_plaintext_header_size: attempt to set invalid plantext_header_size %d", size);
733 return SQLITE_ERROR;
736 int sqlcipher_get_default_plaintext_header_size() {
737 return default_plaintext_header_sz;
740 int sqlcipher_codec_ctx_get_plaintext_header_size(codec_ctx *ctx) {
741 return ctx->plaintext_header_sz;
744 /* manipulate HMAC algorithm */
745 int sqlcipher_set_default_hmac_algorithm(int algorithm) {
746 default_hmac_algorithm = algorithm;
747 return SQLITE_OK;
750 int sqlcipher_codec_ctx_set_hmac_algorithm(codec_ctx *ctx, int algorithm) {
751 ctx->hmac_algorithm = algorithm;
752 return sqlcipher_codec_ctx_reserve_setup(ctx);
755 int sqlcipher_get_default_hmac_algorithm() {
756 return default_hmac_algorithm;
759 int sqlcipher_codec_ctx_get_hmac_algorithm(codec_ctx *ctx) {
760 return ctx->hmac_algorithm;
763 /* manipulate KDF algorithm */
764 int sqlcipher_set_default_kdf_algorithm(int algorithm) {
765 default_kdf_algorithm = algorithm;
766 return SQLITE_OK;
769 int sqlcipher_codec_ctx_set_kdf_algorithm(codec_ctx *ctx, int algorithm) {
770 ctx->kdf_algorithm = algorithm;
771 return SQLITE_OK;
774 int sqlcipher_get_default_kdf_algorithm() {
775 return default_kdf_algorithm;
778 int sqlcipher_codec_ctx_get_kdf_algorithm(codec_ctx *ctx) {
779 return ctx->kdf_algorithm;
782 void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) {
783 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_error %d", error);
784 sqlite3pager_error(ctx->pBt->pBt->pPager, error);
785 ctx->pBt->pBt->db->errCode = error;
788 int sqlcipher_codec_ctx_get_reservesize(codec_ctx *ctx) {
789 return ctx->reserve_sz;
792 void* sqlcipher_codec_ctx_get_data(codec_ctx *ctx) {
793 return ctx->buffer;
796 static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) {
797 sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
799 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
800 return SQLITE_OK; /* don't reload salt when not needed */
803 /* read salt from header, if present, otherwise generate a new random salt */
804 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init_kdf_salt: obtaining salt");
805 if(fd == NULL || fd->pMethods == 0 || sqlite3OsRead(fd, ctx->kdf_salt, ctx->kdf_salt_sz, 0) != SQLITE_OK) {
806 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init_kdf_salt: unable to read salt from file header, generating random");
807 if(ctx->provider->random(ctx->provider_ctx, ctx->kdf_salt, ctx->kdf_salt_sz) != SQLITE_OK) {
808 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init_kdf_salt: error retrieving random bytes from provider");
809 return SQLITE_ERROR;
812 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
813 return SQLITE_OK;
816 int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int size) {
817 if(size >= ctx->kdf_salt_sz) {
818 memcpy(ctx->kdf_salt, salt, ctx->kdf_salt_sz);
819 SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
820 return SQLITE_OK;
822 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size);
823 return SQLITE_ERROR;
826 int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx, void** salt) {
827 int rc = SQLITE_OK;
828 if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
829 if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
830 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_get_kdf_salt: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
833 *salt = ctx->kdf_salt;
835 return rc;
838 void sqlcipher_codec_get_keyspec(codec_ctx *ctx, void **zKey, int *nKey) {
839 *zKey = ctx->read_ctx->keyspec;
840 *nKey = ctx->keyspec_sz;
843 int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) {
844 if(!((size != 0) && ((size & (size - 1)) == 0)) || size < 512 || size > 65536) {
845 sqlcipher_log(SQLCIPHER_LOG_ERROR, "cipher_page_size not a power of 2 and between 512 and 65536 inclusive");
846 return SQLITE_ERROR;
848 /* attempt to free the existing page buffer */
849 if(ctx->buffer) sqlcipher_free(ctx->buffer,ctx->page_sz);
850 ctx->page_sz = size;
852 /* pre-allocate a page buffer of PageSize bytes. This will
853 be used as a persistent buffer for encryption and decryption
854 operations to avoid overhead of multiple memory allocations*/
855 ctx->buffer = sqlcipher_malloc(size);
856 if(ctx->buffer == NULL) return SQLITE_NOMEM;
858 return SQLITE_OK;
861 int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) {
862 return ctx->page_sz;
865 void sqlcipher_set_default_pagesize(int page_size) {
866 default_page_size = page_size;
869 int sqlcipher_get_default_pagesize() {
870 return default_page_size;
873 void sqlcipher_set_mem_security(int on) {
874 /* memory security can only be enabled, not disabled */
875 if(on) {
876 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_set_mem_security: on");
877 sqlcipher_mem_security_on = on;
881 int sqlcipher_get_mem_security() {
882 /* only report that memory security is enabled if pragma cipher_memory_security is ON and
883 SQLCipher's allocator/deallocator was run at least one timecurrently used */
884 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_get_mem_security: sqlcipher_mem_security_on = %d, sqlcipher_mem_executed = %d", sqlcipher_mem_security_on, sqlcipher_mem_executed);
885 return sqlcipher_mem_security_on && sqlcipher_mem_executed;
889 int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const void *zKey, int nKey) {
890 int rc;
891 codec_ctx *ctx;
893 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init: allocating context");
895 *iCtx = sqlcipher_malloc(sizeof(codec_ctx));
896 ctx = *iCtx;
898 if(ctx == NULL) return SQLITE_NOMEM;
900 ctx->pBt = pDb->pBt; /* assign pointer to database btree structure */
902 /* allocate space for salt data. Then read the first 16 bytes
903 directly off the database file. This is the salt for the
904 key derivation function. If we get a short read allocate
905 a new random salt value */
906 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init: allocating kdf_salt");
907 ctx->kdf_salt_sz = FILE_HEADER_SZ;
908 ctx->kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz);
909 if(ctx->kdf_salt == NULL) return SQLITE_NOMEM;
911 /* allocate space for separate hmac salt data. We want the
912 HMAC derivation salt to be different than the encryption
913 key derivation salt */
914 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init: allocating hmac_kdf_salt");
915 ctx->hmac_kdf_salt = sqlcipher_malloc(ctx->kdf_salt_sz);
916 if(ctx->hmac_kdf_salt == NULL) return SQLITE_NOMEM;
918 /* setup default flags */
919 ctx->flags = default_flags;
921 /* setup the crypto provider */
922 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_init: allocating provider");
923 ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider));
924 if(ctx->provider == NULL) return SQLITE_NOMEM;
926 /* make a copy of the provider to be used for the duration of the context */
927 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_codec_ctx_init: entering SQLCIPHER_MUTEX_PROVIDER");
928 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
929 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_codec_ctx_init: entered SQLCIPHER_MUTEX_PROVIDER");
931 memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider));
933 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_codec_ctx_init: leaving SQLCIPHER_MUTEX_PROVIDER");
934 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER));
935 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_codec_ctx_init: left SQLCIPHER_MUTEX_PROVIDER");
937 if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) {
938 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d returned from ctx_init", rc);
939 return rc;
942 ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx);
943 ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx);
944 ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx);
946 /* establic the size for a hex-formated key specification, containing the
947 raw encryption key and the salt used to generate it format. will be x'hexkey...hexsalt'
948 so oversize by 3 bytes */
949 ctx->keyspec_sz = ((ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3;
952 Always overwrite page size and set to the default because the first page of the database
953 in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in
954 cases where bytes 16 & 17 of the page header are a power of 2 as reported by John Lehman
956 if((rc = sqlcipher_codec_ctx_set_pagesize(ctx, default_page_size)) != SQLITE_OK) {
957 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d returned from sqlcipher_codec_ctx_set_pagesize with %d", rc, default_page_size);
958 return rc;
961 /* establish settings for the KDF iterations and fast (HMAC) KDF iterations */
962 if((rc = sqlcipher_codec_ctx_set_kdf_iter(ctx, default_kdf_iter)) != SQLITE_OK) {
963 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting default_kdf_iter %d", rc, default_kdf_iter);
964 return rc;
967 if((rc = sqlcipher_codec_ctx_set_fast_kdf_iter(ctx, FAST_PBKDF2_ITER)) != SQLITE_OK) {
968 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting fast_kdf_iter to %d", rc, FAST_PBKDF2_ITER);
969 return rc;
972 /* set the default HMAC and KDF algorithms which will determine the reserve size */
973 if((rc = sqlcipher_codec_ctx_set_hmac_algorithm(ctx, default_hmac_algorithm)) != SQLITE_OK) {
974 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_hmac_algorithm with %d", rc, default_hmac_algorithm);
975 return rc;
978 /* Note that use_hmac is a special case that requires recalculation of page size
979 so we call set_use_hmac to perform setup */
980 if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC))) != SQLITE_OK) {
981 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC));
982 return rc;
985 if((rc = sqlcipher_codec_ctx_set_kdf_algorithm(ctx, default_kdf_algorithm)) != SQLITE_OK) {
986 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_kdf_algorithm with %d", rc, default_kdf_algorithm);
987 return rc;
990 /* setup the default plaintext header size */
991 if((rc = sqlcipher_codec_ctx_set_plaintext_header_size(ctx, default_plaintext_header_sz)) != SQLITE_OK) {
992 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting sqlcipher_codec_ctx_set_plaintext_header_size with %d", rc, default_plaintext_header_sz);
993 return rc;
996 /* initialize the read and write sub-contexts. this must happen after key_sz is established */
997 if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->read_ctx)) != SQLITE_OK) {
998 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d initializing read_ctx", rc);
999 return rc;
1002 if((rc = sqlcipher_cipher_ctx_init(ctx, &ctx->write_ctx)) != SQLITE_OK) {
1003 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d initializing write_ctx", rc);
1004 return rc;
1007 /* set the key material on one of the sub cipher contexts and sync them up */
1008 if((rc = sqlcipher_codec_ctx_set_pass(ctx, zKey, nKey, 0)) != SQLITE_OK) {
1009 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting pass key", rc);
1010 return rc;
1013 if((rc = sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx)) != SQLITE_OK) {
1014 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d copying write_ctx to read_ctx", rc);
1015 return rc;
1018 return SQLITE_OK;
1022 * Free and wipe memory associated with a cipher_ctx, including the allocated
1023 * read_ctx and write_ctx.
1025 void sqlcipher_codec_ctx_free(codec_ctx **iCtx) {
1026 codec_ctx *ctx = *iCtx;
1027 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "codec_ctx_free: iCtx=%p", iCtx);
1028 if(ctx->kdf_salt) sqlcipher_free(ctx->kdf_salt, ctx->kdf_salt_sz);
1029 if(ctx->hmac_kdf_salt) sqlcipher_free(ctx->hmac_kdf_salt, ctx->kdf_salt_sz);
1030 if(ctx->buffer) sqlcipher_free(ctx->buffer, ctx->page_sz);
1032 if(ctx->provider) {
1033 ctx->provider->ctx_free(&ctx->provider_ctx);
1034 sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
1037 sqlcipher_cipher_ctx_free(ctx, &ctx->read_ctx);
1038 sqlcipher_cipher_ctx_free(ctx, &ctx->write_ctx);
1039 sqlcipher_free(ctx, sizeof(codec_ctx));
1042 /** convert a 32bit unsigned integer to little endian byte ordering */
1043 static void sqlcipher_put4byte_le(unsigned char *p, u32 v) {
1044 p[0] = (u8)v;
1045 p[1] = (u8)(v>>8);
1046 p[2] = (u8)(v>>16);
1047 p[3] = (u8)(v>>24);
1050 static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, unsigned char *in, int in_sz, unsigned char *out) {
1051 unsigned char pgno_raw[sizeof(pgno)];
1052 /* we may convert page number to consistent representation before calculating MAC for
1053 compatibility across big-endian and little-endian platforms.
1055 Note: The public release of sqlcipher 2.0.0 to 2.0.6 had a bug where the bytes of pgno
1056 were used directly in the MAC. SQLCipher convert's to little endian by default to preserve
1057 backwards compatibility on the most popular platforms, but can optionally be configured
1058 to use either big endian or native byte ordering via pragma. */
1060 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { /* compute hmac using little endian pgno*/
1061 sqlcipher_put4byte_le(pgno_raw, pgno);
1062 } else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { /* compute hmac using big endian pgno */
1063 sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */
1064 } else { /* use native byte ordering */
1065 memcpy(pgno_raw, &pgno, sizeof(pgno));
1068 /* include the encrypted page data, initialization vector, and page number in HMAC. This will
1069 prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise
1070 valid pages out of order in a database */
1071 return ctx->provider->hmac(
1072 ctx->provider_ctx, ctx->hmac_algorithm, c_ctx->hmac_key,
1073 ctx->key_sz, in,
1074 in_sz, (unsigned char*) &pgno_raw,
1075 sizeof(pgno), out);
1079 * ctx - codec context
1080 * pgno - page number in database
1081 * size - size in bytes of input and output buffers
1082 * mode - 1 to encrypt, 0 to decrypt
1083 * in - pointer to input bytes
1084 * out - pouter to output bytes
1086 int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int page_sz, unsigned char *in, unsigned char *out) {
1087 cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
1088 unsigned char *iv_in, *iv_out, *hmac_in, *hmac_out, *out_start;
1089 int size;
1091 /* calculate some required positions into various buffers */
1092 size = page_sz - ctx->reserve_sz; /* adjust size to useable size and memset reserve at end of page */
1093 iv_out = out + size;
1094 iv_in = in + size;
1096 /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain
1097 random bytes. note, these pointers are only valid when using hmac */
1098 hmac_in = in + size + ctx->iv_sz;
1099 hmac_out = out + size + ctx->iv_sz;
1100 out_start = out; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */
1102 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_page_cipher: pgno=%d, mode=%d, size=%d", pgno, mode, size);
1103 CODEC_HEXDUMP("sqlcipher_page_cipher: input page data", in, page_sz);
1105 /* the key size should never be zero. If it is, error out. */
1106 if(ctx->key_sz == 0) {
1107 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: error possible context corruption, key_sz is zero for pgno=%d", pgno);
1108 goto error;
1111 if(mode == CIPHER_ENCRYPT) {
1112 /* start at front of the reserve block, write random data to the end */
1113 if(ctx->provider->random(ctx->provider_ctx, iv_out, ctx->reserve_sz) != SQLITE_OK) goto error;
1114 } else { /* CIPHER_DECRYPT */
1115 memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */
1118 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) {
1119 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
1120 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: hmac operation on decrypt failed for pgno=%d", pgno);
1121 goto error;
1124 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_page_cipher: comparing hmac on in=%p out=%p hmac_sz=%d", hmac_in, hmac_out, ctx->hmac_sz);
1125 if(sqlcipher_memcmp(hmac_in, hmac_out, ctx->hmac_sz) != 0) { /* the hmac check failed */
1126 if(sqlite3BtreeGetAutoVacuum(ctx->pBt) != BTREE_AUTOVACUUM_NONE && sqlcipher_ismemset(in, 0, page_sz) == 0) {
1127 /* first check if the entire contents of the page is zeros. If so, this page
1128 resulted from a short read (i.e. sqlite attempted to pull a page after the end of the file. these
1129 short read failures must be ignored for autovaccum mode to work so wipe the output buffer
1130 and return SQLITE_OK to skip the decryption step. */
1131 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_page_cipher: zeroed page (short read) for pgno %d with autovacuum enabled", pgno);
1132 sqlcipher_memset(out, 0, page_sz);
1133 return SQLITE_OK;
1134 } else {
1135 /* if the page memory is not all zeros, it means the there was data and a hmac on the page.
1136 since the check failed, the page was either tampered with or corrupted. wipe the output buffer,
1137 and return SQLITE_ERROR to the caller */
1138 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: hmac check failed for pgno=%d", pgno);
1139 goto error;
1144 if(ctx->provider->cipher(ctx->provider_ctx, mode, c_ctx->key, ctx->key_sz, iv_out, in, size, out) != SQLITE_OK) {
1145 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: cipher operation mode=%d failed for pgno=%d", mode, pgno);
1146 goto error;
1149 if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
1150 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
1151 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: hmac operation on encrypt failed for pgno=%d", pgno);
1152 goto error;
1156 CODEC_HEXDUMP("sqlcipher_page_cipher: output page data", out_start, page_sz);
1158 return SQLITE_OK;
1159 error:
1160 sqlcipher_memset(out, 0, page_sz);
1161 return SQLITE_ERROR;
1165 * Derive an encryption key for a cipher contex key based on the raw password.
1167 * If the raw key data is formated as x'hex' and there are exactly enough hex chars to fill
1168 * the key (i.e 64 hex chars for a 256 bit key) then the key data will be used directly.
1170 * Else, if the raw key data is formated as x'hex' and there are exactly enough hex chars to fill
1171 * the key and the salt (i.e 92 hex chars for a 256 bit key and 16 byte salt) then it will be unpacked
1172 * as the key followed by the salt.
1174 * Otherwise, a key data will be derived using PBKDF2
1176 * returns SQLITE_OK if initialization was successful
1177 * returns SQLITE_ERROR if the key could't be derived (for instance if pass is NULL or pass_sz is 0)
1179 static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
1180 int rc;
1181 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_key_derive: ctx->kdf_salt_sz=%d ctx->kdf_iter=%d ctx->fast_kdf_iter=%d ctx->key_sz=%d",
1182 ctx->kdf_salt_sz, ctx->kdf_iter, ctx->fast_kdf_iter, ctx->key_sz);
1184 if(c_ctx->pass && c_ctx->pass_sz) { /* if key material is present on the context for derivation */
1186 /* if necessary, initialize the salt from the header or random source */
1187 if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
1188 if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
1189 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
1190 return rc;
1194 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)) {
1195 int n = c_ctx->pass_sz - 3; /* adjust for leading x' and tailing ' */
1196 const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */
1197 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_key_derive: using raw key from hex");
1198 cipher_hex2bin(z, n, c_ctx->key);
1199 } 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)) {
1200 const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */
1201 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_key_derive: using raw key from hex");
1202 cipher_hex2bin(z, (ctx->key_sz * 2), c_ctx->key);
1203 cipher_hex2bin(z + (ctx->key_sz * 2), (ctx->kdf_salt_sz * 2), ctx->kdf_salt);
1204 } else {
1205 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_cipher_ctx_key_derive: deriving key using full PBKDF2 with %d iterations", ctx->kdf_iter);
1206 if(ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->pass, c_ctx->pass_sz,
1207 ctx->kdf_salt, ctx->kdf_salt_sz, ctx->kdf_iter,
1208 ctx->key_sz, c_ctx->key) != SQLITE_OK) {
1209 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: error occurred from provider kdf generating encryption key");
1210 return SQLITE_ERROR;
1214 /* set the context "keyspec" containing the hex-formatted key and salt to be used when attaching databases */
1215 if((rc = sqlcipher_cipher_ctx_set_keyspec(ctx, c_ctx, c_ctx->key)) != SQLITE_OK) {
1216 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: error %d from sqlcipher_cipher_ctx_set_keyspec", rc);
1217 return rc;
1220 /* if this context is setup to use hmac checks, generate a seperate and different
1221 key for HMAC. In this case, we use the output of the previous KDF as the input to
1222 this KDF run. This ensures a distinct but predictable HMAC key. */
1223 if(ctx->flags & CIPHER_FLAG_HMAC) {
1224 int i;
1226 /* start by copying the kdf key into the hmac salt slot
1227 then XOR it with the fixed hmac salt defined at compile time
1228 this ensures that the salt passed in to derive the hmac key, while
1229 easy to derive and publically known, is not the same as the salt used
1230 to generate the encryption key */
1231 memcpy(ctx->hmac_kdf_salt, ctx->kdf_salt, ctx->kdf_salt_sz);
1232 for(i = 0; i < ctx->kdf_salt_sz; i++) {
1233 ctx->hmac_kdf_salt[i] ^= hmac_salt_mask;
1236 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "cipher_ctx_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations",
1237 ctx->fast_kdf_iter);
1240 if(ctx->provider->kdf(ctx->provider_ctx, ctx->kdf_algorithm, c_ctx->key, ctx->key_sz,
1241 ctx->hmac_kdf_salt, ctx->kdf_salt_sz, ctx->fast_kdf_iter,
1242 ctx->key_sz, c_ctx->hmac_key) != SQLITE_OK) {
1243 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: error occurred from provider kdf generating HMAC key");
1244 return SQLITE_ERROR;
1248 c_ctx->derive_key = 0;
1249 return SQLITE_OK;
1251 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: key material is not present on the context for key derivation");
1252 return SQLITE_ERROR;
1255 int sqlcipher_codec_key_derive(codec_ctx *ctx) {
1256 /* derive key on first use if necessary */
1257 if(ctx->read_ctx->derive_key) {
1258 if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->read_ctx) != SQLITE_OK) {
1259 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_key_derive: error occurred deriving read_ctx key");
1260 return SQLITE_ERROR;
1264 if(ctx->write_ctx->derive_key) {
1265 if(sqlcipher_cipher_ctx_cmp(ctx->write_ctx, ctx->read_ctx) == 0) {
1266 /* the relevant parameters are the same, just copy read key */
1267 if(sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx) != SQLITE_OK) {
1268 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_key_derive: error occurred copying read_ctx to write_ctx");
1269 return SQLITE_ERROR;
1271 } else {
1272 if(sqlcipher_cipher_ctx_key_derive(ctx, ctx->write_ctx) != SQLITE_OK) {
1273 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_key_derive: error occurred deriving write_ctx key");
1274 return SQLITE_ERROR;
1279 /* TODO: wipe and free passphrase after key derivation */
1280 if(ctx->store_pass != 1) {
1281 sqlcipher_cipher_ctx_set_pass(ctx->read_ctx, NULL, 0);
1282 sqlcipher_cipher_ctx_set_pass(ctx->write_ctx, NULL, 0);
1285 return SQLITE_OK;
1288 int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) {
1289 if(source == CIPHER_READ_CTX) {
1290 return sqlcipher_cipher_ctx_copy(ctx, ctx->write_ctx, ctx->read_ctx);
1291 } else {
1292 return sqlcipher_cipher_ctx_copy(ctx, ctx->read_ctx, ctx->write_ctx);
1296 const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) {
1297 return ctx->provider->get_provider_name(ctx->provider_ctx);
1301 static int sqlcipher_check_connection(const char *filename, char *key, int key_sz, char *sql, int *user_version, char** journal_mode) {
1302 int rc;
1303 sqlite3 *db = NULL;
1304 sqlite3_stmt *statement = NULL;
1305 char *query_journal_mode = "PRAGMA journal_mode;";
1306 char *query_user_version = "PRAGMA user_version;";
1308 rc = sqlite3_open(filename, &db);
1309 if(rc != SQLITE_OK) goto cleanup;
1311 rc = sqlite3_key(db, key, key_sz);
1312 if(rc != SQLITE_OK) goto cleanup;
1314 rc = sqlite3_exec(db, sql, NULL, NULL, NULL);
1315 if(rc != SQLITE_OK) goto cleanup;
1317 /* start by querying the user version.
1318 this will fail if the key is incorrect */
1319 rc = sqlite3_prepare(db, query_user_version, -1, &statement, NULL);
1320 if(rc != SQLITE_OK) goto cleanup;
1322 rc = sqlite3_step(statement);
1323 if(rc == SQLITE_ROW) {
1324 *user_version = sqlite3_column_int(statement, 0);
1325 } else {
1326 goto cleanup;
1328 sqlite3_finalize(statement);
1330 rc = sqlite3_prepare(db, query_journal_mode, -1, &statement, NULL);
1331 if(rc != SQLITE_OK) goto cleanup;
1333 rc = sqlite3_step(statement);
1334 if(rc == SQLITE_ROW) {
1335 *journal_mode = sqlite3_mprintf("%s", sqlite3_column_text(statement, 0));
1336 } else {
1337 goto cleanup;
1339 rc = SQLITE_OK;
1340 /* cleanup will finalize open statement */
1342 cleanup:
1343 if(statement) sqlite3_finalize(statement);
1344 if(db) sqlite3_close(db);
1345 return rc;
1348 int sqlcipher_codec_ctx_integrity_check(codec_ctx *ctx, Parse *pParse, char *column) {
1349 Pgno page = 1;
1350 int rc = 0;
1351 char *result;
1352 unsigned char *hmac_out = NULL;
1353 sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
1354 i64 file_sz;
1356 Vdbe *v = sqlite3GetVdbe(pParse);
1357 sqlite3VdbeSetNumCols(v, 1);
1358 sqlite3VdbeSetColName(v, 0, COLNAME_NAME, column, SQLITE_STATIC);
1360 if(fd == NULL || fd->pMethods == 0) {
1361 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "database file is undefined", P4_TRANSIENT);
1362 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1363 goto cleanup;
1366 if(!(ctx->flags & CIPHER_FLAG_HMAC)) {
1367 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "HMAC is not enabled, unable to integrity check", P4_TRANSIENT);
1368 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1369 goto cleanup;
1372 if((rc = sqlcipher_codec_key_derive(ctx)) != SQLITE_OK) {
1373 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, "unable to derive keys", P4_TRANSIENT);
1374 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1375 goto cleanup;
1378 sqlite3OsFileSize(fd, &file_sz);
1379 hmac_out = sqlcipher_malloc(ctx->hmac_sz);
1381 for(page = 1; page <= file_sz / ctx->page_sz; page++) {
1382 i64 offset = (page - 1) * ctx->page_sz;
1383 int payload_sz = ctx->page_sz - ctx->reserve_sz + ctx->iv_sz;
1384 int read_sz = ctx->page_sz;
1386 /* skip integrity check on PAGER_SJ_PGNO since it will have no valid content */
1387 if(sqlite3pager_is_sj_pgno(ctx->pBt->pBt->pPager, page)) continue;
1389 if(page==1) {
1390 int page1_offset = ctx->plaintext_header_sz ? ctx->plaintext_header_sz : FILE_HEADER_SZ;
1391 read_sz = read_sz - page1_offset;
1392 payload_sz = payload_sz - page1_offset;
1393 offset += page1_offset;
1396 sqlcipher_memset(ctx->buffer, 0, ctx->page_sz);
1397 sqlcipher_memset(hmac_out, 0, ctx->hmac_sz);
1398 if(sqlite3OsRead(fd, ctx->buffer, read_sz, offset) != SQLITE_OK) {
1399 result = sqlite3_mprintf("error reading %d bytes from file page %d at offset %d", read_sz, page, offset);
1400 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1401 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1402 } else if(sqlcipher_page_hmac(ctx, ctx->read_ctx, page, ctx->buffer, payload_sz, hmac_out) != SQLITE_OK) {
1403 result = sqlite3_mprintf("HMAC operation failed for page %d", page);
1404 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1405 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1406 } else if(sqlcipher_memcmp(ctx->buffer + payload_sz, hmac_out, ctx->hmac_sz) != 0) {
1407 result = sqlite3_mprintf("HMAC verification failed for page %d", page);
1408 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1409 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1413 if(file_sz % ctx->page_sz != 0) {
1414 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);
1415 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, result, P4_DYNAMIC);
1416 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 1);
1419 cleanup:
1420 if(hmac_out != NULL) sqlcipher_free(hmac_out, ctx->hmac_sz);
1421 return SQLITE_OK;
1424 int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
1425 int i, pass_sz, keyspec_sz, nRes, user_version, rc, oflags;
1426 Db *pDb = 0;
1427 sqlite3 *db = ctx->pBt->db;
1428 const char *db_filename = sqlite3_db_filename(db, "main");
1429 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;
1430 Btree *pDest = NULL, *pSrc = NULL;
1431 sqlite3_file *srcfile, *destfile;
1432 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1433 LPWSTR w_db_filename = NULL, w_migrated_db_filename = NULL;
1434 int w_db_filename_sz = 0, w_migrated_db_filename_sz = 0;
1435 #endif
1436 pass_sz = keyspec_sz = rc = user_version = 0;
1438 if(!db_filename || sqlite3Strlen30(db_filename) < 1)
1439 goto cleanup; /* exit immediately if this is an in memory database */
1441 /* pull the provided password / key material off the current codec context */
1442 pass_sz = ctx->read_ctx->pass_sz;
1443 pass = sqlcipher_malloc(pass_sz+1);
1444 memset(pass, 0, pass_sz+1);
1445 memcpy(pass, ctx->read_ctx->pass, pass_sz);
1447 /* Version 4 - current, no upgrade required, so exit immediately */
1448 rc = sqlcipher_check_connection(db_filename, pass, pass_sz, "", &user_version, &journal_mode);
1449 if(rc == SQLITE_OK){
1450 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_codec_ctx_migrate: no upgrade required - exiting");
1451 goto cleanup;
1454 for(i = 3; i > 0; i--) {
1455 pragma_compat = sqlite3_mprintf("PRAGMA cipher_compatibility = %d;", i);
1456 rc = sqlcipher_check_connection(db_filename, pass, pass_sz, pragma_compat, &user_version, &journal_mode);
1457 if(rc == SQLITE_OK) {
1458 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_migrate: version %d format found", i);
1459 goto migrate;
1461 if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat));
1462 pragma_compat = NULL;
1465 /* if we exit the loop normally we failed to determine the version, this is an error */
1466 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: unable to determine format version for upgrade: this may indicate custom settings were used ");
1467 goto handle_error;
1469 migrate:
1471 temp = sqlite3_mprintf("%s-migrated", db_filename);
1472 /* overallocate migrated_db_filename, because sqlite3OsOpen will read past the null terminator
1473 * to determine whether the filename was URI formatted */
1474 migrated_db_filename = sqlcipher_malloc(sqlite3Strlen30(temp)+2);
1475 memcpy(migrated_db_filename, temp, sqlite3Strlen30(temp));
1476 sqlcipher_free(temp, sqlite3Strlen30(temp));
1478 attach_command = sqlite3_mprintf("ATTACH DATABASE '%s' as migrate;", migrated_db_filename, pass);
1479 set_user_version = sqlite3_mprintf("PRAGMA migrate.user_version = %d;", user_version);
1481 rc = sqlite3_exec(db, pragma_compat, NULL, NULL, NULL);
1482 if(rc != SQLITE_OK){
1483 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: set compatibility mode failed, error code %d", rc);
1484 goto handle_error;
1487 /* force journal mode to DELETE, we will set it back later if different */
1488 rc = sqlite3_exec(db, "PRAGMA journal_mode = delete;", NULL, NULL, NULL);
1489 if(rc != SQLITE_OK){
1490 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: force journal mode DELETE failed, error code %d", rc);
1491 goto handle_error;
1494 rc = sqlite3_exec(db, attach_command, NULL, NULL, NULL);
1495 if(rc != SQLITE_OK){
1496 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: attach failed, error code %d", rc);
1497 goto handle_error;
1500 rc = sqlite3_key_v2(db, "migrate", pass, pass_sz);
1501 if(rc != SQLITE_OK){
1502 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: keying attached database failed, error code %d", rc);
1503 goto handle_error;
1506 rc = sqlite3_exec(db, "SELECT sqlcipher_export('migrate');", NULL, NULL, NULL);
1507 if(rc != SQLITE_OK){
1508 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: sqlcipher_export failed, error code %d", rc);
1509 goto handle_error;
1512 #ifdef SQLCIPHER_TEST
1513 if((sqlcipher_get_test_flags() & TEST_FAIL_MIGRATE) > 0) {
1514 rc = SQLITE_ERROR;
1515 sqlcipher_log(SQLCIPHER_LOG_WARN, "sqlcipher_codec_ctx_migrate: simulated migrate failure, error code %d", rc);
1516 goto handle_error;
1518 #endif
1520 rc = sqlite3_exec(db, set_user_version, NULL, NULL, NULL);
1521 if(rc != SQLITE_OK){
1522 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: set user version failed, error code %d", rc);
1523 goto handle_error;
1526 if( !db->autoCommit ){
1527 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: cannot migrate from within a transaction");
1528 goto handle_error;
1530 if( db->nVdbeActive>1 ){
1531 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: cannot migrate - SQL statements in progress");
1532 goto handle_error;
1535 pDest = db->aDb[0].pBt;
1536 pDb = &(db->aDb[db->nDb-1]);
1537 pSrc = pDb->pBt;
1539 nRes = sqlite3BtreeGetRequestedReserve(pSrc);
1540 /* unset the BTS_PAGESIZE_FIXED flag to avoid SQLITE_READONLY */
1541 pDest->pBt->btsFlags &= ~BTS_PAGESIZE_FIXED;
1542 rc = sqlite3BtreeSetPageSize(pDest, default_page_size, nRes, 0);
1543 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "set btree page size to %d res %d rc %d", default_page_size, nRes, rc);
1544 if( rc!=SQLITE_OK ) goto handle_error;
1546 sqlcipherCodecGetKey(db, db->nDb - 1, (void**)&keyspec, &keyspec_sz);
1547 SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_KEY_USED);
1548 sqlcipherCodecAttach(db, 0, keyspec, keyspec_sz);
1550 srcfile = sqlite3PagerFile(pSrc->pBt->pPager);
1551 destfile = sqlite3PagerFile(pDest->pBt->pPager);
1553 sqlite3OsClose(srcfile);
1554 sqlite3OsClose(destfile);
1556 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1557 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "performing windows MoveFileExA");
1559 w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, NULL, 0);
1560 w_db_filename = sqlcipher_malloc(w_db_filename_sz * sizeof(wchar_t));
1561 w_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) db_filename, -1, (const LPWSTR) w_db_filename, w_db_filename_sz);
1563 w_migrated_db_filename_sz = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) migrated_db_filename, -1, NULL, 0);
1564 w_migrated_db_filename = sqlcipher_malloc(w_migrated_db_filename_sz * sizeof(wchar_t));
1565 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);
1567 if(!MoveFileExW(w_migrated_db_filename, w_db_filename, MOVEFILE_REPLACE_EXISTING)) {
1568 rc = SQLITE_ERROR;
1569 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc);
1570 goto handle_error;
1572 #else
1573 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "performing POSIX rename");
1574 if ((rc = rename(migrated_db_filename, db_filename)) != 0) {
1575 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: error occurred while renaming migration files %d", rc);
1576 goto handle_error;
1578 #endif
1579 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "renamed migration database %s to main database %s: %d", migrated_db_filename, db_filename, rc);
1581 rc = sqlite3OsOpen(db->pVfs, migrated_db_filename, srcfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags);
1582 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "reopened migration database: %d", rc);
1583 if( rc!=SQLITE_OK ) goto handle_error;
1585 rc = sqlite3OsOpen(db->pVfs, db_filename, destfile, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB, &oflags);
1586 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "reopened main database: %d", rc);
1587 if( rc!=SQLITE_OK ) goto handle_error;
1589 sqlite3pager_reset(pDest->pBt->pPager);
1590 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "reset pager");
1592 rc = sqlite3_exec(db, "DETACH DATABASE migrate;", NULL, NULL, NULL);
1593 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "DETACH DATABASE called %d", rc);
1594 if(rc != SQLITE_OK) goto cleanup;
1596 sqlite3ResetAllSchemasOfConnection(db);
1597 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "reset all schemas");
1599 set_journal_mode = sqlite3_mprintf("PRAGMA journal_mode = %s;", journal_mode);
1600 rc = sqlite3_exec(db, set_journal_mode, NULL, NULL, NULL);
1601 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "%s: %d", set_journal_mode, rc);
1602 if( rc!=SQLITE_OK ) goto handle_error;
1604 goto cleanup;
1606 handle_error:
1607 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_migrate: an error occurred attempting to migrate the database - last error %d", rc);
1609 cleanup:
1610 if(migrated_db_filename) {
1611 int del_rc = sqlite3OsDelete(db->pVfs, migrated_db_filename, 0);
1612 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_ctx_migrate: deleted migration database: %d", del_rc);
1615 if(pass) sqlcipher_free(pass, pass_sz);
1616 if(attach_command) sqlcipher_free(attach_command, sqlite3Strlen30(attach_command));
1617 if(migrated_db_filename) sqlcipher_free(migrated_db_filename, sqlite3Strlen30(migrated_db_filename));
1618 if(set_user_version) sqlcipher_free(set_user_version, sqlite3Strlen30(set_user_version));
1619 if(set_journal_mode) sqlcipher_free(set_journal_mode, sqlite3Strlen30(set_journal_mode));
1620 if(journal_mode) sqlcipher_free(journal_mode, sqlite3Strlen30(journal_mode));
1621 if(pragma_compat) sqlcipher_free(pragma_compat, sqlite3Strlen30(pragma_compat));
1622 #if defined(_WIN32) || defined(SQLITE_OS_WINRT)
1623 if(w_db_filename) sqlcipher_free(w_db_filename, w_db_filename_sz);
1624 if(w_migrated_db_filename) sqlcipher_free(w_migrated_db_filename, w_migrated_db_filename_sz);
1625 #endif
1626 return rc;
1629 int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz){
1630 const char *suffix = &zRight[random_sz-1];
1631 int n = random_sz - 3; /* adjust for leading x' and tailing ' */
1632 if (n > 0 &&
1633 sqlite3StrNICmp((const char *)zRight ,"x'", 2) == 0 &&
1634 sqlite3StrNICmp(suffix, "'", 1) == 0 &&
1635 n % 2 == 0) {
1636 int rc = 0;
1637 int buffer_sz = n / 2;
1638 unsigned char *random;
1639 const unsigned char *z = (const unsigned char *)zRight + 2; /* adjust lead offset of x' */
1640 sqlcipher_log(SQLCIPHER_LOG_DEBUG, "sqlcipher_codec_add_random: using raw random blob from hex");
1641 random = sqlcipher_malloc(buffer_sz);
1642 memset(random, 0, buffer_sz);
1643 cipher_hex2bin(z, n, random);
1644 rc = ctx->provider->add_random(ctx->provider_ctx, random, buffer_sz);
1645 sqlcipher_free(random, buffer_sz);
1646 return rc;
1648 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_add_random: attemt to add random with invalid format");
1649 return SQLITE_ERROR;
1652 #if !defined(SQLITE_OMIT_TRACE)
1654 #define SQLCIPHER_PROFILE_FMT "Elapsed time:%.3f ms - %s\n"
1655 #define SQLCIPHER_PROFILE_FMT_OSLOG "Elapsed time:%{public}.3f ms - %{public}s\n"
1657 static int sqlcipher_profile_callback(unsigned int trace, void *file, void *stmt, void *run_time){
1658 FILE *f = (FILE*) file;
1659 double elapsed = (*((sqlite3_uint64*)run_time))/1000000.0;
1660 if(f == NULL) {
1661 #if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
1662 #if defined(__ANDROID__)
1663 __android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1664 #elif defined(__APPLE__)
1665 os_log(OS_LOG_DEFAULT, SQLCIPHER_PROFILE_FMT_OSLOG, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1666 #endif
1667 #endif
1668 } else {
1669 fprintf(f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
1671 return SQLITE_OK;
1673 #endif
1675 int sqlcipher_cipher_profile(sqlite3 *db, const char *destination){
1676 #if defined(SQLITE_OMIT_TRACE)
1677 return SQLITE_ERROR;
1678 #else
1679 FILE *f = NULL;
1680 if(sqlite3_stricmp(destination, "off") == 0){
1681 sqlite3_trace_v2(db, 0, NULL, NULL); /* disable tracing */
1682 } else {
1683 if(sqlite3_stricmp(destination, "stdout") == 0){
1684 f = stdout;
1685 }else if(sqlite3_stricmp(destination, "stderr") == 0){
1686 f = stderr;
1687 }else if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){
1688 f = NULL; /* file pointer will be NULL indicating the device target (i.e. logcat or oslog). We will accept logcat for backwards compatibility */
1689 }else{
1690 #if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT))
1691 if(fopen_s(&f, destination, "a") != 0) return SQLITE_ERROR;
1692 #else
1693 if((f = fopen(destination, "a")) == 0) return SQLITE_ERROR;
1694 #endif
1696 sqlite3_trace_v2(db, SQLITE_TRACE_PROFILE, sqlcipher_profile_callback, f);
1698 return SQLITE_OK;
1699 #endif
1702 int sqlcipher_codec_fips_status(codec_ctx *ctx) {
1703 return ctx->provider->fips_status(ctx->provider_ctx);
1706 const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
1707 return ctx->provider->get_provider_version(ctx->provider_ctx);
1710 #ifndef SQLCIPHER_OMIT_LOG
1711 /* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */
1712 #define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
1713 #define HECTONANOSEC_PER_SEC 10000000ull
1714 void sqlcipher_log(unsigned int level, const char *message, ...) {
1715 va_list params;
1716 va_start(params, message);
1717 char *formatted = NULL;
1719 #ifdef CODEC_DEBUG
1720 #if defined(SQLCIPHER_OMIT_LOG_DEVICE)
1721 vfprintf(stderr, message, params);
1722 fprintf(stderr, "\n");
1723 goto end;
1724 #else
1725 #if defined(__ANDROID__)
1726 __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params);
1727 goto end;
1728 #elif defined(__APPLE__)
1729 formatted = sqlite3_vmprintf(message, params);
1730 os_log(OS_LOG_DEFAULT, "%s", formatted);
1731 sqlite3_free(formatted);
1732 goto end;
1733 #else
1734 vfprintf(stderr, message, params);
1735 fprintf(stderr, "\n");
1736 goto end;
1737 #endif
1738 #endif
1739 #endif
1741 if(level > sqlcipher_log_level || (sqlcipher_log_device == 0 && sqlcipher_log_file == NULL)) {
1742 /* no log target or tag not in included filters */
1743 goto end;
1746 #if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
1747 if(sqlcipher_log_device) {
1748 #if defined(__ANDROID__)
1749 __android_log_vprint(ANDROID_LOG_DEBUG, "sqlcipher", message, params);
1750 goto end;
1751 #elif defined(__APPLE__)
1752 formatted = sqlite3_vmprintf(message, params);
1753 os_log(OS_LOG_DEFAULT, "%{public}s", formatted);
1754 sqlite3_free(formatted);
1755 goto end;
1756 #endif
1758 #endif
1760 if(sqlcipher_log_file != NULL){
1761 char buffer[24];
1762 struct tm tt;
1763 int ms;
1764 time_t sec;
1765 #ifdef _WIN32
1766 SYSTEMTIME st;
1767 FILETIME ft;
1768 GetSystemTime(&st);
1769 SystemTimeToFileTime(&st, &ft);
1770 sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC);
1771 ms = st.wMilliseconds;
1772 localtime_s(&tt, &sec);
1773 #else
1774 struct timeval tv;
1775 gettimeofday(&tv, NULL);
1776 sec = tv.tv_sec;
1777 ms = tv.tv_usec/1000.0;
1778 localtime_r(&sec, &tt);
1779 #endif
1780 if(strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt)) {
1781 fprintf((FILE*)sqlcipher_log_file, "%s.%03d: ", buffer, ms);
1782 vfprintf((FILE*)sqlcipher_log_file, message, params);
1783 fprintf((FILE*)sqlcipher_log_file, "\n");
1784 goto end;
1788 end:
1789 va_end(params);
1791 #endif
1793 void sqlcipher_set_log_level(unsigned int level) {
1794 sqlcipher_log_level = level;
1797 int sqlcipher_set_log(const char *destination){
1798 #ifdef SQLCIPHER_OMIT_LOG
1799 return SQLITE_ERROR;
1800 #else
1801 /* close open trace file if it is not stdout or stderr, then
1802 reset trace settings */
1803 if(sqlcipher_log_file != NULL && sqlcipher_log_file != stdout && sqlcipher_log_file != stderr) {
1804 fclose((FILE*)sqlcipher_log_file);
1806 sqlcipher_log_file = NULL;
1807 sqlcipher_log_device = 0;
1809 if(sqlite3_stricmp(destination, "logcat") == 0 || sqlite3_stricmp(destination, "device") == 0){
1810 /* use the appropriate device log. accept logcat for backwards compatibility */
1811 sqlcipher_log_device = 1;
1812 } else if(sqlite3_stricmp(destination, "stdout") == 0){
1813 sqlcipher_log_file = stdout;
1814 }else if(sqlite3_stricmp(destination, "stderr") == 0){
1815 sqlcipher_log_file = stderr;
1816 }else if(sqlite3_stricmp(destination, "off") != 0){
1817 #if !defined(SQLCIPHER_PROFILE_USE_FOPEN) && (defined(_WIN32) && (__STDC_VERSION__ > 199901L) || defined(SQLITE_OS_WINRT))
1818 if(fopen_s(&sqlcipher_log_file, destination, "a") != 0) return SQLITE_ERROR;
1819 #else
1820 if((sqlcipher_log_file = fopen(destination, "a")) == 0) return SQLITE_ERROR;
1821 #endif
1823 sqlcipher_log(SQLCIPHER_LOG_INFO, "sqlcipher_set_log: set log to %s", destination);
1824 return SQLITE_OK;
1825 #endif
1828 #endif
1829 /* END SQLCIPHER */