2 # codec.test developed by Stephen Lombardo (Zetetic LLC)
3 # sjlombardo at zetetic dot net
6 # Copyright (c) 2018, ZETETIC LLC
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are met:
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # * Neither the name of the ZETETIC LLC nor the
15 # names of its contributors may be used to endorse or promote products
16 # derived from this software without specific prior written permission.
18 # THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
19 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 # DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
22 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # This file implements regression tests for SQLite library. The
30 # focus of this script is testing code cipher features.
32 # NOTE: tester.tcl has overridden the definition of sqlite3 to
33 # automatically pass in a key value. Thus tests in this file
34 # should explicitly close and open db with sqlite_orig in order
35 # to bypass default key assignment.
37 set testdir [file dirname $argv0]
38 source $testdir/tester.tcl
39 source $testdir/sqlcipher.tcl
41 # verify the pragma cipher_version
42 # returns the currently configured
44 do_test verify-pragma-cipher-version {
45 sqlite_orig db test.db
47 PRAGMA cipher_version;
51 file delete -force test.db
53 # verify the pragma cipher_use_hmac
54 # is set to true be default
55 do_test verify-pragma-cipher-use-hmac-default {
56 sqlite_orig db test.db
59 PRAGMA cipher_use_hmac;
63 file delete -force test.db
65 # verify the pragma cipher_use_hmac
66 # reports the flag turned off
67 do_test verify-pragma-cipher-use-hmac-off {
68 sqlite_orig db test.db
71 PRAGMA cipher_use_hmac = off;
72 PRAGMA cipher_use_hmac;
76 file delete -force test.db
78 # verify the pragma default_cipher_use_hmac
79 # is set to true by default
80 do_test verify-pragma-cipher-default-use-hmac-default {
81 sqlite_orig db test.db
83 PRAGMA cipher_default_use_hmac;
87 file delete -force test.db
89 # verify the pragma default_cipher_use_hmac
90 # reports the flag turned off
91 do_test verify-pragma-cipher-default-use-hmac-off {
92 sqlite_orig db test.db
94 PRAGMA cipher_default_use_hmac = off;
95 PRAGMA cipher_default_use_hmac;
96 -- Be sure to turn cipher_default_use_hmac
97 -- back on or it will break later tests
98 -- (it's a global flag)
99 PRAGMA cipher_default_use_hmac = ON;
103 file delete -force test.db
105 # verify the pragma default_cipher_kdf_iter
106 # is set to 256000 by default
107 do_test verify-pragma-cipher-default-kdf-iter-default {
108 sqlite_orig db test.db
110 PRAGMA cipher_default_kdf_iter;
114 file delete -force test.db
117 # verify the pragma default_cipher_kdf_ter
119 do_test verify-pragma-cipher-default-use-hmac-off {
120 sqlite_orig db test.db
122 PRAGMA cipher_default_kdf_iter = 1000;
123 PRAGMA cipher_default_kdf_iter;
124 PRAGMA cipher_default_kdf_iter = 256000;
128 file delete -force test.db
130 # verify the pragma kdf_iter
131 # reports the default value
132 do_test verify-pragma-kdf-iter-reports-default {
133 sqlite_orig db test.db
140 file delete -force test.db
142 # verify the pragma kdf_iter
143 # reports value changed
144 do_test verify-pragma-kdf-iter-reports-value-changed {
145 sqlite_orig db test.db
148 PRAGMA kdf_iter = 8000;
153 file delete -force test.db
155 # verify the pragma fast_kdf_iter
156 # reports the default value
157 do_test verify-pragma-fast-kdf-iter-reports-default {
158 sqlite_orig db test.db
161 PRAGMA fast_kdf_iter;
165 file delete -force test.db
167 # verify the pragma fast_kdf_iter
168 # reports value changed
169 do_test verify-pragma-kdf-iter-reports-value-changed {
170 sqlite_orig db test.db
173 PRAGMA fast_kdf_iter = 4000;
174 PRAGMA fast_kdf_iter;
176 } {ok {PRAGMA fast_kdf_iter is deprecated, please remove from use} 4000}
178 file delete -force test.db
180 # verify the pragma cipher_page_size
181 # reports default value
182 do_test verify-pragma-cipher-page-size-default {
183 sqlite_orig db test.db
186 PRAGMA cipher_page_size;
190 file delete -force test.db
192 # verify the pragma cipher_page_size
193 # reports change in value
194 do_test verify-pragma-cipher-page-size-changed {
195 sqlite_orig db test.db
198 PRAGMA cipher_page_size = 8192;
199 PRAGMA cipher_page_size;
203 file delete -force test.db
205 # verify that a call to pragma page_size
206 # will report change via both page_size and cipher_page_size
207 # when there is an attached codec
208 do_test verify-pragma-page-size-encrypted {
209 sqlite_orig db test.db
212 PRAGMA page_size = 8192;
214 PRAGMA cipher_page_size;
218 file delete -force test.db
220 # verify that a call to pragma page_size
221 # will not report a change to cipher_page_size for an
222 # unencrypted database
223 do_test verify-pragma-page-size-plaintext {
224 sqlite_orig db test.db
226 PRAGMA page_size = 8192;
228 PRAGMA cipher_page_size;
232 file delete -force test.db
234 # verify setting cipher_store_pass before key
235 # does not cause segfault
236 do_test verify-cipher-store-pass-before-key-does-not-segfault {
237 sqlite_orig db test.db
239 PRAGMA cipher_store_pass = 1;
244 file delete -force test.db
246 # verify setting cipher_store_pass results in deprecation warning
247 do_test verify-cipher-store-pass-deprecated {
248 sqlite_orig db test.db
251 PRAGMA cipher_store_pass = 1;
253 } {ok {PRAGMA cipher_store_pass is deprecated, please remove from use}}
255 file delete -force test.db
257 # verify the pragma cipher
258 # reports the default value
259 if_built_with_openssl verify-pragma-cipher-default {
260 sqlite_orig db test.db
267 file delete -force test.db
269 # verify the pragma cipher_hmac_salt_mask reports default
270 do_test verify-pragma-hmac-salt-mask-reports-default {
271 sqlite_orig db test.db
274 PRAGMA cipher_hmac_salt_mask;
278 file delete -force test.db
280 # verify the pragma cipher_hmac_salt_mask reports
281 # reports value changed
282 do_test verify-pragma-hmac-salt-mask-reports-value-changed {
283 sqlite_orig db test.db
286 PRAGMA cipher_hmac_salt_mask = "x'11'";
287 PRAGMA cipher_hmac_salt_mask;
288 PRAGMA cipher_hmac_salt_mask = "x'3a'";
290 } {ok {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use} 11 {PRAGMA cipher_hmac_salt_mask is deprecated, please remove from use}}
292 file delete -force test.db
294 # verify the pragma cipher_hmac_pgno reports default
295 do_test verify-pragma-hmac-pgno-reports-default {
296 sqlite_orig db test.db
299 PRAGMA cipher_hmac_pgno;
303 file delete -force test.db
305 # verify the pragma cipher_hmac_pgno
306 # reports value changed
307 do_test verify-pragma-hmac-pgno-reports-value-changed {
308 sqlite_orig db test.db
311 PRAGMA cipher_hmac_pgno = be;
312 PRAGMA cipher_hmac_pgno;
313 PRAGMA cipher_hmac_pgno = native;
314 PRAGMA cipher_hmac_pgno;
315 PRAGMA cipher_hmac_pgno = le;
316 PRAGMA cipher_hmac_pgno;
318 } {ok {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} be {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} native {PRAGMA cipher_hmac_pgno is deprecated, please remove from use} le}
320 file delete -force test.db
322 # verify the pragma cipher_hmac_algorithm works properly
323 do_test verify-pragma-cipher-hmac-algorithm-reports-default {
324 sqlite_orig db test.db
327 PRAGMA cipher_hmac_algorithm;
331 file delete -force test.db
333 do_test verify-pragma-cipher-hmac-algorithm-reports-value-changed {
334 sqlite_orig db test.db
337 PRAGMA cipher_hmac_algorithm = HMAC_SHA1;
338 PRAGMA cipher_hmac_algorithm;
342 file delete -force test.db
344 do_test verify-pragma-cipher-default-hmac-algorithm {
345 sqlite_orig db test.db
347 PRAGMA cipher_default_hmac_algorithm;
348 PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1;
349 PRAGMA cipher_default_hmac_algorithm;
350 PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512;
352 } {HMAC_SHA512 HMAC_SHA1}
354 file delete -force test.db
356 # verify the pragma cipher_kdf_algorithm works properly
357 do_test verify-pragma-cipher-kdf-algorithm-reports-default {
358 sqlite_orig db test.db
361 PRAGMA cipher_kdf_algorithm;
363 } {ok PBKDF2_HMAC_SHA512}
365 file delete -force test.db
367 do_test verify-pragma-cipher-kdf-algorithm-reports-value-changed {
368 sqlite_orig db test.db
371 PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;
372 PRAGMA cipher_kdf_algorithm;
374 } {ok PBKDF2_HMAC_SHA1}
376 file delete -force test.db
378 do_test verify-pragma-cipher-default-kdf-algorithm {
379 sqlite_orig db test.db
381 PRAGMA cipher_default_kdf_algorithm;
382 PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1;
383 PRAGMA cipher_default_kdf_algorithm;
384 PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512;
386 } {PBKDF2_HMAC_SHA512 PBKDF2_HMAC_SHA1}
388 file delete -force test.db
390 if_built_with_openssl verify-default-cipher {
391 sqlite_orig db test.db
398 file delete -force test.db
400 if_built_with_libtomcrypt verify-default-cipher {
401 sqlite_orig db test.db
408 file delete -force test.db
410 if_built_with_commoncrypto verify-default-cipher {
411 sqlite_orig db test.db
418 file delete -force test.db
420 if_built_with_nss verify-default-cipher {
421 sqlite_orig db test.db
428 file delete -force test.db
430 do_test verify-cipher_settings_default {
431 sqlite_orig db test.db
434 PRAGMA cipher_settings;
436 } {ok {PRAGMA kdf_iter = 256000;} {PRAGMA cipher_page_size = 4096;} {PRAGMA cipher_use_hmac = 1;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA512;}}
438 file delete -force test.db
440 do_test verify-cipher_settings_v1 {
441 sqlite_orig db test.db
444 PRAGMA cipher_compatibility = 1;
445 PRAGMA cipher_settings;
447 } {ok {PRAGMA kdf_iter = 4000;} {PRAGMA cipher_page_size = 1024;} {PRAGMA cipher_use_hmac = 0;} {PRAGMA cipher_plaintext_header_size = 0;} {PRAGMA cipher_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_kdf_algorithm = PBKDF2_HMAC_SHA1;}}
449 file delete -force test.db
451 do_test verify-cipher_default_settings_v1 {
452 sqlite_orig db test.db
454 PRAGMA cipher_default_compatibility = 1;
455 PRAGMA cipher_default_settings;
456 PRAGMA cipher_default_compatibility = 4;
458 } {{PRAGMA cipher_default_kdf_iter = 4000;} {PRAGMA cipher_default_page_size = 1024;} {PRAGMA cipher_default_use_hmac = 0;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA1;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA1;}}
460 file delete -force test.db
462 do_test verify-cipher_default_settings_default {
463 sqlite_orig db test.db
465 PRAGMA cipher_default_settings;
467 } {{PRAGMA cipher_default_kdf_iter = 256000;} {PRAGMA cipher_default_page_size = 4096;} {PRAGMA cipher_default_use_hmac = 1;} {PRAGMA cipher_default_plaintext_header_size = 0;} {PRAGMA cipher_default_hmac_algorithm = HMAC_SHA512;} {PRAGMA cipher_default_kdf_algorithm = PBKDF2_HMAC_SHA512;}}
469 file delete -force test.db