Skip to content

Commit

Permalink
Support custom cipher_plaintext_header_size
Browse files Browse the repository at this point in the history
For all those people loading databases from their iOS devices
  • Loading branch information
TellowKrinkle authored and MKleusberg committed Nov 13, 2019
1 parent b01c1c9 commit 6b8fb51
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/CipherDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ CipherSettings CipherDialog::getCipherSettings() const
cipherSettings.setKdfIterations(ui->spinKdfIterations->value());
cipherSettings.setHmacAlgorithm("HMAC_" + ui->comboHmacAlgorithm->currentText().toStdString());
cipherSettings.setKdfAlgorithm("PBKDF2_HMAC_" + ui->comboKdfAlgorithm->currentText().toStdString());
cipherSettings.setPlaintextHeaderSize(ui->plaintextHeaderSize->value());

return cipherSettings;
}
Expand Down Expand Up @@ -113,28 +114,33 @@ void CipherDialog::toggleEncryptionSettings()
ui->spinKdfIterations->setValue(64000);
ui->comboHmacAlgorithm->setCurrentText("SHA1");
ui->comboKdfAlgorithm->setCurrentText("SHA1");
ui->plaintextHeaderSize->setValue(0);

ui->comboPageSize->setEnabled(false);
ui->spinKdfIterations->setEnabled(false);
ui->comboHmacAlgorithm->setEnabled(false);
ui->comboKdfAlgorithm->setEnabled(false);
ui->plaintextHeaderSize->setEnabled(false);
} else if(ui->radioEncryptionSqlCipher4->isChecked()) {
// SQLCipher4
ui->comboPageSize->setCurrentText(QLocale().toString(4096));
ui->spinKdfIterations->setValue(256000);
ui->comboHmacAlgorithm->setCurrentText("SHA512");
ui->comboKdfAlgorithm->setCurrentText("SHA512");
ui->plaintextHeaderSize->setValue(0);

ui->comboPageSize->setEnabled(false);
ui->spinKdfIterations->setEnabled(false);
ui->comboHmacAlgorithm->setEnabled(false);
ui->comboKdfAlgorithm->setEnabled(false);
ui->plaintextHeaderSize->setEnabled(false);
} else if(ui->radioEncryptionCustom->isChecked()) {
// Custom

ui->comboPageSize->setEnabled(true);
ui->spinKdfIterations->setEnabled(true);
ui->comboHmacAlgorithm->setEnabled(true);
ui->comboKdfAlgorithm->setEnabled(true);
ui->plaintextHeaderSize->setEnabled(true);
}
}
21 changes: 21 additions & 0 deletions src/CipherDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,26 @@
</item>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Plaintext Header Size</string>
</property>
<property name="buddy">
<cstring>plaintextHeaderSize</cstring>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QSpinBox" name="plaintextHeaderSize">
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>1000000</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand All @@ -243,6 +263,7 @@
<tabstop>spinKdfIterations</tabstop>
<tabstop>comboHmacAlgorithm</tabstop>
<tabstop>comboKdfAlgorithm</tabstop>
<tabstop>plaintextHeaderSize</tabstop>
</tabstops>
<resources/>
<connections>
Expand Down
3 changes: 2 additions & 1 deletion src/CipherSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
CipherSettings::CipherSettings()
: keyFormat(Passphrase),
pageSize(0),
kdfIterations(0)
kdfIterations(0),
plaintextHeaderSize(0)
{
}

Expand Down
4 changes: 4 additions & 0 deletions src/CipherSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class CipherSettings
int getKdfIterations() const { return kdfIterations; }
void setKdfIterations(int value) { kdfIterations = value; }

int getPlaintextHeaderSize() const { return plaintextHeaderSize; }
void setPlaintextHeaderSize(int value) { plaintextHeaderSize = value; }

std::string getHmacAlgorithm() const { return hmacAlgorithm; }
void setHmacAlgorithm(const std::string& value) { hmacAlgorithm = value; }

Expand All @@ -39,6 +42,7 @@ class CipherSettings
std::string password;
int pageSize;
int kdfIterations;
int plaintextHeaderSize;
std::string hmacAlgorithm;
std::string kdfAlgorithm;
};
Expand Down
2 changes: 2 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2902,6 +2902,8 @@ void MainWindow::editEncryption()
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.cipher_kdf_algorithm = " + cipherSettings.getKdfAlgorithm(), false, false);
if(ok)
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.kdf_iter = " + std::to_string(cipherSettings.getKdfIterations()), false, false);
if (ok)
ok = db.executeSQL("PRAGMA sqlitebrowser_edit_encryption.cipher_plaintext_header_size = " + std::to_string(cipherSettings.getPlaintextHeaderSize()), false, false);

// Export the current database to the new one
qApp->processEvents();
Expand Down
11 changes: 11 additions & 0 deletions src/sqlitedb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ bool DBBrowserDB::open(const QString& db, bool readOnly)
executeSQL("PRAGMA kdf_iter = " + std::to_string(cipherSettings->getKdfIterations()), false, false);
executeSQL("PRAGMA cipher_hmac_algorithm = " + cipherSettings->getHmacAlgorithm(), false, false);
executeSQL("PRAGMA cipher_kdf_algorithm = " + cipherSettings->getKdfAlgorithm(), false, false);
executeSQL("PRAGMA cipher_plaintext_header_size = " + std::to_string(cipherSettings->getPlaintextHeaderSize()), false, false);
}
#endif
delete cipherSettings;
Expand Down Expand Up @@ -317,6 +318,11 @@ bool DBBrowserDB::attach(const QString& filePath, QString attach_as)
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
return false;
}
if(!executeSQL("PRAGMA cipher_plaintext_header_size = " + std::to_string(cipherSettings->getPlaintextHeaderSize()), false))
{
QMessageBox::warning(nullptr, qApp->applicationName(), lastErrorMessage);
return false;
}
}

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

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

Expand All @@ -435,6 +443,7 @@ bool DBBrowserDB::tryEncryptionSettings(const QString& filePath, bool* encrypted
cipherSettings->setKdfIterations(kdfIterations);
cipherSettings->setHmacAlgorithm(hmacAlgorithm);
cipherSettings->setKdfAlgorithm(kdfAlgorithm);
cipherSettings->setPlaintextHeaderSize(plaintextHeaderSize);
}
}

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

*encrypted = true;
#else
Expand Down

0 comments on commit 6b8fb51

Please sign in to comment.