4747#endif
4848#endif
4949
50+ static unsigned char cipher_hmac_fixed_salt [HMAC_FIXED_SALT_SZ ] = HMAC_FIXED_SALT ;
5051
5152/* the default implementation of SQLCipher uses a cipher_ctx
5253 to keep track of read / write state separately. The following
@@ -57,6 +58,7 @@ typedef struct {
5758 EVP_CIPHER_CTX ectx ;
5859 HMAC_CTX hctx ;
5960 int kdf_iter ;
61+ int hmac_kdf_iter ;
6062 int key_sz ;
6163 int iv_sz ;
6264 int block_sz ;
@@ -74,7 +76,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *, cipher_ctx *);
7476int sqlcipher_cipher_ctx_copy (cipher_ctx * , cipher_ctx * );
7577int sqlcipher_cipher_ctx_init (cipher_ctx * * );
7678int sqlcipher_cipher_ctx_set_pass (cipher_ctx * , const void * , int );
77- int sqlcipher_cipher_ctx_key_derive (codec_ctx * , cipher_ctx * );
79+ int sqlcipher_cipher_ctx_key_derive (codec_ctx * , cipher_ctx * );
7880
7981/* prototype for pager HMAC function */
8082int sqlcipher_page_hmac (cipher_ctx * , Pgno , unsigned char * , int , unsigned char * );
@@ -83,6 +85,7 @@ struct codec_ctx {
8385 int kdf_salt_sz ;
8486 int page_sz ;
8587 unsigned char * kdf_salt ;
88+ unsigned char * hmac_kdf_salt ;
8689 unsigned char * buffer ;
8790 Btree * pBt ;
8891 cipher_ctx * read_ctx ;
@@ -202,6 +205,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
202205 c1 -> evp_cipher == c2 -> evp_cipher
203206 && c1 -> iv_sz == c2 -> iv_sz
204207 && c1 -> kdf_iter == c2 -> kdf_iter
208+ && c1 -> hmac_kdf_iter == c2 -> hmac_kdf_iter
205209 && c1 -> key_sz == c2 -> key_sz
206210 && c1 -> pass_sz == c2 -> pass_sz
207211 && (
@@ -309,6 +313,21 @@ int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx)
309313 return SQLITE_OK ;
310314}
311315
316+ int sqlcipher_codec_ctx_set_hmac_kdf_iter (codec_ctx * ctx , int hmac_kdf_iter , int for_ctx ) {
317+ cipher_ctx * c_ctx = for_ctx ? ctx -> write_ctx : ctx -> read_ctx ;
318+ int rc ;
319+
320+ c_ctx -> hmac_kdf_iter = hmac_kdf_iter ;
321+ c_ctx -> derive_key = 1 ;
322+
323+ if (for_ctx == 2 )
324+ if ((rc = sqlcipher_cipher_ctx_copy ( for_ctx ? ctx -> read_ctx : ctx -> write_ctx , c_ctx )) != SQLITE_OK )
325+ return rc ;
326+
327+ return SQLITE_OK ;
328+ }
329+
330+
312331int sqlcipher_codec_ctx_set_use_hmac (codec_ctx * ctx , int use ) {
313332 int reserve = EVP_MAX_IV_LENGTH ; /* base reserve size will be IV only */
314333
@@ -386,6 +405,13 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
386405 ctx -> kdf_salt = sqlcipher_malloc (ctx -> kdf_salt_sz );
387406 if (ctx -> kdf_salt == NULL ) return SQLITE_NOMEM ;
388407
408+ /* allocate space for separate hmac salt data. We want the
409+ HMAC derivation salt to be different than the encryption
410+ key derivation salt */
411+ ctx -> hmac_kdf_salt = sqlcipher_malloc (ctx -> kdf_salt_sz );
412+ if (ctx -> hmac_kdf_salt == NULL ) return SQLITE_NOMEM ;
413+
414+
389415 /*
390416 Always overwrite page size and set to the default because the first page of the database
391417 in encrypted and thus sqlite can't effectively determine the pagesize. this causes an issue in
@@ -403,6 +429,7 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
403429
404430 if ((rc = sqlcipher_codec_ctx_set_cipher (ctx , CIPHER , 0 )) != SQLITE_OK ) return rc ;
405431 if ((rc = sqlcipher_codec_ctx_set_kdf_iter (ctx , PBKDF2_ITER , 0 )) != SQLITE_OK ) return rc ;
432+ if ((rc = sqlcipher_codec_ctx_set_hmac_kdf_iter (ctx , HMAC_PBKDF2_ITER , 0 )) != SQLITE_OK ) return rc ;
406433 if ((rc = sqlcipher_codec_ctx_set_pass (ctx , zKey , nKey , 0 )) != SQLITE_OK ) return rc ;
407434
408435 /* Use HMAC signatures by default. Note that codec_set_use_hmac will implicity call
@@ -422,6 +449,7 @@ void sqlcipher_codec_ctx_free(codec_ctx **iCtx) {
422449 codec_ctx * ctx = * iCtx ;
423450 CODEC_TRACE (("codec_ctx_free: entered iCtx=%d\n" , iCtx ));
424451 sqlcipher_free (ctx -> kdf_salt , ctx -> kdf_salt_sz );
452+ sqlcipher_free (ctx -> hmac_kdf_salt , ctx -> kdf_salt_sz );
425453 sqlcipher_free (ctx -> buffer , 0 );
426454 sqlcipher_cipher_ctx_free (& ctx -> read_ctx );
427455 sqlcipher_cipher_ctx_free (& ctx -> write_ctx );
@@ -532,9 +560,11 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
532560 */
533561int sqlcipher_cipher_ctx_key_derive (codec_ctx * ctx , cipher_ctx * c_ctx ) {
534562 CODEC_TRACE (("codec_key_derive: entered c_ctx->pass=%s, c_ctx->pass_sz=%d \
535- ctx->kdf_salt=%d ctx->kdf_salt_sz=%d c_ctx->kdf_iter=%d c_ctx->key_sz=%d\n" ,
536- c_ctx -> pass , c_ctx -> pass_sz , ctx -> kdf_salt , ctx -> kdf_salt_sz ,
537- c_ctx -> kdf_iter , c_ctx -> key_sz ));
563+ ctx->kdf_salt=%d ctx->kdf_salt_sz=%d c_ctx->kdf_iter=%d \
564+ ctx->hmac_kdf_salt=%d, c_ctx->hmac_kdf_iter=%d c_ctx->key_sz=%d\n" ,
565+ c_ctx -> pass , c_ctx -> pass_sz , ctx -> kdf_salt , ctx -> kdf_salt_sz , c_ctx -> kdf_iter ,
566+ ctx -> hmac_kdf_salt , c_ctx -> hmac_kdf_iter , c_ctx -> key_sz ));
567+
538568
539569 if (c_ctx -> pass && c_ctx -> pass_sz ) { // if pass is not null
540570 if (c_ctx -> pass_sz == ((c_ctx -> key_sz * 2 )+ 3 ) && sqlite3StrNICmp (c_ctx -> pass ,"x'" , 2 ) == 0 ) {
@@ -554,10 +584,23 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
554584 key for HMAC. In this case, we use the output of the previous KDF as the input to
555585 this KDF run. This ensures a distinct but predictable HMAC key. */
556586 if (c_ctx -> use_hmac ) {
557- CODEC_TRACE (("codec_key_derive: deriving hmac key using PBKDF2\n" ));
587+ int i ;
588+
589+ /* start by copying the kdf key into the hmac salt slot
590+ then XOR it with the fixed hmac salt defined at compile time
591+ this ensures that the salt passed in to derive the hmac key, while
592+ easy to derive and publically known, is not the same as the salt used
593+ to generate the encryption key */
594+ memcpy (ctx -> hmac_kdf_salt , ctx -> kdf_salt , ctx -> kdf_salt_sz );
595+ for (i = 0 ; i < HMAC_FIXED_SALT_SZ && i < ctx -> kdf_salt_sz ; i ++ ) {
596+ ctx -> hmac_kdf_salt [i ] = ctx -> hmac_kdf_salt [i ] ^ cipher_hmac_fixed_salt [i ];
597+ }
598+
599+ CODEC_TRACE (("codec_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations\n" ,
600+ HMAC_PBKDF2_ITER ));
558601 PKCS5_PBKDF2_HMAC_SHA1 ( (const char * )c_ctx -> key , c_ctx -> key_sz ,
559- ctx -> kdf_salt , ctx -> kdf_salt_sz ,
560- c_ctx -> kdf_iter , c_ctx -> key_sz , c_ctx -> hmac_key );
602+ ctx -> hmac_kdf_salt , ctx -> kdf_salt_sz ,
603+ c_ctx -> hmac_kdf_iter , c_ctx -> key_sz , c_ctx -> hmac_key );
561604 }
562605
563606 c_ctx -> derive_key = 0 ;
0 commit comments