-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsqlcipher_wolfssl_v4.6.1_gitinfo.patch
More file actions
3646 lines (3627 loc) · 112 KB
/
sqlcipher_wolfssl_v4.6.1_gitinfo.patch
File metadata and controls
3646 lines (3627 loc) · 112 KB
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
From fb103ae04134dbc31c5617d076118a0dfe7c7a2b Mon Sep 17 00:00:00 2001
From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com>
Date: Tue, 5 Nov 2024 15:58:33 -0700
Subject: [PATCH 1/2] - add wolfSSL support to refactored sqlcipher - add patch
generation script
---
Makefile.in | 4 +
Makefile.msc | 1 +
README.md | 30 +++++
SQLCipher.podspec.json | 4 +-
configure.ac | 18 ++-
src/crypto_wolfssl.c | 246 +++++++++++++++++++++++++++++++++++++++++
src/sqlcipher.c | 4 +
tool/mksqlite3c.tcl | 1 +
8 files changed, 301 insertions(+), 7 deletions(-)
create mode 100644 src/crypto_wolfssl.c
diff --git a/Makefile.in b/Makefile.in
index 8431c25a..54d438cf 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -149,6 +149,7 @@ CRYPTOLIBOBJ = \
crypto_openssl.lo \
crypto_libtomcrypt.lo \
crypto_nss.lo \
+ crypto_wolfssl.lo \
crypto_cc.lo
CRYPTOSRC = \
@@ -157,6 +158,7 @@ CRYPTOSRC = \
$(TOP)/src/crypto_libtomcrypt.c \
$(TOP)/src/crypto_nss.c \
$(TOP)/src/crypto_openssl.c \
+ $(TOP)/src/crypto_wolfssl.c \
$(TOP)/src/crypto_cc.c
# END CRYPTO
@@ -900,6 +902,8 @@ crypto_libtomcrypt.lo: $(TOP)/src/crypto_libtomcrypt.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/crypto_libtomcrypt.c
crypto_cc.lo: $(TOP)/src/crypto_cc.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/crypto_cc.c
+crypto_wolfssl.lo: $(TOP)/src/crypto_wolfssl.c $(HDR)
+ $(LTCOMPILE) -c $(TOP)/src/crypto_wolfssl.c
# END CRYPTO
# Rules to build individual *.o files from files in the src directory.
diff --git a/Makefile.msc b/Makefile.msc
index d46cee15..bc77cfe4 100644
--- a/Makefile.msc
+++ b/Makefile.msc
@@ -1332,6 +1332,7 @@ SRC00 = \
$(TOP)\src\crypto_libtomcrypt.c \
$(TOP)\src\crypto_nss.c \
$(TOP)\src\crypto_openssl.c \
+ $(TOP)\src\crypto_wolfssl.c \
$(TOP)\src\sqlcipher.h \
$(TOP)\src\alter.c \
$(TOP)\src\analyze.c \
diff --git a/README.md b/README.md
index 83cb3825..4d0282d7 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,33 @@
+## WolfSSL Instructions
+
+To build with wolfSSL, clone this repo and build as usual, but regenerate the configure file and then add the `-with-crypto-lib=wolfssl` option to `configure`. For example, to build and run the tests after cloning (following the instructions in the original README below), you should run the following commands:
+
+```sh
+$ autoreconf --install --force # Necessary only the first time, since we modify configure.ac to add an option for wolfSSL
+$ ./configure --enable-tempstore=yes --with-crypto-lib=wolfssl --enable-fts5 CFLAGS="-DSQLITE_HAS_CODEC -DSQLCIPHER_TEST" LDFLAGS="-lwolfssl"
+$ make
+$ make testfixture
+$ ./testfixture test/sqlcipher.test
+```
+
+Note that if using a FIPS build, the sqlcipher tests will all fail as they use a password/key shorter than the minimum FIPS mandated length (14 bytes). There are some tests that are easy to change to accomodate that (`sqlcipher-backup.test`, for example). For these you can run `sed -i 's/testkey/testkey012345678/g'`. Other tests will take too long to fix as they use random keys ("foo", "0123", etc) and others like `sqlcipher-compatibility.test` operate on databases already encrypted with short keys.
+
+### Troubleshooting
+
+Note that the SQLite test suite requires the tcl development headers to be installed on the system. If they are not installed, `make testfixture` will fail with errors like:
+
+``
+sqlcipher/src/test1.c:32:12: fatal error: tcl.h: No such file or directory
+ 32 | # include "tcl.h"
+ | ^~~~~~~
+compilation terminated.
+``
+
+To fix this on Ubuntu, [install the tcl-dev package](https://askubuntu.com/a/568760) (`apt install tcl-dev`)
+
+
+# Original Readme:
+
## SQLCipher
SQLCipher is a standalone fork of the [SQLite](https://www.sqlite.org/) database library that adds 256 bit AES encryption of database files and other security features like:
diff --git a/SQLCipher.podspec.json b/SQLCipher.podspec.json
index 90c5eb3a..e564baa3 100644
--- a/SQLCipher.podspec.json
+++ b/SQLCipher.podspec.json
@@ -14,7 +14,7 @@
"tvos": "12.0",
"watchos": "7.0"
},
- "prepare_command": "./configure --enable-tempstore=yes --with-crypto-lib=commoncrypto CFLAGS=\"-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT3 -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DHAVE_USLEEP=1 -DSQLITE_MAX_VARIABLE_NUMBER=99999\"; make sqlite3.c",
+ "prepare_command": "./configure --enable-tempstore=yes --with-crypto-lib=wolfssl CFLAGS=\"-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT3 -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DHAVE_USLEEP=1 -DSQLITE_MAX_VARIABLE_NUMBER=99999\"; make sqlite3.c",
"requires_arc": false,
"source": {
"git": "https://github.com/sqlcipher/sqlcipher.git",
@@ -42,7 +42,7 @@
"-DSQLITE_ENABLE_UNLOCK_NOTIFY",
"-DSQLITE_ENABLE_JSON1",
"-DSQLITE_ENABLE_FTS5",
- "-DSQLCIPHER_CRYPTO_CC",
+ "-DSQLCIPHER_CRYPTO_WOLFSSL",
"-DHAVE_USLEEP=1",
"-DSQLITE_MAX_VARIABLE_NUMBER=99999"
],
diff --git a/configure.ac b/configure.ac
index 50273b49..684dd011 100644
--- a/configure.ac
+++ b/configure.ac
@@ -273,11 +273,19 @@ else
AC_CHECK_LIB([nss3], [PK11_Decrypt], ,
AC_MSG_ERROR([Library crypto not found. Install nss!"]))
else
- CFLAGS="$CFLAGS -DSQLCIPHER_CRYPTO_OPENSSL"
- BUILD_CFLAGS="$BUILD_CFLAGS -DSQLCIPHER_CRYPTO_OPENSSL"
- AC_MSG_RESULT([openssl])
- AC_CHECK_LIB([crypto], [HMAC_Init_ex], ,
- AC_MSG_ERROR([Library crypto not found. Install openssl!"]))
+ if test "$crypto_lib" = "wolfssl"; then
+ CFLAGS="$CFLAGS -DSQLCIPHER_CRYPTO_WOLFSSL"
+ BUILD_CFLAGS="$BUILD_CFLAGS -DSQLCIPHER_CRYPTO_WOLFSSL"
+ AC_MSG_RESULT([wolfssl])
+ AC_CHECK_LIB([wolfssl], [wolfCrypt_Init], ,
+ AC_MSG_ERROR([Library crypto not found. Install wolfSSL!"]))
+ else
+ CFLAGS="$CFLAGS -DSQLCIPHER_CRYPTO_OPENSSL"
+ BUILD_CFLAGS="$BUILD_CFLAGS -DSQLCIPHER_CRYPTO_OPENSSL"
+ AC_MSG_RESULT([openssl])
+ AC_CHECK_LIB([crypto], [HMAC_Init_ex], ,
+ AC_MSG_ERROR([Library crypto not found. Install openssl!"]))
+ fi
fi
fi
fi
diff --git a/src/crypto_wolfssl.c b/src/crypto_wolfssl.c
new file mode 100644
index 00000000..40c3ffca
--- /dev/null
+++ b/src/crypto_wolfssl.c
@@ -0,0 +1,246 @@
+/*
+** SQLCipher
+** http://sqlcipher.net
+**
+** Copyright (c) 2008 - 2013, ZETETIC LLC
+** All rights reserved.
+**
+** 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.
+**
+*/
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifdef SQLCIPHER_CRYPTO_WOLFSSL
+#include "sqliteInt.h"
+#include "sqlcipher.h"
+
+#include <wolfssl/options.h>
+#include <wolfssl/wolfcrypt/settings.h>
+#include <wolfssl/wolfcrypt/aes.h>
+#include <wolfssl/wolfcrypt/sha.h>
+#include <wolfssl/wolfcrypt/sha256.h>
+#include <wolfssl/wolfcrypt/sha512.h>
+#include <wolfssl/wolfcrypt/hmac.h>
+#include <wolfssl/wolfcrypt/random.h>
+#include <wolfssl/wolfcrypt/pwdbased.h>
+#include <wolfssl/wolfcrypt/error-crypt.h>
+#include <wolfssl/version.h>
+
+int sqlcipher_wolf_setup(sqlcipher_provider *p);
+
+#ifdef HAVE_FIPS
+#include <wolfssl/wolfcrypt/fips_test.h>
+static void wcFipsCb(int ok, int err, const char* hash)
+{
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, "wolfCrypt Fips error callback, ok = %d, err = %d\n", ok, err);
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, "message = %s\n", wc_GetErrorString(err));
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, "hash = %s\n", hash);
+ if (err == IN_CORE_FIPS_E) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, "In core integrity hash check failure, copy above hash\n");
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, "into verifyCore[] in fips_test.c and rebuild\n");
+ }
+}
+#endif
+
+static int sqlcipher_wolf_add_random(void *ctx, void *buffer, int length) {
+ (void)ctx;
+ (void)buffer;
+ (void)length;
+ /* not used */
+ return SQLITE_OK;
+}
+
+/* generate a defined number of random bytes */
+static WC_RNG gRng;
+static int gRngInit = 0;
+static int sqlcipher_wolf_random(void *ctx, void *buffer, int length) {
+ int ret = -1;
+ if (!gRngInit) {
+ ret = wc_InitRng(&gRng);
+ if (ret == 0) {
+ gRngInit = 1;
+ }
+ }
+ if (gRngInit) {
+ ret = wc_RNG_GenerateBlock(&gRng, buffer, length);
+ }
+ return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static const char* sqlcipher_wolf_get_provider_name(void *ctx) {
+ return "wolfssl";
+}
+
+static const char* sqlcipher_wolf_get_provider_version(void *ctx) {
+ return LIBWOLFSSL_VERSION_STRING;
+}
+
+static int sqlcipher_wolf_hmac(void *ctx, int algorithm, unsigned char *hmac_key,
+ int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
+ int ret;
+ Hmac hmac_context;
+ if(in == NULL) return SQLITE_ERROR;
+ if (wc_HmacInit(&hmac_context, NULL, INVALID_DEVID) != 0) return SQLITE_ERROR;
+ switch(algorithm) {
+ case SQLCIPHER_HMAC_SHA1:
+ ret = wc_HmacSetKey(&hmac_context, WC_SHA, hmac_key, key_sz);
+ break;
+ case SQLCIPHER_HMAC_SHA256:
+ ret = wc_HmacSetKey(&hmac_context, WC_SHA256, hmac_key, key_sz);
+ break;
+ case SQLCIPHER_HMAC_SHA512:
+ ret = wc_HmacSetKey(&hmac_context, WC_SHA512, hmac_key, key_sz);
+ break;
+ default:
+ ret = SQLITE_ERROR;
+ }
+ if (ret == 0)
+ ret = wc_HmacUpdate(&hmac_context, in, in_sz);
+ if (ret == 0 && in2 != NULL)
+ ret = wc_HmacUpdate(&hmac_context, in2, in2_sz);
+ if (ret == 0)
+ ret = wc_HmacFinal(&hmac_context, out);
+ wc_HmacFree(&hmac_context);
+ return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static int sqlcipher_wolf_kdf(void *ctx, int algorithm, const unsigned char *pass,
+ int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
+ int ret;
+ switch(algorithm) {
+ case SQLCIPHER_HMAC_SHA1:
+ ret = wc_PBKDF2(key, pass, pass_sz, salt, salt_sz, workfactor, key_sz, WC_SHA);
+ break;
+ case SQLCIPHER_HMAC_SHA256:
+ ret = wc_PBKDF2(key, pass, pass_sz, salt, salt_sz, workfactor, key_sz, WC_SHA256);
+ break;
+ case SQLCIPHER_HMAC_SHA512:
+ ret = wc_PBKDF2(key, pass, pass_sz, salt, salt_sz, workfactor, key_sz, WC_SHA512);
+ break;
+ default:
+ ret = SQLITE_ERROR;
+ }
+ return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static int sqlcipher_wolf_cipher(void *ctx, int mode, unsigned char *key,
+ int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
+ int ret;
+ Aes aes;
+ if (wc_AesInit(&aes, NULL, INVALID_DEVID) != 0) return SQLITE_ERROR;
+ ret = wc_AesSetKey(&aes, key, key_sz, iv,
+ mode == CIPHER_ENCRYPT ? AES_ENCRYPTION : AES_DECRYPTION);
+ if (ret == 0) {
+ if (mode == CIPHER_ENCRYPT)
+ ret = wc_AesCbcEncrypt(&aes, out, in, in_sz);
+ else
+ ret = wc_AesCbcDecrypt(&aes, out, in, in_sz);
+ }
+ wc_AesFree(&aes);
+ return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
+}
+
+static const char* sqlcipher_wolf_get_cipher(void *ctx) {
+ return "aes-256-cbc";
+}
+
+static int sqlcipher_wolf_get_key_sz(void *ctx) {
+ return AES_256_KEY_SIZE;
+}
+
+static int sqlcipher_wolf_get_iv_sz(void *ctx) {
+ return AES_BLOCK_SIZE;
+}
+
+static int sqlcipher_wolf_get_block_sz(void *ctx) {
+ return AES_BLOCK_SIZE;
+}
+
+static int sqlcipher_wolf_get_hmac_sz(void *ctx, int algorithm) {
+ switch(algorithm) {
+ case SQLCIPHER_HMAC_SHA1:
+ return WC_SHA_DIGEST_SIZE;
+ case SQLCIPHER_HMAC_SHA256:
+ return WC_SHA256_DIGEST_SIZE;
+ case SQLCIPHER_HMAC_SHA512:
+ return WC_SHA512_DIGEST_SIZE;
+ default:
+ return 0;
+ }
+}
+
+static int sqlcipher_wolf_ctx_init(void **ctx) {
+
+ if (wolfCrypt_Init() != 0) {
+ return SQLITE_ERROR;
+ }
+#ifdef HAVE_FIPS
+ wolfCrypt_SetCb_fips(wcFipsCb);
+#if (FIPS_VERSION_GE(5,3))
+ wc_SetSeed_Cb(wc_GenerateSeed);
+#endif
+#endif
+ return SQLITE_OK;
+}
+
+static int sqlcipher_wolf_ctx_free(void **ctx) {
+ if (gRngInit) {
+ wc_FreeRng(&gRng);
+ gRngInit = 0;
+ }
+
+ wolfCrypt_Cleanup();
+ return SQLITE_OK;
+}
+
+static int sqlcipher_wolf_fips_status(void *ctx) {
+#ifdef HAVE_FIPS
+ if (wolfCrypt_GetStatus_fips() == 0) {
+ return 1; /* FIPS available and valid */
+ }
+#endif
+ return 0;
+}
+
+int sqlcipher_wolf_setup(sqlcipher_provider *p) {
+ p->random = sqlcipher_wolf_random;
+ p->get_provider_name = sqlcipher_wolf_get_provider_name;
+ p->hmac = sqlcipher_wolf_hmac;
+ p->kdf = sqlcipher_wolf_kdf;
+ p->cipher = sqlcipher_wolf_cipher;
+ p->get_cipher = sqlcipher_wolf_get_cipher;
+ p->get_key_sz = sqlcipher_wolf_get_key_sz;
+ p->get_iv_sz = sqlcipher_wolf_get_iv_sz;
+ p->get_block_sz = sqlcipher_wolf_get_block_sz;
+ p->get_hmac_sz = sqlcipher_wolf_get_hmac_sz;
+ p->ctx_init = sqlcipher_wolf_ctx_init;
+ p->ctx_free = sqlcipher_wolf_ctx_free;
+ p->add_random = sqlcipher_wolf_add_random;
+ p->fips_status = sqlcipher_wolf_fips_status;
+ p->get_provider_version = sqlcipher_wolf_get_provider_version;
+ return SQLITE_OK;
+}
+
+#endif /* SQLCIPHER_CRYPTO_WOLFSSL */
+#endif
+/* END SQLCIPHER */
diff --git a/src/sqlcipher.c b/src/sqlcipher.c
index 8be4bc92..aeee88d5 100644
--- a/src/sqlcipher.c
+++ b/src/sqlcipher.c
@@ -76,6 +76,7 @@ void sqlite3pager_reset(Pager *pPager);
#if !defined (SQLCIPHER_CRYPTO_CC) \
&& !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
&& !defined (SQLCIPHER_CRYPTO_NSS) \
+ && !defined (SQLCIPHER_CRYPTO_WOLFSSL) \
&& !defined (SQLCIPHER_CRYPTO_OPENSSL)
#define SQLCIPHER_CRYPTO_OPENSSL
#endif
@@ -534,6 +535,9 @@ static void sqlcipher_activate() {
#elif defined (SQLCIPHER_CRYPTO_NSS)
extern int sqlcipher_nss_setup(sqlcipher_provider *p);
sqlcipher_nss_setup(p);
+#elif defined (SQLCIPHER_CRYPTO_WOLFSSL)
+ extern int sqlcipher_wolf_setup(sqlcipher_provider *p);
+ sqlcipher_wolf_setup(p);
#elif defined (SQLCIPHER_CRYPTO_OPENSSL)
extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
sqlcipher_openssl_setup(p);
diff --git a/tool/mksqlite3c.tcl b/tool/mksqlite3c.tcl
index 5cfcd6f0..4542a58d 100644
--- a/tool/mksqlite3c.tcl
+++ b/tool/mksqlite3c.tcl
@@ -434,6 +434,7 @@ set flist {
crypto_nss.c
crypto_openssl.c
crypto_cc.c
+ crypto_wolfssl.c
walker.c
resolve.c
--
2.34.1
From a3520d6709bb3777aba0939dc9f8d661d1687ec9 Mon Sep 17 00:00:00 2001
From: Brett Nicholas <7547222+bigbrett@users.noreply.github.com>
Date: Tue, 3 Dec 2024 14:28:40 -0700
Subject: [PATCH 2/2] - add private key lock/unlock for FIPS 140-3 - change
FIPS version macros, added error prints for failure cases - Add new tests for
wolfSSL FIPS
---
README.md | 2 +-
src/crypto_wolfssl.c | 118 ++-
test/sqlcipher-backup-wolfssl-fips.test | 162 +++
test/sqlcipher-codecerror-wolfssl-fips.test | 177 ++++
test/sqlcipher-core-wolfssl-fips.test | 985 ++++++++++++++++++
test/sqlcipher-integrity-wolfssl-fips.test | 277 +++++
...lcipher-plaintext-header-wolfssl-fips.test | 477 +++++++++
test/sqlcipher-pragmas-wolfssl-fips.test | 504 +++++++++
test/sqlcipher-rekey-wolfssl-fips.test | 280 +++++
test/sqlcipher-wolfssl-fips.test | 66 ++
10 files changed, 3022 insertions(+), 26 deletions(-)
create mode 100644 test/sqlcipher-backup-wolfssl-fips.test
create mode 100644 test/sqlcipher-codecerror-wolfssl-fips.test
create mode 100644 test/sqlcipher-core-wolfssl-fips.test
create mode 100644 test/sqlcipher-integrity-wolfssl-fips.test
create mode 100644 test/sqlcipher-plaintext-header-wolfssl-fips.test
create mode 100644 test/sqlcipher-pragmas-wolfssl-fips.test
create mode 100644 test/sqlcipher-rekey-wolfssl-fips.test
create mode 100644 test/sqlcipher-wolfssl-fips.test
diff --git a/README.md b/README.md
index 4d0282d7..69326aee 100644
--- a/README.md
+++ b/README.md
@@ -10,7 +10,7 @@ $ make testfixture
$ ./testfixture test/sqlcipher.test
```
-Note that if using a FIPS build, the sqlcipher tests will all fail as they use a password/key shorter than the minimum FIPS mandated length (14 bytes). There are some tests that are easy to change to accomodate that (`sqlcipher-backup.test`, for example). For these you can run `sed -i 's/testkey/testkey012345678/g'`. Other tests will take too long to fix as they use random keys ("foo", "0123", etc) and others like `sqlcipher-compatibility.test` operate on databases already encrypted with short keys.
+Note that if using a FIPS build, the normal sqlcipher tests will all fail as they use a password/key shorter than the minimum FIPS mandated length (14 bytes). wolfSSL has provided a modified suite of tests that can ve ran against a FIPS build. These tests use longer FIPS-compliant keys, and remove tests that operate on pre-encrypted databases with these keys. You can run the SQLCipher wolfSSL FIPS tests with `./testsuite sqlcipher-wolfssl-fips.test`. Non-FIPS wolfSSL builds can use the normal sqlcipher tests.
### Troubleshooting
diff --git a/src/crypto_wolfssl.c b/src/crypto_wolfssl.c
index 40c3ffca..d53480f3 100644
--- a/src/crypto_wolfssl.c
+++ b/src/crypto_wolfssl.c
@@ -50,14 +50,13 @@ int sqlcipher_wolf_setup(sqlcipher_provider *p);
#ifdef HAVE_FIPS
#include <wolfssl/wolfcrypt/fips_test.h>
-static void wcFipsCb(int ok, int err, const char* hash)
-{
- sqlcipher_log(SQLCIPHER_LOG_ERROR, "wolfCrypt Fips error callback, ok = %d, err = %d\n", ok, err);
- sqlcipher_log(SQLCIPHER_LOG_ERROR, "message = %s\n", wc_GetErrorString(err));
- sqlcipher_log(SQLCIPHER_LOG_ERROR, "hash = %s\n", hash);
+static void wcFipsCb(int ok, int err, const char* hash) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "wolfCrypt Fips error callback, ok = %d, err = %d\n", ok, err);
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "message = %s\n", wc_GetErrorString(err));
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "hash = %s\n", hash);
if (err == IN_CORE_FIPS_E) {
- sqlcipher_log(SQLCIPHER_LOG_ERROR, "In core integrity hash check failure, copy above hash\n");
- sqlcipher_log(SQLCIPHER_LOG_ERROR, "into verifyCore[] in fips_test.c and rebuild\n");
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "In core integrity hash check failure, copy above hash\n");
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER, "into verifyCore[] in fips_test.c and rebuild\n");
}
}
#endif
@@ -77,12 +76,21 @@ static int sqlcipher_wolf_random(void *ctx, void *buffer, int length) {
int ret = -1;
if (!gRngInit) {
ret = wc_InitRng(&gRng);
- if (ret == 0) {
- gRngInit = 1;
+ if (ret != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt RNG init failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ return SQLITE_ERROR;
}
+ gRngInit = 1;
}
if (gRngInit) {
- ret = wc_RNG_GenerateBlock(&gRng, buffer, length);
+ ret = wc_RNG_GenerateBlock(&gRng, buffer, length);
+ if (ret != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt RNG generation failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ }
}
return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
}
@@ -98,9 +106,16 @@ static const char* sqlcipher_wolf_get_provider_version(void *ctx) {
static int sqlcipher_wolf_hmac(void *ctx, int algorithm, unsigned char *hmac_key,
int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out) {
int ret;
+
Hmac hmac_context;
if(in == NULL) return SQLITE_ERROR;
- if (wc_HmacInit(&hmac_context, NULL, INVALID_DEVID) != 0) return SQLITE_ERROR;
+
+ if (wc_HmacInit(&hmac_context, NULL, INVALID_DEVID) != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt HMAC init failed\n");
+ return SQLITE_ERROR;
+ }
+
switch(algorithm) {
case SQLCIPHER_HMAC_SHA1:
ret = wc_HmacSetKey(&hmac_context, WC_SHA, hmac_key, key_sz);
@@ -114,12 +129,39 @@ static int sqlcipher_wolf_hmac(void *ctx, int algorithm, unsigned char *hmac_key
default:
ret = SQLITE_ERROR;
}
- if (ret == 0)
- ret = wc_HmacUpdate(&hmac_context, in, in_sz);
- if (ret == 0 && in2 != NULL)
- ret = wc_HmacUpdate(&hmac_context, in2, in2_sz);
- if (ret == 0)
- ret = wc_HmacFinal(&hmac_context, out);
+
+ if (ret != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt HMAC set key failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ wc_HmacFree(&hmac_context);
+ return SQLITE_ERROR;
+ }
+
+ if ((ret = wc_HmacUpdate(&hmac_context, in, in_sz)) != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt HMAC update failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ wc_HmacFree(&hmac_context);
+ return SQLITE_ERROR;
+ }
+
+ if (in2 != NULL) {
+ if ((ret = wc_HmacUpdate(&hmac_context, in2, in2_sz)) != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt HMAC update (in2) failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ wc_HmacFree(&hmac_context);
+ return SQLITE_ERROR;
+ }
+ }
+
+ if ((ret = wc_HmacFinal(&hmac_context, out)) != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt HMAC final failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ }
+
wc_HmacFree(&hmac_context);
return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
}
@@ -147,15 +189,37 @@ static int sqlcipher_wolf_cipher(void *ctx, int mode, unsigned char *key,
int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
int ret;
Aes aes;
- if (wc_AesInit(&aes, NULL, INVALID_DEVID) != 0) return SQLITE_ERROR;
+
+ if (wc_AesInit(&aes, NULL, INVALID_DEVID) != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt AES init failed\n");
+ return SQLITE_ERROR;
+ }
+
ret = wc_AesSetKey(&aes, key, key_sz, iv,
mode == CIPHER_ENCRYPT ? AES_ENCRYPTION : AES_DECRYPTION);
- if (ret == 0) {
- if (mode == CIPHER_ENCRYPT)
- ret = wc_AesCbcEncrypt(&aes, out, in, in_sz);
- else
- ret = wc_AesCbcDecrypt(&aes, out, in, in_sz);
+
+ if (ret != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt AES set key failed with code %d: %s\n",
+ ret, wc_GetErrorString(ret));
+ wc_AesFree(&aes);
+ return SQLITE_ERROR;
}
+
+ if (mode == CIPHER_ENCRYPT) {
+ ret = wc_AesCbcEncrypt(&aes, out, in, in_sz);
+ } else {
+ ret = wc_AesCbcDecrypt(&aes, out, in, in_sz);
+ }
+
+ if (ret != 0) {
+ sqlcipher_log(SQLCIPHER_LOG_ERROR, SQLCIPHER_LOG_PROVIDER,
+ "wolfCrypt AES %s failed with code %d: %s\n",
+ mode == CIPHER_ENCRYPT ? "encryption" : "decryption",
+ ret, wc_GetErrorString(ret));
+ }
+
wc_AesFree(&aes);
return (ret == 0) ? SQLITE_OK : SQLITE_ERROR;
}
@@ -190,16 +254,17 @@ static int sqlcipher_wolf_get_hmac_sz(void *ctx, int algorithm) {
}
static int sqlcipher_wolf_ctx_init(void **ctx) {
-
if (wolfCrypt_Init() != 0) {
return SQLITE_ERROR;
}
#ifdef HAVE_FIPS
wolfCrypt_SetCb_fips(wcFipsCb);
-#if (FIPS_VERSION_GE(5,3))
+#if (FIPS_VERSION_GE(5,2))
wc_SetSeed_Cb(wc_GenerateSeed);
+ PRIVATE_KEY_UNLOCK();
#endif
#endif
+ sqlcipher_log(SQLCIPHER_LOG_INFO, SQLCIPHER_LOG_PROVIDER, "wolfSSL provider initialized\n");
return SQLITE_OK;
}
@@ -208,6 +273,9 @@ static int sqlcipher_wolf_ctx_free(void **ctx) {
wc_FreeRng(&gRng);
gRngInit = 0;
}
+#if (defined(HAVE_FIPS) && FIPS_VERSION_GE(5,2))
+ PRIVATE_KEY_LOCK();
+#endif
wolfCrypt_Cleanup();
return SQLITE_OK;
diff --git a/test/sqlcipher-backup-wolfssl-fips.test b/test/sqlcipher-backup-wolfssl-fips.test
new file mode 100644
index 00000000..9bc29378
--- /dev/null
+++ b/test/sqlcipher-backup-wolfssl-fips.test
@@ -0,0 +1,162 @@
+# 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.
+
+# WOLFSSL NOTE: This test file is a modified version of sqlcipher-backup.test
+# that is used to test the wolfSSL provider with FIPS-enabled wolfSSL. The only
+# difference is that database keys are changed to be FIPS compliant (longer),
+# as the original keys are too short.
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/sqlcipher.tcl
+
+# backup from plaintext to plaintext
+# is allowed
+do_test sqlcipher-backup-plain-plain {
+ sqlite_orig db test.db
+ set rc {}
+ execsql {
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(1, randstr(16384,16384));
+ }
+
+ set md5a [execsql {SELECT md5sum(a,b) FROM t1}]
+ sqlite_orig db2 backup.db
+ sqlite3_backup B db2 main db main
+ lappend rc [B step -1]
+ lappend rc [B finish]
+
+ db close
+ db2 close
+
+ sqlite_orig db backup.db
+
+ set md5b [execsql {SELECT md5sum(a,b) FROM t1}]
+
+ lappend rc [ execsql {
+ PRAGMA integrity_check;
+ } ]
+
+ lappend rc [string equal $md5a $md5b]
+} {SQLITE_DONE SQLITE_OK ok 1}
+db close
+file delete -force test.db
+file delete -force backup.db
+
+# backup from encrypted to encrypted
+# is allowed
+do_test sqlcipher-backup-encrypted-encrypted {
+ sqlite_orig db test.db
+ set rc {}
+ execsql {
+ PRAGMA key = 'FIPSTestingKey123!';
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(1, randstr(16384,16384));
+ }
+ set md5a [execsql {SELECT md5sum(a,b) FROM t1}]
+
+ sqlite_orig db2 backup.db
+ execsql { PRAGMA key = 'FIPSTestingKey123!' } db2;
+
+ sqlite3_backup B db2 main db main
+ lappend rc [B step -1]
+ lappend rc [B finish]
+
+ db close
+ db2 close
+
+ sqlite_orig db backup.db
+ execsql { PRAGMA key = 'FIPSTestingKey123!' };
+
+ set md5b [execsql {SELECT md5sum(a,b) FROM t1}]
+
+ lappend rc [ execsql {
+ PRAGMA integrity_check;
+ PRAGMA cipher_integrity_check;
+ } ]
+
+ lappend rc [string equal $md5a $md5b]
+
+} {SQLITE_DONE SQLITE_OK ok 1}
+db close
+file delete -force test.db
+file delete -force backup.db
+
+# backup from plaintext to encrypted
+# is blocked
+do_test sqlcipher-backup-plain-encrypted {
+ sqlite_orig db test.db
+ set rc {}
+ execsql {
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(1, randstr(16384,16384));
+ }
+
+ sqlite_orig db2 backup.db
+ execsql { PRAGMA key = 'FIPSTestingKey123!' } db2;
+
+ lappend rc [catch {sqlite3_backup B db2 main db main}]
+ lappend rc [sqlite3_errcode db2]
+ lappend rc [sqlite3_errmsg db2]
+} {1 SQLITE_ERROR {backup is not supported with encrypted databases}}
+db close
+db2 close
+file delete -force test.db
+file delete -force backup.db
+
+# backup from encrypted to plaintext
+# is blocked
+do_test sqlcipher-backup-encrypted-plain {
+ sqlite_orig db test.db
+ set rc {}
+ execsql {
+ PRAGMA key = 'FIPSTestingKey123!';
+ CREATE TABLE t1(a,b);
+ INSERT INTO t1 VALUES(1, randstr(16384,16384));
+ }
+
+ sqlite_orig db2 backup.db
+
+ lappend rc [catch {sqlite3_backup B db2 main db main}]
+ lappend rc [sqlite3_errcode db2]
+ lappend rc [sqlite3_errmsg db2]
+} {1 SQLITE_ERROR {backup is not supported with encrypted databases}}
+db close
+db2 close
+file delete -force test.db
+file delete -force backup.db
+
+finish_test
diff --git a/test/sqlcipher-codecerror-wolfssl-fips.test b/test/sqlcipher-codecerror-wolfssl-fips.test
new file mode 100644
index 00000000..37dc022c
--- /dev/null
+++ b/test/sqlcipher-codecerror-wolfssl-fips.test
@@ -0,0 +1,177 @@
+# 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.
+
+# WOLFSSL NOTE: This test file is a modified version of
+# sqlcipher-codecerror.test
+# that is used to test the wolfSSL provider with FIPS-enabled wolfSSL. The only
+# difference is that database keys are changed to be FIPS compliant (longer),
+# as the original keys are too short.
+
+set testdir [file dirname $argv0]
+source $testdir/tester.tcl
+source $testdir/sqlcipher.tcl
+
+proc codec-test-setup {} {
+ sqlite_orig db test.db
+
+ execsql {
+ PRAGMA key = 'wolfsslFIPStestkey';
+ CREATE table t1(a INTEGER PRIMARY KEY,b);
+ BEGIN;
+ }
+
+ for {set i 1} {$i<=10000} {incr i} {
+ execsql "INSERT INTO t1(a,b) VALUES($i,'value $i');"
+ }
+
+ execsql {
+ COMMIT;
+ }
+
+ db close
+}
+
+
+do_test codec-error-journal-delete {
+ codec-test-setup
+
+ sqlite_orig db test.db
+
+ catchsql {
+ PRAGMA key = 'wolfsslFIPStestkey';
+ PRAGMA cipher_test_on = fail_encrypt;
+ UPDATE t1 SET b = 'fail' WHERE a = 5000;
+ }
+
+ db close
+ sqlite_orig db test.db
+
+ execsql {
+ PRAGMA cipher_test_off = fail_encrypt;
+ PRAGMA key = 'wolfsslFIPStestkey';
+ PRAGMA cipher_integrity_check;
+ PRAGMA integrity_check;
+ SELECT b FROM t1 where a = 5000;
+ }
+
+} {ok ok {value 5000}}
+db close
+file delete -force test.db
+
+do_test codec-error-journal-wal {
+ codec-test-setup
+
+ sqlite_orig db test.db
+
+ catchsql {
+ PRAGMA key = 'wolfsslFIPStestkey';
+ PRAGMA cipher_test_on = fail_encrypt;
+ UPDATE t1 SET b = 'fail' WHERE a = 5000;
+ }
+
+ db close
+ sqlite_orig db test.db
+
+ execsql {
+ PRAGMA cipher_test_off = fail_encrypt;
+ PRAGMA key = 'wolfsslFIPStestkey';
+ PRAGMA cipher_integrity_check;
+ PRAGMA integrity_check;
+ SELECT b FROM t1 where a = 5000;
+ }
+
+} {ok ok {value 5000}}
+db close
+file delete -force test.db
+
+do_test codec-error-journal-wal-transaction {
+ codec-test-setup
+
+ sqlite_orig db test.db
+
+ catchsql {
+ PRAGMA key = 'wolfsslFIPStestkey';
+ BEGIN;
+ UPDATE t1 SET b = 'success' WHERE a = 1;
+ PRAGMA cipher_test_on = fail_encrypt;
+ UPDATE t1 SET b = 'fail' WHERE a = 5000;
+ COMMIT;
+ }
+
+ db close
+ sqlite_orig db test.db
+
+ execsql {
+ PRAGMA cipher_test_off = fail_encrypt;
+ PRAGMA key = 'wolfsslFIPStestkey';
+ PRAGMA cipher_integrity_check;
+ PRAGMA integrity_check;
+ SELECT b FROM t1 where a = 1;
+ SELECT b FROM t1 where a = 5000;
+ }
+
+} {ok ok {value 1} {value 5000}}
+db close
+file delete -force test.db
+
+do_test codec-error-journal-wal-read {
+ codec-test-setup
+
+ sqlite_orig db test.db
+
+ catchsql {
+ PRAGMA key = 'wolfsslFIPStestkey';
+ SELECT count(*) FROM sqlite_schema;
+ PRAGMA cipher_test_on = fail_decrypt;
+ UPDATE t1 SET b = 'fail' WHERE a = 5000;