Skip to content

Commit bba319a

Browse files
Fix for cipher_migrate on passphrases longer than 64 characters and raw keys
1 parent b03221f commit bba319a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/crypto_impl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -995,14 +995,14 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
995995
char *attach_command = sqlite3_mprintf("ATTACH DATABASE '%s-migrated' as migrate KEY '%q';",
996996
db_filename, key);
997997

998-
int rc = sqlcipher_check_connection(db_filename, key, key_sz, "", &user_version);
998+
int rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, "", &user_version);
999999
if(rc == SQLITE_OK){
10001000
CODEC_TRACE(("No upgrade required - exiting\n"));
10011001
goto exit;
10021002
}
10031003

10041004
// Version 2 - check for 4k with hmac format
1005-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_4k_kdf_iter, &user_version);
1005+
rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, pragma_4k_kdf_iter, &user_version);
10061006
if(rc == SQLITE_OK) {
10071007
CODEC_TRACE(("Version 2 format found\n"));
10081008
upgrade_4k_format = 1;
@@ -1011,7 +1011,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
10111011
// Version 1 - check both no hmac and 4k together
10121012
pragma_1x_and_4k = sqlite3_mprintf("%s%s", pragma_hmac_off,
10131013
pragma_4k_kdf_iter);
1014-
rc = sqlcipher_check_connection(db_filename, key, key_sz, pragma_1x_and_4k, &user_version);
1014+
rc = sqlcipher_check_connection(db_filename, key, ctx->read_ctx->pass_sz, pragma_1x_and_4k, &user_version);
10151015
sqlite3_free(pragma_1x_and_4k);
10161016
if(rc == SQLITE_OK) {
10171017
CODEC_TRACE(("Version 1 format found\n"));

test/crypto.test

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,6 +2162,42 @@ file delete -force test.db
21622162
file delete -force test2.db
21632163
file delete -force test3.db
21642164

2165+
do_test can-migrate-with-keys-longer-than-64-characters {
2166+
sqlite_orig db test.db
2167+
execsql {
2168+
PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345";
2169+
PRAGMA kdf_iter = 4000;
2170+
PRAGMA user_version = 5;
2171+
}
2172+
db close
2173+
sqlite_orig db test.db
2174+
execsql {
2175+
PRAGMA key = "012345678901234567890123456789012345678901234567890123456789012345";
2176+
PRAGMA cipher_migrate;
2177+
PRAGMA user_version;
2178+
}
2179+
} {0 5}
2180+
db close
2181+
file delete -force test.db
2182+
2183+
do_test can-migrate-with-raw-hex-key {
2184+
sqlite_orig db test.db
2185+
execsql {
2186+
PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
2187+
PRAGMA kdf_iter = 4000;
2188+
PRAGMA cipher_use_hmac = off;
2189+
PRAGMA user_version = 5;
2190+
}
2191+
db close
2192+
sqlite_orig db test.db
2193+
execsql {
2194+
PRAGMA key = "x'2DD29CA851E7B56E4697B0E1F08507293D761A05CE4D1B628663F411A8086D99'";
2195+
PRAGMA cipher_migrate;
2196+
PRAGMA user_version;
2197+
}
2198+
} {0 5}
2199+
db close
2200+
file delete -force test.db
21652201

21662202
sqlite3_test_control_pending_byte $old_pending_byte
21672203
finish_test

0 commit comments

Comments
 (0)