8000 initial implementation of CommonCrypto provider · PHPDOTSQL/sqlcipher@35b4d9a · GitHub
[go: up one dir, main page]

Skip to content

Commit 35b4d9a

Browse files
committed
initial implementation of CommonCrypto provider
1 parent f868ae3 commit 35b4d9a

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

Makefile.in

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,17 @@ CRYPTOLIBOBJ = \
137137
crypto.lo \
138138
crypto_impl.lo \
139139
crypto_openssl.lo \
140-
crypto_libtomcrypt.lo
140+
crypto_libtomcrypt.lo \
141+
crypto_cc.lo
141142

142143
CRYPTOSRC = \
143144
$(TOP)/src/crypto.h \
144145
$(TOP)/src/sqlcipher.h \
145146
$(TOP)/src/crypto.c \
146147
$(TOP)/src/crypto_impl.c \
147148
$(TOP)/src/crypto_libtomcrypt.c \
148-
$(TOP)/src/crypto_openssl.c
149+
$(TOP)/src/crypto_openssl.c \
150+
$(TOP)/src/crypto_cc.c
149151

150152
# END CRYPTO
151153

@@ -597,6 +599,8 @@ crypto_openssl.lo: $(TOP)/src/crypto_openssl.c $(HDR)
597599
$(LTCOMPILE) -c $(TOP)/src/crypto_openssl.c
598600
crypto_libtomcrypt.lo: $(TOP)/src/crypto_libtomcrypt.c $(HDR)
599601
$(LTCOMPILE) -c $(TOP)/src/crypto_libtomcrypt.c
602+
crypto_cc.lo: $(TOP)/src/crypto_cc.c $(HDR)
603+
$(LTCOMPILE) -c $(TOP)/src/crypto_cc.c
600604
# END CRYPTO
601605

602606
# Rules to build individual *.o files from files in the src directory.

src/crypto_cc.c

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#ifdef SQLCIPHER_CRYPTO_CC
2+
#include "crypto.h"
3+
#include "sqlcipher.h"
4+
#include <CommonCrypto/CommonCrypto.h>
5+
#include <Security/SecRandom.h>
6+
7+
/* generate a defined number of random bytes */
8+
static int sqlcipher_cc_random (void *ctx, void *buffer, int length) {
9+
return (SecRandomCopyBytes(kSecRandomDefault, length, (uint8_t *)buffer) == 0);
10+
return RAND_bytes((unsigned char *)buffer, length);
11+
}
12+
13+
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) {
14+
CCHmacContext hmac_context;
15+
CCHmacInit(&hmac_context, kCCHmacAlgSHA1, hmac_key, key_sz);
16+
CCHmacUpdate(&hmac_context, in, in_sz);
17+
CCHmacUpdate(&hmac_context, in2, in2_sz);
18+
CCHmacFinal(&hmac_context, out);
19+
return SQLITE_OK;
20+
}
21+
22+
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) {
23+
CCKeyDerivationPBKDF(kCCPBKDF2, pass, pass_sz, salt, salt_sz, kCCPRFHmacAlgSHA1, workfactor, key, key_sz);
24+
return SQLITE_OK;
25+
}
26+
27+
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) {
28+
CCCryptorRef cryptor;
29+
CCOptions cryptor_options;
30+
size_t tmp_csz, csz;
31+
CCOperation op = mode == CIPHER_ENCRYPT ? kCCEncrypt : kCCDecrypt;
32+
33+
CCCryptorCreate(op, kCCAlgorithmAES128, 0, key, kCCKeySizeAES256, iv, &cryptor);
34+
CCCryptorUpdate(cryptor, in, in_sz, out, in_sz, &tmp_csz);
35+
csz = tmp_csz;
36+
out += tmp_csz;
37+
CCCryptorFinal(cryptor, out, in_sz - csz, &tmp_csz);
38+
csz += tmp_csz;
39+
CCCryptorRelease(cryptor);
40+
assert(size == csz);
41+
42+
return SQLITE_OK;
43+
}
44+
45+
static int sqlcipher_cc_set_cipher(void *ctx, const char *cipher_name) {
46+
return SQLITE_OK;
47+
}
48+
49+
static const char* sqlcipher_cc_get_cipher(void *ctx) {
50+
return "aes-256-cbc";
51+
}
52+
53+
static int sqlcipher_cc_get_key_sz(void *ctx) {
54+
return kCCKeySizeAES256;
55+
}
56+
57+
static int sqlcipher_cc_get_iv_sz(void *ctx) {
58+
return kCCBlockSizeAES128;
59+
}
60+
61+
static int sqlcipher_cc_get_block_sz(void *ctx) {
62+
return kCCBlockSizeAES128;
63+
}
64+
65+
static int sqlcipher_cc_get_hmac_sz(void *ctx) {
66+
return CC_SHA1_DIGEST_LENGTH;
67+
}
68+
69+
static int sqlcipher_cc_ctx_copy(void *target_ctx, void *source_ctx) {
70+
return SQLITE_OK;
71+
}
72+
73+
static int sqlcipher_cc_ctx_cmp(void *c1, void *c2) {
74+
return SQLITE_OK;
75+
}
76+
77+
static int sqlcipher_cc_ctx_init(void **ctx) {
78+
return SQLITE_OK;
79+
}
80+
81+
static int sqlcipher_cc_ctx_free(void **ctx) {
82+
return SQLITE_OK;
83+
}
84+
85+
int sqlcipher_cc_setup(sqlcipher_provider *p) {
86+
p->random = sqlcipher_cc_random;
87+
p->hmac = sqlcipher_cc_hmac;
88+
p->kdf = sqlcipher_cc_kdf;
89+
p->cipher = sqlcipher_cc_cipher;
90+
p->set_cipher = sqlcipher_cc_set_cipher;
91+
p->get_cipher = sqlcipher_cc_get_cipher;
92+
p->get_key_sz = sqlcipher_cc_get_key_sz;
93+
p->get_iv_sz = sqlcipher_cc_get_iv_sz;
94+
p->get_block_sz = sqlcipher_cc_get_block_sz;
95+
p->get_hmac_sz = sqlcipher_cc_get_hmac_sz;
96+
p->ctx_copy = sqlcipher_cc_ctx_copy;
97+
p->ctx_cmp = sqlcipher_cc_ctx_cmp;
98+
p->ctx_init = sqlcipher_cc_ctx_init;
99+
p->ctx_free = sqlcipher_cc_ctx_free;
100+
}
101+
102+
#endif

src/crypto_impl.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ void sqlcipher_activate() {
100100
#elif SQLCIPHER_CRYPTO_LIBTOMCRYPT
101101
extern int sqlcipher_ltc_setup(sqlcipher_provider *p);
102102
sqlcipher_ltc_setup(p);
103+
#elif SQLCIPHER_CRYPTO_CC
104 9257 +
extern int sqlcipher_cc_setup(sqlcipher_provider *p);
105+
sqlcipher_cc_setup(p);
103106
#endif
104107
}
105108
sqlcipher_register_provider(p);

tool/mksqlite3c.tcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ foreach file {
230230
crypto_impl.c
231231
crypto_libtomcrypt.c
232232
crypto_openssl.c
233+
crypto_cc.c
233234

234235
global.c
235236
ctime.c

0 commit comments

Comments
 (0)
0