3
3
** Purpose: Amalgamation of the SQLite3 Multiple Ciphers encryption extension for SQLite
4
4
** Author: Ulrich Telle
5
5
** Created: 2020-02-28
6
- ** Copyright: (c) 2006-2020 Ulrich Telle
6
+ ** Copyright: (c) 2006-2021 Ulrich Telle
7
7
** License: MIT
8
8
*/
9
9
@@ -50,16 +50,32 @@ void sqlite3mc_shutdown(void);
50
50
#endif
51
51
52
52
#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
+
53
69
#include <windows.h>
54
70
55
71
/* 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*);
63
79
#endif
64
80
65
81
/*
@@ -11027,6 +11043,8 @@ SQLITE_API int sqlite3_deserialize(
11027
11043
SQLITE_PRIVATE int sqlite3mcFileControlPragma(sqlite3*, const char*, int, void*);
11028
11044
SQLITE_PRIVATE int sqlite3mcHandleAttachKey(sqlite3*, const char*, const char*, sqlite3_value*, char**);
11029
11045
SQLITE_PRIVATE int sqlite3mcHandleMainKey(sqlite3*, const char*);
11046
+ typedef struct PgHdr PgHdr;
11047
+ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdr* pPg);
11030
11048
11031
11049
/******** Begin file sqlite3rtree.h *********/
11032
11050
/*
@@ -63362,7 +63380,7 @@ static int walWriteOneFrame(
63362
63380
int rc; /* Result code from subfunctions */
63363
63381
void *pData; /* Data actually written */
63364
63382
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 ;
63366
63384
walEncodeFrame(p->pWal, pPage->pgno, nTruncate, pData, aFrame);
63367
63385
rc = walWriteToLog(p, aFrame, sizeof(aFrame), iOffset);
63368
63386
if( rc ) return rc;
@@ -63545,7 +63563,7 @@ SQLITE_PRIVATE int sqlite3WalFrames(
63545
63563
if( pWal->iReCksum==0 || iWrite<pWal->iReCksum ){
63546
63564
pWal->iReCksum = iWrite;
63547
63565
}
63548
- pData = p->pData ;
63566
+ if( ( pData = sqlite3mcPagerCodec(p))==0 ) return SQLITE_NOMEM ;
63549
63567
rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff);
63550
63568
if( rc ) return rc;
63551
63569
p->flags &= ~PGHDR_WAL_APPEND;
@@ -246921,10 +246939,10 @@ SQLITE_API void sqlite3mc_vfs_shutdown();
246921
246939
#define SQLITE3MC_VERSION_H_
246922
246940
246923
246941
#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
246926
246944
#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 "
246928
246946
246929
246947
#endif /* SQLITE3MC_VERSION_H_ */
246930
246948
/*** End of #include "sqlite3mc_version.h" ***/
@@ -249559,6 +249577,48 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
249559
249577
* Platform-specific entropy functions for seeding RNG
249560
249578
*/
249561
249579
#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
+
249562
249622
#include <windows.h>
249563
249623
#define RtlGenRandom SystemFunction036
249564
249624
BOOLEAN NTAPI RtlGenRandom(PVOID RandomBuffer, ULONG RandomBufferLength);
@@ -249567,7 +249627,11 @@ static size_t entropy(void* buf, size_t n)
249567
249627
{
249568
249628
return RtlGenRandom(buf, (ULONG) n) ? n : 0;
249569
249629
}
249630
+
249631
+ #endif
249632
+
249570
249633
#elif defined(__linux__) || defined(__unix__) || defined(__APPLE__)
249634
+
249571
249635
#ifndef _GNU_SOURCE
249572
249636
#define _GNU_SOURCE
249573
249637
#endif
@@ -250497,7 +250561,7 @@ static int
250497
250561
aesHardwareCheck()
250498
250562
{
250499
250563
unsigned int CPUInfo[4];
250500
- __cpuid(CPUInfo, 1);
250564
+ __cpuid((int*) CPUInfo, 1);
250501
250565
return (CPUInfo[2] & (1 << 25)) != 0 && (CPUInfo[2] & (1 << 19)) != 0; /* Check AES and SSE4.1 */
250502
250566
}
250503
250567
@@ -256488,7 +256552,6 @@ sqlite3mcFileControlPragma(sqlite3* db, const char* zDbName, int op, void* pArg)
256488
256552
if (cipherParams != NULL)
256489
256553
{
256490
256554
const char* cipherName = globalCodecParameterTable[j].m_name;
256491
- int j;
256492
256555
for (j = 0; strlen(cipherParams[j].m_name) > 0; ++j)
256493
256556
{
256494
256557
if (sqlite3_stricmp(pragmaName, cipherParams[j].m_name) == 0) break;
@@ -257001,6 +257064,11 @@ mcReportCodecError(BtShared* pBt, int error)
257001
257064
pBt->pPager->errCode = error;
257002
257065
setGetterMethod(pBt->pPager);
257003
257066
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
+ }
257004
257072
}
257005
257073
257006
257074
/*
@@ -257088,7 +257156,7 @@ mcAdjustBtree(Btree* pBt, int nPageSize, int nReserved, int isLegacy)
257088
257156
}
257089
257157
257090
257158
/* Adjust the page size and the reserved area */
257091
- if (pager->nReserve != nReserved)
257159
+ if (pager->pageSize != pagesize || pager-> nReserve != nReserved)
257092
257160
{
257093
257161
if (isLegacy != 0)
257094
257162
{
@@ -267537,6 +267605,37 @@ SQLITE_PRIVATE void sqlite3mcSetCodec(sqlite3* db, const char* zFileName, Codec*
267537
267605
}
267538
267606
}
267539
267607
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
+
267540
267639
/*
267541
267640
** Implementation of VFS methods
267542
267641
*/
@@ -268254,10 +268353,17 @@ static int mcIoWrite(sqlite3_file* pFile, const void* buffer, int count, sqlite3
268254
268353
*/
268255
268354
}
268256
268355
#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
+ */
268257
268362
else if (mcFile->openFlags & SQLITE_OPEN_WAL)
268258
268363
{
268259
268364
rc = mcWriteWal(pFile, buffer, count, offset);
268260
268365
}
268366
+ #endif
268261
268367
else
268262
268368
{
268263
268369
rc = REALFILE(pFile)->pMethods->xWrite(REALFILE(pFile), buffer, count, offset);
0 commit comments