Skip to content

Commit e44eb58

Browse files
committed
standardize flag handling
1 parent 15fe89b commit e44eb58

File tree

3 files changed

+41
-54
lines changed

3 files changed

+41
-54
lines changed

src/crypto.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
114114
if( zRight ) {
115115
unsigned int flags = sqlcipher_get_test_flags();
116116
if(sqlite3_stricmp(zRight, "fail_encrypt")==0) {
117-
flags |= TEST_FAIL_ENCRYPT;
117+
SQLCIPHER_FLAG_SET(flags,TEST_FAIL_ENCRYPT);
118118
} else
119119
if(sqlite3_stricmp(zRight, "fail_decrypt")==0) {
120-
flags |= TEST_FAIL_DECRYPT;
120+
SQLCIPHER_FLAG_SET(flags,TEST_FAIL_DECRYPT);
121121
} else
122122
if(sqlite3_stricmp(zRight, "fail_migrate")==0) {
123-
flags |= TEST_FAIL_MIGRATE;
123+
SQLCIPHER_FLAG_SET(flags,TEST_FAIL_MIGRATE);
124124
}
125125
sqlcipher_set_test_flags(flags);
126126
}
@@ -129,13 +129,13 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
129129
if( zRight ) {
130130
unsigned int flags = sqlcipher_get_test_flags();
131131
if(sqlite3_stricmp(zRight, "fail_encrypt")==0) {
132-
flags &= ~TEST_FAIL_ENCRYPT;
132+
SQLCIPHER_FLAG_UNSET(flags,TEST_FAIL_ENCRYPT);
133133
} else
134134
if(sqlite3_stricmp(zRight, "fail_decrypt")==0) {
135-
flags &= ~TEST_FAIL_DECRYPT;
135+
SQLCIPHER_FLAG_UNSET(flags,TEST_FAIL_DECRYPT);
136136
} else
137137
if(sqlite3_stricmp(zRight, "fail_migrate")==0) {
138-
flags &= ~TEST_FAIL_MIGRATE;
138+
SQLCIPHER_FLAG_UNSET(flags,TEST_FAIL_MIGRATE);
139139
}
140140
sqlcipher_set_test_flags(flags);
141141
}
@@ -310,22 +310,22 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
310310
char *deprecation = "PRAGMA cipher_hmac_pgno is deprecated, please remove from use";
311311
/* clear both pgno endian flags */
312312
if(sqlite3_stricmp(zRight, "le") == 0) {
313-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_BE_PGNO);
314-
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_LE_PGNO);
313+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO);
314+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_LE_PGNO);
315315
} else if(sqlite3_stricmp(zRight, "be") == 0) {
316-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_LE_PGNO);
317-
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_BE_PGNO);
316+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO);
317+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_BE_PGNO);
318318
} else if(sqlite3_stricmp(zRight, "native") == 0) {
319-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_LE_PGNO);
320-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_BE_PGNO);
319+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_LE_PGNO);
320+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_BE_PGNO);
321321
}
322322
sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", deprecation, P4_TRANSIENT);
323323
sqlite3_log(SQLITE_WARNING, deprecation);
324324

325325
} else {
326-
if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_LE_PGNO)) {
326+
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) {
327327
sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "le", P4_TRANSIENT);
328-
} else if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_BE_PGNO)) {
328+
} else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) {
329329
sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "be", P4_TRANSIENT);
330330
} else {
331331
sqlcipher_vdbe_return_string(pParse, "cipher_hmac_pgno", "native", P4_TRANSIENT);
@@ -771,7 +771,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
771771
sqlcipher_memset((unsigned char*) buffer+offset, 0, page_sz-offset);
772772
sqlcipher_codec_ctx_set_error(ctx, rc);
773773
} else {
774-
ctx->flags |= CIPHER_FLAG_KEY_USED; /* inline to avoid function call */
774+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED);
775775
}
776776
memcpy(pData, buffer, page_sz); /* copy buffer data back to pData and return */
777777
return pData;
@@ -806,7 +806,7 @@ static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
806806
sqlcipher_codec_ctx_set_error(ctx, rc);
807807
return NULL;
808808
}
809-
ctx->flags |= CIPHER_FLAG_KEY_USED; /* inline to avoid function call */
809+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_KEY_USED);
810810
return buffer; /* return persistent buffer data, pData remains intact */
811811
break;
812812

@@ -838,7 +838,7 @@ int sqlcipherCodecAttach(sqlite3* db, int nDb, const void *zKey, int nKey) {
838838

839839
ctx = (codec_ctx*) sqlcipherPagerGetCodec(pDb->pBt->pBt->pPager);
840840

841-
if(ctx != NULL && sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_KEY_USED)) {
841+
if(ctx != NULL && SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) {
842842
/* there is already a codec attached to this database, so we should not proceed */
843843
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipherCodecAttach: no codec attached to db, exiting");
844844
return SQLITE_OK;

src/crypto.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,18 @@ void sqlite3pager_reset(Pager *pPager);
102102
#define PBKDF2_ITER 256000
103103
#endif
104104

105-
/* possible flags for cipher_ctx->flags */
105+
#define SQLCIPHER_FLAG_GET(FLAG,BIT) ((FLAG & BIT) != 0)
106+
#define SQLCIPHER_FLAG_SET(FLAG,BIT) FLAG |= BIT
107+
#define SQLCIPHER_FLAG_UNSET(FLAG,BIT) FLAG &= ~BIT
108+
109+
/* possible flags for codec_ctx->flags */
106110
#define CIPHER_FLAG_HMAC (1 << 0)
107111
#define CIPHER_FLAG_LE_PGNO (1 << 1)
108112
#define CIPHER_FLAG_BE_PGNO (1 << 2)
109113
#define CIPHER_FLAG_KEY_USED (1 << 3)
110114
#define CIPHER_FLAG_HAS_KDF_SALT (1 << 4)
111115

116+
112117
#ifndef DEFAULT_CIPHER_FLAGS
113118
#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
114119
#endif
@@ -287,10 +292,6 @@ unsigned char sqlcipher_get_hmac_salt_mask(void);
287292
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
288293
int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx);
289294

290-
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
291-
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
292-
int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag);
293-
294295
const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
295296
int sqlcipher_codec_ctx_migrate(codec_ctx *ctx);
296297
int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz);

src/crypto_impl.c

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -664,12 +664,12 @@ int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *ctx) {
664664

665665
/* set the global default flag for HMAC */
666666
void sqlcipher_set_default_use_hmac(int use) {
667-
if(use) default_flags |= CIPHER_FLAG_HMAC;
668-
else default_flags &= ~CIPHER_FLAG_HMAC;
667+
if(use) SQLCIPHER_FLAG_SET(default_flags, CIPHER_FLAG_HMAC);
668+
else SQLCIPHER_FLAG_UNSET(default_flags,CIPHER_FLAG_HMAC);
669669
}
670670

671671
int sqlcipher_get_default_use_hmac() {
672-
return (default_flags & CIPHER_FLAG_HMAC) != 0;
672+
return SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC);
673673
}
674674

675675
void sqlcipher_set_hmac_salt_mask(unsigned char mask) {
@@ -683,16 +683,16 @@ unsigned char sqlcipher_get_hmac_salt_mask() {
683683
/* set the codec flag for whether this individual database should be using hmac */
684684
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
685685
if(use) {
686-
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_HMAC);
686+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC);
687687
} else {
688-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_HMAC);
688+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC);
689689
}
690690

691691
return sqlcipher_codec_ctx_reserve_setup(ctx);
692692
}
693693

694694
int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx) {
695-
return (ctx->flags & CIPHER_FLAG_HMAC) != 0;
695+
return SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC);
696696
}
697697

698698
/* the length of plaintext header size must be:
@@ -761,20 +761,6 @@ int sqlcipher_codec_ctx_get_kdf_algorithm(codec_ctx *ctx) {
761761
return ctx->kdf_algorithm;
762762
}
763763

764-
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag) {
765-
ctx->flags |= flag;
766-
return SQLITE_OK;
767-
}
768-
769-
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag) {
770-
ctx->flags &= ~flag;
771-
return SQLITE_OK;
772-
}
773-
774-
int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag) {
775-
return (ctx->flags & flag) != 0;
776-
}
777-
778764
void sqlcipher_codec_ctx_set_error(codec_ctx *ctx, int error) {
779765
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_error: ctx=%p, error=%d", ctx, error);
780766
sqlite3pager_error(ctx->pBt->pBt->pPager, error);
@@ -792,7 +778,7 @@ void* sqlcipher_codec_ctx_get_data(codec_ctx *ctx) {
792778
static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) {
793779
sqlite3_file *fd = sqlite3PagerFile(ctx->pBt->pBt->pPager);
794780

795-
if(sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_HAS_KDF_SALT)) {
781+
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
796782
return SQLITE_OK; /* don't reload salt when not needed */
797783
}
798784

@@ -805,14 +791,14 @@ static int sqlcipher_codec_ctx_init_kdf_salt(codec_ctx *ctx) {
805791
return SQLITE_ERROR;
806792
}
807793
}
808-
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_HAS_KDF_SALT);
794+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
809795
return SQLITE_OK;
810796
}
811797

812798
int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int size) {
813799
if(size >= ctx->kdf_salt_sz) {
814800
memcpy(ctx->kdf_salt, salt, ctx->kdf_salt_sz);
815-
sqlcipher_codec_ctx_set_flag(ctx, CIPHER_FLAG_HAS_KDF_SALT);
801+
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT);
816802
return SQLITE_OK;
817803
}
818804
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_set_kdf_salt: attempt to set salt of incorrect size %d", size);
@@ -821,7 +807,7 @@ int sqlcipher_codec_ctx_set_kdf_salt(codec_ctx *ctx, unsigned char *salt, int si
821807

822808
int sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx, void** salt) {
823809
int rc = SQLITE_OK;
824-
if(!sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_HAS_KDF_SALT)) {
810+
if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
825811
if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
826812
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_get_kdf_salt: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
827813
}
@@ -973,8 +959,8 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, const voi
973959

974960
/* Note that use_hmac is a special case that requires recalculation of page size
975961
so we call set_use_hmac to perform setup */
976-
if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, default_flags & CIPHER_FLAG_HMAC)) != SQLITE_OK) {
977-
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, default_flags & CIPHER_FLAG_HMAC);
962+
if((rc = sqlcipher_codec_ctx_set_use_hmac(ctx, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC))) != SQLITE_OK) {
963+
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_codec_ctx_init: error %d setting use_hmac %d", rc, SQLCIPHER_FLAG_GET(default_flags, CIPHER_FLAG_HMAC));
978964
return rc;
979965
}
980966

@@ -1051,9 +1037,9 @@ static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, uns
10511037
backwards compatibility on the most popular platforms, but can optionally be configured
10521038
to use either big endian or native byte ordering via pragma. */
10531039

1054-
if(ctx->flags & CIPHER_FLAG_LE_PGNO) { /* compute hmac using little endian pgno*/
1040+
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_LE_PGNO)) { /* compute hmac using little endian pgno*/
10551041
sqlcipher_put4byte_le(pgno_raw, pgno);
1056-
} else if(ctx->flags & CIPHER_FLAG_BE_PGNO) { /* compute hmac using big endian pgno */
1042+
} else if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_BE_PGNO)) { /* compute hmac using big endian pgno */
10571043
sqlite3Put4byte(pgno_raw, pgno); /* sqlite3Put4byte converts 32bit uint to big endian */
10581044
} else { /* use native byte ordering */
10591045
memcpy(pgno_raw, &pgno, sizeof(pgno));
@@ -1109,7 +1095,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
11091095
memcpy(iv_out, iv_in, ctx->iv_sz); /* copy the iv from the input to output buffer */
11101096
}
11111097

1112-
if((ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) {
1098+
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) {
11131099
if(sqlcipher_page_hmac(ctx, c_ctx, pgno, in, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
11141100
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: hmac operation on decrypt failed for pgno=%d", pgno);
11151101
goto error;
@@ -1140,7 +1126,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
11401126
goto error;
11411127
};
11421128

1143-
if((ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
1129+
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC) && (mode == CIPHER_ENCRYPT)) {
11441130
if(sqlcipher_page_hmac(ctx, c_ctx, pgno, out_start, size + ctx->iv_sz, hmac_out) != SQLITE_OK) {
11451131
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_page_cipher: hmac operation on encrypt failed for pgno=%d", pgno);
11461132
goto error;
@@ -1178,7 +1164,7 @@ static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
11781164
if(c_ctx->pass && c_ctx->pass_sz) { /* if key material is present on the context for derivation */
11791165

11801166
/* if necessary, initialize the salt from the header or random source */
1181-
if(!sqlcipher_codec_ctx_get_flag(ctx, CIPHER_FLAG_HAS_KDF_SALT)) {
1167+
if(!SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HAS_KDF_SALT)) {
11821168
if((rc = sqlcipher_codec_ctx_init_kdf_salt(ctx)) != SQLITE_OK) {
11831169
sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_cipher_ctx_key_derive: error %d from sqlcipher_codec_ctx_init_kdf_salt", rc);
11841170
return rc;
@@ -1538,7 +1524,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
15381524
if( rc!=SQLITE_OK ) goto handle_error;
15391525

15401526
sqlcipherCodecGetKey(db, db->nDb - 1, (void**)&keyspec, &keyspec_sz);
1541-
sqlcipher_codec_ctx_unset_flag(ctx, CIPHER_FLAG_KEY_USED);
1527+
SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_KEY_USED);
15421528
sqlcipherCodecAttach(db, 0, keyspec, keyspec_sz);
15431529

15441530
srcfile = sqlite3PagerFile(pSrc->pBt->pPager);

0 commit comments

Comments
 (0)