Skip to content

Commit 88ce4ac

Browse files
committed
sqlcipher: Add new encryption settings to improve support for SQLCipher4
This adds three new settings to the cipher dialog: KDF iterations, HMAC algorithm, and KDF algorithm. To simplify things we also add two presets for all the encryption settings: SQLCipher3 defaults and SQLCipher4 defaults. The preselected default is chosen depending on the SQLCipher version which we use. This should work with any combination of SQLCipher3 and SQLCipher4 and any database created by either. It should also work with DotEnv files as expected. Again, the defaults which are used for missing values in the DotEnv files are chosen depending on the SQLCipher version we use.
1 parent 2c8883d commit 88ce4ac

7 files changed

Lines changed: 314 additions & 39 deletions

File tree

src/CipherDialog.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "CipherDialog.h"
22
#include "ui_CipherDialog.h"
3+
#include "sqlitedb.h"
34

45
#include <QPushButton>
56
#include <QRegExpValidator>
@@ -41,6 +42,14 @@ CipherDialog::CipherDialog(QWidget* parent, bool encrypt) :
4142
ui->editPassword2->setVisible(false);
4243
ui->labelPassword2->setVisible(false);
4344
}
45+
46+
// Set the default encryption settings depending on the SQLCipher version we use
47+
QString sqlite_version, sqlcipher_version;
48+
DBBrowserDB::getSqliteVersion(sqlite_version, sqlcipher_version);
49+
if(sqlcipher_version.startsWith('4'))
50+
ui->radioEncryptionSqlCipher4->setChecked(true);
51+
else
52+
ui->radioEncryptionSqlCipher3->setChecked(true);
4453
}
4554

4655
CipherDialog::~CipherDialog()
@@ -60,6 +69,9 @@ CipherSettings CipherDialog::getCipherSettings() const
6069
cipherSettings.setKeyFormat(keyFormat);
6170
cipherSettings.setPassword(password);
6271
cipherSettings.setPageSize(pageSize);
72+
cipherSettings.setKdfIterations(ui->spinKdfIterations->value());
73+
cipherSettings.setHmacAlgorithm(ui->comboHmacAlgorithm->currentText());
74+
cipherSettings.setKdfAlgorithm(ui->comboKdfAlgorithm->currentText());
6375

6476
return cipherSettings;
6577
}
@@ -91,3 +103,38 @@ void CipherDialog::checkInputFields()
91103

92104
ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid);
93105
}
106+
107+
void CipherDialog::toggleEncryptionSettings()
108+
{
109+
if(ui->radioEncryptionSqlCipher3->isChecked())
110+
{
111+
// SQLCipher3
112+
ui->comboPageSize->setCurrentText(QLocale().toString(1024));
113+
ui->spinKdfIterations->setValue(64000);
114+
ui->comboHmacAlgorithm->setCurrentText("SHA1");
115+
ui->comboKdfAlgorithm->setCurrentText("SHA1");
116+
117+
ui->comboPageSize->setEnabled(false);
118+
ui->spinKdfIterations->setEnabled(false);
119+
ui->comboHmacAlgorithm->setEnabled(false);
120+
ui->comboKdfAlgorithm->setEnabled(false);
121+
} else if(ui->radioEncryptionSqlCipher4->isChecked()) {
122+
// SQLCipher4
123+
ui->comboPageSize->setCurrentText(QLocale().toString(4096));
124+
ui->spinKdfIterations->setValue(256000);
125+
ui->comboHmacAlgorithm->setCurrentText("SHA512");
126+
ui->comboKdfAlgorithm->setCurrentText("SHA512");
127+
128+
ui->comboPageSize->setEnabled(false);
129+
ui->spinKdfIterations->setEnabled(false);
130+
ui->comboHmacAlgorithm->setEnabled(false);
131+
ui->comboKdfAlgorithm->setEnabled(false);
132+
} else if(ui->radioEncryptionCustom->isChecked()) {
133+
// Custom
134+
135+
ui->comboPageSize->setEnabled(true);
136+
ui->spinKdfIterations->setEnabled(true);
137+
ui->comboHmacAlgorithm->setEnabled(true);
138+
ui->comboKdfAlgorithm->setEnabled(true);
139+
}
140+
}

src/CipherDialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class CipherDialog : public QDialog
3030

3131
private slots:
3232
void checkInputFields();
33+
void toggleEncryptionSettings();
3334
};
3435

3536
#endif

src/CipherDialog.ui

Lines changed: 194 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<x>0</x>
88
<y>0</y>
99
<width>712</width>
10-
<height>183</height>
10+
<height>299</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -18,7 +18,7 @@
1818
<widget class="QLabel" name="labelDialogDescription"/>
1919
</item>
2020
<item>
21-
<layout class="QHBoxLayout" name="horizontalLayout">
21+
<layout class="QHBoxLayout" name="horizontalLayout_2">
2222
<item>
2323
<layout class="QFormLayout" name="formLayout">
2424
<item row="0" column="0">
@@ -55,19 +55,6 @@
5555
</property>
5656
</widget>
5757
</item>
58-
<item row="2" column="0">
59-
<widget class="QLabel" name="label_3">
60-
<property name="text">
61-
<string>Page si&amp;ze</string>
62-
</property>
63-
<property name="buddy">
64-
<cstring>comboPageSize</cstring>
65-
</property>
66-
</widget>
67-
</item>
68-
<item row="2" column="1">
69-
<widget class="QComboBox" name="comboPageSize"/>
70-
</item>
7158
</layout>
7259
</item>
7360
<item>
@@ -103,6 +90,136 @@
10390
</item>
10491
</layout>
10592
</item>
93+
<item>
94+
<layout class="QFormLayout" name="formLayout_2">
95+
<item row="0" column="0">
96+
<widget class="QLabel" name="label">
97+
<property name="text">
98+
<string>Encr&amp;yption settings</string>
99+
</property>
100+
<property name="buddy">
101+
<cstring>radioEncryptionSqlCipher3</cstring>
102+
</property>
103+
</widget>
104+
</item>
105+
<item row="0" column="1">
106+
<layout class="QHBoxLayout" name="horizontalLayout">
107+
<item>
108+
<widget class="QRadioButton" name="radioEncryptionSqlCipher3">
109+
<property name="text">
110+
<string>SQLCipher &amp;3 defaults</string>
111+
</property>
112+
</widget>
113+
</item>
114+
<item>
115+
<widget class="QRadioButton" name="radioEncryptionSqlCipher4">
116+
<property name="text">
117+
<string>SQLCipher &amp;4 defaults</string>
118+
</property>
119+
</widget>
120+
</item>
121+
<item>
122+
<widget class="QRadioButton" name="radioEncryptionCustom">
123+
<property name="text">
124+
<string>Custo&amp;m</string>
125+
</property>
126+
</widget>
127+
</item>
128+
</layout>
129+
</item>
130+
<item row="1" column="0">
131+
<widget class="QLabel" name="label_3">
132+
<property name="text">
133+
<string>Page si&amp;ze</string>
134+
</property>
135+
<property name="buddy">
136+
<cstring>comboPageSize</cstring>
137+
</property>
138+
</widget>
139+
</item>
140+
<item row="1" column="1">
141+
<widget class="QComboBox" name="comboPageSize"/>
142+
</item>
143+
<item row="2" column="0">
144+
<widget class="QLabel" name="label_2">
145+
<property name="text">
146+
<string>&amp;KDF iterations</string>
147+
</property>
148+
<property name="buddy">
149+
<cstring>spinKdfIterations</cstring>
150+
</property>
151+
</widget>
152+
</item>
153+
<item row="2" column="1">
154+
<widget class="QSpinBox" name="spinKdfIterations">
155+
<property name="minimum">
156+
<number>1</number>
157+
</property>
158+
<property name="maximum">
159+
<number>1000000</number>
160+
</property>
161+
</widget>
162+
</item>
163+
<item row="3" column="0">
164+
<widget class="QLabel" name="label_4">
165+
<property name="text">
166+
<string>HMAC algorithm</string>
167+
</property>
168+
<property name="buddy">
169+
<cstring>comboHmacAlgorithm</cstring>
170+
</property>
171+
</widget>
172+
</item>
173+
<item row="3" column="1">
174+
<widget class="QComboBox" name="comboHmacAlgorithm">
175+
<item>
176+
<property name="text">
177+
<string notr="true">SHA512</string>
178+
</property>
179+
</item>
180+
<item>
181+
<property name="text">
182+
<string notr="true">SHA256</string>
183+
</property>
184+
</item>
185+
<item>
186+
<property name="text">
187+
<string notr="true">SHA1</string>
188+
</property>
189+
</item>
190+
</widget>
191+
</item>
192+
<item row="4" column="0">
193+
<widget class="QLabel" name="label_5">
194+
<property name="text">
195+
<string>KDF algorithm</string>
196+
</property>
197+
<property name="buddy">
198+
<cstring>comboKdfAlgorithm</cstring>
199+
</property>
200+
</widget>
201+
</item>
202+
<item row="4" column="1">
203+
<widget class="QComboBox" name="comboKdfAlgorithm">
204+
<item>
205+
<property name="text">
206+
<string notr="true">SHA512</string>
207+
</property>
208+
</item>
209+
<item>
210+
<property name="text">
211+
<string notr="true">SHA256</string>
212+
</property>
213+
</item>
214+
<item>
215+
<property name="text">
216+
<string notr="true">SHA1</string>
217+
</property>
218+
</item>
219+
</widget>
220+
</item>
221+
</layout>
222+
</item>
106223
<item>
107224
<widget class="QDialogButtonBox" name="buttonBox">
108225
<property name="orientation">
@@ -117,9 +234,15 @@
117234
</widget>
118235
<tabstops>
119236
<tabstop>editPassword</tabstop>
120-
<tabstop>comboKeyFormat</tabstop>
121237
<tabstop>editPassword2</tabstop>
238+
<tabstop>comboKeyFormat</tabstop>
239+
<tabstop>radioEncryptionSqlCipher3</tabstop>
240+
<tabstop>radioEncryptionSqlCipher4</tabstop>
241+
<tabstop>radioEncryptionCustom</tabstop>
122242
<tabstop>comboPageSize</tabstop>
243+
<tabstop>spinKdfIterations</tabstop>
244+
<tabstop>comboHmacAlgorithm</tabstop>
245+
<tabstop>comboKdfAlgorithm</tabstop>
123246
</tabstops>
124247
<resources/>
125248
<connections>
@@ -130,8 +253,8 @@
130253
<slot>accept()</slot>
131254
<hints>
132255
<hint type="sourcelabel">
133-
<x>233</x>
134-
<y>174</y>
256+
<x>175</x>
257+
<y>265</y>
135258
</hint>
136259
<hint type="destinationlabel">
137260
<x>157</x>
@@ -146,8 +269,8 @@
146269
<slot>reject()</slot>
147270
<hints>
148271
<hint type="sourcelabel">
149-
<x>301</x>
150-
<y>174</y>
272+
<x>175</x>
273+
<y>265</y>
151274
</hint>
152275
<hint type="destinationlabel">
153276
<x>286</x>
@@ -178,8 +301,8 @@
178301
<slot>checkInputFields()</slot>
179302
<hints>
180303
<hint type="sourcelabel">
181-
<x>319</x>
182-
<y>96</y>
304+
<x>446</x>
305+
<y>79</y>
183306
</hint>
184307
<hint type="destinationlabel">
185308
<x>206</x>
@@ -203,8 +326,57 @@
203326
</hint>
204327
</hints>
205328
</connection>
329+
<connection>
330+
<sender>radioEncryptionSqlCipher3</sender>
331+
<signal>toggled(bool)</signal>
332+
<receiver>CipherDialog</receiver>
333+
<slot>toggleEncryptionSettings()</slot>
334+
<hints>
335+
<hint type="sourcelabel">
336+
<x>217</x>
337+
<y>114</y>
338+
</hint>
339+
<hint type="destinationlabel">
340+
<x>231</x>
341+
<y>94</y>
342+
</hint>
343+
</hints>
344+
</connection>
345+
<connection>
346+
<sender>radioEncryptionSqlCipher4</sender>
347+
<signal>toggled(bool)</signal>
348+
<receiver>CipherDialog</receiver>
349+
<slot>toggleEncryptionSettings()</slot>
350+
<hints>
351+
<hint type="sourcelabel">
352+
<x>353</x>
353+
<y>117</y>
354+
</hint>
355+
<hint type="destinationlabel">
356+
<x>407</x>
357+
<y>97</y>
358+
</hint>
359+
</hints>
360+
</connection>
361+
<connection>
362+
<sender>radioEncryptionCustom</sender>
363+
<signal>toggled(bool)</signal>
364+
<receiver>CipherDialog</receiver>
365+
<slot>toggleEncryptionSettings()</slot>
366+
<hints>
367+
<hint type="sourcelabel">
368+
<x>552</x>
369+
<y>120</y>
370+
</hint>
371+
<hint type="destinationlabel">
372+
<x>590</x>
373+
<y>99</y>
374+
</hint>
375+
</hints>
376+
</connection>
206377
</connections>
207378
<slots>
208379
<slot>checkInputFields()</slot>
380+
<slot>toggleEncryptionSettings()</slot>
209381
</slots>
210382
</ui>

src/CipherSettings.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ void CipherSettings::setPassword(const QString &value)
3232

3333
int CipherSettings::getPageSize() const
3434
{
35-
if (pageSize == 0)
36-
return defaultPageSize;
37-
3835
return pageSize;
3936
}
4037

0 commit comments

Comments
 (0)