@@ -969,12 +969,11 @@ static int sqlcipher_page_hmac(codec_ctx *ctx, cipher_ctx *c_ctx, Pgno pgno, uns
969969 /* include the encrypted page data, initialization vector, and page number in HMAC. This will
970970 prevent both tampering with the ciphertext, manipulation of the IV, or resequencing otherwise
971971 valid pages out of order in a database */
972- ctx -> provider -> hmac (
972+ return ctx -> provider -> hmac (
973973 ctx -> provider_ctx , ctx -> hmac_algorithm , c_ctx -> hmac_key ,
974974 ctx -> key_sz , in ,
975975 in_sz , (unsigned char * ) & pgno_raw ,
976976 sizeof (pgno ), out );
977- return SQLITE_OK ;
978977}
979978
980979/*
@@ -1007,22 +1006,20 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
10071006 /* the key size should never be zero. If it is, error out. */
10081007 if (ctx -> key_sz == 0 ) {
10091008 CODEC_TRACE ("codec_cipher: error possible context corruption, key_sz is zero for pgno=%d\n" , pgno );
1010- sqlcipher_memset (out , 0 , page_sz );
1011- return SQLITE_ERROR ;
1009+ goto error ;
10121010 }
10131011
10141012 if (mode == CIPHER_ENCRYPT ) {
10151013 /* start at front of the reserve block, write random data to the end */
1016- if (ctx -> provider -> random (ctx -> provider_ctx , iv_out , ctx -> reserve_sz ) != SQLITE_OK ) return SQLITE_ERROR ;
1014+ if (ctx -> provider -> random (ctx -> provider_ctx , iv_out , ctx -> reserve_sz ) != SQLITE_OK ) goto error ;
10171015 } else { /* CIPHER_DECRYPT */
10181016 memcpy (iv_out , iv_in , ctx -> iv_sz ); /* copy the iv from the input to output buffer */
10191017 }
10201018
10211019 if ((ctx -> flags & CIPHER_FLAG_HMAC ) && (mode == CIPHER_DECRYPT ) && !ctx -> skip_read_hmac ) {
10221020 if (sqlcipher_page_hmac (ctx , c_ctx , pgno , in , size + ctx -> iv_sz , hmac_out ) != SQLITE_OK ) {
1023- sqlcipher_memset (out , 0 , page_sz );
1024- CODEC_TRACE ("codec_cipher: hmac operations failed for pgno=%d\n" , pgno );
1025- return SQLITE_ERROR ;
1021+ CODEC_TRACE ("codec_cipher: hmac operation on decrypt failed for pgno=%d\n" , pgno );
1022+ goto error ;
10261023 }
10271024
10281025 CODEC_TRACE ("codec_cipher: comparing hmac on in=%p out=%p hmac_sz=%d\n" , hmac_in , hmac_out , ctx -> hmac_sz );
@@ -1040,21 +1037,29 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
10401037 since the check failed, the page was either tampered with or corrupted. wipe the output buffer,
10411038 and return SQLITE_ERROR to the caller */
10421039 CODEC_TRACE ("codec_cipher: hmac check failed for pgno=%d returning SQLITE_ERROR\n" , pgno );
1043- sqlcipher_memset (out , 0 , page_sz );
1044- return SQLITE_ERROR ;
1040+ goto error ;
10451041 }
10461042 }
10471043 }
10481044
1049- ctx -> provider -> cipher (ctx -> provider_ctx , mode , c_ctx -> key , ctx -> key_sz , iv_out , in , size , out );
1045+ if (ctx -> provider -> cipher (ctx -> provider_ctx , mode , c_ctx -> key , ctx -> key_sz , iv_out , in , size , out ) != SQLITE_OK ) {
1046+ CODEC_TRACE ("codec_cipher: cipher operation mode=%d failed for pgno=%d returning SQLITE_ERROR\n" , mode , pgno );
1047+ goto error ;
1048+ };
10501049
10511050 if ((ctx -> flags & CIPHER_FLAG_HMAC ) && (mode == CIPHER_ENCRYPT )) {
1052- sqlcipher_page_hmac (ctx , c_ctx , pgno , out_start , size + ctx -> iv_sz , hmac_out );
1051+ if (sqlcipher_page_hmac (ctx , c_ctx , pgno , out_start , size + ctx -> iv_sz , hmac_out ) != SQLITE_OK ) {
1052+ CODEC_TRACE ("codec_cipher: hmac operation on encrypt failed for pgno=%d\n" , pgno );
1053+ goto error ;
1054+ };
10531055 }
10541056
10551057 CODEC_HEXDUMP ("codec_cipher: output page data" , out_start , page_sz );
10561058
10571059 return SQLITE_OK ;
1060+ error :
1061+ sqlcipher_memset (out , 0 , page_sz );
1062+ return SQLITE_ERROR ;
10581063}
10591064
10601065/**
@@ -1099,9 +1104,9 @@ static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
10991104 cipher_hex2bin (z + (ctx -> key_sz * 2 ), (ctx -> kdf_salt_sz * 2 ), ctx -> kdf_salt );
11001105 } else {
11011106 CODEC_TRACE ("cipher_ctx_key_derive: deriving key using full PBKDF2 with %d iterations\n" , c_ctx -> kdf_iter );
1102- ctx -> provider -> kdf (ctx -> provider_ctx , ctx -> kdf_algorithm , c_ctx -> pass , c_ctx -> pass_sz ,
1107+ if ( ctx -> provider -> kdf (ctx -> provider_ctx , ctx -> kdf_algorithm , c_ctx -> pass , c_ctx -> pass_sz ,
11031108 ctx -> kdf_salt , ctx -> kdf_salt_sz , ctx -> kdf_iter ,
1104- ctx -> key_sz , c_ctx -> key );
1109+ ctx -> key_sz , c_ctx -> key ) != SQLITE_OK ) return SQLITE_ERROR ;
11051110 }
11061111
11071112 /* set the context "keyspec" containing the hex-formatted key and salt to be used when attaching databases */
@@ -1127,9 +1132,9 @@ static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) {
11271132 c_ctx -> fast_kdf_iter );
11281133
11291134
1130- ctx -> provider -> kdf (ctx -> provider_ctx , ctx -> kdf_algorithm , c_ctx -> key , ctx -> key_sz ,
1135+ if ( ctx -> provider -> kdf (ctx -> provider_ctx , ctx -> kdf_algorithm , c_ctx -> key , ctx -> key_sz ,
11311136 ctx -> hmac_kdf_salt , ctx -> kdf_salt_sz , ctx -> fast_kdf_iter ,
1132- ctx -> key_sz , c_ctx -> hmac_key );
1137+ ctx -> key_sz , c_ctx -> hmac_key ) != SQLITE_OK ) return SQLITE_ERROR ;
11331138 }
11341139
11351140 c_ctx -> derive_key = 0 ;
@@ -1465,11 +1470,10 @@ const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
14651470 return ctx -> provider -> get_provider_version (ctx -> provider_ctx );
14661471}
14671472
1468- int sqlcipher_codec_hmac (const codec_ctx * ctx , const unsigned char * hmac_key , int key_sz ,
1473+ int sqlcipher_codec_hmac_sha1 (const codec_ctx * ctx , const unsigned char * hmac_key , int key_sz ,
14691474 unsigned char * in , int in_sz , unsigned char * in2 , int in2_sz ,
14701475 unsigned char * out ) {
1471- ctx -> provider -> hmac (ctx -> provider_ctx , SQLCIPHER_HMAC_SHA1 , (unsigned char * )hmac_key , key_sz , in , in_sz , in2 , in2_sz , out );
1472- return SQLITE_OK ;
1476+ return ctx -> provider -> hmac (ctx -> provider_ctx , SQLCIPHER_HMAC_SHA1 , (unsigned char * )hmac_key , key_sz , in , in_sz , in2 , in2_sz , out );
14731477}
14741478
14751479
0 commit comments