Skip to content

Commit bc05812

Browse files
Static ltc prng state, add partitioned data to prng
1 parent 930b441 commit bc05812

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/crypto_libtomcrypt.c

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,31 @@ typedef struct {
3939
prng_state prng;
4040
} ltc_ctx;
4141

42+
static ltc_ctx *ltc_state = {0};
4243
static unsigned int random_block_sz = 32;
4344
static unsigned int ltc_init = 0;
45+
static unsigned int ltc_ref_count = 0;
46+
static sqlite3_mutex* ltc_rand_mutex = NULL;
4447

4548
static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
4649
ltc_ctx *ltc = (ltc_ctx*)ctx;
50+
int rc = 0;
4751
int block_count = length / random_block_sz;
52+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
53+
sqlite3_mutex_enter(ltc_rand_mutex);
54+
#endif
4855
for(int block_idx = 0; block_idx < block_count; block_idx++){
49-
int rc = fortuna_add_entropy(buffer, random_block_sz, &(ltc->prng));
56+
rc = fortuna_add_entropy(buffer, random_block_sz, &(ltc->prng));
5057
buffer += random_block_sz;
51-
if(rc != CRYPT_OK) {
52-
return SQLITE_ERROR;
58+
rc = rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
59+
if(rc != SQLITE_OK) {
60+
break;
5361
}
5462
}
55-
return SQLITE_OK;
63+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
64+
sqlite3_mutex_leave(ltc_rand_mutex);
65+
#endif
66+
return rc;
5667
}
5768

5869
static int sqlcipher_ltc_activate(void *ctx) {
@@ -65,11 +76,16 @@ static int sqlcipher_ltc_activate(void *ctx) {
6576
if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
6677
if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
6778
if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR;
79+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
80+
if(ltc_rand_mutex == NULL){
81+
ltc_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
82+
}
83+
#endif
84+
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) {
85+
return SQLITE_ERROR;
86+
}
6887
ltc_init = 1;
6988
}
70-
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) {
71-
return SQLITE_ERROR;
72-
}
7389
sqlite3_randomness(random_buffer_sz, random_buffer);
7490
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
7591
return SQLITE_ERROR;
@@ -81,12 +97,20 @@ static int sqlcipher_ltc_activate(void *ctx) {
8197
return SQLITE_ERROR;
8298
}
8399
sqlcipher_free(random_buffer, random_buffer_sz);
100+
ltc_ref_count++;
84101
return SQLITE_OK;
85102
}
86103

87104
static int sqlcipher_ltc_deactivate(void *ctx) {
88105
ltc_ctx *ltc = (ltc_ctx*)ctx;
89-
fortuna_done(&(ltc->prng));
106+
ltc_ref_count--;
107+
if(ltc_ref_count == 0){
108+
fortuna_done(&(ltc->prng));
109+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
110+
sqlite3_mutex_free(ltc_rand_mutex);
111+
ltc_rand_mutex = NULL;
112+
#endif
113+
}
90114
return SQLITE_OK;
91115
}
92116

@@ -190,15 +214,20 @@ static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) {
190214
}
191215

192216
static int sqlcipher_ltc_ctx_init(void **ctx) {
193-
*ctx = sqlcipher_malloc(sizeof(ltc_ctx));
217+
if(!ltc_state){
218+
ltc_state = sqlcipher_malloc(sizeof(ltc_ctx));
219+
}
220+
*ctx = ltc_state;
194221
if(*ctx == NULL) return SQLITE_NOMEM;
195222
sqlcipher_ltc_activate(*ctx);
196223
return SQLITE_OK;
197224
}
198225

199226
static int sqlcipher_ltc_ctx_free(void **ctx) {
200227
sqlcipher_ltc_deactivate(&ctx);
201-
sqlcipher_free(*ctx, sizeof(ltc_ctx));
228+
if(ltc_ref_count == 0){
229+
sqlcipher_free(*ctx, sizeof(ltc_ctx));
230+
}
202231
return SQLITE_OK;
203232
}
204233

0 commit comments

Comments
 (0)