Replaced system SQLite with SQLCipher to support encrypted database

This commit is contained in:
Oleksii Zghurskyi
2025-06-07 18:11:17 +03:00
parent f4198d62a7
commit 177d74700f
534 changed files with 362771 additions and 21 deletions

View File

@@ -0,0 +1,120 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_decrypt.c
CFB implementation, decrypt data, Tom St Denis
*/
#ifdef LTC_CFB_MODE
#ifndef LTC_CFB_SHIFT1LEFT
#define LTC_CFB_SHIFT1LEFT
static LTC_INLINE void s_shift1left_64(unsigned char *b, unsigned char v)
{
ulong64 bval;
LOAD64H(bval, b);
bval <<= 1;
bval |= v & 0x01u;
STORE64H(bval, b);
}
static LTC_INLINE void s_shift1left_128(unsigned char *b, unsigned char v)
{
ulong64 bval[2];
LOAD64H(bval[0], b);
LOAD64H(bval[1], b + 8);
bval[0] <<= 1;
bval[0] |= (bval[1] >> 63) & 0x01u;
bval[1] <<= 1;
bval[1] |= v & 0x01u;
STORE64H(bval[0], b);
STORE64H(bval[1], b + 8);
}
#endif /* LTC_CFB_SHIFT1LEFT */
/**
CFB decrypt
@param ct Ciphertext
@param pt [out] Plaintext
@param len Length of ciphertext (octets)
@param cfb CFB state
@return CRYPT_OK if successful
*/
int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb)
{
int err;
ulong64 bitlen = len * 8, bits_per_round;
unsigned int cur_bit = 0;
unsigned char pt_ = 0, ct_ = 0;
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(cfb != NULL);
if (bitlen < len) {
return CRYPT_OVERFLOW;
}
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen/padlen valid? */
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
return CRYPT_INVALID_ARG;
}
bits_per_round = cfb->width == 1 ? 1 : 8;
while (bitlen > 0) {
if (cfb->padlen == cfb->blocklen) {
if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {
return err;
}
cfb->padlen = 0;
}
switch (cfb->width) {
case 1:
if (cur_bit++ % 8 == 0) {
ct_ = *ct++;
pt_ = 0;
} else {
ct_ <<= 1;
pt_ <<= 1;
}
if (cfb->blocklen == 16)
s_shift1left_128(cfb->pad, ct_ >> 7);
else
s_shift1left_64(cfb->pad, ct_ >> 7);
pt_ |= ((ct_ ^ cfb->IV[0]) >> 7) & 0x01u;
cfb->padlen = cfb->blocklen;
if (cur_bit % 8 == 0) {
*pt++ = pt_;
cur_bit = 0;
}
break;
case 8:
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1);
cfb->pad[cfb->blocklen - 1] = *ct;
*pt++ = *ct++ ^ cfb->IV[0];
cfb->padlen = cfb->blocklen;
break;
case 64:
case 128:
cfb->pad[cfb->padlen] = *ct;
*pt++ = *ct++ ^ cfb->IV[cfb->padlen];
++(cfb->padlen);
break;
default:
return CRYPT_INVALID_ARG;
}
bitlen -= bits_per_round;
}
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,30 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_done.c
CFB implementation, finish chain, Tom St Denis
*/
#ifdef LTC_CFB_MODE
/** Terminate the chain
@param cfb The CFB chain to terminate
@return CRYPT_OK on success
*/
int cfb_done(symmetric_CFB *cfb)
{
int err;
LTC_ARGCHK(cfb != NULL);
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
return err;
}
cipher_descriptor[cfb->cipher].done(&cfb->key);
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,119 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_encrypt.c
CFB implementation, encrypt data, Tom St Denis
*/
#ifdef LTC_CFB_MODE
#ifndef LTC_CFB_SHIFT1LEFT
#define LTC_CFB_SHIFT1LEFT
static LTC_INLINE void s_shift1left_64(unsigned char *b, unsigned char v)
{
ulong64 bval;
LOAD64H(bval, b);
bval <<= 1;
bval |= v & 0x01u;
STORE64H(bval, b);
}
static LTC_INLINE void s_shift1left_128(unsigned char *b, unsigned char v)
{
ulong64 bval[2];
LOAD64H(bval[0], b);
LOAD64H(bval[1], b + 8);
bval[0] <<= 1;
bval[0] |= (bval[1] >> 63) & 0x01u;
bval[1] <<= 1;
bval[1] |= v & 0x01u;
STORE64H(bval[0], b);
STORE64H(bval[1], b + 8);
}
#endif /* LTC_CFB_SHIFT1LEFT */
/**
CFB encrypt
@param pt Plaintext
@param ct [out] Ciphertext
@param len Length of plaintext (octets)
@param cfb CFB state
@return CRYPT_OK if successful
*/
int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb)
{
int err;
ulong64 bitlen = len * 8, bits_per_round;
unsigned int cur_bit = 0;
unsigned char pt_ = 0, ct_ = 0;
LTC_ARGCHK(pt != NULL);
LTC_ARGCHK(ct != NULL);
LTC_ARGCHK(cfb != NULL);
if (bitlen < len) {
return CRYPT_OVERFLOW;
}
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
return err;
}
/* is blocklen/padlen valid? */
if (cfb->blocklen < 0 || cfb->blocklen > (int)sizeof(cfb->IV) ||
cfb->padlen < 0 || cfb->padlen > (int)sizeof(cfb->pad)) {
return CRYPT_INVALID_ARG;
}
bits_per_round = cfb->width == 1 ? 1 : 8;
while (bitlen > 0) {
if (cfb->padlen == cfb->blocklen) {
if ((err = cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key)) != CRYPT_OK) {
return err;
}
cfb->padlen = 0;
}
switch (cfb->width) {
case 1:
if (cur_bit++ % 8 == 0) {
pt_ = *pt++;
ct_ = 0;
} else {
pt_ <<= 1;
ct_ <<= 1;
}
ct_ |= ((pt_ ^ cfb->IV[0]) >> 7) & 0x01u;
if (cfb->blocklen == 16)
s_shift1left_128(cfb->pad, ct_);
else
s_shift1left_64(cfb->pad, ct_);
cfb->padlen = cfb->blocklen;
if (cur_bit % 8 == 0) {
*ct++ = ct_;
cur_bit = 0;
}
break;
case 8:
XMEMMOVE(cfb->pad, cfb->pad + 1, cfb->blocklen - 1);
cfb->pad[cfb->blocklen - 1] = (*ct = *pt ^ cfb->IV[0]);
++pt;
++ct;
cfb->padlen = cfb->blocklen;
break;
case 64:
case 128:
cfb->pad[cfb->padlen] = (*ct = *pt ^ cfb->IV[cfb->padlen]);
++pt;
++ct;
++(cfb->padlen);
break;
}
bitlen -= bits_per_round;
}
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,34 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_getiv.c
CFB implementation, get IV, Tom St Denis
*/
#ifdef LTC_CFB_MODE
/**
Get the current initialization vector
@param IV [out] The destination of the initialization vector
@param len [in/out] The max size and resulting size of the initialization vector
@param cfb The CFB state
@return CRYPT_OK if successful
*/
int cfb_getiv(unsigned char *IV, unsigned long *len, const symmetric_CFB *cfb)
{
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(len != NULL);
LTC_ARGCHK(cfb != NULL);
if ((unsigned long)cfb->blocklen > *len) {
*len = cfb->blocklen;
return CRYPT_BUFFER_OVERFLOW;
}
XMEMCPY(IV, cfb->pad, cfb->blocklen);
*len = cfb->blocklen;
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,41 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_setiv.c
CFB implementation, set IV, Tom St Denis
*/
#ifdef LTC_CFB_MODE
/**
Set an initialization vector
@param IV The initialization vector
@param len The length of the vector (in octets)
@param cfb The CFB state
@return CRYPT_OK if successful
*/
int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb)
{
int err;
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(cfb != NULL);
if ((err = cipher_is_valid(cfb->cipher)) != CRYPT_OK) {
return err;
}
if (len != (unsigned long)cfb->blocklen) {
return CRYPT_INVALID_ARG;
}
/* force next block */
cfb->padlen = 0;
XMEMCPY(cfb->pad, IV, len);
return cipher_descriptor[cfb->cipher].ecb_encrypt(IV, cfb->IV, &cfb->key);
}
#endif

View File

@@ -0,0 +1,90 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file cfb_start.c
CFB implementation, start chain, Tom St Denis
*/
#ifdef LTC_CFB_MODE
/**
Extended initialization of a CFB context
@param cipher The index of the cipher desired
@param IV The initialization vector
@param key The secret key
@param keylen The length of the secret key (octets)
@param num_rounds Number of rounds in the cipher desired (0 for default)
@param width The width of the mode in bits (0 for default)
@param cfb The CFB state to initialize
@return CRYPT_OK if successful
*/
int cfb_start_ex(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, int width, symmetric_CFB *cfb)
{
int x, err;
LTC_ARGCHK(IV != NULL);
LTC_ARGCHK(key != NULL);
LTC_ARGCHK(cfb != NULL);
if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
return err;
}
switch (width) {
case 0:
width = cipher_descriptor[cipher].block_length * 8;
break;
case 1:
case 8:
LTC_ARGCHK(cipher_descriptor[cipher].block_length == 8
|| cipher_descriptor[cipher].block_length == 16);
break;
case 64:
case 128:
LTC_ARGCHK(width == cipher_descriptor[cipher].block_length * 8);
break;
default:
return CRYPT_INVALID_ARG;
}
/* copy data */
cfb->cipher = cipher;
cfb->width = width;
cfb->blocklen = cipher_descriptor[cipher].block_length;
for (x = 0; x < cfb->blocklen; x++) {
cfb->pad[x] = IV[x];
}
/* init the cipher */
if ((err = cipher_descriptor[cipher].setup(key, keylen, num_rounds, &cfb->key)) != CRYPT_OK) {
return err;
}
/* encrypt the IV */
cfb->padlen = 0;
return cipher_descriptor[cfb->cipher].ecb_encrypt(cfb->pad, cfb->IV, &cfb->key);
}
/**
Initialize a CFB context
@param cipher The index of the cipher desired
@param IV The initialization vector
@param key The secret key
@param keylen The length of the secret key (octets)
@param num_rounds Number of rounds in the cipher desired (0 for default)
@param cfb The CFB state to initialize
@return CRYPT_OK if successful
*/
int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key,
int keylen, int num_rounds, symmetric_CFB *cfb)
{
return cfb_start_ex(cipher, IV, key, keylen, num_rounds, 0, cfb);
}
#endif