|
| 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 |
0 commit comments