diff --git a/src/crypto.c b/src/crypto.c index 1e159683..262318b7 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -98,7 +98,10 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c if( zRight ) { if(ctx) sqlcipher_codec_ctx_set_cipher(ctx, zRight, 2); // change cipher for both }else { - if(ctx) sqlcipher_codec_ctx_get_cipher(pParse, ctx, 2); + if(ctx) { + codec_vdbe_return_static_string(pParse, "cipher", + sqlcipher_codec_ctx_get_cipher(ctx, 2)); + } } }else 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 if( zRight ) { if(ctx) sqlcipher_codec_ctx_set_kdf_iter(ctx, atoi(zRight), 2); // change of RW PBKDF2 iteration } else { - if(ctx) sqlcipher_codec_ctx_get_kdf_iter(pParse, ctx, 2); + if(ctx) { + char *kdf_iter = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_kdf_iter(ctx, 2)); + codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter); + sqlite3_free(kdf_iter); + } } }else if( sqlite3StrICmp(zLeft, "fast_kdf_iter")==0 && zRight ){ @@ -126,7 +133,9 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c rc = codec_set_btree_to_codec_pagesize(db, pDb, ctx); if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); } else { - sqlcipher_codec_ctx_get_cipher_pagesize(pParse, ctx); + char * page_size = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_pagesize(ctx)); + codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size); + sqlite3_free(page_size); } } }else @@ -134,7 +143,9 @@ int codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const c if( zRight ) { sqlcipher_set_default_use_hmac(sqlite3GetBoolean(zRight,1)); } else { - sqlcipher_get_default_use_hmac(pParse); + char *default_use_hmac = sqlite3_mprintf("%d", sqlcipher_get_default_use_hmac()); + codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac); + sqlite3_free(default_use_hmac); } }else 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 if(rc != SQLITE_OK) sqlcipher_codec_ctx_set_error(ctx, rc); } } else { - if(ctx) sqlcipher_codec_ctx_get_use_hmac(pParse, ctx, 2); + if(ctx) { + char *hmac_flag = sqlite3_mprintf("%d", sqlcipher_codec_ctx_get_use_hmac(ctx, 2)); + codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag); + sqlite3_free(hmac_flag); + } } }else if( sqlite3StrICmp(zLeft,"cipher_hmac_pgno")==0 ){ diff --git a/src/crypto.h b/src/crypto.h index e44f82a3..5e2e308d 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -166,20 +166,26 @@ int sqlcipher_codec_ctx_get_pagesize(codec_ctx *); int sqlcipher_codec_ctx_get_reservesize(codec_ctx *); int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int); +int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int); + void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx); int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int); int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int); +const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx); void* sqlcipher_codec_ctx_get_data(codec_ctx *); void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **); void sqlcipher_set_default_use_hmac(int use); +int sqlcipher_get_default_use_hmac(); + void sqlcipher_set_hmac_salt_mask(unsigned char mask); int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use); +int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx); int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag); int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag); diff --git a/src/crypto_impl.c b/src/crypto_impl.c index e434c527..1ff86fe3 100644 --- a/src/crypto_impl.c +++ b/src/crypto_impl.c @@ -346,13 +346,10 @@ int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int return SQLITE_OK; } -int sqlcipher_codec_ctx_get_cipher(Parse *pParse, codec_ctx *ctx, int for_ctx) { +const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx) { cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; EVP_CIPHER *evp_cipher = c_ctx->evp_cipher; - const char* name = EVP_CIPHER_name(evp_cipher); - codec_vdbe_return_static_string(pParse, "cipher", name); - - return SQLITE_OK; + return EVP_CIPHER_name(evp_cipher); } 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) return SQLITE_OK; } -int sqlcipher_codec_ctx_get_kdf_iter(Parse *pParse, codec_ctx *ctx, int for_ctx) { +int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int for_ctx) { cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - char *kdf_iter = sqlite3_mprintf("%d", c_ctx->kdf_iter); - codec_vdbe_return_static_string(pParse, "kdf_iter", kdf_iter); - sqlite3_free(kdf_iter); - - return SQLITE_OK; + return c_ctx->kdf_iter; } 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) { else default_flags &= ~CIPHER_FLAG_HMAC; } -void sqlcipher_set_hmac_salt_mask(unsigned char mask) { - hmac_salt_mask = mask; +int sqlcipher_get_default_use_hmac() { + return default_flags & CIPHER_FLAG_HMAC > 0; } -int sqlcipher_get_default_use_hmac(Parse *pParse) { - int default_use_hmac_set = default_flags & CIPHER_FLAG_HMAC > 0; - char *default_use_hmac = sqlite3_mprintf("%d", default_use_hmac_set); - codec_vdbe_return_static_string(pParse, "cipher_default_use_hmac", default_use_hmac); - sqlite3_free(default_use_hmac); - - return SQLITE_OK; +void sqlcipher_set_hmac_salt_mask(unsigned char mask) { + hmac_salt_mask = mask; } /* 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) { return SQLITE_OK; } -int sqlcipher_codec_ctx_get_use_hmac(Parse *pParse, codec_ctx *ctx, int for_ctx) { +int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx) { cipher_ctx * c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx; - int hmac_flag_set = c_ctx->flags & CIPHER_FLAG_HMAC > 0; - char *hmac_flag = sqlite3_mprintf("%d", hmac_flag_set); - codec_vdbe_return_static_string(pParse, "cipher_use_hmac", hmac_flag); - sqlite3_free(hmac_flag); - - return SQLITE_OK; + return c_ctx->flags & CIPHER_FLAG_HMAC > 0; } 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) { return SQLITE_OK; } - void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) { CODEC_TRACE(("sqlcipher_codec_ctx_set_error: ctx=%p, error=%d\n", ctx, error)); sqlite3pager_sqlite3PagerSetError(ctx->pBt->pBt->pPager, error); ctx->pBt->pBt->db->errCode = error; } -int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) { - return ctx->page_sz; -} - int sqlcipher_codec_ctx_get_reservesize(codec_ctx *ctx) { return ctx->read_ctx->reserve_sz; } @@ -501,13 +479,8 @@ int sqlcipher_codec_ctx_set_pagesize(codec_ctx *ctx, int size) { return SQLITE_OK; } -int sqlcipher_codec_ctx_get_cipher_pagesize(Parse *pParse, codec_ctx *ctx) { - int page_size_value = ctx->page_sz; - char *page_size = sqlite3_mprintf("%d", page_size_value); - codec_vdbe_return_static_string(pParse, "cipher_page_size", page_size); - sqlite3_free(page_size); - - return SQLITE_OK; +int sqlcipher_codec_ctx_get_pagesize(codec_ctx *ctx) { + return ctx->page_sz; } int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_file *fd, const void *zKey, int nKey) {