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]; if(ltc_init == 0) { if(register_prng(&fortuna_desc) != CRYPT_OK) return SQLITE_ERROR; if(register_cipher(&rijndael_desc) != CRYPT_OK) return SQLITE_ERROR; if(register_hash(&sha1_desc) != CRYPT_OK) return SQLITE_ERROR; ltc_init = 1; } if(fortuna_start(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } sqlite3_randomness(random_buffer_sz, &random_buffer); if(sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz) != SQLITE_OK) { return SQLITE_ERROR; } if(sqlcipher_ltc_add_random(ctx, <c, sizeof(ltc_ctx*)) != SQLITE_OK) { return SQLITE_ERROR; } if(fortuna_ready(&(ltc->prng)) != CRYPT_OK) { return SQLITE_ERROR; } return SQLITE_OK; }
static int sqlcipher_ltc_activate(void *ctx) { unsigned char random_buffer[FORTUNA_MAX_SZ]; #ifndef SQLCIPHER_LTC_NO_MUTEX_RAND if(ltc_rand_mutex == NULL){ ltc_rand_mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); } sqlite3_mutex_enter(ltc_rand_mutex); #endif sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); if(ltc_init == 0) { if(register_prng(&fortuna_desc) < 0) return SQLITE_ERROR; if(register_cipher(&rijndael_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha512_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha256_desc) < 0) return SQLITE_ERROR; if(register_hash(&sha1_desc) < 0) return SQLITE_ERROR; if(fortuna_start(&prng) != CRYPT_OK) { return SQLITE_ERROR; } ltc_init = 1; } ltc_ref_count++; #ifndef SQLCIPHER_TEST sqlite3_randomness(FORTUNA_MAX_SZ, random_buffer); #endif #ifndef SQLCIPHER_LTC_NO_MUTEX_RAND sqlite3_mutex_leave(ltc_rand_mutex); #endif if(sqlcipher_ltc_add_random(ctx, random_buffer, FORTUNA_MAX_SZ) != SQLITE_OK) { return SQLITE_ERROR; } sqlcipher_memset(random_buffer, 0, FORTUNA_MAX_SZ); 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) { 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; hash_idx = find_hash("sha1"); if((rc = pkcs_5_alg2(pass, pass_sz, salt, salt_sz, workfactor, hash_idx, key, &outlen)) != CRYPT_OK) { return SQLITE_ERROR; } if((rc = pkcs_5_alg2(key, key_sz, salt, salt_sz, 1, hash_idx, random_buffer, &random_buffer_sz)) != CRYPT_OK) { return SQLITE_ERROR; } sqlcipher_ltc_add_random(ctx, random_buffer, random_buffer_sz); return SQLITE_OK; }