Skip to content

Commit fc2fcff

Browse files
Ignore hmac read during Btree file copy
1 parent 314f573 commit fc2fcff

3 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/crypto_impl.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ struct codec_ctx {
8282
Btree *pBt;
8383
cipher_ctx *read_ctx;
8484
cipher_ctx *write_ctx;
85+
unsigned int skip_read_hmac;
8586
};
8687

8788
int sqlcipher_register_provider(sqlcipher_provider *p) {
@@ -756,7 +757,7 @@ int sqlcipher_page_cipher(codec_ctx *ctx, int for_ctx, Pgno pgno, int mode, int
756757
memcpy(iv_out, iv_in, c_ctx->iv_sz); /* copy the iv from the input to output buffer */
757758
}
758759

759-
if((c_ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT)) {
760+
if((c_ctx->flags & CIPHER_FLAG_HMAC) && (mode == CIPHER_DECRYPT) && !ctx->skip_read_hmac) {
760761
if(sqlcipher_page_hmac(c_ctx, pgno, in, size + c_ctx->iv_sz, hmac_out) != SQLITE_OK) {
761762
sqlcipher_memset(out, 0, page_sz);
762763
CODEC_TRACE(("codec_cipher: hmac operations failed for pgno=%d\n", pgno));
@@ -1048,7 +1049,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10481049
CODEC_TRACE(("cannot migrate - SQL statements in progress"));
10491050
goto handle_error;
10501051
}
1051-
1052+
10521053
/* Save the current value of the database flags so that it can be
10531054
** restored before returning. Then set the writable-schema flag, and
10541055
** disable CHECK and foreign key constraints. */
@@ -1063,25 +1064,26 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10631064
pDest = db->aDb[0].pBt;
10641065
pDb = &(db->aDb[db->nDb-1]);
10651066
pSrc = pDb->pBt;
1066-
1067+
10671068
rc = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
10681069
rc = sqlite3BtreeBeginTrans(pSrc, 2);
10691070
rc = sqlite3BtreeBeginTrans(pDest, 2);
10701071

10711072
assert( 1==sqlite3BtreeIsInTrans(pDest) );
10721073
assert( 1==sqlite3BtreeIsInTrans(pSrc) );
10731074

1074-
10751075
sqlite3CodecGetKey(db, db->nDb - 1, (void**)&key, &password_sz);
10761076
sqlite3CodecAttach(db, 0, key, password_sz);
1077+
sqlite3pager_get_codec(pDest->pBt->pPager, (void**)&ctx);
10771078

1079+
ctx->skip_read_hmac = 1;
10781080
for(i=0; i<ArraySize(aCopy); i+=2){
10791081
sqlite3BtreeGetMeta(pSrc, aCopy[i], &meta);
10801082
rc = sqlite3BtreeUpdateMeta(pDest, aCopy[i], meta+aCopy[i+1]);
10811083
if( NEVER(rc!=SQLITE_OK) ) goto handle_error;
10821084
}
1083-
10841085
rc = sqlite3BtreeCopyFile(pDest, pSrc);
1086+
ctx->skip_read_hmac = 0;
10851087
if( rc!=SQLITE_OK ) goto handle_error;
10861088
rc = sqlite3BtreeCommit(pDest);
10871089

test/crypto.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,10 +1643,10 @@ do_test multipage-schema-autovacuum-shortread-wal {
16431643
db close
16441644
file delete -force test.db
16451645

1646-
# open a 2.3 database with little endian hmac page numbers (default)
1646+
# open a 3.0 database with little endian hmac page numbers (default)
16471647
# verify it can be opened
1648-
do_test open-2.3-le-database {
1649-
sqlite_orig db sqlcipher-2.3-testkey.db
1648+
do_test open-3.0-le-database {
1649+
sqlite_orig db sqlcipher-3.0-testkey.db
16501650
execsql {
16511651
PRAGMA key = 'testkey';
16521652
SELECT count(*) FROM t1;

0 commit comments

Comments
 (0)