Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cipher_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/cipher_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions src/fastpbkdf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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, \
Expand Down Expand Up @@ -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, \
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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;
}
Expand Down
8 changes: 7 additions & 1 deletion src/sqlite3mc_vle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Loading