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,31 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_export.c
Export an Ed25519 key to a binary packet, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
/**
Export an Ed25519 key to a binary packet
@param out [out] The destination for the key
@param outlen [in/out] The max size and resulting size of the Ed25519 key
@param type Which type of key (PK_PRIVATE, PK_PUBLIC|PK_STD or PK_PUBLIC)
@param key The key you wish to export
@return CRYPT_OK if successful
*/
int ed25519_export( unsigned char *out, unsigned long *outlen,
int which,
const curve25519_key *key)
{
LTC_ARGCHK(key != NULL);
if (key->pka != LTC_PKA_ED25519) return CRYPT_PK_INVALID_TYPE;
return ec25519_export(out, outlen, which, key);
}
#endif

View File

@@ -0,0 +1,35 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_import.c
Import a Ed25519 key from a SubjectPublicKeyInfo, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
/**
Import an Ed25519 public key
@param in The packet to read
@param inlen The length of the input packet
@param key [out] Where to import the key to
@return CRYPT_OK if successful, on error all allocated memory is freed automatically
*/
int ed25519_import(const unsigned char *in, unsigned long inlen, curve25519_key *key)
{
int err;
unsigned long key_len;
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(key != NULL);
key_len = sizeof(key->pub);
if ((err = x509_decode_subject_public_key_info(in, inlen, LTC_OID_ED25519, key->pub, &key_len, LTC_ASN1_EOL, NULL, 0uL)) == CRYPT_OK) {
key->type = PK_PUBLIC;
key->pka = LTC_PKA_ED25519;
}
return err;
}
#endif

View File

@@ -0,0 +1,33 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_import_pkcs8.c
Import an Ed25519 key in PKCS#8 format, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
int ed25519_import_pkcs8_asn1(ltc_asn1_list *alg_id, ltc_asn1_list *priv_key,
curve25519_key *key)
{
return ec25519_import_pkcs8_asn1(alg_id, priv_key, LTC_OID_ED25519, key);
}
/**
Import an Ed25519 private key in PKCS#8 format
@param in The packet to import from
@param inlen It's length (octets)
@param pw_ctx The password context when decrypting the private key
@param key [out] Destination for newly imported key
@return CRYPT_OK if successful, on error all allocated memory is freed automatically
*/
int ed25519_import_pkcs8(const unsigned char *in, unsigned long inlen,
const password_ctx *pw_ctx,
curve25519_key *key)
{
return ec25519_import_pkcs8(in, inlen, pw_ctx, LTC_OID_ED25519, key);
}
#endif

View File

@@ -0,0 +1,46 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_import_raw.c
Set the parameters of an Ed25519 key, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
/**
Set the parameters of an Ed25519 key
@param in The key
@param inlen The length of the key
@param which Which type of key (PK_PRIVATE or PK_PUBLIC)
@param key [out] Destination of the key
@return CRYPT_OK if successful
*/
int ed25519_import_raw(const unsigned char *in, unsigned long inlen, int which, curve25519_key *key)
{
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(key != NULL);
if (which == PK_PRIVATE) {
LTC_ARGCHK(inlen == 32uL || inlen == 64uL);
XMEMCPY(key->priv, in, sizeof(key->priv));
if (inlen == 32) {
tweetnacl_crypto_sk_to_pk(key->pub, key->priv);
} else {
XMEMCPY(key->pub, in + 32, sizeof(key->pub));
}
} else if (which == PK_PUBLIC) {
LTC_ARGCHK(inlen == 32uL);
XMEMCPY(key->pub, in, sizeof(key->pub));
} else {
return CRYPT_INVALID_ARG;
}
key->pka = LTC_PKA_ED25519;
key->type = which;
return CRYPT_OK;
}
#endif

View File

@@ -0,0 +1,45 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_import_x509.c
Import an Ed25519 key from a X.509 certificate, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
static int s_ed25519_decode(const unsigned char *in, unsigned long inlen, curve25519_key *key)
{
if (inlen != sizeof(key->pub)) return CRYPT_PK_INVALID_SIZE;
XMEMCPY(key->pub, in, sizeof(key->pub));
return CRYPT_OK;
}
/**
Import an Ed25519 public key from a X.509 certificate
@param in The DER encoded X.509 certificate
@param inlen The length of the certificate
@param key [out] Where to import the key to
@return CRYPT_OK if successful, on error all allocated memory is freed automatically
*/
int ed25519_import_x509(const unsigned char *in, unsigned long inlen, curve25519_key *key)
{
int err;
LTC_ARGCHK(in != NULL);
LTC_ARGCHK(key != NULL);
if ((err = x509_decode_public_key_from_certificate(in, inlen,
LTC_OID_ED25519,
LTC_ASN1_EOL, NULL, NULL,
(public_key_decode_cb)s_ed25519_decode, key)) != CRYPT_OK) {
return err;
}
key->type = PK_PUBLIC;
key->pka = LTC_PKA_ED25519;
return err;
}
#endif

View File

@@ -0,0 +1,36 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_make_key.c
Create an Ed25519 key, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
/**
Create an Ed25519 key
@param prng An active PRNG state
@param wprng The index of the PRNG desired
@param key [out] Destination of a newly created private key pair
@return CRYPT_OK if successful
*/
int ed25519_make_key(prng_state *prng, int wprng, curve25519_key *key)
{
int err;
LTC_ARGCHK(prng != NULL);
LTC_ARGCHK(key != NULL);
if ((err = tweetnacl_crypto_sign_keypair(prng, wprng, key->pub, key->priv)) != CRYPT_OK) {
return err;
}
key->type = PK_PRIVATE;
key->pka = LTC_PKA_ED25519;
return err;
}
#endif

View File

@@ -0,0 +1,126 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_shared_secret.c
Create an Ed25519 signature, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
static int s_ed25519_sign(const unsigned char *msg, unsigned long msglen,
unsigned char *sig, unsigned long *siglen,
const unsigned char *ctx, unsigned long ctxlen,
const curve25519_key *private_key)
{
unsigned char *s;
unsigned long long smlen;
int err;
LTC_ARGCHK(msg != NULL);
LTC_ARGCHK(sig != NULL);
LTC_ARGCHK(siglen != NULL);
LTC_ARGCHK(private_key != NULL);
if (private_key->pka != LTC_PKA_ED25519) return CRYPT_PK_INVALID_TYPE;
if (private_key->type != PK_PRIVATE) return CRYPT_PK_INVALID_TYPE;
if (*siglen < 64uL) {
*siglen = 64uL;
return CRYPT_BUFFER_OVERFLOW;
}
smlen = msglen + 64;
s = XMALLOC(smlen);
if (s == NULL) return CRYPT_MEM;
err = tweetnacl_crypto_sign(s, &smlen,
msg, msglen,
private_key->priv, private_key->pub,
ctx, ctxlen);
XMEMCPY(sig, s, 64uL);
*siglen = 64uL;
#ifdef LTC_CLEAN_STACK
zeromem(s, smlen);
#endif
XFREE(s);
return err;
}
/**
Create an Ed25519ctx signature.
@param msg The data to be signed
@param msglen [in] The size of the date to be signed
@param sig [out] The destination of the shared data
@param siglen [in/out] The max size and resulting size of the shared data.
@param ctx [in] The context is a constant null terminated string
@param private_key The private Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519ctx_sign(const unsigned char *msg, unsigned long msglen,
unsigned char *sig, unsigned long *siglen,
const unsigned char *ctx, unsigned long ctxlen,
const curve25519_key *private_key)
{
int err;
unsigned char ctx_prefix[292];
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
LTC_ARGCHK(ctx != NULL);
if ((err = ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 0, ctx, ctxlen)) != CRYPT_OK)
return err;
return s_ed25519_sign(msg, msglen, sig, siglen, ctx_prefix, ctx_prefix_size, private_key);
}
/**
Create an Ed25519ph signature.
@param msg The data to be signed
@param msglen [in] The size of the date to be signed
@param sig [out] The destination of the shared data
@param siglen [in/out] The max size and resulting size of the shared data.
@param ctx [in] The context is a constant null terminated string
@param private_key The private Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519ph_sign(const unsigned char *msg, unsigned long msglen,
unsigned char *sig, unsigned long *siglen,
const unsigned char *ctx, unsigned long ctxlen,
const curve25519_key *private_key)
{
int err;
unsigned char msg_hash[64];
unsigned char ctx_prefix[292];
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
if ((err = ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 1, ctx, ctxlen)) != CRYPT_OK)
return err;
if ((err = tweetnacl_crypto_ph(msg_hash, msg, msglen)) != CRYPT_OK)
return err;
return s_ed25519_sign(msg_hash, sizeof(msg_hash), sig, siglen, ctx_prefix, ctx_prefix_size, private_key);
}
/**
Create an Ed25519 signature.
@param msg The data to be signed
@param msglen [in] The size of the date to be signed
@param sig [out] The destination of the shared data
@param siglen [in/out] The max size and resulting size of the shared data.
@param private_key The private Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519_sign(const unsigned char *msg, unsigned long msglen,
unsigned char *sig, unsigned long *siglen,
const curve25519_key *private_key)
{
return s_ed25519_sign(msg, msglen, sig, siglen, NULL, 0, private_key);
}
#endif

View File

@@ -0,0 +1,134 @@
/* LibTomCrypt, modular cryptographic library -- Tom St Denis */
/* SPDX-License-Identifier: Unlicense */
#include "tomcrypt_private.h"
/**
@file ed25519_verify.c
Verify an Ed25519 signature, Steffen Jaeckel
*/
#ifdef LTC_CURVE25519
static int s_ed25519_verify(const unsigned char *msg, unsigned long msglen,
const unsigned char *sig, unsigned long siglen,
const unsigned char *ctx, unsigned long ctxlen,
int *stat,
const curve25519_key *public_key)
{
unsigned char* m;
unsigned long long mlen;
int err;
LTC_ARGCHK(msg != NULL);
LTC_ARGCHK(sig != NULL);
LTC_ARGCHK(stat != NULL);
LTC_ARGCHK(public_key != NULL);
*stat = 0;
if (siglen != 64uL) return CRYPT_INVALID_ARG;
if (public_key->pka != LTC_PKA_ED25519) return CRYPT_PK_INVALID_TYPE;
mlen = msglen + siglen;
if ((mlen < msglen) || (mlen < siglen)) return CRYPT_OVERFLOW;
m = XMALLOC(mlen);
if (m == NULL) return CRYPT_MEM;
XMEMCPY(m, sig, siglen);
XMEMCPY(m + siglen, msg, msglen);
err = tweetnacl_crypto_sign_open(stat,
m, &mlen,
m, mlen,
ctx, ctxlen,
public_key->pub);
#ifdef LTC_CLEAN_STACK
zeromem(m, msglen + siglen);
#endif
XFREE(m);
return err;
}
/**
Verify an Ed25519ctx signature.
@param msg [in] The data to be verified
@param msglen [in] The size of the data to be verified
@param sig [in] The signature to be verified
@param siglen [in] The size of the signature to be verified
@param ctx [in] The context
@param ctxlen [in] The size of the context
@param stat [out] The result of the signature verification, 1==valid, 0==invalid
@param public_key [in] The public Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519ctx_verify(const unsigned char *msg, unsigned long msglen,
const unsigned char *sig, unsigned long siglen,
const unsigned char *ctx, unsigned long ctxlen,
int *stat,
const curve25519_key *public_key)
{
unsigned char ctx_prefix[292];
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
LTC_ARGCHK(ctx != NULL);
if (ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 0, ctx, ctxlen) != CRYPT_OK)
return CRYPT_INVALID_ARG;
return s_ed25519_verify(msg, msglen, sig, siglen, ctx_prefix, ctx_prefix_size, stat, public_key);
}
/**
Verify an Ed25519ph signature.
@param msg [in] The data to be verified
@param msglen [in] The size of the data to be verified
@param sig [in] The signature to be verified
@param siglen [in] The size of the signature to be verified
@param ctx [in] The context
@param ctxlen [in] The size of the context
@param stat [out] The result of the signature verification, 1==valid, 0==invalid
@param public_key [in] The public Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519ph_verify(const unsigned char *msg, unsigned long msglen,
const unsigned char *sig, unsigned long siglen,
const unsigned char *ctx, unsigned long ctxlen,
int *stat,
const curve25519_key *public_key)
{
int err;
unsigned char msg_hash[64];
unsigned char ctx_prefix[292];
unsigned long ctx_prefix_size = sizeof(ctx_prefix);
if ((err = ec25519_crypto_ctx(ctx_prefix, &ctx_prefix_size, 1, ctx, ctxlen)) != CRYPT_OK)
return err;
if ((err = tweetnacl_crypto_ph(msg_hash, msg, msglen)) != CRYPT_OK)
return err;
return s_ed25519_verify(msg_hash, sizeof(msg_hash), sig, siglen, ctx_prefix, ctx_prefix_size, stat, public_key);
}
/**
Verify an Ed25519 signature.
@param msg [in] The data to be verified
@param msglen [in] The size of the data to be verified
@param sig [in] The signature to be verified
@param siglen [in] The size of the signature to be verified
@param stat [out] The result of the signature verification, 1==valid, 0==invalid
@param public_key [in] The public Ed25519 key in the pair
@return CRYPT_OK if successful
*/
int ed25519_verify(const unsigned char *msg, unsigned long msglen,
const unsigned char *sig, unsigned long siglen,
int *stat,
const curve25519_key *public_key)
{
return s_ed25519_verify(msg, msglen, sig, siglen, NULL, 0, stat, public_key);
}
#endif