diff --git a/src/cipher_common.c b/src/cipher_common.c index 2434129c..9dc495a5 100644 --- a/src/cipher_common.c +++ b/src/cipher_common.c @@ -233,7 +233,7 @@ sqlite3mcCodecTerm(Codec* codec) globalCodecDescriptorTable[codec->m_writeCipherType - 1].m_freeCipher(codec->m_writeCipher); codec->m_writeCipher = NULL; } - memset(codec, 0, sizeof(Codec)); + sqlite3mcSecureZeroMemory(codec, sizeof(Codec)); } SQLITE_PRIVATE void diff --git a/src/cipher_config.c b/src/cipher_config.c index 021ca5b0..39f53c24 100644 --- a/src/cipher_config.c +++ b/src/cipher_config.c @@ -1074,6 +1074,7 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg) unsigned char* zHexKey = sqlite3_malloc(nValue/2); sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey); rc = sqlite3_key_v2(db, zDbName, zHexKey, nValue/2); + sqlite3mcSecureZeroMemory(zHexKey, nValue/2); sqlite3_free(zHexKey); if (rc == SQLITE_OK) { @@ -1124,6 +1125,7 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg) unsigned char* zHexKey = sqlite3_malloc(nValue/2); sqlite3mcConvertHex2Bin((const unsigned char*) pragmaValue, nValue, zHexKey); rc = sqlite3_rekey_v2(db, zDbName, zHexKey, nValue/2); + sqlite3mcSecureZeroMemory(zHexKey, nValue/2); sqlite3_free(zHexKey); if (rc == SQLITE_OK) { @@ -1289,6 +1291,7 @@ sqlite3mcCodecQueryParameters(sqlite3* db, const char* zDb, const char* zUri) if ((i & 1) != 0) zDecoded[i/2] = iByte; } sqlite3_key_v2(db, zDb, zDecoded, i/2); + sqlite3mcSecureZeroMemory(zDecoded, nKey); sqlite3_free(zDecoded); } else if ((zKey = sqlite3_uri_parameter(zUri, "key")) != 0) diff --git a/src/fastpbkdf2.c b/src/fastpbkdf2.c index f7a72651..b67366d8 100644 --- a/src/fastpbkdf2.c +++ b/src/fastpbkdf2.c @@ -20,6 +20,9 @@ #include "sha1.h" #include "sha2.h" +/* Defined in memory_secure.c */ +SQLITE_PRIVATE void sqlite3mcSecureZeroMemory(void* v, size_t n); + /* --- MSVC doesn't support C99 --- */ #if defined(_MSC_VER) && !defined(__clang__) #define restrict @@ -175,7 +178,10 @@ static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t ms /* And outer. */ \ _init(&ctx->outer); \ _update(&ctx->outer, blk_outer, sizeof blk_outer); \ + sqlite3mcSecureZeroMemory(blk_inner, sizeof blk_inner); \ + sqlite3mcSecureZeroMemory(blk_outer, sizeof blk_outer); \ } \ + sqlite3mcSecureZeroMemory(k, sizeof k); \ } \ \ static inline void HMAC_UPDATE(_name)(HMAC_CTX(_name) *ctx, \ @@ -238,6 +244,9 @@ static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t ms \ /* Reform result into output buffer. */ \ _xtract(&result, out); \ + sqlite3mcSecureZeroMemory(Ublock, sizeof Ublock); \ + sqlite3mcSecureZeroMemory(&ctx, sizeof ctx); \ + sqlite3mcSecureZeroMemory(&result, sizeof result); \ } \ \ static inline void PBKDF2(_name)(const uint8_t *pw, size_t npw, \ @@ -268,7 +277,9 @@ static inline void md_pad(uint8_t *block, size_t blocksz, size_t used, size_t ms offset = (counter - 1) * _hashsz; \ taken = MIN(nout - offset, _hashsz); \ memcpy(out + offset, block, taken); \ + sqlite3mcSecureZeroMemory(block, sizeof block); \ } \ + sqlite3mcSecureZeroMemory(&ctx, sizeof ctx); \ } static inline void sha1_extract(sha1_ctx *restrict ctx, uint8_t *restrict out) @@ -451,6 +462,7 @@ void sqlcipher_hmac(int algorithm, unsigned char* key, int nkey, unsigned char* HMAC_sha1_update(&hctx, in2, in2_sz); } HMAC_sha1_final(&hctx, out); + sqlite3mcSecureZeroMemory(&hctx, sizeof(hctx)); } break; @@ -464,6 +476,7 @@ void sqlcipher_hmac(int algorithm, unsigned char* key, int nkey, unsigned char* HMAC_sha256_update(&hctx, in2, in2_sz); } HMAC_sha256_final(&hctx, out); + sqlite3mcSecureZeroMemory(&hctx, sizeof(hctx)); } break; @@ -478,6 +491,7 @@ void sqlcipher_hmac(int algorithm, unsigned char* key, int nkey, unsigned char* HMAC_sha512_update(&hctx, in2, in2_sz); } HMAC_sha512_final(&hctx, out); + sqlite3mcSecureZeroMemory(&hctx, sizeof(hctx)); } break; } diff --git a/src/sqlite3mc_vle.c b/src/sqlite3mc_vle.c index d774d166..7edbf40c 100644 --- a/src/sqlite3mc_vle.c +++ b/src/sqlite3mc_vle.c @@ -353,6 +353,12 @@ static const char* VLE_CONTEXT_KEY = "vle_context"; // ---------- Helper Functions ---------- +static void vle_freeContext(void* ctx) +{ + sqlite3mcSecureZeroMemory(ctx, sizeof(VleContext)); + sqlite3_free(ctx); +} + static VleContext* vle_getContext(sqlite3* db) { VleContext* ctx = sqlite3_get_clientdata(db, VLE_CONTEXT_KEY); @@ -364,7 +370,7 @@ static VleContext* vle_getContext(sqlite3* db) ctx->flags = 0; ctx->nonceLen = VLE_NONCE_LEN; ctx->tagLen = VLE_TAG_LEN; - sqlite3_set_clientdata(db, VLE_CONTEXT_KEY, ctx, sqlite3_free); + sqlite3_set_clientdata(db, VLE_CONTEXT_KEY, ctx, vle_freeContext); } return ctx; }