@@ -63,7 +63,7 @@ typedef struct {
6363 int pass_sz ;
6464 int reserve_sz ;
6565 int hmac_sz ;
66- int use_hmac ;
66+ unsigned int flags ;
6767 unsigned char * key ;
6868 unsigned char * hmac_key ;
6969 char * pass ;
@@ -79,7 +79,7 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *, cipher_ctx *);
7979/* prototype for pager HMAC function */
8080int sqlcipher_page_hmac (cipher_ctx * , Pgno , unsigned char * , int , unsigned char * );
8181
82- static int default_use_hmac = DEFAULT_USE_HMAC ;
82+ static unsigned int default_flags = DEFAULT_CIPHER_FLAGS ;
8383
8484struct codec_ctx {
8585 int kdf_salt_sz ;
@@ -185,6 +185,10 @@ int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
185185 ctx -> hmac_key = (unsigned char * ) sqlcipher_malloc (EVP_MAX_KEY_LENGTH );
186186 if (ctx -> key == NULL ) return SQLITE_NOMEM ;
187187 if (ctx -> hmac_key == NULL ) return SQLITE_NOMEM ;
188+
189+ /* setup default flags */
190+ ctx -> flags = default_flags ;
191+
188192 return SQLITE_OK ;
189193}
190194
@@ -216,7 +220,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
216220 && c1 -> fast_kdf_iter == c2 -> fast_kdf_iter
217221 && c1 -> key_sz == c2 -> key_sz
218222 && c1 -> pass_sz == c2 -> pass_sz
219- && c1 -> use_hmac == c2 -> use_hmac
223+ && c1 -> flags == c2 -> flags
220224 && c1 -> hmac_sz == c2 -> hmac_sz
221225 && (
222226 c1 -> pass == c2 -> pass
@@ -339,7 +343,8 @@ int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter, int
339343
340344/* set the global default flag for HMAC */
341345void sqlcipher_set_default_use_hmac (int use ) {
342- default_use_hmac = use ;
346+ if (use ) default_flags |= CIPHER_FLAG_HMAC ;
347+ else default_flags &= ~CIPHER_FLAG_HMAC ;
343348}
344349
345350/* set the codec flag for whether this individual database should be using hmac */
@@ -356,7 +361,15 @@ int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use) {
356361 CODEC_TRACE (("sqlcipher_codec_ctx_set_use_hmac: use=%d block_sz=%d md_size=%d reserve=%d\n" ,
357362 use , ctx -> read_ctx -> block_sz , ctx -> read_ctx -> hmac_sz , reserve ));
358363
359- ctx -> write_ctx -> use_hmac = ctx -> read_ctx -> use_hmac = use ;
364+
365+ if (use ) {
366+ ctx -> write_ctx -> flags |= CIPHER_FLAG_HMAC ;
367+ ctx -> read_ctx -> flags |= CIPHER_FLAG_HMAC ;
368+ } else {
369+ ctx -> write_ctx -> flags &= ~CIPHER_FLAG_HMAC ;
370+ ctx -> read_ctx -> flags &= ~CIPHER_FLAG_HMAC ;
371+ }
372+
360373 ctx -> write_ctx -> reserve_sz = ctx -> read_ctx -> reserve_sz = reserve ;
361374
362375 return SQLITE_OK ;
@@ -449,9 +462,9 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
449462 if ((rc = sqlcipher_codec_ctx_set_fast_kdf_iter (ctx , FAST_PBKDF2_ITER , 0 )) != SQLITE_OK ) return rc ;
450463 if ((rc = sqlcipher_codec_ctx_set_pass (ctx , zKey , nKey , 0 )) != SQLITE_OK ) return rc ;
451464
452- /* Use HMAC signatures by default. Note that codec_set_use_hmac will implicity call
453- codec_set_page_size to set the default */
454- if ((rc = sqlcipher_codec_ctx_set_use_hmac (ctx , default_use_hmac )) != SQLITE_OK ) return rc ;
465+ /* Note that use_hmac is a special case that requires recalculation of page size
466+ so we call set_use_hmac to perform setup */
467+ if ((rc = sqlcipher_codec_ctx_set_use_hmac (ctx , default_flags & CIPHER_FLAG_HMAC )) != SQLITE_OK ) return rc ;
455468
456469 if ((rc = sqlcipher_cipher_ctx_copy (ctx -> write_ctx , ctx -> read_ctx )) != SQLITE_OK ) return rc ;
457470
@@ -524,7 +537,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
524537 iv_in = in + size ;
525538
526539 /* hmac will be written immediately after the initialization vector. the remainder of the page reserve will contain
527- random bytes. note, these pointers are only valid when use_hmac is true */
540+ random bytes. note, these pointers are only valid when using hmac */
528541 hmac_in = in + size + c_ctx -> iv_sz ;
529542 hmac_out = out + size + c_ctx -> iv_sz ;
530543 out_start = out ; /* note the original position of the output buffer pointer, as out will be rewritten during encryption */
@@ -546,7 +559,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
546559 memcpy (iv_out , iv_in , c_ctx -> iv_sz ); /* copy the iv from the input to output buffer */
547560 }
548561
549- if (c_ctx -> use_hmac && (mode == CIPHER_DECRYPT )) {
562+ if (( c_ctx -> flags & CIPHER_FLAG_HMAC ) && (mode == CIPHER_DECRYPT )) {
550563 if (sqlcipher_page_hmac (c_ctx , pgno , in , size + c_ctx -> iv_sz , hmac_out ) != SQLITE_OK ) {
551564 memset (out , 0 , page_sz );
552565 CODEC_TRACE (("codec_cipher: hmac operations failed for pgno=%d\n" , pgno ));
@@ -585,7 +598,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
585598 EVP_CIPHER_CTX_cleanup (& c_ctx -> ectx );
586599 assert (size == csz );
587600
588- if (c_ctx -> use_hmac && (mode == CIPHER_ENCRYPT )) {
601+ if (( c_ctx -> flags & CIPHER_FLAG_HMAC ) && (mode == CIPHER_ENCRYPT )) {
589602 sqlcipher_page_hmac (c_ctx , pgno , out_start , size + c_ctx -> iv_sz , hmac_out );
590603 }
591604
@@ -630,7 +643,7 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
630643 /* if this context is setup to use hmac checks, generate a seperate and different
631644 key for HMAC. In this case, we use the output of the previous KDF as the input to
632645 this KDF run. This ensures a distinct but predictable HMAC key. */
633- if (c_ctx -> use_hmac ) {
646+ if (c_ctx -> flags & CIPHER_FLAG_HMAC ) {
634647 int i ;
635648
636649 /* start by copying the kdf key into the hmac salt slot
0 commit comments