Skip to content

Commit eb0b371

Browse files
Add entropy to fortuna pool during hmac derivation
1 parent dc2cc9a commit eb0b371

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/crypto_libtomcrypt.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ typedef struct {
99

1010
static unsigned int ltc_init = 0;
1111

12+
static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
13+
ltc_ctx *ltc = (ltc_ctx*)ctx;
14+
int rc = fortuna_add_entropy(buffer, length, &(ltc->prng));
15+
return rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
16+
}
17+
1218
static int sqlcipher_ltc_activate(void *ctx) {
1319
ltc_ctx *ltc = (ltc_ctx*)ctx;
1420
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
@@ -40,7 +46,7 @@ static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
4046
ltc_ctx *ltc = (ltc_ctx*)ctx;
4147
sqlite3_randomness(sizeof(random_value), &random_value);
4248
sqlite3_snprintf(random_buffer_sz, random_buffer, "%d", random_value);
43-
if(fortuna_add_entropy(random_buffer, random_buffer_sz, &(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
49+
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) return SQLITE_ERROR;
4450
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR;
4551
fortuna_read(buffer, length, &(ltc->prng));
4652
return SQLITE_OK;
@@ -56,6 +62,7 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un
5662
if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR;
5763
if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR;
5864
if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR;
65+
sqlcipher_ltc_add_random(ctx, out, outlen);
5966
return SQLITE_OK;
6067
}
6168

@@ -148,6 +155,7 @@ int sqlcipher_ltc_setup(sqlcipher_provider *p) {
148155
p->ctx_cmp = sqlcipher_ltc_ctx_cmp;
149156
p->ctx_init = sqlcipher_ltc_ctx_init;
150157
p->ctx_free = sqlcipher_ltc_ctx_free;
158+
p->add_random = sqlcipher_ltc_add_random;
151159
}
152160

153161

src/sqlcipher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ typedef struct {
4040
int (*activate)(void *ctx);
4141
int (*deactivate)(void *ctx);
4242
const char* (*get_provider_name)(void *ctx);
43+
int (*add_random)(void *ctx, void *buffer, int length);
4344
int (*random)(void *ctx, void *buffer, int length);
4445
int (*hmac)(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out);
4546
int (*kdf)(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key);

0 commit comments

Comments
 (0)