Replaced system SQLite with SQLCipher to support encrypted database
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file der_decode_integer.c
|
||||
ASN.1 DER, decode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/**
|
||||
Read a mp_int integer
|
||||
@param in The DER encoded data
|
||||
@param inlen Size of DER encoded data
|
||||
@param num The first mp_int to decode
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_decode_integer(const unsigned char *in, unsigned long inlen, void *num)
|
||||
{
|
||||
unsigned long x, y;
|
||||
int err;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(in != NULL);
|
||||
|
||||
/* min DER INTEGER is 0x02 01 00 == 0 */
|
||||
if (inlen < (1 + 1 + 1)) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* ok expect 0x02 when we AND with 0001 1111 [1F] */
|
||||
x = 0;
|
||||
if ((in[x++] & 0x1F) != 0x02) {
|
||||
return CRYPT_INVALID_PACKET;
|
||||
}
|
||||
|
||||
/* get the length of the data */
|
||||
inlen -= x;
|
||||
if ((err = der_decode_asn1_length(in + x, &inlen, &y)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
x += inlen;
|
||||
|
||||
if ((err = ltc_mp_read_unsigned_bin(num, (unsigned char *)in + x, y)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* see if it's negative */
|
||||
if (in[x] & 0x80) {
|
||||
void *tmp;
|
||||
if (ltc_mp_init(&tmp) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
if (ltc_mp_2expt(tmp, ltc_mp_count_bits(num)) != CRYPT_OK || ltc_mp_sub(num, tmp, num) != CRYPT_OK) {
|
||||
ltc_mp_clear(tmp);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
ltc_mp_clear(tmp);
|
||||
}
|
||||
|
||||
return CRYPT_OK;
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,111 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wconversion"
|
||||
#pragma clang diagnostic ignored "-Wshorten-64-to-32"
|
||||
|
||||
/**
|
||||
@file der_encode_integer.c
|
||||
ASN.1 DER, encode an integer, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
|
||||
/* Exports a positive bignum as DER format (upto 2^32 bytes in size) */
|
||||
/**
|
||||
Store a mp_int integer
|
||||
@param num The first mp_int to encode
|
||||
@param out [out] The destination for the DER encoded integers
|
||||
@param outlen [in/out] The max size and resulting size of the DER encoded integers
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_encode_integer(void *num, unsigned char *out, unsigned long *outlen)
|
||||
{
|
||||
unsigned long tmplen, y, len;
|
||||
int err, leading_zero;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(out != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
/* find out how big this will be */
|
||||
if ((err = der_length_integer(num, &tmplen)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (*outlen < tmplen) {
|
||||
*outlen = tmplen;
|
||||
return CRYPT_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
if (ltc_mp_cmp_d(num, 0) != LTC_MP_LT) {
|
||||
/* we only need a leading zero if the msb of the first byte is one */
|
||||
if ((ltc_mp_count_bits(num) & 7) == 0 || ltc_mp_iszero(num) == LTC_MP_YES) {
|
||||
leading_zero = 1;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
}
|
||||
|
||||
/* get length of num in bytes (plus 1 since we force the msbyte to zero) */
|
||||
y = ltc_mp_unsigned_bin_size(num) + leading_zero;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
y = ltc_mp_count_bits(num);
|
||||
y = y + (8 - (y & 7));
|
||||
y = y >> 3;
|
||||
if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) --y;
|
||||
}
|
||||
|
||||
/* now store initial data */
|
||||
*out++ = 0x02;
|
||||
len = *outlen - 1;
|
||||
if ((err = der_encode_asn1_length(y, out, &len)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
out += len;
|
||||
|
||||
/* now store msbyte of zero if num is non-zero */
|
||||
if (leading_zero) {
|
||||
*out++ = 0x00;
|
||||
}
|
||||
|
||||
/* if it's not zero store it as big endian */
|
||||
if (ltc_mp_cmp_d(num, 0) == LTC_MP_GT) {
|
||||
/* now store the mpint */
|
||||
if ((err = ltc_mp_to_unsigned_bin(num, out)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
} else if (ltc_mp_iszero(num) != LTC_MP_YES) {
|
||||
void *tmp;
|
||||
|
||||
/* negative */
|
||||
if (ltc_mp_init(&tmp) != CRYPT_OK) {
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
|
||||
/* 2^roundup and subtract */
|
||||
y = ltc_mp_count_bits(num);
|
||||
y = y + (8 - (y & 7));
|
||||
if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) y -= 8;
|
||||
if (ltc_mp_2expt(tmp, y) != CRYPT_OK || ltc_mp_add(tmp, num, tmp) != CRYPT_OK) {
|
||||
ltc_mp_clear(tmp);
|
||||
return CRYPT_MEM;
|
||||
}
|
||||
if ((err = ltc_mp_to_unsigned_bin(tmp, out)) != CRYPT_OK) {
|
||||
ltc_mp_clear(tmp);
|
||||
return err;
|
||||
}
|
||||
ltc_mp_clear(tmp);
|
||||
}
|
||||
|
||||
/* we good */
|
||||
*outlen = tmplen;
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#pragma clang diagnostic pop
|
||||
@@ -0,0 +1,55 @@
|
||||
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
|
||||
/* SPDX-License-Identifier: Unlicense */
|
||||
#include "tomcrypt_private.h"
|
||||
|
||||
/**
|
||||
@file der_length_integer.c
|
||||
ASN.1 DER, get length of encoding, Tom St Denis
|
||||
*/
|
||||
|
||||
|
||||
#ifdef LTC_DER
|
||||
/**
|
||||
Gets length of DER encoding of num
|
||||
@param num The int to get the size of
|
||||
@param outlen [out] The length of the DER encoding for the given integer
|
||||
@return CRYPT_OK if successful
|
||||
*/
|
||||
int der_length_integer(void *num, unsigned long *outlen)
|
||||
{
|
||||
unsigned long z, len;
|
||||
int leading_zero, err;
|
||||
|
||||
LTC_ARGCHK(num != NULL);
|
||||
LTC_ARGCHK(outlen != NULL);
|
||||
|
||||
if (ltc_mp_cmp_d(num, 0) != LTC_MP_LT) {
|
||||
/* positive */
|
||||
|
||||
/* we only need a leading zero if the msb of the first byte is one */
|
||||
if ((ltc_mp_count_bits(num) & 7) == 0 || ltc_mp_iszero(num) == LTC_MP_YES) {
|
||||
leading_zero = 1;
|
||||
} else {
|
||||
leading_zero = 0;
|
||||
}
|
||||
|
||||
/* size for bignum */
|
||||
len = leading_zero + ltc_mp_unsigned_bin_size(num);
|
||||
} else {
|
||||
/* it's negative */
|
||||
/* find power of 2 that is a multiple of eight and greater than count bits */
|
||||
z = ltc_mp_count_bits(num);
|
||||
z = z + (8 - (z & 7));
|
||||
if (((ltc_mp_cnt_lsb(num)+1)==ltc_mp_count_bits(num)) && ((ltc_mp_count_bits(num)&7)==0)) --z;
|
||||
len = z >> 3;
|
||||
}
|
||||
|
||||
if ((err = der_length_asn1_length(len, &z)) != CRYPT_OK) {
|
||||
return err;
|
||||
}
|
||||
*outlen = 1 + z + len;
|
||||
|
||||
return CRYPT_OK;
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user