forked from mattn/go-sqlite3
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsqlite3-binding.c.diff
2675 lines (2668 loc) · 97.9 KB
/
sqlite3-binding.c.diff
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
diff --git a/sqlite3-binding.c b/sqlite3-binding.c
index a4e32d1..7c35ee9 100644
--- a/sqlite3-binding.c
+++ b/sqlite3-binding.c
@@ -58311,6 +58311,38 @@ SQLITE_PRIVATE int sqlite3PagerWalFramesize(Pager *pPager){
#endif /* SQLITE_OMIT_DISKIO */
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+void sqlite3pager_get_codec(Pager *pPager, void **ctx) {
+ *ctx = pPager->pCodec;
+}
+
+int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno) {
+ return (PAGER_MJ_PGNO(pPager) == pgno) ? 1 : 0;
+}
+
+sqlite3_file *sqlite3Pager_get_fd(Pager *pPager) {
+ return (isOpen(pPager->fd)) ? pPager->fd : NULL;
+}
+
+void sqlite3pager_sqlite3PagerSetCodec(
+ Pager *pPager,
+ void *(*xCodec)(void*,void*,Pgno,int),
+ void (*xCodecSizeChng)(void*,int,int),
+ void (*xCodecFree)(void*),
+ void *pCodec
+){
+ sqlite3PagerSetCodec(pPager, xCodec, xCodecSizeChng, xCodecFree, pCodec);
+}
+
+void sqlite3pager_sqlite3PagerSetError( Pager *pPager, int error) {
+ pPager->errCode = error;
+ setGetterMethod(pPager);
+}
+
+#endif
+/* END SQLCIPHER */
+
/************** End of pager.c ***********************************************/
/************** Begin file wal.c *********************************************/
/*
@@ -105977,6 +106009,13 @@ static int resolveAttachExpr(NameContext *pName, Expr *pExpr)
return rc;
}
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
+int sqlite3CodecAttach(sqlite3*, int, const void*, int);
+#endif
+/* END SQLCIPHER */
+
/*
** An SQL user-function registered to do the work of an ATTACH statement. The
** three arguments to the function come directly from an attach statement:
@@ -106120,11 +106159,8 @@ static void attachFunc(
rc = SQLITE_NOMEM_BKPT;
}
-
#ifdef SQLITE_HAS_CODEC
if( rc==SQLITE_OK ){
- extern int sqlite3CodecAttach(sqlite3*, int, const void*, int);
- extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
int nKey;
char *zKey;
int t = sqlite3_value_type(argv[2]);
@@ -121536,6 +121572,11 @@ SQLITE_PRIVATE void sqlite3Pragma(
Db *pDb; /* The specific database being pragmaed */
Vdbe *v = sqlite3GetVdbe(pParse); /* Prepared statement */
const PragmaName *pPragma; /* The pragma */
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+ extern int sqlcipher_codec_pragma(sqlite3*, int, Parse *, const char *, const char *);
+#endif
+/* END SQLCIPHER */
if( v==0 ) return;
sqlite3VdbeRunOnlyOnce(v);
@@ -121606,6 +121647,15 @@ SQLITE_PRIVATE void sqlite3Pragma(
goto pragma_out;
}
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+ if(sqlcipher_codec_pragma(db, iDb, pParse, zLeft, zRight)) {
+ /* sqlcipher_codec_pragma executes internal */
+ goto pragma_out;
+ }
+#endif
+/* END SQLCIPHER */
+
/* Locate the pragma in the lookup table */
pPragma = pragmaLocate(zLeft);
if( pPragma==0 ) goto pragma_out;
@@ -134118,7 +134168,6 @@ SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3RunVacuum(
/* A VACUUM cannot change the pagesize of an encrypted database. */
#ifdef SQLITE_HAS_CODEC
if( db->nextPagesize ){
- extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
int nKey;
char *zKey;
sqlite3CodecGetKey(db, iDb, (void**)&zKey, &nKey);
@@ -223234,3 +223283,2569 @@ int sqlite3_user_delete(
}
#endif /* SQLITE_USER_AUTHENTICATION */
+
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+
+#ifndef OMIT_MEMLOCK
+#if defined(__unix__) || defined(__APPLE__) || defined(_AIX)
+#include <errno.h>
+#include <unistd.h>
+#include <sys/resource.h>
+#include <sys/mman.h>
+#elif defined(_WIN32)
+#include <windows.h>
+#endif
+#endif
+
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifndef CRYPTO_H
+#define CRYPTO_H
+
+#if !defined (SQLCIPHER_CRYPTO_CC) \
+ && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
+ && !defined (SQLCIPHER_CRYPTO_OPENSSL)
+#define SQLCIPHER_CRYPTO_OPENSSL
+#endif
+
+#define FILE_HEADER_SZ 16
+
+#ifndef CIPHER_VERSION
+#ifdef SQLCIPHER_FIPS
+#define CIPHER_VERSION "3.4.2 FIPS"
+#else
+#define CIPHER_VERSION "3.4.2"
+#endif
+#endif
+
+#ifndef CIPHER
+#define CIPHER "aes-256-cbc"
+#endif
+
+#define CIPHER_DECRYPT 0
+#define CIPHER_ENCRYPT 1
+
+#define CIPHER_READ_CTX 0
+#define CIPHER_WRITE_CTX 1
+#define CIPHER_READWRITE_CTX 2
+
+#ifndef PBKDF2_ITER
+#define PBKDF2_ITER 64000
+#endif
+
+/* possible flags for cipher_ctx->flags */
+#define CIPHER_FLAG_HMAC 0x01
+#define CIPHER_FLAG_LE_PGNO 0x02
+#define CIPHER_FLAG_BE_PGNO 0x04
+
+#ifndef DEFAULT_CIPHER_FLAGS
+#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
+#endif
+
+
+/* by default, sqlcipher will use a reduced number of iterations to generate
+ the HMAC key / or transform a raw cipher key
+ */
+#ifndef FAST_PBKDF2_ITER
+#define FAST_PBKDF2_ITER 2
+#endif
+
+/* this if a fixed random array that will be xor'd with the database salt to ensure that the
+ salt passed to the HMAC key derivation function is not the same as that used to derive
+ the encryption key. This can be overridden at compile time but it will make the resulting
+ binary incompatible with the default builds when using HMAC. A future version of SQLcipher
+ will likely allow this to be defined at runtime via pragma */
+#ifndef HMAC_SALT_MASK
+#define HMAC_SALT_MASK 0x3a
+#endif
+
+#ifndef CIPHER_MAX_IV_SZ
+#define CIPHER_MAX_IV_SZ 16
+#endif
+
+#ifndef CIPHER_MAX_KEY_SZ
+#define CIPHER_MAX_KEY_SZ 64
+#endif
+
+#ifdef __ANDROID__
+#include <android/log.h>
+#endif
+
+#ifdef CODEC_DEBUG_MUTEX
+#ifdef __ANDROID__
+#define CODEC_TRACE_MUTEX(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
+#else
+#define CODEC_TRACE_MUTEX(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
+#endif
+#else
+#define CODEC_TRACE_MUTEX(...)
+#endif
+
+#ifdef CODEC_DEBUG
+#ifdef __ANDROID__
+#define CODEC_TRACE(...) {__android_log_print(ANDROID_LOG_DEBUG, "sqlcipher", __VA_ARGS__);}
+#else
+#define CODEC_TRACE(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
+#endif
+#else
+#define CODEC_TRACE(...)
+#endif
+
+#ifdef CODEC_DEBUG_PAGEDATA
+#define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
+ { \
+ int __pctr; \
+ printf(DESC); \
+ for(__pctr=0; __pctr < LEN; __pctr++) { \
+ if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \
+ printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \
+ } \
+ printf("\n"); \
+ fflush(stdout); \
+ }
+#else
+#define CODEC_HEXDUMP(DESC,BUFFER,LEN)
+#endif
+
+/* extensions defined in pager.c */
+void sqlite3pager_get_codec(Pager *pPager, void **ctx);
+int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno);
+sqlite3_file *sqlite3Pager_get_fd(Pager *pPager);
+void sqlite3pager_sqlite3PagerSetCodec(
+ Pager *pPager,
+ void *(*xCodec)(void*,void*,Pgno,int),
+ void (*xCodecSizeChng)(void*,int,int),
+ void (*xCodecFree)(void*),
+ void *pCodec
+);
+void sqlite3pager_sqlite3PagerSetError(Pager *pPager, int error);
+/* end extensions defined in pager.c */
+
+/*
+** Simple shared routines for converting hex char strings to binary data
+ */
+static int cipher_hex2int(char c) {
+ return (c>='0' && c<='9') ? (c)-'0' :
+ (c>='A' && c<='F') ? (c)-'A'+10 :
+ (c>='a' && c<='f') ? (c)-'a'+10 : 0;
+}
+
+static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){
+ int i;
+ for(i = 0; i < sz; i += 2){
+ out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]);
+ }
+}
+
+static void cipher_bin2hex(const unsigned char* in, int sz, char *out) {
+ int i;
+ for(i=0; i < sz; i++) {
+ sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]);
+ }
+}
+
+static int cipher_isHex(const unsigned char *hex, int sz){
+ int i;
+ for(i = 0; i < sz; i++) {
+ unsigned char c = hex[i];
+ if ((c < '0' || c > '9') &&
+ (c < 'A' || c > 'F') &&
+ (c < 'a' || c > 'f')) {
+ return 0;
+ }
+ }
+ return 1;
+}
+
+/* extensions defined in crypto_impl.c */
+typedef struct codec_ctx codec_ctx;
+
+/* activation and initialization */
+void sqlcipher_activate();
+void sqlcipher_deactivate();
+int sqlcipher_codec_ctx_init(codec_ctx **, Db *, Pager *, sqlite3_file *, const void *, int);
+void sqlcipher_codec_ctx_free(codec_ctx **);
+int sqlcipher_codec_key_derive(codec_ctx *);
+int sqlcipher_codec_key_copy(codec_ctx *, int);
+
+/* page cipher implementation */
+int sqlcipher_page_cipher(codec_ctx *, int, Pgno, int, int, unsigned char *, unsigned char *);
+
+/* context setters & getters */
+void sqlcipher_codec_ctx_set_error(codec_ctx *, int);
+
+int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int);
+void sqlcipher_codec_get_keyspec(codec_ctx *, void **zKey, int *nKey);
+
+int sqlcipher_codec_ctx_set_pagesize(codec_ctx *, int);
+int sqlcipher_codec_ctx_get_pagesize(codec_ctx *);
+int sqlcipher_codec_ctx_get_reservesize(codec_ctx *);
+
+void sqlcipher_set_default_pagesize(int page_size);
+int sqlcipher_get_default_pagesize();
+
+void sqlcipher_set_default_kdf_iter(int iter);
+int sqlcipher_get_default_kdf_iter();
+
+int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
+int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
+
+void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
+
+int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int);
+int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *, int);
+
+int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int);
+const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
+
+void* sqlcipher_codec_ctx_get_data(codec_ctx *);
+
+void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
+
+void sqlcipher_set_default_use_hmac(int use);
+int sqlcipher_get_default_use_hmac();
+
+void sqlcipher_set_hmac_salt_mask(unsigned char mask);
+unsigned char sqlcipher_get_hmac_salt_mask();
+
+int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
+int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx);
+
+int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
+int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
+int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx);
+
+const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
+int sqlcipher_codec_ctx_migrate(codec_ctx *ctx);
+int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz);
+int sqlcipher_cipher_profile(sqlite3 *db, const char *destination);
+int sqlcipher_codec_get_store_pass(codec_ctx *ctx);
+void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
+void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
+int sqlcipher_codec_fips_status(codec_ctx *ctx);
+const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx);
+int sqlcipher_codec_hmac(const codec_ctx *ctx, const unsigned char *hmac_key, int key_sz,
+ unsigned char* in, int in_sz, unsigned char *in2, int in2_sz,
+ unsigned char *out);
+#endif
+#endif
+/* END SQLCIPHER */
+
+/* BEGIN SQLCIPHER */
+#ifdef SQLITE_HAS_CODEC
+#ifndef SQLCIPHER_H
+#define SQLCIPHER_H
+
+
+typedef struct {
+ int (*activate)(void *ctx);
+ int (*deactivate)(void *ctx);
+ const char* (*get_provider_name)(void *ctx);
+ int (*add_random)(void *ctx, void *buffer, int length);
+ int (*random)(void *ctx, void *buffer, int length);
+ int (*hmac)(void *ctx, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out);
+ int (*kdf)(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key);
+ int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out);
+ int (*set_cipher)(void *ctx, const char *cipher_name);
+ const char* (*get_cipher)(void *ctx);
+ int (*get_key_sz)(void *ctx);
+ int (*get_iv_sz)(void *ctx);
+ int (*get_block_sz)(void *ctx);
+ int (*get_hmac_sz)(void *ctx);
+ int (*ctx_copy)(void *target_ctx, void *source_ctx);
+ int (*ctx_cmp)(void *c1, void *c2);
+ int (*ctx_init)(void **ctx);
+ int (*ctx_free)(void **ctx);
+ int (*fips_status)(void *ctx);
+ const char* (*get_provider_version)(void *ctx);
+} sqlcipher_provider;
+
+/* utility functions */
+void sqlcipher_free(void *ptr, int sz);
+void* sqlcipher_malloc(int sz);
+void* sqlcipher_memset(void *v, unsigned char value, int len);
+int sqlcipher_ismemset(const void *v, unsigned char value, int len);
+int sqlcipher_memcmp(const void *v0, const void *v1, int len);
+void sqlcipher_free(void *, int);
+
+/* provider interfaces */
+int sqlcipher_register_provider(sqlcipher_provider *p);
+sqlcipher_provider* sqlcipher_get_provider();
+
+#endif
+#endif
+/* END SQLCIPHER */
+
+/* the default implementation of SQLCipher uses a cipher_ctx
+ to keep track of read / write state separately. The following
+ struct and associated functions are defined here */
+typedef struct {
+ int store_pass;
+ int derive_key;
+ int kdf_iter;
+ int fast_kdf_iter;
+ int key_sz;
+ int iv_sz;
+ int block_sz;
+ int pass_sz;
+ int reserve_sz;
+ int hmac_sz;
+ int keyspec_sz;
+ unsigned int flags;
+ unsigned char *key;
+ unsigned char *hmac_key;
+ unsigned char *pass;
+ char *keyspec;
+ sqlcipher_provider *provider;
+ void *provider_ctx;
+} cipher_ctx;
+
+static unsigned int default_flags = DEFAULT_CIPHER_FLAGS;
+static unsigned char hmac_salt_mask = HMAC_SALT_MASK;
+static int default_kdf_iter = PBKDF2_ITER;
+static int default_page_size = 1024;
+static unsigned int sqlcipher_activate_count = 0;
+static sqlite3_mutex* sqlcipher_provider_mutex = NULL;
+static sqlcipher_provider *default_provider = NULL;
+
+struct codec_ctx {
+ int kdf_salt_sz;
+ int page_sz;
+ unsigned char *kdf_salt;
+ unsigned char *hmac_kdf_salt;
+ unsigned char *buffer;
+ Btree *pBt;
+ cipher_ctx *read_ctx;
+ cipher_ctx *write_ctx;
+ unsigned int skip_read_hmac;
+ unsigned int need_kdf_salt;
+};
+
+int sqlcipher_register_provider(sqlcipher_provider *p) {
+ CODEC_TRACE_MUTEX("sqlcipher_register_provider: entering sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_register_provider: entered sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ if(default_provider != NULL && default_provider != p) {
+ /* only free the current registerd provider if it has been initialized
+ and it isn't a pointer to the same provider passed to the function
+ (i.e. protect against a caller calling register twice for the same provider) */
+ sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
+ }
+ default_provider = p;
+ CODEC_TRACE_MUTEX("sqlcipher_register_provider: leaving sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_register_provider: left sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ return SQLITE_OK;
+}
+
+/* return a pointer to the currently registered provider. This will
+ allow an application to fetch the current registered provider and
+ make minor changes to it */
+sqlcipher_provider* sqlcipher_get_provider() {
+ return default_provider;
+}
+
+void sqlcipher_activate() {
+ CODEC_TRACE_MUTEX("sqlcipher_activate: entering static master mutex\n");
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ CODEC_TRACE_MUTEX("sqlcipher_activate: entered static master mutex\n");
+
+ if(sqlcipher_provider_mutex == NULL) {
+ /* allocate a new mutex to guard access to the provider */
+ CODEC_TRACE_MUTEX("sqlcipher_activate: allocating sqlcipher provider mutex\n");
+ sqlcipher_provider_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
+ CODEC_TRACE_MUTEX("sqlcipher_activate: allocated sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ }
+
+ /* check to see if there is a provider registered at this point
+ if there no provider registered at this point, register the
+ default provider */
+ if(sqlcipher_get_provider() == NULL) {
+ sqlcipher_provider *p = sqlcipher_malloc(sizeof(sqlcipher_provider));
+#if defined (SQLCIPHER_CRYPTO_CC)
+ extern int sqlcipher_cc_setup(sqlcipher_provider *p);
+ sqlcipher_cc_setup(p);
+#elif defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT)
+ extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
+ sqlcipher_ltc_setup(p);
+#elif defined (SQLCIPHER_CRYPTO_OPENSSL)
+ extern int sqlcipher_openssl_setup(sqlcipher_provider *p);
+ sqlcipher_openssl_setup(p);
+#else
+#error "NO DEFAULT SQLCIPHER CRYPTO PROVIDER DEFINED"
+#endif
+ CODEC_TRACE("sqlcipher_activate: calling sqlcipher_register_provider(%p)\n", p);
+ sqlcipher_register_provider(p);
+ CODEC_TRACE("sqlcipher_activate: called sqlcipher_register_provider(%p)\n",p);
+ }
+
+ sqlcipher_activate_count++; /* increment activation count */
+
+ CODEC_TRACE_MUTEX("sqlcipher_activate: leaving static master mutex\n");
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ CODEC_TRACE_MUTEX("sqlcipher_activate: left static master mutex\n");
+}
+
+void sqlcipher_deactivate() {
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: entering static master mutex\n");
+ sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: entered static master mutex\n");
+
+ sqlcipher_activate_count--;
+ /* if no connections are using sqlcipher, cleanup globals */
+ if(sqlcipher_activate_count < 1) {
+ int rc;
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: entering sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: entered sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ if(default_provider != NULL) {
+ sqlcipher_free(default_provider, sizeof(sqlcipher_provider));
+ default_provider = NULL;
+ }
+
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: leaving sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: left sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ /* last connection closed, free provider mutex*/
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: freeing sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_free(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: freed sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ sqlcipher_provider_mutex = NULL;
+
+ sqlcipher_activate_count = 0; /* reset activation count */
+ }
+
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: leaving static master mutex\n");
+ sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
+ CODEC_TRACE_MUTEX("sqlcipher_deactivate: left static master mutex\n");
+}
+
+/* constant time memset using volitile to avoid having the memset
+ optimized out by the compiler.
+ Note: As suggested by Joachim Schipper ([email protected])
+*/
+void* sqlcipher_memset(void *v, unsigned char value, int len) {
+ int i = 0;
+ volatile unsigned char *a = v;
+
+ if (v == NULL) return v;
+
+ CODEC_TRACE("sqlcipher_memset: setting %p[0-%d]=%d)\n", a, len, value);
+ for(i = 0; i < len; i++) {
+ a[i] = value;
+ }
+
+ return v;
+}
+
+/* constant time memory check tests every position of a memory segement
+ matches a single value (i.e. the memory is all zeros)
+ returns 0 if match, 1 of no match */
+int sqlcipher_ismemset(const void *v, unsigned char value, int len) {
+ const unsigned char *a = v;
+ int i = 0, result = 0;
+
+ for(i = 0; i < len; i++) {
+ result |= a[i] ^ value;
+ }
+
+ return (result != 0);
+}
+
+/* constant time memory comparison routine.
+ returns 0 if match, 1 if no match */
+int sqlcipher_memcmp(const void *v0, const void *v1, int len) {
+ const unsigned char *a0 = v0, *a1 = v1;
+ int i = 0, result = 0;
+
+ for(i = 0; i < len; i++) {
+ result |= a0[i] ^ a1[i];
+ }
+
+ return (result != 0);
+}
+
+/**
+ * Free and wipe memory. Uses SQLites internal sqlite3_free so that memory
+ * can be countend and memory leak detection works in the test suite.
+ * If ptr is not null memory will be freed.
+ * If sz is greater than zero, the memory will be overwritten with zero before it is freed
+ * If sz is > 0, and not compiled with OMIT_MEMLOCK, system will attempt to unlock the
+ * memory segment so it can be paged
+ */
+void sqlcipher_free(void *ptr, int sz) {
+ if(ptr) {
+ if(sz > 0) {
+#ifndef OMIT_MEMLOCK
+ int rc;
+#if defined(__unix__) || defined(__APPLE__)
+ unsigned long pagesize = sysconf(_SC_PAGESIZE);
+ unsigned long offset = (unsigned long) ptr % pagesize;
+#endif
+#endif
+ CODEC_TRACE("sqlcipher_free: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
+ sqlcipher_memset(ptr, 0, sz);
+#ifndef OMIT_MEMLOCK
+#if defined(__unix__) || defined(__APPLE__)
+ CODEC_TRACE("sqlcipher_free: calling munlock(%p,%lu)\n", ptr - offset, sz + offset);
+ rc = munlock(ptr - offset, sz + offset);
+ if(rc!=0) {
+ CODEC_TRACE("sqlcipher_free: munlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
+ }
+#elif defined(_WIN32)
+#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
+ rc = VirtualUnlock(ptr, sz);
+ if(!rc) {
+ CODEC_TRACE("sqlcipher_free: VirtualUnlock(%p,%d) returned %d LastError=%d\n", ptr, sz, rc, GetLastError());
+ }
+#endif
+#endif
+#endif
+ }
+ sqlite3_free(ptr);
+ }
+}
+
+/**
+ * allocate memory. Uses sqlite's internall malloc wrapper so memory can be
+ * reference counted and leak detection works. Unless compiled with OMIT_MEMLOCK
+ * attempts to lock the memory pages so sensitive information won't be swapped
+ */
+void* sqlcipher_malloc(int sz) {
+ void *ptr;
+ CODEC_TRACE("sqlcipher_malloc: calling sqlite3Malloc(%d)\n", sz);
+ ptr = sqlite3Malloc(sz);
+ CODEC_TRACE("sqlcipher_malloc: calling sqlcipher_memset(%p,0,%d)\n", ptr, sz);
+ sqlcipher_memset(ptr, 0, sz);
+#ifndef OMIT_MEMLOCK
+ if(ptr) {
+ int rc;
+#if defined(__unix__) || defined(__APPLE__)
+ unsigned long pagesize = sysconf(_SC_PAGESIZE);
+ unsigned long offset = (unsigned long) ptr % pagesize;
+ CODEC_TRACE("sqlcipher_malloc: calling mlock(%p,%lu); _SC_PAGESIZE=%lu\n", ptr - offset, sz + offset, pagesize);
+ rc = mlock(ptr - offset, sz + offset);
+ if(rc!=0) {
+ CODEC_TRACE("sqlcipher_malloc: mlock(%p,%lu) returned %d errno=%d\n", ptr - offset, sz + offset, rc, errno);
+ }
+#elif defined(_WIN32)
+#if !(defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP || WINAPI_FAMILY == WINAPI_FAMILY_APP))
+ rc = VirtualLock(ptr, sz);
+ if(rc==0) {
+ CODEC_TRACE("sqlcipher_malloc: VirtualLock(%p,%d) returned %d LastError=%d\n", ptr, sz, rc, GetLastError());
+ }
+#endif
+#endif
+ }
+#endif
+ return ptr;
+}
+
+
+/**
+ * Initialize new cipher_ctx struct. This function will allocate memory
+ * for the cipher context and for the key
+ *
+ * returns SQLITE_OK if initialization was successful
+ * returns SQLITE_NOMEM if an error occured allocating memory
+ */
+static int sqlcipher_cipher_ctx_init(cipher_ctx **iCtx) {
+ int rc;
+ cipher_ctx *ctx;
+ CODEC_TRACE("sqlcipher_cipher_ctx_init: allocating context\n");
+ *iCtx = (cipher_ctx *) sqlcipher_malloc(sizeof(cipher_ctx));
+ ctx = *iCtx;
+ if(ctx == NULL) return SQLITE_NOMEM;
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_init: allocating provider\n");
+ ctx->provider = (sqlcipher_provider *) sqlcipher_malloc(sizeof(sqlcipher_provider));
+ if(ctx->provider == NULL) return SQLITE_NOMEM;
+
+ /* make a copy of the provider to be used for the duration of the context */
+ CODEC_TRACE_MUTEX("sqlcipher_cipher_ctx_init: entering sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_enter(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_cipher_ctx_init: entered sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ memcpy(ctx->provider, default_provider, sizeof(sqlcipher_provider));
+
+ CODEC_TRACE_MUTEX("sqlcipher_cipher_ctx_init: leaving sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+ sqlite3_mutex_leave(sqlcipher_provider_mutex);
+ CODEC_TRACE_MUTEX("sqlcipher_cipher_ctx_init: left sqlcipher provider mutex %p\n", sqlcipher_provider_mutex);
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_init: calling provider ctx_init\n");
+ if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) return rc;
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_init: allocating key\n");
+ ctx->key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ);
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_init: allocating hmac_key\n");
+ ctx->hmac_key = (unsigned char *) sqlcipher_malloc(CIPHER_MAX_KEY_SZ);
+
+ if(ctx->key == NULL) return SQLITE_NOMEM;
+ if(ctx->hmac_key == NULL) return SQLITE_NOMEM;
+
+ /* setup default flags */
+ ctx->flags = default_flags;
+
+ return SQLITE_OK;
+}
+
+/**
+ * Free and wipe memory associated with a cipher_ctx
+ */
+static void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) {
+ cipher_ctx *ctx = *iCtx;
+ CODEC_TRACE("cipher_ctx_free: entered iCtx=%p\n", iCtx);
+ ctx->provider->ctx_free(&ctx->provider_ctx);
+ sqlcipher_free(ctx->provider, sizeof(sqlcipher_provider));
+ sqlcipher_free(ctx->key, ctx->key_sz);
+ sqlcipher_free(ctx->hmac_key, ctx->key_sz);
+ sqlcipher_free(ctx->pass, ctx->pass_sz);
+ sqlcipher_free(ctx->keyspec, ctx->keyspec_sz);
+ sqlcipher_free(ctx, sizeof(cipher_ctx));
+}
+
+/**
+ * Compare one cipher_ctx to another.
+ *
+ * returns 0 if all the parameters (except the derived key data) are the same
+ * returns 1 otherwise
+ */
+static int sqlcipher_cipher_ctx_cmp(cipher_ctx *c1, cipher_ctx *c2) {
+ int are_equal = (
+ c1->iv_sz == c2->iv_sz
+ && c1->kdf_iter == c2->kdf_iter
+ && c1->fast_kdf_iter == c2->fast_kdf_iter
+ && c1->key_sz == c2->key_sz
+ && c1->pass_sz == c2->pass_sz
+ && c1->flags == c2->flags
+ && c1->hmac_sz == c2->hmac_sz
+ && c1->provider->ctx_cmp(c1->provider_ctx, c2->provider_ctx)
+ && (
+ c1->pass == c2->pass
+ || !sqlcipher_memcmp((const unsigned char*)c1->pass,
+ (const unsigned char*)c2->pass,
+ c1->pass_sz)
+ ));
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_cmp: entered \
+ c1=%p c2=%p \
+ c1->iv_sz=%d c2->iv_sz=%d \
+ c1->kdf_iter=%d c2->kdf_iter=%d \
+ c1->fast_kdf_iter=%d c2->fast_kdf_iter=%d \
+ c1->key_sz=%d c2->key_sz=%d \
+ c1->pass_sz=%d c2->pass_sz=%d \
+ c1->flags=%d c2->flags=%d \
+ c1->hmac_sz=%d c2->hmac_sz=%d \
+ c1->provider_ctx=%p c2->provider_ctx=%p \
+ c1->pass=%p c2->pass=%p \
+ c1->pass=%s c2->pass=%s \
+ provider->ctx_cmp=%d \
+ sqlcipher_memcmp=%d \
+ are_equal=%d \
+ \n",
+ c1, c2,
+ c1->iv_sz, c2->iv_sz,
+ c1->kdf_iter, c2->kdf_iter,
+ c1->fast_kdf_iter, c2->fast_kdf_iter,
+ c1->key_sz, c2->key_sz,
+ c1->pass_sz, c2->pass_sz,
+ c1->flags, c2->flags,
+ c1->hmac_sz, c2->hmac_sz,
+ c1->provider_ctx, c2->provider_ctx,
+ c1->pass, c2->pass,
+ c1->pass, c2->pass,
+ c1->provider->ctx_cmp(c1->provider_ctx, c2->provider_ctx),
+ (c1->pass == NULL || c2->pass == NULL)
+ ? -1 : sqlcipher_memcmp(
+ (const unsigned char*)c1->pass,
+ (const unsigned char*)c2->pass,
+ c1->pass_sz),
+ are_equal
+ );
+
+ return !are_equal; /* return 0 if they are the same, 1 otherwise */
+}
+
+/**
+ * Copy one cipher_ctx to another. For instance, assuming that read_ctx is a
+ * fully initialized context, you could copy it to write_ctx and all yet data
+ * and pass information across
+ *
+ * returns SQLITE_OK if initialization was successful
+ * returns SQLITE_NOMEM if an error occured allocating memory
+ */
+static int sqlcipher_cipher_ctx_copy(cipher_ctx *target, cipher_ctx *source) {
+ void *key = target->key;
+ void *hmac_key = target->hmac_key;
+ void *provider = target->provider;
+ void *provider_ctx = target->provider_ctx;
+
+ CODEC_TRACE("sqlcipher_cipher_ctx_copy: entered target=%p, source=%p\n", target, source);
+ sqlcipher_free(target->pass, target->pass_sz);
+ sqlcipher_free(target->keyspec, target->keyspec_sz);
+ memcpy(target, source, sizeof(cipher_ctx));
+
+ target->key = key; //restore pointer to previously allocated key data
+ memcpy(target->key, source->key, CIPHER_MAX_KEY_SZ);
+
+ target->hmac_key = hmac_key; //restore pointer to previously allocated hmac key data
+ memcpy(target->hmac_key, source->hmac_key, CIPHER_MAX_KEY_SZ);
+
+ target->provider = provider; // restore pointer to previouly allocated provider;
+ memcpy(target->provider, source->provider, sizeof(sqlcipher_provider));
+
+ target->provider_ctx = provider_ctx; // restore pointer to previouly allocated provider context;
+ target->provider->ctx_copy(target->provider_ctx, source->provider_ctx);
+
+ if(source->pass && source->pass_sz) {
+ target->pass = sqlcipher_malloc(source->pass_sz);
+ if(target->pass == NULL) return SQLITE_NOMEM;
+ memcpy(target->pass, source->pass, source->pass_sz);
+ }
+ if(source->keyspec && source->keyspec_sz) {
+ target->keyspec = sqlcipher_malloc(source->keyspec_sz);
+ if(target->keyspec == NULL) return SQLITE_NOMEM;
+ memcpy(target->keyspec, source->keyspec, source->keyspec_sz);
+ }
+ return SQLITE_OK;
+}
+
+/**
+ * Set the keyspec for the cipher_ctx
+ *
+ * returns SQLITE_OK if assignment was successfull
+ * returns SQLITE_NOMEM if an error occured allocating memory
+ */
+static int sqlcipher_cipher_ctx_set_keyspec(cipher_ctx *ctx, const unsigned char *key, int key_sz, const unsigned char *salt, int salt_sz) {
+
+ /* free, zero existing pointers and size */
+ sqlcipher_free(ctx->keyspec, ctx->keyspec_sz);
+ ctx->keyspec = NULL;
+ ctx->keyspec_sz = 0;
+
+ /* establic a hex-formated key specification, containing the raw encryption key and
+ the salt used to generate it */
+ ctx->keyspec_sz = ((key_sz + salt_sz) * 2) + 3;
+ ctx->keyspec = sqlcipher_malloc(ctx->keyspec_sz);
+ if(ctx->keyspec == NULL) return SQLITE_NOMEM;
+
+ ctx->keyspec[0] = 'x';
+ ctx->keyspec[1] = '\'';
+ cipher_bin2hex(key, key_sz, ctx->keyspec + 2);
+ cipher_bin2hex(salt, salt_sz, ctx->keyspec + (key_sz * 2) + 2);
+ ctx->keyspec[ctx->keyspec_sz - 1] = '\'';
+ return SQLITE_OK;
+}
+
+int sqlcipher_codec_get_store_pass(codec_ctx *ctx) {
+ return ctx->read_ctx->store_pass;
+}
+
+void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value) {
+ ctx->read_ctx->store_pass = value;
+}
+
+void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey) {
+ *zKey = ctx->read_ctx->pass;
+ *nKey = ctx->read_ctx->pass_sz;
+}
+
+/**
+ * Set the passphrase for the cipher_ctx
+ *
+ * returns SQLITE_OK if assignment was successfull
+ * returns SQLITE_NOMEM if an error occured allocating memory
+ */
+static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
+
+ /* free, zero existing pointers and size */
+ sqlcipher_free(ctx->pass, ctx->pass_sz);
+ ctx->pass = NULL;
+ ctx->pass_sz = 0;
+
+ if(zKey && nKey) { /* if new password is provided, copy it */
+ ctx->pass_sz = nKey;
+ ctx->pass = sqlcipher_malloc(nKey);
+ if(ctx->pass == NULL) return SQLITE_NOMEM;
+ memcpy(ctx->pass, zKey, nKey);
+ }
+ return SQLITE_OK;
+}
+
+int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ int rc;
+
+ if((rc = sqlcipher_cipher_ctx_set_pass(c_ctx, zKey, nKey)) != SQLITE_OK) return rc;
+ c_ctx->derive_key = 1;
+
+ if(for_ctx == 2)
+ if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK)
+ return rc;
+
+ return SQLITE_OK;
+}
+
+int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ int rc;
+
+ rc = c_ctx->provider->set_cipher(c_ctx->provider_ctx, cipher_name);
+ if(rc != SQLITE_OK){
+ sqlcipher_codec_ctx_set_error(ctx, rc);
+ return rc;
+ }
+ c_ctx->key_sz = c_ctx->provider->get_key_sz(c_ctx->provider_ctx);
+ c_ctx->iv_sz = c_ctx->provider->get_iv_sz(c_ctx->provider_ctx);
+ c_ctx->block_sz = c_ctx->provider->get_block_sz(c_ctx->provider_ctx);
+ c_ctx->hmac_sz = c_ctx->provider->get_hmac_sz(c_ctx->provider_ctx);
+ c_ctx->derive_key = 1;
+
+ if(for_ctx == 2)
+ if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK)
+ return rc;
+
+ return SQLITE_OK;
+}
+
+const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ return c_ctx->provider->get_cipher(c_ctx->provider_ctx);
+}
+
+/* set the global default KDF iteration */
+void sqlcipher_set_default_kdf_iter(int iter) {
+ default_kdf_iter = iter;
+}
+
+int sqlcipher_get_default_kdf_iter() {
+ return default_kdf_iter;
+}
+
+int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *ctx, int kdf_iter, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ int rc;
+
+ c_ctx->kdf_iter = kdf_iter;
+ c_ctx->derive_key = 1;
+
+ if(for_ctx == 2)
+ if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK)
+ return rc;
+
+ return SQLITE_OK;
+}
+
+int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ return c_ctx->kdf_iter;
+}
+
+int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *ctx, int fast_kdf_iter, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ int rc;
+
+ c_ctx->fast_kdf_iter = fast_kdf_iter;
+ c_ctx->derive_key = 1;
+
+ if(for_ctx == 2)
+ if((rc = sqlcipher_cipher_ctx_copy( for_ctx ? ctx->read_ctx : ctx->write_ctx, c_ctx)) != SQLITE_OK)
+ return rc;
+
+ return SQLITE_OK;
+}
+
+int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *ctx, int for_ctx) {
+ cipher_ctx *c_ctx = for_ctx ? ctx->write_ctx : ctx->read_ctx;
+ return c_ctx->fast_kdf_iter;
+}
+
+/* set the global default flag for HMAC */
+void sqlcipher_set_default_use_hmac(int use) {
+ if(use) default_flags |= CIPHER_FLAG_HMAC;
+ else default_flags &= ~CIPHER_FLAG_HMAC;
+}
+
+int sqlcipher_get_default_use_hmac() {