8000 remove internal mutexing and move fortuna initialization to activate · magic-coder/sqlcipher@3659159 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3659159

Browse files
committed
remove internal mutexing and move fortuna initialization to activate
1 parent 5b639be commit 3659159

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,13 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
296296

297297
sqlcipher_activate(); /* perform internal initialization for sqlcipher */
298298

299+
sqlite3_mutex_enter(db->mutex);
300+
299301
/* point the internal codec argument against the contet to be prepared */
300302
rc = sqlcipher_codec_ctx_init(&ctx, pDb, pDb->pBt->pBt->pPager, fd, zKey, nKey);
301303

302304
if(rc != SQLITE_OK) return rc; /* initialization failed, do not attach potentially corrupted context */
303305

304-
sqlite3_mutex_enter(db->mutex);
305-
306306
sqlite3pager_sqlite3PagerSetCodec(sqlite3BtreePager(pDb->pBt), sqlite3Codec, NULL, sqlite3FreeCodecArg, (void *) ctx);
307307

308308
codec_set_btree_to_codec_pagesize(db, pDb, ctx);

src/crypto_libtomcrypt.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,25 @@ static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
1717

1818
static int sqlcipher_ltc_activate(void *ctx) {
1919
ltc_ctx *ltc = (ltc_ctx*)ctx;
20-
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
20+
int random_buffer_sz = 256;
21+
unsigned char random_buffer[random_buffer_sz];
22+
2123
if(ltc_init == 0) {
2224
if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
2325
if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
2426
if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR;
25-
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
26-
if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx *)) != SQLITE_OK) return SQLITE_ERROR;
2727
ltc_init = 1;
2828
}
29-
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
29+

30+
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
31+
sqlite3_randomness(random_buffer_sz, &random_buffer);
32+
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
33+
return SQLITE_ERROR;
34+
}
35+
if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx *)) != SQLITE_OK) return SQLITE_ERROR;
36+
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
37+
return SQLITE_ERROR;
38+
}
3039
return SQLITE_OK;
3140
}
3241

@@ -41,16 +50,15 @@ static const char* sqlcipher_ltc_get_provider_name(void *ctx) {
4150

4251
static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
4352
ltc_ctx *ltc = (ltc_ctx*)ctx;
53+
/*
4454
int random_buffer_sz = 256;
4555
char random_buffer[random_buffer_sz];
4656
4757
sqlite3_randomness(random_buffer_sz, &random_buffer);
4858
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
4959
return SQLITE_ERROR;
5060
}
51-
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
52-
return SQLITE_ERROR;
53-
}
61+
*/
5462
fortuna_read(buffer, length, &(ltc->prng));
5563
return SQLITE_OK;
5664
}
@@ -65,7 +73,6 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un
6573
if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR;
6674
if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR;
6775
if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR;
68-
sqlcipher_ltc_add_random(ctx, out, outlen);
6976
return SQLITE_OK;
7077
}
7178

src/crypto_openssl.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
2727
sqlcipher_openssl_deactivate() will free the EVP structures.
2828
*/
2929
static int sqlcipher_openssl_activate(void *ctx) {
30-
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
31-
3230
/* we'll initialize openssl and increment the internal init counter
3331
but only if it hasn't been initalized outside of SQLCipher by this program
3432
e.g. on startup */
@@ -45,7 +43,6 @@ static int sqlcipher_openssl_activate(void *ctx) {
4543
}
4644
openssl_init_count++;
4745
}
48-
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
4946
}
5047

5148
/* deactivate SQLCipher, most imporantly decremeting the activation count and

0 commit comments

Comments
 (0)
0