Skip to content

Commit 369b22d

Browse files
committed
remove PRAGMA cipher
1 parent c40ffaa commit 369b22d

8 files changed

Lines changed: 51 additions & 101 deletions

File tree

src/crypto.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ static int codec_set_pass_key(sqlite3* db, int nDb, const void *zKey, int nKey,
8787
}
8888

8989
int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLeft, const char *zRight) {
90-
char *pragma_cipher_deprecated_msg = "PRAGMA cipher command is deprecated, please remove from usage.";
9190
struct Db *pDb = &db->aDb[iDb];
9291
codec_ctx *ctx = NULL;
9392
int rc;
@@ -152,10 +151,9 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
152151
if( sqlite3StrICmp(zLeft, "cipher")==0 ){
153152
if(ctx) {
154153
if( zRight ) {
155-
rc = sqlcipher_codec_ctx_set_cipher(ctx, zRight); // change cipher for both
156-
codec_vdbe_return_static_string(pParse, "cipher", pragma_cipher_deprecated_msg);
157-
sqlite3_log(SQLITE_WARNING, pragma_cipher_deprecated_msg);
158-
return rc;
154+
const char* message = "PRAGMA cipher is no longer supported.";
155+
codec_vdbe_return_static_string(pParse, "cipher", message);
156+
sqlite3_log(SQLITE_WARNING, message);
159157
}else {
160158
codec_vdbe_return_static_string(pParse, "cipher",
161159
sqlcipher_codec_ctx_get_cipher(ctx));

src/crypto.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@
5454
#endif
5555
#endif
5656

57-
#ifndef CIPHER
58-
#define CIPHER "aes-256-cbc"
59-
#endif
60-
6157
#define CIPHER_DECRYPT 0
6258
#define CIPHER_ENCRYPT 1
6359

@@ -244,7 +240,6 @@ void* sqlcipher_codec_ctx_get_kdf_salt(codec_ctx *ctx);
244240
int sqlcipher_codec_ctx_set_fast_kdf_iter(codec_ctx *, int);
245241
int sqlcipher_codec_ctx_get_fast_kdf_iter(codec_ctx *);
246242

247-
int sqlcipher_codec_ctx_set_cipher(codec_ctx *, const char *);
248243
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx);
249244

250245
void* sqlcipher_codec_ctx_get_data(codec_ctx *);

src/crypto_cc.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ static int sqlcipher_cc_cipher(void *ctx, int mode, unsigned char *key, int key_
120120
return SQLITE_OK;
121121
}
122122

123-
static int sqlcipher_cc_set_cipher(void *ctx, const char *cipher_name) {
124-
return SQLITE_OK;
125-
}
126-
127123
static const char* sqlcipher_cc_get_cipher(void *ctx) {
128124
return "aes-256-cbc";
129125
}
@@ -182,7 +178,6 @@ int sqlcipher_cc_setup(sqlcipher_provider *p) {
182178
p->hmac = sqlcipher_cc_hmac;
183179
p->kdf = sqlcipher_cc_kdf;
184180
p->cipher = sqlcipher_cc_cipher;
185-
p->set_cipher = sqlcipher_cc_set_cipher;
186181
p->get_cipher = sqlcipher_cc_get_cipher;
187182
p->get_key_sz = sqlcipher_cc_get_key_sz;
188183
p->get_iv_sz = sqlcipher_cc_get_iv_sz;

src/crypto_impl.c

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ static void sqlcipher_cipher_ctx_free(codec_ctx* ctx, cipher_ctx **iCtx) {
423423
}
424424

425425
static int sqlcipher_codec_ctx_reserve_setup(codec_ctx *ctx) {
426-
int base_reserve = CIPHER_MAX_IV_SZ; /* base reserve size will be IV only */
426+
int base_reserve = ctx->iv_sz;; /* base reserve size will be IV only */
427427
int reserve = base_reserve;
428428

429429
ctx->hmac_sz = ctx->provider->get_hmac_sz(ctx->provider_ctx, ctx->hmac_algorithm);
@@ -593,24 +593,6 @@ int sqlcipher_codec_ctx_set_pass(codec_ctx *ctx, const void *zKey, int nKey, int
593593
return SQLITE_OK;
594594
}
595595

596-
int sqlcipher_codec_ctx_set_cipher(codec_ctx *ctx, const char *cipher_name) {
597-
int rc;
598-
599-
rc = ctx->provider->set_cipher(ctx->provider_ctx, cipher_name);
600-
if(rc != SQLITE_OK){
601-
sqlcipher_codec_ctx_set_error(ctx, rc);
602-
return rc;
603-
}
604-
ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx);
605-
ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx);
606-
ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx);
607-
608-
sqlcipher_set_derive_key(ctx, 1);
609-
610-
sqlcipher_codec_ctx_reserve_setup(ctx);
611-
return SQLITE_OK;
612-
}
613-
614596
const char* sqlcipher_codec_ctx_get_cipher(codec_ctx *ctx) {
615597
return ctx->provider->get_cipher(ctx->provider_ctx);
616598
}
@@ -884,9 +866,9 @@ int sqlcipher_codec_ctx_init(codec_ctx **iCtx, Db *pDb, Pager *pPager, sqlite3_f
884866
CODEC_TRACE("sqlcipher_codec_ctx_init: calling provider ctx_init\n");
885867
if((rc = ctx->provider->ctx_init(&ctx->provider_ctx)) != SQLITE_OK) return rc;
886868

887-
/* setup the cipher to establish the key_sz, iv_sz, etc */
888-
CODEC_TRACE("sqlcipher_codec_ctx_init: setting cipher\n");
889-
if((rc = sqlcipher_codec_ctx_set_cipher(ctx, CIPHER)) != SQLITE_OK) return rc;
869+
ctx->key_sz = ctx->provider->get_key_sz(ctx->provider_ctx);
870+
ctx->iv_sz = ctx->provider->get_iv_sz(ctx->provider_ctx);
871+
ctx->block_sz = ctx->provider->get_block_sz(ctx->provider_ctx);
890872

891873
/* establic the size for a hex-formated key specification, containing the
892874
raw encryption key and the salt used to generate it format. will be x'hexkey...hexsalt'

src/crypto_libtomcrypt.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737

3838
#define FORTUNA_MAX_SZ 32
3939
static prng_state prng;
40-
static unsigned int ltc_init = 0;
41-
static unsigned int ltc_ref_count = 0;
40+
static volatile unsigned int ltc_init = 0;
41+
static volatile unsigned int ltc_ref_count = 0;
4242
static sqlite3_mutex* ltc_rand_mutex = NULL;
4343

44+
#define LTC_CIPHER "rijndael"
45+
4446
static int sqlcipher_ltc_add_random(void *ctx, void *buffer, int length) {
4547
int rc = 0;
4648
int data_to_read = length;
@@ -206,37 +208,33 @@ static int sqlcipher_ltc_kdf(void *ctx, int algorithm, const unsigned char *pass
206208
}
207209

208210
static const char* sqlcipher_ltc_get_cipher(void *ctx) {
209-
return "rijndael";
211+
return "aes-256-cbc";
210212
}
211213

212214
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) {
213215
int rc, cipher_idx;
214216
symmetric_CBC cbc;
215217

216-
if((cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx))) == -1) return SQLITE_ERROR;
218+
if((cipher_idx = find_cipher(LTC_CIPHER)) == -1) return SQLITE_ERROR;
217219
if((rc = cbc_start(cipher_idx, iv, key, key_sz, 0, &cbc)) != CRYPT_OK) return SQLITE_ERROR;
218220
rc = mode == 1 ? cbc_encrypt(in, out, in_sz, &cbc) : cbc_decrypt(in, out, in_sz, &cbc);
219221
if(rc != CRYPT_OK) return SQLITE_ERROR;
220222
cbc_done(&cbc);
221223
return SQLITE_OK;
222224
}
223225

224-
static int sqlcipher_ltc_set_cipher(void *ctx, const char *cipher_name) {
225-
return SQLITE_OK;
226-
}
227-
228226
static int sqlcipher_ltc_get_key_sz(void *ctx) {
229-
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
227+
int cipher_idx = find_cipher(LTC_CIPHER);
230228
return cipher_descriptor[cipher_idx].max_key_length;
231229
}
232230

233231
static int sqlcipher_ltc_get_iv_sz(void *ctx) {
234-
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
232+
int cipher_idx = find_cipher(LTC_CIPHER);
235233
return cipher_descriptor[cipher_idx].block_length;
236234
}
237235

238236
static int sqlcipher_ltc_get_block_sz(void *ctx) {
239-
int cipher_idx = find_cipher(sqlcipher_ltc_get_cipher(ctx));
237+
int cipher_idx = find_cipher(LTC_CIPHER);
240238
return cipher_descriptor[cipher_idx].block_length;
241239
}
242240

@@ -291,7 +289,6 @@ int sqlcipher_ltc_setup(sqlcipher_provider *p) {
291289
p->hmac = sqlcipher_ltc_hmac;
292290
p->kdf = sqlcipher_ltc_kdf;
293291
p->cipher = sqlcipher_ltc_cipher;
294-
p->set_cipher = sqlcipher_ltc_set_cipher;
295292
p->get_cipher = sqlcipher_ltc_get_cipher;
296293
p->get_key_sz = sqlcipher_ltc_get_key_sz;
297294
p->get_iv_sz = sqlcipher_ltc_get_iv_sz;

src/crypto_openssl.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ static int sqlcipher_openssl_add_random(void *ctx, void *buffer, int length) {
8585
return SQLITE_OK;
8686
}
8787

88+
#define OPENSSL_CIPHER "aes-256-cbc"
89+
90+
8891
/* activate and initialize sqlcipher. Most importantly, this will automatically
8992
intialize OpenSSL's EVP system if it hasn't already be externally. Note that
9093
this function may be called multiple times as new codecs are intiialized.
@@ -99,7 +102,7 @@ static int sqlcipher_openssl_activate(void *ctx) {
99102
sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MASTER));
100103
CODEC_TRACE_MUTEX("sqlcipher_openssl_activate: entered static master mutex");
101104

102-
if(openssl_init_count == 0 && EVP_get_cipherbyname(CIPHER) != NULL) {
105+
if(openssl_init_count == 0 && EVP_get_cipherbyname(OPENSSL_CIPHER) != NULL) {
103106
/* if openssl has not yet been initialized by this library, but
104107
a call to get_cipherbyname works, then the openssl library
105108
has been initialized externally already. */
@@ -265,15 +268,6 @@ static int sqlcipher_openssl_cipher(void *ctx, int mode, unsigned char *key, int
265268
return SQLITE_OK;
266269
}
267270

268-
static int sqlcipher_openssl_set_cipher(void *ctx, const char *cipher_name) {
269-
openssl_ctx *o_ctx = (openssl_ctx *)ctx;
270-
EVP_CIPHER* cipher = (EVP_CIPHER *) EVP_get_cipherbyname(cipher_name);
271-
if(cipher != NULL) {
272-
o_ctx->evp_cipher = cipher;
273-
}
274-
return cipher != NULL ? SQLITE_OK : SQLITE_ERROR;
275-
}
276-
277271
static const char* sqlcipher_openssl_get_cipher(void *ctx) {
278272
return EVP_CIPHER_name(((openssl_ctx *)ctx)->evp_cipher);
279273
}
@@ -316,10 +310,15 @@ static int sqlcipher_openssl_ctx_cmp(void *c1, void *c2) {
316310
}
317311

318312
static int sqlcipher_openssl_ctx_init(void **ctx) {
313+
openssl_ctx *o_ctx;
314+
319315
*ctx = sqlcipher_malloc(sizeof(openssl_ctx));
320316
if(*ctx == NULL) return SQLITE_NOMEM;
321317
sqlcipher_openssl_activate(*ctx);
322-
return SQLITE_OK;
318+
319+
o_ctx = (openssl_ctx *)*ctx;
320+
o_ctx->evp_cipher = (EVP_CIPHER *) EVP_get_cipherbyname(OPENSSL_CIPHER);
321+
return o_ctx->evp_cipher != NULL ? SQLITE_OK : SQLITE_ERROR;
323322
}
324323

325324
static int sqlcipher_openssl_ctx_free(void **ctx) {
@@ -344,7 +343,6 @@ int sqlcipher_openssl_setup(sqlcipher_provider *p) {
344343
p->hmac = sqlcipher_openssl_hmac;
345344
p->kdf = sqlcipher_openssl_kdf;
346345
p->cipher = sqlcipher_openssl_cipher;
347-
p->set_cipher = sqlcipher_openssl_set_cipher;
348346
p->get_cipher = sqlcipher_openssl_get_cipher;
349347
p->get_key_sz = sqlcipher_openssl_get_key_sz;
350348
p->get_iv_sz = sqlcipher_openssl_get_iv_sz;

src/sqlcipher.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ typedef struct {
6060
int (*hmac)(void *ctx, int algorithm, unsigned char *hmac_key, int key_sz, unsigned char *in, int in_sz, unsigned char *in2, int in2_sz, unsigned char *out);
6161
int (*kdf)(void *ctx, int algorithm, const unsigned char *pass, int pass_sz, unsigned char* salt, int salt_sz, int workfactor, int key_sz, unsigned char *key);
6262
int (*cipher)(void *ctx, int mode, unsigned char *key, int key_sz, unsigned char *iv, unsigned char *in, int in_sz, unsigned char *out);
63-
int (*set_cipher)(void *ctx, const char *cipher_name);
6463
const char* (*get_cipher)(void *ctx);
6564
int (*get_key_sz)(void *ctx);
6665
int (*get_iv_sz)(void *ctx);

test/crypto.test

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,19 @@ do_test verify-errors-for-rekey-kdf-and-cipher-changes {
611611
db close
612612
file delete -force test.db
613613

614+
615+
setup test.db "'testkey'"
616+
do_test verify-errors-for-cipher-change {
617+
sqlite_orig db test.db
618+
execsql {
619+
PRAGMA key = 'testkey';
620+
PRAGMA cipher = 'aes-256-ecb';
621+
}
622+
} {{PRAGMA cipher is no longer supported.}}
623+
db close
624+
file delete -force test.db
625+
626+
614627
# create an unencrypted database, attach a new encrypted volume
615628
# copy data between, verify the encypted database is good afterwards
616629
do_test unencrypted-attach {
@@ -981,7 +994,6 @@ do_test attached-database-pragmas {
981994
COMMIT;
982995
ATTACH DATABASE 'test2.db' AS db2 KEY 'testkey2';
983996
PRAGMA db2.cipher_page_size = 8192;
984-
PRAGMA db2.cipher = 'aes-128-cbc';
985997
PRAGMA db2.kdf_iter = 1000;
986998
PRAGMA db2.cipher_use_hmac = OFF;
987999
CREATE TABLE db2.t1(a,b);
@@ -994,12 +1006,11 @@ do_test attached-database-pragmas {
9941006
execsql {
9951007
PRAGMA key = 'testkey2';
9961008
PRAGMA cipher_page_size = 8192;
997-
PRAGMA cipher = 'aes-128-cbc';
9981009
PRAGMA kdf_iter = 1000;
9991010
PRAGMA cipher_use_hmac = OFF;
10001011
SELECT count(*) FROM t1;
10011012
}
1002-
} {{PRAGMA cipher command is deprecated, please remove from usage.} 1000}
1013+
} {1000}
10031014
db close
10041015
file delete -force test.db
10051016
file delete -force test2.db
@@ -1380,7 +1391,6 @@ do_test cipher-options-before-keys {
13801391
execsql {
13811392
PRAGMA kdf_iter = 1000;
13821393
PRAGMA cipher_page_size = 8192;
1383-
PRAGMA cipher = 'aes-128-cbc';
13841394
PRAGMA cipher_use_hmac = OFF;
13851395
PRAGMA key = 'testkey';
13861396
CREATE table t1(a,b);
@@ -1903,19 +1913,6 @@ if_built_with_openssl verify-pragma-cipher-default {
19031913
db close
19041914
file delete -force test.db
19051915

1906-
# verify the pragma cipher
1907-
# reports a change in value
1908-
if_built_with_openssl verify-pragma-cipher-changed {
1909-
sqlite_orig db test.db
1910-
execsql {
1911-
PRAGMA key = 'test';
1912-
PRAGMA cipher = 'AES-256-ECB';
1913-
PRAGMA cipher;
1914-
}
1915-
} {{PRAGMA cipher command is deprecated, please remove from usage.} AES-256-ECB}
1916-
db close
1917-
file delete -force test.db
1918-
19191916
# verify the pragma cipher_hmac_salt_mask reports default
19201917
do_test verify-pragma-hmac-salt-mask-reports-default {
19211918
sqlite_orig db test.db
@@ -2022,13 +2019,23 @@ do_test 2.0-beta-to-2.0-migration {
20222019
db close
20232020
file delete -force test.db
20242021

2022+
if_built_with_openssl verify-default-cipher {
2023+
sqlite_orig db test.db
2024+
execsql {
2025+
PRAGMA key='test';
2026+
PRAGMA cipher;
2027+
}
2028+
} {AES-256-CBC}
2029+
db close
2030+
file delete -force test.db
2031+
20252032
if_built_with_libtomcrypt verify-default-cipher {
20262033
sqlite_orig db test.db
20272034
execsql {
20282035
PRAGMA key='test';
20292036
PRAGMA cipher;
20302037
}
2031-
} {rijndael}
2038+
} {aes-256-cbc}
20322039
db close
20332040
file delete -force test.db
20342041

@@ -2276,27 +2283,6 @@ do_test attach_database_with_non_default_page_size {
22762283
db close
22772284
file delete -force test.db test2.db
22782285

2279-
if_built_with_openssl wont-write-database-with-invalid-cipher {
2280-
sqlite_orig db test.db
2281-
catchsql {
2282-
PRAGMA key = 'test';
2283-
PRAGMA cipher = 'foobar';
2284-
CREATE TABLE t1(a,b);
2285-
}
2286-
} {1 {SQL logic error}}
2287-
db close
2288-
file delete -force test.db
2289-
2290-
if_built_with_openssl wont-write-database-with-invalid-cipher-2 {
2291-
sqlite_orig db test.db
2292-
execsql {
2293-
PRAGMA key = 'test';
2294-
PRAGMA cipher = 'foobar';
2295-
}
2296-
} {{PRAGMA cipher command is deprecated, please remove from usage.}}
2297-
db close
2298-
file delete -force test.db
2299-
23002286
do_test verify-cipher-export-with-trace-configured {
23012287
sqlite_orig db plain.db
23022288
execsql {

0 commit comments

Comments
 (0)