1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
|
# SQLCipher
# codec.test developed by Stephen Lombardo (Zetetic LLC)
# sjlombardo at zetetic dot net
# http://zetetic.net
#
# Copyright (c) 2018, ZETETIC LLC
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the ZETETIC LLC nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# This file implements regression tests for SQLite library. The
# focus of this script is testing code cipher features.
#
# NOTE: tester.tcl has overridden the definition of sqlite3 to
# automatically pass in a key value. Thus tests in this file
# should explicitly close and open db with sqlite_orig in order
# to bypass default key assignment.
set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/sqlcipher.tcl
# The database is initially empty.
# set an hex key create some basic data
# create table and insert operations should work
# close database, open it again with the same
# hex key. verify that the table is readable
# and the data just inserted is visible
setup test.db "\"x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'\""
do_test will-open-with-correct-raw-key {
sqlite_orig db test.db
execsql {
PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836481'";
SELECT name FROM sqlite_schema WHERE type='table';
SELECT * from t1;
}
} {ok t1 test1 test2}
db close
file delete -force test.db
# set an encryption key (non-hex) and create some basic data
# create table and insert operations should work
# close database, open it again with the same
# key. verify that the table is readable
# and the data just inserted is visible
setup test.db "'testkey'"
do_test will-open-with-correct-derived-key {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
SELECT name FROM sqlite_schema WHERE type='table';
SELECT * from t1;
}
} {ok t1 test1 test2}
db close
file delete -force test.db
# set an encryption key (non-hex) and create
# temp tables, verify you can read from
# sqlite_temp_master
setup test.db "'testkey'"
do_test test-temp-master {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
CREATE TEMPORARY TABLE temp_t1(a,b);
INSERT INTO temp_t1(a,b) VALUES ('test1', 'test2');
SELECT name FROM sqlite_temp_master WHERE type='table';
SELECT * from temp_t1;
}
} {ok temp_t1 test1 test2}
db close
file delete -force test.db
# verify that a when a standard database is encrypted the first
# 16 bytes are not "SQLite format 3\0"
do_test test-sqlcipher-header-overwrite {
sqlite_orig db test.db
execsql {
PRAGMA key = 'test';
CREATE TABLE t1(a,b);
}
db close
set header [hexio_read test.db 0 16]
string equal $header "53514C69746520666F726D6174203300"
} {0}
file delete -force test.db
# open the database and try to read from it without
# providing a passphrase. verify that the
# an error is returned from the library
setup test.db "'testkey'"
do_test wont-open-without-key {
sqlite_orig db test.db
catchsql {
SELECT name FROM sqlite_schema WHERE type='table';
}
} {1 {file is not a database}}
db close
file delete -force test.db
# open the database and try to set an invalid
# passphrase. verify that an error is returned
# and that data couldn't be read
setup test.db "'testkey'"
do_test wont-open-with-invalid-derived-key {
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey2';
SELECT name FROM sqlite_schema WHERE type='table';
}
} {1 {file is not a database}}
db close
file delete -force test.db
# open the database and try to set an invalid
# hex key. verify that an error is returned
# and that data couldn't be read
setup test.db "'testkey'"
do_test wont-open-with-invalid-raw-key {
sqlite_orig db test.db
catchsql {
PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480'";
SELECT name FROM sqlite_schema WHERE type='table';
}
} {1 {file is not a database}}
db close
file delete -force test.db
# test a large number of inserts in a transaction to a memory database
do_test memory-database {
sqlite_orig db :memory:
execsql {
PRAGMA key = 'testkey3';
BEGIN;
CREATE TABLE t2(a,b);
}
for {set i 1} {$i<=25000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t2 VALUES($i,$r);"
}
execsql {
COMMIT;
SELECT count(*) FROM t2;
DELETE FROM t2;
SELECT count(*) FROM t2;
}
} {25000 0}
db close
# test a large number of inserts in a transaction for multiple pages
do_test multi-page-database {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
CREATE TABLE t2(a,b);
BEGIN;
}
for {set i 1} {$i<=25000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t2 VALUES($i,$r);"
}
execsql {
COMMIT;
SELECT count(*) FROM t2;
}
} {25000}
db close
file delete -force test.db
# attach an encrypted database
# without specifying key, verify it fails
# even if the source passwords are the same
# because the kdf salts are different
setup test.db "'testkey'"
do_test attach-database-with-default-key {
sqlite_orig db2 test2.db
set rc {}
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher_add_random = "x'deadbaad'";
CREATE TABLE t2(a,b);
INSERT INTO t2 VALUES ('test1', 'test2');
} db2
lappend rc [catchsql {
ATTACH 'test.db' AS db;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {{1 {file is not a database}} 0}
db2 close
file delete -force test.db
file delete -force test2.db
# attach an empty encrypted database
# without specifying key, verify the database has the same
# salt and as the original
setup test.db "'testkey'"
do_test attach-empty-database-with-default-key {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key='testkey';
INSERT INTO t1(a,b) values (1,2);
ATTACH DATABASE 'test2.db' AS test;
CREATE TABLE test.t1(a,b);
INSERT INTO test.t1 SELECT * FROM t1;
DETACH DATABASE test;
}
sqlite_orig db2 test2.db
lappend rc [execsql {
PRAGMA key='testkey';
SELECT count(*) FROM t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {{ok 2} 1}
db close
db2 close
file delete -force test.db
file delete -force test2.db
# attach an empty encrypted database as the first operation on a keyed database. Verify
# that the new database has the same salt as the original.
#
# HISTORICAL NOTE: The original behavior of SQLCipher under these conditions
# was that the databases would have different salts but the same keys. This was because
# derivation of the key spec would not have occurred yet. However, upstream check-in
# https://sqlite.org/src/info/a02da71f3a80dd8e changed this behavior by
# forcing a read of the main database schema during the attach operation.
# This causes the main database to be opened and the key derivation logic to fire which
# reads the salt. Thus the current behavior of this test should now be identical
# to the previous attach-empty-database-with-default-key.
setup test.db "'testkey'"
do_test attach-empty-database-with-default-key-first-op {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key='testkey';
ATTACH DATABASE 'test2.db' AS test;
CREATE TABLE test.t1(a,b);
INSERT INTO test.t1 SELECT * FROM t1;
DETACH DATABASE test;
}
sqlite_orig db2 test2.db
lappend rc [execsql {
PRAGMA key='testkey';
SELECT count(*) FROM t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {{ok 1} 1}
db close
db2 close
file delete -force test.db
file delete -force test2.db
# attach an empty encrypted database
# on a keyed database when PRAGMA cipher_store_pass = 1
# and verify different salts
setup test.db "'testkey'"
do_test attach-empty-database-with-cipher-store-pass {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key='testkey';
PRAGMA cipher_store_pass = 1;
INSERT INTO t1(a,b) VALUES (1,2);
ATTACH DATABASE 'test2.db' AS test;
CREATE TABLE test.t1(a,b);
INSERT INTO test.t1 SELECT * FROM t1;
DETACH DATABASE test;
}
sqlite_orig db2 test2.db
lappend rc [execsql {
PRAGMA key='testkey';
SELECT count(*) FROM t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {{ok 2} 0}
db close
db2 close
file delete -force test.db
file delete -force test2.db
# attach an encrypted database
# without specifying key, verify it attaches
# correctly when PRAGMA cipher_store_pass = 1
# is set
do_test attach-database-with-default-key-using-cipher-store-pass {
sqlite_orig db1 test.db
execsql {
PRAGMA key = 'testkey';
CREATE TABLE t1(a,b);
INSERT INTO t1(a,b) VALUES('foo', 'bar');
} db1
db1 close
sqlite_orig db2 test2.db
execsql {
PRAGMA key = 'testkey';
CREATE TABLE t2(a,b);
INSERT INTO t2 VALUES ('test1', 'test2');
} db2
db2 close
sqlite_orig db1 test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher_store_pass = 1;
ATTACH DATABASE 'test2.db' as db2;
SELECT sqlcipher_export('db2');
DETACH DATABASE db2;
} db1
db1 close
sqlite_orig db2 test2.db
execsql {
PRAGMA key = 'testkey';
SELECT * FROM t1;
} db2
} {ok foo bar}
db2 close
file delete -force test.db
file delete -force test2.db
# attach an encrypted database
# where both database have the same
# key explicitly and verify they have different
# salt values
setup test.db "'testkey'"
do_test attach-database-with-same-key {
sqlite_orig db2 test2.db
set rc {}
execsql {
PRAGMA key = 'testkey';
CREATE TABLE t2(a,b);
INSERT INTO t2 VALUES ('test1', 'test2');
} db2
lappend rc [execsql {
SELECT count(*) FROM t2;
ATTACH 'test.db' AS db KEY 'testkey';
SELECT count(*) FROM db.t1;
} db2]
lappend rc [string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]]
} {{1 1} 0}
db2 close
file delete -force test.db
file delete -force test2.db
# attach an encrypted database
# where databases have different keys
setup test.db "'testkey'"
do_test attach-database-with-different-keys {
sqlite_orig db2 test2.db
execsql {
PRAGMA key = 'testkey2';
CREATE TABLE t2(a,b);
INSERT INTO t2 VALUES ('test1', 'test2');
} db2
execsql {
ATTACH 'test.db' AS db KEY 'testkey';
SELECT count(*) FROM db.t1;
SELECT count(*) FROM t2;
} db2
} {1 1}
db2 close
file delete -force test.db
file delete -force test2.db
# test locking across multiple handles
setup test.db "'testkey'"
do_test locking-across-multiple-handles-start {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
BEGIN EXCLUSIVE;
INSERT INTO t1 VALUES(1,2);
}
sqlite_orig dba test.db
catchsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
} dba
} {1 {database is locked}}
do_test locking-accross-multiple-handles-finish {
execsql {
COMMIT;
}
execsql {
SELECT count(*) FROM t1;
} dba
} {2}
db close
dba close
file delete -force test.db
# alter schema
setup test.db "'testkey'"
do_test alter-schema {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
ALTER TABLE t1 ADD COLUMN c;
INSERT INTO t1 VALUES (1,2,3);
INSERT INTO t1 VALUES (1,2,4);
CREATE TABLE t1a (a);
INSERT INTO t1a VALUES ('teststring');
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1 WHERE a IS NOT NULL;
SELECT count(*) FROM t1 WHERE c IS NOT NULL;
SELECT * FROM t1a;
}
} {ok 3 2 teststring}
db close
file delete -force test.db
# test alterations of KDF iterations and ciphers
# rekey then add
setup test.db "'testkey'"
do_test verify-errors-for-rekey-kdf-and-cipher-changes {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA rekey_kdf_iter = 1000;
PRAGMA rekey_cipher = 'aes-256-ecb';
}
} {ok {PRAGMA rekey_kdf_iter is no longer supported.} {PRAGMA rekey_cipher is no longer supported.}}
db close
file delete -force test.db
setup test.db "'testkey'"
do_test verify-errors-for-cipher-change {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher = 'aes-256-ecb';
}
} {ok {PRAGMA cipher is no longer supported.}}
db close
file delete -force test.db
# 1. create a database with a custom page size,
# 2. create table and insert operations should work
# 3. close database, open it again with the same
# key and page size
# 4. verify that the table is readable
# and the data just inserted is visible
do_test custom-pagesize-pragma-cipher-page-size {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher_page_size = 8192;
CREATE table t1(a,b);
BEGIN;
}
for {set i 1} {$i<=1000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t1 VALUES($i,'value $r');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher_page_size = 8192;
SELECT count(*) FROM t1;
}
} {ok 1000}
db close
file delete -force test.db
# run the same logic as previous test but use
# pragma page_size instead
do_test custom-pagesize-pragma-pagesize {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA page_size = 8192;
CREATE table t1(a,b);
BEGIN;
}
for {set i 1} {$i<=1000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t1 VALUES($i,'value $r');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA page_size = 8192;
SELECT count(*) FROM t1;
}
} {ok 1000}
db close
file delete -force test.db
# open the database with the default page size
## and verfiy that it is not readable
do_test custom-pagesize-must-match {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cipher_page_size = 8192;
CREATE table t1(a,b);
}
db close
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey';
SELECT name FROM sqlite_schema WHERE type='table';
}
} {1 {file is not a database}}
db close
file delete -force test.db
# 1. create a database with WAL journal mode
# 2. create table and insert operations should work
# 3. close database, open it again
# 4. verify that the table is present, readable, and that
# the journal mode is WAL
do_test journal-mode-wal {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA journal_mode = WAL;
CREATE table t1(a,b);
BEGIN;
}
for {set i 1} {$i<=1000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t1 VALUES($i,'value $r');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
PRAGMA journal_mode;
}
} {ok 1000 wal}
db close
file delete -force test.db
# open a database and try to use an invalid
# passphrase. verify that an error is returned
# and that data couldn't be read. without closing the databsae
# set the correct key and verify it is working.
setup test.db "'testkey'"
do_test multiple-key-calls-safe-wrong-key-first {
sqlite_orig db test.db
set rc {}
lappend rc [catchsql {
PRAGMA key = 'testkey2';
SELECT count(*) FROM sqlite_schema;
}]
lappend rc [execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM sqlite_schema;
}]
} {{1 {file is not a database}} {ok 1}}
db close
file delete -force test.db
# open a databse and use the valid key. Then
# use pragma key to try to set an invalid key
# without closing the database. It should not do anything
setup test.db "'testkey'"
do_test multiple-key-calls-safe {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cache_size = 0;
SELECT name FROM sqlite_schema WHERE type='table';
PRAGMA key = 'wrong key';
SELECT name FROM sqlite_schema WHERE type='table';
PRAGMA key = 'testkey';
SELECT name FROM sqlite_schema WHERE type='table';
}
} {ok t1 ok t1 ok t1}
db close
file delete -force test.db
# open a databse and use the valid key. Then
# use pragma cipher_compatability to adjust settings that
# would normally trigger key derivation again.
# the new settings should be ignored
setup test.db "'testkey'"
do_test setting-changes-after-key-calls-safe {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA cache_size = 0;
SELECT name FROM sqlite_schema WHERE type='table';
INSERT INTO t1(a,b) VALUES (2,zeroblob(8192));
PRAGMA cipher_compatibility=3;
INSERT INTO t1(a,b) VALUES (3,zeroblob(8192));
SELECT name FROM sqlite_schema WHERE type='table';
SELECT count(*) FROM t1;
}
} {ok t1 t1 3}
db close
file delete -force test.db
# 1. create a database with a custom hmac kdf iteration count,
# 2. create table and insert operations should work
# 3. close database, open it again with the same
# key and hmac kdf iteration count
# 4. verify that the table is readable
# and the data just inserted is visible
do_test custom-hmac-kdf-iter {
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA kdf_iter = 10;
CREATE table t1(a,b);
BEGIN;
}
for {set i 1} {$i<=1000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t1 VALUES($i,'value $r');"
}
execsql {
COMMIT;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
PRAGMA kdf_iter = 10;
SELECT count(*) FROM t1;
}
} {ok 1000}
db close
# open the database with the default hmac
# kdf iteration count
# to verify that it is not readable
do_test custom-hmac-kdf-iter-must-match {
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey';
SELECT name FROM sqlite_schema WHERE type='table';
}
} {1 {file is not a database}}
db close
file delete -force test.db
# open the database and turn on auto_vacuum
# then insert a bunch of data, delete it
# and verify that the file has become smaller
# but can still be opened with the proper
# key
do_test auto-vacuum {
sqlite_orig db test.db
set rc {}
execsql {
PRAGMA key = 'testkey';
PRAGMA auto_vacuum=FULL;
CREATE table t1(a,b);
BEGIN;
}
for {set i 1} {$i<=10000} {incr i} {
set r [expr {int(rand()*500000)}]
execsql "INSERT INTO t1 VALUES($i,'value $r');"
}
lappend rc [execsql {
COMMIT;
SELECT count(*) FROM t1;
}]
# grab current size of file
set sz [file size test.db]
# delete some records, and verify
# autovacuum removes them
execsql {
DELETE FROM t1 WHERE rowid > 5000;
}
db close
# grab new file size, post
# autovacuum
set sz2 [file size test.db]
# verify that the new size is
# smaller than the old size
if {$sz > $sz2} { lappend rc true }
sqlite_orig db test.db
lappend rc [execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
}]
} {10000 true {ok 5000}}
db close
file delete -force test.db
# test kdf_iter and other pragmas
# before a key is set. Verify that they
# are no-ops
do_test cipher-options-before-keys {
sqlite_orig db test.db
execsql {
PRAGMA kdf_iter = 1000;
PRAGMA cipher_page_size = 8192;
PRAGMA cipher_use_hmac = OFF;
PRAGMA key = 'testkey';
CREATE table t1(a,b);
INSERT INTO t1 VALUES(1,2);
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
}
} {ok 1}
db close
file delete -force test.db
# verify memory security behavior
# initially should report OFF
# then enable, check that it is ON
# try to turn if off, but verify that it
# can't be unset.
do_test verify-memory-security {
sqlite_orig db test.db
execsql {
PRAGMA cipher_memory_security;
PRAGMA cipher_memory_security = ON;
PRAGMA cipher_memory_security;
PRAGMA cipher_memory_security = OFF;
PRAGMA cipher_memory_security;
}
} {0 1 1}
db close
file delete -force test.db
# create two new database files, write to each
# and verify that they have different (i.e. random)
# salt values
do_test test-random-salt {
sqlite_orig db test.db
sqlite_orig db2 test2.db
execsql {
PRAGMA key = 'test';
CREATE TABLE t1(a,b);
INSERT INTO t1(a,b) VALUES (1,2);
}
execsql {
PRAGMA key = 'test';
CREATE TABLE t1(a,b);
INSERT INTO t1(a,b) VALUES (1,2);
} db2
db close
db2 close
string equal [hexio_read test.db 0 16] [hexio_read test2.db 0 16]
} {0}
file delete -force test.db
file delete -force test2.db
# test scenario where multiple handles are opened
# to a file that does not exist, where both handles
# use the same key
do_test multiple-handles-same-key-and-salt {
sqlite_orig db test.db
sqlite_orig dba test.db
execsql {
PRAGMA key = 'testkey';
}
execsql {
PRAGMA key = 'testkey';
} dba
execsql {
CREATE TABLE t1(a,b);
INSERT INTO t1 VALUES(1,2);
}
execsql {
SELECT count(*) FROM t1;
}
execsql {
SELECT count(*) FROM t1;
} dba
} {1}
db close
dba close
file delete -force test.db
do_test test_flags_fail_encrypt {
sqlite_orig db :memory:
execsql {
PRAGMA cipher_test;
PRAGMA cipher_test_on = fail_encrypt;
PRAGMA cipher_test;
PRAGMA cipher_test_off = fail_encrypt;
PRAGMA cipher_test;
}
} {0 1 0}
db close
do_test test_flags_fail_decrypt {
sqlite_orig db :memory:
execsql {
PRAGMA cipher_test;
PRAGMA cipher_test_on = fail_decrypt;
PRAGMA cipher_test;
PRAGMA cipher_test_off = fail_decrypt;
PRAGMA cipher_test;
}
} {0 2 0}
db close
do_test test_flags_fail_migrate {
sqlite_orig db :memory:
execsql {
PRAGMA cipher_test;
PRAGMA cipher_test_on = fail_migrate;
PRAGMA cipher_test;
PRAGMA cipher_test_off = fail_migrate;
PRAGMA cipher_test;
}
} {0 4 0}
db close
do_test test_flags_combo {
sqlite_orig db :memory:
execsql {
PRAGMA cipher_test;
PRAGMA cipher_test_on = fail_encrypt;
PRAGMA cipher_test_on = fail_migrate;
PRAGMA cipher_test;
PRAGMA cipher_test_off = fail_encrypt;
PRAGMA cipher_test_off = fail_migrate;
PRAGMA cipher_test;
}
} {0 5 0}
db close
# test empty key
# it should raise an error
do_test empty-key {
sqlite_orig db test.db
catchsql {
PRAGMA key = '';
}
} {1 {An error occurred with PRAGMA key or rekey. PRAGMA key requires a key of one or more characters. PRAGMA rekey can only be run on an existing encrypted database. Use sqlcipher_export() and ATTACH to convert encrypted/plaintext databases.}}
db close
file delete -force test.db
# configure URI filename support
# create a new encrypted database with the key via parameter
# close database
# open normally providing key via pragma verify
# correct key works
sqlite3_shutdown
sqlite3_config_uri 1
do_test uri-key {
sqlite_orig db file:test.db?a=a&key=testkey&c=c
execsql {
CREATE TABLE t1(a,b);
INSERT INTO t1 VALUES(1,2);
}
db close
sqlite_orig db test.db
catchsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
}
db close
sqlite_orig db test.db
execsql {
PRAGMA key = 'testkey';
SELECT count(*) FROM t1;
}
} {ok 1}
db close
# verify wrong key fails
do_test uri-key-2 {
sqlite_orig db test.db
catchsql {
PRAGMA key = 'test';
SELECT count(*) FROM t1;
}
} {1 {file is not a database}}
db close
file delete -force test.db
sqlite3_shutdown
sqlite3_config_uri 0
finish_test
|