2008-07-30 10:43:21 -04:00
|
|
|
/*
|
2009-06-04 09:25:20 -04:00
|
|
|
** SQLCipher
|
2008-07-30 10:43:21 -04:00
|
|
|
** crypto.h developed by Stephen Lombardo (Zetetic LLC)
|
|
|
|
|
** sjlombardo at zetetic dot net
|
|
|
|
|
** http://zetetic.net
|
2008-07-30 13:15:55 -04:00
|
|
|
**
|
2008-11-30 13:59:21 -05:00
|
|
|
** Copyright (c) 2008, 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.
|
2008-07-30 13:33:57 -04:00
|
|
|
**
|
2008-07-30 10:43:21 -04:00
|
|
|
*/
|
|
|
|
|
/* BEGIN CRYPTO */
|
2010-08-28 08:31:38 -04:00
|
|
|
#ifdef SQLITE_HAS_CODEC
|
2008-07-30 10:43:21 -04:00
|
|
|
#ifndef CRYPTO_H
|
|
|
|
|
#define CRYPTO_H
|
|
|
|
|
|
2009-04-21 17:36:53 -04:00
|
|
|
#define FILE_HEADER_SZ 16
|
|
|
|
|
|
2012-05-17 07:59:43 -05:00
|
|
|
#ifndef CIPHER_VERSION
|
2012-12-07 10:56:20 -05:00
|
|
|
#define CIPHER_VERSION "2.1.1"
|
2012-05-17 07:59:43 -05:00
|
|
|
#endif
|
|
|
|
|
|
2009-06-04 09:25:20 -04:00
|
|
|
#ifndef CIPHER
|
|
|
|
|
#define CIPHER "aes-256-cbc"
|
|
|
|
|
#endif
|
|
|
|
|
|
2008-08-04 15:46:58 -04:00
|
|
|
#define CIPHER_DECRYPT 0
|
|
|
|
|
#define CIPHER_ENCRYPT 1
|
2008-07-30 10:43:21 -04:00
|
|
|
|
2011-03-02 13:11:00 -05:00
|
|
|
#define CIPHER_READ_CTX 0
|
|
|
|
|
#define CIPHER_WRITE_CTX 1
|
|
|
|
|
#define CIPHER_READWRITE_CTX 2
|
|
|
|
|
|
2009-04-21 16:32:07 -04:00
|
|
|
#ifndef PBKDF2_ITER
|
|
|
|
|
#define PBKDF2_ITER 4000
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-07-17 16:19:40 -04:00
|
|
|
/* possible flags for cipher_ctx->flags */
|
2012-07-17 16:45:02 -04:00
|
|
|
#define CIPHER_FLAG_HMAC 0x01
|
|
|
|
|
#define CIPHER_FLAG_LE_PGNO 0x02
|
2012-07-18 11:21:12 -04:00
|
|
|
#define CIPHER_FLAG_BE_PGNO 0x04
|
2012-07-17 16:19:40 -04:00
|
|
|
|
|
|
|
|
#ifndef DEFAULT_CIPHER_FLAGS
|
2012-07-17 16:45:02 -04:00
|
|
|
#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO
|
2011-02-19 14:57:11 -05:00
|
|
|
#endif
|
|
|
|
|
|
2012-07-17 16:19:40 -04:00
|
|
|
|
2012-01-12 23:42:55 -05:00
|
|
|
/* 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
|
2012-01-03 17:26:44 -05:00
|
|
|
#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 */
|
2012-01-12 23:42:55 -05:00
|
|
|
#ifndef HMAC_SALT_MASK
|
|
|
|
|
#define HMAC_SALT_MASK 0x3a
|
2012-01-03 17:26:44 -05:00
|
|
|
#endif
|
|
|
|
|
|
2011-03-02 13:11:00 -05:00
|
|
|
#ifdef CODEC_DEBUG
|
|
|
|
|
#define CODEC_TRACE(X) {printf X;fflush(stdout);}
|
|
|
|
|
#else
|
|
|
|
|
#define CODEC_TRACE(X)
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-06-13 12:27:59 -04:00
|
|
|
#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
|
2011-03-02 13:11:00 -05:00
|
|
|
|
2012-05-15 20:48:10 -04:00
|
|
|
/* extensions defined in pager.c */
|
2009-02-26 13:54:38 -05:00
|
|
|
void sqlite3pager_get_codec(Pager *pPager, void **ctx);
|
2009-02-19 13:09:17 -05:00
|
|
|
int sqlite3pager_is_mj_pgno(Pager *pPager, Pgno pgno);
|
2009-04-21 22:38:41 -04:00
|
|
|
sqlite3_file *sqlite3Pager_get_fd(Pager *pPager);
|
2009-08-11 16:49:37 -04:00
|
|
|
void sqlite3pager_sqlite3PagerSetCodec(
|
|
|
|
|
Pager *pPager,
|
|
|
|
|
void *(*xCodec)(void*,void*,Pgno,int),
|
|
|
|
|
void (*xCodecSizeChng)(void*,int,int),
|
|
|
|
|
void (*xCodecFree)(void*),
|
|
|
|
|
void *pCodec
|
|
|
|
|
);
|
2012-05-15 20:48:10 -04:00
|
|
|
void sqlite3pager_sqlite3PagerSetError(Pager *pPager, int error);
|
|
|
|
|
/* end extensions defined in pager.c */
|
2011-03-02 13:11:00 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
** 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 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]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* extensions defined in crypto_impl.c */
|
|
|
|
|
|
|
|
|
|
typedef struct codec_ctx codec_ctx;
|
|
|
|
|
|
|
|
|
|
/* utility functions */
|
2012-11-16 15:05:33 -05:00
|
|
|
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);
|
2011-03-02 13:11:00 -05:00
|
|
|
int sqlcipher_pseudorandom(void *, int);
|
|
|
|
|
void sqlcipher_free(void *, int);
|
|
|
|
|
|
|
|
|
|
/* activation and initialization */
|
|
|
|
|
void sqlcipher_activate();
|
2012-10-02 11:55:07 -04:00
|
|
|
void sqlcipher_deactivate();
|
2011-03-02 13:11:00 -05:00
|
|
|
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_pass(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 *);
|
|
|
|
|
|
|
|
|
|
int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
|
2012-11-16 12:42:41 -05:00
|
|
|
int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
|
|
|
|
|
|
2011-03-02 13:11:00 -05:00
|
|
|
void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
|
|
|
|
|
|
2012-01-12 23:42:55 -05:00
|
|
|
int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int, int);
|
2012-11-16 12:52:51 -05:00
|
|
|
int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *, int);
|
2012-01-03 17:26:44 -05:00
|
|
|
|
2011-03-02 13:11:00 -05:00
|
|
|
int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *, int);
|
2012-11-16 12:42:41 -05:00
|
|
|
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
|
2011-03-02 13:11:00 -05:00
|
|
|
|
|
|
|
|
void* sqlcipher_codec_ctx_get_data(codec_ctx *);
|
|
|
|
|
|
2011-04-14 16:44:29 -04:00
|
|
|
void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
|
|
|
|
|
|
2012-02-29 11:14:42 -05:00
|
|
|
void sqlcipher_set_default_use_hmac(int use);
|
2012-11-16 12:42:41 -05:00
|
|
|
int sqlcipher_get_default_use_hmac();
|
|
|
|
|
|
2012-11-16 11:13:14 -05:00
|
|
|
void sqlcipher_set_hmac_salt_mask(unsigned char mask);
|
2012-11-16 13:32:03 -05:00
|
|
|
unsigned char sqlcipher_get_hmac_salt_mask();
|
2012-02-29 11:14:42 -05:00
|
|
|
|
2011-05-09 15:14:17 -07:00
|
|
|
int sqlcipher_codec_ctx_set_use_hmac(codec_ctx *ctx, int use);
|
2012-11-16 12:42:41 -05:00
|
|
|
int sqlcipher_codec_ctx_get_use_hmac(codec_ctx *ctx, int for_ctx);
|
2012-07-17 22:54:38 -04:00
|
|
|
|
|
|
|
|
int sqlcipher_codec_ctx_set_flag(codec_ctx *ctx, unsigned int flag);
|
|
|
|
|
int sqlcipher_codec_ctx_unset_flag(codec_ctx *ctx, unsigned int flag);
|
2012-11-16 16:16:10 -05:00
|
|
|
int sqlcipher_codec_ctx_get_flag(codec_ctx *ctx, unsigned int flag, int for_ctx);
|
2012-07-17 22:54:38 -04:00
|
|
|
|
2011-03-02 13:11:00 -05:00
|
|
|
/* end extensions defined in crypto_impl.c */
|
2009-02-19 13:09:17 -05:00
|
|
|
|
2009-02-26 13:54:38 -05:00
|
|
|
#endif
|
2008-07-30 10:43:21 -04:00
|
|
|
#endif
|
2008-08-04 15:46:58 -04:00
|
|
|
/* END CRYPTO */
|