better-sqlite3-multiple-ciphers 9.1.1 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,9 +7,31 @@
7
7
  ** License: MIT
8
8
  */
9
9
 
10
+ /*
11
+ ** Force some options required for WASM builds
12
+ */
13
+
14
+ #ifdef __WASM__
15
+
16
+ /* Disable User Authentication Extension */
17
+ #ifdef SQLITE_USER_AUTHENTICATION
18
+ #undef SQLITE_USER_AUTHENTICATION
19
+ #endif
20
+ #define SQLITE_USER_AUTHENTICATION 0
21
+
22
+ /* Disable AES hardware support */
23
+ /* Note: this may be changed in the future depending on available support */
24
+ #ifdef SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT
25
+ #undef SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT
26
+ #endif
27
+ #define SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT 1
28
+
29
+ #endif
30
+
10
31
  /*
11
32
  ** Enable SQLite debug assertions if requested
12
33
  */
34
+
13
35
  #ifndef SQLITE_DEBUG
14
36
  #if defined(SQLITE_ENABLE_DEBUG) && (SQLITE_ENABLE_DEBUG == 1)
15
37
  #define SQLITE_DEBUG 1
@@ -67,6 +89,13 @@ void sqlite3mc_shutdown(void);
67
89
  #if !defined(_CRT_RAND_S)
68
90
  #define _CRT_RAND_S
69
91
  #endif
92
+
93
+ #else /* !WIN32 */
94
+
95
+ /* Define this before <string.h> is included to */
96
+ /* retrieve memset_s() declaration if available. */
97
+ #define __STDC_WANT_LIB_EXT1__ 1
98
+
70
99
  #endif
71
100
 
72
101
  #ifndef SQLITE_API
@@ -104,7 +133,7 @@ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
104
133
  /*** Begin of #include "sqlite3patched.c" ***/
105
134
  /******************************************************************************
106
135
  ** This file is an amalgamation of many separate C source files from SQLite
107
- ** version 3.44.0. By combining all the individual C code files into this
136
+ ** version 3.44.2. By combining all the individual C code files into this
108
137
  ** single large file, the entire code can be compiled as a single translation
109
138
  ** unit. This allows many compilers to do optimizations that would not be
110
139
  ** possible if the files were compiled separately. Performance improvements
@@ -122,7 +151,7 @@ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
122
151
  ** separate file. This file contains only code for the core SQLite library.
123
152
  **
124
153
  ** The content in this amalgamation comes from Fossil check-in
125
- ** 17129ba1ff7f0daf37100ee82d507aef7827.
154
+ ** ebead0e7230cd33bcec9f95d2183069565b9.
126
155
  */
127
156
  #define SQLITE_CORE 1
128
157
  #define SQLITE_AMALGAMATION 1
@@ -563,9 +592,9 @@ extern "C" {
563
592
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
564
593
  ** [sqlite_version()] and [sqlite_source_id()].
565
594
  */
566
- #define SQLITE_VERSION "3.44.0"
567
- #define SQLITE_VERSION_NUMBER 3044000
568
- #define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
595
+ #define SQLITE_VERSION "3.44.2"
596
+ #define SQLITE_VERSION_NUMBER 3044002
597
+ #define SQLITE_SOURCE_ID "2023-11-24 11:41:44 ebead0e7230cd33bcec9f95d2183069565b9e709bf745c9b5db65cc0cbf92c0f"
569
598
 
570
599
  /*
571
600
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -5990,13 +6019,27 @@ SQLITE_API int sqlite3_create_window_function(
5990
6019
  ** </dd>
5991
6020
  **
5992
6021
  ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5993
- ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6022
+ ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5994
6023
  ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
5995
- ** Specifying this flag makes no difference for scalar or aggregate user
5996
- ** functions. However, if it is not specified for a user-defined window
5997
- ** function, then any sub-types belonging to arguments passed to the window
5998
- ** function may be discarded before the window function is called (i.e.
5999
- ** sqlite3_value_subtype() will always return 0).
6024
+ ** This flag instructs SQLite to omit some corner-case optimizations that
6025
+ ** might disrupt the operation of the [sqlite3_value_subtype()] function,
6026
+ ** causing it to return zero rather than the correct subtype().
6027
+ ** SQL functions that invokes [sqlite3_value_subtype()] should have this
6028
+ ** property. If the SQLITE_SUBTYPE property is omitted, then the return
6029
+ ** value from [sqlite3_value_subtype()] might sometimes be zero even though
6030
+ ** a non-zero subtype was specified by the function argument expression.
6031
+ **
6032
+ ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
6033
+ ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
6034
+ ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
6035
+ ** result.
6036
+ ** Every function that invokes [sqlite3_result_subtype()] should have this
6037
+ ** property. If it does not, then the call to [sqlite3_result_subtype()]
6038
+ ** might become a no-op if the function is used as term in an
6039
+ ** [expression index]. On the other hand, SQL functions that never invoke
6040
+ ** [sqlite3_result_subtype()] should avoid setting this property, as the
6041
+ ** purpose of this property is to disable certain optimizations that are
6042
+ ** incompatible with subtypes.
6000
6043
  ** </dd>
6001
6044
  ** </dl>
6002
6045
  */
@@ -6004,6 +6047,7 @@ SQLITE_API int sqlite3_create_window_function(
6004
6047
  #define SQLITE_DIRECTONLY 0x000080000
6005
6048
  #define SQLITE_SUBTYPE 0x000100000
6006
6049
  #define SQLITE_INNOCUOUS 0x000200000
6050
+ #define SQLITE_RESULT_SUBTYPE 0x001000000
6007
6051
 
6008
6052
  /*
6009
6053
  ** CAPI3REF: Deprecated Functions
@@ -6200,6 +6244,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6200
6244
  ** information can be used to pass a limited amount of context from
6201
6245
  ** one SQL function to another. Use the [sqlite3_result_subtype()]
6202
6246
  ** routine to set the subtype for the return value of an SQL function.
6247
+ **
6248
+ ** Every [application-defined SQL function] that invoke this interface
6249
+ ** should include the [SQLITE_SUBTYPE] property in the text
6250
+ ** encoding argument when the function is [sqlite3_create_function|registered].
6251
+ ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6252
+ ** might return zero instead of the upstream subtype in some corner cases.
6203
6253
  */
6204
6254
  SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6205
6255
 
@@ -6330,14 +6380,22 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6330
6380
  ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6331
6381
  ** parameter)^, or
6332
6382
  ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6333
- ** allocation error occurs.)^ </ul>
6383
+ ** allocation error occurs.)^
6384
+ ** <li> ^(during the original sqlite3_set_auxdata() call if the function
6385
+ ** is evaluated during query planning instead of during query execution,
6386
+ ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6334
6387
  **
6335
- ** Note the last bullet in particular. The destructor X in
6388
+ ** Note the last two bullets in particular. The destructor X in
6336
6389
  ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6337
6390
  ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6338
6391
  ** should be called near the end of the function implementation and the
6339
6392
  ** function implementation should not make any use of P after
6340
- ** sqlite3_set_auxdata() has been called.
6393
+ ** sqlite3_set_auxdata() has been called. Furthermore, a call to
6394
+ ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6395
+ ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6396
+ ** condition occurred during the sqlite3_set_auxdata() call or if the
6397
+ ** function is being evaluated during query planning rather than during
6398
+ ** query execution.
6341
6399
  **
6342
6400
  ** ^(In practice, auxiliary data is preserved between function calls for
6343
6401
  ** function parameters that are compile-time constants, including literal
@@ -6611,6 +6669,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6611
6669
  ** higher order bits are discarded.
6612
6670
  ** The number of subtype bytes preserved by SQLite might increase
6613
6671
  ** in future releases of SQLite.
6672
+ **
6673
+ ** Every [application-defined SQL function] that invokes this interface
6674
+ ** should include the [SQLITE_RESULT_SUBTYPE] property in its
6675
+ ** text encoding argument when the SQL function is
6676
+ ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6677
+ ** property is omitted from the function that invokes sqlite3_result_subtype(),
6678
+ ** then in some cases the sqlite3_result_subtype() might fail to set
6679
+ ** the result subtype.
6680
+ **
6681
+ ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6682
+ ** SQL function that invokes the sqlite3_result_subtype() interface
6683
+ ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6684
+ ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6685
+ ** by default.
6614
6686
  */
6615
6687
  SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6616
6688
 
@@ -17926,14 +17998,15 @@ struct FuncDestructor {
17926
17998
  #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17927
17999
  ** single query - might change over time */
17928
18000
  #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17929
- /* 0x8000 -- available for reuse */
18001
+ #define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
17930
18002
  #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17931
18003
  #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17932
18004
  #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17933
- #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
18005
+ /* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
17934
18006
  #define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17935
18007
  #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17936
18008
  #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
18009
+ /* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
17937
18010
  #define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17938
18011
 
17939
18012
  /* Identifier numbers for each in-line function */
@@ -18025,9 +18098,10 @@ struct FuncDestructor {
18025
18098
  #define MFUNCTION(zName, nArg, xPtr, xFunc) \
18026
18099
  {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
18027
18100
  xPtr, 0, xFunc, 0, 0, 0, #zName, {0} }
18028
- #define JFUNCTION(zName, nArg, iArg, xFunc) \
18029
- {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|\
18030
- SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
18101
+ #define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, iArg, xFunc) \
18102
+ {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
18103
+ SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
18104
+ ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
18031
18105
  SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
18032
18106
  #define INLINE_FUNC(zName, nArg, iArg, mFlags) \
18033
18107
  {nArg, SQLITE_FUNC_BUILTIN|\
@@ -29568,7 +29642,7 @@ SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29568
29642
  SQLITE_MEMORY_BARRIER;
29569
29643
  #elif defined(__GNUC__)
29570
29644
  __sync_synchronize();
29571
- #elif MSVC_VERSION>=1300
29645
+ #elif MSVC_VERSION>=1400
29572
29646
  _ReadWriteBarrier();
29573
29647
  #elif defined(MemoryBarrier)
29574
29648
  MemoryBarrier();
@@ -61573,10 +61647,13 @@ act_like_temp_file:
61573
61647
  */
61574
61648
  SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61575
61649
  Pager *pPager;
61650
+ const char *p;
61576
61651
  while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61577
61652
  zName--;
61578
61653
  }
61579
- pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
61654
+ p = zName - 4 - sizeof(Pager*);
61655
+ assert( EIGHT_BYTE_ALIGNMENT(p) );
61656
+ pPager = *(Pager**)p;
61580
61657
  return pPager->fd;
61581
61658
  }
61582
61659
 
@@ -83537,7 +83614,7 @@ static int valueFromFunction(
83537
83614
  #endif
83538
83615
  assert( pFunc );
83539
83616
  if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83540
- || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
83617
+ || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
83541
83618
  ){
83542
83619
  return SQLITE_OK;
83543
83620
  }
@@ -84261,10 +84338,11 @@ static int growOpArray(Vdbe *v, int nOp){
84261
84338
  ** sqlite3CantopenError(lineno)
84262
84339
  */
84263
84340
  static void test_addop_breakpoint(int pc, Op *pOp){
84264
- static int n = 0;
84341
+ static u64 n = 0;
84265
84342
  (void)pc;
84266
84343
  (void)pOp;
84267
84344
  n++;
84345
+ if( n==LARGEST_UINT64 ) abort(); /* so that n is used, preventing a warning */
84268
84346
  }
84269
84347
  #endif
84270
84348
 
@@ -90078,6 +90156,18 @@ SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubt
90078
90156
  #ifdef SQLITE_ENABLE_API_ARMOR
90079
90157
  if( pCtx==0 ) return;
90080
90158
  #endif
90159
+ #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
90160
+ if( pCtx->pFunc!=0
90161
+ && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90162
+ ){
90163
+ char zErr[200];
90164
+ sqlite3_snprintf(sizeof(zErr), zErr,
90165
+ "misuse of sqlite3_result_subtype() by %s()",
90166
+ pCtx->pFunc->zName);
90167
+ sqlite3_result_error(pCtx, zErr, -1);
90168
+ return;
90169
+ }
90170
+ #endif /* SQLITE_STRICT_SUBTYPE */
90081
90171
  pOut = pCtx->pOut;
90082
90172
  assert( sqlite3_mutex_held(pOut->db->mutex) );
90083
90173
  pOut->eSubtype = eSubtype & 0xff;
@@ -92396,11 +92486,12 @@ SQLITE_API int sqlite3_found_count = 0;
92396
92486
  ** sqlite3CantopenError(lineno)
92397
92487
  */
92398
92488
  static void test_trace_breakpoint(int pc, Op *pOp, Vdbe *v){
92399
- static int n = 0;
92489
+ static u64 n = 0;
92400
92490
  (void)pc;
92401
92491
  (void)pOp;
92402
92492
  (void)v;
92403
92493
  n++;
92494
+ if( n==LARGEST_UINT64 ) abort(); /* So that n is used, preventing a warning */
92404
92495
  }
92405
92496
  #endif
92406
92497
 
@@ -100447,7 +100538,7 @@ case OP_VCheck: { /* out2 */
100447
100538
  pTab = pOp->p4.pTab;
100448
100539
  assert( pTab!=0 );
100449
100540
  assert( IsVirtual(pTab) );
100450
- assert( pTab->u.vtab.p!=0 );
100541
+ if( pTab->u.vtab.p==0 ) break;
100451
100542
  pVtab = pTab->u.vtab.p->pVtab;
100452
100543
  assert( pVtab!=0 );
100453
100544
  pModule = pVtab->pModule;
@@ -114043,8 +114134,8 @@ SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB
114043
114134
  */
114044
114135
  SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
114045
114136
  return sqlite3ExprCompare(0,
114046
- sqlite3ExprSkipCollateAndLikely(pA),
114047
- sqlite3ExprSkipCollateAndLikely(pB),
114137
+ sqlite3ExprSkipCollate(pA),
114138
+ sqlite3ExprSkipCollate(pB),
114048
114139
  iTab);
114049
114140
  }
114050
114141
 
@@ -143683,7 +143774,8 @@ SQLITE_PRIVATE void sqlite3SubqueryColumnTypes(
143683
143774
  NameContext sNC;
143684
143775
 
143685
143776
  assert( pSelect!=0 );
143686
- assert( (pSelect->selFlags & SF_Resolved)!=0 );
143777
+ testcase( (pSelect->selFlags & SF_Resolved)==0 );
143778
+ assert( (pSelect->selFlags & SF_Resolved)!=0 || IN_RENAME_OBJECT );
143687
143779
  assert( pTab->nCol==pSelect->pEList->nExpr || pParse->nErr>0 );
143688
143780
  assert( aff==SQLITE_AFF_NONE || aff==SQLITE_AFF_BLOB );
143689
143781
  if( db->mallocFailed || IN_RENAME_OBJECT ) return;
@@ -147736,10 +147828,11 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
147736
147828
  SrcList *pTabList;
147737
147829
  SrcItem *pFrom;
147738
147830
 
147739
- assert( p->selFlags & SF_Resolved );
147740
147831
  if( p->selFlags & SF_HasTypeInfo ) return;
147741
147832
  p->selFlags |= SF_HasTypeInfo;
147742
147833
  pParse = pWalker->pParse;
147834
+ testcase( (p->selFlags & SF_Resolved)==0 );
147835
+ assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
147743
147836
  pTabList = p->pSrc;
147744
147837
  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147745
147838
  Table *pTab = pFrom->pTab;
@@ -148761,6 +148854,7 @@ SQLITE_PRIVATE int sqlite3Select(
148761
148854
  TREETRACE(0x1000,pParse,p,
148762
148855
  ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148763
148856
  pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
148857
+ unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
148764
148858
  }
148765
148859
  }
148766
148860
  if( pItem->fg.jointype & JT_LTORJ ){
@@ -148775,17 +148869,15 @@ SQLITE_PRIVATE int sqlite3Select(
148775
148869
  TREETRACE(0x1000,pParse,p,
148776
148870
  ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148777
148871
  pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
148872
+ unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
148778
148873
  }
148779
148874
  }
148780
148875
  }
148781
- for(j=pTabList->nSrc-1; j>=i; j--){
148876
+ for(j=pTabList->nSrc-1; j>=0; j--){
148782
148877
  pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148783
148878
  if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148784
148879
  }
148785
148880
  }
148786
- assert( pItem->iCursor>=0 );
148787
- unsetJoinExpr(p->pWhere, pItem->iCursor,
148788
- pTabList->a[0].fg.jointype & JT_LTORJ);
148789
148881
  }
148790
148882
 
148791
148883
  /* No further action if this term of the FROM clause is not a subquery */
@@ -166189,6 +166281,20 @@ static SQLITE_NOINLINE void whereAddIndexedExpr(
166189
166281
  continue;
166190
166282
  }
166191
166283
  if( sqlite3ExprIsConstant(pExpr) ) continue;
166284
+ if( pExpr->op==TK_FUNCTION ){
166285
+ /* Functions that might set a subtype should not be replaced by the
166286
+ ** value taken from an expression index since the index omits the
166287
+ ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
166288
+ int n;
166289
+ FuncDef *pDef;
166290
+ sqlite3 *db = pParse->db;
166291
+ assert( ExprUseXList(pExpr) );
166292
+ n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166293
+ pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166294
+ if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166295
+ continue;
166296
+ }
166297
+ }
166192
166298
  p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166193
166299
  if( p==0 ) break;
166194
166300
  p->pIENext = pParse->pIdxEpr;
@@ -168371,7 +168477,7 @@ SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
168371
168477
  assert( ExprUseXList(pWin->pOwner) );
168372
168478
  assert( pWin->pWFunc!=0 );
168373
168479
  pArgs = pWin->pOwner->x.pList;
168374
- if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
168480
+ if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
168375
168481
  selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
168376
168482
  pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168377
168483
  pWin->bExprArgs = 1;
@@ -179545,7 +179651,7 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
179545
179651
  assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
179546
179652
  assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
179547
179653
  extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
179548
- SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
179654
+ SQLITE_SUBTYPE|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE);
179549
179655
  enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
179550
179656
 
179551
179657
  /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
@@ -203134,13 +203240,19 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
203134
203240
  zIn++;
203135
203241
  N -= 2;
203136
203242
  while( N>0 ){
203137
- for(i=0; i<N && zIn[i]!='\\'; i++){}
203243
+ for(i=0; i<N && zIn[i]!='\\' && zIn[i]!='"'; i++){}
203138
203244
  if( i>0 ){
203139
203245
  jsonAppendRawNZ(p, zIn, i);
203140
203246
  zIn += i;
203141
203247
  N -= i;
203142
203248
  if( N==0 ) break;
203143
203249
  }
203250
+ if( zIn[0]=='"' ){
203251
+ jsonAppendRawNZ(p, "\\\"", 2);
203252
+ zIn++;
203253
+ N--;
203254
+ continue;
203255
+ }
203144
203256
  assert( zIn[0]=='\\' );
203145
203257
  switch( (u8)zIn[1] ){
203146
203258
  case '\'':
@@ -203535,7 +203647,8 @@ static void jsonReturnJson(
203535
203647
  JsonParse *pParse, /* The complete JSON */
203536
203648
  JsonNode *pNode, /* Node to return */
203537
203649
  sqlite3_context *pCtx, /* Return value for this function */
203538
- int bGenerateAlt /* Also store the rendered text in zAlt */
203650
+ int bGenerateAlt, /* Also store the rendered text in zAlt */
203651
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203539
203652
  ){
203540
203653
  JsonString s;
203541
203654
  if( pParse->oom ){
@@ -203550,7 +203663,7 @@ static void jsonReturnJson(
203550
203663
  pParse->nAlt = s.nUsed;
203551
203664
  }
203552
203665
  jsonResult(&s);
203553
- sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203666
+ if( !omitSubtype ) sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203554
203667
  }
203555
203668
  }
203556
203669
 
@@ -203591,7 +203704,8 @@ static u32 jsonHexToInt4(const char *z){
203591
203704
  static void jsonReturn(
203592
203705
  JsonParse *pParse, /* Complete JSON parse tree */
203593
203706
  JsonNode *pNode, /* Node to return */
203594
- sqlite3_context *pCtx /* Return value for this function */
203707
+ sqlite3_context *pCtx, /* Return value for this function */
203708
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203595
203709
  ){
203596
203710
  switch( pNode->eType ){
203597
203711
  default: {
@@ -203737,7 +203851,7 @@ static void jsonReturn(
203737
203851
  }
203738
203852
  case JSON_ARRAY:
203739
203853
  case JSON_OBJECT: {
203740
- jsonReturnJson(pParse, pNode, pCtx, 0);
203854
+ jsonReturnJson(pParse, pNode, pCtx, 0, omitSubtype);
203741
203855
  break;
203742
203856
  }
203743
203857
  }
@@ -205089,7 +205203,7 @@ static void jsonParseFunc(
205089
205203
  printf("iSubst = %u\n", p->iSubst);
205090
205204
  printf("iHold = %u\n", p->iHold);
205091
205205
  jsonDebugPrintNodeEntries(p->aNode, p->nNode);
205092
- jsonReturnJson(p, p->aNode, ctx, 1);
205206
+ jsonReturnJson(p, p->aNode, ctx, 1, 0);
205093
205207
  }
205094
205208
 
205095
205209
  /*
@@ -205275,15 +205389,14 @@ static void jsonExtractFunc(
205275
205389
  }
205276
205390
  if( pNode ){
205277
205391
  if( flags & JSON_JSON ){
205278
- jsonReturnJson(p, pNode, ctx, 0);
205392
+ jsonReturnJson(p, pNode, ctx, 0, 0);
205279
205393
  }else{
205280
- jsonReturn(p, pNode, ctx);
205281
- sqlite3_result_subtype(ctx, 0);
205394
+ jsonReturn(p, pNode, ctx, 1);
205282
205395
  }
205283
205396
  }
205284
205397
  }else{
205285
205398
  pNode = jsonLookup(p, zPath, 0, ctx);
205286
- if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
205399
+ if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx, 0);
205287
205400
  }
205288
205401
  }else{
205289
205402
  /* Two or more PATH arguments results in a JSON array with each
@@ -205409,7 +205522,7 @@ static void jsonPatchFunc(
205409
205522
  if( pResult && pX->oom==0 ){
205410
205523
  jsonDebugPrintParse(pX);
205411
205524
  jsonDebugPrintNode(pResult);
205412
- jsonReturnJson(pX, pResult, ctx, 0);
205525
+ jsonReturnJson(pX, pResult, ctx, 0, 0);
205413
205526
  }else{
205414
205527
  sqlite3_result_error_nomem(ctx);
205415
205528
  }
@@ -205488,7 +205601,7 @@ static void jsonRemoveFunc(
205488
205601
  }
205489
205602
  }
205490
205603
  if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
205491
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205604
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205492
205605
  }
205493
205606
  remove_done:
205494
205607
  jsonDebugPrintParse(p);
@@ -205617,7 +205730,7 @@ static void jsonReplaceFunc(
205617
205730
  jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205618
205731
  }
205619
205732
  }
205620
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205733
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205621
205734
  replace_err:
205622
205735
  jsonDebugPrintParse(pParse);
205623
205736
  jsonParseFree(pParse);
@@ -205671,7 +205784,7 @@ static void jsonSetFunc(
205671
205784
  }
205672
205785
  }
205673
205786
  jsonDebugPrintParse(pParse);
205674
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205787
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205675
205788
  jsonSetDone:
205676
205789
  jsonParseFree(pParse);
205677
205790
  }
@@ -206186,7 +206299,7 @@ static int jsonEachColumn(
206186
206299
  case JEACH_KEY: {
206187
206300
  if( p->i==0 ) break;
206188
206301
  if( p->eType==JSON_OBJECT ){
206189
- jsonReturn(&p->sParse, pThis, ctx);
206302
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206190
206303
  }else if( p->eType==JSON_ARRAY ){
206191
206304
  u32 iKey;
206192
206305
  if( p->bRecursive ){
@@ -206202,7 +206315,7 @@ static int jsonEachColumn(
206202
206315
  }
206203
206316
  case JEACH_VALUE: {
206204
206317
  if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206205
- jsonReturn(&p->sParse, pThis, ctx);
206318
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206206
206319
  break;
206207
206320
  }
206208
206321
  case JEACH_TYPE: {
@@ -206213,7 +206326,7 @@ static int jsonEachColumn(
206213
206326
  case JEACH_ATOM: {
206214
206327
  if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206215
206328
  if( pThis->eType>=JSON_ARRAY ) break;
206216
- jsonReturn(&p->sParse, pThis, ctx);
206329
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206217
206330
  break;
206218
206331
  }
206219
206332
  case JEACH_ID: {
@@ -206506,34 +206619,43 @@ static sqlite3_module jsonTreeModule = {
206506
206619
  SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
206507
206620
  #ifndef SQLITE_OMIT_JSON
206508
206621
  static FuncDef aJsonFunc[] = {
206509
- JFUNCTION(json, 1, 0, jsonRemoveFunc),
206510
- JFUNCTION(json_array, -1, 0, jsonArrayFunc),
206511
- JFUNCTION(json_array_length, 1, 0, jsonArrayLengthFunc),
206512
- JFUNCTION(json_array_length, 2, 0, jsonArrayLengthFunc),
206513
- JFUNCTION(json_error_position,1, 0, jsonErrorFunc),
206514
- JFUNCTION(json_extract, -1, 0, jsonExtractFunc),
206515
- JFUNCTION(->, 2, JSON_JSON, jsonExtractFunc),
206516
- JFUNCTION(->>, 2, JSON_SQL, jsonExtractFunc),
206517
- JFUNCTION(json_insert, -1, 0, jsonSetFunc),
206518
- JFUNCTION(json_object, -1, 0, jsonObjectFunc),
206519
- JFUNCTION(json_patch, 2, 0, jsonPatchFunc),
206520
- JFUNCTION(json_quote, 1, 0, jsonQuoteFunc),
206521
- JFUNCTION(json_remove, -1, 0, jsonRemoveFunc),
206522
- JFUNCTION(json_replace, -1, 0, jsonReplaceFunc),
206523
- JFUNCTION(json_set, -1, JSON_ISSET, jsonSetFunc),
206524
- JFUNCTION(json_type, 1, 0, jsonTypeFunc),
206525
- JFUNCTION(json_type, 2, 0, jsonTypeFunc),
206526
- JFUNCTION(json_valid, 1, 0, jsonValidFunc),
206527
- #if SQLITE_DEBUG
206528
- JFUNCTION(json_parse, 1, 0, jsonParseFunc),
206529
- JFUNCTION(json_test1, 1, 0, jsonTest1Func),
206622
+ /* calls sqlite3_result_subtype() */
206623
+ /* | */
206624
+ /* Uses cache ______ | __ calls sqlite3_value_subtype() */
206625
+ /* | | | */
206626
+ /* Num args _________ | | | ___ Flags */
206627
+ /* | | | | | */
206628
+ /* | | | | | */
206629
+ JFUNCTION(json, 1, 1, 1, 0, 0, jsonRemoveFunc),
206630
+ JFUNCTION(json_array, -1, 0, 1, 1, 0, jsonArrayFunc),
206631
+ JFUNCTION(json_array_length, 1, 1, 0, 0, 0, jsonArrayLengthFunc),
206632
+ JFUNCTION(json_array_length, 2, 1, 0, 0, 0, jsonArrayLengthFunc),
206633
+ JFUNCTION(json_error_position,1, 1, 0, 0, 0, jsonErrorFunc),
206634
+ JFUNCTION(json_extract, -1, 1, 1, 0, 0, jsonExtractFunc),
206635
+ JFUNCTION(->, 2, 1, 1, 0, JSON_JSON, jsonExtractFunc),
206636
+ JFUNCTION(->>, 2, 1, 0, 0, JSON_SQL, jsonExtractFunc),
206637
+ JFUNCTION(json_insert, -1, 1, 1, 1, 0, jsonSetFunc),
206638
+ JFUNCTION(json_object, -1, 0, 1, 1, 0, jsonObjectFunc),
206639
+ JFUNCTION(json_patch, 2, 1, 1, 0, 0, jsonPatchFunc),
206640
+ JFUNCTION(json_quote, 1, 0, 1, 1, 0, jsonQuoteFunc),
206641
+ JFUNCTION(json_remove, -1, 1, 1, 0, 0, jsonRemoveFunc),
206642
+ JFUNCTION(json_replace, -1, 1, 1, 1, 0, jsonReplaceFunc),
206643
+ JFUNCTION(json_set, -1, 1, 1, 1, JSON_ISSET, jsonSetFunc),
206644
+ JFUNCTION(json_type, 1, 1, 0, 0, 0, jsonTypeFunc),
206645
+ JFUNCTION(json_type, 2, 1, 0, 0, 0, jsonTypeFunc),
206646
+ JFUNCTION(json_valid, 1, 1, 0, 0, 0, jsonValidFunc),
206647
+ #ifdef SQLITE_DEBUG
206648
+ JFUNCTION(json_parse, 1, 1, 1, 0, 0, jsonParseFunc),
206649
+ JFUNCTION(json_test1, 1, 1, 0, 1, 0, jsonTest1Func),
206530
206650
  #endif
206531
206651
  WAGGREGATE(json_group_array, 1, 0, 0,
206532
206652
  jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
206533
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
206653
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206654
+ SQLITE_DETERMINISTIC),
206534
206655
  WAGGREGATE(json_group_object, 2, 0, 0,
206535
206656
  jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
206536
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
206657
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206658
+ SQLITE_DETERMINISTIC)
206537
206659
  };
206538
206660
  sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
206539
206661
  #endif
@@ -236270,10 +236392,8 @@ static Fts5HashEntry *fts5HashEntryMerge(
236270
236392
  }
236271
236393
 
236272
236394
  /*
236273
- ** Extract all tokens from hash table iHash and link them into a list
236274
- ** in sorted order. The hash table is cleared before returning. It is
236275
- ** the responsibility of the caller to free the elements of the returned
236276
- ** list.
236395
+ ** Link all tokens from hash table iHash into a list in sorted order. The
236396
+ ** tokens are not removed from the hash table.
236277
236397
  */
236278
236398
  static int fts5HashEntrySort(
236279
236399
  Fts5Hash *pHash,
@@ -239139,6 +239259,14 @@ static void fts5SegIterHashInit(
239139
239259
  pLeaf->p = (u8*)pList;
239140
239260
  }
239141
239261
  }
239262
+
239263
+ /* The call to sqlite3Fts5HashScanInit() causes the hash table to
239264
+ ** fill the size field of all existing position lists. This means they
239265
+ ** can no longer be appended to. Since the only scenario in which they
239266
+ ** can be appended to is if the previous operation on this table was
239267
+ ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
239268
+ ** possibility altogether. */
239269
+ p->bDelete = 0;
239142
239270
  }else{
239143
239271
  p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
239144
239272
  (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
@@ -240816,7 +240944,7 @@ static void fts5WriteAppendPoslistData(
240816
240944
  const u8 *a = aData;
240817
240945
  int n = nData;
240818
240946
 
240819
- assert( p->pConfig->pgsz>0 );
240947
+ assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK );
240820
240948
  while( p->rc==SQLITE_OK
240821
240949
  && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
240822
240950
  ){
@@ -241549,18 +241677,24 @@ static void fts5DoSecureDelete(
241549
241677
 
241550
241678
  iOff = iStart;
241551
241679
 
241552
- /* Set variable bLastInDoclist to true if this entry happens to be
241553
- ** the last rowid in the doclist for its term. */
241680
+ /* If the position-list for the entry being removed flows over past
241681
+ ** the end of this page, delete the portion of the position-list on the
241682
+ ** next page and beyond.
241683
+ **
241684
+ ** Set variable bLastInDoclist to true if this entry happens
241685
+ ** to be the last rowid in the doclist for its term. */
241686
+ if( iNextOff>=iPgIdx ){
241687
+ int pgno = pSeg->iLeafPgno+1;
241688
+ fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist);
241689
+ iNextOff = iPgIdx;
241690
+ }
241691
+
241554
241692
  if( pSeg->bDel==0 ){
241555
- if( iNextOff>=iPgIdx ){
241556
- int pgno = pSeg->iLeafPgno+1;
241557
- fts5SecureDeleteOverflow(p, pSeg->pSeg, pgno, &bLastInDoclist);
241558
- iNextOff = iPgIdx;
241559
- }else{
241693
+ if( iNextOff!=iPgIdx ){
241560
241694
  /* Loop through the page-footer. If iNextOff (offset of the
241561
241695
  ** entry following the one we are removing) is equal to the
241562
241696
  ** offset of a key on this page, then the entry is the last
241563
- ** in its doclist. */
241697
+ ** in its doclist. */
241564
241698
  int iKeyOff = 0;
241565
241699
  for(iIdx=0; iIdx<nIdx; /* no-op */){
241566
241700
  u32 iVal = 0;
@@ -242076,8 +242210,9 @@ static int sqlite3Fts5IndexOptimize(Fts5Index *p){
242076
242210
 
242077
242211
  assert( p->rc==SQLITE_OK );
242078
242212
  fts5IndexFlush(p);
242079
- assert( p->nContentlessDelete==0 );
242213
+ assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 );
242080
242214
  pStruct = fts5StructureRead(p);
242215
+ assert( p->rc!=SQLITE_OK || pStruct!=0 );
242081
242216
  fts5StructureInvalidate(p);
242082
242217
 
242083
242218
  if( pStruct ){
@@ -247654,7 +247789,7 @@ static void fts5SourceIdFunc(
247654
247789
  ){
247655
247790
  assert( nArg==0 );
247656
247791
  UNUSED_PARAM2(nArg, apUnused);
247657
- sqlite3_result_text(pCtx, "fts5: 2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301", -1, SQLITE_TRANSIENT);
247792
+ sqlite3_result_text(pCtx, "fts5: 2023-11-24 11:41:44 ebead0e7230cd33bcec9f95d2183069565b9e709bf745c9b5db65cc0cbf92c0f", -1, SQLITE_TRANSIENT);
247658
247793
  }
247659
247794
 
247660
247795
  /*
@@ -252761,7 +252896,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252761
252896
  ** Purpose: Header file for SQLite3 Multiple Ciphers compile-time configuration
252762
252897
  ** Author: Ulrich Telle
252763
252898
  ** Created: 2021-09-27
252764
- ** Copyright: (c) 2019-2022 Ulrich Telle
252899
+ ** Copyright: (c) 2019-2023 Ulrich Telle
252765
252900
  ** License: MIT
252766
252901
  */
252767
252902
 
@@ -252795,6 +252930,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252795
252930
  #define HAVE_CIPHER_RC4 WXSQLITE3_HAVE_CIPHER_RC4
252796
252931
  #endif
252797
252932
 
252933
+ #ifdef WXSQLITE3_HAVE_CIPHER_ASCON128
252934
+ #define HAVE_CIPHER_ASCON128 WXSQLITE3_HAVE_CIPHER_ASCON128
252935
+ #endif
252936
+
252798
252937
  /*
252799
252938
  ** Actual definitions of supported ciphers
252800
252939
  */
@@ -252818,6 +252957,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252818
252957
  #define HAVE_CIPHER_RC4 1
252819
252958
  #endif
252820
252959
 
252960
+ #ifndef HAVE_CIPHER_ASCON128
252961
+ #define HAVE_CIPHER_ASCON128 1
252962
+ #endif
252963
+
252821
252964
  /*
252822
252965
  ** Disable all built-in ciphers on request
252823
252966
  */
@@ -252832,11 +252975,13 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252832
252975
  #undef HAVE_CIPHER_CHACHA20
252833
252976
  #undef HAVE_CIPHER_SQLCIPHER
252834
252977
  #undef HAVE_CIPHER_RC4
252978
+ #undef HAVE_CIPHER_ASCON128
252835
252979
  #define HAVE_CIPHER_AES_128_CBC 0
252836
252980
  #define HAVE_CIPHER_AES_256_CBC 0
252837
252981
  #define HAVE_CIPHER_CHACHA20 0
252838
252982
  #define HAVE_CIPHER_SQLCIPHER 0
252839
252983
  #define HAVE_CIPHER_RC4 0
252984
+ #define HAVE_CIPHER_ASCON128 0
252840
252985
  #endif
252841
252986
 
252842
252987
  /*
@@ -252926,7 +253071,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252926
253071
  ** Purpose: Header file for SQLite3 Multiple Ciphers support
252927
253072
  ** Author: Ulrich Telle
252928
253073
  ** Created: 2020-03-01
252929
- ** Copyright: (c) 2019-2022 Ulrich Telle
253074
+ ** Copyright: (c) 2019-2023 Ulrich Telle
252930
253075
  ** License: MIT
252931
253076
  */
252932
253077
 
@@ -252953,10 +253098,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252953
253098
  #define SQLITE3MC_VERSION_H_
252954
253099
 
252955
253100
  #define SQLITE3MC_VERSION_MAJOR 1
252956
- #define SQLITE3MC_VERSION_MINOR 7
252957
- #define SQLITE3MC_VERSION_RELEASE 4
253101
+ #define SQLITE3MC_VERSION_MINOR 8
253102
+ #define SQLITE3MC_VERSION_RELEASE 1
252958
253103
  #define SQLITE3MC_VERSION_SUBRELEASE 0
252959
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.7.4"
253104
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.8.1"
252960
253105
 
252961
253106
  #endif /* SQLITE3MC_VERSION_H_ */
252962
253107
  /*** End of #include "sqlite3mc_version.h" ***/
@@ -253115,9 +253260,9 @@ extern "C" {
253115
253260
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
253116
253261
  ** [sqlite_version()] and [sqlite_source_id()].
253117
253262
  */
253118
- #define SQLITE_VERSION "3.44.0"
253119
- #define SQLITE_VERSION_NUMBER 3044000
253120
- #define SQLITE_SOURCE_ID "2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301"
253263
+ #define SQLITE_VERSION "3.44.2"
253264
+ #define SQLITE_VERSION_NUMBER 3044002
253265
+ #define SQLITE_SOURCE_ID "2023-11-24 11:41:44 ebead0e7230cd33bcec9f95d2183069565b9e709bf745c9b5db65cc0cbf92c0f"
253121
253266
 
253122
253267
  /*
253123
253268
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -258542,13 +258687,27 @@ SQLITE_API int sqlite3_create_window_function(
258542
258687
  ** </dd>
258543
258688
  **
258544
258689
  ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
258545
- ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
258690
+ ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
258546
258691
  ** [sqlite3_value_subtype()] to inspect the sub-types of its arguments.
258547
- ** Specifying this flag makes no difference for scalar or aggregate user
258548
- ** functions. However, if it is not specified for a user-defined window
258549
- ** function, then any sub-types belonging to arguments passed to the window
258550
- ** function may be discarded before the window function is called (i.e.
258551
- ** sqlite3_value_subtype() will always return 0).
258692
+ ** This flag instructs SQLite to omit some corner-case optimizations that
258693
+ ** might disrupt the operation of the [sqlite3_value_subtype()] function,
258694
+ ** causing it to return zero rather than the correct subtype().
258695
+ ** SQL functions that invokes [sqlite3_value_subtype()] should have this
258696
+ ** property. If the SQLITE_SUBTYPE property is omitted, then the return
258697
+ ** value from [sqlite3_value_subtype()] might sometimes be zero even though
258698
+ ** a non-zero subtype was specified by the function argument expression.
258699
+ **
258700
+ ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
258701
+ ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
258702
+ ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
258703
+ ** result.
258704
+ ** Every function that invokes [sqlite3_result_subtype()] should have this
258705
+ ** property. If it does not, then the call to [sqlite3_result_subtype()]
258706
+ ** might become a no-op if the function is used as term in an
258707
+ ** [expression index]. On the other hand, SQL functions that never invoke
258708
+ ** [sqlite3_result_subtype()] should avoid setting this property, as the
258709
+ ** purpose of this property is to disable certain optimizations that are
258710
+ ** incompatible with subtypes.
258552
258711
  ** </dd>
258553
258712
  ** </dl>
258554
258713
  */
@@ -258556,6 +258715,7 @@ SQLITE_API int sqlite3_create_window_function(
258556
258715
  #define SQLITE_DIRECTONLY 0x000080000
258557
258716
  #define SQLITE_SUBTYPE 0x000100000
258558
258717
  #define SQLITE_INNOCUOUS 0x000200000
258718
+ #define SQLITE_RESULT_SUBTYPE 0x001000000
258559
258719
 
258560
258720
  /*
258561
258721
  ** CAPI3REF: Deprecated Functions
@@ -258752,6 +258912,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
258752
258912
  ** information can be used to pass a limited amount of context from
258753
258913
  ** one SQL function to another. Use the [sqlite3_result_subtype()]
258754
258914
  ** routine to set the subtype for the return value of an SQL function.
258915
+ **
258916
+ ** Every [application-defined SQL function] that invoke this interface
258917
+ ** should include the [SQLITE_SUBTYPE] property in the text
258918
+ ** encoding argument when the function is [sqlite3_create_function|registered].
258919
+ ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
258920
+ ** might return zero instead of the upstream subtype in some corner cases.
258755
258921
  */
258756
258922
  SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
258757
258923
 
@@ -258882,14 +259048,22 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
258882
259048
  ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
258883
259049
  ** parameter)^, or
258884
259050
  ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
258885
- ** allocation error occurs.)^ </ul>
259051
+ ** allocation error occurs.)^
259052
+ ** <li> ^(during the original sqlite3_set_auxdata() call if the function
259053
+ ** is evaluated during query planning instead of during query execution,
259054
+ ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
258886
259055
  **
258887
- ** Note the last bullet in particular. The destructor X in
259056
+ ** Note the last two bullets in particular. The destructor X in
258888
259057
  ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
258889
259058
  ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
258890
259059
  ** should be called near the end of the function implementation and the
258891
259060
  ** function implementation should not make any use of P after
258892
- ** sqlite3_set_auxdata() has been called.
259061
+ ** sqlite3_set_auxdata() has been called. Furthermore, a call to
259062
+ ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
259063
+ ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
259064
+ ** condition occurred during the sqlite3_set_auxdata() call or if the
259065
+ ** function is being evaluated during query planning rather than during
259066
+ ** query execution.
258893
259067
  **
258894
259068
  ** ^(In practice, auxiliary data is preserved between function calls for
258895
259069
  ** function parameters that are compile-time constants, including literal
@@ -259163,6 +259337,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
259163
259337
  ** higher order bits are discarded.
259164
259338
  ** The number of subtype bytes preserved by SQLite might increase
259165
259339
  ** in future releases of SQLite.
259340
+ **
259341
+ ** Every [application-defined SQL function] that invokes this interface
259342
+ ** should include the [SQLITE_RESULT_SUBTYPE] property in its
259343
+ ** text encoding argument when the SQL function is
259344
+ ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
259345
+ ** property is omitted from the function that invokes sqlite3_result_subtype(),
259346
+ ** then in some cases the sqlite3_result_subtype() might fail to set
259347
+ ** the result subtype.
259348
+ **
259349
+ ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
259350
+ ** SQL function that invokes the sqlite3_result_subtype() interface
259351
+ ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
259352
+ ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
259353
+ ** by default.
259166
259354
  */
259167
259355
  SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
259168
259356
 
@@ -266269,7 +266457,7 @@ extern "C" {
266269
266457
  ** If the SQLITE_USER table is not present in the database file, then
266270
266458
  ** this interface is a harmless no-op returnning SQLITE_OK.
266271
266459
  */
266272
- int sqlite3_user_authenticate(
266460
+ SQLITE_API int sqlite3_user_authenticate(
266273
266461
  sqlite3 *db, /* The database connection */
266274
266462
  const char *zUsername, /* Username */
266275
266463
  const char *aPW, /* Password or credentials */
@@ -266286,7 +266474,7 @@ int sqlite3_user_authenticate(
266286
266474
  ** for any ATTACH-ed databases. Any call to sqlite3_user_add() by a
266287
266475
  ** non-admin user results in an error.
266288
266476
  */
266289
- int sqlite3_user_add(
266477
+ SQLITE_API int sqlite3_user_add(
266290
266478
  sqlite3 *db, /* Database connection */
266291
266479
  const char *zUsername, /* Username to be added */
266292
266480
  const char *aPW, /* Password or credentials */
@@ -266301,7 +266489,7 @@ int sqlite3_user_add(
266301
266489
  ** credentials or admin privilege setting. No user may change their own
266302
266490
  ** admin privilege setting.
266303
266491
  */
266304
- int sqlite3_user_change(
266492
+ SQLITE_API int sqlite3_user_change(
266305
266493
  sqlite3 *db, /* Database connection */
266306
266494
  const char *zUsername, /* Username to change */
266307
266495
  const char *aPW, /* New password or credentials */
@@ -266316,7 +266504,7 @@ int sqlite3_user_change(
266316
266504
  ** the database cannot be converted into a no-authentication-required
266317
266505
  ** database.
266318
266506
  */
266319
- int sqlite3_user_delete(
266507
+ SQLITE_API int sqlite3_user_delete(
266320
266508
  sqlite3 *db, /* Database connection */
266321
266509
  const char *zUsername /* Username to remove */
266322
266510
  );
@@ -266339,7 +266527,8 @@ int sqlite3_user_delete(
266339
266527
  #define CODEC_TYPE_CHACHA20 3
266340
266528
  #define CODEC_TYPE_SQLCIPHER 4
266341
266529
  #define CODEC_TYPE_RC4 5
266342
- #define CODEC_TYPE_MAX_BUILTIN 5
266530
+ #define CODEC_TYPE_ASCON128 6
266531
+ #define CODEC_TYPE_MAX_BUILTIN 6
266343
266532
 
266344
266533
  /*
266345
266534
  ** Definition of API functions
@@ -269200,7 +269389,16 @@ int poly1305_tagcmp(const uint8_t tag1[16], const uint8_t tag2[16])
269200
269389
  /*
269201
269390
  * Platform-specific entropy functions for seeding RNG
269202
269391
  */
269203
- #if defined(_WIN32) || defined(__CYGWIN__)
269392
+ #if defined(__WASM__)
269393
+
269394
+ extern int getentropy(void* buf, size_t n);
269395
+
269396
+ static size_t entropy(void* buf, size_t n)
269397
+ {
269398
+ return (getentropy(buf, n) == 0) ? n : 0;
269399
+ }
269400
+
269401
+ #elif defined(_WIN32) || defined(__CYGWIN__)
269204
269402
 
269205
269403
  #if SQLITE3MC_USE_RAND_S
269206
269404
 
@@ -269354,15 +269552,6 @@ static size_t entropy(void* buf, size_t n)
269354
269552
  return read_urandom(buf, n);
269355
269553
  }
269356
269554
 
269357
- #elif defined(__WASM__)
269358
-
269359
- extern size_t getentropy(void* buf, size_t n);
269360
-
269361
- static size_t entropy(void* buf, size_t n)
269362
- {
269363
- return (getentropy(buf, n) == 0) ? n : 0;
269364
- }
269365
-
269366
269555
  #else
269367
269556
  # error "Secure pseudorandom number generator not implemented for this OS"
269368
269557
  #endif
@@ -269610,7 +269799,7 @@ void sqlite3CryptFunc(
269610
269799
  ** If the SQLITE_USER table is not present in the database file, then
269611
269800
  ** this interface is a harmless no-op returnning SQLITE_OK.
269612
269801
  */
269613
- int sqlite3_user_authenticate(
269802
+ SQLITE_API int sqlite3_user_authenticate(
269614
269803
  sqlite3 *db, /* The database connection */
269615
269804
  const char *zUsername, /* Username */
269616
269805
  const char *zPW, /* Password or credentials */
@@ -269650,7 +269839,7 @@ int sqlite3_user_authenticate(
269650
269839
  ** for any ATTACH-ed databases. Any call to sqlite3_user_add() by a
269651
269840
  ** non-admin user results in an error.
269652
269841
  */
269653
- int sqlite3_user_add(
269842
+ SQLITE_API int sqlite3_user_add(
269654
269843
  sqlite3 *db, /* Database connection */
269655
269844
  const char *zUsername, /* Username to be added */
269656
269845
  const char *aPW, /* Password or credentials */
@@ -269697,7 +269886,7 @@ int sqlite3_user_add(
269697
269886
  ** credentials or admin privilege setting. No user may change their own
269698
269887
  ** admin privilege setting.
269699
269888
  */
269700
- int sqlite3_user_change(
269889
+ SQLITE_API int sqlite3_user_change(
269701
269890
  sqlite3 *db, /* Database connection */
269702
269891
  const char *zUsername, /* Username to change */
269703
269892
  const char *aPW, /* Modified password or credentials */
@@ -269748,7 +269937,7 @@ int sqlite3_user_change(
269748
269937
  ** the database cannot be converted into a no-authentication-required
269749
269938
  ** database.
269750
269939
  */
269751
- int sqlite3_user_delete(
269940
+ SQLITE_API int sqlite3_user_delete(
269752
269941
  sqlite3 *db, /* Database connection */
269753
269942
  const char *zUsername /* Username to remove */
269754
269943
  ){
@@ -272790,6 +272979,8 @@ SQLITE_PRIVATE int sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath,
272790
272979
 
272791
272980
  SQLITE_PRIVATE void sqlite3mcCodecGetKey(sqlite3* db, int nDb, void** zKey, int* nKey);
272792
272981
 
272982
+ SQLITE_PRIVATE void sqlite3mcSecureZeroMemory(void* v, size_t n);
272983
+
272793
272984
  /* Debugging */
272794
272985
 
272795
272986
  #if 0
@@ -274707,6 +274898,1582 @@ SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
274707
274898
  #endif
274708
274899
  /*** End of #include "cipher_sds_rc4.c" ***/
274709
274900
 
274901
+ /* #include "cipher_ascon.c" */
274902
+ /*** Begin of #include "cipher_ascon.c" ***/
274903
+ /*
274904
+ ** Name: cipher_ascon.c
274905
+ ** Purpose: Implementation of cipher Ascon
274906
+ ** Author: Ulrich Telle
274907
+ ** Created: 2023-11-13
274908
+ ** Copyright: (c) 2023-2023 Ulrich Telle
274909
+ ** License: MIT
274910
+ */
274911
+
274912
+ /* #include "cipher_common.h" */
274913
+
274914
+
274915
+ /* --- Ascon --- */
274916
+ #if HAVE_CIPHER_ASCON128
274917
+
274918
+ #define CIPHER_NAME_ASCON128 "ascon128"
274919
+
274920
+ /*
274921
+ ** Configuration parameters for "ascon128a"
274922
+ **
274923
+ ** - kdf_iter : number of iterations for key derivation
274924
+ */
274925
+
274926
+ #define ASCON128_KDF_ITER_DEFAULT 64007
274927
+
274928
+ /* #include "ascon/prolog.h" */
274929
+ /*** Begin of #include "ascon/prolog.h" ***/
274930
+ /*
274931
+ ** Name: prolog.h
274932
+ ** Purpose: Include important header files, before
274933
+ ** Based on: Public domain Ascon reference implementation
274934
+ ** and optimized variants for 32- and 64-bit
274935
+ ** (see https://github.com/ascon/ascon-c)
274936
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
274937
+ ** Modified by: Ulrich Telle
274938
+ ** Copyright: (c) 2023-2023 Ulrich Telle
274939
+ ** License: MIT
274940
+ */
274941
+
274942
+ #ifndef PROLOG_H
274943
+ #define PROLOG_H
274944
+
274945
+ /* #include "word.h" */
274946
+ /*** Begin of #include "word.h" ***/
274947
+ #ifndef WORD_H_
274948
+ #define WORD_H_
274949
+
274950
+ #include <stdint.h>
274951
+ #include <string.h>
274952
+
274953
+ /* #include "bendian.h" */
274954
+ /*** Begin of #include "bendian.h" ***/
274955
+ #ifndef ENDIAN_H_
274956
+ #define ENDIAN_H_
274957
+
274958
+ #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
274959
+
274960
+ /* macros for big endian machines */
274961
+ #ifdef PRAGMA_ENDIAN
274962
+ #pragma message("Using macros for big endian machines")
274963
+ #endif
274964
+ #define ASCON_U64BIG(x) (x)
274965
+ #define ASCON_U32BIG(x) (x)
274966
+ #define ASCON_U16BIG(x) (x)
274967
+
274968
+ #elif defined(_MSC_VER) || \
274969
+ (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
274970
+
274971
+ /* macros for little endian machines */
274972
+ #ifdef PRAGMA_ENDIAN
274973
+ #pragma message("Using macros for little endian machines")
274974
+ #endif
274975
+ #define ASCON_U64BIG(x) \
274976
+ (((0x00000000000000FFULL & (x)) << 56) | \
274977
+ ((0x000000000000FF00ULL & (x)) << 40) | \
274978
+ ((0x0000000000FF0000ULL & (x)) << 24) | \
274979
+ ((0x00000000FF000000ULL & (x)) << 8) | \
274980
+ ((0x000000FF00000000ULL & (x)) >> 8) | \
274981
+ ((0x0000FF0000000000ULL & (x)) >> 24) | \
274982
+ ((0x00FF000000000000ULL & (x)) >> 40) | \
274983
+ ((0xFF00000000000000ULL & (x)) >> 56))
274984
+ #define ASCON_U32BIG(x) \
274985
+ (((0x000000FF & (x)) << 24) | ((0x0000FF00 & (x)) << 8) | \
274986
+ ((0x00FF0000 & (x)) >> 8) | ((0xFF000000 & (x)) >> 24))
274987
+ #define ASCON_U16BIG(x) (((0x00FF & (x)) << 8) | ((0xFF00 & (x)) >> 8))
274988
+
274989
+ #else
274990
+ #error "Ascon byte order macros not defined in bendian.h"
274991
+ #endif
274992
+
274993
+ #endif /* ENDIAN_H_ */
274994
+ /*** End of #include "bendian.h" ***/
274995
+
274996
+ /* #include "forceinline.h" */
274997
+ /*** Begin of #include "forceinline.h" ***/
274998
+ #ifndef FORCEINLINE_H_
274999
+ #define FORCEINLINE_H_
275000
+
275001
+ /* define forceinline macro */
275002
+ #ifdef _MSC_VER
275003
+ #define forceinline __forceinline
275004
+ #elif defined(__GNUC__)
275005
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
275006
+ #define forceinline inline __attribute__((__always_inline__))
275007
+ #else
275008
+ #define forceinline static inline
275009
+ #endif
275010
+ #elif defined(__CLANG__)
275011
+ #if __has_attribute(__always_inline__)
275012
+ #define forceinline inline __attribute__((__always_inline__))
275013
+ #else
275014
+ #define forceinline inline
275015
+ #endif
275016
+ #else
275017
+ #define forceinline inline
275018
+ #endif
275019
+
275020
+ #endif /* FORCEINLINE_H_ */
275021
+ /*** End of #include "forceinline.h" ***/
275022
+
275023
+
275024
+ typedef union {
275025
+ uint64_t x;
275026
+ uint32_t w[2];
275027
+ uint8_t b[8];
275028
+ } word_t;
275029
+
275030
+ #define ASCON_U64TOWORD(x) ASCON_U64BIG(x)
275031
+ #define ASCON_WORDTOU64(x) ASCON_U64BIG(x)
275032
+
275033
+ forceinline uint64_t ASCON_ROR(uint64_t x, int n) { return x >> n | x << (-n & 63); }
275034
+
275035
+ forceinline uint64_t ASCON_KEYROT(uint64_t lo2hi, uint64_t hi2lo) {
275036
+ return lo2hi << 32 | hi2lo >> 32;
275037
+ }
275038
+
275039
+ forceinline int ASCON_NOTZERO(uint64_t a, uint64_t b) {
275040
+ uint64_t result = a | b;
275041
+ result |= result >> 32;
275042
+ result |= result >> 16;
275043
+ result |= result >> 8;
275044
+ return ((((int)(result & 0xff) - 1) >> 8) & 1) - 1;
275045
+ }
275046
+
275047
+ forceinline uint64_t ASCON_PAD(int i) { return 0x80ull << (56 - 8 * i); }
275048
+
275049
+ forceinline uint64_t ASCON_PRFS_MLEN(uint64_t len) { return len << 51; }
275050
+
275051
+ forceinline uint64_t ASCON_CLEAR(uint64_t w, int n) {
275052
+ /* undefined for n == 0 */
275053
+ uint64_t mask = ~0ull >> (8 * n);
275054
+ return w & mask;
275055
+ }
275056
+
275057
+ forceinline uint64_t ASCON_MASK(int n) {
275058
+ /* undefined for n == 0 */
275059
+ return ~0ull >> (64 - 8 * n);
275060
+ }
275061
+
275062
+ forceinline uint64_t ASCON_LOAD(const uint8_t* bytes, int n) {
275063
+ uint64_t x = *(uint64_t*)bytes & ASCON_MASK(n);
275064
+ return ASCON_U64TOWORD(x);
275065
+ }
275066
+
275067
+ forceinline void ASCON_STORE(uint8_t* bytes, uint64_t w, int n) {
275068
+ *(uint64_t*)bytes &= ~ASCON_MASK(n);
275069
+ *(uint64_t*)bytes |= ASCON_WORDTOU64(w);
275070
+ }
275071
+
275072
+ forceinline uint64_t ASCON_LOADBYTES(const uint8_t* bytes, int n) {
275073
+ uint64_t x = 0;
275074
+ memcpy(&x, bytes, n);
275075
+ return ASCON_U64TOWORD(x);
275076
+ }
275077
+
275078
+ forceinline void ASCON_STOREBYTES(uint8_t* bytes, uint64_t w, int n) {
275079
+ uint64_t x = ASCON_WORDTOU64(w);
275080
+ memcpy(bytes, &x, n);
275081
+ }
275082
+
275083
+ #endif /* WORD_H_ */
275084
+ /*** End of #include "word.h" ***/
275085
+
275086
+
275087
+ #endif
275088
+ /*** End of #include "ascon/prolog.h" ***/
275089
+
275090
+ /* #include "ascon/aead.c" */
275091
+ /*** Begin of #include "ascon/aead.c" ***/
275092
+ /*
275093
+ ** Name: aead.c
275094
+ ** Purpose: Stream encryption/decryption with Ascon
275095
+ ** Based on: Public domain Ascon reference implementation
275096
+ ** and optimized variants for 32- and 64-bit
275097
+ ** (see https://github.com/ascon/ascon-c)
275098
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275099
+ ** Modified by: Ulrich Telle
275100
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275101
+ ** License: MIT
275102
+ */
275103
+
275104
+ /* #include "api.h" */
275105
+ /*** Begin of #include "api.h" ***/
275106
+ /*
275107
+ ** Name: api.h
275108
+ ** Purpose: Definition of preprocessor symbols
275109
+ ** Based on: Public domain Ascon reference implementation
275110
+ ** and optimized variants for 32- and 64-bit
275111
+ ** (see https://github.com/ascon/ascon-c)
275112
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275113
+ ** Combined symbols from AEAD and HASH
275114
+ ** Modified by: Ulrich Telle
275115
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275116
+ ** License: MIT
275117
+ */
275118
+
275119
+ #define CRYPTO_VERSION "1.2.7"
275120
+ #define CRYPTO_KEYBYTES 16
275121
+ #define CRYPTO_NSECBYTES 0
275122
+ #define CRYPTO_NPUBBYTES 16
275123
+ #define CRYPTO_ABYTES 16
275124
+ #define CRYPTO_NOOVERLAP 1
275125
+ #define ASCON_AEAD_RATE 8
275126
+
275127
+ #define CRYPTO_BYTES 32
275128
+ #define ASCON_HASH_BYTES 32 /* HASH */
275129
+ #define ASCON_HASH_ROUNDS 12
275130
+
275131
+ #define ASCON_AEAD_KEY_LEN CRYPTO_KEYBYTES
275132
+ #define ASCON_AEAD_NONCE_LEN CRYPTO_NPUBBYTES
275133
+ #define ASCON_AEAD_TAG_LEN CRYPTO_ABYTES
275134
+ #define ASCON_SALT_LEN CRYPTO_NPUBBYTES
275135
+ /*** End of #include "api.h" ***/
275136
+
275137
+ /* #include "ascon.h" */
275138
+ /*** Begin of #include "ascon.h" ***/
275139
+ #ifndef ASCON_H_
275140
+ #define ASCON_H_
275141
+
275142
+ #include <stdint.h>
275143
+
275144
+ /* #include "api.h" */
275145
+
275146
+ /* #include "config.h" */
275147
+ /*** Begin of #include "config.h" ***/
275148
+ #ifndef CONFIG_H_
275149
+ #define CONFIG_H_
275150
+
275151
+ /* inline the ascon mode */
275152
+ #ifndef ASCON_INLINE_MODE
275153
+ #define ASCON_INLINE_MODE 1
275154
+ #endif
275155
+
275156
+ /* inline all permutations */
275157
+ #ifndef ASCON_INLINE_PERM
275158
+ #define ASCON_INLINE_PERM 1
275159
+ #endif
275160
+
275161
+ /* unroll permutation loops */
275162
+ #ifndef ASCON_UNROLL_LOOPS
275163
+ #define ASCON_UNROLL_LOOPS 1
275164
+ #endif
275165
+
275166
+ #endif /* CONFIG_H_ */
275167
+ /*** End of #include "config.h" ***/
275168
+
275169
+
275170
+ typedef union {
275171
+ uint64_t x[5];
275172
+ uint32_t w[5][2];
275173
+ uint8_t b[5][8];
275174
+ } ascon_state_t;
275175
+
275176
+ #ifdef ASCON_AEAD_RATE
275177
+
275178
+ #define ASCON_KEYWORDS (CRYPTO_KEYBYTES + 7) / 8
275179
+
275180
+ typedef union {
275181
+ uint64_t x[ASCON_KEYWORDS];
275182
+ uint32_t w[ASCON_KEYWORDS][2];
275183
+ uint8_t b[ASCON_KEYWORDS][8];
275184
+ } ascon_key_t;
275185
+
275186
+ #if !ASCON_INLINE_MODE
275187
+
275188
+ void ascon_loadkey(ascon_key_t* key, const uint8_t* k);
275189
+ void ascon_initaead(ascon_state_t* s, const ascon_key_t* key,
275190
+ const uint8_t* npub);
275191
+ void ascon_adata(ascon_state_t* s, const uint8_t* ad, uint64_t adlen);
275192
+ void ascon_encrypt(ascon_state_t* s, uint8_t* c, const uint8_t* m,
275193
+ uint64_t mlen);
275194
+ void ascon_decrypt(ascon_state_t* s, uint8_t* m, const uint8_t* c,
275195
+ uint64_t clen);
275196
+ void ascon_final(ascon_state_t* s, const ascon_key_t* k);
275197
+
275198
+ #endif
275199
+
275200
+ #endif
275201
+
275202
+ #ifdef ASCON_HASH_BYTES
275203
+
275204
+ #if !ASCON_INLINE_MODE
275205
+
275206
+ void ascon_inithash(ascon_state_t* s);
275207
+ void ascon_absorb(ascon_state_t* s, const uint8_t* in, uint64_t inlen);
275208
+ void ascon_squeeze(ascon_state_t* s, uint8_t* out, uint64_t outlen);
275209
+
275210
+ #endif
275211
+
275212
+ #endif
275213
+
275214
+ #endif /* ASCON_H_ */
275215
+ /*** End of #include "ascon.h" ***/
275216
+
275217
+ /* #include "crypto_aead.h" */
275218
+ /*** Begin of #include "crypto_aead.h" ***/
275219
+ /*
275220
+ ** Name: hash.c
275221
+ ** Purpose: API definition for Hash algorithm with Ascon
275222
+ ** Based on: Public domain Ascon reference implementation
275223
+ ** and optimized variants for 32- and 64-bit
275224
+ ** (see https://github.com/ascon/ascon-c)
275225
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275226
+ ** Modified by: Ulrich Telle
275227
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275228
+ ** License: MIT
275229
+ */
275230
+
275231
+ #ifndef CRYPTO_AEAD_H
275232
+ #define CRYPTO_AEAD_H
275233
+
275234
+ #include <stddef.h>
275235
+
275236
+ /*
275237
+ ** Encryption using ASCON-AEAD.
275238
+ **
275239
+ ** \param ctext Output buffer for encrypted text (same length as plain text)
275240
+ ** \param tag Output buffer for tag with fixed length of ASCON_AEAD_TAG_LEN
275241
+ ** \param mtext Input buffer with plain message text
275242
+ ** \param mlen Length of message text
275243
+ ** \param ad Input buffer with associated data
275244
+ ** \param adlen Length of associated data
275245
+ ** \param nonce Buffer with nonce data
275246
+ ** \param k Buffer with key data
275247
+ */
275248
+ int ascon_aead_encrypt(uint8_t* ctext, uint8_t tag[ASCON_AEAD_TAG_LEN],
275249
+ const uint8_t* mtext, uint64_t mlen,
275250
+ const uint8_t* ad, uint64_t adlen,
275251
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275252
+ const uint8_t k[ASCON_AEAD_KEY_LEN]);
275253
+
275254
+ /*
275255
+ ** Encryption using ASCON-AEAD.
275256
+ **
275257
+ ** \param mtext Output buffer with decrypted plain message text (same length as encrypted text)
275258
+ ** \param ctext Input buffer for encrypted text
275259
+ ** \param clen Length of encrypted text
275260
+ ** \param ad Input buffer with associated data
275261
+ ** \param adlen Length of associated data
275262
+ ** \param tag Input buffer for expected tag with fixed length of ASCON_AEAD_TAG_LEN
275263
+ ** \param nonce Buffer with nonce data
275264
+ ** \param k Buffer with key data
275265
+ */
275266
+ int ascon_aead_decrypt(uint8_t* mtext, const uint8_t* ctext, uint64_t clen,
275267
+ const uint8_t* ad, uint64_t adlen,
275268
+ const uint8_t tag[ASCON_AEAD_TAG_LEN],
275269
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275270
+ const uint8_t k[ASCON_AEAD_KEY_LEN]);
275271
+
275272
+ #endif
275273
+ /*** End of #include "crypto_aead.h" ***/
275274
+
275275
+ /* #include "permutations.h" */
275276
+ /*** Begin of #include "permutations.h" ***/
275277
+ #ifndef PERMUTATIONS_H_
275278
+ #define PERMUTATIONS_H_
275279
+
275280
+ #include <stdint.h>
275281
+
275282
+ /* #include "api.h" */
275283
+
275284
+ /* #include "ascon.h" */
275285
+
275286
+ /* #include "config.h" */
275287
+
275288
+ /* #include "constants.h" */
275289
+ /*** Begin of #include "constants.h" ***/
275290
+ #ifndef CONSTANTS_H_
275291
+ #define CONSTANTS_H_
275292
+
275293
+ #include <stdint.h>
275294
+
275295
+ #define ASCON_128_KEYBYTES 16
275296
+ #define ASCON_128A_KEYBYTES 16
275297
+ #define ASCON_80PQ_KEYBYTES 20
275298
+
275299
+ #define ASCON_128_RATE 8
275300
+ #define ASCON_128A_RATE 16
275301
+ #define ASCON_HASH_RATE 8
275302
+ #define ASCON_PRF_IN_RATE 32
275303
+ #define ASCON_PRFA_IN_RATE 40
275304
+ #define ASCON_PRF_OUT_RATE 16
275305
+
275306
+ #define ASCON_128_PA_ROUNDS 12
275307
+ #define ASCON_128_PB_ROUNDS 6
275308
+ #define ASCON_128A_PA_ROUNDS 12
275309
+ #define ASCON_128A_PB_ROUNDS 8
275310
+
275311
+ #define ASCON_HASH_PA_ROUNDS 12
275312
+ #define ASCON_HASH_PB_ROUNDS 12
275313
+ #define ASCON_HASHA_PA_ROUNDS 12
275314
+ #define ASCON_HASHA_PB_ROUNDS 8
275315
+
275316
+ #define ASCON_PRF_PA_ROUNDS 12
275317
+ #define ASCON_PRF_PB_ROUNDS 12
275318
+ #define ASCON_PRFA_PA_ROUNDS 12
275319
+ #define ASCON_PRFA_PB_ROUNDS 8
275320
+
275321
+ #define ASCON_128_IV 0x80400c0600000000ull
275322
+ #define ASCON_128A_IV 0x80800c0800000000ull
275323
+ #define ASCON_80PQ_IV 0xa0400c0600000000ull
275324
+
275325
+ #define ASCON_HASH_IV 0x00400c0000000100ull
275326
+ #define ASCON_HASHA_IV 0x00400c0400000100ull
275327
+ #define ASCON_XOF_IV 0x00400c0000000000ull
275328
+ #define ASCON_XOFA_IV 0x00400c0400000000ull
275329
+
275330
+ #define ASCON_HASH_IV0 0xee9398aadb67f03dull
275331
+ #define ASCON_HASH_IV1 0x8bb21831c60f1002ull
275332
+ #define ASCON_HASH_IV2 0xb48a92db98d5da62ull
275333
+ #define ASCON_HASH_IV3 0x43189921b8f8e3e8ull
275334
+ #define ASCON_HASH_IV4 0x348fa5c9d525e140ull
275335
+
275336
+ #define ASCON_HASHA_IV0 0x01470194fc6528a6ull
275337
+ #define ASCON_HASHA_IV1 0x738ec38ac0adffa7ull
275338
+ #define ASCON_HASHA_IV2 0x2ec8e3296c76384cull
275339
+ #define ASCON_HASHA_IV3 0xd6f6a54d7f52377dull
275340
+ #define ASCON_HASHA_IV4 0xa13c42a223be8d87ull
275341
+
275342
+ #define ASCON_XOF_IV0 0xb57e273b814cd416ull
275343
+ #define ASCON_XOF_IV1 0x2b51042562ae2420ull
275344
+ #define ASCON_XOF_IV2 0x66a3a7768ddf2218ull
275345
+ #define ASCON_XOF_IV3 0x5aad0a7a8153650cull
275346
+ #define ASCON_XOF_IV4 0x4f3e0e32539493b6ull
275347
+
275348
+ #define ASCON_XOFA_IV0 0x44906568b77b9832ull
275349
+ #define ASCON_XOFA_IV1 0xcd8d6cae53455532ull
275350
+ #define ASCON_XOFA_IV2 0xf7b5212756422129ull
275351
+ #define ASCON_XOFA_IV3 0x246885e1de0d225bull
275352
+ #define ASCON_XOFA_IV4 0xa8cb5ce33449973full
275353
+
275354
+ #define ASCON_MAC_IV 0x80808c0000000080ull
275355
+ #define ASCON_MACA_IV 0x80808c0400000080ull
275356
+ #define ASCON_PRF_IV 0x80808c0000000000ull
275357
+ #define ASCON_PRFA_IV 0x80808c0400000000ull
275358
+ #define ASCON_PRFS_IV 0x80004c8000000000ull
275359
+
275360
+ #define ASCON_RC0 0xf0
275361
+ #define ASCON_RC1 0xe1
275362
+ #define ASCON_RC2 0xd2
275363
+ #define ASCON_RC3 0xc3
275364
+ #define ASCON_RC4 0xb4
275365
+ #define ASCON_RC5 0xa5
275366
+ #define ASCON_RC6 0x96
275367
+ #define ASCON_RC7 0x87
275368
+ #define ASCON_RC8 0x78
275369
+ #define ASCON_RC9 0x69
275370
+ #define ASCON_RCa 0x5a
275371
+ #define ASCON_RCb 0x4b
275372
+
275373
+ #define ASCON_RC(i) (i)
275374
+
275375
+ #define ASCON_START(n) ((3 + (n)) << 4 | (12 - (n)))
275376
+ #define ASCON_INC -0x0f
275377
+ #define ASCON_END 0x3c
275378
+
275379
+ #endif /* CONSTANTS_H_ */
275380
+ /*** End of #include "constants.h" ***/
275381
+
275382
+ /* #include "printstate.h" */
275383
+ /*** Begin of #include "printstate.h" ***/
275384
+ #ifndef PRINTSTATE_H_
275385
+ #define PRINTSTATE_H_
275386
+
275387
+ #ifdef ASCON_PRINT_STATE
275388
+
275389
+ /* #include "ascon.h" */
275390
+
275391
+ /* #include "word.h" */
275392
+
275393
+
275394
+ void ascon_printword(const char* text, const uint64_t x);
275395
+ void ascon_printstate(const char* text, const ascon_state_t* s);
275396
+
275397
+ #else
275398
+
275399
+ #define ascon_printword(text, w) \
275400
+ do { \
275401
+ } while (0)
275402
+
275403
+ #define ascon_printstate(text, s) \
275404
+ do { \
275405
+ } while (0)
275406
+
275407
+ #endif
275408
+
275409
+ #endif /* PRINTSTATE_H_ */
275410
+ /*** End of #include "printstate.h" ***/
275411
+
275412
+ /* #include "round.h" */
275413
+ /*** Begin of #include "round.h" ***/
275414
+ /*
275415
+ ** Name: round.h
275416
+ ** Purpose: Selector for Ascon implementation variant for 32- resp 64-bit
275417
+ ** Based on: Public domain Ascon reference implementation
275418
+ ** and optimized variants for 32- and 64-bit
275419
+ ** (see https://github.com/ascon/ascon-c)
275420
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275421
+ ** Modified by: Ulrich Telle
275422
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275423
+ ** License: MIT
275424
+ */
275425
+
275426
+ #ifndef ROUND_H
275427
+ #define ROUND_H
275428
+
275429
+ /* #include "forceinline.h" */
275430
+
275431
+
275432
+ #if defined(__LP64__) || defined(_WIN64)
275433
+ /* 64-bit machine, Windows or Linux or OS X */
275434
+ /* #include "round64.h" */
275435
+ /*** Begin of #include "round64.h" ***/
275436
+ #ifndef ROUND64_H_
275437
+ #define ROUND64_H_
275438
+
275439
+ /* #include "ascon.h" */
275440
+
275441
+ /* #include "constants.h" */
275442
+
275443
+ /* #include "forceinline.h" */
275444
+
275445
+ /* #include "printstate.h" */
275446
+
275447
+ /* #include "word.h" */
275448
+
275449
+
275450
+ forceinline void ASCON_ROUND(ascon_state_t* s, uint8_t C) {
275451
+ ascon_state_t t;
275452
+ /* round constant */
275453
+ s->x[2] ^= C;
275454
+ /* s-box layer */
275455
+ s->x[0] ^= s->x[4];
275456
+ s->x[4] ^= s->x[3];
275457
+ s->x[2] ^= s->x[1];
275458
+ t.x[0] = s->x[0] ^ (~s->x[1] & s->x[2]);
275459
+ t.x[2] = s->x[2] ^ (~s->x[3] & s->x[4]);
275460
+ t.x[4] = s->x[4] ^ (~s->x[0] & s->x[1]);
275461
+ t.x[1] = s->x[1] ^ (~s->x[2] & s->x[3]);
275462
+ t.x[3] = s->x[3] ^ (~s->x[4] & s->x[0]);
275463
+ t.x[1] ^= t.x[0];
275464
+ t.x[3] ^= t.x[2];
275465
+ t.x[0] ^= t.x[4];
275466
+ /* linear layer */
275467
+ s->x[2] = t.x[2] ^ ASCON_ROR(t.x[2], 6 - 1);
275468
+ s->x[3] = t.x[3] ^ ASCON_ROR(t.x[3], 17 - 10);
275469
+ s->x[4] = t.x[4] ^ ASCON_ROR(t.x[4], 41 - 7);
275470
+ s->x[0] = t.x[0] ^ ASCON_ROR(t.x[0], 28 - 19);
275471
+ s->x[1] = t.x[1] ^ ASCON_ROR(t.x[1], 61 - 39);
275472
+ s->x[2] = t.x[2] ^ ASCON_ROR(s->x[2], 1);
275473
+ s->x[3] = t.x[3] ^ ASCON_ROR(s->x[3], 10);
275474
+ s->x[4] = t.x[4] ^ ASCON_ROR(s->x[4], 7);
275475
+ s->x[0] = t.x[0] ^ ASCON_ROR(s->x[0], 19);
275476
+ s->x[1] = t.x[1] ^ ASCON_ROR(s->x[1], 39);
275477
+ s->x[2] = ~s->x[2];
275478
+ ascon_printstate(" round output", s);
275479
+ }
275480
+
275481
+ forceinline void ASCON_PROUNDS(ascon_state_t* s, int nr) {
275482
+ int i = ASCON_START(nr);
275483
+ do {
275484
+ ASCON_ROUND(s, ASCON_RC(i));
275485
+ i += ASCON_INC;
275486
+ } while (i != ASCON_END);
275487
+ }
275488
+
275489
+ #endif /* ROUND64_H_ */
275490
+ /*** End of #include "round64.h" ***/
275491
+
275492
+ #else
275493
+ /* 32-bit machine, Windows or Linux or OS X */
275494
+ /* #include "round32.h" */
275495
+ /*** Begin of #include "round32.h" ***/
275496
+ #ifndef ROUND32_H_
275497
+ #define ROUND32_H_
275498
+
275499
+ /* #include "ascon.h" */
275500
+
275501
+ /* #include "constants.h" */
275502
+
275503
+ /* #include "forceinline.h" */
275504
+
275505
+ /* #include "printstate.h" */
275506
+
275507
+ /* #include "word.h" */
275508
+
275509
+
275510
+ forceinline void ASCON_ROUND(ascon_state_t* s, uint8_t C) {
275511
+ uint64_t xtemp;
275512
+ /* round constant */
275513
+ s->x[2] ^= C;
275514
+ /* s-box layer */
275515
+ s->x[0] ^= s->x[4];
275516
+ s->x[4] ^= s->x[3];
275517
+ s->x[2] ^= s->x[1];
275518
+ xtemp = s->x[0] & ~s->x[4];
275519
+ s->x[0] ^= s->x[2] & ~s->x[1];
275520
+ s->x[2] ^= s->x[4] & ~s->x[3];
275521
+ s->x[4] ^= s->x[1] & ~s->x[0];
275522
+ s->x[1] ^= s->x[3] & ~s->x[2];
275523
+ s->x[3] ^= xtemp;
275524
+ s->x[1] ^= s->x[0];
275525
+ s->x[3] ^= s->x[2];
275526
+ s->x[0] ^= s->x[4];
275527
+ s->x[2] = ~s->x[2];
275528
+ /* linear layer */
275529
+ s->x[0] ^=
275530
+ (s->x[0] >> 19) ^ (s->x[0] << 45) ^ (s->x[0] >> 28) ^ (s->x[0] << 36);
275531
+ s->x[1] ^=
275532
+ (s->x[1] >> 61) ^ (s->x[1] << 3) ^ (s->x[1] >> 39) ^ (s->x[1] << 25);
275533
+ s->x[2] ^=
275534
+ (s->x[2] >> 1) ^ (s->x[2] << 63) ^ (s->x[2] >> 6) ^ (s->x[2] << 58);
275535
+ s->x[3] ^=
275536
+ (s->x[3] >> 10) ^ (s->x[3] << 54) ^ (s->x[3] >> 17) ^ (s->x[3] << 47);
275537
+ s->x[4] ^=
275538
+ (s->x[4] >> 7) ^ (s->x[4] << 57) ^ (s->x[4] >> 41) ^ (s->x[4] << 23);
275539
+ ascon_printstate(" round output", s);
275540
+ }
275541
+
275542
+ forceinline void ASCON_PROUNDS(ascon_state_t* s, int nr) {
275543
+ int i = ASCON_START(nr);
275544
+ do {
275545
+ ASCON_ROUND(s, ASCON_RC(i));
275546
+ i += ASCON_INC;
275547
+ } while (i != ASCON_END);
275548
+ }
275549
+
275550
+ #endif /* ROUND32_H_ */
275551
+ /*** End of #include "round32.h" ***/
275552
+
275553
+ #endif
275554
+
275555
+ #endif
275556
+ /*** End of #include "round.h" ***/
275557
+
275558
+
275559
+ forceinline void ASCON_P12ROUNDS(ascon_state_t* s) {
275560
+ ASCON_ROUND(s, ASCON_RC0);
275561
+ ASCON_ROUND(s, ASCON_RC1);
275562
+ ASCON_ROUND(s, ASCON_RC2);
275563
+ ASCON_ROUND(s, ASCON_RC3);
275564
+ ASCON_ROUND(s, ASCON_RC4);
275565
+ ASCON_ROUND(s, ASCON_RC5);
275566
+ ASCON_ROUND(s, ASCON_RC6);
275567
+ ASCON_ROUND(s, ASCON_RC7);
275568
+ ASCON_ROUND(s, ASCON_RC8);
275569
+ ASCON_ROUND(s, ASCON_RC9);
275570
+ ASCON_ROUND(s, ASCON_RCa);
275571
+ ASCON_ROUND(s, ASCON_RCb);
275572
+ }
275573
+
275574
+ forceinline void ASCON_P8ROUNDS(ascon_state_t* s) {
275575
+ ASCON_ROUND(s, ASCON_RC4);
275576
+ ASCON_ROUND(s, ASCON_RC5);
275577
+ ASCON_ROUND(s, ASCON_RC6);
275578
+ ASCON_ROUND(s, ASCON_RC7);
275579
+ ASCON_ROUND(s, ASCON_RC8);
275580
+ ASCON_ROUND(s, ASCON_RC9);
275581
+ ASCON_ROUND(s, ASCON_RCa);
275582
+ ASCON_ROUND(s, ASCON_RCb);
275583
+ }
275584
+
275585
+ forceinline void ASCON_P6ROUNDS(ascon_state_t* s) {
275586
+ ASCON_ROUND(s, ASCON_RC6);
275587
+ ASCON_ROUND(s, ASCON_RC7);
275588
+ ASCON_ROUND(s, ASCON_RC8);
275589
+ ASCON_ROUND(s, ASCON_RC9);
275590
+ ASCON_ROUND(s, ASCON_RCa);
275591
+ ASCON_ROUND(s, ASCON_RCb);
275592
+ }
275593
+
275594
+ #if ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
275595
+
275596
+ forceinline void ASCON_P(ascon_state_t* s, int nr) {
275597
+ if (nr == 12) ASCON_P12ROUNDS(s);
275598
+ if (nr == 8) ASCON_P8ROUNDS(s);
275599
+ if (nr == 6) ASCON_P6ROUNDS(s);
275600
+ }
275601
+
275602
+ #elif !ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
275603
+
275604
+ void ASCON_P12(ascon_state_t* s);
275605
+ void ASCON_P8(ascon_state_t* s);
275606
+ void ASCON_P6(ascon_state_t* s);
275607
+
275608
+ forceinline void ASCON_P(ascon_state_t* s, int nr) {
275609
+ if (nr == 12) ASCON_P12(s);
275610
+ if (nr == 8) ASCON_P8(s);
275611
+ if (nr == 6) ASCON_P6(s);
275612
+ }
275613
+
275614
+ #elif ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS
275615
+
275616
+ forceinline void ASCON_P(ascon_state_t* s, int nr) { ASCON_PROUNDS(s, nr); }
275617
+
275618
+ #else /* !ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS */
275619
+
275620
+ void ASCON_P(ascon_state_t* s, int nr);
275621
+
275622
+ #endif
275623
+
275624
+ #endif /* PERMUTATIONS_H_ */
275625
+ /*** End of #include "permutations.h" ***/
275626
+
275627
+ /* #include "printstate.h" */
275628
+
275629
+
275630
+ #if !ASCON_INLINE_MODE
275631
+ #undef forceinline
275632
+ #define forceinline
275633
+ #endif
275634
+
275635
+ #ifdef ASCON_AEAD_RATE
275636
+
275637
+ forceinline void ascon_loadkey(ascon_key_t* key, const uint8_t* k) {
275638
+ #if CRYPTO_KEYBYTES == 16
275639
+ key->x[0] = ASCON_LOAD(k, 8);
275640
+ key->x[1] = ASCON_LOAD(k + 8, 8);
275641
+ #else /* CRYPTO_KEYBYTES == 20 */
275642
+ key->x[0] = ASCON_KEYROT(0, ASCON_LOADBYTES(k, 4));
275643
+ key->x[1] = ASCON_LOADBYTES(k + 4, 8);
275644
+ key->x[2] = ASCON_LOADBYTES(k + 12, 8);
275645
+ #endif
275646
+ }
275647
+
275648
+ forceinline void ascon_initaead(ascon_state_t* s, const ascon_key_t* key,
275649
+ const uint8_t* npub) {
275650
+ #if CRYPTO_KEYBYTES == 16
275651
+ if (ASCON_AEAD_RATE == 8) s->x[0] = ASCON_128_IV;
275652
+ if (ASCON_AEAD_RATE == 16) s->x[0] = ASCON_128A_IV;
275653
+ s->x[1] = key->x[0];
275654
+ s->x[2] = key->x[1];
275655
+ #else /* CRYPTO_KEYBYTES == 20 */
275656
+ s->x[0] = key->x[0] ^ ASCON_80PQ_IV;
275657
+ s->x[1] = key->x[1];
275658
+ s->x[2] = key->x[2];
275659
+ #endif
275660
+ s->x[3] = ASCON_LOAD(npub, 8);
275661
+ s->x[4] = ASCON_LOAD(npub + 8, 8);
275662
+ ascon_printstate("init 1st key xor", s);
275663
+ ASCON_P(s, 12);
275664
+ #if CRYPTO_KEYBYTES == 16
275665
+ s->x[3] ^= key->x[0];
275666
+ s->x[4] ^= key->x[1];
275667
+ #else /* CRYPTO_KEYBYTES == 20 */
275668
+ s->x[2] ^= key->x[0];
275669
+ s->x[3] ^= key->x[1];
275670
+ s->x[4] ^= key->x[2];
275671
+ #endif
275672
+ ascon_printstate("init 2nd key xor", s);
275673
+ }
275674
+
275675
+ forceinline void ascon_adata(ascon_state_t* s, const uint8_t* ad,
275676
+ uint64_t adlen) {
275677
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275678
+ if (adlen) {
275679
+ /* full associated data blocks */
275680
+ while (adlen >= ASCON_AEAD_RATE) {
275681
+ s->x[0] ^= ASCON_LOAD(ad, 8);
275682
+ if (ASCON_AEAD_RATE == 16) s->x[1] ^= ASCON_LOAD(ad + 8, 8);
275683
+ ascon_printstate("absorb adata", s);
275684
+ ASCON_P(s, nr);
275685
+ ad += ASCON_AEAD_RATE;
275686
+ adlen -= ASCON_AEAD_RATE;
275687
+ }
275688
+ /* final associated data block */
275689
+ uint64_t* px = &s->x[0];
275690
+ if (ASCON_AEAD_RATE == 16 && adlen >= 8) {
275691
+ s->x[0] ^= ASCON_LOAD(ad, 8);
275692
+ px = &s->x[1];
275693
+ ad += 8;
275694
+ adlen -= 8;
275695
+ }
275696
+ *px ^= ASCON_PAD(adlen);
275697
+ if (adlen) *px ^= ASCON_LOADBYTES(ad, adlen);
275698
+ ascon_printstate("pad adata", s);
275699
+ ASCON_P(s, nr);
275700
+ }
275701
+ /* domain separation */
275702
+ s->x[4] ^= 1;
275703
+ ascon_printstate("domain separation", s);
275704
+ }
275705
+
275706
+ forceinline void ascon_encrypt(ascon_state_t* s, uint8_t* c, const uint8_t* m,
275707
+ uint64_t mlen) {
275708
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275709
+ /* full plaintext blocks */
275710
+ while (mlen >= ASCON_AEAD_RATE) {
275711
+ s->x[0] ^= ASCON_LOAD(m, 8);
275712
+ ASCON_STORE(c, s->x[0], 8);
275713
+ if (ASCON_AEAD_RATE == 16) {
275714
+ s->x[1] ^= ASCON_LOAD(m + 8, 8);
275715
+ ASCON_STORE(c + 8, s->x[1], 8);
275716
+ }
275717
+ ascon_printstate("absorb plaintext", s);
275718
+ ASCON_P(s, nr);
275719
+ m += ASCON_AEAD_RATE;
275720
+ c += ASCON_AEAD_RATE;
275721
+ mlen -= ASCON_AEAD_RATE;
275722
+ }
275723
+ /* final plaintext block */
275724
+ uint64_t* px = &s->x[0];
275725
+ if (ASCON_AEAD_RATE == 16 && mlen >= 8) {
275726
+ s->x[0] ^= ASCON_LOAD(m, 8);
275727
+ ASCON_STORE(c, s->x[0], 8);
275728
+ px = &s->x[1];
275729
+ m += 8;
275730
+ c += 8;
275731
+ mlen -= 8;
275732
+ }
275733
+ *px ^= ASCON_PAD(mlen);
275734
+ if (mlen) {
275735
+ *px ^= ASCON_LOADBYTES(m, mlen);
275736
+ ASCON_STOREBYTES(c, *px, mlen);
275737
+ }
275738
+ ascon_printstate("pad plaintext", s);
275739
+ }
275740
+
275741
+ forceinline void ascon_decrypt(ascon_state_t* s, uint8_t* m, const uint8_t* c,
275742
+ uint64_t clen) {
275743
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275744
+ /* full ciphertext blocks */
275745
+ while (clen >= ASCON_AEAD_RATE) {
275746
+ uint64_t cx = ASCON_LOAD(c, 8);
275747
+ s->x[0] ^= cx;
275748
+ ASCON_STORE(m, s->x[0], 8);
275749
+ s->x[0] = cx;
275750
+ if (ASCON_AEAD_RATE == 16) {
275751
+ cx = ASCON_LOAD(c + 8, 8);
275752
+ s->x[1] ^= cx;
275753
+ ASCON_STORE(m + 8, s->x[1], 8);
275754
+ s->x[1] = cx;
275755
+ }
275756
+ ascon_printstate("insert ciphertext", s);
275757
+ ASCON_P(s, nr);
275758
+ m += ASCON_AEAD_RATE;
275759
+ c += ASCON_AEAD_RATE;
275760
+ clen -= ASCON_AEAD_RATE;
275761
+ }
275762
+ /* final ciphertext block */
275763
+ uint64_t* px = &s->x[0];
275764
+ if (ASCON_AEAD_RATE == 16 && clen >= 8) {
275765
+ uint64_t cx = ASCON_LOAD(c, 8);
275766
+ s->x[0] ^= cx;
275767
+ ASCON_STORE(m, s->x[0], 8);
275768
+ s->x[0] = cx;
275769
+ px = &s->x[1];
275770
+ m += 8;
275771
+ c += 8;
275772
+ clen -= 8;
275773
+ }
275774
+ *px ^= ASCON_PAD(clen);
275775
+ if (clen) {
275776
+ uint64_t cx = ASCON_LOADBYTES(c, clen);
275777
+ *px ^= cx;
275778
+ ASCON_STOREBYTES(m, *px, clen);
275779
+ *px = ASCON_CLEAR(*px, clen);
275780
+ *px ^= cx;
275781
+ }
275782
+ ascon_printstate("pad ciphertext", s);
275783
+ }
275784
+
275785
+ forceinline void ascon_final(ascon_state_t* s, const ascon_key_t* key) {
275786
+ #if CRYPTO_KEYBYTES == 16
275787
+ if (ASCON_AEAD_RATE == 8) {
275788
+ s->x[1] ^= key->x[0];
275789
+ s->x[2] ^= key->x[1];
275790
+ } else {
275791
+ s->x[2] ^= key->x[0];
275792
+ s->x[3] ^= key->x[1];
275793
+ }
275794
+ #else /* CRYPTO_KEYBYTES == 20 */
275795
+ s->x[1] ^= KEYROT(key->x[0], key->x[1]);
275796
+ s->x[2] ^= KEYROT(key->x[1], key->x[2]);
275797
+ s->x[3] ^= KEYROT(key->x[2], 0);
275798
+ #endif
275799
+ ascon_printstate("final 1st key xor", s);
275800
+ ASCON_P(s, 12);
275801
+ #if CRYPTO_KEYBYTES == 16
275802
+ s->x[3] ^= key->x[0];
275803
+ s->x[4] ^= key->x[1];
275804
+ #else /* CRYPTO_KEYBYTES == 20 */
275805
+ s->x[3] ^= key->x[1];
275806
+ s->x[4] ^= key->x[2];
275807
+ #endif
275808
+ ascon_printstate("final 2nd key xor", s);
275809
+ }
275810
+
275811
+ int ascon_aead_encrypt(uint8_t* ctext,
275812
+ uint8_t tag[ASCON_AEAD_TAG_LEN],
275813
+ const uint8_t* mtext, uint64_t mlen,
275814
+ const uint8_t* ad, uint64_t adlen,
275815
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275816
+ const uint8_t k[ASCON_AEAD_KEY_LEN])
275817
+ {
275818
+ ascon_state_t s;
275819
+ /* perform ascon computation */
275820
+ ascon_key_t key;
275821
+ ascon_loadkey(&key, k);
275822
+ ascon_initaead(&s, &key, nonce);
275823
+ ascon_adata(&s, ad, adlen);
275824
+ ascon_encrypt(&s, ctext, mtext, mlen);
275825
+ ascon_final(&s, &key);
275826
+ /* set tag */
275827
+ ASCON_STOREBYTES(tag, s.x[3], 8);
275828
+ ASCON_STOREBYTES(tag + 8, s.x[4], 8);
275829
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275830
+ sqlite3mcSecureZeroMemory(&key, sizeof(ascon_key_t));
275831
+ return 0;
275832
+ }
275833
+
275834
+ int ascon_aead_decrypt(uint8_t* mtext,
275835
+ const uint8_t* ctext, uint64_t clen,
275836
+ const uint8_t* ad, uint64_t adlen,
275837
+ const uint8_t tag[ASCON_AEAD_TAG_LEN],
275838
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275839
+ const uint8_t k[ASCON_AEAD_KEY_LEN])
275840
+ {
275841
+ int rc = 0;
275842
+ ascon_state_t s;
275843
+ if (clen < CRYPTO_ABYTES) return -1;
275844
+ /* perform ascon computation */
275845
+ ascon_key_t key;
275846
+ ascon_loadkey(&key, k);
275847
+ ascon_initaead(&s, &key, nonce);
275848
+ ascon_adata(&s, ad, adlen);
275849
+ ascon_decrypt(&s, mtext, ctext, clen);
275850
+ ascon_final(&s, &key);
275851
+ /* verify tag (should be constant time, check compiler output) */
275852
+ s.x[3] ^= ASCON_LOADBYTES(tag, 8);
275853
+ s.x[4] ^= ASCON_LOADBYTES(tag + 8, 8);
275854
+ rc = ASCON_NOTZERO(s.x[3], s.x[4]);
275855
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275856
+ sqlite3mcSecureZeroMemory(&key, sizeof(ascon_key_t));
275857
+ return rc;
275858
+ }
275859
+
275860
+ #endif
275861
+ /*** End of #include "ascon/aead.c" ***/
275862
+
275863
+ /* #include "ascon/hash.c" */
275864
+ /*** Begin of #include "ascon/hash.c" ***/
275865
+ /*
275866
+ ** Name: hash.c
275867
+ ** Purpose: Hash algorithm with Ascon
275868
+ ** Based on: Public domain Ascon reference implementation
275869
+ ** and optimized variants for 32- and 64-bit
275870
+ ** (see https://github.com/ascon/ascon-c)
275871
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275872
+ ** Modified by: Ulrich Telle
275873
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275874
+ ** License: MIT
275875
+ */
275876
+
275877
+ /* #include "api.h" */
275878
+
275879
+ /* #include "ascon.h" */
275880
+
275881
+ /* #include "crypto_hash.h" */
275882
+ /*** Begin of #include "crypto_hash.h" ***/
275883
+ /*
275884
+ ** Name: hash.c
275885
+ ** Purpose: API definition for Hash algorithm with Ascon
275886
+ ** Based on: Public domain Ascon reference implementation
275887
+ ** and optimized variants for 32- and 64-bit
275888
+ ** (see https://github.com/ascon/ascon-c)
275889
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275890
+ ** Modified by: Ulrich Telle
275891
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275892
+ ** License: MIT
275893
+ */
275894
+
275895
+ #ifndef CRYPTO_HASH_H
275896
+ #define CRYPTO_HASH_H
275897
+
275898
+ #include <stddef.h>
275899
+
275900
+ /*
275901
+ ** Derives hash value using ASCON-HASH.
275902
+ **
275903
+ ** \param out Output buffer for hash with fixed length of ASCON_HASH_BYTES
275904
+ ** \param in Buffer with input data
275905
+ ** \param passwordlen Length of input data in bytes
275906
+ */
275907
+ int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen);
275908
+
275909
+ #endif
275910
+ /*** End of #include "crypto_hash.h" ***/
275911
+
275912
+ /* #include "permutations.h" */
275913
+
275914
+ /* #include "printstate.h" */
275915
+
275916
+
275917
+ #if !ASCON_INLINE_MODE
275918
+ #undef forceinline
275919
+ #define forceinline
275920
+ #endif
275921
+
275922
+ #ifdef ASCON_HASH_BYTES
275923
+
275924
+ forceinline void ascon_inithash(ascon_state_t* s) {
275925
+ int i;
275926
+ /* initialize */
275927
+ #ifdef ASCON_PRINT_STATE
275928
+ #if ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 12
275929
+ s->x[0] = ASCON_HASH_IV;
275930
+ #elif ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 8
275931
+ s->x[0] = ASCON_HASHA_IV;
275932
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 12
275933
+ s->x[0] = ASCON_XOF_IV;
275934
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 8
275935
+ s->x[0] = ASCON_XOFA_IV;
275936
+ #endif
275937
+ for (i = 1; i < 5; ++i) s->x[i] = 0;
275938
+ ascon_printstate("initial value", s);
275939
+ ASCON_P(s, 12);
275940
+ #endif
275941
+ #if ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 12
275942
+ const uint64_t iv[5] = {ASCON_HASH_IV0, ASCON_HASH_IV1, ASCON_HASH_IV2,
275943
+ ASCON_HASH_IV3, ASCON_HASH_IV4};
275944
+ #elif ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 8
275945
+ const uint64_t iv[5] = {ASCON_HASHA_IV0, ASCON_HASHA_IV1, ASCON_HASHA_IV2,
275946
+ ASCON_HASHA_IV3, ASCON_HASHA_IV4};
275947
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 12
275948
+ const uint64_t iv[5] = {ASCON_XOF_IV0, ASCON_XOF_IV1, ASCON_XOF_IV2,
275949
+ ASCON_XOF_IV3, ASCON_XOF_IV4};
275950
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 8
275951
+ const uint64_t iv[5] = {ASCON_XOFA_IV0, ASCON_XOFA_IV1, ASCON_XOFA_IV2,
275952
+ ASCON_XOFA_IV3, ASCON_XOFA_IV4};
275953
+ #endif
275954
+ for (i = 0; i < 5; ++i) s->x[i] = (iv[i]);
275955
+ ascon_printstate("initialization", s);
275956
+ }
275957
+
275958
+ forceinline void ascon_absorb(ascon_state_t* s, const uint8_t* in,
275959
+ uint64_t inlen) {
275960
+ /* absorb full plaintext blocks */
275961
+ while (inlen >= ASCON_HASH_RATE) {
275962
+ s->x[0] ^= ASCON_LOAD(in, 8);
275963
+ ascon_printstate("absorb plaintext", s);
275964
+ ASCON_P(s, ASCON_HASH_ROUNDS);
275965
+ in += ASCON_HASH_RATE;
275966
+ inlen -= ASCON_HASH_RATE;
275967
+ }
275968
+ /* absorb final plaintext block */
275969
+ s->x[0] ^= ASCON_LOADBYTES(in, inlen);
275970
+ s->x[0] ^= ASCON_PAD(inlen);
275971
+ ascon_printstate("pad plaintext", s);
275972
+ }
275973
+
275974
+ forceinline void ascon_squeeze(ascon_state_t* s, uint8_t* out,
275975
+ uint64_t outlen) {
275976
+ /* squeeze full output blocks */
275977
+ ASCON_P(s, 12);
275978
+ while (outlen > ASCON_HASH_RATE) {
275979
+ ASCON_STORE(out, s->x[0], 8);
275980
+ ascon_printstate("squeeze output", s);
275981
+ ASCON_P(s, ASCON_HASH_ROUNDS);
275982
+ out += ASCON_HASH_RATE;
275983
+ outlen -= ASCON_HASH_RATE;
275984
+ }
275985
+ /* squeeze final output block */
275986
+ ASCON_STOREBYTES(out, s->x[0], outlen);
275987
+ ascon_printstate("squeeze output", s);
275988
+ }
275989
+
275990
+ int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen)
275991
+ {
275992
+ ascon_state_t s;
275993
+ ascon_inithash(&s);
275994
+ ascon_absorb(&s, in, inlen);
275995
+ ascon_squeeze(&s, out, ASCON_HASH_BYTES);
275996
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275997
+ return 0;
275998
+ }
275999
+
276000
+ #endif
276001
+ /*** End of #include "ascon/hash.c" ***/
276002
+
276003
+ /* #include "ascon/pbkdf2.c" */
276004
+ /*** Begin of #include "ascon/pbkdf2.c" ***/
276005
+ /*
276006
+ ** Name: pbkdf2.c
276007
+ ** Purpose: Implementation of PBKDF2 algoritm with Ascon
276008
+ ** Based on: Public domain Ascon reference implementation
276009
+ ** and optimized variants for 32- and 64-bit
276010
+ ** (see https://github.com/ascon/ascon-c)
276011
+ ** and the paper "Additional Modes for ASCON Version 1.1"
276012
+ ** by Rhys Weatherley, Southern Storm Software, Pty Ltd.
276013
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
276014
+ ** Created by: Ulrich Telle
276015
+ ** Copyright: (c) 2023-2023 Ulrich Telle
276016
+ ** License: MIT
276017
+ */
276018
+
276019
+ #define ASCON_HASH_SIZE 32
276020
+ #define ASCON_PBKDF2_SIZE 32
276021
+
276022
+ void ascon_pbkdf2_init(ascon_state_t* state, const char* functionName,
276023
+ const unsigned char* custom, uint32_t customlen, uint32_t outlen)
276024
+ {
276025
+ /* Format the initial block with the function name and output length */
276026
+ uint8_t initial[ASCON_HASH_SIZE];
276027
+ size_t fnLength = functionName ? strlen(functionName) : 0;
276028
+
276029
+ if (fnLength == 0)
276030
+ {
276031
+ /* No function name specified */
276032
+ memset(initial, 0, ASCON_HASH_SIZE);
276033
+ }
276034
+ else if (fnLength <= ASCON_HASH_SIZE)
276035
+ {
276036
+ /* Pad the function name with zeroes */
276037
+ memcpy(initial, functionName, fnLength);
276038
+ memset(initial + fnLength, 0, ASCON_HASH_SIZE - fnLength);
276039
+ }
276040
+ else
276041
+ {
276042
+ ascon_hash(initial, (const uint8_t*) functionName, fnLength);
276043
+ }
276044
+
276045
+ state->x[0] = ASCON_HASH_IV;
276046
+ state->x[1] = ASCON_LOAD(initial, 8);
276047
+ state->x[2] = ASCON_LOAD(initial + 8, 8);
276048
+ state->x[3] = ASCON_LOAD(initial + 16, 8);
276049
+ state->x[4] = ASCON_LOAD(initial + 24, 8);
276050
+ ASCON_P(state, 12);
276051
+
276052
+ if (customlen > 0)
276053
+ {
276054
+ ascon_absorb(state, custom, customlen);
276055
+ ASCON_P(state, 12);
276056
+ /* domain separation */
276057
+ state->x[4] ^= 1;
276058
+ }
276059
+ }
276060
+
276061
+ /*
276062
+ * Implementation of the "F" function from RFC 8018, section 5.2
276063
+ *
276064
+ * Note: Instead of HMAC like in RFC 8018, use the following PRF:
276065
+ * PRF(P, X) = ASCON-cXOF(X, 256, "PBKDF2", P)
276066
+ */
276067
+ static void ascon_pbkdf2_f(ascon_state_t* state,
276068
+ uint8_t* T, /*uint8_t* U,*/
276069
+ const uint8_t* salt, uint32_t saltlen,
276070
+ uint32_t count, uint32_t blocknum)
276071
+ {
276072
+ uint32_t asconSaltLen = (saltlen < ASCON_SALT_LEN) ? saltlen : ASCON_SALT_LEN;
276073
+ uint8_t temp[ASCON_SALT_LEN+4];
276074
+ ascon_state_t state2;
276075
+ int j;
276076
+
276077
+ memset(temp, 0, ASCON_SALT_LEN);
276078
+ memcpy(temp, salt, asconSaltLen);
276079
+ STORE32_BE(temp+ASCON_SALT_LEN, blocknum);
276080
+
276081
+ /* Copy initial state */
276082
+ for (j = 0; j < 5; ++j) state2.x[j] = state->x[j];
276083
+
276084
+ ascon_absorb(&state2, temp, ASCON_SALT_LEN+4);
276085
+ ascon_squeeze(&state2, T, ASCON_PBKDF2_SIZE);
276086
+ sqlite3mcSecureZeroMemory(temp, sizeof(temp));
276087
+
276088
+ if (count > 1)
276089
+ {
276090
+ uint8_t U[ASCON_PBKDF2_SIZE];
276091
+ memcpy(U, T, ASCON_PBKDF2_SIZE);
276092
+ while (count > 1)
276093
+ {
276094
+ uint8_t* dst = T;
276095
+ uint8_t* src = U;
276096
+ uint32_t len = ASCON_PBKDF2_SIZE;
276097
+ /* Copy initial state */
276098
+ for (j = 0; j < 5; ++j) state2.x[j] = state->x[j];
276099
+ /* Absorb U */
276100
+ ascon_absorb(&state2, U, ASCON_PBKDF2_SIZE);
276101
+ /* Squeeze next U */
276102
+ ascon_squeeze(&state2, U, ASCON_PBKDF2_SIZE);
276103
+ /* XOR T with U */
276104
+ while (len > 0)
276105
+ {
276106
+ *dst++ ^= *src++;
276107
+ --len;
276108
+ }
276109
+ --count;
276110
+ }
276111
+ sqlite3mcSecureZeroMemory(U, sizeof(U));
276112
+ }
276113
+ sqlite3mcSecureZeroMemory(&state2, sizeof(ascon_state_t));
276114
+ }
276115
+
276116
+ void ascon_pbkdf2(uint8_t* out, uint32_t outlen,
276117
+ const uint8_t* password, uint32_t passwordlen,
276118
+ const uint8_t* salt, uint32_t saltlen, uint32_t count)
276119
+ {
276120
+ ascon_state_t state;
276121
+ uint32_t blocknum = 1;
276122
+ ascon_pbkdf2_init(&state, "PBKDF2", password, passwordlen, ASCON_PBKDF2_SIZE);
276123
+ while (outlen > 0)
276124
+ {
276125
+ if (outlen >= ASCON_PBKDF2_SIZE)
276126
+ {
276127
+ ascon_pbkdf2_f(&state, out, /*U,*/ salt, saltlen, count, blocknum);
276128
+ out += ASCON_PBKDF2_SIZE;
276129
+ outlen -= ASCON_PBKDF2_SIZE;
276130
+ }
276131
+ else
276132
+ {
276133
+ uint8_t T[ASCON_PBKDF2_SIZE];
276134
+ ascon_pbkdf2_f(&state, T, /*U,*/ salt, saltlen, count, blocknum);
276135
+ memcpy(out, T, outlen);
276136
+ sqlite3mcSecureZeroMemory(T, sizeof(T));
276137
+ break;
276138
+ }
276139
+ ++blocknum;
276140
+ }
276141
+ }
276142
+ /*** End of #include "ascon/pbkdf2.c" ***/
276143
+
276144
+
276145
+ SQLITE_PRIVATE CipherParams mcAscon128Params[] =
276146
+ {
276147
+ { "kdf_iter", ASCON128_KDF_ITER_DEFAULT, ASCON128_KDF_ITER_DEFAULT, 1, 0x7fffffff },
276148
+ CIPHER_PARAMS_SENTINEL
276149
+ };
276150
+
276151
+ #define KEYLENGTH_ASCON128 32
276152
+ #define SALTLENGTH_ASCON128 16
276153
+ #define PAGE_NONCE_LEN_ASCON128 16
276154
+ #define PAGE_TAG_LEN_ASCON128 16
276155
+ #define PAGE_RESERVED_ASCON128 (PAGE_NONCE_LEN_ASCON128 + PAGE_TAG_LEN_ASCON128)
276156
+
276157
+ typedef struct _ascon128Cipher
276158
+ {
276159
+ int m_kdfIter;
276160
+ int m_keyLength;
276161
+ uint8_t m_key[KEYLENGTH_ASCON128];
276162
+ uint8_t m_salt[SALTLENGTH_ASCON128];
276163
+ } Ascon128Cipher;
276164
+
276165
+ static void*
276166
+ AllocateAscon128Cipher(sqlite3* db)
276167
+ {
276168
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) sqlite3_malloc(sizeof(Ascon128Cipher));
276169
+ if (ascon128Cipher != NULL)
276170
+ {
276171
+ memset(ascon128Cipher, 0, sizeof(Ascon128Cipher));
276172
+ ascon128Cipher->m_keyLength = KEYLENGTH_ASCON128;
276173
+ memset(ascon128Cipher->m_key, 0, KEYLENGTH_ASCON128);
276174
+ memset(ascon128Cipher->m_salt, 0, SALTLENGTH_ASCON128);
276175
+ }
276176
+ if (ascon128Cipher != NULL)
276177
+ {
276178
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_ASCON128);
276179
+ ascon128Cipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
276180
+ }
276181
+ return ascon128Cipher;
276182
+ }
276183
+
276184
+ static void
276185
+ FreeAscon128Cipher(void* cipher)
276186
+ {
276187
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276188
+ memset(ascon128Cipher, 0, sizeof(Ascon128Cipher));
276189
+ sqlite3_free(ascon128Cipher);
276190
+ }
276191
+
276192
+ static void
276193
+ CloneAscon128Cipher(void* cipherTo, void* cipherFrom)
276194
+ {
276195
+ Ascon128Cipher* ascon128CipherTo = (Ascon128Cipher*) cipherTo;
276196
+ Ascon128Cipher* ascon128CipherFrom = (Ascon128Cipher*) cipherFrom;
276197
+ ascon128CipherTo->m_kdfIter = ascon128CipherFrom->m_kdfIter;
276198
+ ascon128CipherTo->m_keyLength = ascon128CipherFrom->m_keyLength;
276199
+ memcpy(ascon128CipherTo->m_key, ascon128CipherFrom->m_key, KEYLENGTH_ASCON128);
276200
+ memcpy(ascon128CipherTo->m_salt, ascon128CipherFrom->m_salt, SALTLENGTH_ASCON128);
276201
+ }
276202
+
276203
+ static int
276204
+ GetLegacyAscon128Cipher(void* cipher)
276205
+ {
276206
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*)cipher;
276207
+ return 0;
276208
+ }
276209
+
276210
+ static int
276211
+ GetPageSizeAscon128Cipher(void* cipher)
276212
+ {
276213
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276214
+ int pageSize = 0;
276215
+ return pageSize;
276216
+ }
276217
+
276218
+ static int
276219
+ GetReservedAscon128Cipher(void* cipher)
276220
+ {
276221
+ return PAGE_RESERVED_ASCON128;
276222
+ }
276223
+
276224
+ static unsigned char*
276225
+ GetSaltAscon128Cipher(void* cipher)
276226
+ {
276227
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276228
+ return ascon128Cipher->m_salt;
276229
+ }
276230
+
276231
+ static void
276232
+ GenerateKeyAscon128Cipher(void* cipher, BtShared* pBt, char* userPassword, int passwordLength, int rekey, unsigned char* cipherSalt)
276233
+ {
276234
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276235
+ int bypass = 0;
276236
+
276237
+ Pager *pPager = pBt->pPager;
276238
+ sqlite3_file* fd = (isOpen(pPager->fd)) ? pPager->fd : NULL;
276239
+
276240
+ int keyOnly = 1;
276241
+ if (rekey || fd == NULL || sqlite3OsRead(fd, ascon128Cipher->m_salt, SALTLENGTH_ASCON128, 0) != SQLITE_OK)
276242
+ {
276243
+ chacha20_rng(ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276244
+ keyOnly = 0;
276245
+ }
276246
+ else if (cipherSalt != NULL)
276247
+ {
276248
+ memcpy(ascon128Cipher->m_salt, cipherSalt, SALTLENGTH_ASCON128);
276249
+ }
276250
+
276251
+ /* Bypass key derivation if the key string starts with "raw:" */
276252
+ if (passwordLength > 4 && !memcmp(userPassword, "raw:", 4))
276253
+ {
276254
+ const int nRaw = passwordLength - 4;
276255
+ const unsigned char* zRaw = (const unsigned char*) userPassword + 4;
276256
+ switch (nRaw)
276257
+ {
276258
+ /* Binary key (and salt) */
276259
+ case KEYLENGTH_ASCON128 + SALTLENGTH_ASCON128:
276260
+ if (!keyOnly)
276261
+ {
276262
+ memcpy(ascon128Cipher->m_salt, zRaw + KEYLENGTH_ASCON128, SALTLENGTH_ASCON128);
276263
+ }
276264
+ /* fall-through */
276265
+ case KEYLENGTH_ASCON128:
276266
+ memcpy(ascon128Cipher->m_key, zRaw, KEYLENGTH_ASCON128);
276267
+ bypass = 1;
276268
+ break;
276269
+
276270
+ /* Hex-encoded key */
276271
+ case 2 * KEYLENGTH_ASCON128:
276272
+ if (sqlite3mcIsHexKey(zRaw, nRaw) != 0)
276273
+ {
276274
+ sqlite3mcConvertHex2Bin(zRaw, nRaw, ascon128Cipher->m_key);
276275
+ bypass = 1;
276276
+ }
276277
+ break;
276278
+
276279
+ /* Hex-encoded key and salt */
276280
+ case 2 * (KEYLENGTH_ASCON128 + SALTLENGTH_ASCON128):
276281
+ if (sqlite3mcIsHexKey(zRaw, nRaw) != 0)
276282
+ {
276283
+ sqlite3mcConvertHex2Bin(zRaw, 2 * KEYLENGTH_ASCON128, ascon128Cipher->m_key);
276284
+ if (!keyOnly)
276285
+ {
276286
+ sqlite3mcConvertHex2Bin(zRaw + 2 * KEYLENGTH_ASCON128, 2 * SALTLENGTH_ASCON128, ascon128Cipher->m_salt);
276287
+ }
276288
+ bypass = 1;
276289
+ }
276290
+ break;
276291
+
276292
+ default:
276293
+ break;
276294
+ }
276295
+ }
276296
+
276297
+ if (!bypass)
276298
+ {
276299
+ ascon_pbkdf2(ascon128Cipher->m_key, KEYLENGTH_ASCON128,
276300
+ (const uint8_t*) userPassword, passwordLength,
276301
+ ascon128Cipher->m_salt, SALTLENGTH_ASCON128, ascon128Cipher->m_kdfIter);
276302
+ }
276303
+ SQLITE3MC_DEBUG_LOG("generate: codec=%p pFile=%p\n", ascon128Cipher, fd);
276304
+ SQLITE3MC_DEBUG_HEX("generate key:", ascon128Cipher->m_key, KEYLENGTH_ASCON128);
276305
+ SQLITE3MC_DEBUG_HEX("generate salt:", ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276306
+ }
276307
+
276308
+ static int
276309
+ AsconGenOtk(uint8_t* out, const uint8_t* key, const uint8_t* nonce, int page)
276310
+ {
276311
+ ascon_state_t s;
276312
+ uint8_t temp[KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128+4];
276313
+ memcpy(temp, key, KEYLENGTH_ASCON128);
276314
+ memcpy(temp+KEYLENGTH_ASCON128, nonce, PAGE_NONCE_LEN_ASCON128);
276315
+ STORE32_BE(temp+KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128, page);
276316
+ ascon_inithash(&s);
276317
+ ascon_absorb(&s, temp, KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128+4);
276318
+ ascon_squeeze(&s, out, ASCON_HASH_BYTES);
276319
+ sqlite3mcSecureZeroMemory(temp, sizeof(temp));
276320
+ return 0;
276321
+ }
276322
+
276323
+ static int
276324
+ EncryptPageAscon128Cipher(void* cipher, int page, unsigned char* data, int len, int reserved)
276325
+ {
276326
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276327
+ int rc = SQLITE_OK;
276328
+ int nReserved = (reserved == 0) ? 0 : GetReservedAscon128Cipher(cipher);
276329
+ int n = len - nReserved;
276330
+ uint64_t mlen = n;
276331
+
276332
+ /* Generate one-time keys */
276333
+ uint8_t otk[ASCON_HASH_BYTES];
276334
+ int offset;
276335
+
276336
+ /* Check whether number of required reserved bytes and actually reserved bytes match */
276337
+ if (nReserved > reserved)
276338
+ {
276339
+ return SQLITE_CORRUPT;
276340
+ }
276341
+
276342
+ if (nReserved > 0)
276343
+ {
276344
+ /* Encrypt and authenticate */
276345
+ memset(otk, 0, ASCON_HASH_BYTES);
276346
+ /* Generate nonce */
276347
+ chacha20_rng(data + n + PAGE_TAG_LEN_ASCON128, PAGE_NONCE_LEN_ASCON128);
276348
+ AsconGenOtk(otk, ascon128Cipher->m_key, data + n + PAGE_TAG_LEN_ASCON128, page);
276349
+
276350
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276351
+ ascon_aead_encrypt(data + offset, data + n, data + offset, mlen - offset,
276352
+ NULL /* ad */, 0 /* adlen*/,
276353
+ data + n + PAGE_TAG_LEN_ASCON128, otk);
276354
+ if (page == 1)
276355
+ {
276356
+ memcpy(data, ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276357
+ }
276358
+ }
276359
+ else
276360
+ {
276361
+ /* Encrypt only */
276362
+ uint8_t nonce[PAGE_NONCE_LEN_ASCON128];
276363
+ uint8_t dummyTag[PAGE_TAG_LEN_ASCON128];
276364
+ memset(dummyTag, 0, PAGE_TAG_LEN_ASCON128);
276365
+ memset(otk, 0, ASCON_HASH_BYTES);
276366
+ sqlite3mcGenerateInitialVector(page, nonce);
276367
+ AsconGenOtk(otk, ascon128Cipher->m_key, nonce, page);
276368
+
276369
+ /* Encrypt */
276370
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276371
+ ascon_aead_encrypt(data + offset, dummyTag, data + offset, mlen - offset,
276372
+ NULL /* ad */, 0 /* adlen*/,
276373
+ nonce, otk);
276374
+ if (page == 1)
276375
+ {
276376
+ memcpy(data, ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276377
+ }
276378
+ }
276379
+
276380
+ return rc;
276381
+ }
276382
+
276383
+ static int
276384
+ DecryptPageAscon128Cipher(void* cipher, int page, unsigned char* data, int len, int reserved, int hmacCheck)
276385
+ {
276386
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276387
+ int rc = SQLITE_OK;
276388
+ int nReserved = (reserved == 0) ? 0 : GetReservedAscon128Cipher(cipher);
276389
+ int n = len - nReserved;
276390
+ uint64_t clen = n;
276391
+ int tagOk;
276392
+
276393
+ /* Generate one-time keys */
276394
+ uint8_t otk[ASCON_HASH_BYTES];
276395
+ int offset;
276396
+
276397
+ /* Check whether number of required reserved bytes and actually reserved bytes match */
276398
+ if (nReserved > reserved)
276399
+ {
276400
+ return (page == 1) ? SQLITE_NOTADB : SQLITE_CORRUPT;
276401
+ }
276402
+
276403
+ if (nReserved > 0)
276404
+ {
276405
+ /* Decrypt and verify MAC */
276406
+ memset(otk, 0, ASCON_HASH_BYTES);
276407
+ AsconGenOtk(otk, ascon128Cipher->m_key, data + n + PAGE_TAG_LEN_ASCON128, page);
276408
+
276409
+ /* Determine MAC and decrypt */
276410
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276411
+ tagOk = ascon_aead_decrypt(data + offset, data + offset, clen - offset,
276412
+ NULL /* ad */, 0 /* adlen */,
276413
+ data + n, data + n + PAGE_TAG_LEN_ASCON128, otk);
276414
+ if (hmacCheck != 0)
276415
+ {
276416
+ /* Verify the MAC */
276417
+ if (tagOk != 0)
276418
+ {
276419
+ SQLITE3MC_DEBUG_LOG("decrypt: codec=%p page=%d\n", ascon128Cipher, page);
276420
+ SQLITE3MC_DEBUG_HEX("decrypt key:", ascon128Cipher->m_key, 32);
276421
+ SQLITE3MC_DEBUG_HEX("decrypt otk:", otk, 64);
276422
+ SQLITE3MC_DEBUG_HEX("decrypt data+00:", data, 16);
276423
+ SQLITE3MC_DEBUG_HEX("decrypt data+24:", data + 24, 16);
276424
+ SQLITE3MC_DEBUG_HEX("decrypt data+n:", data + n, 16);
276425
+ SQLITE3MC_DEBUG_HEX("decrypt tag r:", data + n + PAGE_NONCE_LEN_ASCON128, PAGE_TAG_LEN_ASCON128);
276426
+ SQLITE3MC_DEBUG_HEX("decrypt tag c:", tag, PAGE_TAG_LEN_ASCON128);
276427
+ /* Bad MAC */
276428
+ rc = (page == 1) ? SQLITE_NOTADB : SQLITE_CORRUPT;
276429
+ }
276430
+ }
276431
+ if (page == 1 && rc == SQLITE_OK)
276432
+ {
276433
+ memcpy(data, SQLITE_FILE_HEADER, 16);
276434
+ }
276435
+ }
276436
+ else
276437
+ {
276438
+ /* Decrypt only */
276439
+ uint8_t nonce[PAGE_NONCE_LEN_ASCON128];
276440
+ uint8_t dummyTag[PAGE_TAG_LEN_ASCON128];
276441
+ memset(dummyTag, 0, PAGE_TAG_LEN_ASCON128);
276442
+ memset(otk, 0, ASCON_HASH_BYTES);
276443
+ sqlite3mcGenerateInitialVector(page, nonce);
276444
+ AsconGenOtk(otk, ascon128Cipher->m_key, nonce, page);
276445
+
276446
+ /* Decrypt */
276447
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276448
+ tagOk = ascon_aead_decrypt(data + offset, data + offset, clen - offset,
276449
+ NULL /* ad */, 0 /* adlen */,
276450
+ dummyTag, nonce, otk);
276451
+ if (page == 1)
276452
+ {
276453
+ memcpy(data, SQLITE_FILE_HEADER, 16);
276454
+ }
276455
+ }
276456
+
276457
+ return rc;
276458
+ }
276459
+
276460
+ SQLITE_PRIVATE const CipherDescriptor mcAscon128Descriptor =
276461
+ {
276462
+ CIPHER_NAME_ASCON128,
276463
+ AllocateAscon128Cipher,
276464
+ FreeAscon128Cipher,
276465
+ CloneAscon128Cipher,
276466
+ GetLegacyAscon128Cipher,
276467
+ GetPageSizeAscon128Cipher,
276468
+ GetReservedAscon128Cipher,
276469
+ GetSaltAscon128Cipher,
276470
+ GenerateKeyAscon128Cipher,
276471
+ EncryptPageAscon128Cipher,
276472
+ DecryptPageAscon128Cipher
276473
+ };
276474
+ #endif
276475
+ /*** End of #include "cipher_ascon.c" ***/
276476
+
274710
276477
  /* #include "cipher_common.c" */
274711
276478
  /*** Begin of #include "cipher_common.c" ***/
274712
276479
  /*
@@ -276638,7 +278405,7 @@ sqlite3mcBtreeSetPageSize(Btree* p, int pageSize, int nReserve, int iFix)
276638
278405
  ** Change 4: Call sqlite3mcBtreeSetPageSize instead of sqlite3BtreeSetPageSize for main database
276639
278406
  ** (sqlite3mcBtreeSetPageSize allows to reduce the number of reserved bytes)
276640
278407
  **
276641
- ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.44.0 amalgamation.
278408
+ ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.44.2 amalgamation.
276642
278409
  */
276643
278410
  SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3mcRunVacuumForRekey(
276644
278411
  char **pzErrMsg, /* Write error message here */
@@ -277501,6 +279268,33 @@ sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey)
277501
279268
  ** License: MIT
277502
279269
  */
277503
279270
 
279271
+ /* For memset, memset_s */
279272
+ #include <string.h>
279273
+
279274
+ #ifdef _WIN32
279275
+ /* For SecureZeroMemory */
279276
+ #include <windows.h>
279277
+ #include <winbase.h>
279278
+ #endif
279279
+
279280
+ SQLITE_PRIVATE void sqlite3mcSecureZeroMemory(void* v, size_t n)
279281
+ {
279282
+ #ifdef _WIN32
279283
+ SecureZeroMemory(v, n);
279284
+ #elif defined(__DARWIN__) || defined(__STDC_LIB_EXT1__)
279285
+ /* memset_s() is available since OS X 10.9, */
279286
+ /* and may be available on other platforms. */
279287
+ memset_s(v, n, 0, n);
279288
+ #elif defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 11)
279289
+ /* Non-standard function */
279290
+ explicit_bzero(v, n);
279291
+ #else
279292
+ /* Generic implementation based on volatile pointers */
279293
+ static void* (* const volatile memset_sec)(void*, int, size_t) = &memset;
279294
+ memset_sec(v, 0, n);
279295
+ #endif
279296
+ }
279297
+
277504
279298
  #if SQLITE3MC_SECURE_MEMORY
277505
279299
 
277506
279300
  /* Flag indicating whether securing memory allocations is initialized */
@@ -277571,7 +279365,7 @@ static void mcMemoryFree(void* pPrior)
277571
279365
  mcRandomFill((char*) pPrior, nSize)
277572
279366
  #else
277573
279367
  int nSize = mcMemorySize(pPrior);
277574
- memset(pPrior, 0, nSize);
279368
+ sqlite3mcSecureZeroMemory(pPrior, 0, nSize);
277575
279369
  #endif
277576
279370
  }
277577
279371
  mcDefaultMemoryMethods.xFree(pPrior);
@@ -281689,7 +283483,7 @@ SQLITE_EXTENSION_INIT1
281689
283483
 
281690
283484
  #undef LONGDOUBLE_CONSTANT
281691
283485
  #undef LONGDOUBLE_TYPE
281692
- #if defined(__GNUC__) && defined(_WIN64)
283486
+ #if defined(SQLITE_USE_QUADMATH) && defined(__GNUC__) && defined(_WIN64)
281693
283487
  #include <quadmath.h>
281694
283488
  #define LONGDOUBLE_TYPE __float128
281695
283489
  #define LONGDOUBLE_CONSTANT(x) x##Q
@@ -283214,7 +285008,7 @@ static int vsvtabColumn(
283214
285008
  {
283215
285009
  LONGDOUBLE_TYPE dv, fp, ip;
283216
285010
 
283217
- #if defined(__GNUC__) && defined(_WIN64)
285011
+ #if defined(SQLITE_USE_QUADMATH) && defined(__GNUC__) && defined(_WIN64)
283218
285012
  if (!hasExtended) hasExtended = 1;
283219
285013
  #else
283220
285014
  if (!hasExtended) {
@@ -299748,7 +301542,57 @@ static const int walFileHeaderSize = 32;
299748
301542
  /*
299749
301543
  ** Global I/O method structure of SQLite3 Multiple Ciphers VFS
299750
301544
  */
299751
- static sqlite3_io_methods mcIoMethodsGlobal =
301545
+
301546
+ #define IOMETHODS_VERSION_MIN 1
301547
+ #define IOMETHODS_VERSION_MAX 3
301548
+
301549
+ static sqlite3_io_methods mcIoMethodsGlobal1 =
301550
+ {
301551
+ 1, /* iVersion */
301552
+ mcIoClose, /* xClose */
301553
+ mcIoRead, /* xRead */
301554
+ mcIoWrite, /* xWrite */
301555
+ mcIoTruncate, /* xTruncate */
301556
+ mcIoSync, /* xSync */
301557
+ mcIoFileSize, /* xFileSize */
301558
+ mcIoLock, /* xLock */
301559
+ mcIoUnlock, /* xUnlock */
301560
+ mcIoCheckReservedLock, /* xCheckReservedLock */
301561
+ mcIoFileControl, /* xFileControl */
301562
+ mcIoSectorSize, /* xSectorSize */
301563
+ mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
301564
+ 0, /* xShmMap */
301565
+ 0, /* xShmLock */
301566
+ 0, /* xShmBarrier */
301567
+ 0, /* xShmUnmap */
301568
+ 0, /* xFetch */
301569
+ 0, /* xUnfetch */
301570
+ };
301571
+
301572
+ static sqlite3_io_methods mcIoMethodsGlobal2 =
301573
+ {
301574
+ 2, /* iVersion */
301575
+ mcIoClose, /* xClose */
301576
+ mcIoRead, /* xRead */
301577
+ mcIoWrite, /* xWrite */
301578
+ mcIoTruncate, /* xTruncate */
301579
+ mcIoSync, /* xSync */
301580
+ mcIoFileSize, /* xFileSize */
301581
+ mcIoLock, /* xLock */
301582
+ mcIoUnlock, /* xUnlock */
301583
+ mcIoCheckReservedLock, /* xCheckReservedLock */
301584
+ mcIoFileControl, /* xFileControl */
301585
+ mcIoSectorSize, /* xSectorSize */
301586
+ mcIoDeviceCharacteristics, /* xDeviceCharacteristics */
301587
+ mcIoShmMap, /* xShmMap */
301588
+ mcIoShmLock, /* xShmLock */
301589
+ mcIoShmBarrier, /* xShmBarrier */
301590
+ mcIoShmUnmap, /* xShmUnmap */
301591
+ 0, /* xFetch */
301592
+ 0, /* xUnfetch */
301593
+ };
301594
+
301595
+ static sqlite3_io_methods mcIoMethodsGlobal3 =
299752
301596
  {
299753
301597
  3, /* iVersion */
299754
301598
  mcIoClose, /* xClose */
@@ -299771,6 +301615,9 @@ static sqlite3_io_methods mcIoMethodsGlobal =
299771
301615
  mcIoUnfetch, /* xUnfetch */
299772
301616
  };
299773
301617
 
301618
+ static sqlite3_io_methods* mcIoMethodsGlobal[] =
301619
+ { 0, &mcIoMethodsGlobal1 , &mcIoMethodsGlobal2 , &mcIoMethodsGlobal3 };
301620
+
299774
301621
  /*
299775
301622
  ** Internal functions
299776
301623
  */
@@ -299945,7 +301792,9 @@ SQLITE_PRIVATE void* sqlite3mcPagerCodec(PgHdrMC* pPg)
299945
301792
  {
299946
301793
  sqlite3_file* pFile = sqlite3PagerFile(pPg->pPager);
299947
301794
  void* aData = 0;
299948
- if (pFile->pMethods == &mcIoMethodsGlobal)
301795
+ if (pFile->pMethods == &mcIoMethodsGlobal1 ||
301796
+ pFile->pMethods == &mcIoMethodsGlobal2 ||
301797
+ pFile->pMethods == &mcIoMethodsGlobal3)
299949
301798
  {
299950
301799
  sqlite3mc_file* mcFile = (sqlite3mc_file*) pFile;
299951
301800
  Codec* codec = mcFile->codec;
@@ -300047,9 +301896,18 @@ static int mcVfsOpen(sqlite3_vfs* pVfs, const char* zName, sqlite3_file* pFile,
300047
301896
  if (rc == SQLITE_OK)
300048
301897
  {
300049
301898
  /*
300050
- ** Real open succeeded, initialize methods, register main database files
301899
+ ** Real open succeeded
301900
+ ** Initialize methods (use same version number as underlying implementation
301901
+ ** Register main database files
300051
301902
  */
300052
- pFile->pMethods = &mcIoMethodsGlobal;
301903
+ int ioMethodsVersion = mcFile->pFile->pMethods->iVersion;
301904
+ if (ioMethodsVersion < IOMETHODS_VERSION_MIN ||
301905
+ ioMethodsVersion > IOMETHODS_VERSION_MAX)
301906
+ {
301907
+ /* If version out of range, use highest known version */
301908
+ ioMethodsVersion = IOMETHODS_VERSION_MAX;
301909
+ }
301910
+ pFile->pMethods = mcIoMethodsGlobal[ioMethodsVersion];
300053
301911
  if (flags & SQLITE_OPEN_MAIN_DB)
300054
301912
  {
300055
301913
  mcMainListAdd(mcFile);
@@ -300835,7 +302693,10 @@ static int mcIoFileControl(sqlite3_file* pFile, int op, void* pArg)
300835
302693
 
300836
302694
  static int mcIoSectorSize(sqlite3_file* pFile)
300837
302695
  {
300838
- return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
302696
+ if (REALFILE(pFile)->pMethods->xSectorSize)
302697
+ return REALFILE(pFile)->pMethods->xSectorSize(REALFILE(pFile));
302698
+ else
302699
+ return SQLITE_DEFAULT_SECTOR_SIZE;
300839
302700
  }
300840
302701
 
300841
302702
  static int mcIoDeviceCharacteristics(sqlite3_file* pFile)
@@ -301391,6 +303252,12 @@ sqlite3mc_initialize(const char* arg)
301391
303252
  rc = sqlite3mcRegisterCipher(&mcRC4Descriptor, mcRC4Params, (CODEC_TYPE_RC4 == CODEC_TYPE));
301392
303253
  }
301393
303254
  #endif
303255
+ #if HAVE_CIPHER_ASCON128
303256
+ if (rc == SQLITE_OK)
303257
+ {
303258
+ rc = sqlite3mcRegisterCipher(&mcAscon128Descriptor, mcAscon128Params, (CODEC_TYPE_ASCON128 == CODEC_TYPE));
303259
+ }
303260
+ #endif
301394
303261
 
301395
303262
  /*
301396
303263
  ** Initialize and register MultiCipher VFS as default VFS