Replaced system SQLite with SQLCipher to support encrypted database
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file der_decode_object_identifier.c
|
||||
ASN.1 DER, Decode Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Decode OID data and store the array of integers in words
|
||||
@param in The OID DER encoded data
|
||||
@param inlen The length of the OID data
|
||||
@param words [out] The destination of the OID words
|
||||
@param outlen [in/out] The number of OID words
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_object_identifier(const unsigned char *in, unsigned long inlen,
|
||||
unsigned long *words, unsigned long *outlen)
|
||||
{
|
||||
unsigned long x, y, t, len;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(in != NULL);
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* header is at least 3 bytes */
|
||||
if (inlen < 3) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* must be room for at least two words */
|
||||
if (*outlen < 2) {
|
||||
*outlen = 2;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* decode the packet header */
|
||||
x = 0;
|
||||
if ((in[x++] & 0x1F) != 0x06) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get the length of the data */
|
||||
y = inlen - x;
|
||||
if ((err = der_decode_asn1_length(in + x, &y, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
x += y;
|
||||
|
||||
if ((len == 0) || (len > (inlen - x))) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* decode words */
|
||||
y = 0;
|
||||
t = 0;
|
||||
while (len--) {
|
||||
t = (t << 7) | (in[x] & 0x7F);
|
||||
if (!(in[x++] & 0x80)) {
|
||||
/* store t */
|
||||
if (y >= *outlen) {
|
||||
y++;
|
||||
} else {
|
||||
if (y == 0) {
|
||||
if (t <= 79) {
|
||||
words[0] = t / 40;
|
||||
words[1] = t % 40;
|
||||
} else {
|
||||
words[0] = 2;
|
||||
words[1] = t - 80;
|
||||
}
|
||||
y = 2;
|
||||
} else {
|
||||
words[y++] = t;
|
||||
}
|
||||
}
|
||||
t = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (y > *outlen) {
|
||||
err = CRYPT_BUFFER_OVERFLOW;
|
||||
} else {
|
||||
err = CRYPT_OK;
|
||||
}
|
||||
|
||||
*outlen = y;
|
||||
return err;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file der_encode_object_identifier.c
|
||||
ASN.1 DER, Encode Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Encode an OID
|
||||
@param words The words to encode (upto 32-bits each)
|
||||
@param nwords The number of words in the OID
|
||||
@param out [out] Destination of OID data
|
||||
@param outlen [in/out] The max and resulting size of the OID
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_object_identifier(const unsigned long *words, unsigned long nwords,
|
||||
unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long i, x, y, z, t, mask, wordbuf;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* check length */
|
||||
if ((err = der_length_object_identifier(words, nwords, &x)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
if (x > *outlen) {
|
||||
*outlen = x;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
/* compute length to store OID data */
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
wordbuf = words[y + 1];
|
||||
}
|
||||
}
|
||||
|
||||
/* store header + length */
|
||||
x = 0;
|
||||
out[x++] = 0x06;
|
||||
y = *outlen - x;
|
||||
if ((err = der_encode_asn1_length(z, out + x, &y)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
x += y;
|
||||
|
||||
/* store first byte */
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (i = 1; i < nwords; i++) {
|
||||
/* store 7 bit words in little endian */
|
||||
t = wordbuf & 0xFFFFFFFF;
|
||||
if (t) {
|
||||
y = x;
|
||||
mask = 0;
|
||||
while (t) {
|
||||
out[x++] = (unsigned char)((t & 0x7F) | mask);
|
||||
t >>= 7;
|
||||
mask |= 0x80; /* upper bit is set on all but the last byte */
|
||||
}
|
||||
/* now swap bytes y...x-1 */
|
||||
z = x - 1;
|
||||
while (y < z) {
|
||||
t = out[y]; out[y] = out[z]; out[z] = (unsigned char)t;
|
||||
++y;
|
||||
--z;
|
||||
}
|
||||
} else {
|
||||
/* zero word */
|
||||
out[x++] = 0x00;
|
||||
}
|
||||
|
||||
if (i < nwords - 1) {
|
||||
wordbuf = words[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
*outlen = x;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file der_length_object_identifier.c
|
||||
ASN.1 DER, get length of Object Identifier, Tom St Denis
|
||||
*/
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
unsigned long der_object_identifier_bits(unsigned long x)
|
||||
{
|
||||
unsigned long c;
|
||||
x &= 0xFFFFFFFF;
|
||||
c = 0;
|
||||
while (x) {
|
||||
++c;
|
||||
x >>= 1;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
Gets length of DER encoding of Object Identifier
|
||||
@param nwords The number of OID words
|
||||
@param words The actual OID words to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given string
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_object_identifier(const unsigned long *words, unsigned long nwords, unsigned long *outlen)
|
||||
{
|
||||
unsigned long y, z, t, wordbuf;
|
||||
|
||||
LTC_ARGCHK(words != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
|
||||
/* must be >= 2 words */
|
||||
if (nwords < 2) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* word1 = 0,1,2 and word2 0..39 */
|
||||
if (words[0] > 2 || (words[0] < 2 && words[1] > 39)) {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
/* leading word is the first two */
|
||||
z = 0;
|
||||
wordbuf = words[0] * 40 + words[1];
|
||||
for (y = 1; y < nwords; y++) {
|
||||
t = der_object_identifier_bits(wordbuf);
|
||||
z += t/7 + ((t%7) ? 1 : 0) + (wordbuf == 0 ? 1 : 0);
|
||||
if (y < nwords - 1) {
|
||||
/* grab next word */
|
||||
wordbuf = words[y+1];
|
||||
}
|
||||
}
|
||||
|
||||
/* now depending on the length our length encoding changes */
|
||||
if (z < 128) {
|
||||
z += 2;
|
||||
} else if (z < 256) {
|
||||
z += 3;
|
||||
} else if (z < 65536UL) {
|
||||
z += 4;
|
||||
} else {
|
||||
return CRYPT_INVALID_ARG;
|
||||
}
|
||||
|
||||
*outlen = z;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user