mirror of
https://github.com/status-im/sqlcipher.git
synced 2025-02-23 09:18:11 +00:00
takes advantage of sqlites new hexkey and hexrekey pragmas
- detect number of key bytes passed in to make decision about whether to hash key
This commit is contained in:
parent
cbbb7b2a89
commit
4314238203
@ -141,6 +141,7 @@ CRYPTOLIBOBJ = \
|
|||||||
crypto.lo
|
crypto.lo
|
||||||
|
|
||||||
CRYPTOSRC = \
|
CRYPTOSRC = \
|
||||||
|
$(TOP)/src/crypto.h \
|
||||||
$(TOP)/src/crypto.c
|
$(TOP)/src/crypto.c
|
||||||
# END CRYPTO
|
# END CRYPTO
|
||||||
|
|
||||||
|
20
src/crypto.c
20
src/crypto.c
@ -65,18 +65,11 @@ static int codec_passphrase_hash(const void *in, int inLen, void *out, int *outL
|
|||||||
*outLen = md_sz;
|
*outLen = md_sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int codec_prepare_key(sqlite3 *db, const void *zKey, int nKey, void *out, int *nOut) {
|
static void codec_prepare_key(sqlite3 *db, const void *zKey, int nKey, void *out, int *nOut) {
|
||||||
/* if key string starts with x' then assume this is a blob literal key*/
|
/* if key data lenght is exactly 256 bits / 32 bytes use the data directly */
|
||||||
if (sqlite3StrNICmp(zKey ,"x'", 2) == 0) {
|
if (nKey == 32) {
|
||||||
int n = nKey - 3; /* adjust for leading x' and tailing ' */
|
memcpy(out, zKey, nKey);
|
||||||
int half_n = n/2;
|
*nOut = nKey;
|
||||||
const char *z = zKey + 2; /* adjust lead offset of x' */
|
|
||||||
void *key = sqlite3HexToBlob(db, z, n);
|
|
||||||
memcpy(out, key, half_n);
|
|
||||||
*nOut = half_n;
|
|
||||||
|
|
||||||
memset(key, 0, half_n); /* cleanup temporary key data */
|
|
||||||
sqlite3DbFree(db, key);
|
|
||||||
/* otherwise the key is provided as a string so hash it to get key data */
|
/* otherwise the key is provided as a string so hash it to get key data */
|
||||||
} else {
|
} else {
|
||||||
codec_passphrase_hash(zKey, nKey, out, nOut);
|
codec_passphrase_hash(zKey, nKey, out, nOut);
|
||||||
@ -127,7 +120,8 @@ static int codec_cipher(codec_ctx *ctx, Pgno pgno, int mode, int size, void *in,
|
|||||||
csz += tmp_csz;
|
csz += tmp_csz;
|
||||||
EVP_CIPHER_CTX_cleanup(&ectx);
|
EVP_CIPHER_CTX_cleanup(&ectx);
|
||||||
assert(size == csz);
|
assert(size == csz);
|
||||||
|
|
||||||
|
return SQLITE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -110,7 +110,7 @@ do_test codec-1.6 {
|
|||||||
db close
|
db close
|
||||||
sqlite_orig db test.db
|
sqlite_orig db test.db
|
||||||
execsql {
|
execsql {
|
||||||
PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836489'";
|
PRAGMA hexkey = '98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836489';
|
||||||
SELECT * from t1;
|
SELECT * from t1;
|
||||||
}
|
}
|
||||||
} {test1 test2}
|
} {test1 test2}
|
||||||
@ -120,7 +120,7 @@ do_test codec-1.7 {
|
|||||||
db close
|
db close
|
||||||
sqlite_orig db test.db
|
sqlite_orig db test.db
|
||||||
catchsql {
|
catchsql {
|
||||||
PRAGMA key = "x'98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480'";
|
PRAGMA hexkey = '98483C6EB40B6C31A448C22A66DED3B5E5E8D5119CAC8327B655C8B5C4836480';
|
||||||
SELECT name FROM sqlite_master WHERE type='table';
|
SELECT name FROM sqlite_master WHERE type='table';
|
||||||
}
|
}
|
||||||
} {1 {file is encrypted or is not a database}}
|
} {1 {file is encrypted or is not a database}}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user