Skip to content

Commit 6b8fb51

Browse files
TellowKrinkleMKleusberg
authored andcommitted
Support custom cipher_plaintext_header_size
For all those people loading databases from their iOS devices
1 parent b01c1c9 commit 6b8fb51

6 files changed

Lines changed: 46 additions & 1 deletion

File tree

src/CipherDialog.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ CipherSettings CipherDialog::getCipherSettings() const
7272
cipherSettings.setKdfIterations(ui->spinKdfIterations->value());
7373
cipherSettings.setHmacAlgorithm("HMAC_" + ui->comboHmacAlgorithm->currentText().toStdString());
7474
cipherSettings.setKdfAlgorithm("PBKDF2_HMAC_" + ui->comboKdfAlgorithm->currentText().toStdString());
75+
cipherSettings.setPlaintextHeaderSize(ui->plaintextHeaderSize->value());
7576

7677
return cipherSettings;
7778
}
@@ -113,28 +114,33 @@ void CipherDialog::toggleEncryptionSettings()
113114
ui->spinKdfIterations->setValue(64000);
114115
ui->comboHmacAlgorithm->setCurrentText("SHA1");
115116
ui->comboKdfAlgorithm->setCurrentText("SHA1");
117+
ui->plaintextHeaderSize->setValue(0);
116118

117119
ui->comboPageSize->setEnabled(false);
118120
ui->spinKdfIterations->setEnabled(false);
119121
ui->comboHmacAlgorithm->setEnabled(false);
120122
ui->comboKdfAlgorithm->setEnabled(false);
123+
ui->plaintextHeaderSize->setEnabled(false);
121124
} else if(ui->radioEncryptionSqlCipher4->isChecked()) {
122125
// SQLCipher4
123126
ui->comboPageSize->setCurrentText(QLocale().toString(4096));
124127
ui->spinKdfIterations->setValue(256000);
125128
ui->comboHmacAlgorithm->setCurrentText("SHA512");
126129
ui->comboKdfAlgorithm->setCurrentText("SHA512");
130+
ui->plaintextHeaderSize->setValue(0);
127131

128132
ui->comboPageSize->setEnabled(false);
129133
ui->spinKdfIterations->setEnabled(false);
130134
ui->comboHmacAlgorithm->setEnabled(false);
131135
ui->comboKdfAlgorithm->setEnabled(false);
136+
ui->plaintextHeaderSize->setEnabled(false);
132137
} else if(ui->radioEncryptionCustom->isChecked()) {
133138
// Custom
134139

135140
ui->comboPageSize->setEnabled(true);
136141
ui->spinKdfIterations->setEnabled(true);
137142
ui->comboHmacAlgorithm->setEnabled(true);
138143
ui->comboKdfAlgorithm->setEnabled(true);
144+
ui->plaintextHeaderSize->setEnabled(true);
139145
}
140146
}

src/CipherDialog.ui

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,26 @@
218218
</item>
219219
</widget>
220220
</item>
221+
<item row="5" column="0">
222+
<widget class="QLabel" name="label_2">
223+
<property name="text">
224+
<string>Plaintext Header Size</string>
225+
</property>
226+
<property name="buddy">
227+
<cstring>plaintextHeaderSize</cstring>
228+
</property>
229+
</widget>
230+
</item>
231+
<item row="5" column="1">
232+
<widget class="QSpinBox" name="plaintextHeaderSize">
233+
<property name="minimum">
234+
<number>0</number>
235+
</property>
236+
<property name="maximum">
237+
<number>1000000</number>
238+
</property>
239+
</widget>
240+
</item>
221241
</layout>
222242
</item>
223243
<item>
@@ -243,6 +263,7 @@
243263
<tabstop>spinKdfIterations</tabstop>
244264
<tabstop>comboHmacAlgorithm</tabstop>
245265
<tabstop>comboKdfAlgorithm</tabstop>
266+
<tabstop>plaintextHeaderSize</tabstop>
246267
</tabstops>
247268
<resources/>
248269
<connections>

src/CipherSettings.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
CipherSettings::CipherSettings()
55
: keyFormat(Passphrase),
66
pageSize(0),
7-
kdfIterations(0)
7+
kdfIterations(0),
8+
plaintextHeaderSize(0)
89
{
910
}
1011

src/CipherSettings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class CipherSettings
2626
int getKdfIterations() const { return kdfIterations; }
2727
void setKdfIterations(int value) { kdfIterations = value; }
2828

29+
int getPlaintextHeaderSize() const { return plaintextHeaderSize; }
30+
void setPlaintextHeaderSize(int value) { plaintextHeaderSize = value; }
31+
2932
std::string getHmacAlgorithm() const { return hmacAlgorithm; }
3033
void setHmacAlgorithm(const std::string& value) { hmacAlgorithm = value; }
3134

@@ -39,6 +42,7 @@ class CipherSettings
3942
std::string password;
4043
int pageSize;
4144
int kdfIterations;
45+
int plaintextHeaderSize;
4246
std::string hmacAlgorithm;
4347
std::string kdfAlgorithm;
4448
};

src/MainWindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,8 @@ void MainWindow::editEncryption()
29022902
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.cipher_kdf_algorithm = " + cipherSettings.getKdfAlgorithm(), false, false);
29032903
if(ok)
29042904
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.kdf_iter = " + std::to_string(cipherSettings.getKdfIterations()), false, false);
2905+
if (ok)
2906+
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.cipher_plaintext_header_size = " + std::to_string(cipherSettings.getPlaintextHeaderSize()), false, false);
29052907

29062908
// Export the current database to the new one
29072909
qApp->processEvents();

src/sqlitedb.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ bool DBBrowserDB::open(const QString& db, bool readOnly)
177177
executeSQL("PRAGMA kdf_iter = " + std::to_string(cipherSettings->getKdfIterations()), false, false);
178178
executeSQL("PRAGMA cipher_hmac_algorithm = " + cipherSettings->getHmacAlgorithm(), false, false);
179179
executeSQL("PRAGMA cipher_kdf_algorithm = " + cipherSettings->getKdfAlgorithm(), false, false);
180+
executeSQL("PRAGMA cipher_plaintext_header_size = " + std::to_string(cipherSettings->getPlaintextHeaderSize()), false, false);
180181
}
181182
#endif
182183
delete cipherSettings;
@@ -317,6 +318,11 @@ bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
317318
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
318319
return false;
319320
}
321+
if(!executeSQL("PRAGMA cipher_plaintext_header_size = " + std::to_string(cipherSettings->getPlaintextHeaderSize()), false))
322+
{
323+
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
324+
return false;
325+
}
320326
}
321327

322328
if(!executeSQL("ATTACH " + sqlb::escapeString(filePath.toStdString()) + " AS " + sqlb::escapeIdentifier(attach_as.toStdString()) + " " + key, false))
@@ -360,6 +366,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
360366
QString sqlite_version, sqlcipher_version;
361367
getSqliteVersion(sqlite_version, sqlcipher_version);
362368
int enc_default_page_size, enc_default_kdf_iter;
369+
int enc_default_plaintext_header_size = 0;
363370
std::string enc_default_hmac_algorithm, enc_default_kdf_algorithm;
364371
if(sqlcipher_version.startsWith('4'))
365372
{
@@ -423,6 +430,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
423430

424431
int pageSize = dotenv.value(databaseFileName + "_pageSize", enc_default_page_size).toInt();
425432
int kdfIterations = dotenv.value(databaseFileName + "_kdfIter", enc_default_kdf_iter).toInt();
433+
int plaintextHeaderSize = dotenv.value(databaseFileName + "_plaintextHeaderSize", enc_default_kdf_iter).toInt();
426434
std::string hmacAlgorithm = dotenv.value(databaseFileName + "_hmacAlgorithm", QString::fromStdString(enc_default_hmac_algorithm)).toString().toStdString();
427435
std::string kdfAlgorithm = dotenv.value(databaseFileName + "_kdfAlgorithm", QString::fromStdString(enc_default_kdf_algorithm)).toString().toStdString();
428436

@@ -435,6 +443,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
435443
cipherSettings->setKdfIterations(kdfIterations);
436444
cipherSettings->setHmacAlgorithm(hmacAlgorithm);
437445
cipherSettings->setKdfAlgorithm(kdfAlgorithm);
446+
cipherSettings->setPlaintextHeaderSize(plaintextHeaderSize);
438447
}
439448
}
440449

@@ -477,6 +486,8 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
477486
sqlite3_exec(dbHandle, ("PRAGMA cipher_hmac_algorithm = " + cipherSettings->getHmacAlgorithm()).c_str(), nullptr, nullptr, nullptr);
478487
if(cipherSettings->getKdfAlgorithm() != enc_default_kdf_algorithm)
479488
sqlite3_exec(dbHandle, ("PRAGMA cipher_kdf_algorithm = " + cipherSettings->getKdfAlgorithm()).c_str(), nullptr, nullptr, nullptr);
489+
if(cipherSettings->getPlaintextHeaderSize() != enc_default_plaintext_header_size)
490+
sqlite3_exec(dbHandle, ("PRAGMA cipher_plaintext_header_size = " + std::to_string(cipherSettings->getPlaintextHeaderSize())).c_str(), nullptr, nullptr, nullptr);
480491

481492
*encrypted = true;
482493
#else

0 commit comments

Comments
 (0)