2013-06-12 11:01:38 -04:00
|
|
|
/*
|
|
|
|
|
** 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
|
2013-06-04 10:13:02 -05:00
|
|
|
#ifdef SQLCIPHER_CRYPTO_LIBTOMCRYPT
|
2013-05-31 02:18:05 -04:00
|
|
|
#include "sqliteInt.h"
|
|
|
|
|
#include "sqlcipher.h"
|
2013-05-31 14:40:12 -05:00
|
|
|
#include <tomcrypt.h>
|
|
|
|
|
|
2013-06-05 10:18:33 -05:00
|
|
|
typedef struct {
|
|
|
|
|
prng_state prng;
|
|
|
|
|
} ltc_ctx;
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static unsigned int ltc_init = 0;
|
|
|
|
|
|
2013-06-05 16:48:36 -05:00
|
|
|
static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
|
|
|
|
|
ltc_ctx *ltc = (ltc_ctx*)ctx;
|
|
|
|
|
int rc = fortuna_add_entropy(buffer, length, &(ltc->prng));
|
|
|
|
|
return rc != CRYPT_OK ? SQLITE_ERROR : SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_activate(void *ctx) {
|
2013-06-05 10:18:33 -05:00
|
|
|
ltc_ctx *ltc = (ltc_ctx*)ctx;
|
2013-06-11 09:23:08 -05:00
|
|
|
int random_buffer_sz = 32;
|
2013-06-06 15:48:11 -04:00
|
|
|
unsigned char random_buffer[random_buffer_sz];
|
2013-06-11 11:24:29 -05:00
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
if(ltc_init == 0) {
|
2013-06-05 10:18:33 -05:00
|
|
|
if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR;
|
2013-05-31 00:52:19 -04:00
|
|
|
ltc_init = 1;
|
|
|
|
|
}
|
2013-06-11 09:23:08 -05:00
|
|
|
if(fortuna_start(&(ltc->prng)) != CRYPT_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-06 15:48:11 -04:00
|
|
|
sqlite3_randomness(random_buffer_sz, &random_buffer);
|
|
|
|
|
if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-11 09:23:08 -05:00
|
|
|
if(sqlcipher_ltc_add_random(ctx, <c, sizeof(ltc_ctx*)) != SQLITE_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-06 15:48:11 -04:00
|
|
|
if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-05 10:18:33 -05:00
|
|
|
return SQLITE_OK;
|
2013-05-31 14:40:12 -05:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_deactivate(void *ctx) {
|
2013-06-05 10:18:33 -05:00
|
|
|
ltc_ctx *ltc = (ltc_ctx*)ctx;
|
|
|
|
|
fortuna_done(&(ltc->prng));
|
2013-05-31 14:40:12 -05:00
|
|
|
}
|
|
|
|
|
|
2013-06-05 11:13:22 -05:00
|
|
|
static const char* sqlcipher_ltc_get_provider_name(void *ctx) {
|
|
|
|
|
return "libtomcrypt";
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_random(void *ctx, void *buffer, int length) {
|
2013-06-05 11:13:22 -05:00
|
|
|
ltc_ctx *ltc = (ltc_ctx*)ctx;
|
2013-06-11 11:24:29 -05:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
|
|
if((rc = fortuna_ready(&(ltc->prng))) != CRYPT_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-05 10:18:33 -05:00
|
|
|
fortuna_read(buffer, length, &(ltc->prng));
|
2013-05-31 14:40:12 -05:00
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_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) {
|
2013-05-31 14:40:12 -05:00
|
|
|
int rc, hash_idx;
|
|
|
|
|
hmac_state hmac;
|
|
|
|
|
unsigned long outlen = key_sz;
|
|
|
|
|
|
|
|
|
|
hash_idx = find_hash("sha1");
|
|
|
|
|
if((rc = hmac_init(&hmac, hash_idx, hmac_key, key_sz)) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
if((rc = hmac_process(&hmac, in, in_sz)) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
if((rc = hmac_process(&hmac, in2, in2_sz)) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
if((rc = hmac_done(&hmac, out, &outlen)) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-21 11:01:18 -04:00
|
|
|
static int sqlcipher_ltc_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) {
|
2013-05-31 14:40:12 -05:00
|
|
|
int rc, hash_idx;
|
|
|
|
|
unsigned long outlen = key_sz;
|
2013-06-06 10:26:12 -04:00
|
|
|
unsigned long random_buffer_sz = 256;
|
|
|
|
|
char random_buffer[random_buffer_sz];
|
2013-06-11 09:23:08 -05:00
|
|
|
ltc_ctx *ltc = (ltc_ctx*)ctx;
|
2013-05-31 14:40:12 -05:00
|
|
|
|
|
|
|
|
hash_idx = find_hash("sha1");
|
|
|
|
|
if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz,
|
2013-06-06 11:04:03 -05:00
|
|
|
workfactor, hash_idx, key, &outlen)) != CRYPT_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-06 10:26:12 -04:00
|
|
|
if((rc = pkcs_5_alg2(key, key_sz, salt, salt_sz,
|
2013-06-06 11:04:03 -05:00
|
|
|
1, hash_idx, random_buffer, &random_buffer_sz)) != CRYPT_OK) {
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
|
}
|
2013-06-06 10:26:12 -04:00
|
|
|
sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz);
|
2013-05-31 14:40:12 -05:00
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static const char* sqlcipher_ltc_get_cipher(void *ctx) {
|
|
|
|
|
return "rijndael";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int sqlcipher_ltc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
|
2013-05-31 14:40:12 -05:00
|
|
|
int rc, cipher_idx, hash_idx;
|
|
|
|
|
symmetric_CBC cbc;
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR;
|
2013-05-31 14:40:12 -05:00
|
|
|
if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
|
|
|
|
|
if(rc != CRYPT_OK) return SQLITE_ERROR;
|
|
|
|
|
cbc_done(&cbc);
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_set_cipher(void *ctx, const char *cipher_name) {
|
2013-05-31 14:40:12 -05:00
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_get_key_sz(void *ctx) {
|
|
|
|
|
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
|
2013-05-31 14:40:12 -05:00
|
|
|
return cipher_descriptor[cipher_idx].max_key_length;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_get_iv_sz(void *ctx) {
|
|
|
|
|
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
|
2013-05-31 14:40:12 -05:00
|
|
|
return cipher_descriptor[cipher_idx].block_length;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_get_block_sz(void *ctx) {
|
|
|
|
|
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
|
2013-05-31 14:40:12 -05:00
|
|
|
return cipher_descriptor[cipher_idx].block_length;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_get_hmac_sz(void *ctx) {
|
2013-05-31 14:40:12 -05:00
|
|
|
int hash_idx = find_hash("sha1");
|
|
|
|
|
return hash_descriptor[hash_idx].hashsize;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_ctx_copy(void *target_ctx, void *source_ctx) {
|
2013-06-11 09:23:08 -05:00
|
|
|
memcpy(target_ctx, source_ctx, sizeof(ltc_ctx));
|
|
|
|
|
return SQLITE_OK;
|
2013-05-31 14:40:12 -05:00
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_ctx_cmp(void *c1, void *c2) {
|
2013-05-31 14:40:12 -05:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_ctx_init(void **ctx) {
|
2013-06-05 10:18:33 -05:00
|
|
|
*ctx = sqlcipher_malloc(sizeof(ltc_ctx));
|
|
|
|
|
if(*ctx == NULL) return SQLITE_NOMEM;
|
|
|
|
|
sqlcipher_ltc_activate(*ctx);
|
2013-05-31 14:40:12 -05:00
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-31 00:52:19 -04:00
|
|
|
static int sqlcipher_ltc_ctx_free(void **ctx) {
|
|
|
|
|
sqlcipher_ltc_deactivate(&ctx);
|
2013-06-05 10:18:33 -05:00
|
|
|
sqlcipher_free(*ctx, sizeof(ltc_ctx));
|
2013-05-31 14:40:12 -05:00
|
|
|
return SQLITE_OK;
|
|
|
|
|
}
|
2013-05-31 00:52:19 -04:00
|
|
|
|
|
|
|
|
int sqlcipher_ltc_setup(sqlcipher_provider *p) {
|
2013-06-11 09:23:08 -05:00
|
|
|
p->activate = sqlcipher_ltc_activate;
|
2013-06-05 11:13:22 -05:00
|
|
|
p->deactivate = sqlcipher_ltc_deactivate;
|
|
|
|
|
p->get_provider_name = sqlcipher_ltc_get_provider_name;
|
2013-05-31 00:52:19 -04:00
|
|
|
p->random = sqlcipher_ltc_random;
|
|
|
|
|
p->hmac = sqlcipher_ltc_hmac;
|
|
|
|
|
p->kdf = sqlcipher_ltc_kdf;
|
|
|
|
|
p->cipher = sqlcipher_ltc_cipher;
|
|
|
|
|
p->set_cipher = sqlcipher_ltc_set_cipher;
|
|
|
|
|
p->get_cipher = sqlcipher_ltc_get_cipher;
|
|
|
|
|
p->get_key_sz = sqlcipher_ltc_get_key_sz;
|
|
|
|
|
p->get_iv_sz = sqlcipher_ltc_get_iv_sz;
|
|
|
|
|
p->get_block_sz = sqlcipher_ltc_get_block_sz;
|
|
|
|
|
p->get_hmac_sz = sqlcipher_ltc_get_hmac_sz;
|
|
|
|
|
p->ctx_copy = sqlcipher_ltc_ctx_copy;
|
|
|
|
|
p->ctx_cmp = sqlcipher_ltc_ctx_cmp;
|
|
|
|
|
p->ctx_init = sqlcipher_ltc_ctx_init;
|
|
|
|
|
p->ctx_free = sqlcipher_ltc_ctx_free;
|
2013-06-05 16:48:36 -05:00
|
|
|
p->add_random = sqlcipher_ltc_add_random;
|
2013-05-31 00:52:19 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-03 16:45:44 -05:00
|
|
|
#endif
|
2013-06-12 11:01:38 -04:00
|
|
|
#endif
|
|
|
|
|
/* END SQLCIPHER */
|