Skip to content

Commit 99d36b4

Browse files
committed
relocate test flag management to crypto_impl.c
1 parent e883653 commit 99d36b4

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

src/crypto.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@
3939
#include "sqlcipher_ext.h"
4040
#endif
4141

42-
#ifdef SQLCIPHER_TEST
43-
static int cipher_test_flags = 0;
44-
#endif
45-
46-
/* Generate code to return a string value */
4742
static void codec_vdbe_return_string(Parse *pParse, const char *zLabel, const char *value, int value_type){
4843
Vdbe *v = sqlite3GetVdbe(pParse);
4944
sqlite3VdbeSetNumCols(v, 1);
@@ -119,13 +114,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
119114
if( sqlite3StrICmp(zLeft,"cipher_test")==0 ){
120115
if( zRight ) {
121116
if(sqlite3StrICmp(zRight, "fail_next_encrypt")) {
122-
cipher_test_flags ^= TEST_FAIL_NEXT_ENCRYPT;
117+
sqlcipher_set_test_flags(sqlcipher_get_test_flags() ^ TEST_FAIL_NEXT_ENCRYPT);
123118
} else
124119
if(sqlite3StrICmp(zRight, "fail_next_decrypt")) {
125-
cipher_test_flags ^= TEST_FAIL_NEXT_DECRYPT;
120+
sqlcipher_set_test_flags(sqlcipher_get_test_flags() ^ TEST_FAIL_NEXT_DECRYPT);
126121
}
127122
} else {
128-
char *flags = sqlite3_mprintf("%d", cipher_test_flags);
123+
char *flags = sqlite3_mprintf("%d", sqlcipher_get_test_flags());
129124
codec_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC);
130125
}
131126
}else
@@ -714,7 +709,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
714709

715710
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_DECRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
716711
#ifdef SQLCIPHER_TEST
717-
if((cipher_test_flags & TEST_FAIL_NEXT_ENCRYPT) > 0) rc = SQLITE_ERROR;
712+
if((sqlcipher_get_test_flags() & TEST_FAIL_NEXT_ENCRYPT) > 0) rc = SQLITE_ERROR;
718713
#endif
719714
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
720715
sqlcipher_memset((unsigned char*) buffer+offset, 0, page_sz-offset);
@@ -739,7 +734,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
739734
}
740735
rc = sqlcipher_page_cipher(ctx, cctx, pgno, CIPHER_ENCRYPT, page_sz - offset, pData + offset, (unsigned char*)buffer + offset);
741736
#ifdef SQLCIPHER_TEST
742-
if((cipher_test_flags & TEST_FAIL_NEXT_DECRYPT) > 0) rc = SQLITE_ERROR;
737+
if((sqlcipher_get_test_flags() & TEST_FAIL_NEXT_DECRYPT) > 0) rc = SQLITE_ERROR;
743738
#endif
744739
if(rc != SQLITE_OK) { /* clear results of failed cipher operation and set error */
745740
sqlcipher_memset((unsigned char*)buffer+offset, 0, page_sz-offset);

src/crypto.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ static int cipher_isHex(const unsigned char *hex, int sz){
195195
#ifdef SQLCIPHER_TEST
196196
#define TEST_FAIL_NEXT_ENCRYPT (1ul << 0) /* 1 */
197197
#define TEST_FAIL_NEXT_DECRYPT (1ul << 1) /* 2 */
198+
int sqlcipher_get_test_flags(void);
199+
void sqlcipher_set_test_flags(int);
198200
#endif
199201

200202
/* extensions defined in crypto_impl.c */

src/crypto_impl.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444
#endif
4545
#endif
4646

47+
#ifdef SQLCIPHER_TEST
48+
static volatile int cipher_test_flags = 0;
49+
#endif
50+
51+
/* Generate code to return a string value */
52+
4753
static volatile unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
4854
static volatile unsigned char hmac_salt_mask = HMAC_SALT_MASK;
4955
static volatile int default_kdf_iter = PBKDF2_ITER;
@@ -396,6 +402,16 @@ char* sqlcipher_version() {
396402
return version;
397403
}
398404

405+
#ifdef SQLCIPHER_TEST
406+
int sqlcipher_get_test_flags() {
407+
return cipher_test_flags;
408+
}
409+
410+
void sqlcipher_set_test_flags(int flags) {
411+
cipher_test_flags = flags;
412+
}
413+
#endif
414+
399415
/**
400416
* Initialize new cipher_ctx struct. This function will allocate memory
401417
* for the cipher context and for the key

0 commit comments

Comments
 (0)