remove internal mutexing and move fortuna initialization to activate

This commit is contained in:
Stephen Lombardo
2013-06-06 15:48:11 -04:00
parent 5b639be3c4
commit 3659159b72
3 changed files with 17 additions and 13 deletions
+2 -2
View File
@@ -296,13 +296,13 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
sqlcipher_activate(); /* perform internal initialization for sqlcipher */
sqlite3_mutex_enter(db->mutex);
/* point the internal codec argument against the contet to be prepared */
rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, fd, zKey, nKey);
if(rc != SQLITE_OK) return rc; /* initialization failed, do not attach potentially corrupted context */
sqlite3_mutex_enter(db->mutex);
sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
codec_set_btree_to_codec_pagesize(db, pDb, ctx);
+15 -8
View File
@@ -17,16 +17,25 @@ static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
static int sqlcipher_ltc_activate(void *ctx) {
ltc_ctx *ltc = (ltc_ctx*)ctx;
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
int random_buffer_sz = 256;
unsigned char random_buffer[random_buffer_sz];
if(ltc_init == 0) {
if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR;
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx *)) != SQLITE_OK) return SQLITE_ERROR;
ltc_init = 1;
}
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));

if(fortuna_start(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
sqlite3_randomness(random_buffer_sz, &random_buffer);
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
return SQLITE_ERROR;
}
if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx *)) != SQLITE_OK) return SQLITE_ERROR;
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
return SQLITE_ERROR;
}
return SQLITE_OK;
}
@@ -41,6 +50,7 @@ static const char* sqlcipher_ltc_get_provider_name(void *ctx) {
static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
ltc_ctx *ltc = (ltc_ctx*)ctx;
/*
int random_buffer_sz = 256;
char random_buffer[random_buffer_sz];
@@ -48,9 +58,7 @@ static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
return SQLITE_ERROR;
}
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
return SQLITE_ERROR;
}
*/
fortuna_read(buffer, length, &(ltc->prng));
return SQLITE_OK;
}
@@ -65,7 +73,6 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un
if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR;
if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR;
if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR;
sqlcipher_ltc_add_random(ctx, out, outlen);
return SQLITE_OK;
}
-3
View File
@@ -27,8 +27,6 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
sqlcipher_openssl_deactivate() will free the EVP structures.
*/
static int sqlcipher_openssl_activate(void *ctx) {
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
/* we'll initialize openssl and increment the internal init counter
but only if it hasn't been initalized outside of SQLCipher by this program
e.g. on startup */
@@ -45,7 +43,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
}
openssl_init_count++;
}
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
}
/* deactivate SQLCipher, most imporantly decremeting the activation count and