Add contributions section to README
[sqlcipher.git] / src / crypto.h
bloba614ede5a47cc829915ef8ddc85baeebb70f9f51
1 /*
2 ** SQLCipher
3 ** crypto.h developed by Stephen Lombardo (Zetetic LLC)
4 ** sjlombardo at zetetic dot net
5 ** http://zetetic.net
6 **
7 ** Copyright (c) 2008, ZETETIC LLC
8 ** All rights reserved.
9 **
10 ** Redistribution and use in source and binary forms, with or without
11 ** modification, are permitted provided that the following conditions are met:
12 ** * Redistributions of source code must retain the above copyright
13 ** notice, this list of conditions and the following disclaimer.
14 ** * Redistributions in binary form must reproduce the above copyright
15 ** notice, this list of conditions and the following disclaimer in the
16 ** documentation and/or other materials provided with the distribution.
17 ** * Neither the name of the ZETETIC LLC nor the
18 ** names of its contributors may be used to endorse or promote products
19 ** derived from this software without specific prior written permission.
20 **
21 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
22 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
25 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 **
33 /* BEGIN SQLCIPHER */
34 #ifdef SQLITE_HAS_CODEC
35 #ifndef CRYPTO_H
36 #define CRYPTO_H
38 #if !defined (SQLCIPHER_CRYPTO_CC) \
39 && !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
40 && !defined (SQLCIPHER_CRYPTO_OPENSSL)
41 #define SQLCIPHER_CRYPTO_OPENSSL
42 #endif
44 #define FILE_HEADER_SZ 16
46 #ifndef CIPHER_VERSION
47 #ifdef SQLCIPHER_FIPS
48 #define CIPHER_VERSION "3.3.0 FIPS"
49 #else
50 #define CIPHER_VERSION "3.3.0"
51 #endif
52 #endif
54 #ifndef CIPHER
55 #define CIPHER "aes-256-cbc"
56 #endif
58 #define CIPHER_DECRYPT 0
59 #define CIPHER_ENCRYPT 1
61 #define CIPHER_READ_CTX 0
62 #define CIPHER_WRITE_CTX 1
63 #define CIPHER_READWRITE_CTX 2
65 #ifndef PBKDF2_ITER
66 #define PBKDF2_ITER 64000
67 #endif
69 /* possible flags for cipher_ctx->flags */
70 #define CIPHER_FLAG_HMAC 0x01
71 #define CIPHER_FLAG_LE_PGNO 0x02
72 #define CIPHER_FLAG_BE_PGNO 0x04
74 #ifndef DEFAULT_CIPHER_FLAGS
75 #define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
76 #endif
79 /* by default, sqlcipher will use a reduced number of iterations to generate
80 the HMAC key / or transform a raw cipher key
82 #ifndef FAST_PBKDF2_ITER
83 #define FAST_PBKDF2_ITER 2
84 #endif
86 /* this if a fixed random array that will be xor'd with the database salt to ensure that the
87 salt passed to the HMAC key derivation function is not the same as that used to derive
88 the encryption key. This can be overridden at compile time but it will make the resulting
89 binary incompatible with the default builds when using HMAC. A future version of SQLcipher
90 will likely allow this to be defined at runtime via pragma */
91 #ifndef HMAC_SALT_MASK
92 #define HMAC_SALT_MASK 0x3a
93 #endif
95 #ifndef CIPHER_MAX_IV_SZ
96 #define CIPHER_MAX_IV_SZ 16
97 #endif
99 #ifndef CIPHER_MAX_KEY_SZ
100 #define CIPHER_MAX_KEY_SZ 64
101 #endif
104 #ifdef CODEC_DEBUG
105 #define CODEC_TRACE(X) {printf X;fflush(stdout);}
106 #else
107 #define CODEC_TRACE(X)
108 #endif
110 #ifdef CODEC_DEBUG_PAGEDATA
111 #define CODEC_HEXDUMP(DESC,BUFFER,LEN) \
113 int __pctr; \
114 printf(DESC); \
115 for(__pctr=0; __pctr < LEN; __pctr++) { \
116 if(__pctr % 16 == 0) printf("\n%05x: ",__pctr); \
117 printf("%02x ",((unsigned char*) BUFFER)[__pctr]); \
119 printf("\n"); \
120 fflush(stdout); \
122 #else
123 #define CODEC_HEXDUMP(DESC,BUFFER,LEN)
124 #endif
126 /* extensions defined in pager.c */
127 void sqlite3pager_get_codec(Pager *pPager, void **ctx);
128 int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno);
129 sqlite3_file *sqlite3Pager_get_fd(Pager *pPager);
130 void sqlite3pager_sqlite3PagerSetCodec(
131 Pager *pPager,
132 void *(*xCodec)(void*,void*,Pgno,int),
133 void (*xCodecSizeChng)(void*,int,int),
134 void (*xCodecFree)(void*),
135 void *pCodec
137 void sqlite3pager_sqlite3PagerSetError(Pager *pPager, int error);
138 /* end extensions defined in pager.c */
141 ** Simple shared routines for converting hex char strings to binary data
143 static int cipher_hex2int(char c) {
144 return (c>='0' && c<='9') ? (c)-'0' :
145 (c>='A' && c<='F') ? (c)-'A'+10 :
146 (c>='a' && c<='f') ? (c)-'a'+10 : 0;
149 static void cipher_hex2bin(const unsigned char *hex, int sz, unsigned char *out){
150 int i;
151 for(i = 0; i < sz; i += 2){
152 out[i/2] = (cipher_hex2int(hex[i])<<4) | cipher_hex2int(hex[i+1]);
156 static void cipher_bin2hex(const unsigned char* in, int sz, char *out) {
157 int i;
158 for(i=0; i < sz; i++) {
159 sqlite3_snprintf(3, out + (i*2), "%02x ", in[i]);
163 /* extensions defined in crypto_impl.c */
164 typedef struct codec_ctx codec_ctx;
166 /* activation and initialization */
167 void sqlcipher_activate();
168 void sqlcipher_deactivate();
169 int sqlcipher_codec_ctx_init(codec_ctx **, Db *, Pager *, sqlite3_file *, const void *, int);
170 void sqlcipher_codec_ctx_free(codec_ctx **);
171 int sqlcipher_codec_key_derive(codec_ctx *);
172 int sqlcipher_codec_key_copy(codec_ctx *, int);
174 /* page cipher implementation */
175 int sqlcipher_page_cipher(codec_ctx *, int, Pgno, int, int, unsigned char *, unsigned char *);
177 /* context setters & getters */
178 void sqlcipher_codec_ctx_set_error(codec_ctx *, int);
180 int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int);
181 void sqlcipher_codec_get_keyspec(codec_ctx *, void **zKey, int *nKey);
183 int sqlcipher_codec_ctx_set_pagesize(codec_ctx *, int);
184 int sqlcipher_codec_ctx_get_pagesize(codec_ctx *);
185 int sqlcipher_codec_ctx_get_reservesize(codec_ctx *);
187 void sqlcipher_set_default_pagesize(int page_size);
188 int sqlcipher_get_default_pagesize();
190 void sqlcipher_set_default_kdf_iter(int iter);
191 int sqlcipher_get_default_kdf_iter();
193 int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
194 int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
196 void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
198 int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int);
199 int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *, int);
201 int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int);
202 const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
204 void* sqlcipher_codec_ctx_get_data(codec_ctx *);
206 void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
208 void sqlcipher_set_default_use_hmac(int use);
209 int sqlcipher_get_default_use_hmac();
211 void sqlcipher_set_hmac_salt_mask(unsigned char mask);
212 unsigned char sqlcipher_get_hmac_salt_mask();
214 int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
215 int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx);
217 int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
218 int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
219 int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx);
221 const char* sqlcipher_codec_get_cipher_provider(codec_ctx *ctx);
222 int sqlcipher_codec_ctx_migrate(codec_ctx *ctx);
223 int sqlcipher_codec_add_random(codec_ctx *ctx, const char *data, int random_sz);
224 int sqlcipher_cipher_profile(sqlite3 *db, const char *destination);
225 static void sqlcipher_profile_callback(void *file, const char *sql, sqlite3_uint64 run_time);
226 static int sqlcipher_codec_get_store_pass(codec_ctx *ctx);
227 static void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
228 static void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
229 int sqlcipher_codec_fips_status(codec_ctx *ctx);
231 #endif
232 #endif
233 /* END SQLCIPHER */