cleanup to enable building with --disable-amalgamation

This commit is contained in:
Stephen Lombardo 2018-09-06 09:52:32 -04:00
parent 02cee4ca2a
commit 1cdb2fcdce
4 changed files with 75 additions and 38 deletions

View File

@ -32,10 +32,8 @@
#ifdef SQLITE_HAS_CODEC
#include <assert.h>
#include "sqliteInt.h"
#include "btreeInt.h"
#include "crypto.h"
#include "sqlcipher.h"
#include "crypto.h"
static const char* codec_get_cipher_version() {
return CIPHER_VERSION;
@ -437,7 +435,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
* decrypt mode - expected to return a pointer to pData, with
* the data decrypted in the input buffer
*/
void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
static void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
codec_ctx *ctx = (codec_ctx *) iCtx;
int offset = 0, rc = 0;
int page_sz = sqlcipher_codec_ctx_get_pagesize(ctx);
@ -490,7 +488,7 @@ void* sqlite3Codec(void *iCtx, void *data, Pgno pgno, int mode) {
}
}
void sqlite3FreeCodecArg(void *pCodecArg) {
static void sqlite3FreeCodecArg(void *pCodecArg) {
codec_ctx *ctx = (codec_ctx *) pCodecArg;
if(pCodecArg == NULL) return;
sqlcipher_codec_ctx_free(&ctx); // wipe and free allocated memory for the context

View File

@ -35,6 +35,9 @@
#ifndef CRYPTO_H
#define CRYPTO_H
#include "sqliteInt.h"
#include "btreeInt.h"
#if !defined (SQLCIPHER_CRYPTO_CC) \
&& !defined (SQLCIPHER_CRYPTO_LIBTOMCRYPT) \
&& !defined (SQLCIPHER_CRYPTO_OPENSSL)
@ -152,6 +155,7 @@ void sqlite3pager_sqlite3PagerSetCodec(
void *pCodec
);
void sqlite3pager_sqlite3PagerSetError(Pager *pPager, int error);
int sqlite3pager_truncate(Pager *pPager, Pgno nPage);
/* end extensions defined in pager.c */
/*
@ -193,9 +197,20 @@ static int cipher_isHex(const unsigned char *hex, int sz){
/* extensions defined in crypto_impl.c */
typedef struct codec_ctx codec_ctx;
/* crypto.c functions */
int sqlcipher_codec_pragma(sqlite3*, int, Parse*, const char *, const char*);
int sqlite3CodecAttach(sqlite3*, int, const void *, int);
void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
/* crypto_impl.c functions */
void sqlcipher_init_memmethods();
/* activation and initialization */
void sqlcipher_activate();
void sqlcipher_deactivate();
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 *);
@ -207,6 +222,7 @@ int sqlcipher_page_cipher(codec_ctx *, int, Pgno, int, int, unsigned char *, uns
/* context setters & getters */
void sqlcipher_codec_ctx_set_error(codec_ctx *, int);
void sqlcipher_codec_get_pass(codec_ctx *, void **, int *);
int sqlcipher_codec_ctx_set_pass(codec_ctx *, const void *, int, int);
void sqlcipher_codec_get_keyspec(codec_ctx *, void **zKey, int *nKey);
@ -219,7 +235,6 @@ int sqlcipher_get_default_pagesize();
void sqlcipher_set_default_kdf_iter(int iter);
int sqlcipher_get_default_kdf_iter();
int sqlcipher_codec_ctx_set_kdf_iter(codec_ctx *, int, int);
int sqlcipher_codec_ctx_get_kdf_iter(codec_ctx *ctx, int);
@ -234,8 +249,6 @@ const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx, int for_ctx);
void* sqlcipher_codec_ctx_get_data(codec_ctx *);
void sqlcipher_exportFunc(sqlite3_context *, int, sqlite3_value **);
void sqlcipher_set_default_use_hmac(int use);
int sqlcipher_get_default_use_hmac();
@ -258,6 +271,7 @@ void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey);
void sqlcipher_codec_set_store_pass(codec_ctx *ctx, int value);
int sqlcipher_codec_fips_status(codec_ctx *ctx);
const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx);
int sqlcipher_codec_hmac(const codec_ctx *ctx, const unsigned char *hmac_key, int key_sz,
unsigned char* in, int in_sz, unsigned char *in2, int in2_sz,
unsigned char *out);

View File

@ -31,8 +31,6 @@
/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
#include "sqliteInt.h"
#include "btreeInt.h"
#include "sqlcipher.h"
#include "crypto.h"
#ifndef OMIT_MEMLOCK
@ -98,7 +96,7 @@ struct codec_ctx {
unsigned int need_kdf_salt;
};
sqlite3_mem_methods default_mem_methods;
static sqlite3_mem_methods default_mem_methods;
static int sqlcipher_mem_init(void *pAppData) {
return default_mem_methods.xInit(pAppData);
@ -430,6 +428,27 @@ static void sqlcipher_cipher_ctx_free(cipher_ctx **iCtx) {
sqlcipher_free(ctx, sizeof(cipher_ctx));
}
static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
int base_reserve = CIPHER_MAX_IV_SZ; /* base reserve size will be IV only */
int reserve = base_reserve;
ctx->write_ctx->hmac_sz = ctx->read_ctx->hmac_sz = ctx->read_ctx->provider->get_hmac_sz(ctx->read_ctx->provider_ctx, ctx->hmac_algorithm);
if(sqlcipher_codec_ctx_get_use_hmac(ctx, 0))
reserve += ctx->read_ctx->hmac_sz; /* if reserve will include hmac, update that size */
/* calculate the amount of reserve needed in even increments of the cipher block size */
reserve = ((reserve % ctx->read_ctx->block_sz) == 0) ? reserve :
((reserve / ctx->read_ctx->block_sz) + 1) * ctx->read_ctx->block_sz;
CODEC_TRACE("sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d\n",
base_reserve, ctx->read_ctx->block_sz, ctx->read_ctx->hmac_sz, reserve);
ctx->write_ctx->reserve_sz = ctx->read_ctx->reserve_sz = reserve;
return SQLITE_OK;
}
/**
* Compare one cipher_ctx to another.
*
@ -576,29 +595,6 @@ void sqlcipher_codec_get_pass(codec_ctx *ctx, void **zKey, int *nKey) {
*nKey = ctx->read_ctx->pass_sz;
}
static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
int base_reserve = CIPHER_MAX_IV_SZ; /* base reserve size will be IV only */
int reserve = base_reserve;
ctx->write_ctx->hmac_sz = ctx->read_ctx->hmac_sz = ctx->read_ctx->provider->get_hmac_sz(ctx->read_ctx->provider_ctx, ctx->hmac_algorithm);
if(sqlcipher_codec_ctx_get_use_hmac(ctx, 0))
reserve += ctx->read_ctx->hmac_sz; /* if reserve will include hmac, update that size */
/* calculate the amount of reserve needed in even increments of the cipher block size */
reserve = ((reserve % ctx->read_ctx->block_sz) == 0) ? reserve :
((reserve / ctx->read_ctx->block_sz) + 1) * ctx->read_ctx->block_sz;
CODEC_TRACE("sqlcipher_codec_ctx_reserve_setup: base_reserve=%d block_sz=%d md_size=%d reserve=%d\n",
base_reserve, ctx->read_ctx->block_sz, ctx->read_ctx->hmac_sz, reserve);
ctx->write_ctx->reserve_sz = ctx->read_ctx->reserve_sz = reserve;
return SQLITE_OK;
}
/**
* Set the passphrase for the cipher_ctx
*
@ -606,7 +602,6 @@ static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
* returns SQLITE_NOMEM if an error occured allocating memory
*/
static int sqlcipher_cipher_ctx_set_pass(cipher_ctx *ctx, const void *zKey, int nKey) {
/* free, zero existing pointers and size */
sqlcipher_free(ctx->pass, ctx->pass_sz);
ctx->pass = NULL;
@ -1258,8 +1253,6 @@ cleanup:
return rc;
}
static int pager_truncate(Pager *pPager, Pgno nPage);
int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
u32 meta;
int i, password_sz, key_sz, saved_flags, saved_nChange, saved_nTotalChange, nRes, user_version = 0, upgrade_from = 0, rc = 0;
@ -1386,7 +1379,7 @@ int sqlcipher_codec_ctx_migrate(codec_ctx *ctx) {
rc = sqlite3_exec(db, "BEGIN;", NULL, NULL, NULL);
if( rc!=SQLITE_OK ) goto handle_error;
pager_truncate(pDest->pBt->pPager, 0);
sqlite3pager_truncate(pDest->pBt->pPager, 0);
rc = sqlite3BtreeBeginTrans(pSrc, 2);
if( rc!=SQLITE_OK ) goto handle_error;

View File

@ -7577,6 +7577,38 @@ void sqlite3pager_sqlite3PagerSetError( Pager *pPager, int error) {
setGetterMethod(pPager);
}
int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
int rc = SQLITE_OK;
assert( pPager->eState!=PAGER_ERROR );
assert( pPager->eState!=PAGER_READER );
if( isOpen(pPager->fd)
&& (pPager->eState>=PAGER_WRITER_DBMOD || pPager->eState==PAGER_OPEN)
){
i64 currentSize, newSize;
int szPage = pPager->pageSize;
assert( pPager->eLock==EXCLUSIVE_LOCK );
/* TODO: Is it safe to use Pager.dbFileSize here? */
rc = sqlite3OsFileSize(pPager->fd, &currentSize);
newSize = szPage*(i64)nPage;
if( rc==SQLITE_OK && currentSize!=newSize ){
if( currentSize>newSize ){
rc = sqlite3OsTruncate(pPager->fd, newSize);
}else if( (currentSize+szPage)<=newSize ){
char *pTmp = pPager->pTmpSpace;
memset(pTmp, 0, szPage);
testcase( (newSize-szPage) == currentSize );
testcase( (newSize-szPage) > currentSize );
rc = sqlite3OsWrite(pPager->fd, pTmp, szPage, newSize-szPage);
}
if( rc==SQLITE_OK ){
pPager->dbFileSize = nPage;
}
}
}
return rc;
}
#endif
/* END SQLCIPHER */