47
47
#endif
48
48
#endif
49
49
50
+ static unsigned char cipher_hmac_fixed_salt [HMAC_FIXED_SALT_SZ ] = HMAC_FIXED_SALT ;
50
51
51
52
/* the default implementation of SQLCipher uses a cipher_ctx
52
53
to keep track of read / write state separately. The following
@@ -57,6 +58,7 @@ typedef struct {
57
58
EVP_CIPHER_CTX ectx ;
58
59
HMAC_CTX hctx ;
59
60
int kdf_iter ;
61
+ int hmac_kdf_iter ;
60
62
int key_sz ;
61
63
int iv_sz ;
62
64
int block_sz ;
@@ -74,7 +76,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *, cipher_ctx *);
74
76
int sqlcipher_cipher_ctx_copy (cipher_ctx * , cipher_ctx * );
75
77
int sqlcipher_cipher_ctx_init (cipher_ctx * * );
76
78
int 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 * );
78
80
79
81
/* prototype for pager HMAC function */
80
82
int sqlcipher_page_hmac (cipher_ctx * , Pgno , unsigned char * , int , unsigned char * );
@@ -83,6 +85,7 @@ struct codec_ctx {
83
85
int kdf_salt_sz ;
84
86
int page_sz ;
85
87
unsigned char * kdf_salt ;
88
+ unsigned char * hmac_kdf_salt ;
86
89
unsigned char * buffer ;
87
90
Btree * pBt ;
88
91
cipher_ctx * read_ctx ;
@@ -202,6 +205,7 @@ int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
202
205
c1 -> evp_cipher == c2 -> evp_cipher
203
206
&& c1 -> iv_sz == c2 -> iv_sz
204
207
&& c1 -> kdf_iter == c2 -> kdf_iter
208
+ && c1 -> hmac_kdf_iter == c2 -> hmac_kdf_iter
205
209
&& c1 -> key_sz == c2 -> key_sz
206
210
&& c1 -> pass_sz == c2 -> pass_sz
207
211
&& (
@@ -309,6 +313,21 @@ int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx)
309
313
return SQLITE_OK ;
310
314
}
311
315
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
+
312
331
int sqlcipher_codec_ctx_set_use_hmac (codec_ctx * ctx , int use ) {
313
332
int reserve = EVP_MAX_IV_LENGTH ; /* base reserve size will be IV only */
314
333
@@ -386,6 +405,13 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
386
405
ctx -> kdf_salt = sqlcipher_malloc (ctx -> kdf_salt_sz );
387
406
if (ctx -> kdf_salt == NULL ) return SQLITE_NOMEM ;
388
407
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
+
389
415
/*
390
416
Always overwrite page size and set to the default because the first page of the database
391
417
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
403
429
404
430
if ((rc = sqlcipher_codec_ctx_set_cipher (ctx , CIPHER , 0 )) != SQLITE_OK ) return rc ;
405
431
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 ;
406
433
if ((rc = sqlcipher_codec_ctx_set_pass (ctx , zKey , nKey , 0 )) != SQLITE_OK ) return rc ;
407
434
408
435
/* 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) {
422
449
codec_ctx * ctx = * iCtx ;
423
450
CODEC_TRACE (("codec_ctx_free: entered iCtx=%d\n" , iCtx ));
424
451
sqlcipher_free (ctx -> kdf_salt , ctx -> kdf_salt_sz );
452
+ sqlcipher_free (ctx -> hmac_kdf_salt , ctx -> kdf_salt_sz );
425
453
sqlcipher_free (ctx -> buffer , 0 );
426
454
sqlcipher_cipher_ctx_free (& ctx -> read_ctx );
427
455
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
532
560
*/
<
10000
code>533 561
int sqlcipher_cipher_ctx_key_derive (codec_ctx * ctx , cipher_ctx * c_ctx ) {
534
562
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
+
538
568
539
569
if (c_ctx -> pass && c_ctx -> pass_sz ) { // if pass is not null
540
570
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) {
554
584
key for HMAC. In this case, we use the output of the previous KDF as the input to
555
585
this KDF run. This ensures a distinct but predictable HMAC key. */
556
586
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 ));
558
601
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 );
561
604
}
562
605
563
606
c_ctx -> derive_key = 0 ;
0 commit comments