Skip to content

Commit 4ded3fc

Browse files
committed
Merge branch 'multi-lib' of github.com:sqlcipher/sqlcipher into multi-lib
2 parents 35b4d9a + 9c4d19e commit 4ded3fc

6 files changed

Lines changed: 42 additions & 14 deletions

File tree

src/crypto.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
9191

9292
CODEC_TRACE(("codec_pragma: entered db=%p iDb=%d pParse=%p zLeft=%s zRight=%s ctx=%p\n", db, iDb, pParse, zLeft, zRight, ctx));
9393

94+
if( sqlite3StrICmp(zLeft, "cipher_provider")==0 && !zRight ){
95+
if(ctx) { codec_vdbe_return_static_string(pParse, "cipher_provider",
96+
sqlcipher_codec_get_cipher_provider(ctx));
97+
}
98+
} else
9499
if( sqlite3StrICmp(zLeft, "cipher_version")==0 && !zRight ){
95100
codec_vdbe_return_static_string(pParse, "cipher_version", codec_get_cipher_version());
96101
}else

src/crypto.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
195195
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
196196
int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx);
197197

198+
const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
198199
#endif
199200
#endif
200201
/* END CRYPTO */

src/crypto_impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,5 +795,8 @@ int sqlcipher_codec_key_copy(codec_ctx *ctx, int source) {
795795
}
796796
}
797797

798+
const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx) {
799+
return ctx->read_ctx->provider->get_provider_name(ctx->read_ctx);
800+
}
798801

799802
#endif

src/crypto_libtomcrypt.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,46 @@
33
#include "sqlcipher.h"
44
#include <tomcrypt.h>
55

6+
typedef struct {
7+
prng_state prng;
8+
} ltc_ctx;
9+
610
static unsigned int ltc_init = 0;
711

812
static int sqlcipher_ltc_activate(void *ctx) {
13+
ltc_ctx *ltc = (ltc_ctx*)ctx;
914
sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
1015
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;
1520
ltc_init = 1;
1621
}
1722
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
23+
return SQLITE_OK;
1824
}
1925

2026
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";
2133
}
2234

2335
static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
24-
prng_state prng;
2536
int random_value;
2637
int random_buffer_sz = 256;
2738
char random_buffer[random_buffer_sz];
2839

29-
if(fortuna_start(&prng) != CRYPT_OK) return SQLITE_ERROR;
40+
ltc_ctx *ltc = (ltc_ctx*)ctx;
3041
sqlite3_randomness(sizeof(random_value), &random_value);
3142
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));
3646
return SQLITE_OK;
3747
}
3848

@@ -68,7 +78,6 @@ static int sqlcipher_ltc_cipher(void *ctx, int mode, unsigned char *key, int key
6878
symmetric_CBC cbc;
6979

7080
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;
7281
if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR;
7382
rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
7483
if(rc != CRYPT_OK) return SQLITE_ERROR;
@@ -109,18 +118,22 @@ static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) {
109118
}
110119

111120
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);
113124
return SQLITE_OK;
114125
}
115126

116127
static int sqlcipher_ltc_ctx_free(void **ctx) {
117128
sqlcipher_ltc_deactivate(&ctx);
129+
sqlcipher_free(*ctx, sizeof(ltc_ctx));
118130
return SQLITE_OK;
119131
}
120132

121133
int sqlcipher_ltc_setup(sqlcipher_provider *p) {
122134
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;
124137
p->random = sqlcipher_ltc_random;
125138
p->hmac = sqlcipher_ltc_hmac;
126139
p->kdf = sqlcipher_ltc_kdf;

src/crypto_openssl.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ static int sqlcipher_openssl_deactivate(void *ctx) {
6060
sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
6161
}
6262

63+
static const char* sqlcipher_openssl_get_provider_name(void *ctx) {
64+
return "openssl";
65+
}
66+
6367
/* generate a defined number of pseudorandom bytes */
6468
static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
6569
return RAND_bytes((unsigned char *)buffer, length);
@@ -148,7 +152,8 @@ static int sqlcipher_openssl_ctx_free(void **ctx) {
148152

149153
int sqlcipher_openssl_setup(sqlcipher_provider *p) {
150154
p->activate = sqlcipher_openssl_activate;
151-
p->deactivate = sqlcipher_openssl_deactivate;
155+
p->deactivate = sqlcipher_openssl_deactivate;
156+
p->get_provider_name = sqlcipher_openssl_get_provider_name;
152157
p->random = sqlcipher_openssl_random;
153158
p->hmac = sqlcipher_openssl_hmac;
154159
p->kdf = sqlcipher_openssl_kdf;

src/sqlcipher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
typedef struct {
4040
int (*activate)(void *ctx);
4141
int (*deactivate)(void *ctx);
42+
const char* (*get_provider_name)(void *ctx);
4243
int (*random)(void *ctx, void *buffer, int length);
4344
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);
4445
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)