mirror of
https://github.com/status-im/sqlcipher.git
synced 2026-08-30 22:11:14 +00:00
remove internal mutexing and move fortuna initialization to activate
This commit is contained in:
+2
-2
@@ -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);
|
||||
|
||||
@@ -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, <c, 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, <c, 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user