Skip to content

Commit dcbefc4

Browse files
committed
Merge branch 'pr/625' into beta
2 parents 154bca1 + 6ba97cd commit dcbefc4

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎src/sqlcipher.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,8 +1780,9 @@ static int sqlcipher_ctx_set_hmac_fast_kdf(sqlcipher_ctx *ctx, int use) {
17801780
if(SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_KEY_USED)) return SQLITE_OK;
17811781

17821782
if(use) {
1783-
/* HMAC is a prerequisite for HMAC_FAST_KDF */
1784-
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC);
1783+
/* clears AEAD and resizes the reserve */
1784+
int rc = sqlcipher_ctx_set_use_hmac(ctx, 1);
1785+
if(rc != SQLITE_OK) return rc;
17851786
SQLCIPHER_FLAG_SET(ctx->flags, CIPHER_FLAG_HMAC_FAST_KDF);
17861787
} else SQLCIPHER_FLAG_UNSET(ctx->flags, CIPHER_FLAG_HMAC_FAST_KDF);
17871788

@@ -3694,6 +3695,9 @@ int sqlcipher_pragma(sqlite3* db, const char *zDb, int iDb, Parse *pParse, const
36943695
if( zRight ) {
36953696
rc = sqlcipher_ctx_set_hmac_fast_kdf(ctx, sqlite3GetBoolean(zRight,1));
36963697
if(rc != SQLITE_OK) sqlcipher_ctx_set_error(db, pDb, ctx, rc);
3698+
/* resync page size after a possible hmac change */
3699+
rc = sqlcipher_set_btree_pagesize(db, pDb, ctx);
3700+
if(rc != SQLITE_OK) sqlcipher_ctx_set_error(db, pDb, ctx, rc);
36973701
} else {
36983702
char *flag = sqlite3_mprintf("%d", SQLCIPHER_FLAG_GET(ctx->flags, CIPHER_FLAG_HMAC_FAST_KDF));
36993703
sqlcipher_vdbe_return_string(pParse, "cipher_hmac_fast_kdf", flag, P4_DYNAMIC);

‎test/sqlcipher-pragmas.test‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,63 @@ do_test verify-pragma-fast-kdf-iter-reports-default {
174174
db close
175175
file delete -force test.db
176176

177+
# cipher_hmac_fast_kdf alone recomputes the reserve, round-trip without corruption
178+
do_test verify-pragma-hmac-fast-kdf-only-roundtrip {
179+
sqlite_orig db test.db
180+
execsql {
181+
PRAGMA key = 'test';
182+
PRAGMA cipher_hmac_fast_kdf = ON;
183+
CREATE TABLE t1(a,b);
184+
INSERT INTO t1 VALUES('test1', 'test2');
185+
}
186+
db close
187+
sqlite_orig db test.db
188+
execsql {
189+
PRAGMA key = 'test';
190+
PRAGMA cipher_hmac_fast_kdf = ON;
191+
SELECT * FROM t1;
192+
}
193+
} {ok test1 test2}
194+
db close
195+
file delete -force test.db
196+
197+
# cipher_hmac_fast_kdf alone also resizes the btree, so an AEAD database rejects as not-a-database
198+
setup test.db "'test'"
199+
do_test verify-pragma-hmac-fast-kdf-only-rejects-aead-database {
200+
sqlite_orig db test.db
201+
execsql {
202+
PRAGMA key = 'test';
203+
PRAGMA cipher_hmac_fast_kdf = ON;
204+
}
205+
catchsql {
206+
SELECT * FROM t1;
207+
}
208+
} {1 {file is not a database}}
209+
db close
210+
file delete -force test.db
211+
212+
# the same fix holds under compatibility 4, whose narrower page leaves less margin
213+
do_test verify-pragma-hmac-fast-kdf-only-compatibility-4-roundtrip {
214+
sqlite_orig db test.db
215+
execsql {
216+
PRAGMA key = 'test';
217+
PRAGMA cipher_compatibility = 4;
218+
PRAGMA cipher_use_hmac = OFF;
219+
PRAGMA cipher_hmac_fast_kdf = ON;
220+
CREATE TABLE t1(a,b);
221+
INSERT INTO t1 VALUES('test1', 'test2');
222+
}
223+
db close
224+
sqlite_orig db test.db
225+
execsql {
226+
PRAGMA key = 'test';
227+
PRAGMA cipher_compatibility = 4;
228+
SELECT * FROM t1;
229+
}
230+
} {ok test1 test2}
231+
db close
232+
file delete -force test.db
233+
177234
# verify the pragma cipher_page_size
178235
# reports default value
179236
do_test verify-pragma-cipher-page-size-default {

0 commit comments

Comments
 (0)