cipher_integrity check will report expected page size if invalid
[sqlcipher.git] / src / crypto_openssl.c
blob8ab1ad3abd67a65c1092e2ee043584a7fcb47215
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/crypto.h>
38 #include <openssl/rand.h>
39 #include <openssl/evp.h>
40 #include <openssl/objects.h>
41 #include <openssl/hmac.h>
42 #include <openssl/err.h>
44 static unsigned int openssl_init_count = 0;
46 static void sqlcipher_openssl_log_errors() {
47 unsigned long err = 0;
48 while((err = ERR_get_error()) != 0) {
49 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_log_errors: ERR_get_error() returned %lx: %s", err, ERR_error_string(err, NULL));
53 #if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L) || (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x20700000L)
54 static HMAC_CTX *HMAC_CTX_new(void)
56 HMAC_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
57 if (ctx != NULL) {
58 HMAC_CTX_init(ctx);
60 return ctx;
63 /* Per 1.1.0 (https://wiki.openssl.org/index.php/1.1_API_Changes)
64 HMAC_CTX_free should call HMAC_CTX_cleanup, then EVP_MD_CTX_Cleanup.
65 HMAC_CTX_cleanup internally calls EVP_MD_CTX_cleanup so these
66 calls are not needed. */
67 static void HMAC_CTX_free(HMAC_CTX *ctx)
69 if (ctx != NULL) {
70 HMAC_CTX_cleanup(ctx);
71 OPENSSL_free(ctx);
74 #endif
76 static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
77 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
78 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_add_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND");
79 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND));
80 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_add_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND");
81 #endif
82 RAND_add(buffer, length, 0);
83 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
84 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_add_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND");
85 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND));
86 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_add_random: left SQLCIPHER_MUTEX_PROVIDER_RAND");
87 #endif
88 return SQLITE_OK;
91 #define OPENSSL_CIPHER EVP_aes_256_cbc()
93 /* activate and initialize sqlcipher. Most importantly, this will automatically
94 intialize OpenSSL's EVP system if it hasn't already be externally. Note that
95 this function may be called multiple times as new codecs are intiialized.
96 Thus it performs some basic counting to ensure that only the last and final
97 sqlcipher_openssl_deactivate() will free the EVP structures.
99 static int sqlcipher_openssl_activate(void *ctx) {
100 /* initialize openssl and increment the internal init counter
101 but only if it hasn't been initalized outside of SQLCipher by this program
102 e.g. on startup */
103 int rc = 0;
105 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_activate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
106 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
107 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_activate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
109 #if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L)
110 ERR_load_crypto_strings();
111 #endif
113 #ifdef SQLCIPHER_FIPS
114 if(!FIPS_mode()){
115 if(!(rc = FIPS_mode_set(1))){
116 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_activate: FIPS_mode_set() returned %d", rc);
117 sqlcipher_openssl_log_errors();
120 #endif
122 openssl_init_count++;
123 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_activate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
124 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
125 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_activate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
126 return SQLITE_OK;
129 /* deactivate SQLCipher, most imporantly decremeting the activation count and
130 freeing the EVP structures on the final deactivation to ensure that
131 OpenSSL memory is cleaned up */
132 static int sqlcipher_openssl_deactivate(void *ctx) {
133 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_deactivate: entering SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
134 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
135 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_deactivate: entered SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
137 openssl_init_count--;
139 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_deactivate: leaving SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
140 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_ACTIVATE));
141 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_deactivate: left SQLCIPHER_MUTEX_PROVIDER_ACTIVATE");
142 return SQLITE_OK;
145 static const char* sqlcipher_openssl_get_provider_name(void *ctx) {
146 return "openssl";
149 static const char* sqlcipher_openssl_get_provider_version(void *ctx) {
150 #if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L)
151 return OPENSSL_VERSION_TEXT;
152 #else
153 return OpenSSL_version(OPENSSL_VERSION);
154 #endif
157 /* generate a defined number of random bytes */
158 static int sqlcipher_openssl_random (void *ctx, void *buffer, int length) {
159 int rc = 0;
160 /* concurrent calls to RAND_bytes can cause a crash under some openssl versions when a
161 naive application doesn't use CRYPTO_set_locking_callback and
162 CRYPTO_THREADID_set_callback to ensure openssl thread safety.
163 This is simple workaround to prevent this common crash
164 but a more proper solution is that applications setup platform-appropriate
165 thread saftey in openssl externally */
166 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
167 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_random: entering SQLCIPHER_MUTEX_PROVIDER_RAND");
168 sqlite3_mutex_enter(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND));
169 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_random: entered SQLCIPHER_MUTEX_PROVIDER_RAND");
170 #endif
171 rc = RAND_bytes((unsigned char *)buffer, length);
172 #ifndef SQLCIPHER_OPENSSL_NO_MUTEX_RAND
173 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_random: leaving SQLCIPHER_MUTEX_PROVIDER_RAND");
174 sqlite3_mutex_leave(sqlcipher_mutex(SQLCIPHER_MUTEX_PROVIDER_RAND));
175 sqlcipher_log(SQLCIPHER_LOG_TRACE, "sqlcipher_openssl_random: left SQLCIPHER_MUTEX_PROVIDER_RAND");
176 #endif
177 if(!rc) {
178 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_random: RAND_bytes() returned %d", rc);
179 sqlcipher_openssl_log_errors();
180 return SQLITE_ERROR;
182 return SQLITE_OK;
185 static int sqlcipher_openssl_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) {
186 int rc = 0;
188 #if (defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x30000000L)
189 unsigned int outlen;
190 HMAC_CTX* hctx = NULL;
192 if(in == NULL) goto error;
194 hctx = HMAC_CTX_new();
195 if(hctx == NULL) {
196 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_CTX_new() failed");
197 sqlcipher_openssl_log_errors();
198 goto error;
201 switch(algorithm) {
202 case SQLCIPHER_HMAC_SHA1:
203 if(!(rc = HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha1(), NULL))) {
204 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Init_ex() with key size %d and EVP_sha1() returned %d", key_sz, rc);
205 sqlcipher_openssl_log_errors();
206 goto error;
208 break;
209 case SQLCIPHER_HMAC_SHA256:
210 if(!(rc = HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha256(), NULL))) {
211 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Init_ex() with key size %d and EVP_sha256() returned %d", key_sz, rc);
212 sqlcipher_openssl_log_errors();
213 goto error;
215 break;
216 case SQLCIPHER_HMAC_SHA512:
217 if(!(rc = HMAC_Init_ex(hctx, hmac_key, key_sz, EVP_sha512(), NULL))) {
218 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Init_ex() with key size %d and EVP_sha512() returned %d", key_sz, rc);
219 sqlcipher_openssl_log_errors();
220 goto error;
222 break;
223 default:
224 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: invalid algorithm %d", algorithm);
225 goto error;
228 if(!(rc = HMAC_Update(hctx, in, in_sz))) {
229 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Update() on 1st input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc);
230 sqlcipher_openssl_log_errors();
231 goto error;
234 if(in2 != NULL) {
235 if(!(rc = HMAC_Update(hctx, in2, in2_sz))) {
236 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Update() on 2nd input buffer of %d bytes using algorithm %d returned %d", in2_sz, algorithm, rc);
237 sqlcipher_openssl_log_errors();
238 goto error;
242 if(!(rc = HMAC_Final(hctx, out, &outlen))) {
243 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: HMAC_Final() using algorithm %d returned %d", algorithm, rc);
244 sqlcipher_openssl_log_errors();
245 goto error;
248 rc = SQLITE_OK;
249 goto cleanup;
251 error:
252 rc = SQLITE_ERROR;
254 cleanup:
255 if(hctx) HMAC_CTX_free(hctx);
257 #else
258 size_t outlen;
259 EVP_MAC *mac = NULL;
260 EVP_MAC_CTX *hctx = NULL;
261 OSSL_PARAM sha1[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha1", 4, 0 }, OSSL_PARAM_END };
262 OSSL_PARAM sha256[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha256", 6, 0 }, OSSL_PARAM_END };
263 OSSL_PARAM sha512[] = { { "digest", OSSL_PARAM_UTF8_STRING, "sha512", 6, 0 }, OSSL_PARAM_END };
265 if(in == NULL) goto error;
267 mac = EVP_MAC_fetch(NULL, "HMAC", NULL);
268 if(mac == NULL) {
269 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_fetch for HMAC failed");
270 sqlcipher_openssl_log_errors();
271 goto error;
274 hctx = EVP_MAC_CTX_new(mac);
275 if(hctx == NULL) {
276 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_CTX_new() failed");
277 sqlcipher_openssl_log_errors();
278 goto error;
281 switch(algorithm) {
282 case SQLCIPHER_HMAC_SHA1:
283 if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha1))) {
284 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha1 returned %d", key_sz, rc);
285 sqlcipher_openssl_log_errors();
286 goto error;
288 break;
289 case SQLCIPHER_HMAC_SHA256:
290 if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha256))) {
291 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha256 returned %d", key_sz, rc);
292 sqlcipher_openssl_log_errors();
293 goto error;
295 break;
296 case SQLCIPHER_HMAC_SHA512:
297 if(!(rc = EVP_MAC_init(hctx, hmac_key, key_sz, sha512))) {
298 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_init() with key size %d and sha512 returned %d", key_sz, rc);
299 sqlcipher_openssl_log_errors();
300 goto error;
302 break;
303 default:
304 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: invalid algorithm %d", algorithm);
305 goto error;
308 if(!(rc = EVP_MAC_update(hctx, in, in_sz))) {
309 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_update() on 1st input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc);
310 sqlcipher_openssl_log_errors();
311 goto error;
314 if(in2 != NULL) {
315 if(!(rc = EVP_MAC_update(hctx, in2, in2_sz))) {
316 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: EVP_MAC_update() on 2nd input buffer of %d bytes using algorithm %d returned %d", in_sz, algorithm, rc);
317 sqlcipher_openssl_log_errors();
318 goto error;
322 if(!(rc = EVP_MAC_final(hctx, NULL, &outlen, 0))) {
323 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: 1st EVP_MAC_final() for output length calculation using algorithm %d returned %d", algorithm, rc);
324 sqlcipher_openssl_log_errors();
325 goto error;
328 if(!(rc = EVP_MAC_final(hctx, out, &outlen, outlen))) {
329 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_hmac: 2nd EVP_MAC_final() using algorithm %d returned %d", algorithm, rc);
330 sqlcipher_openssl_log_errors();
331 goto error;
334 rc = SQLITE_OK;
335 goto cleanup;
337 error:
338 rc = SQLITE_ERROR;
340 cleanup:
341 if(hctx) EVP_MAC_CTX_free(hctx);
342 if(mac) EVP_MAC_free(mac);
344 #endif
346 return rc;
349 static int sqlcipher_openssl_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) {
350 int rc = 0;
352 switch(algorithm) {
353 case SQLCIPHER_HMAC_SHA1:
354 if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha1(), key_sz, key))) {
355 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha1() workfactor %d and key size %d returned %d", workfactor, key_sz, rc);
356 sqlcipher_openssl_log_errors();
357 goto error;
359 break;
360 case SQLCIPHER_HMAC_SHA256:
361 if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha256(), key_sz, key))) {
362 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha256() workfactor %d and key size %d returned %d", workfactor, key_sz, rc);
363 sqlcipher_openssl_log_errors();
364 goto error;
366 break;
367 case SQLCIPHER_HMAC_SHA512:
368 if(!(rc = PKCS5_PBKDF2_HMAC((const char *)pass, pass_sz, salt, salt_sz, workfactor, EVP_sha512(), key_sz, key))) {
369 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_kdf: PKCS5_PBKDF2_HMAC() for EVP_sha512() workfactor %d and key size %d returned %d", workfactor, key_sz, rc);
370 sqlcipher_openssl_log_errors();
371 goto error;
373 break;
374 default:
375 return SQLITE_ERROR;
378 rc = SQLITE_OK;
379 goto cleanup;
380 error:
381 rc = SQLITE_ERROR;
382 cleanup:
383 return rc;
386 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) {
387 int tmp_csz, csz, rc = 0;
388 EVP_CIPHER_CTX* ectx = EVP_CIPHER_CTX_new();
389 if(ectx == NULL) {
390 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_new failed");
391 sqlcipher_openssl_log_errors();
392 goto error;
395 if(!(rc = EVP_CipherInit_ex(ectx, OPENSSL_CIPHER, NULL, NULL, NULL, mode))) {
396 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc);
397 sqlcipher_openssl_log_errors();
398 goto error;
401 if(!(rc = EVP_CIPHER_CTX_set_padding(ectx, 0))) { /* no padding */
402 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CIPHER_CTX_set_padding 0 returned %d", rc);
403 sqlcipher_openssl_log_errors();
404 goto error;
407 if(!(rc = EVP_CipherInit_ex(ectx, NULL, NULL, key, iv, mode))) {
408 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CipherInit_ex for mode %d returned %d", mode, rc);
409 sqlcipher_openssl_log_errors();
410 goto error;
413 if(!(rc = EVP_CipherUpdate(ectx, out, &tmp_csz, in, in_sz))) {
414 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CipherUpdate returned %d", rc);
415 sqlcipher_openssl_log_errors();
416 goto error;
419 csz = tmp_csz;
420 out += tmp_csz;
421 if(!(rc = EVP_CipherFinal_ex(ectx, out, &tmp_csz))) {
422 sqlcipher_log(SQLCIPHER_LOG_ERROR, "sqlcipher_openssl_cipher: EVP_CipherFinal_ex returned %d", rc);
423 sqlcipher_openssl_log_errors();
424 goto error;
427 csz += tmp_csz;
428 assert(in_sz == csz);
430 rc = SQLITE_OK;
431 goto cleanup;
432 error:
433 rc = SQLITE_ERROR;
434 cleanup:
435 if(ectx) EVP_CIPHER_CTX_free(ectx);
436 return rc;
439 static const char* sqlcipher_openssl_get_cipher(void *ctx) {
440 return OBJ_nid2sn(EVP_CIPHER_nid(OPENSSL_CIPHER));
443 static int sqlcipher_openssl_get_key_sz(void *ctx) {
444 return EVP_CIPHER_key_length(OPENSSL_CIPHER);
447 static int sqlcipher_openssl_get_iv_sz(void *ctx) {
448 return EVP_CIPHER_iv_length(OPENSSL_CIPHER);
451 static int sqlcipher_openssl_get_block_sz(void *ctx) {
452 return EVP_CIPHER_block_size(OPENSSL_CIPHER);
455 static int sqlcipher_openssl_get_hmac_sz(void *ctx, int algorithm) {
456 switch(algorithm) {
457 case SQLCIPHER_HMAC_SHA1:
458 return EVP_MD_size(EVP_sha1());
459 break;
460 case SQLCIPHER_HMAC_SHA256:
461 return EVP_MD_size(EVP_sha256());
462 break;
463 case SQLCIPHER_HMAC_SHA512:
464 return EVP_MD_size(EVP_sha512());
465 break;
466 default:
467 return 0;
471 static int sqlcipher_openssl_ctx_init(void **ctx) {
472 return sqlcipher_openssl_activate(*ctx);
475 static int sqlcipher_openssl_ctx_free(void **ctx) {
476 return sqlcipher_openssl_deactivate(NULL);
479 static int sqlcipher_openssl_fips_status(void *ctx) {
480 #ifdef SQLCIPHER_FIPS
481 return FIPS_mode();
482 #else
483 return 0;
484 #endif
487 int sqlcipher_openssl_setup(sqlcipher_provider *p) {
488 p->activate = sqlcipher_openssl_activate;
489 p->deactivate = sqlcipher_openssl_deactivate;
490 p->get_provider_name = sqlcipher_openssl_get_provider_name;
491 p->random = sqlcipher_openssl_random;
492 p->hmac = sqlcipher_openssl_hmac;
493 p->kdf = sqlcipher_openssl_kdf;
494 p->cipher = sqlcipher_openssl_cipher;
495 p->get_cipher = sqlcipher_openssl_get_cipher;
496 p->get_key_sz = sqlcipher_openssl_get_key_sz;
497 p->get_iv_sz = sqlcipher_openssl_get_iv_sz;
498 p->get_block_sz = sqlcipher_openssl_get_block_sz;
499 p->get_hmac_sz = sqlcipher_openssl_get_hmac_sz;
500 p->ctx_init = sqlcipher_openssl_ctx_init;
501 p->ctx_free = sqlcipher_openssl_ctx_free;
502 p->add_random = sqlcipher_openssl_add_random;
503 p->fips_status = sqlcipher_openssl_fips_status;
504 p->get_provider_version = sqlcipher_openssl_get_provider_version;
505 return SQLITE_OK;
508 #endif
509 #endif
510 /* END SQLCIPHER */