Move declaration to top level of block
[sqlcipher.git] / src / crypto_cc.c
blob7890ff8ab0c8cdeb623a9d7637a91533bf4ec46c
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_CC
34 #include "crypto.h"
35 #include "sqlcipher.h"
36 #include <CommonCrypto/CommonCrypto.h>
37 #include <Security/SecRandom.h>
38 #include <CoreFoundation/CoreFoundation.h>
40 static int sqlcipher_cc_add_random(void *ctx, void *buffer, int length) {
41 return SQLITE_OK;
44 /* generate a defined number of random bytes */
45 static int sqlcipher_cc_random (void *ctx, void *buffer, int length) {
46 return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == 0) ? SQLITE_OK : SQLITE_ERROR;
49 static const char* sqlcipher_cc_get_provider_name(void *ctx) {
50 return "commoncrypto";
53 static const char* sqlcipher_cc_get_provider_version(void *ctx) {
54 #if TARGET_OS_MAC
55 CFTypeRef version;
56 CFBundleRef bundle = CFBundleGetBundleWithIdentifier(CFSTR("com.apple.security"));
57 if(bundle == NULL) {
58 return "unknown";
60 version = CFBundleGetValueForInfoDictionaryKey(bundle, CFSTR("CFBundleShortVersionString"));
61 return CFStringGetCStringPtr(version, kCFStringEncodingUTF8);
62 #else
63 return "unknown";
64 #endif
67 static int sqlcipher_cc_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) {
68 CCHmacContext hmac_context;
69 CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz);
70 CCHmacUpdate(&hmac_context, in, in_sz);
71 CCHmacUpdate(&hmac_context, in2, in2_sz);
72 CCHmacFinal(&hmac_context, out);
73 return SQLITE_OK;
76 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) {
77 CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz);
78 return SQLITE_OK;
81 static int sqlcipher_cc_cipher(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out) {
82 CCCryptorRef cryptor;
83 size_t tmp_csz, csz;
84 CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt;
86 CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor);
87 CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz);
88 csz = tmp_csz;
89 out += tmp_csz;
90 CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz);
91 csz += tmp_csz;
92 CCCryptorRelease(cryptor);
93 assert(size == csz);
95 return SQLITE_OK;
98 static int sqlcipher_cc_set_cipher(void *ctx, const char *cipher_name) {
99 return SQLITE_OK;
102 static const char* sqlcipher_cc_get_cipher(void *ctx) {
103 return "aes-256-cbc";
106 static int sqlcipher_cc_get_key_sz(void *ctx) {
107 return kCCKeySizeAES256;
110 static int sqlcipher_cc_get_iv_sz(void *ctx) {
111 return kCCBlockSizeAES128;
114 static int sqlcipher_cc_get_block_sz(void *ctx) {
115 return kCCBlockSizeAES128;
118 static int sqlcipher_cc_get_hmac_sz(void *ctx) {
119 return CC_SHA1_DIGEST_LENGTH;
122 static int sqlcipher_cc_ctx_copy(void *target_ctx, void *source_ctx) {
123 return SQLITE_OK;
126 static int sqlcipher_cc_ctx_cmp(void *c1, void *c2) {
127 return 1; /* always indicate contexts are the same */
130 static int sqlcipher_cc_ctx_init(void **ctx) {
131 return SQLITE_OK;
134 static int sqlcipher_cc_ctx_free(void **ctx) {
135 return SQLITE_OK;
138 static int sqlcipher_cc_fips_status(void *ctx) {
139 return 0;
142 int sqlcipher_cc_setup(sqlcipher_provider *p) {
143 p->random = sqlcipher_cc_random;
144 p->get_provider_name = sqlcipher_cc_get_provider_name;
145 p->hmac = sqlcipher_cc_hmac;
146 p->kdf = sqlcipher_cc_kdf;
147 p->cipher = sqlcipher_cc_cipher;
148 p->set_cipher = sqlcipher_cc_set_cipher;
149 p->get_cipher = sqlcipher_cc_get_cipher;
150 p->get_key_sz = sqlcipher_cc_get_key_sz;
151 p->get_iv_sz = sqlcipher_cc_get_iv_sz;
152 p->get_block_sz = sqlcipher_cc_get_block_sz;
153 p->get_hmac_sz = sqlcipher_cc_get_hmac_sz;
154 p->ctx_copy = sqlcipher_cc_ctx_copy;
155 p->ctx_cmp = sqlcipher_cc_ctx_cmp;
156 p->ctx_init = sqlcipher_cc_ctx_init;
157 p->ctx_free = sqlcipher_cc_ctx_free;
158 p->add_random = sqlcipher_cc_add_random;
159 p->fips_status = sqlcipher_cc_fips_status;
160 p->get_provider_version = sqlcipher_cc_get_provider_version;
161 return SQLITE_OK;
164 #endif
165 #endif
166 /* END SQLCIPHER */