Skip to content

Commit 41d4c53

Browse files
committed
rework some pragma related interfaces
1 parent caf3865 commit 41d4c53

3 files changed

Lines changed: 38 additions & 44 deletions

File tree

src/crypto.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
9898
if( zRight ) {
9999
if(ctx) sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both
100100
}else {
101-
if(ctx) sqlcipher_codec_ctx_get_cipher(pParse, ctx, 2);
101+
if(ctx) {
102+
codec_vdbe_return_static_string(pParse, "cipher",
103+
sqlcipher_codec_ctx_get_cipher(ctx, 2));
104+
}
102105
}
103106
}else
104107
if( sqlite3StrICmp(zLeft, "rekey_cipher")==0 && zRight ){
@@ -108,7 +111,11 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
108111
if( zRight ) {
109112
if(ctx) sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration
110113
} else {
111-
if(ctx) sqlcipher_codec_ctx_get_kdf_iter(pParse, ctx, 2);
114+
if(ctx) {
115+
char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_kdf_iter(ctx, 2));
116+
codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter);
117+
sqlite3_free(kdf_iter);
118+
}
112119
}
113120
}else
114121
if( sqlite3StrICmp(zLeft, "fast_kdf_iter")==0 && zRight ){
@@ -126,15 +133,19 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
126133
rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx);
127134
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
128135
} else {
129-
sqlcipher_codec_ctx_get_cipher_pagesize(pParse, ctx);
136+
char * page_size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_pagesize(ctx));
137+
codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size);
138+
sqlite3_free(page_size);
130139
}
131140
}
132141
}else
133142
if( sqlite3StrICmp(zLeft,"cipher_default_use_hmac")==0 ){
134143
if( zRight ) {
135144
sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1));
136145
} else {
137-
sqlcipher_get_default_use_hmac(pParse);
146+
char *default_use_hmac = sqlite3_mprintf("%d", sqlcipher_get_default_use_hmac());
147+
codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac);
148+
sqlite3_free(default_use_hmac);
138149
}
139150
}else
140151
if( sqlite3StrICmp(zLeft,"cipher_use_hmac")==0 ){
@@ -148,7 +159,11 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c
148159
if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc);
149160
}
150161
} else {
151-
if(ctx) sqlcipher_codec_ctx_get_use_hmac(pParse, ctx, 2);
162+
if(ctx) {
163+
char *hmac_flag = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_use_hmac(ctx, 2));
164+
codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag);
165+
sqlite3_free(hmac_flag);
166+
}
152167
}
153168
}else
154169
if( sqlite3StrICmp(zLeft,"cipher_hmac_pgno")==0 ){

src/crypto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,26 @@ int sqlcipher_codec_ctx_get_pagesize(codec_ctx *);
166166
int sqlcipher_codec_ctx_get_reservesize(codec_ctx *);
167167

168168
int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
169+
int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
170+
169171
void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
170172

171173
int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int);
172174

173175
int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int);
176+
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
174177

175178
void* sqlcipher_codec_ctx_get_data(codec_ctx *);
176179

177180
void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
178181

179182
void sqlcipher_set_default_use_hmac(int use);
183+
int sqlcipher_get_default_use_hmac();
184+
180185
void sqlcipher_set_hmac_salt_mask(unsigned char mask);
181186

182187
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
188+
int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx);
183189

184190
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
185191
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);

src/crypto_impl.c

Lines changed: 12 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,10 @@ int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int
346346
return SQLITE_OK;
347347
}
348348

349-
int sqlcipher_codec_ctx_get_cipher(Parse *pParse, codec_ctx *ctx, int for_ctx) {
349+
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx) {
350350
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
351351
EVP_CIPHER *evp_cipher = c_ctx->evp_cipher;
352-
const char* name = EVP_CIPHER_name(evp_cipher);
353-
codec_vdbe_return_static_string(pParse, "cipher", name);
354-
355-
return SQLITE_OK;
352+
return EVP_CIPHER_name(evp_cipher);
356353
}
357354

358355
int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx) {
@@ -369,13 +366,9 @@ int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx)
369366
return SQLITE_OK;
370367
}
371368

372-
int sqlcipher_codec_ctx_get_kdf_iter(Parse *pParse, codec_ctx *ctx, int for_ctx) {
369+
int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int for_ctx) {
373370
cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
374-
char *kdf_iter = sqlite3_mprintf("%d", c_ctx->kdf_iter);
375-
codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter);
376-
sqlite3_free(kdf_iter);
377-
378-
return SQLITE_OK;
371+
return c_ctx->kdf_iter;
379372
}
380373

381374
int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter, int for_ctx) {
@@ -398,17 +391,12 @@ void sqlcipher_set_default_use_hmac(int use) {
398391
else default_flags &= ~CIPHER_FLAG_HMAC;
399392
}
400393

401-
void sqlcipher_set_hmac_salt_mask(unsigned char mask) {
402-
hmac_salt_mask = mask;
394+
int sqlcipher_get_default_use_hmac() {
395+
return default_flags & CIPHER_FLAG_HMAC > 0;
403396
}
404397

405-
int sqlcipher_get_default_use_hmac(Parse *pParse) {
406-
int default_use_hmac_set = default_flags & CIPHER_FLAG_HMAC > 0;
407-
char *default_use_hmac = sqlite3_mprintf("%d", default_use_hmac_set);
408-
codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac);
409-
sqlite3_free(default_use_hmac);
410-
411-
return SQLITE_OK;
398+
void sqlcipher_set_hmac_salt_mask(unsigned char mask) {
399+
hmac_salt_mask = mask;
412400
}
413401

414402
/* set the codec flag for whether this individual database should be using hmac */
@@ -437,14 +425,9 @@ int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
437425
return SQLITE_OK;
438426
}
439427

440-
int sqlcipher_codec_ctx_get_use_hmac(Parse *pParse, codec_ctx *ctx, int for_ctx) {
428+
int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx) {
441429
cipher_ctx * c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
442-
int hmac_flag_set = c_ctx->flags & CIPHER_FLAG_HMAC > 0;
443-
char *hmac_flag = sqlite3_mprintf("%d", hmac_flag_set);
444-
codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag);
445-
sqlite3_free(hmac_flag);
446-
447-
return SQLITE_OK;
430+
return c_ctx->flags & CIPHER_FLAG_HMAC > 0;
448431
}
449432

450433
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag) {
@@ -459,17 +442,12 @@ int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag) {
459442
return SQLITE_OK;
460443
}
461444

462-
463445
void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) {
464446
CODEC_TRACE(("sqlcipher_codec_ctx_set_error: ctx=%p, error=%d\n", ctx, error));
465447
sqlite3pager_sqlite3PagerSetError(ctx->pBt->pBt->pPager, error);
466448
ctx->pBt->pBt->db->errCode = error;
467449
}
468450

469-
int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) {
470-
return ctx->page_sz;
471-
}
472-
473451
int sqlcipher_codec_ctx_get_reservesize(codec_ctx *ctx) {
474452
return ctx->read_ctx->reserve_sz;
475453
}
@@ -501,13 +479,8 @@ int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) {
501479
return SQLITE_OK;
502480
}
503481

504-
int sqlcipher_codec_ctx_get_cipher_pagesize(Parse *pParse, codec_ctx *ctx) {
505-
int page_size_value = ctx->page_sz;
506-
char *page_size = sqlite3_mprintf("%d", page_size_value);
507-
codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size);
508-
sqlite3_free(page_size);
509-
510-
return SQLITE_OK;
482+
int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) {
483+
return ctx->page_sz;
511484
}
512485

513486
int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_file *fd, const void *zKey, int nKey) {

0 commit comments

Comments
 (0)