@@ -108199,11 +108199,6 @@ SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
108199108199/* BEGIN SQLCIPHER */
108200108200#ifdef SQLITE_HAS_CODEC
108201108201
108202- /* #include "sqliteInt.h" */
108203- /* #include "btreeInt.h" */
108204- /* #include "pager.h" */
108205- /* #include "vdbeInt.h" */
108206-
108207108202#if !defined(SQLCIPHER_OMIT_LOG_DEVICE)
108208108203#if defined(__ANDROID__)
108209108204#include <android/log.h>
@@ -108271,6 +108266,10 @@ SQLITE_PRIVATE int sqlite3JournalSize(sqlite3_vfs *pVfs){
108271108266#define SQLCIPHER_H
108272108267
108273108268/* #include "sqlite3.h" */
108269+ /* #include "sqliteInt.h" */
108270+
108271+ #define SQLCIPHER_DECRYPT 0
108272+ #define SQLCIPHER_ENCRYPT 1
108274108273
108275108274#define SQLCIPHER_HMAC_SHA1 0
108276108275#define SQLCIPHER_HMAC_SHA1_LABEL "HMAC_SHA1"
@@ -108397,6 +108396,9 @@ void sqlcipher_log(unsigned int level, unsigned int source, const char *message,
108397108396
108398108397/************** End of sqlcipher.h *******************************************/
108399108398/************** Continuing where we left off in sqlcipher.c ******************/
108399+ /* #include "btreeInt.h" */
108400+ /* #include "pager.h" */
108401+ /* #include "vdbeInt.h" */
108400108402
108401108403#if !defined(SQLITE_EXTRA_INIT) || !defined(SQLITE_EXTRA_SHUTDOWN)
108402108404#error "SQLCipher must be compiled with -DSQLITE_EXTRA_INIT=sqlcipher_extra_init -DSQLITE_EXTRA_SHUTDOWN=sqlcipher_extra_shutdown"
@@ -108432,16 +108434,13 @@ SQLITE_API void sqlite3pager_reset(Pager *pPager);
108432108434#define CIPHER_STR(s) #s
108433108435
108434108436#ifndef CIPHER_VERSION_NUMBER
108435- #define CIPHER_VERSION_NUMBER 4.10 .0
108437+ #define CIPHER_VERSION_NUMBER 4.11 .0
108436108438#endif
108437108439
108438108440#ifndef CIPHER_VERSION_BUILD
108439108441#define CIPHER_VERSION_BUILD community
108440108442#endif
108441108443
108442- #define CIPHER_DECRYPT 0
108443- #define CIPHER_ENCRYPT 1
108444-
108445108444#define CIPHER_READ_CTX 0
108446108445#define CIPHER_WRITE_CTX 1
108447108446#define CIPHER_READWRITE_CTX 2
@@ -108735,11 +108734,11 @@ static void sqlcipher_fini(void) {
108735108734 }
108736108735 #endif
108737108736#elif defined(__APPLE__)
108738- #if !defined(__has_feature) || !__has_feature(address_sanitizer)
108739- static void (*const sqlcipher_fini_func)(void) __attribute__((used, section("__DATA,__mod_term_func"))) = sqlcipher_fini;
108740- #else
108737+ #if defined(__has_feature) && __has_feature(address_sanitizer)
108741108738 static void sqlcipher_cleanup_destructor(void) __attribute__((destructor));
108742108739 static void sqlcipher_cleanup_destructor(void) { sqlcipher_fini(); }
108740+ #else
108741+ static void (*const sqlcipher_fini_func)(void) __attribute__((used, section("__DATA,__mod_term_func"))) = sqlcipher_fini;
108743108742 #endif
108744108743#else
108745108744static void (*const sqlcipher_fini_func)(void) __attribute__((used, section(".fini_array"))) = sqlcipher_fini;
@@ -108813,11 +108812,11 @@ int sqlcipher_extra_init(const char* arg) {
108813108812 while(private_heap_sz >= SQLCIPHER_PRIVATE_HEAP_SIZE_STEP) {
108814108813 /* attempt to allocate the private heap. If allocation fails, reduce the size and try again */
108815108814 if((private_heap = sqlcipher_internal_malloc(private_heap_sz))) {
108816- xoshiro_randomness(private_heap, private_heap_sz);
108815+ xoshiro_randomness(private_heap, (int) private_heap_sz);
108817108816 /* initialize the head block of the linked list at the start of the heap */
108818108817 private_block *head = (private_block *) private_heap;
108819108818 head->is_used = 0;
108820- head->size = private_heap_sz - sizeof(private_block);
108819+ head->size = (u32) private_heap_sz - sizeof(private_block);
108821108820 head->next = NULL;
108822108821 break;
108823108822 }
@@ -108880,13 +108879,13 @@ int sqlcipher_extra_init(const char* arg) {
108880108879 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to allocate shield mask", __func__);
108881108880 goto error;
108882108881 }
108883- if((rc = default_provider->random(provider_ctx, sqlcipher_shield_mask, sqlcipher_shield_mask_sz)) != SQLITE_OK) {
108882+ if((rc = default_provider->random(provider_ctx, sqlcipher_shield_mask, (int) sqlcipher_shield_mask_sz)) != SQLITE_OK) {
108884108883 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_MEMORY, "%s: failed to generate requisite random mask data %d", __func__, rc);
108885108884 goto error;
108886108885 }
108887108886 }
108888108887
108889- default_provider->ctx_free(provider_ctx);
108888+ default_provider->ctx_free(& provider_ctx);
108890108889
108891108890 sqlcipher_init = 1;
108892108891 sqlcipher_shutdown = 0;
@@ -110006,14 +110005,14 @@ static int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mod
110006110005 goto error;
110007110006 }
110008110007
110009- if(mode == CIPHER_ENCRYPT ) {
110008+ if(mode == SQLCIPHER_ENCRYPT ) {
110010110009 /* start at front of the reserve block, write random data to the end */
110011110010 if(ctx->provider->random(ctx->provider_ctx, iv_out, ctx->reserve_sz) != SQLITE_OK) goto error;
110012- } else { /* CIPHER_DECRYPT */
110011+ } else { /* SQLCIPHER_DECRYPT */
110013110012 memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */
110014110013 }
110015110014
110016- if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT )) {
110015+ if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == SQLCIPHER_DECRYPT )) {
110017110016 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
110018110017 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on decrypt failed for pgno=%d", __func__, pgno);
110019110018 goto error;
@@ -110048,7 +110047,7 @@ static int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mod
110048110047 goto error;
110049110048 };
110050110049
110051- if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT )) {
110050+ if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == SQLCIPHER_ENCRYPT )) {
110052110051 if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
110053110052 sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_CORE, "%s: hmac operation on encrypt failed for pgno=%d", __func__, pgno);
110054110053 goto error;
@@ -110599,6 +110598,43 @@ static int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int ra
110599110598 return SQLITE_ERROR;
110600110599}
110601110600
110601+ #if defined(_WIN32)
110602+ /* On windows convert to utf-16 when writing to stderr or stdout to avoid
110603+ * a potential exception when writing mixed context to those streams
110604+ * when using the shell. */
110605+ static int sqlcipher_fprintf(FILE* stream, const char* format, ...) {
110606+ int sz;
110607+ va_list ap;
110608+
110609+ if (stream == stderr || stream == stdout) {
110610+ char* buffer = NULL;
110611+ wchar_t* wbuffer = NULL;
110612+
110613+ va_start(ap, format);
110614+ buffer = sqlite3_vmprintf(format, ap);
110615+ va_end(ap);
110616+ sz = (int)strlen(buffer);
110617+
110618+ wbuffer = sqlite3_malloc((sz + 1) * sizeof(wchar_t));
110619+ if (wbuffer == NULL) return -1;
110620+
110621+ sz = MultiByteToWideChar(CP_UTF8, 0, buffer, sz, wbuffer, sz);
110622+ wbuffer[sz] = (wchar_t) 0;
110623+ fputws(wbuffer, stream);
110624+
110625+ sqlite3_free(wbuffer);
110626+ sqlite3_free(buffer);
110627+ } else {
110628+ va_start(ap, format);
110629+ sz = vfprintf(stream, format, ap);
110630+ va_end(ap);
110631+ }
110632+ return sz;
110633+ }
110634+ #else
110635+ #define sqlcipher_fprintf fprintf
110636+ #endif
110637+
110602110638#if !defined(SQLITE_OMIT_TRACE)
110603110639
110604110640#define SQLCIPHER_PROFILE_FMT "Elapsed time:%.3f ms - %s\n"
@@ -110616,7 +110652,7 @@ static int sqlcipher_profile_callback(unsigned int trace, void *file, void *stmt
110616110652#endif
110617110653#endif
110618110654 } else {
110619- fprintf (f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
110655+ sqlcipher_fprintf (f, SQLCIPHER_PROFILE_FMT, elapsed, sqlite3_sql((sqlite3_stmt*)stmt));
110620110656 }
110621110657 return SQLITE_OK;
110622110658}
@@ -110720,8 +110756,9 @@ void sqlcipher_log(unsigned int level, unsigned int source, const char *message,
110720110756
110721110757#ifdef CODEC_DEBUG
110722110758#if defined(SQLCIPHER_OMIT_LOG_DEVICE) || (!defined(__ANDROID__) && !defined(__APPLE__))
110723- vfprintf(stderr, message, params);
110724- fprintf(stderr, "\n");
110759+ sqlite3_vsnprintf(MAX_LOG_LEN, formatted, message, params);
110760+ sqlcipher_fprintf(stderr, formatted);
110761+ sqlcipher_fprintf(stderr, "\n");
110725110762 goto end;
110726110763#else
110727110764#if defined(__ANDROID__)
@@ -110780,7 +110817,7 @@ void sqlcipher_log(unsigned int level, unsigned int source, const char *message,
110780110817 localtime_r(&sec, &tt);
110781110818#endif
110782110819 if(strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt)) {
110783- fprintf ((FILE*)sqlcipher_log_file, "%s.%03d: %s\n", buffer, ms, formatted);
110820+ sqlcipher_fprintf ((FILE*)sqlcipher_log_file, "%s.%03d: %s\n", buffer, ms, formatted);
110784110821 goto end;
110785110822 }
110786110823 }
@@ -111563,7 +111600,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
111563111600 if(pgno == 1) /* copy initial part of file header or SQLite magic to buffer */
111564111601 memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : (void *) SQLITE_FILE_HEADER, offset);
111565111602
111566- rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT , ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
111603+ rc = sqlcipher_page_cipher(ctx, cctx, pgno, SQLCIPHER_DECRYPT , ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
111567111604#ifdef SQLCIPHER_TEST
111568111605 if((cipher_test_flags & TEST_FAIL_DECRYPT) > 0 && sqlcipher_get_test_fail()) {
111569111606 rc = SQLITE_ERROR;
@@ -111604,7 +111641,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
111604111641 }
111605111642 memcpy(ctx->buffer, ctx->plaintext_header_sz ? pData : kdf_salt, offset);
111606111643 }
111607- rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT , ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
111644+ rc = sqlcipher_page_cipher(ctx, cctx, pgno, SQLCIPHER_ENCRYPT , ctx->page_sz - offset, pData + offset, (unsigned char*)ctx->buffer + offset);
111608111645#ifdef SQLCIPHER_TEST
111609111646 if((cipher_test_flags & TEST_FAIL_ENCRYPT) > 0 && sqlcipher_get_test_fail()) {
111610111647 rc = SQLITE_ERROR;
@@ -112243,6 +112280,7 @@ static int sqlcipher_ltc_add_random(void *ctx, const void *buffer, int length) {
112243112280
112244112281static int sqlcipher_ltc_activate(void *ctx) {
112245112282 unsigned char random_buffer[FORTUNA_MAX_SZ];
112283+ int bytes = 0;
112246112284
112247112285 sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_MUTEX, "sqlcipher_ltc_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
112248112286 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
@@ -112264,8 +112302,9 @@ static int sqlcipher_ltc_activate(void *ctx) {
112264112302 ltc_ref_count++;
112265112303
112266112304#ifndef SQLCIPHER_TEST
112267- sqlite3_randomness( FORTUNA_MAX_SZ, random_buffer );
112305+ bytes = rng_get_bytes(random_buffer, FORTUNA_MAX_SZ, NULL );
112268112306#endif
112307+ sqlcipher_log(SQLCIPHER_LOG_TRACE, SQLCIPHER_LOG_PROVIDER, "sqlcipher_ltc_activate: seeded fortuna with %d bytes from rng_get_bytes", bytes);
112269112308
112270112309 if(sqlcipher_ltc_add_random(ctx, random_buffer, FORTUNA_MAX_SZ) != SQLITE_OK) {
112271112310 return SQLITE_ERROR;
@@ -112402,7 +112441,7 @@ static int sqlcipher_ltc_cipher(
112402112441
112403112442 if((cipher_idx = find_cipher(LTC_CIPHER)) == -1) return SQLITE_ERROR;
112404112443 if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR;
112405- rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
112444+ rc = mode == SQLCIPHER_ENCRYPT ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
112406112445 if(rc != CRYPT_OK) return SQLITE_ERROR;
112407112446 cbc_done(&cbc);
112408112447 return SQLITE_OK;
@@ -112752,7 +112791,7 @@ static int sqlcipher_nss_cipher(
112752112791 CKA_ENCRYPT, &keyItem, NULL);
112753112792 if (symKey == NULL) goto error;
112754112793 SECStatus rv;
112755- if (mode == CIPHER_ENCRYPT ) {
112794+ if (mode == SQLCIPHER_ENCRYPT ) {
112756112795 rv = PK11_Encrypt(symKey, CKM_AES_CBC, ¶ms, out, &outLen,
112757112796 in_sz + 16, in, in_sz);
112758112797 } else {
@@ -113372,7 +113411,7 @@ static int sqlcipher_cc_cipher(
113372113411) {
113373113412 CCCryptorRef cryptor;
113374113413 size_t tmp_csz, csz;
113375- CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt;
113414+ CCOperation op = mode == SQLCIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt;
113376113415
113377113416 if(CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor) != kCCSuccess) return SQLITE_ERROR;
113378113417 if(CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz) != kCCSuccess) return SQLITE_ERROR;
0 commit comments