Skip to content

Commit d9b8f81

Browse files
author
Szymon Zimnowoda
committed
SQLite: Execute SQLCipher pragmas as very first operations on the database
SQLCipher requires, apart from 'key' pragma also other cipher-related to be executed before read/write to the database.
1 parent 9a6d07f commit d9b8f81

File tree

1 file changed

+40
-4
lines changed
  • sqlx-core/src/sqlite/options

1 file changed

+40
-4
lines changed

sqlx-core/src/sqlite/options/mod.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,42 @@ impl SqliteConnectOptions {
101101
// SQLCipher special case: if the `key` pragma is set, it must be executed first.
102102
pragmas.insert("key".into(), None);
103103

104+
// Other SQLCipher pragmas that has to be after the key, but before any other operation on the database.
105+
// https://www.zetetic.net/sqlcipher/sqlcipher-api/
106+
107+
// Bytes of the database file that is not encrypted
108+
// Default for SQLCipher v4 is 0
109+
// If greater than zero 'cipher_salt' pragma must be also defined
110+
pragmas.insert("cipher_plaintext_header_size".into(), None);
111+
112+
// Allows to provide salt manually
113+
// By default SQLCipher sets salt automatically, use only in conjunction with
114+
// 'cipher_plaintext_header_size' pragma
115+
pragmas.insert("cipher_salt".into(), None);
116+
117+
// Number of iterations used in PBKDF2 key derivation.
118+
// Default for SQLCipher v4 is 256000
119+
pragmas.insert("kdf_iter".into(), None);
120+
121+
// Define KDF algorithm to be used.
122+
// Default for SQLCipher v4 is PBKDF2_HMAC_SHA512.
123+
pragmas.insert("cipher_kdf_algorithm".into(), None);
124+
125+
// Enable or disable HMAC functionality.
126+
// Default for SQLCipher v4 is 1.
127+
pragmas.insert("cipher_use_hmac".into(), None);
128+
129+
// Set default encryption settings depending on the version 1,2,3, or 4.
130+
pragmas.insert("cipher_compatibility".into(), None);
131+
132+
// Page size of encrypted database.
133+
// Default for SQLCipher v4 is 4096.
134+
pragmas.insert("cipher_page_size".into(), None);
135+
136+
// Choose algorithm used for HMAC.
137+
// Default for SQLCipher v4 is HMAC_SHA512.
138+
pragmas.insert("cipher_hmac_algorithm".into(), None);
139+
104140
// Normally, page_size must be set before any other action on the database.
105141
// Defaults to 4096 for new databases.
106142
pragmas.insert("page_size".into(), None);
@@ -282,9 +318,9 @@ impl SqliteConnectOptions {
282318
/// Note this excerpt:
283319
/// > The collating function must obey the following properties for all strings A, B, and C:
284320
/// >
285-
/// > If A==B then B==A.
286-
/// > If A==B and B==C then A==C.
287-
/// > If A\<B then B>A.
321+
/// > If A==B then B==A.
322+
/// > If A==B and B==C then A==C.
323+
/// > If A\<B then B>A.
288324
/// > If A<B and B<C then A<C.
289325
/// >
290326
/// > If a collating function fails any of the above constraints and that collating function is
@@ -326,7 +362,7 @@ impl SqliteConnectOptions {
326362
/// ### Note
327363
/// Setting this to `true` may help if you are getting access violation errors or segmentation
328364
/// faults, but will also incur a significant performance penalty. You should leave this
329-
/// set to `false` if at all possible.
365+
/// set to `false` if at all possible.
330366
///
331367
/// If you do end up needing to set this to `true` for some reason, please
332368
/// [open an issue](https://github.com/launchbadge/sqlx/issues/new/choose) as this may indicate

0 commit comments

Comments
 (0)