Skip to content

Commit b5ece46

Browse files
Remove ltc_ctx, adjust sqlcipher_ltc_add_random behavior
1 parent 8348d8e commit b5ece46

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

src/crypto_libtomcrypt.c

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,44 @@
3535
#include "sqlcipher.h"
3636
#include <tomcrypt.h>
3737

38-
typedef struct {
39-
prng_state prng;
40-
} ltc_ctx;
41-
42-
static ltc_ctx *ltc_state = {0};
38+
static prng_state prng;
4339
static unsigned int random_block_sz = 32;
4440
static unsigned int ltc_init = 0;
4541
static unsigned int ltc_ref_count = 0;
4642
static sqlite3_mutex* ltc_rand_mutex = NULL;
4743

4844
static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
49-
ltc_ctx *ltc = (ltc_ctx*)ctx;
50-
int rc, block_idx = 0;
51-
int block_count = length / random_block_sz;
45+
int rc = 0;
46+
int data_to_read = length;
47+
int block_sz = data_to_read < random_block_sz ? data_to_read : random_block_sz;
5248
const unsigned char * data = (const unsigned char *)buffer;
5349
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
5450
sqlite3_mutex_enter(ltc_rand_mutex);
5551
#endif
56-
for(; block_idx < block_count; block_idx++){
57-
rc = fortuna_add_entropy(data, random_block_sz, &(ltc->prng));
58-
data += random_block_sz;
59-
rc = rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
60-
if(rc != SQLITE_OK) {
61-
break;
52+
while(data_to_read > 0){
53+
rc = fortuna_add_entropy(data, block_sz, &prng);
54+
rc = rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
55+
if(rc != SQLITE_OK){
56+
break;
57+
}
58+
data_to_read -= block_sz;
59+
if(data_to_read > 0){
60+
block_sz = data_to_read < random_block_sz ? data_to_read : random_block_sz;
61+
data += block_sz;
62+
}
6263
}
63-
}
64+
fortuna_ready(&prng);
6465
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
6566
sqlite3_mutex_leave(ltc_rand_mutex);
6667
#endif
6768
return rc;
6869
}
6970

7071
static int sqlcipher_ltc_activate(void *ctx) {
71-
ltc_ctx *ltc = (ltc_ctx*)ctx;
7272
int random_buffer_sz = sizeof(char) * 32;
7373
unsigned char *random_buffer = sqlcipher_malloc(random_buffer_sz);
7474
sqlcipher_memset(random_buffer, 0, random_buffer_sz);
75-
75+
7676
if(ltc_init == 0) {
7777
if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
7878
if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
@@ -82,7 +82,7 @@ static int sqlcipher_ltc_activate(void *ctx) {
8282
ltc_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
8383
}
8484
#endif
85-
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) {
85+
if(fortuna_start(&prng) != CRYPT_OK) {
8686
return SQLITE_ERROR;
8787
}
8888
ltc_init = 1;
@@ -91,22 +91,15 @@ static int sqlcipher_ltc_activate(void *ctx) {
9191
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
9292
return SQLITE_ERROR;
9393
}
94-
if(sqlcipher_ltc_add_random(ctx, &ltc, sizeof(ltc_ctx*)) != SQLITE_OK) {
95-
return SQLITE_ERROR;
96-
}
97-
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
98-
return SQLITE_ERROR;
99-
}
10094
sqlcipher_free(random_buffer, random_buffer_sz);
10195
ltc_ref_count++;
10296
return SQLITE_OK;
10397
}
10498

10599
static int sqlcipher_ltc_deactivate(void *ctx) {
106-
ltc_ctx *ltc = (ltc_ctx*)ctx;
107100
ltc_ref_count--;
108101
if(ltc_ref_count == 0){
109-
fortuna_done(&(ltc->prng));
102+
fortuna_done(&prng);
110103
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
111104
sqlite3_mutex_free(ltc_rand_mutex);
112105
ltc_rand_mutex = NULL;
@@ -120,13 +113,14 @@ static const char* sqlcipher_ltc_get_provider_name(void *ctx) {
120113
}
121114

122115
static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
123-
ltc_ctx *ltc = (ltc_ctx*)ctx;
124116
int rc;
125-
126-
if((rc = fortuna_ready(&(ltc->prng))) != CRYPT_OK) {
127-
return SQLITE_ERROR;
128-
}
129-
fortuna_read(buffer, length, &(ltc->prng));
117+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
118+
sqlite3_mutex_enter(ltc_rand_mutex);
119+
#endif
120+
fortuna_read(buffer, length, &prng);
121+
#ifndef SQLCIPHER_LTC_NO_MUTEX_RAND
122+
sqlite3_mutex_leave(ltc_rand_mutex);
123+
#endif
130124
return SQLITE_OK;
131125
}
132126

@@ -145,7 +139,6 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un
145139

146140
static int sqlcipher_ltc_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
147141
int rc, hash_idx;
148-
ltc_ctx *ltc = (ltc_ctx*)ctx;
149142
unsigned long outlen = key_sz;
150143
unsigned long random_buffer_sz = sizeof(char) * 256;
151144
unsigned char *random_buffer = sqlcipher_malloc(random_buffer_sz);
@@ -206,7 +199,6 @@ static int sqlcipher_ltc_get_hmac_sz(void *ctx) {
206199
}
207200

208201
static int sqlcipher_ltc_ctx_copy(void *target_ctx, void *source_ctx) {
209-
memcpy(target_ctx, source_ctx, sizeof(ltc_ctx));
210202
return SQLITE_OK;
211203
}
212204

@@ -215,20 +207,12 @@ static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) {
215207
}
216208

217209
static int sqlcipher_ltc_ctx_init(void **ctx) {
218-
if(!ltc_state){
219-
ltc_state = sqlcipher_malloc(sizeof(ltc_ctx));
220-
}
221-
*ctx = ltc_state;
222-
if(*ctx == NULL) return SQLITE_NOMEM;
223-
sqlcipher_ltc_activate(*ctx);
210+
sqlcipher_ltc_activate(NULL);
224211
return SQLITE_OK;
225212
}
226213

227214
static int sqlcipher_ltc_ctx_free(void **ctx) {
228215
sqlcipher_ltc_deactivate(&ctx);
229-
if(ltc_ref_count == 0){
230-
sqlcipher_free(*ctx, sizeof(ltc_ctx));
231-
}
232216
return SQLITE_OK;
233217
}
234218

0 commit comments

Comments
 (0)