From 894b8ac578b992bfd88afc6ac73a58e7fc401588 Mon Sep 17 00:00:00 2001 From: Billy Gray Date: Mon, 14 Oct 2013 11:40:58 -0400 Subject: [PATCH 1/4] minor adjustments to .xcodeproj --- sqlcipher.xcodeproj/project.pbxproj | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sqlcipher.xcodeproj/project.pbxproj b/sqlcipher.xcodeproj/project.pbxproj index 81fa3f4..56d9f4a 100644 --- a/sqlcipher.xcodeproj/project.pbxproj +++ b/sqlcipher.xcodeproj/project.pbxproj @@ -149,7 +149,7 @@ ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; - shellScript = "./configure --enable-tempstore=yes --with-crypto-lib=commoncrypto CFLAGS=\"-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLCIPHER_CRYPTO_CC\"\nmake sqlite3.c\nexit 0"; + shellScript = "./configure --enable-tempstore=yes --with-crypto-lib=commoncrypto CFLAGS=\"-DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2\"\nmake sqlite3.c\nexit 0"; }; /* End PBXShellScriptBuildPhase section */ @@ -239,7 +239,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 4.3; - "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 6.0; + "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 7.0; ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SKIP_INSTALL = YES; @@ -261,7 +261,7 @@ GCC_WARN_ABOUT_RETURN_TYPE = YES; GCC_WARN_UNUSED_VARIABLE = YES; IPHONEOS_DEPLOYMENT_TARGET = 4.3; - "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 6.0; + "IPHONEOS_DEPLOYMENT_TARGET[arch=arm64]" = 7.0; SDKROOT = iphoneos; SKIP_INSTALL = YES; SUPPORTED_PLATFORMS = "iphonesimulator macosx iphoneos"; From d2a266e1040aedc0fc9b79c1c988c9227aa5fc62 Mon Sep 17 00:00:00 2001 From: Billy Gray Date: Mon, 14 Oct 2013 11:53:15 -0400 Subject: [PATCH 2/4] adjusts xcodeproj, resumes use of amalgation target as dependency, no longer needs manual/makefile build --- sqlcipher.xcodeproj/project.pbxproj | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sqlcipher.xcodeproj/project.pbxproj b/sqlcipher.xcodeproj/project.pbxproj index 56d9f4a..e984986 100644 --- a/sqlcipher.xcodeproj/project.pbxproj +++ b/sqlcipher.xcodeproj/project.pbxproj @@ -25,6 +25,16 @@ 9069D0A30FCE1A4D0042E34C /* sqlite3.c in Sources */ = {isa = PBXBuildFile; fileRef = 9069D0A20FCE1A4D0042E34C /* sqlite3.c */; }; /* End PBXBuildFile section */ +/* Begin PBXContainerItemProxy section */ + 289BE0E7180C4930003E52DA /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 08FB7793FE84155DC02AAC07 /* Project object */; + proxyType = 1; + remoteGlobalIDString = 9069D08B0FCE185A0042E34C; + remoteInfo = amalgamation; + }; +/* End PBXContainerItemProxy section */ + /* Begin PBXFileReference section */ 28B46E6217CD07A600672510 /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; }; 9069D0A20FCE1A4D0042E34C /* sqlite3.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sqlite3.c; sourceTree = ""; }; @@ -101,6 +111,7 @@ buildRules = ( ); dependencies = ( + 289BE0E8180C4930003E52DA /* PBXTargetDependency */, ); name = sqlcipher; productName = sqlcipher; @@ -164,6 +175,14 @@ }; /* End PBXSourcesBuildPhase section */ +/* Begin PBXTargetDependency section */ + 289BE0E8180C4930003E52DA /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 9069D08B0FCE185A0042E34C /* amalgamation */; + targetProxy = 289BE0E7180C4930003E52DA /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + /* Begin XCBuildConfiguration section */ 1DEB91EC08733DB70010E9CD /* Debug */ = { isa = XCBuildConfiguration; From 035ae3eb1751a19005d89247bfa717c5d8302ea3 Mon Sep 17 00:00:00 2001 From: Nick Parker Date: Tue, 15 Oct 2013 09:43:07 -0500 Subject: [PATCH 3/4] Fix compiler warnings --- src/crypto_libtomcrypt.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/crypto_libtomcrypt.c b/src/crypto_libtomcrypt.c index 0299771..bb42e50 100644 --- a/src/crypto_libtomcrypt.c +++ b/src/crypto_libtomcrypt.c @@ -49,8 +49,9 @@ static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) { static int sqlcipher_ltc_activate(void *ctx) { ltc_ctx *ltc = (ltc_ctx*)ctx; - int random_buffer_sz = 32; - unsigned char random_buffer[random_buffer_sz]; + int random_buffer_sz = sizeof(char) * 32; + unsigned char *random_buffer = sqlcipher_malloc(random_buffer_sz); + sqlcipher_memset(random_buffer, 0, random_buffer_sz); if(ltc_init == 0) { if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR; @@ -61,7 +62,7 @@ static int sqlcipher_ltc_activate(void *ctx) { if(fortuna_start(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } - sqlite3_randomness(random_buffer_sz, &random_buffer); + sqlite3_randomness(random_buffer_sz, random_buffer); if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) { return SQLITE_ERROR; } @@ -71,12 +72,14 @@ static int sqlcipher_ltc_activate(void *ctx) { if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } + sqlcipher_free(random_buffer, random_buffer_sz); return SQLITE_OK; } static int sqlcipher_ltc_deactivate(void *ctx) { ltc_ctx *ltc = (ltc_ctx*)ctx; fortuna_done(&(ltc->prng)); + return SQLITE_OK; } static const char* sqlcipher_ltc_get_provider_name(void *ctx) { @@ -109,10 +112,11 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un 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) { int rc, hash_idx; - unsigned long outlen = key_sz; - unsigned long random_buffer_sz = 256; - char random_buffer[random_buffer_sz]; ltc_ctx *ltc = (ltc_ctx*)ctx; + unsigned long outlen = key_sz; + unsigned long random_buffer_sz = sizeof(char) * 256; + unsigned char *random_buffer = sqlcipher_malloc(random_buffer_sz); + sqlcipher_memset(random_buffer, 0, random_buffer_sz); hash_idx = find_hash("sha1"); if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz, @@ -124,6 +128,7 @@ static int sqlcipher_ltc_kdf(void *ctx, const char *pass, int pass_sz, unsigned return SQLITE_ERROR; } sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz); + sqlcipher_free(random_buffer, random_buffer_sz); return SQLITE_OK; } @@ -132,7 +137,7 @@ static const char* sqlcipher_ltc_get_cipher(void *ctx) { } 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) { - int rc, cipher_idx, hash_idx; + int rc, cipher_idx; symmetric_CBC cbc; if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR; @@ -208,6 +213,7 @@ int sqlcipher_ltc_setup(sqlcipher_provider *p) { p->ctx_init = sqlcipher_ltc_ctx_init; p->ctx_free = sqlcipher_ltc_ctx_free; p->add_random = sqlcipher_ltc_add_random; + return SQLITE_OK; } #endif From bdd8d0a5c684fc8a8ac55d8883c71668c87ae117 Mon Sep 17 00:00:00 2001 From: Nick Parker Date: Tue, 15 Oct 2013 10:06:27 -0500 Subject: [PATCH 4/4] Change pass field to unsigned char * in cipher_ctx --- src/crypto.c | 2 +- src/crypto.h | 2 +- src/crypto_cc.c | 4 ++-- src/crypto_impl.c | 14 +++++++------- src/crypto_libtomcrypt.c | 2 +- src/crypto_openssl.c | 4 ++-- src/sqlcipher.h | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/crypto.c b/src/crypto.c index 54b0a1c..6bb27f0 100644 --- a/src/crypto.c +++ b/src/crypto.c @@ -220,7 +220,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef if(zRight) { if (sqlite3StrNICmp(zRight ,"x'", 2) == 0 && sqlite3Strlen30(zRight) == 5) { unsigned char mask = 0; - const char *hex = zRight+2; + const unsigned char *hex = (const unsigned char *)zRight+2; cipher_hex2bin(hex,2,&mask); sqlcipher_set_hmac_salt_mask(mask); } diff --git a/src/crypto.h b/src/crypto.h index f7738db..ba0d399 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -142,7 +142,7 @@ static int cipher_hex2int(char c) { (c>='a' && c<='f') ? (c)-'a'+10 : 0; } -static void cipher_hex2bin(const char *hex, int sz, unsigned char *out){ +static void cipher_hex2bin(const unsigned 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]); diff --git a/src/crypto_cc.c b/src/crypto_cc.c index cf932f6..c994c91 100644 --- a/src/crypto_cc.c +++ b/src/crypto_cc.c @@ -54,8 +54,8 @@ static int sqlcipher_cc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, uns return SQLITE_OK; } -static int sqlcipher_cc_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { - CCKeyDerivationPBKDF(kCCPBKDF2, pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz); +static int sqlcipher_cc_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { + CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz); return SQLITE_OK; } diff --git a/src/crypto_impl.c b/src/crypto_impl.c index ae78974..0e454d2 100644 --- a/src/crypto_impl.c +++ b/src/crypto_impl.c @@ -60,7 +60,7 @@ typedef struct { unsigned int flags; unsigned char *key; unsigned char *hmac_key; - char *pass; + unsigned char *pass; char *keyspec; sqlcipher_provider *provider; void *provider_ctx; @@ -821,19 +821,19 @@ static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) { if(c_ctx->pass && c_ctx->pass_sz) { // if pass is not null - if (c_ctx->pass_sz == ((c_ctx->key_sz * 2) + 3) && sqlite3StrNICmp(c_ctx->pass ,"x'", 2) == 0) { + if (c_ctx->pass_sz == ((c_ctx->key_sz * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0) { int n = c_ctx->pass_sz - 3; /* adjust for leading x' and tailing ' */ - const char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ + const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ CODEC_TRACE(("cipher_ctx_key_derive: using raw key from hex\n")); cipher_hex2bin(z, n, c_ctx->key); - } else if (c_ctx->pass_sz == (((c_ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3) && sqlite3StrNICmp(c_ctx->pass ,"x'", 2) == 0) { - const char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ + } else if (c_ctx->pass_sz == (((c_ctx->key_sz + ctx->kdf_salt_sz) * 2) + 3) && sqlite3StrNICmp((const char *)c_ctx->pass ,"x'", 2) == 0) { + const unsigned char *z = c_ctx->pass + 2; /* adjust lead offset of x' */ CODEC_TRACE(("cipher_ctx_key_derive: using raw key from hex\n")); cipher_hex2bin(z, (c_ctx->key_sz * 2), c_ctx->key); cipher_hex2bin(z + (c_ctx->key_sz * 2), (ctx->kdf_salt_sz * 2), ctx->kdf_salt); } else { CODEC_TRACE(("cipher_ctx_key_derive: deriving key using full PBKDF2 with %d iterations\n", c_ctx->kdf_iter)); - c_ctx->provider->kdf(c_ctx->provider_ctx, (const char*) c_ctx->pass, c_ctx->pass_sz, + c_ctx->provider->kdf(c_ctx->provider_ctx, c_ctx->pass, c_ctx->pass_sz, ctx->kdf_salt, ctx->kdf_salt_sz, c_ctx->kdf_iter, c_ctx->key_sz, c_ctx->key); } @@ -861,7 +861,7 @@ static int sqlcipher_cipher_ctx_key_derive(codec_ctx *ctx, cipher_ctx *c_ctx) { c_ctx->fast_kdf_iter)); - c_ctx->provider->kdf(c_ctx->provider_ctx, (const char*)c_ctx->key, c_ctx->key_sz, + c_ctx->provider->kdf(c_ctx->provider_ctx, c_ctx->key, c_ctx->key_sz, ctx->hmac_kdf_salt, ctx->kdf_salt_sz, c_ctx->fast_kdf_iter, c_ctx->key_sz, c_ctx->hmac_key); } diff --git a/src/crypto_libtomcrypt.c b/src/crypto_libtomcrypt.c index bb42e50..9df5781 100644 --- a/src/crypto_libtomcrypt.c +++ b/src/crypto_libtomcrypt.c @@ -110,7 +110,7 @@ static int sqlcipher_ltc_hmac(void *ctx, unsigned char *hmac_key, int key_sz, un return SQLITE_OK; } -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) { +static int sqlcipher_ltc_kdf(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { int rc, hash_idx; ltc_ctx *ltc = (ltc_ctx*)ctx; unsigned long outlen = key_sz; diff --git a/src/crypto_openssl.c b/src/crypto_openssl.c index 6035b7c..150ab92 100644 --- a/src/crypto_openssl.c +++ b/src/crypto_openssl.c @@ -154,8 +154,8 @@ static int sqlcipher_openssl_hmac(void *ctx, unsigned char *hmac_key, int key_sz return SQLITE_OK; } -static int sqlcipher_openssl_kdf(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key) { - PKCS5_PBKDF2_HMAC_SHA1(pass, pass_sz, salt, salt_sz, workfactor, key_sz, key); +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) { + PKCS5_PBKDF2_HMAC_SHA1((const char *)pass, pass_sz, salt, salt_sz, workfactor, key_sz, key); return SQLITE_OK; } diff --git a/src/sqlcipher.h b/src/sqlcipher.h index 41f8f83..37ecf3b 100644 --- a/src/sqlcipher.h +++ b/src/sqlcipher.h @@ -43,7 +43,7 @@ typedef struct { int (*add_random)(void *ctx, void *buffer, int length); int (*random)(void *ctx, void *buffer, int length); int (*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); - int (*kdf)(void *ctx, const char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key); + int (*kdf)(void *ctx, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key); int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out); int (*set_cipher)(void *ctx, const char *cipher_name); const char* (*get_cipher)(void *ctx);