Skip to content

Commit

Permalink
Fix issue #166, check parameter legacy_page_size
Browse files Browse the repository at this point in the history
- #166: Add missing SQLITE_PRIVATE attributes to several internal functions
- Add check for cipher configuration parameter legacy_page_size to accept only valid page sizes
  • Loading branch information
utelle committed Jun 4, 2024
1 parent 879f4c6 commit 56ac1e2
Show file tree
Hide file tree
Showing 23 changed files with 179 additions and 69 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Removed extern keyword in function declarations
- Cleaned up white space
- Added SQLITE_PRIVATE for several internal functions
- The cipher configuration parameter `legacy_page_size` now accepts only valid page sizes

### Fixed

- Fixed issue [#156](../../issues/156)) - corrupted database if MMAP_SIZE > 0 was used
- Fixed issue [#158](../../issues/158)) - add check to verify compatibility of source and target database in backup operation
- Fixed issue [#160](../../issues/160)) - fix accessing memory out of array bounds
- Fixed issue [#162](../../issues/162)) - fix loading/storing misaligned data
- Fixed issue [#166](../../issues/166)) - missing attribute SQLITE_PRIVATE for several internal functions

## [1.8.5] - 2024-05-24

### Changed
Expand Down
2 changes: 2 additions & 0 deletions src/ascon/aead.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ forceinline void ascon_final(ascon_state_t* s, const ascon_key_t* key) {
ascon_printstate("final 2nd key xor", s);
}

SQLITE_PRIVATE
int ascon_aead_encrypt(uint8_t* ctext,
uint8_t tag[ASCON_AEAD_TAG_LEN],
const uint8_t* mtext, uint64_t mlen,
Expand All @@ -220,6 +221,7 @@ int ascon_aead_encrypt(uint8_t* ctext,
return 0;
}

SQLITE_PRIVATE
int ascon_aead_decrypt(uint8_t* mtext,
const uint8_t* ctext, uint64_t clen,
const uint8_t* ad, uint64_t adlen,
Expand Down
6 changes: 6 additions & 0 deletions src/ascon/crypto_aead.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#ifndef CRYPTO_AEAD_H
#define CRYPTO_AEAD_H

#ifndef SQLITE_PRIVATE
#define SQLITE_PRIVATE
#endif

#include <stddef.h>

/*
Expand All @@ -27,6 +31,7 @@
** \param nonce Buffer with nonce data
** \param k Buffer with key data
*/
SQLITE_PRIVATE
int ascon_aead_encrypt(uint8_t* ctext, uint8_t tag[ASCON_AEAD_TAG_LEN],
const uint8_t* mtext, uint64_t mlen,
const uint8_t* ad, uint64_t adlen,
Expand All @@ -45,6 +50,7 @@ int ascon_aead_encrypt(uint8_t* ctext, uint8_t tag[ASCON_AEAD_TAG_LEN],
** \param nonce Buffer with nonce data
** \param k Buffer with key data
*/
SQLITE_PRIVATE
int ascon_aead_decrypt(uint8_t* mtext, const uint8_t* ctext, uint64_t clen,
const uint8_t* ad, uint64_t adlen,
const uint8_t tag[ASCON_AEAD_TAG_LEN],
Expand Down
5 changes: 5 additions & 0 deletions src/ascon/crypto_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#ifndef CRYPTO_HASH_H
#define CRYPTO_HASH_H

#ifndef SQLITE_PRIVATE
#define SQLITE_PRIVATE
#endif

#include <stddef.h>

/*
Expand All @@ -22,6 +26,7 @@
** \param in Buffer with input data
** \param passwordlen Length of input data in bytes
*/
SQLITE_PRIVATE
int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen);

#endif
5 changes: 5 additions & 0 deletions src/ascon/crypto_pbkdf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#ifndef ASCON_PBKDF2_H
#define ASCON_PBKDF2_H

#ifndef SQLITE_PRIVATE
#define SQLITE_PRIVATE
#endif

#include <stddef.h>

/*
Expand All @@ -35,6 +39,7 @@
** \param saltlen Number of bytes in the salt
** \param count Number of iterations to perform
*/
SQLITE_PRIVATE
void ascon_pbkdf2(uint8_t* out, uint32_t outlen,
const uint8_t* password, uint32_t passwordlen,
const uint8_t* salt, uint32_t saltlen, uint32_t count)
Expand Down
1 change: 1 addition & 0 deletions src/ascon/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ forceinline void ascon_squeeze(ascon_state_t* s, uint8_t* out,
ascon_printstate("squeeze output", s);
}

SQLITE_PRIVATE
int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen)
{
ascon_state_t s;
Expand Down
11 changes: 7 additions & 4 deletions src/ascon/pbkdf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define ASCON_HASH_SIZE 32
#define ASCON_PBKDF2_SIZE 32

SQLITE_PRIVATE
void ascon_pbkdf2_init(ascon_state_t* state, const char* functionName,
const unsigned char* custom, uint32_t customlen, uint32_t outlen)
{
Expand Down Expand Up @@ -60,10 +61,11 @@ void ascon_pbkdf2_init(ascon_state_t* state, const char* functionName,
* Note: Instead of HMAC like in RFC 8018, use the following PRF:
* PRF(P, X) = ASCON-cXOF(X, 256, "PBKDF2", P)
*/
static void ascon_pbkdf2_f(ascon_state_t* state,
uint8_t* T, /*uint8_t* U,*/
const uint8_t* salt, uint32_t saltlen,
uint32_t count, uint32_t blocknum)
static
void ascon_pbkdf2_f(ascon_state_t* state,
uint8_t* T, /*uint8_t* U,*/
const uint8_t* salt, uint32_t saltlen,
uint32_t count, uint32_t blocknum)
{
uint32_t asconSaltLen = (saltlen < ASCON_SALT_LEN) ? saltlen : ASCON_SALT_LEN;
uint8_t temp[ASCON_SALT_LEN+4];
Expand Down Expand Up @@ -109,6 +111,7 @@ static void ascon_pbkdf2_f(ascon_state_t* state,
sqlite3mcSecureZeroMemory(&state2, sizeof(ascon_state_t));
}

SQLITE_PRIVATE
void ascon_pbkdf2(uint8_t* out, uint32_t outlen,
const uint8_t* password, uint32_t passwordlen,
const uint8_t* salt, uint32_t saltlen, uint32_t count)
Expand Down
4 changes: 4 additions & 0 deletions src/chacha20poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ static void chacha20_block(uint32_t x[16])
#undef CC20QR
}

SQLITE_PRIVATE
void chacha20_xor(void* buffer, size_t n, const uint8_t key[32],
const uint8_t nonce[12], uint32_t counter)
{
Expand Down Expand Up @@ -141,6 +142,7 @@ void chacha20_xor(void* buffer, size_t n, const uint8_t key[32],
/*
* Poly1305 authentication tags
*/
SQLITE_PRIVATE
void poly1305(const uint8_t* msg, size_t n, const uint8_t key[32],
uint8_t tag[16])
{
Expand Down Expand Up @@ -210,6 +212,7 @@ void poly1305(const uint8_t* msg, size_t n, const uint8_t key[32],
s4 = d4; STORE32_LE(tag + 12, s4);
}

SQLITE_PRIVATE
int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
{
uint8_t d = 0;
Expand Down Expand Up @@ -405,6 +408,7 @@ static size_t entropy(void* buf, size_t n)
/*
* ChaCha20 random number generator
*/
SQLITE_PRIVATE
void chacha20_rng(void* out, size_t n)
{
static uint8_t key[32], nonce[12], buffer[64] = { 0 };
Expand Down
3 changes: 2 additions & 1 deletion src/cipher_chacha20.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ EncryptPageChaCha20Cipher(void* cipher, int page, unsigned char* data, int len,
return rc;
}

int chacha20_ismemset(const void* v, unsigned char value, int len)
static int
chacha20_ismemset(const void* v, unsigned char value, int len)
{
const unsigned char* a = v;
int i = 0, result = 0;
Expand Down
14 changes: 13 additions & 1 deletion src/cipher_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ sqlite3mc_cipher_name(int cipherIndex)
return cipherName;
}

static
int checkLegacyPageSize(const char* paramName, int pageSize)
{
int ok = 1;
if (sqlite3_stricmp(paramName, "legacy_page_size") == 0 && pageSize > 0)
{
ok = pageSize >= 512 && pageSize <= SQLITE_MAX_PAGE_SIZE && ((pageSize - 1) & pageSize) == 0;
}
return ok;
}

SQLITE_API int
sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramName, int newValue)
{
Expand Down Expand Up @@ -294,7 +305,8 @@ sqlite3mc_config_cipher(sqlite3* db, const char* cipherName, const char* paramNa
value = (hasDefaultPrefix) ? param->m_default : (hasMinPrefix) ? param->m_minValue : (hasMaxPrefix) ? param->m_maxValue : param->m_value;
if (!hasMinPrefix && !hasMaxPrefix)
{
if (newValue >= 0 && newValue >= param->m_minValue && newValue <= param->m_maxValue)
if (newValue >= 0 && newValue >= param->m_minValue && newValue <= param->m_maxValue &&
checkLegacyPageSize(paramName, newValue))
{
if (hasDefaultPrefix)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cipher_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ SQLITE_PRIVATE void sqlite3mcConfigTable(sqlite3_context* context, int argc, sql
SQLITE_PRIVATE CodecParameter* sqlite3mcGetCodecParams(sqlite3* db);

/* Forward declaration */
static unsigned char* sqlite3mcGetSaltWriteCipher(Codec* codec);
SQLITE_PRIVATE unsigned char* sqlite3mcGetSaltWriteCipher(Codec* codec);

SQLITE_PRIVATE void sqlite3mcCodecDataSql(sqlite3_context* context, int argc, sqlite3_value** argv);
SQLITE_PRIVATE void sqlite3mcConfigParams(sqlite3_context* context, int argc, sqlite3_value** argv);
Expand Down
4 changes: 4 additions & 0 deletions src/fastpbkdf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ DECL_PBKDF2(sha512,
sha512_extract,
sha512_xor)

SQLITE_PRIVATE
void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -402,6 +403,7 @@ void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
#endif
}

SQLITE_PRIVATE
void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -410,6 +412,7 @@ void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
PBKDF2(sha256)(pw, npw, salt, nsalt, iterations, out, nout);
}

SQLITE_PRIVATE
void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -418,6 +421,7 @@ void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
PBKDF2(sha512)(pw, npw, salt, nsalt, iterations, out, nout);
}

SQLITE_PRIVATE
void sqlcipher_hmac(int algorithm, unsigned char* key, int nkey, unsigned char* in, int in_sz, unsigned char* in2, int in2_sz, unsigned char* out)
{
switch (algorithm)
Expand Down
8 changes: 8 additions & 0 deletions src/fastpbkdf2.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
#ifndef FASTPBKDF2_H
#define FASTPBKDF2_H

#ifndef SQLITE_PRIVATE
#define SQLITE_PRIVATE
#endif

#include <stdlib.h>
#include "mystdint.h"

Expand All @@ -31,6 +35,7 @@ extern "C" {
*
* This function cannot fail; it does not report errors.
*/
SQLITE_PRIVATE
void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -45,6 +50,7 @@ void fastpbkdf2_hmac_sha1(const uint8_t *pw, size_t npw,
*
* This function cannot fail; it does not report errors.
*/
SQLITE_PRIVATE
void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -59,6 +65,7 @@ void fastpbkdf2_hmac_sha256(const uint8_t *pw, size_t npw,
*
* This function cannot fail; it does not report errors.
*/
SQLITE_PRIVATE
void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
const uint8_t *salt, size_t nsalt,
uint32_t iterations,
Expand All @@ -68,6 +75,7 @@ void fastpbkdf2_hmac_sha512(const uint8_t *pw, size_t npw,
*
* This function cannot fail; it does not report errors.
*/
SQLITE_PRIVATE
void sqlcipher_hmac(int algorithm,
unsigned char* key, int nkey,
unsigned char* in, int in_sz,
Expand Down
10 changes: 10 additions & 0 deletions src/rijndael.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,11 +1003,13 @@ static UINT32 rcon[30]=
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

SQLITE_PRIVATE
void RijndaelCreate(Rijndael* rijndael)
{
rijndael->m_state = RIJNDAEL_State_Invalid;
}

SQLITE_PRIVATE
int RijndaelInit(Rijndael* rijndael, int mode, int dir, UINT8* key, int keyLen, UINT8* initVector)
{
UINT32 uKeyLenInBytes;
Expand Down Expand Up @@ -1252,6 +1254,7 @@ int RijndaelBlockEncrypt(Rijndael* rijndael, UINT8* input, int inputLen, UINT8*
return 128 * numBlocks;
}

SQLITE_PRIVATE
int RijndaelPadEncrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer)
{
int i, numBlocks, padLen;
Expand Down Expand Up @@ -1310,6 +1313,7 @@ int RijndaelPadEncrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8
return 16*(numBlocks + 1);
}

SQLITE_PRIVATE
int RijndaelBlockDecrypt(Rijndael* rijndael, UINT8* input, int inputLen, UINT8* outBuffer)
{
int i, k, numBlocks, lenFrag;
Expand Down Expand Up @@ -1471,6 +1475,7 @@ int RijndaelBlockDecrypt(Rijndael* rijndael, UINT8* input, int inputLen, UINT8*
return 128*numBlocks;
}

SQLITE_PRIVATE
int RijndaelPadDecrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8 *outBuffer)
{
int i, numBlocks, padLen;
Expand Down Expand Up @@ -1549,6 +1554,7 @@ int RijndaelPadDecrypt(Rijndael* rijndael, UINT8 *input, int inputOctets, UINT8
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
*/

SQLITE_PRIVATE
void RijndaelKeySched(Rijndael* rijndael, UINT8 key[_MAX_KEY_COLUMNS][4])
{
int j,rconpointer = 0;
Expand Down Expand Up @@ -1629,6 +1635,7 @@ void RijndaelKeySched(Rijndael* rijndael, UINT8 key[_MAX_KEY_COLUMNS][4])
}
}

SQLITE_PRIVATE
void RijndaelKeyEncToDec(Rijndael* rijndael)
{
UINT32 r;
Expand All @@ -1647,6 +1654,7 @@ void RijndaelKeyEncToDec(Rijndael* rijndael)
}
}

SQLITE_PRIVATE
void RijndaelEncrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16])
{
UINT32 r;
Expand Down Expand Up @@ -1722,6 +1730,7 @@ void RijndaelEncrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16])
*((UINT32*)(b+12)) ^= *((UINT32*)rijndael->m_expandedKey[rijndael->m_uRounds][3]);
}

SQLITE_PRIVATE
void RijndaelDecrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16])
{
int r;
Expand Down Expand Up @@ -1798,6 +1807,7 @@ void RijndaelDecrypt(Rijndael* rijndael, UINT8 a[16], UINT8 b[16])
*((UINT32*)(b+12)) ^= *((UINT32*)rijndael->m_expandedKey[0][3]);
}

SQLITE_PRIVATE
void RijndaelInvalidate(Rijndael* rijndael)
{
rijndael->m_state = RIJNDAEL_State_Invalid;
Expand Down
Loading

0 comments on commit 56ac1e2

Please sign in to comment.