Add contributions section to README
[sqlcipher.git] / src / crypto_openssl.c
blob66553a0e446a01fa57a83825a5bf1853d78b2c1b
1 /*
2 ** SQLCipher
3 ** http://sqlcipher.net
4 **
5 ** Copyright (c) 2008 - 2013, ZETETIC LLC
6 ** All rights reserved.
7 **
8 ** Redistribution and use in source and binary forms, with or without
9 ** modification, are permitted provided that the following conditions are met:
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in the
14 ** documentation and/or other materials provided with the distribution.
15 ** * Neither the name of the ZETETIC LLC nor the
16 ** names of its contributors may be used to endorse or promote products
17 ** derived from this software without specific prior written permission.
19 ** THIS SOFTWARE IS PROVIDED BY ZETETIC LLC ''AS IS'' AND ANY
20 ** EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 ** DISCLAIMED. IN NO EVENT SHALL ZETETIC LLC BE LIABLE FOR ANY
23 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 /* BEGIN SQLCIPHER */
32 #ifdef SQLITE_HAS_CODEC
33 #ifdef SQLCIPHER_CRYPTO_OPENSSL
34 #include "sqliteInt.h"
35 #include "crypto.h"
36 #include "sqlcipher.h"
37 #include <openssl/rand.h>
38 #include <openssl/evp.h>
39 #include <openssl/hmac.h>
41 typedef struct {
42 EVP_CIPHER *evp_cipher;
43 } openssl_ctx;
45 static unsigned int openssl_external_init = 0;
46 static unsigned int openssl_init_count = 0;
47 static sqlite3_mutex* openssl_rand_mutex = NULL;
49 static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
50 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
51 sqlite3_mutex_enter(openssl_rand_mutex);
52 #endif
53 RAND_add(buffer, length, 0);
54 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
55 sqlite3_mutex_leave(openssl_rand_mutex);
56 #endif
57 return SQLITE_OK;
60 /* activate and initialize sqlcipher. Most importantly, this will automatically
61 intialize OpenSSL's EVP system if it hasn't already be externally. Note that
62 this function may be called multiple times as new codecs are intiialized.
63 Thus it performs some basic counting to ensure that only the last and final
64 sqlcipher_openssl_deactivate() will free the EVP structures.
66 static int sqlcipher_openssl_activate(void *ctx) {
67 /* initialize openssl and increment the internal init counter
68 but only if it hasn't been initalized outside of SQLCipher by this program
69 e.g. on startup */
70 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
72 if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) {
73 /* if openssl has not yet been initialized by this library, but
74 a call to get_cipherbyname works, then the openssl library
75 has been initialized externally already. */
76 openssl_external_init = 1;
79 #ifdef SQLCIPHER_FIPS
80 if(!FIPS_mode()){
81 if(!FIPS_mode_set(1)){
82 ERR_load_crypto_strings();
83 ERR_print_errors_fp(stderr);
86 #endif
88 if(openssl_init_count == 0 && openssl_external_init == 0) {
89 /* if the library was not externally initialized, then should be now */
90 OpenSSL_add_all_algorithms();
93 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
94 if(openssl_rand_mutex == NULL) {
95 /* allocate a mutex to guard against concurrent calls to RAND_bytes() */
96 openssl_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
98 #endif
100 openssl_init_count++;
101 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
102 return SQLITE_OK;
105 /* deactivate SQLCipher, most imporantly decremeting the activation count and
106 freeing the EVP structures on the final deactivation to ensure that
107 OpenSSL memory is cleaned up */
108 static int sqlcipher_openssl_deactivate(void *ctx) {
109 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
110 openssl_init_count--;
112 if(openssl_init_count == 0) {
113 if(openssl_external_init == 0) {
114 /* if OpenSSL hasn't be initialized externally, and the counter reaches zero
115 after it's decremented, release EVP memory
116 Note: this code will only be reached if OpensSSL_add_all_algorithms()
117 is called by SQLCipher internally. This should prevent SQLCipher from
118 "cleaning up" openssl when it was initialized externally by the program */
119 EVP_cleanup();
121 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
122 sqlite3_mutex_free(openssl_rand_mutex);
123 openssl_rand_mutex = NULL;
124 #endif
126 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
127 return SQLITE_OK;
130 static const char* sqlcipher_openssl_get_provider_name(void *ctx) {
131 return "openssl";
134 /* generate a defined number of random bytes */
135 static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
136 int rc = 0;
137 /* concurrent calls to RAND_bytes can cause a crash under some openssl versions when a
138 naive application doesn't use CRYPTO_set_locking_callback and
139 CRYPTO_THREADID_set_callback to ensure openssl thread safety.
140 This is simple workaround to prevent this common crash
141 but a more proper solution is that applications setup platform-appropriate
142 thread saftey in openssl externally */
143 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
144 sqlite3_mutex_enter(openssl_rand_mutex);
145 #endif
146 rc = RAND_bytes((unsigned char *)buffer, length);
147 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
148 sqlite3_mutex_leave(openssl_rand_mutex);
149 #endif
150 return (rc == 1) ? SQLITE_OK : SQLITE_ERROR;
153 static int sqlcipher_openssl_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) {
154 HMAC_CTX hctx;
155 unsigned int outlen;
156 HMAC_CTX_init(&hctx);
157 HMAC_Init_ex(&hctx, hmac_key, key_sz, EVP_sha1(), NULL);
158 HMAC_Update(&hctx, in, in_sz);
159 HMAC_Update(&hctx, in2, in2_sz);
160 HMAC_Final(&hctx, out, &outlen);
161 HMAC_CTX_cleanup(&hctx);
162 return SQLITE_OK;
165 static int sqlcipher_openssl_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
166 PKCS5_PBKDF2_HMAC_SHA1((const char *)pass, pass_sz, salt, salt_sz, workfactor, key_sz, key);
167 return SQLITE_OK;
170 static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
171 EVP_CIPHER_CTX ectx;
172 int tmp_csz, csz;
174 EVP_CipherInit(&ectx, ((openssl_ctx *)ctx)->evp_cipher, NULL, NULL, mode);
175 EVP_CIPHER_CTX_set_padding(&ectx, 0); // no padding
176 EVP_CipherInit(&ectx, NULL, key, iv, mode);
177 EVP_CipherUpdate(&ectx, out, &tmp_csz, in, in_sz);
178 csz = tmp_csz;
179 out += tmp_csz;
180 EVP_CipherFinal(&ectx, out, &tmp_csz);
181 csz += tmp_csz;
182 EVP_CIPHER_CTX_cleanup(&ectx);
183 assert(in_sz == csz);
184 return SQLITE_OK;
187 static int sqlcipher_openssl_set_cipher(void *ctx, const char *cipher_name) {
188 openssl_ctx *o_ctx = (openssl_ctx *)ctx;
189 o_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
190 return SQLITE_OK;
193 static const char* sqlcipher_openssl_get_cipher(void *ctx) {
194 return EVP_CIPHER_name(((openssl_ctx *)ctx)->evp_cipher);
197 static int sqlcipher_openssl_get_key_sz(void *ctx) {
198 return EVP_CIPHER_key_length(((openssl_ctx *)ctx)->evp_cipher);
201 static int sqlcipher_openssl_get_iv_sz(void *ctx) {
202 return EVP_CIPHER_iv_length(((openssl_ctx *)ctx)->evp_cipher);
205 static int sqlcipher_openssl_get_block_sz(void *ctx) {
206 return EVP_CIPHER_block_size(((openssl_ctx *)ctx)->evp_cipher);
209 static int sqlcipher_openssl_get_hmac_sz(void *ctx) {
210 return EVP_MD_size(EVP_sha1());
213 static int sqlcipher_openssl_ctx_copy(void *target_ctx, void *source_ctx) {
214 memcpy(target_ctx, source_ctx, sizeof(openssl_ctx));
215 return SQLITE_OK;
218 static int sqlcipher_openssl_ctx_cmp(void *c1, void *c2) {
219 return ((openssl_ctx *)c1)->evp_cipher == ((openssl_ctx *)c2)->evp_cipher;
222 static int sqlcipher_openssl_ctx_init(void **ctx) {
223 *ctx = sqlcipher_malloc(sizeof(openssl_ctx));
224 if(*ctx == NULL) return SQLITE_NOMEM;
225 sqlcipher_openssl_activate(*ctx);
226 return SQLITE_OK;
229 static int sqlcipher_openssl_ctx_free(void **ctx) {
230 sqlcipher_openssl_deactivate(*ctx);
231 sqlcipher_free(*ctx, sizeof(openssl_ctx));
232 return SQLITE_OK;
235 static int sqlcipher_openssl_fips_status(void *ctx) {
236 #ifdef SQLCIPHER_FIPS
237 return FIPS_mode();
238 #else
239 return 0;
240 #endif
243 int sqlcipher_openssl_setup(sqlcipher_provider *p) {
244 p->activate = sqlcipher_openssl_activate;
245 p->deactivate = sqlcipher_openssl_deactivate;
246 p->get_provider_name = sqlcipher_openssl_get_provider_name;
247 p->random = sqlcipher_openssl_random;
248 p->hmac = sqlcipher_openssl_hmac;
249 p->kdf = sqlcipher_openssl_kdf;
250 p->cipher = sqlcipher_openssl_cipher;
251 p->set_cipher = sqlcipher_openssl_set_cipher;
252 p->get_cipher = sqlcipher_openssl_get_cipher;
253 p->get_key_sz = sqlcipher_openssl_get_key_sz;
254 p->get_iv_sz = sqlcipher_openssl_get_iv_sz;
255 p->get_block_sz = sqlcipher_openssl_get_block_sz;
256 p->get_hmac_sz = sqlcipher_openssl_get_hmac_sz;
257 p->ctx_copy = sqlcipher_openssl_ctx_copy;
258 p->ctx_cmp = sqlcipher_openssl_ctx_cmp;
259 p->ctx_init = sqlcipher_openssl_ctx_init;
260 p->ctx_free = sqlcipher_openssl_ctx_free;
261 p->add_random = sqlcipher_openssl_add_random;
262 p->fips_status = sqlcipher_openssl_fips_status;
263 return SQLITE_OK;
266 #endif
267 #endif
268 /* END SQLCIPHER */