|
3 | 3 | #include "sqlcipher.h" |
4 | 4 | #include <tomcrypt.h> |
5 | 5 |
|
| 6 | +typedef struct { |
| 7 | + prng_state prng; |
| 8 | +} ltc_ctx; |
| 9 | + |
6 | 10 | static unsigned int ltc_init = 0; |
7 | 11 |
|
8 | 12 | static int sqlcipher_ltc_activate(void *ctx) { |
| 13 | + ltc_ctx *ltc = (ltc_ctx*)ctx; |
9 | 14 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
10 | 15 | if(ltc_init == 0) { |
11 | | - register_prng(&fortuna_desc); |
12 | | - register_cipher(&rijndael_desc); |
13 | | - register_hash(&sha256_desc); |
14 | | - register_hash(&sha1_desc); |
| 16 | + if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR; |
| 17 | + if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR; |
| 18 | + if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR; |
| 19 | + if(fortuna_start(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR; |
15 | 20 | ltc_init = 1; |
16 | 21 | } |
17 | 22 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 23 | + return SQLITE_OK; |
18 | 24 | } |
19 | 25 |
|
20 | 26 | static int sqlcipher_ltc_deactivate(void *ctx) { |
| 27 | + ltc_ctx *ltc = (ltc_ctx*)ctx; |
| 28 | + fortuna_done(&(ltc->prng)); |
| 29 | +} |
| 30 | + |
| 31 | +static const char* sqlcipher_ltc_get_provider_name(void *ctx) { |
| 32 | + return "libtomcrypt"; |
21 | 33 | } |
22 | 34 |
|
23 | 35 | static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) { |
24 | | - prng_state prng; |
25 | 36 | int random_value; |
26 | 37 | int random_buffer_sz = 256; |
27 | 38 | char random_buffer[random_buffer_sz]; |
28 | 39 |
|
29 | | - if(fortuna_start(&prng) != CRYPT_OK) return SQLITE_ERROR; |
| 40 | + ltc_ctx *ltc = (ltc_ctx*)ctx; |
30 | 41 | sqlite3_randomness(sizeof(random_value), &random_value); |
31 | 42 | sqlite3_snprintf(random_buffer_sz, random_buffer, "%d", random_value); |
32 | | - if(fortuna_add_entropy(random_buffer, random_buffer_sz, &prng) != CRYPT_OK) return SQLITE_ERROR; |
33 | | - if(fortuna_ready(&prng) != CRYPT_OK) return SQLITE_ERROR; |
34 | | - fortuna_read(buffer, length, &prng); |
35 | | - fortuna_done(&prng); |
| 43 | + if(fortuna_add_entropy(random_buffer, random_buffer_sz, &(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR; |
| 44 | + if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) return SQLITE_ERROR; |
| 45 | + fortuna_read(buffer, length, &(ltc->prng)); |
36 | 46 | return SQLITE_OK; |
37 | 47 | } |
38 | 48 |
|
@@ -68,7 +78,6 @@ static int sqlcipher_ltc_cipher(void *ctx, int mode, unsigned char *key, int key |
68 | 78 | symmetric_CBC cbc; |
69 | 79 |
|
70 | 80 | if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR; |
71 | | - if((hash_idx = find_hash("sha256")) == -1) return SQLITE_ERROR; |
72 | 81 | if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR; |
73 | 82 | rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc); |
74 | 83 | if(rc != CRYPT_OK) return SQLITE_ERROR; |
@@ -109,18 +118,22 @@ static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) { |
109 | 118 | } |
110 | 119 |
|
111 | 120 | static int sqlcipher_ltc_ctx_init(void **ctx) { |
112 | | - sqlcipher_ltc_activate(&ctx); |
| 121 | + *ctx = sqlcipher_malloc(sizeof(ltc_ctx)); |
| 122 | + if(*ctx == NULL) return SQLITE_NOMEM; |
| 123 | + sqlcipher_ltc_activate(*ctx); |
113 | 124 | return SQLITE_OK; |
114 | 125 | } |
115 | 126 |
|
116 | 127 | static int sqlcipher_ltc_ctx_free(void **ctx) { |
117 | 128 | sqlcipher_ltc_deactivate(&ctx); |
| 129 | + sqlcipher_free(*ctx, sizeof(ltc_ctx)); |
118 | 130 | return SQLITE_OK; |
119 | 131 | } |
120 | 132 |
|
121 | 133 | int sqlcipher_ltc_setup(sqlcipher_provider *p) { |
122 | 134 | p->activate = sqlcipher_ltc_activate; |
123 | | - p->deactivate = sqlcipher_ltc_deactivate; |
| 135 | + p->deactivate = sqlcipher_ltc_deactivate; |
| 136 | + p->get_provider_name = sqlcipher_ltc_get_provider_name; |
124 | 137 | p->random = sqlcipher_ltc_random; |
125 | 138 | p->hmac = sqlcipher_ltc_hmac; |
126 | 139 | p->kdf = sqlcipher_ltc_kdf; |
|
0 commit comments