4747#endif
4848#endif
4949
50- static unsigned char cipher_hmac_fixed_salt [HMAC_FIXED_SALT_SZ ] = HMAC_FIXED_SALT ;
51-
5250/* the default implementation of SQLCipher uses a cipher_ctx
5351 to keep track of read / write state separately. The following
5452 struct and associated functions are defined here */
@@ -58,7 +56,7 @@ typedef struct {
5856 EVP_CIPHER_CTX ectx ;
5957 HMAC_CTX hctx ;
6058 int kdf_iter ;
61- int hmac_kdf_iter ;
59+ int fast_kdf_iter ;
6260 int key_sz ;
6361 int iv_sz ;
6462 int block_sz ;
@@ -205,7 +203,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
205203 c1 -> evp_cipher == c2 -> evp_cipher
206204 && c1 -> iv_sz == c2 -> iv_sz
207205 && c1 -> kdf_iter == c2 -> kdf_iter
208- && c1 -> hmac_kdf_iter == c2 -> hmac_kdf_iter
206+ && c1 -> fast_kdf_iter == c2 -> fast_kdf_iter
209207 && c1 -> key_sz == c2 -> key_sz
210208 && c1 -> pass_sz == c2 -> pass_sz
211209 && (
@@ -313,11 +311,11 @@ int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx)
313311 return SQLITE_OK ;
314312}
315313
316- int sqlcipher_codec_ctx_set_hmac_kdf_iter (codec_ctx * ctx , int hmac_kdf_iter , int for_ctx ) {
314+ int sqlcipher_codec_ctx_set_fast_kdf_iter (codec_ctx * ctx , int fast_kdf_iter , int for_ctx ) {
317315 cipher_ctx * c_ctx = for_ctx ? ctx -> write_ctx : ctx -> read_ctx ;
318316 int rc ;
319317
320- c_ctx -> hmac_kdf_iter = hmac_kdf_iter ;
318+ c_ctx -> fast_kdf_iter = fast_kdf_iter ;
321319 c_ctx -> derive_key = 1 ;
322320
323321 if (for_ctx == 2 )
@@ -429,7 +427,7 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
429427
430428 if ((rc = sqlcipher_codec_ctx_set_cipher (ctx , CIPHER , 0 )) != SQLITE_OK ) return rc ;
431429 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 ;
430+ if ((rc = sqlcipher_codec_ctx_set_fast_kdf_iter (ctx , FAST_PBKDF2_ITER , 0 )) != SQLITE_OK ) return rc ;
433431 if ((rc = sqlcipher_codec_ctx_set_pass (ctx , zKey , nKey , 0 )) != SQLITE_OK ) return rc ;
434432
435433 /* Use HMAC signatures by default. Note that codec_set_use_hmac will implicity call
@@ -561,19 +559,19 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
561559int sqlcipher_cipher_ctx_key_derive (codec_ctx * ctx , cipher_ctx * c_ctx ) {
562560 CODEC_TRACE (("codec_key_derive: entered c_ctx->pass=%s, c_ctx->pass_sz=%d \
563561 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" ,
562+ ctx->hmac_kdf_salt=%d, c_ctx->fast_kdf_iter =%d c_ctx->key_sz=%d\n" ,
565563 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 ));
564+ ctx -> hmac_kdf_salt , c_ctx -> fast_kdf_iter , c_ctx -> key_sz ));
567565
568566
569567 if (c_ctx -> pass && c_ctx -> pass_sz ) { // if pass is not null
570568 if (c_ctx -> pass_sz == ((c_ctx -> key_sz * 2 )+ 3 ) && sqlite3StrNICmp (c_ctx -> pass ,"x'" , 2 ) == 0 ) {
571569 int n = c_ctx -> pass_sz - 3 ; /* adjust for leading x' and tailing ' */
572- const char * z = c_ctx -> pass + 2 ; /* adjust lead offset of x' */
570+ const char * z = c_ctx -> pass + 2 ; /* adjust lead offset of x' */
573571 CODEC_TRACE (("codec_key_derive: deriving key from hex\n" ));
574572 cipher_hex2bin (z , n , c_ctx -> key );
575573 } else {
576- CODEC_TRACE (("codec_key_derive: deriving key using PBKDF2\n" ));
574+ CODEC_TRACE (("codec_key_derive: deriving key using full PBKDF2 with %d iterations \n" , c_ctx -> kdf_iter ));
577575 PKCS5_PBKDF2_HMAC_SHA1 ( c_ctx -> pass , c_ctx -> pass_sz ,
578576 ctx -> kdf_salt , ctx -> kdf_salt_sz ,
579577 c_ctx -> kdf_iter , c_ctx -> key_sz , c_ctx -> key );
@@ -592,15 +590,15 @@ int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
592590 easy to derive and publically known, is not the same as the salt used
593591 to generate the encryption key */
594592 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 ] ;
593+ for (i = 0 ; i < ctx -> kdf_salt_sz ; i ++ ) {
594+ ctx -> hmac_kdf_salt [i ] ^= HMAC_SALT_MASK ;
597595 }
598596
599597 CODEC_TRACE (("codec_key_derive: deriving hmac key from encryption key using PBKDF2 with %d iterations\n" ,
600- HMAC_PBKDF2_ITER ));
598+ c_ctx -> fast_kdf_iter ));
601599 PKCS5_PBKDF2_HMAC_SHA1 ( (const char * )c_ctx -> key , c_ctx -> key_sz ,
602600 ctx -> hmac_kdf_salt , ctx -> kdf_salt_sz ,
603- c_ctx -> hmac_kdf_iter , c_ctx -> key_sz , c_ctx -> hmac_key );
601+ c_ctx -> fast_kdf_iter , c_ctx -> key_sz , c_ctx -> hmac_key );
604602 }
605603
606604 c_ctx -> derive_key = 0 ;
0 commit comments