33** Purpose: Amalgamation of the SQLite3 Multiple Ciphers encryption extension for SQLite
44** Author: Ulrich Telle
55** Created: 2020-02-28
6- ** Copyright: (c) 2006-2020 Ulrich Telle
6+ ** Copyright: (c) 2006-2021 Ulrich Telle
77** License: MIT
88*/
99
@@ -50,16 +50,32 @@ void sqlite3mc_shutdown(void);
5050#endif
5151
5252#if defined(_WIN32) || defined(WIN32)
53+
54+ #ifndef SQLITE3MC_USE_RAND_S
55+ #define SQLITE3MC_USE_RAND_S 1
56+ #endif
57+
58+ #if SQLITE3MC_USE_RAND_S
59+ /* Force header stdlib.h to define rand_s() */
60+ #if !defined(_CRT_RAND_S)
61+ #define _CRT_RAND_S
62+ #endif
63+ #endif
64+
65+ #ifndef SQLITE_API
66+ #define SQLITE_API
67+ #endif
68+
5369#include <windows.h>
5470
5571/* SQLite functions only needed on Win32 */
56- extern void sqlite3_win32_write_debug(const char*, int);
57- extern char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
58- extern char *sqlite3_win32_mbcs_to_utf8(const char*);
59- extern char *sqlite3_win32_mbcs_to_utf8_v2(const char*, int);
60- extern char *sqlite3_win32_utf8_to_mbcs(const char*);
61- extern char *sqlite3_win32_utf8_to_mbcs_v2(const char*, int);
62- extern LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
72+ extern SQLITE_API void sqlite3_win32_write_debug(const char*, int);
73+ extern SQLITE_API char *sqlite3_win32_unicode_to_utf8(LPCWSTR);
74+ extern SQLITE_API char *sqlite3_win32_mbcs_to_utf8(const char*);
75+ extern SQLITE_API char *sqlite3_win32_mbcs_to_utf8_v2(const char*, int);
76+ extern SQLITE_API char *sqlite3_win32_utf8_to_mbcs(const char*);
77+ extern SQLITE_API char *sqlite3_win32_utf8_to_mbcs_v2(const char*, int);
78+ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
6379#endif
6480
6581/*
@@ -11027,6 +11043,8 @@ SQLITE_API int sqlite3_deserialize(
1102711043SQLITE_PRIVATE int sqlite3mcFileControlPragma(sqlite3*, const char*, int, void*);
1102811044SQLITE_PRIVATE int sqlite3mcHandleAttachKey(sqlite3*, const char*, const char*, sqlite3_value*, char**);
1102911045SQLITE_PRIVATE int sqlite3mcHandleMainKey(sqlite3*, const char*);
11046+ typedef struct PgHdr PgHdr;
11047+ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdr* pPg);
1103011048
1103111049/******** Begin file sqlite3rtree.h *********/
1103211050/*
@@ -63362,7 +63380,7 @@ static int walWriteOneFrame(
6336263380 int rc; /* Result code from subfunctions */
6336363381 void *pData; /* Data actually written */
6336463382 u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */
63365- pData = pPage->pData ;
63383+ if( ( pData = sqlite3mcPagerCodec( pPage))==0 ) return SQLITE_NOMEM_BKPT ;
6336663384 walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
6336763385 rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
6336863386 if( rc ) return rc;
@@ -63545,7 +63563,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(
6354563563 if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
6354663564 pWal->iReCksum = iWrite;
6354763565 }
63548- pData = p->pData ;
63566+ if( ( pData = sqlite3mcPagerCodec(p))==0 ) return SQLITE_NOMEM ;
6354963567 rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
6355063568 if( rc ) return rc;
6355163569 p->flags &= ~PGHDR_WAL_APPEND;
@@ -246921,10 +246939,10 @@ SQLITE_API void sqlite3mc_vfs_shutdown();
246921246939#define SQLITE3MC_VERSION_H_
246922246940
246923246941#define SQLITE3MC_VERSION_MAJOR 1
246924- #define SQLITE3MC_VERSION_MINOR 2
246925- #define SQLITE3MC_VERSION_RELEASE 5
246942+ #define SQLITE3MC_VERSION_MINOR 3
246943+ #define SQLITE3MC_VERSION_RELEASE 0
246926246944#define SQLITE3MC_VERSION_SUBRELEASE 0
246927- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.2.5 "
246945+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.3.0 "
246928246946
246929246947#endif /* SQLITE3MC_VERSION_H_ */
246930246948/*** End of #include "sqlite3mc_version.h" ***/
@@ -249559,6 +249577,48 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
249559249577 * Platform-specific entropy functions for seeding RNG
249560249578 */
249561249579#if defined(_WIN32) || defined(__CYGWIN__)
249580+
249581+ #if SQLITE3MC_USE_RAND_S
249582+
249583+ /* Force header stdlib.h to define rand_s() */
249584+ #if !defined(_CRT_RAND_S)
249585+ #define _CRT_RAND_S
249586+ #endif
249587+ #include <stdlib.h>
249588+
249589+ /*
249590+ Provide declaration of rand_s() for MinGW-32 (not 64).
249591+ MinGW-32 didn't declare it prior to version 5.3.0.
249592+ */
249593+ #if defined(__MINGW32__) && defined(__MINGW32_VERSION) && __MINGW32_VERSION < 5003000L && !defined(__MINGW64_VERSION_MAJOR)
249594+ __declspec(dllimport) int rand_s(unsigned int *);
249595+ #endif
249596+
249597+ static size_t entropy(void* buf, size_t n)
249598+ {
249599+ size_t totalBytes = 0;
249600+ while (totalBytes < n)
249601+ {
249602+ unsigned int random32 = 0;
249603+ size_t j = 0;
249604+
249605+ if (rand_s(&random32))
249606+ {
249607+ /* rand_s failed */
249608+ return 0;
249609+ }
249610+
249611+ for (; (j < sizeof(random32)) && (totalBytes < n); j++, totalBytes++)
249612+ {
249613+ const uint8_t random8 = (uint8_t)(random32 >> (j * 8));
249614+ ((uint8_t*) buf)[totalBytes] = random8;
249615+ }
249616+ }
249617+ return n;
249618+ }
249619+
249620+ #else
249621+
249562249622#include <windows.h>
249563249623#define RtlGenRandom SystemFunction036
249564249624BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
@@ -249567,7 +249627,11 @@ static size_t entropy(void* buf, size_t n)
249567249627{
249568249628 return RtlGenRandom(buf, (ULONG) n) ? n : 0;
249569249629}
249630+
249631+ #endif
249632+
249570249633#elif defined(__linux__) || defined(__unix__) || defined(__APPLE__)
249634+
249571249635#ifndef _GNU_SOURCE
249572249636#define _GNU_SOURCE
249573249637#endif
@@ -250497,7 +250561,7 @@ static int
250497250561aesHardwareCheck()
250498250562{
250499250563 unsigned int CPUInfo[4];
250500- __cpuid(CPUInfo, 1);
250564+ __cpuid((int*) CPUInfo, 1);
250501250565 return (CPUInfo[2] & (1 << 25)) != 0 && (CPUInfo[2] & (1 << 19)) != 0; /* Check AES and SSE4.1 */
250502250566}
250503250567
@@ -256488,7 +256552,6 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
256488256552 if (cipherParams != NULL)
256489256553 {
256490256554 const char* cipherName = globalCodecParameterTable[j].m_name;
256491- int j;
256492256555 for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
256493256556 {
256494256557 if (sqlite3_stricmp(pragmaName, cipherParams[j].m_name) == 0) break;
@@ -257001,6 +257064,11 @@ mcReportCodecError(BtShared* pBt, int error)
257001257064 pBt->pPager->errCode = error;
257002257065 setGetterMethod(pBt->pPager);
257003257066 pBt->db->errCode = error;
257067+ if (error == SQLITE_OK)
257068+ {
257069+ /* Clear cache to force reread of database after a new passphrase has been set */
257070+ sqlite3PagerClearCache(pBt->pPager);
257071+ }
257004257072}
257005257073
257006257074/*
@@ -257088,7 +257156,7 @@ mcAdjustBtree(Btree* pBt, int nPageSize, int nReserved, int isLegacy)
257088257156 }
257089257157
257090257158 /* Adjust the page size and the reserved area */
257091- if (pager->nReserve != nReserved)
257159+ if (pager->pageSize != pagesize || pager-> nReserve != nReserved)
257092257160 {
257093257161 if (isLegacy != 0)
257094257162 {
@@ -267537,6 +267605,37 @@ SQLITE_PRIVATE void sqlite3mcSetCodec(sqlite3* db, const char* zFileName, Codec*
267537267605 }
267538267606}
267539267607
267608+ /*
267609+ ** This function is called by the wal module when writing page content
267610+ ** into the log file.
267611+ **
267612+ ** This function returns a pointer to a buffer containing the encrypted
267613+ ** page content. If a malloc fails, this function may return NULL.
267614+ */
267615+ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdr* pPg)
267616+ {
267617+ sqlite3_file* pFile = sqlite3PagerFile(pPg->pPager);
267618+ void* aData = 0;
267619+ if (pFile->pMethods == &mcIoMethodsGlobal)
267620+ {
267621+ sqlite3mc_file* mcFile = (sqlite3mc_file*) pFile;
267622+ Codec* codec = (mcFile->pMainDb) ? mcFile->pMainDb->codec : 0;
267623+ if (codec != 0 && sqlite3mcIsEncrypted(codec))
267624+ {
267625+ aData = sqlite3mcCodec(codec, pPg->pData, pPg->pgno, 6);
267626+ }
267627+ else
267628+ {
267629+ aData = (char*) pPg->pData;
267630+ }
267631+ }
267632+ else
267633+ {
267634+ aData = (char*) pPg->pData;
267635+ }
267636+ return aData;
267637+ }
267638+
267540267639/*
267541267640** Implementation of VFS methods
267542267641*/
@@ -268254,10 +268353,17 @@ static int mcIoWrite(sqlite3_file* pFile, const void* buffer, int count, sqlite3
268254268353 */
268255268354 }
268256268355#endif
268356+ #if 0
268357+ /*
268358+ ** The page content is encrypted in memory in the WAL journal handler.
268359+ ** This provides for compatibility with legacy applications using the
268360+ ** previous SQLITE_HAS_CODEC encryption API.
268361+ */
268257268362 else if (mcFile->openFlags & SQLITE_OPEN_WAL)
268258268363 {
268259268364 rc = mcWriteWal(pFile, buffer, count, offset);
268260268365 }
268366+ #endif
268261268367 else
268262268368 {
268263268369 rc = REALFILE(pFile)->pMethods->xWrite(REALFILE(pFile), buffer, count, offset);
0 commit comments