better-sqlite3-multiple-ciphers 9.1.1 → 9.1.2-beta.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.
@@ -67,6 +67,13 @@ void sqlite3mc_shutdown(void);
67
67
  #if !defined(_CRT_RAND_S)
68
68
  #define _CRT_RAND_S
69
69
  #endif
70
+
71
+ #else /* !WIN32 */
72
+
73
+ /* Define this before <string.h> is included to */
74
+ /* retrieve memset_s() declaration if available. */
75
+ #define __STDC_WANT_LIB_EXT1__ 1
76
+
70
77
  #endif
71
78
 
72
79
  #ifndef SQLITE_API
@@ -104,7 +111,7 @@ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
104
111
  /*** Begin of #include "sqlite3patched.c" ***/
105
112
  /******************************************************************************
106
113
  ** 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
114
+ ** version 3.44.1. By combining all the individual C code files into this
108
115
  ** single large file, the entire code can be compiled as a single translation
109
116
  ** unit. This allows many compilers to do optimizations that would not be
110
117
  ** possible if the files were compiled separately. Performance improvements
@@ -122,7 +129,7 @@ extern SQLITE_API LPWSTR sqlite3_win32_utf8_to_unicode(const char*);
122
129
  ** separate file. This file contains only code for the core SQLite library.
123
130
  **
124
131
  ** The content in this amalgamation comes from Fossil check-in
125
- ** 17129ba1ff7f0daf37100ee82d507aef7827.
132
+ ** d295f48e8f367b066b881780c98bdf980a1d.
126
133
  */
127
134
  #define SQLITE_CORE 1
128
135
  #define SQLITE_AMALGAMATION 1
@@ -563,9 +570,9 @@ extern "C" {
563
570
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
564
571
  ** [sqlite_version()] and [sqlite_source_id()].
565
572
  */
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"
573
+ #define SQLITE_VERSION "3.44.1"
574
+ #define SQLITE_VERSION_NUMBER 3044001
575
+ #define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
569
576
 
570
577
  /*
571
578
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -5990,13 +5997,27 @@ SQLITE_API int sqlite3_create_window_function(
5990
5997
  ** </dd>
5991
5998
  **
5992
5999
  ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
5993
- ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
6000
+ ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
5994
6001
  ** [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).
6002
+ ** This flag instructs SQLite to omit some corner-case optimizations that
6003
+ ** might disrupt the operation of the [sqlite3_value_subtype()] function,
6004
+ ** causing it to return zero rather than the correct subtype().
6005
+ ** SQL functions that invokes [sqlite3_value_subtype()] should have this
6006
+ ** property. If the SQLITE_SUBTYPE property is omitted, then the return
6007
+ ** value from [sqlite3_value_subtype()] might sometimes be zero even though
6008
+ ** a non-zero subtype was specified by the function argument expression.
6009
+ **
6010
+ ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
6011
+ ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
6012
+ ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
6013
+ ** result.
6014
+ ** Every function that invokes [sqlite3_result_subtype()] should have this
6015
+ ** property. If it does not, then the call to [sqlite3_result_subtype()]
6016
+ ** might become a no-op if the function is used as term in an
6017
+ ** [expression index]. On the other hand, SQL functions that never invoke
6018
+ ** [sqlite3_result_subtype()] should avoid setting this property, as the
6019
+ ** purpose of this property is to disable certain optimizations that are
6020
+ ** incompatible with subtypes.
6000
6021
  ** </dd>
6001
6022
  ** </dl>
6002
6023
  */
@@ -6004,6 +6025,7 @@ SQLITE_API int sqlite3_create_window_function(
6004
6025
  #define SQLITE_DIRECTONLY 0x000080000
6005
6026
  #define SQLITE_SUBTYPE 0x000100000
6006
6027
  #define SQLITE_INNOCUOUS 0x000200000
6028
+ #define SQLITE_RESULT_SUBTYPE 0x001000000
6007
6029
 
6008
6030
  /*
6009
6031
  ** CAPI3REF: Deprecated Functions
@@ -6200,6 +6222,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
6200
6222
  ** information can be used to pass a limited amount of context from
6201
6223
  ** one SQL function to another. Use the [sqlite3_result_subtype()]
6202
6224
  ** routine to set the subtype for the return value of an SQL function.
6225
+ **
6226
+ ** Every [application-defined SQL function] that invoke this interface
6227
+ ** should include the [SQLITE_SUBTYPE] property in the text
6228
+ ** encoding argument when the function is [sqlite3_create_function|registered].
6229
+ ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
6230
+ ** might return zero instead of the upstream subtype in some corner cases.
6203
6231
  */
6204
6232
  SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
6205
6233
 
@@ -6330,14 +6358,22 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
6330
6358
  ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
6331
6359
  ** parameter)^, or
6332
6360
  ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
6333
- ** allocation error occurs.)^ </ul>
6361
+ ** allocation error occurs.)^
6362
+ ** <li> ^(during the original sqlite3_set_auxdata() call if the function
6363
+ ** is evaluated during query planning instead of during query execution,
6364
+ ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
6334
6365
  **
6335
- ** Note the last bullet in particular. The destructor X in
6366
+ ** Note the last two bullets in particular. The destructor X in
6336
6367
  ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
6337
6368
  ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
6338
6369
  ** should be called near the end of the function implementation and the
6339
6370
  ** function implementation should not make any use of P after
6340
- ** sqlite3_set_auxdata() has been called.
6371
+ ** sqlite3_set_auxdata() has been called. Furthermore, a call to
6372
+ ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
6373
+ ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
6374
+ ** condition occurred during the sqlite3_set_auxdata() call or if the
6375
+ ** function is being evaluated during query planning rather than during
6376
+ ** query execution.
6341
6377
  **
6342
6378
  ** ^(In practice, auxiliary data is preserved between function calls for
6343
6379
  ** function parameters that are compile-time constants, including literal
@@ -6611,6 +6647,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
6611
6647
  ** higher order bits are discarded.
6612
6648
  ** The number of subtype bytes preserved by SQLite might increase
6613
6649
  ** in future releases of SQLite.
6650
+ **
6651
+ ** Every [application-defined SQL function] that invokes this interface
6652
+ ** should include the [SQLITE_RESULT_SUBTYPE] property in its
6653
+ ** text encoding argument when the SQL function is
6654
+ ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
6655
+ ** property is omitted from the function that invokes sqlite3_result_subtype(),
6656
+ ** then in some cases the sqlite3_result_subtype() might fail to set
6657
+ ** the result subtype.
6658
+ **
6659
+ ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
6660
+ ** SQL function that invokes the sqlite3_result_subtype() interface
6661
+ ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
6662
+ ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
6663
+ ** by default.
6614
6664
  */
6615
6665
  SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
6616
6666
 
@@ -17926,14 +17976,15 @@ struct FuncDestructor {
17926
17976
  #define SQLITE_FUNC_SLOCHNG 0x2000 /* "Slow Change". Value constant during a
17927
17977
  ** single query - might change over time */
17928
17978
  #define SQLITE_FUNC_TEST 0x4000 /* Built-in testing functions */
17929
- /* 0x8000 -- available for reuse */
17979
+ #define SQLITE_FUNC_RUNONLY 0x8000 /* Cannot be used by valueFromFunction */
17930
17980
  #define SQLITE_FUNC_WINDOW 0x00010000 /* Built-in window-only function */
17931
17981
  #define SQLITE_FUNC_INTERNAL 0x00040000 /* For use by NestedParse() only */
17932
17982
  #define SQLITE_FUNC_DIRECT 0x00080000 /* Not for use in TRIGGERs or VIEWs */
17933
- #define SQLITE_FUNC_SUBTYPE 0x00100000 /* Result likely to have sub-type */
17983
+ /* SQLITE_SUBTYPE 0x00100000 // Consumer of subtypes */
17934
17984
  #define SQLITE_FUNC_UNSAFE 0x00200000 /* Function has side effects */
17935
17985
  #define SQLITE_FUNC_INLINE 0x00400000 /* Functions implemented in-line */
17936
17986
  #define SQLITE_FUNC_BUILTIN 0x00800000 /* This is a built-in function */
17987
+ /* SQLITE_RESULT_SUBTYPE 0x01000000 // Generator of subtypes */
17937
17988
  #define SQLITE_FUNC_ANYORDER 0x08000000 /* count/min/max aggregate */
17938
17989
 
17939
17990
  /* Identifier numbers for each in-line function */
@@ -18025,9 +18076,10 @@ struct FuncDestructor {
18025
18076
  #define MFUNCTION(zName, nArg, xPtr, xFunc) \
18026
18077
  {nArg, SQLITE_FUNC_BUILTIN|SQLITE_FUNC_CONSTANT|SQLITE_UTF8, \
18027
18078
  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, \
18079
+ #define JFUNCTION(zName, nArg, bUseCache, bWS, bRS, iArg, xFunc) \
18080
+ {nArg, SQLITE_FUNC_BUILTIN|SQLITE_DETERMINISTIC|SQLITE_FUNC_CONSTANT|\
18081
+ SQLITE_UTF8|((bUseCache)*SQLITE_FUNC_RUNONLY)|\
18082
+ ((bRS)*SQLITE_SUBTYPE)|((bWS)*SQLITE_RESULT_SUBTYPE), \
18031
18083
  SQLITE_INT_TO_PTR(iArg), 0, xFunc, 0, 0, 0, #zName, {0} }
18032
18084
  #define INLINE_FUNC(zName, nArg, iArg, mFlags) \
18033
18085
  {nArg, SQLITE_FUNC_BUILTIN|\
@@ -29568,7 +29620,7 @@ SQLITE_PRIVATE void sqlite3MemoryBarrier(void){
29568
29620
  SQLITE_MEMORY_BARRIER;
29569
29621
  #elif defined(__GNUC__)
29570
29622
  __sync_synchronize();
29571
- #elif MSVC_VERSION>=1300
29623
+ #elif MSVC_VERSION>=1400
29572
29624
  _ReadWriteBarrier();
29573
29625
  #elif defined(MemoryBarrier)
29574
29626
  MemoryBarrier();
@@ -61573,10 +61625,13 @@ act_like_temp_file:
61573
61625
  */
61574
61626
  SQLITE_API sqlite3_file *sqlite3_database_file_object(const char *zName){
61575
61627
  Pager *pPager;
61628
+ const char *p;
61576
61629
  while( zName[-1]!=0 || zName[-2]!=0 || zName[-3]!=0 || zName[-4]!=0 ){
61577
61630
  zName--;
61578
61631
  }
61579
- pPager = *(Pager**)(zName - 4 - sizeof(Pager*));
61632
+ p = zName - 4 - sizeof(Pager*);
61633
+ assert( EIGHT_BYTE_ALIGNMENT(p) );
61634
+ pPager = *(Pager**)p;
61580
61635
  return pPager->fd;
61581
61636
  }
61582
61637
 
@@ -83537,7 +83592,7 @@ static int valueFromFunction(
83537
83592
  #endif
83538
83593
  assert( pFunc );
83539
83594
  if( (pFunc->funcFlags & (SQLITE_FUNC_CONSTANT|SQLITE_FUNC_SLOCHNG))==0
83540
- || (pFunc->funcFlags & SQLITE_FUNC_NEEDCOLL)
83595
+ || (pFunc->funcFlags & (SQLITE_FUNC_NEEDCOLL|SQLITE_FUNC_RUNONLY))!=0
83541
83596
  ){
83542
83597
  return SQLITE_OK;
83543
83598
  }
@@ -90078,6 +90133,18 @@ SQLITE_API void sqlite3_result_subtype(sqlite3_context *pCtx, unsigned int eSubt
90078
90133
  #ifdef SQLITE_ENABLE_API_ARMOR
90079
90134
  if( pCtx==0 ) return;
90080
90135
  #endif
90136
+ #if defined(SQLITE_STRICT_SUBTYPE) && SQLITE_STRICT_SUBTYPE+0!=0
90137
+ if( pCtx->pFunc!=0
90138
+ && (pCtx->pFunc->funcFlags & SQLITE_RESULT_SUBTYPE)==0
90139
+ ){
90140
+ char zErr[200];
90141
+ sqlite3_snprintf(sizeof(zErr), zErr,
90142
+ "misuse of sqlite3_result_subtype() by %s()",
90143
+ pCtx->pFunc->zName);
90144
+ sqlite3_result_error(pCtx, zErr, -1);
90145
+ return;
90146
+ }
90147
+ #endif /* SQLITE_STRICT_SUBTYPE */
90081
90148
  pOut = pCtx->pOut;
90082
90149
  assert( sqlite3_mutex_held(pOut->db->mutex) );
90083
90150
  pOut->eSubtype = eSubtype & 0xff;
@@ -100447,7 +100514,7 @@ case OP_VCheck: { /* out2 */
100447
100514
  pTab = pOp->p4.pTab;
100448
100515
  assert( pTab!=0 );
100449
100516
  assert( IsVirtual(pTab) );
100450
- assert( pTab->u.vtab.p!=0 );
100517
+ if( pTab->u.vtab.p==0 ) break;
100451
100518
  pVtab = pTab->u.vtab.p->pVtab;
100452
100519
  assert( pVtab!=0 );
100453
100520
  pModule = pVtab->pModule;
@@ -114043,8 +114110,8 @@ SQLITE_PRIVATE int sqlite3ExprListCompare(const ExprList *pA, const ExprList *pB
114043
114110
  */
114044
114111
  SQLITE_PRIVATE int sqlite3ExprCompareSkip(Expr *pA,Expr *pB, int iTab){
114045
114112
  return sqlite3ExprCompare(0,
114046
- sqlite3ExprSkipCollateAndLikely(pA),
114047
- sqlite3ExprSkipCollateAndLikely(pB),
114113
+ sqlite3ExprSkipCollate(pA),
114114
+ sqlite3ExprSkipCollate(pB),
114048
114115
  iTab);
114049
114116
  }
114050
114117
 
@@ -147736,10 +147803,11 @@ static void selectAddSubqueryTypeInfo(Walker *pWalker, Select *p){
147736
147803
  SrcList *pTabList;
147737
147804
  SrcItem *pFrom;
147738
147805
 
147739
- assert( p->selFlags & SF_Resolved );
147740
147806
  if( p->selFlags & SF_HasTypeInfo ) return;
147741
147807
  p->selFlags |= SF_HasTypeInfo;
147742
147808
  pParse = pWalker->pParse;
147809
+ testcase( (p->selFlags & SF_Resolved)==0 );
147810
+ assert( (p->selFlags & SF_Resolved) || IN_RENAME_OBJECT );
147743
147811
  pTabList = p->pSrc;
147744
147812
  for(i=0, pFrom=pTabList->a; i<pTabList->nSrc; i++, pFrom++){
147745
147813
  Table *pTab = pFrom->pTab;
@@ -148761,6 +148829,7 @@ SQLITE_PRIVATE int sqlite3Select(
148761
148829
  TREETRACE(0x1000,pParse,p,
148762
148830
  ("LEFT-JOIN simplifies to JOIN on term %d\n",i));
148763
148831
  pItem->fg.jointype &= ~(JT_LEFT|JT_OUTER);
148832
+ unsetJoinExpr(p->pWhere, pItem->iCursor, 0);
148764
148833
  }
148765
148834
  }
148766
148835
  if( pItem->fg.jointype & JT_LTORJ ){
@@ -148775,17 +148844,15 @@ SQLITE_PRIVATE int sqlite3Select(
148775
148844
  TREETRACE(0x1000,pParse,p,
148776
148845
  ("RIGHT-JOIN simplifies to JOIN on term %d\n",j));
148777
148846
  pI2->fg.jointype &= ~(JT_RIGHT|JT_OUTER);
148847
+ unsetJoinExpr(p->pWhere, pI2->iCursor, 1);
148778
148848
  }
148779
148849
  }
148780
148850
  }
148781
- for(j=pTabList->nSrc-1; j>=i; j--){
148851
+ for(j=pTabList->nSrc-1; j>=0; j--){
148782
148852
  pTabList->a[j].fg.jointype &= ~JT_LTORJ;
148783
148853
  if( pTabList->a[j].fg.jointype & JT_RIGHT ) break;
148784
148854
  }
148785
148855
  }
148786
- assert( pItem->iCursor>=0 );
148787
- unsetJoinExpr(p->pWhere, pItem->iCursor,
148788
- pTabList->a[0].fg.jointype & JT_LTORJ);
148789
148856
  }
148790
148857
 
148791
148858
  /* No further action if this term of the FROM clause is not a subquery */
@@ -166189,6 +166256,20 @@ static SQLITE_NOINLINE void whereAddIndexedExpr(
166189
166256
  continue;
166190
166257
  }
166191
166258
  if( sqlite3ExprIsConstant(pExpr) ) continue;
166259
+ if( pExpr->op==TK_FUNCTION ){
166260
+ /* Functions that might set a subtype should not be replaced by the
166261
+ ** value taken from an expression index since the index omits the
166262
+ ** subtype. https://sqlite.org/forum/forumpost/68d284c86b082c3e */
166263
+ int n;
166264
+ FuncDef *pDef;
166265
+ sqlite3 *db = pParse->db;
166266
+ assert( ExprUseXList(pExpr) );
166267
+ n = pExpr->x.pList ? pExpr->x.pList->nExpr : 0;
166268
+ pDef = sqlite3FindFunction(db, pExpr->u.zToken, n, ENC(db), 0);
166269
+ if( pDef==0 || (pDef->funcFlags & SQLITE_RESULT_SUBTYPE)!=0 ){
166270
+ continue;
166271
+ }
166272
+ }
166192
166273
  p = sqlite3DbMallocRaw(pParse->db, sizeof(IndexedExpr));
166193
166274
  if( p==0 ) break;
166194
166275
  p->pIENext = pParse->pIdxEpr;
@@ -168371,7 +168452,7 @@ SQLITE_PRIVATE int sqlite3WindowRewrite(Parse *pParse, Select *p){
168371
168452
  assert( ExprUseXList(pWin->pOwner) );
168372
168453
  assert( pWin->pWFunc!=0 );
168373
168454
  pArgs = pWin->pOwner->x.pList;
168374
- if( pWin->pWFunc->funcFlags & SQLITE_FUNC_SUBTYPE ){
168455
+ if( pWin->pWFunc->funcFlags & SQLITE_SUBTYPE ){
168375
168456
  selectWindowRewriteEList(pParse, pMWin, pSrc, pArgs, pTab, &pSublist);
168376
168457
  pWin->iArgCol = (pSublist ? pSublist->nExpr : 0);
168377
168458
  pWin->bExprArgs = 1;
@@ -179545,7 +179626,7 @@ SQLITE_PRIVATE int sqlite3CreateFunc(
179545
179626
  assert( SQLITE_FUNC_CONSTANT==SQLITE_DETERMINISTIC );
179546
179627
  assert( SQLITE_FUNC_DIRECT==SQLITE_DIRECTONLY );
179547
179628
  extraFlags = enc & (SQLITE_DETERMINISTIC|SQLITE_DIRECTONLY|
179548
- SQLITE_SUBTYPE|SQLITE_INNOCUOUS);
179629
+ SQLITE_SUBTYPE|SQLITE_INNOCUOUS|SQLITE_RESULT_SUBTYPE);
179549
179630
  enc &= (SQLITE_FUNC_ENCMASK|SQLITE_ANY);
179550
179631
 
179551
179632
  /* The SQLITE_INNOCUOUS flag is the same bit as SQLITE_FUNC_UNSAFE. But
@@ -203134,13 +203215,19 @@ static void jsonAppendNormalizedString(JsonString *p, const char *zIn, u32 N){
203134
203215
  zIn++;
203135
203216
  N -= 2;
203136
203217
  while( N>0 ){
203137
- for(i=0; i<N && zIn[i]!='\\'; i++){}
203218
+ for(i=0; i<N && zIn[i]!='\\' && zIn[i]!='"'; i++){}
203138
203219
  if( i>0 ){
203139
203220
  jsonAppendRawNZ(p, zIn, i);
203140
203221
  zIn += i;
203141
203222
  N -= i;
203142
203223
  if( N==0 ) break;
203143
203224
  }
203225
+ if( zIn[0]=='"' ){
203226
+ jsonAppendRawNZ(p, "\\\"", 2);
203227
+ zIn++;
203228
+ N--;
203229
+ continue;
203230
+ }
203144
203231
  assert( zIn[0]=='\\' );
203145
203232
  switch( (u8)zIn[1] ){
203146
203233
  case '\'':
@@ -203535,7 +203622,8 @@ static void jsonReturnJson(
203535
203622
  JsonParse *pParse, /* The complete JSON */
203536
203623
  JsonNode *pNode, /* Node to return */
203537
203624
  sqlite3_context *pCtx, /* Return value for this function */
203538
- int bGenerateAlt /* Also store the rendered text in zAlt */
203625
+ int bGenerateAlt, /* Also store the rendered text in zAlt */
203626
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203539
203627
  ){
203540
203628
  JsonString s;
203541
203629
  if( pParse->oom ){
@@ -203550,7 +203638,7 @@ static void jsonReturnJson(
203550
203638
  pParse->nAlt = s.nUsed;
203551
203639
  }
203552
203640
  jsonResult(&s);
203553
- sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203641
+ if( !omitSubtype ) sqlite3_result_subtype(pCtx, JSON_SUBTYPE);
203554
203642
  }
203555
203643
  }
203556
203644
 
@@ -203591,7 +203679,8 @@ static u32 jsonHexToInt4(const char *z){
203591
203679
  static void jsonReturn(
203592
203680
  JsonParse *pParse, /* Complete JSON parse tree */
203593
203681
  JsonNode *pNode, /* Node to return */
203594
- sqlite3_context *pCtx /* Return value for this function */
203682
+ sqlite3_context *pCtx, /* Return value for this function */
203683
+ int omitSubtype /* Do not call sqlite3_result_subtype() */
203595
203684
  ){
203596
203685
  switch( pNode->eType ){
203597
203686
  default: {
@@ -203737,7 +203826,7 @@ static void jsonReturn(
203737
203826
  }
203738
203827
  case JSON_ARRAY:
203739
203828
  case JSON_OBJECT: {
203740
- jsonReturnJson(pParse, pNode, pCtx, 0);
203829
+ jsonReturnJson(pParse, pNode, pCtx, 0, omitSubtype);
203741
203830
  break;
203742
203831
  }
203743
203832
  }
@@ -205089,7 +205178,7 @@ static void jsonParseFunc(
205089
205178
  printf("iSubst = %u\n", p->iSubst);
205090
205179
  printf("iHold = %u\n", p->iHold);
205091
205180
  jsonDebugPrintNodeEntries(p->aNode, p->nNode);
205092
- jsonReturnJson(p, p->aNode, ctx, 1);
205181
+ jsonReturnJson(p, p->aNode, ctx, 1, 0);
205093
205182
  }
205094
205183
 
205095
205184
  /*
@@ -205275,15 +205364,14 @@ static void jsonExtractFunc(
205275
205364
  }
205276
205365
  if( pNode ){
205277
205366
  if( flags & JSON_JSON ){
205278
- jsonReturnJson(p, pNode, ctx, 0);
205367
+ jsonReturnJson(p, pNode, ctx, 0, 0);
205279
205368
  }else{
205280
- jsonReturn(p, pNode, ctx);
205281
- sqlite3_result_subtype(ctx, 0);
205369
+ jsonReturn(p, pNode, ctx, 1);
205282
205370
  }
205283
205371
  }
205284
205372
  }else{
205285
205373
  pNode = jsonLookup(p, zPath, 0, ctx);
205286
- if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx);
205374
+ if( p->nErr==0 && pNode ) jsonReturn(p, pNode, ctx, 0);
205287
205375
  }
205288
205376
  }else{
205289
205377
  /* Two or more PATH arguments results in a JSON array with each
@@ -205409,7 +205497,7 @@ static void jsonPatchFunc(
205409
205497
  if( pResult && pX->oom==0 ){
205410
205498
  jsonDebugPrintParse(pX);
205411
205499
  jsonDebugPrintNode(pResult);
205412
- jsonReturnJson(pX, pResult, ctx, 0);
205500
+ jsonReturnJson(pX, pResult, ctx, 0, 0);
205413
205501
  }else{
205414
205502
  sqlite3_result_error_nomem(ctx);
205415
205503
  }
@@ -205488,7 +205576,7 @@ static void jsonRemoveFunc(
205488
205576
  }
205489
205577
  }
205490
205578
  if( (pParse->aNode[0].jnFlags & JNODE_REMOVE)==0 ){
205491
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205579
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205492
205580
  }
205493
205581
  remove_done:
205494
205582
  jsonDebugPrintParse(p);
@@ -205617,7 +205705,7 @@ static void jsonReplaceFunc(
205617
205705
  jsonReplaceNode(ctx, pParse, (u32)(pNode - pParse->aNode), argv[i+1]);
205618
205706
  }
205619
205707
  }
205620
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205708
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205621
205709
  replace_err:
205622
205710
  jsonDebugPrintParse(pParse);
205623
205711
  jsonParseFree(pParse);
@@ -205671,7 +205759,7 @@ static void jsonSetFunc(
205671
205759
  }
205672
205760
  }
205673
205761
  jsonDebugPrintParse(pParse);
205674
- jsonReturnJson(pParse, pParse->aNode, ctx, 1);
205762
+ jsonReturnJson(pParse, pParse->aNode, ctx, 1, 0);
205675
205763
  jsonSetDone:
205676
205764
  jsonParseFree(pParse);
205677
205765
  }
@@ -206186,7 +206274,7 @@ static int jsonEachColumn(
206186
206274
  case JEACH_KEY: {
206187
206275
  if( p->i==0 ) break;
206188
206276
  if( p->eType==JSON_OBJECT ){
206189
- jsonReturn(&p->sParse, pThis, ctx);
206277
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206190
206278
  }else if( p->eType==JSON_ARRAY ){
206191
206279
  u32 iKey;
206192
206280
  if( p->bRecursive ){
@@ -206202,7 +206290,7 @@ static int jsonEachColumn(
206202
206290
  }
206203
206291
  case JEACH_VALUE: {
206204
206292
  if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206205
- jsonReturn(&p->sParse, pThis, ctx);
206293
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206206
206294
  break;
206207
206295
  }
206208
206296
  case JEACH_TYPE: {
@@ -206213,7 +206301,7 @@ static int jsonEachColumn(
206213
206301
  case JEACH_ATOM: {
206214
206302
  if( pThis->jnFlags & JNODE_LABEL ) pThis++;
206215
206303
  if( pThis->eType>=JSON_ARRAY ) break;
206216
- jsonReturn(&p->sParse, pThis, ctx);
206304
+ jsonReturn(&p->sParse, pThis, ctx, 0);
206217
206305
  break;
206218
206306
  }
206219
206307
  case JEACH_ID: {
@@ -206506,34 +206594,43 @@ static sqlite3_module jsonTreeModule = {
206506
206594
  SQLITE_PRIVATE void sqlite3RegisterJsonFunctions(void){
206507
206595
  #ifndef SQLITE_OMIT_JSON
206508
206596
  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),
206597
+ /* calls sqlite3_result_subtype() */
206598
+ /* | */
206599
+ /* Uses cache ______ | __ calls sqlite3_value_subtype() */
206600
+ /* | | | */
206601
+ /* Num args _________ | | | ___ Flags */
206602
+ /* | | | | | */
206603
+ /* | | | | | */
206604
+ JFUNCTION(json, 1, 1, 1, 0, 0, jsonRemoveFunc),
206605
+ JFUNCTION(json_array, -1, 0, 1, 1, 0, jsonArrayFunc),
206606
+ JFUNCTION(json_array_length, 1, 1, 0, 0, 0, jsonArrayLengthFunc),
206607
+ JFUNCTION(json_array_length, 2, 1, 0, 0, 0, jsonArrayLengthFunc),
206608
+ JFUNCTION(json_error_position,1, 1, 0, 0, 0, jsonErrorFunc),
206609
+ JFUNCTION(json_extract, -1, 1, 1, 0, 0, jsonExtractFunc),
206610
+ JFUNCTION(->, 2, 1, 1, 0, JSON_JSON, jsonExtractFunc),
206611
+ JFUNCTION(->>, 2, 1, 0, 0, JSON_SQL, jsonExtractFunc),
206612
+ JFUNCTION(json_insert, -1, 1, 1, 1, 0, jsonSetFunc),
206613
+ JFUNCTION(json_object, -1, 0, 1, 1, 0, jsonObjectFunc),
206614
+ JFUNCTION(json_patch, 2, 1, 1, 0, 0, jsonPatchFunc),
206615
+ JFUNCTION(json_quote, 1, 0, 1, 1, 0, jsonQuoteFunc),
206616
+ JFUNCTION(json_remove, -1, 1, 1, 0, 0, jsonRemoveFunc),
206617
+ JFUNCTION(json_replace, -1, 1, 1, 1, 0, jsonReplaceFunc),
206618
+ JFUNCTION(json_set, -1, 1, 1, 1, JSON_ISSET, jsonSetFunc),
206619
+ JFUNCTION(json_type, 1, 1, 0, 0, 0, jsonTypeFunc),
206620
+ JFUNCTION(json_type, 2, 1, 0, 0, 0, jsonTypeFunc),
206621
+ JFUNCTION(json_valid, 1, 1, 0, 0, 0, jsonValidFunc),
206622
+ #ifdef SQLITE_DEBUG
206623
+ JFUNCTION(json_parse, 1, 1, 1, 0, 0, jsonParseFunc),
206624
+ JFUNCTION(json_test1, 1, 1, 0, 1, 0, jsonTest1Func),
206530
206625
  #endif
206531
206626
  WAGGREGATE(json_group_array, 1, 0, 0,
206532
206627
  jsonArrayStep, jsonArrayFinal, jsonArrayValue, jsonGroupInverse,
206533
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC),
206628
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206629
+ SQLITE_DETERMINISTIC),
206534
206630
  WAGGREGATE(json_group_object, 2, 0, 0,
206535
206631
  jsonObjectStep, jsonObjectFinal, jsonObjectValue, jsonGroupInverse,
206536
- SQLITE_SUBTYPE|SQLITE_UTF8|SQLITE_DETERMINISTIC)
206632
+ SQLITE_SUBTYPE|SQLITE_RESULT_SUBTYPE|SQLITE_UTF8|
206633
+ SQLITE_DETERMINISTIC)
206537
206634
  };
206538
206635
  sqlite3InsertBuiltinFuncs(aJsonFunc, ArraySize(aJsonFunc));
206539
206636
  #endif
@@ -236270,10 +236367,8 @@ static Fts5HashEntry *fts5HashEntryMerge(
236270
236367
  }
236271
236368
 
236272
236369
  /*
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.
236370
+ ** Link all tokens from hash table iHash into a list in sorted order. The
236371
+ ** tokens are not removed from the hash table.
236277
236372
  */
236278
236373
  static int fts5HashEntrySort(
236279
236374
  Fts5Hash *pHash,
@@ -239139,6 +239234,14 @@ static void fts5SegIterHashInit(
239139
239234
  pLeaf->p = (u8*)pList;
239140
239235
  }
239141
239236
  }
239237
+
239238
+ /* The call to sqlite3Fts5HashScanInit() causes the hash table to
239239
+ ** fill the size field of all existing position lists. This means they
239240
+ ** can no longer be appended to. Since the only scenario in which they
239241
+ ** can be appended to is if the previous operation on this table was
239242
+ ** a DELETE, by clearing the Fts5Index.bDelete flag we can avoid this
239243
+ ** possibility altogether. */
239244
+ p->bDelete = 0;
239142
239245
  }else{
239143
239246
  p->rc = sqlite3Fts5HashQuery(p->pHash, sizeof(Fts5Data),
239144
239247
  (const char*)pTerm, nTerm, (void**)&pLeaf, &nList
@@ -240816,7 +240919,7 @@ static void fts5WriteAppendPoslistData(
240816
240919
  const u8 *a = aData;
240817
240920
  int n = nData;
240818
240921
 
240819
- assert( p->pConfig->pgsz>0 );
240922
+ assert( p->pConfig->pgsz>0 || p->rc!=SQLITE_OK );
240820
240923
  while( p->rc==SQLITE_OK
240821
240924
  && (pPage->buf.n + pPage->pgidx.n + n)>=p->pConfig->pgsz
240822
240925
  ){
@@ -242076,8 +242179,9 @@ static int sqlite3Fts5IndexOptimize(Fts5Index *p){
242076
242179
 
242077
242180
  assert( p->rc==SQLITE_OK );
242078
242181
  fts5IndexFlush(p);
242079
- assert( p->nContentlessDelete==0 );
242182
+ assert( p->rc!=SQLITE_OK || p->nContentlessDelete==0 );
242080
242183
  pStruct = fts5StructureRead(p);
242184
+ assert( p->rc!=SQLITE_OK || pStruct!=0 );
242081
242185
  fts5StructureInvalidate(p);
242082
242186
 
242083
242187
  if( pStruct ){
@@ -247654,7 +247758,7 @@ static void fts5SourceIdFunc(
247654
247758
  ){
247655
247759
  assert( nArg==0 );
247656
247760
  UNUSED_PARAM2(nArg, apUnused);
247657
- sqlite3_result_text(pCtx, "fts5: 2023-11-01 11:23:50 17129ba1ff7f0daf37100ee82d507aef7827cf38de1866e2633096ae6ad81301", -1, SQLITE_TRANSIENT);
247761
+ sqlite3_result_text(pCtx, "fts5: 2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4", -1, SQLITE_TRANSIENT);
247658
247762
  }
247659
247763
 
247660
247764
  /*
@@ -252761,7 +252865,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252761
252865
  ** Purpose: Header file for SQLite3 Multiple Ciphers compile-time configuration
252762
252866
  ** Author: Ulrich Telle
252763
252867
  ** Created: 2021-09-27
252764
- ** Copyright: (c) 2019-2022 Ulrich Telle
252868
+ ** Copyright: (c) 2019-2023 Ulrich Telle
252765
252869
  ** License: MIT
252766
252870
  */
252767
252871
 
@@ -252795,6 +252899,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252795
252899
  #define HAVE_CIPHER_RC4 WXSQLITE3_HAVE_CIPHER_RC4
252796
252900
  #endif
252797
252901
 
252902
+ #ifdef WXSQLITE3_HAVE_CIPHER_ASCON128
252903
+ #define HAVE_CIPHER_ASCON128 WXSQLITE3_HAVE_CIPHER_ASCON128
252904
+ #endif
252905
+
252798
252906
  /*
252799
252907
  ** Actual definitions of supported ciphers
252800
252908
  */
@@ -252818,6 +252926,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252818
252926
  #define HAVE_CIPHER_RC4 1
252819
252927
  #endif
252820
252928
 
252929
+ #ifndef HAVE_CIPHER_ASCON128
252930
+ #define HAVE_CIPHER_ASCON128 1
252931
+ #endif
252932
+
252821
252933
  /*
252822
252934
  ** Disable all built-in ciphers on request
252823
252935
  */
@@ -252832,11 +252944,13 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252832
252944
  #undef HAVE_CIPHER_CHACHA20
252833
252945
  #undef HAVE_CIPHER_SQLCIPHER
252834
252946
  #undef HAVE_CIPHER_RC4
252947
+ #undef HAVE_CIPHER_ASCON128
252835
252948
  #define HAVE_CIPHER_AES_128_CBC 0
252836
252949
  #define HAVE_CIPHER_AES_256_CBC 0
252837
252950
  #define HAVE_CIPHER_CHACHA20 0
252838
252951
  #define HAVE_CIPHER_SQLCIPHER 0
252839
252952
  #define HAVE_CIPHER_RC4 0
252953
+ #define HAVE_CIPHER_ASCON128 0
252840
252954
  #endif
252841
252955
 
252842
252956
  /*
@@ -252926,7 +253040,7 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252926
253040
  ** Purpose: Header file for SQLite3 Multiple Ciphers support
252927
253041
  ** Author: Ulrich Telle
252928
253042
  ** Created: 2020-03-01
252929
- ** Copyright: (c) 2019-2022 Ulrich Telle
253043
+ ** Copyright: (c) 2019-2023 Ulrich Telle
252930
253044
  ** License: MIT
252931
253045
  */
252932
253046
 
@@ -252953,10 +253067,10 @@ SQLITE_API const char *sqlite3_sourceid(void){ return SQLITE_SOURCE_ID; }
252953
253067
  #define SQLITE3MC_VERSION_H_
252954
253068
 
252955
253069
  #define SQLITE3MC_VERSION_MAJOR 1
252956
- #define SQLITE3MC_VERSION_MINOR 7
252957
- #define SQLITE3MC_VERSION_RELEASE 4
253070
+ #define SQLITE3MC_VERSION_MINOR 8
253071
+ #define SQLITE3MC_VERSION_RELEASE 0
252958
253072
  #define SQLITE3MC_VERSION_SUBRELEASE 0
252959
- #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.7.4"
253073
+ #define SQLITE3MC_VERSION_STRING "SQLite3 Multiple Ciphers 1.8.0"
252960
253074
 
252961
253075
  #endif /* SQLITE3MC_VERSION_H_ */
252962
253076
  /*** End of #include "sqlite3mc_version.h" ***/
@@ -253115,9 +253229,9 @@ extern "C" {
253115
253229
  ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
253116
253230
  ** [sqlite_version()] and [sqlite_source_id()].
253117
253231
  */
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"
253232
+ #define SQLITE_VERSION "3.44.1"
253233
+ #define SQLITE_VERSION_NUMBER 3044001
253234
+ #define SQLITE_SOURCE_ID "2023-11-22 14:18:12 d295f48e8f367b066b881780c98bdf980a1d550397d5ba0b0e49842c95b3e8b4"
253121
253235
 
253122
253236
  /*
253123
253237
  ** CAPI3REF: Run-Time Library Version Numbers
@@ -258542,13 +258656,27 @@ SQLITE_API int sqlite3_create_window_function(
258542
258656
  ** </dd>
258543
258657
  **
258544
258658
  ** [[SQLITE_SUBTYPE]] <dt>SQLITE_SUBTYPE</dt><dd>
258545
- ** The SQLITE_SUBTYPE flag indicates to SQLite that a function may call
258659
+ ** The SQLITE_SUBTYPE flag indicates to SQLite that a function might call
258546
258660
  ** [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).
258661
+ ** This flag instructs SQLite to omit some corner-case optimizations that
258662
+ ** might disrupt the operation of the [sqlite3_value_subtype()] function,
258663
+ ** causing it to return zero rather than the correct subtype().
258664
+ ** SQL functions that invokes [sqlite3_value_subtype()] should have this
258665
+ ** property. If the SQLITE_SUBTYPE property is omitted, then the return
258666
+ ** value from [sqlite3_value_subtype()] might sometimes be zero even though
258667
+ ** a non-zero subtype was specified by the function argument expression.
258668
+ **
258669
+ ** [[SQLITE_RESULT_SUBTYPE]] <dt>SQLITE_RESULT_SUBTYPE</dt><dd>
258670
+ ** The SQLITE_RESULT_SUBTYPE flag indicates to SQLite that a function might call
258671
+ ** [sqlite3_result_subtype()] to cause a sub-type to be associated with its
258672
+ ** result.
258673
+ ** Every function that invokes [sqlite3_result_subtype()] should have this
258674
+ ** property. If it does not, then the call to [sqlite3_result_subtype()]
258675
+ ** might become a no-op if the function is used as term in an
258676
+ ** [expression index]. On the other hand, SQL functions that never invoke
258677
+ ** [sqlite3_result_subtype()] should avoid setting this property, as the
258678
+ ** purpose of this property is to disable certain optimizations that are
258679
+ ** incompatible with subtypes.
258552
258680
  ** </dd>
258553
258681
  ** </dl>
258554
258682
  */
@@ -258556,6 +258684,7 @@ SQLITE_API int sqlite3_create_window_function(
258556
258684
  #define SQLITE_DIRECTONLY 0x000080000
258557
258685
  #define SQLITE_SUBTYPE 0x000100000
258558
258686
  #define SQLITE_INNOCUOUS 0x000200000
258687
+ #define SQLITE_RESULT_SUBTYPE 0x001000000
258559
258688
 
258560
258689
  /*
258561
258690
  ** CAPI3REF: Deprecated Functions
@@ -258752,6 +258881,12 @@ SQLITE_API int sqlite3_value_encoding(sqlite3_value*);
258752
258881
  ** information can be used to pass a limited amount of context from
258753
258882
  ** one SQL function to another. Use the [sqlite3_result_subtype()]
258754
258883
  ** routine to set the subtype for the return value of an SQL function.
258884
+ **
258885
+ ** Every [application-defined SQL function] that invoke this interface
258886
+ ** should include the [SQLITE_SUBTYPE] property in the text
258887
+ ** encoding argument when the function is [sqlite3_create_function|registered].
258888
+ ** If the [SQLITE_SUBTYPE] property is omitted, then sqlite3_value_subtype()
258889
+ ** might return zero instead of the upstream subtype in some corner cases.
258755
258890
  */
258756
258891
  SQLITE_API unsigned int sqlite3_value_subtype(sqlite3_value*);
258757
258892
 
@@ -258882,14 +259017,22 @@ SQLITE_API sqlite3 *sqlite3_context_db_handle(sqlite3_context*);
258882
259017
  ** <li> ^(when sqlite3_set_auxdata() is invoked again on the same
258883
259018
  ** parameter)^, or
258884
259019
  ** <li> ^(during the original sqlite3_set_auxdata() call when a memory
258885
- ** allocation error occurs.)^ </ul>
259020
+ ** allocation error occurs.)^
259021
+ ** <li> ^(during the original sqlite3_set_auxdata() call if the function
259022
+ ** is evaluated during query planning instead of during query execution,
259023
+ ** as sometimes happens with [SQLITE_ENABLE_STAT4].)^ </ul>
258886
259024
  **
258887
- ** Note the last bullet in particular. The destructor X in
259025
+ ** Note the last two bullets in particular. The destructor X in
258888
259026
  ** sqlite3_set_auxdata(C,N,P,X) might be called immediately, before the
258889
259027
  ** sqlite3_set_auxdata() interface even returns. Hence sqlite3_set_auxdata()
258890
259028
  ** should be called near the end of the function implementation and the
258891
259029
  ** function implementation should not make any use of P after
258892
- ** sqlite3_set_auxdata() has been called.
259030
+ ** sqlite3_set_auxdata() has been called. Furthermore, a call to
259031
+ ** sqlite3_get_auxdata() that occurs immediately after a corresponding call
259032
+ ** to sqlite3_set_auxdata() might still return NULL if an out-of-memory
259033
+ ** condition occurred during the sqlite3_set_auxdata() call or if the
259034
+ ** function is being evaluated during query planning rather than during
259035
+ ** query execution.
258893
259036
  **
258894
259037
  ** ^(In practice, auxiliary data is preserved between function calls for
258895
259038
  ** function parameters that are compile-time constants, including literal
@@ -259163,6 +259306,20 @@ SQLITE_API int sqlite3_result_zeroblob64(sqlite3_context*, sqlite3_uint64 n);
259163
259306
  ** higher order bits are discarded.
259164
259307
  ** The number of subtype bytes preserved by SQLite might increase
259165
259308
  ** in future releases of SQLite.
259309
+ **
259310
+ ** Every [application-defined SQL function] that invokes this interface
259311
+ ** should include the [SQLITE_RESULT_SUBTYPE] property in its
259312
+ ** text encoding argument when the SQL function is
259313
+ ** [sqlite3_create_function|registered]. If the [SQLITE_RESULT_SUBTYPE]
259314
+ ** property is omitted from the function that invokes sqlite3_result_subtype(),
259315
+ ** then in some cases the sqlite3_result_subtype() might fail to set
259316
+ ** the result subtype.
259317
+ **
259318
+ ** If SQLite is compiled with -DSQLITE_STRICT_SUBTYPE=1, then any
259319
+ ** SQL function that invokes the sqlite3_result_subtype() interface
259320
+ ** and that does not have the SQLITE_RESULT_SUBTYPE property will raise
259321
+ ** an error. Future versions of SQLite might enable -DSQLITE_STRICT_SUBTYPE=1
259322
+ ** by default.
259166
259323
  */
259167
259324
  SQLITE_API void sqlite3_result_subtype(sqlite3_context*,unsigned int);
259168
259325
 
@@ -266339,7 +266496,8 @@ int sqlite3_user_delete(
266339
266496
  #define CODEC_TYPE_CHACHA20 3
266340
266497
  #define CODEC_TYPE_SQLCIPHER 4
266341
266498
  #define CODEC_TYPE_RC4 5
266342
- #define CODEC_TYPE_MAX_BUILTIN 5
266499
+ #define CODEC_TYPE_ASCON128 6
266500
+ #define CODEC_TYPE_MAX_BUILTIN 6
266343
266501
 
266344
266502
  /*
266345
266503
  ** Definition of API functions
@@ -272790,6 +272948,8 @@ SQLITE_PRIVATE int sqlite3mcCodecAttach(sqlite3* db, int nDb, const char* zPath,
272790
272948
 
272791
272949
  SQLITE_PRIVATE void sqlite3mcCodecGetKey(sqlite3* db, int nDb, void** zKey, int* nKey);
272792
272950
 
272951
+ SQLITE_PRIVATE void sqlite3mcSecureZeroMemory(void* v, size_t n);
272952
+
272793
272953
  /* Debugging */
272794
272954
 
272795
272955
  #if 0
@@ -274707,6 +274867,1582 @@ SQLITE_PRIVATE const CipherDescriptor mcRC4Descriptor =
274707
274867
  #endif
274708
274868
  /*** End of #include "cipher_sds_rc4.c" ***/
274709
274869
 
274870
+ /* #include "cipher_ascon.c" */
274871
+ /*** Begin of #include "cipher_ascon.c" ***/
274872
+ /*
274873
+ ** Name: cipher_ascon.c
274874
+ ** Purpose: Implementation of cipher Ascon
274875
+ ** Author: Ulrich Telle
274876
+ ** Created: 2023-11-13
274877
+ ** Copyright: (c) 2023-2023 Ulrich Telle
274878
+ ** License: MIT
274879
+ */
274880
+
274881
+ /* #include "cipher_common.h" */
274882
+
274883
+
274884
+ /* --- Ascon --- */
274885
+ #if HAVE_CIPHER_ASCON128
274886
+
274887
+ #define CIPHER_NAME_ASCON128 "ascon128"
274888
+
274889
+ /*
274890
+ ** Configuration parameters for "ascon128a"
274891
+ **
274892
+ ** - kdf_iter : number of iterations for key derivation
274893
+ */
274894
+
274895
+ #define ASCON128_KDF_ITER_DEFAULT 64007
274896
+
274897
+ /* #include "ascon/prolog.h" */
274898
+ /*** Begin of #include "ascon/prolog.h" ***/
274899
+ /*
274900
+ ** Name: prolog.h
274901
+ ** Purpose: Include important header files, before
274902
+ ** Based on: Public domain Ascon reference implementation
274903
+ ** and optimized variants for 32- and 64-bit
274904
+ ** (see https://github.com/ascon/ascon-c)
274905
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
274906
+ ** Modified by: Ulrich Telle
274907
+ ** Copyright: (c) 2023-2023 Ulrich Telle
274908
+ ** License: MIT
274909
+ */
274910
+
274911
+ #ifndef PROLOG_H
274912
+ #define PROLOG_H
274913
+
274914
+ /* #include "word.h" */
274915
+ /*** Begin of #include "word.h" ***/
274916
+ #ifndef WORD_H_
274917
+ #define WORD_H_
274918
+
274919
+ #include <stdint.h>
274920
+ #include <string.h>
274921
+
274922
+ /* #include "bendian.h" */
274923
+ /*** Begin of #include "bendian.h" ***/
274924
+ #ifndef ENDIAN_H_
274925
+ #define ENDIAN_H_
274926
+
274927
+ #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
274928
+
274929
+ /* macros for big endian machines */
274930
+ #ifdef PRAGMA_ENDIAN
274931
+ #pragma message("Using macros for big endian machines")
274932
+ #endif
274933
+ #define ASCON_U64BIG(x) (x)
274934
+ #define ASCON_U32BIG(x) (x)
274935
+ #define ASCON_U16BIG(x) (x)
274936
+
274937
+ #elif defined(_MSC_VER) || \
274938
+ (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
274939
+
274940
+ /* macros for little endian machines */
274941
+ #ifdef PRAGMA_ENDIAN
274942
+ #pragma message("Using macros for little endian machines")
274943
+ #endif
274944
+ #define ASCON_U64BIG(x) \
274945
+ (((0x00000000000000FFULL & (x)) << 56) | \
274946
+ ((0x000000000000FF00ULL & (x)) << 40) | \
274947
+ ((0x0000000000FF0000ULL & (x)) << 24) | \
274948
+ ((0x00000000FF000000ULL & (x)) << 8) | \
274949
+ ((0x000000FF00000000ULL & (x)) >> 8) | \
274950
+ ((0x0000FF0000000000ULL & (x)) >> 24) | \
274951
+ ((0x00FF000000000000ULL & (x)) >> 40) | \
274952
+ ((0xFF00000000000000ULL & (x)) >> 56))
274953
+ #define ASCON_U32BIG(x) \
274954
+ (((0x000000FF & (x)) << 24) | ((0x0000FF00 & (x)) << 8) | \
274955
+ ((0x00FF0000 & (x)) >> 8) | ((0xFF000000 & (x)) >> 24))
274956
+ #define ASCON_U16BIG(x) (((0x00FF & (x)) << 8) | ((0xFF00 & (x)) >> 8))
274957
+
274958
+ #else
274959
+ #error "Ascon byte order macros not defined in bendian.h"
274960
+ #endif
274961
+
274962
+ #endif /* ENDIAN_H_ */
274963
+ /*** End of #include "bendian.h" ***/
274964
+
274965
+ /* #include "forceinline.h" */
274966
+ /*** Begin of #include "forceinline.h" ***/
274967
+ #ifndef FORCEINLINE_H_
274968
+ #define FORCEINLINE_H_
274969
+
274970
+ /* define forceinline macro */
274971
+ #ifdef _MSC_VER
274972
+ #define forceinline __forceinline
274973
+ #elif defined(__GNUC__)
274974
+ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
274975
+ #define forceinline inline __attribute__((__always_inline__))
274976
+ #else
274977
+ #define forceinline static inline
274978
+ #endif
274979
+ #elif defined(__CLANG__)
274980
+ #if __has_attribute(__always_inline__)
274981
+ #define forceinline inline __attribute__((__always_inline__))
274982
+ #else
274983
+ #define forceinline inline
274984
+ #endif
274985
+ #else
274986
+ #define forceinline inline
274987
+ #endif
274988
+
274989
+ #endif /* FORCEINLINE_H_ */
274990
+ /*** End of #include "forceinline.h" ***/
274991
+
274992
+
274993
+ typedef union {
274994
+ uint64_t x;
274995
+ uint32_t w[2];
274996
+ uint8_t b[8];
274997
+ } word_t;
274998
+
274999
+ #define ASCON_U64TOWORD(x) ASCON_U64BIG(x)
275000
+ #define ASCON_WORDTOU64(x) ASCON_U64BIG(x)
275001
+
275002
+ forceinline uint64_t ASCON_ROR(uint64_t x, int n) { return x >> n | x << (-n & 63); }
275003
+
275004
+ forceinline uint64_t ASCON_KEYROT(uint64_t lo2hi, uint64_t hi2lo) {
275005
+ return lo2hi << 32 | hi2lo >> 32;
275006
+ }
275007
+
275008
+ forceinline int ASCON_NOTZERO(uint64_t a, uint64_t b) {
275009
+ uint64_t result = a | b;
275010
+ result |= result >> 32;
275011
+ result |= result >> 16;
275012
+ result |= result >> 8;
275013
+ return ((((int)(result & 0xff) - 1) >> 8) & 1) - 1;
275014
+ }
275015
+
275016
+ forceinline uint64_t ASCON_PAD(int i) { return 0x80ull << (56 - 8 * i); }
275017
+
275018
+ forceinline uint64_t ASCON_PRFS_MLEN(uint64_t len) { return len << 51; }
275019
+
275020
+ forceinline uint64_t ASCON_CLEAR(uint64_t w, int n) {
275021
+ /* undefined for n == 0 */
275022
+ uint64_t mask = ~0ull >> (8 * n);
275023
+ return w & mask;
275024
+ }
275025
+
275026
+ forceinline uint64_t ASCON_MASK(int n) {
275027
+ /* undefined for n == 0 */
275028
+ return ~0ull >> (64 - 8 * n);
275029
+ }
275030
+
275031
+ forceinline uint64_t ASCON_LOAD(const uint8_t* bytes, int n) {
275032
+ uint64_t x = *(uint64_t*)bytes & ASCON_MASK(n);
275033
+ return ASCON_U64TOWORD(x);
275034
+ }
275035
+
275036
+ forceinline void ASCON_STORE(uint8_t* bytes, uint64_t w, int n) {
275037
+ *(uint64_t*)bytes &= ~ASCON_MASK(n);
275038
+ *(uint64_t*)bytes |= ASCON_WORDTOU64(w);
275039
+ }
275040
+
275041
+ forceinline uint64_t ASCON_LOADBYTES(const uint8_t* bytes, int n) {
275042
+ uint64_t x = 0;
275043
+ memcpy(&x, bytes, n);
275044
+ return ASCON_U64TOWORD(x);
275045
+ }
275046
+
275047
+ forceinline void ASCON_STOREBYTES(uint8_t* bytes, uint64_t w, int n) {
275048
+ uint64_t x = ASCON_WORDTOU64(w);
275049
+ memcpy(bytes, &x, n);
275050
+ }
275051
+
275052
+ #endif /* WORD_H_ */
275053
+ /*** End of #include "word.h" ***/
275054
+
275055
+
275056
+ #endif
275057
+ /*** End of #include "ascon/prolog.h" ***/
275058
+
275059
+ /* #include "ascon/aead.c" */
275060
+ /*** Begin of #include "ascon/aead.c" ***/
275061
+ /*
275062
+ ** Name: aead.c
275063
+ ** Purpose: Stream encryption/decryption with Ascon
275064
+ ** Based on: Public domain Ascon reference implementation
275065
+ ** and optimized variants for 32- and 64-bit
275066
+ ** (see https://github.com/ascon/ascon-c)
275067
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275068
+ ** Modified by: Ulrich Telle
275069
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275070
+ ** License: MIT
275071
+ */
275072
+
275073
+ /* #include "api.h" */
275074
+ /*** Begin of #include "api.h" ***/
275075
+ /*
275076
+ ** Name: api.h
275077
+ ** Purpose: Definition of preprocessor symbols
275078
+ ** Based on: Public domain Ascon reference implementation
275079
+ ** and optimized variants for 32- and 64-bit
275080
+ ** (see https://github.com/ascon/ascon-c)
275081
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275082
+ ** Combined symbols from AEAD and HASH
275083
+ ** Modified by: Ulrich Telle
275084
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275085
+ ** License: MIT
275086
+ */
275087
+
275088
+ #define CRYPTO_VERSION "1.2.7"
275089
+ #define CRYPTO_KEYBYTES 16
275090
+ #define CRYPTO_NSECBYTES 0
275091
+ #define CRYPTO_NPUBBYTES 16
275092
+ #define CRYPTO_ABYTES 16
275093
+ #define CRYPTO_NOOVERLAP 1
275094
+ #define ASCON_AEAD_RATE 8
275095
+
275096
+ #define CRYPTO_BYTES 32
275097
+ #define ASCON_HASH_BYTES 32 /* HASH */
275098
+ #define ASCON_HASH_ROUNDS 12
275099
+
275100
+ #define ASCON_AEAD_KEY_LEN CRYPTO_KEYBYTES
275101
+ #define ASCON_AEAD_NONCE_LEN CRYPTO_NPUBBYTES
275102
+ #define ASCON_AEAD_TAG_LEN CRYPTO_ABYTES
275103
+ #define ASCON_SALT_LEN CRYPTO_NPUBBYTES
275104
+ /*** End of #include "api.h" ***/
275105
+
275106
+ /* #include "ascon.h" */
275107
+ /*** Begin of #include "ascon.h" ***/
275108
+ #ifndef ASCON_H_
275109
+ #define ASCON_H_
275110
+
275111
+ #include <stdint.h>
275112
+
275113
+ /* #include "api.h" */
275114
+
275115
+ /* #include "config.h" */
275116
+ /*** Begin of #include "config.h" ***/
275117
+ #ifndef CONFIG_H_
275118
+ #define CONFIG_H_
275119
+
275120
+ /* inline the ascon mode */
275121
+ #ifndef ASCON_INLINE_MODE
275122
+ #define ASCON_INLINE_MODE 1
275123
+ #endif
275124
+
275125
+ /* inline all permutations */
275126
+ #ifndef ASCON_INLINE_PERM
275127
+ #define ASCON_INLINE_PERM 1
275128
+ #endif
275129
+
275130
+ /* unroll permutation loops */
275131
+ #ifndef ASCON_UNROLL_LOOPS
275132
+ #define ASCON_UNROLL_LOOPS 1
275133
+ #endif
275134
+
275135
+ #endif /* CONFIG_H_ */
275136
+ /*** End of #include "config.h" ***/
275137
+
275138
+
275139
+ typedef union {
275140
+ uint64_t x[5];
275141
+ uint32_t w[5][2];
275142
+ uint8_t b[5][8];
275143
+ } ascon_state_t;
275144
+
275145
+ #ifdef ASCON_AEAD_RATE
275146
+
275147
+ #define ASCON_KEYWORDS (CRYPTO_KEYBYTES + 7) / 8
275148
+
275149
+ typedef union {
275150
+ uint64_t x[ASCON_KEYWORDS];
275151
+ uint32_t w[ASCON_KEYWORDS][2];
275152
+ uint8_t b[ASCON_KEYWORDS][8];
275153
+ } ascon_key_t;
275154
+
275155
+ #if !ASCON_INLINE_MODE
275156
+
275157
+ void ascon_loadkey(ascon_key_t* key, const uint8_t* k);
275158
+ void ascon_initaead(ascon_state_t* s, const ascon_key_t* key,
275159
+ const uint8_t* npub);
275160
+ void ascon_adata(ascon_state_t* s, const uint8_t* ad, uint64_t adlen);
275161
+ void ascon_encrypt(ascon_state_t* s, uint8_t* c, const uint8_t* m,
275162
+ uint64_t mlen);
275163
+ void ascon_decrypt(ascon_state_t* s, uint8_t* m, const uint8_t* c,
275164
+ uint64_t clen);
275165
+ void ascon_final(ascon_state_t* s, const ascon_key_t* k);
275166
+
275167
+ #endif
275168
+
275169
+ #endif
275170
+
275171
+ #ifdef ASCON_HASH_BYTES
275172
+
275173
+ #if !ASCON_INLINE_MODE
275174
+
275175
+ void ascon_inithash(ascon_state_t* s);
275176
+ void ascon_absorb(ascon_state_t* s, const uint8_t* in, uint64_t inlen);
275177
+ void ascon_squeeze(ascon_state_t* s, uint8_t* out, uint64_t outlen);
275178
+
275179
+ #endif
275180
+
275181
+ #endif
275182
+
275183
+ #endif /* ASCON_H_ */
275184
+ /*** End of #include "ascon.h" ***/
275185
+
275186
+ /* #include "crypto_aead.h" */
275187
+ /*** Begin of #include "crypto_aead.h" ***/
275188
+ /*
275189
+ ** Name: hash.c
275190
+ ** Purpose: API definition for Hash algorithm with Ascon
275191
+ ** Based on: Public domain Ascon reference implementation
275192
+ ** and optimized variants for 32- and 64-bit
275193
+ ** (see https://github.com/ascon/ascon-c)
275194
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275195
+ ** Modified by: Ulrich Telle
275196
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275197
+ ** License: MIT
275198
+ */
275199
+
275200
+ #ifndef CRYPTO_AEAD_H
275201
+ #define CRYPTO_AEAD_H
275202
+
275203
+ #include <stddef.h>
275204
+
275205
+ /*
275206
+ ** Encryption using ASCON-AEAD.
275207
+ **
275208
+ ** \param ctext Output buffer for encrypted text (same length as plain text)
275209
+ ** \param tag Output buffer for tag with fixed length of ASCON_AEAD_TAG_LEN
275210
+ ** \param mtext Input buffer with plain message text
275211
+ ** \param mlen Length of message text
275212
+ ** \param ad Input buffer with associated data
275213
+ ** \param adlen Length of associated data
275214
+ ** \param nonce Buffer with nonce data
275215
+ ** \param k Buffer with key data
275216
+ */
275217
+ int ascon_aead_encrypt(uint8_t* ctext, uint8_t tag[ASCON_AEAD_TAG_LEN],
275218
+ const uint8_t* mtext, uint64_t mlen,
275219
+ const uint8_t* ad, uint64_t adlen,
275220
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275221
+ const uint8_t k[ASCON_AEAD_KEY_LEN]);
275222
+
275223
+ /*
275224
+ ** Encryption using ASCON-AEAD.
275225
+ **
275226
+ ** \param mtext Output buffer with decrypted plain message text (same length as encrypted text)
275227
+ ** \param ctext Input buffer for encrypted text
275228
+ ** \param clen Length of encrypted text
275229
+ ** \param ad Input buffer with associated data
275230
+ ** \param adlen Length of associated data
275231
+ ** \param tag Input buffer for expected tag with fixed length of ASCON_AEAD_TAG_LEN
275232
+ ** \param nonce Buffer with nonce data
275233
+ ** \param k Buffer with key data
275234
+ */
275235
+ int ascon_aead_decrypt(uint8_t* mtext, const uint8_t* ctext, uint64_t clen,
275236
+ const uint8_t* ad, uint64_t adlen,
275237
+ const uint8_t tag[ASCON_AEAD_TAG_LEN],
275238
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275239
+ const uint8_t k[ASCON_AEAD_KEY_LEN]);
275240
+
275241
+ #endif
275242
+ /*** End of #include "crypto_aead.h" ***/
275243
+
275244
+ /* #include "permutations.h" */
275245
+ /*** Begin of #include "permutations.h" ***/
275246
+ #ifndef PERMUTATIONS_H_
275247
+ #define PERMUTATIONS_H_
275248
+
275249
+ #include <stdint.h>
275250
+
275251
+ /* #include "api.h" */
275252
+
275253
+ /* #include "ascon.h" */
275254
+
275255
+ /* #include "config.h" */
275256
+
275257
+ /* #include "constants.h" */
275258
+ /*** Begin of #include "constants.h" ***/
275259
+ #ifndef CONSTANTS_H_
275260
+ #define CONSTANTS_H_
275261
+
275262
+ #include <stdint.h>
275263
+
275264
+ #define ASCON_128_KEYBYTES 16
275265
+ #define ASCON_128A_KEYBYTES 16
275266
+ #define ASCON_80PQ_KEYBYTES 20
275267
+
275268
+ #define ASCON_128_RATE 8
275269
+ #define ASCON_128A_RATE 16
275270
+ #define ASCON_HASH_RATE 8
275271
+ #define ASCON_PRF_IN_RATE 32
275272
+ #define ASCON_PRFA_IN_RATE 40
275273
+ #define ASCON_PRF_OUT_RATE 16
275274
+
275275
+ #define ASCON_128_PA_ROUNDS 12
275276
+ #define ASCON_128_PB_ROUNDS 6
275277
+ #define ASCON_128A_PA_ROUNDS 12
275278
+ #define ASCON_128A_PB_ROUNDS 8
275279
+
275280
+ #define ASCON_HASH_PA_ROUNDS 12
275281
+ #define ASCON_HASH_PB_ROUNDS 12
275282
+ #define ASCON_HASHA_PA_ROUNDS 12
275283
+ #define ASCON_HASHA_PB_ROUNDS 8
275284
+
275285
+ #define ASCON_PRF_PA_ROUNDS 12
275286
+ #define ASCON_PRF_PB_ROUNDS 12
275287
+ #define ASCON_PRFA_PA_ROUNDS 12
275288
+ #define ASCON_PRFA_PB_ROUNDS 8
275289
+
275290
+ #define ASCON_128_IV 0x80400c0600000000ull
275291
+ #define ASCON_128A_IV 0x80800c0800000000ull
275292
+ #define ASCON_80PQ_IV 0xa0400c0600000000ull
275293
+
275294
+ #define ASCON_HASH_IV 0x00400c0000000100ull
275295
+ #define ASCON_HASHA_IV 0x00400c0400000100ull
275296
+ #define ASCON_XOF_IV 0x00400c0000000000ull
275297
+ #define ASCON_XOFA_IV 0x00400c0400000000ull
275298
+
275299
+ #define ASCON_HASH_IV0 0xee9398aadb67f03dull
275300
+ #define ASCON_HASH_IV1 0x8bb21831c60f1002ull
275301
+ #define ASCON_HASH_IV2 0xb48a92db98d5da62ull
275302
+ #define ASCON_HASH_IV3 0x43189921b8f8e3e8ull
275303
+ #define ASCON_HASH_IV4 0x348fa5c9d525e140ull
275304
+
275305
+ #define ASCON_HASHA_IV0 0x01470194fc6528a6ull
275306
+ #define ASCON_HASHA_IV1 0x738ec38ac0adffa7ull
275307
+ #define ASCON_HASHA_IV2 0x2ec8e3296c76384cull
275308
+ #define ASCON_HASHA_IV3 0xd6f6a54d7f52377dull
275309
+ #define ASCON_HASHA_IV4 0xa13c42a223be8d87ull
275310
+
275311
+ #define ASCON_XOF_IV0 0xb57e273b814cd416ull
275312
+ #define ASCON_XOF_IV1 0x2b51042562ae2420ull
275313
+ #define ASCON_XOF_IV2 0x66a3a7768ddf2218ull
275314
+ #define ASCON_XOF_IV3 0x5aad0a7a8153650cull
275315
+ #define ASCON_XOF_IV4 0x4f3e0e32539493b6ull
275316
+
275317
+ #define ASCON_XOFA_IV0 0x44906568b77b9832ull
275318
+ #define ASCON_XOFA_IV1 0xcd8d6cae53455532ull
275319
+ #define ASCON_XOFA_IV2 0xf7b5212756422129ull
275320
+ #define ASCON_XOFA_IV3 0x246885e1de0d225bull
275321
+ #define ASCON_XOFA_IV4 0xa8cb5ce33449973full
275322
+
275323
+ #define ASCON_MAC_IV 0x80808c0000000080ull
275324
+ #define ASCON_MACA_IV 0x80808c0400000080ull
275325
+ #define ASCON_PRF_IV 0x80808c0000000000ull
275326
+ #define ASCON_PRFA_IV 0x80808c0400000000ull
275327
+ #define ASCON_PRFS_IV 0x80004c8000000000ull
275328
+
275329
+ #define ASCON_RC0 0xf0
275330
+ #define ASCON_RC1 0xe1
275331
+ #define ASCON_RC2 0xd2
275332
+ #define ASCON_RC3 0xc3
275333
+ #define ASCON_RC4 0xb4
275334
+ #define ASCON_RC5 0xa5
275335
+ #define ASCON_RC6 0x96
275336
+ #define ASCON_RC7 0x87
275337
+ #define ASCON_RC8 0x78
275338
+ #define ASCON_RC9 0x69
275339
+ #define ASCON_RCa 0x5a
275340
+ #define ASCON_RCb 0x4b
275341
+
275342
+ #define ASCON_RC(i) (i)
275343
+
275344
+ #define ASCON_START(n) ((3 + (n)) << 4 | (12 - (n)))
275345
+ #define ASCON_INC -0x0f
275346
+ #define ASCON_END 0x3c
275347
+
275348
+ #endif /* CONSTANTS_H_ */
275349
+ /*** End of #include "constants.h" ***/
275350
+
275351
+ /* #include "printstate.h" */
275352
+ /*** Begin of #include "printstate.h" ***/
275353
+ #ifndef PRINTSTATE_H_
275354
+ #define PRINTSTATE_H_
275355
+
275356
+ #ifdef ASCON_PRINT_STATE
275357
+
275358
+ /* #include "ascon.h" */
275359
+
275360
+ /* #include "word.h" */
275361
+
275362
+
275363
+ void ascon_printword(const char* text, const uint64_t x);
275364
+ void ascon_printstate(const char* text, const ascon_state_t* s);
275365
+
275366
+ #else
275367
+
275368
+ #define ascon_printword(text, w) \
275369
+ do { \
275370
+ } while (0)
275371
+
275372
+ #define ascon_printstate(text, s) \
275373
+ do { \
275374
+ } while (0)
275375
+
275376
+ #endif
275377
+
275378
+ #endif /* PRINTSTATE_H_ */
275379
+ /*** End of #include "printstate.h" ***/
275380
+
275381
+ /* #include "round.h" */
275382
+ /*** Begin of #include "round.h" ***/
275383
+ /*
275384
+ ** Name: round.h
275385
+ ** Purpose: Selector for Ascon implementation variant for 32- resp 64-bit
275386
+ ** Based on: Public domain Ascon reference implementation
275387
+ ** and optimized variants for 32- and 64-bit
275388
+ ** (see https://github.com/ascon/ascon-c)
275389
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275390
+ ** Modified by: Ulrich Telle
275391
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275392
+ ** License: MIT
275393
+ */
275394
+
275395
+ #ifndef ROUND_H
275396
+ #define ROUND_H
275397
+
275398
+ /* #include "forceinline.h" */
275399
+
275400
+
275401
+ #if defined(__LP64__) || defined(_WIN64)
275402
+ /* 64-bit machine, Windows or Linux or OS X */
275403
+ /* #include "round64.h" */
275404
+ /*** Begin of #include "round64.h" ***/
275405
+ #ifndef ROUND64_H_
275406
+ #define ROUND64_H_
275407
+
275408
+ /* #include "ascon.h" */
275409
+
275410
+ /* #include "constants.h" */
275411
+
275412
+ /* #include "forceinline.h" */
275413
+
275414
+ /* #include "printstate.h" */
275415
+
275416
+ /* #include "word.h" */
275417
+
275418
+
275419
+ forceinline void ASCON_ROUND(ascon_state_t* s, uint8_t C) {
275420
+ ascon_state_t t;
275421
+ /* round constant */
275422
+ s->x[2] ^= C;
275423
+ /* s-box layer */
275424
+ s->x[0] ^= s->x[4];
275425
+ s->x[4] ^= s->x[3];
275426
+ s->x[2] ^= s->x[1];
275427
+ t.x[0] = s->x[0] ^ (~s->x[1] & s->x[2]);
275428
+ t.x[2] = s->x[2] ^ (~s->x[3] & s->x[4]);
275429
+ t.x[4] = s->x[4] ^ (~s->x[0] & s->x[1]);
275430
+ t.x[1] = s->x[1] ^ (~s->x[2] & s->x[3]);
275431
+ t.x[3] = s->x[3] ^ (~s->x[4] & s->x[0]);
275432
+ t.x[1] ^= t.x[0];
275433
+ t.x[3] ^= t.x[2];
275434
+ t.x[0] ^= t.x[4];
275435
+ /* linear layer */
275436
+ s->x[2] = t.x[2] ^ ASCON_ROR(t.x[2], 6 - 1);
275437
+ s->x[3] = t.x[3] ^ ASCON_ROR(t.x[3], 17 - 10);
275438
+ s->x[4] = t.x[4] ^ ASCON_ROR(t.x[4], 41 - 7);
275439
+ s->x[0] = t.x[0] ^ ASCON_ROR(t.x[0], 28 - 19);
275440
+ s->x[1] = t.x[1] ^ ASCON_ROR(t.x[1], 61 - 39);
275441
+ s->x[2] = t.x[2] ^ ASCON_ROR(s->x[2], 1);
275442
+ s->x[3] = t.x[3] ^ ASCON_ROR(s->x[3], 10);
275443
+ s->x[4] = t.x[4] ^ ASCON_ROR(s->x[4], 7);
275444
+ s->x[0] = t.x[0] ^ ASCON_ROR(s->x[0], 19);
275445
+ s->x[1] = t.x[1] ^ ASCON_ROR(s->x[1], 39);
275446
+ s->x[2] = ~s->x[2];
275447
+ ascon_printstate(" round output", s);
275448
+ }
275449
+
275450
+ forceinline void ASCON_PROUNDS(ascon_state_t* s, int nr) {
275451
+ int i = ASCON_START(nr);
275452
+ do {
275453
+ ASCON_ROUND(s, ASCON_RC(i));
275454
+ i += ASCON_INC;
275455
+ } while (i != ASCON_END);
275456
+ }
275457
+
275458
+ #endif /* ROUND64_H_ */
275459
+ /*** End of #include "round64.h" ***/
275460
+
275461
+ #else
275462
+ /* 32-bit machine, Windows or Linux or OS X */
275463
+ /* #include "round32.h" */
275464
+ /*** Begin of #include "round32.h" ***/
275465
+ #ifndef ROUND32_H_
275466
+ #define ROUND32_H_
275467
+
275468
+ /* #include "ascon.h" */
275469
+
275470
+ /* #include "constants.h" */
275471
+
275472
+ /* #include "forceinline.h" */
275473
+
275474
+ /* #include "printstate.h" */
275475
+
275476
+ /* #include "word.h" */
275477
+
275478
+
275479
+ forceinline void ASCON_ROUND(ascon_state_t* s, uint8_t C) {
275480
+ uint64_t xtemp;
275481
+ /* round constant */
275482
+ s->x[2] ^= C;
275483
+ /* s-box layer */
275484
+ s->x[0] ^= s->x[4];
275485
+ s->x[4] ^= s->x[3];
275486
+ s->x[2] ^= s->x[1];
275487
+ xtemp = s->x[0] & ~s->x[4];
275488
+ s->x[0] ^= s->x[2] & ~s->x[1];
275489
+ s->x[2] ^= s->x[4] & ~s->x[3];
275490
+ s->x[4] ^= s->x[1] & ~s->x[0];
275491
+ s->x[1] ^= s->x[3] & ~s->x[2];
275492
+ s->x[3] ^= xtemp;
275493
+ s->x[1] ^= s->x[0];
275494
+ s->x[3] ^= s->x[2];
275495
+ s->x[0] ^= s->x[4];
275496
+ s->x[2] = ~s->x[2];
275497
+ /* linear layer */
275498
+ s->x[0] ^=
275499
+ (s->x[0] >> 19) ^ (s->x[0] << 45) ^ (s->x[0] >> 28) ^ (s->x[0] << 36);
275500
+ s->x[1] ^=
275501
+ (s->x[1] >> 61) ^ (s->x[1] << 3) ^ (s->x[1] >> 39) ^ (s->x[1] << 25);
275502
+ s->x[2] ^=
275503
+ (s->x[2] >> 1) ^ (s->x[2] << 63) ^ (s->x[2] >> 6) ^ (s->x[2] << 58);
275504
+ s->x[3] ^=
275505
+ (s->x[3] >> 10) ^ (s->x[3] << 54) ^ (s->x[3] >> 17) ^ (s->x[3] << 47);
275506
+ s->x[4] ^=
275507
+ (s->x[4] >> 7) ^ (s->x[4] << 57) ^ (s->x[4] >> 41) ^ (s->x[4] << 23);
275508
+ ascon_printstate(" round output", s);
275509
+ }
275510
+
275511
+ forceinline void ASCON_PROUNDS(ascon_state_t* s, int nr) {
275512
+ int i = ASCON_START(nr);
275513
+ do {
275514
+ ASCON_ROUND(s, ASCON_RC(i));
275515
+ i += ASCON_INC;
275516
+ } while (i != ASCON_END);
275517
+ }
275518
+
275519
+ #endif /* ROUND32_H_ */
275520
+ /*** End of #include "round32.h" ***/
275521
+
275522
+ #endif
275523
+
275524
+ #endif
275525
+ /*** End of #include "round.h" ***/
275526
+
275527
+
275528
+ forceinline void ASCON_P12ROUNDS(ascon_state_t* s) {
275529
+ ASCON_ROUND(s, ASCON_RC0);
275530
+ ASCON_ROUND(s, ASCON_RC1);
275531
+ ASCON_ROUND(s, ASCON_RC2);
275532
+ ASCON_ROUND(s, ASCON_RC3);
275533
+ ASCON_ROUND(s, ASCON_RC4);
275534
+ ASCON_ROUND(s, ASCON_RC5);
275535
+ ASCON_ROUND(s, ASCON_RC6);
275536
+ ASCON_ROUND(s, ASCON_RC7);
275537
+ ASCON_ROUND(s, ASCON_RC8);
275538
+ ASCON_ROUND(s, ASCON_RC9);
275539
+ ASCON_ROUND(s, ASCON_RCa);
275540
+ ASCON_ROUND(s, ASCON_RCb);
275541
+ }
275542
+
275543
+ forceinline void ASCON_P8ROUNDS(ascon_state_t* s) {
275544
+ ASCON_ROUND(s, ASCON_RC4);
275545
+ ASCON_ROUND(s, ASCON_RC5);
275546
+ ASCON_ROUND(s, ASCON_RC6);
275547
+ ASCON_ROUND(s, ASCON_RC7);
275548
+ ASCON_ROUND(s, ASCON_RC8);
275549
+ ASCON_ROUND(s, ASCON_RC9);
275550
+ ASCON_ROUND(s, ASCON_RCa);
275551
+ ASCON_ROUND(s, ASCON_RCb);
275552
+ }
275553
+
275554
+ forceinline void ASCON_P6ROUNDS(ascon_state_t* s) {
275555
+ ASCON_ROUND(s, ASCON_RC6);
275556
+ ASCON_ROUND(s, ASCON_RC7);
275557
+ ASCON_ROUND(s, ASCON_RC8);
275558
+ ASCON_ROUND(s, ASCON_RC9);
275559
+ ASCON_ROUND(s, ASCON_RCa);
275560
+ ASCON_ROUND(s, ASCON_RCb);
275561
+ }
275562
+
275563
+ #if ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
275564
+
275565
+ forceinline void ASCON_P(ascon_state_t* s, int nr) {
275566
+ if (nr == 12) ASCON_P12ROUNDS(s);
275567
+ if (nr == 8) ASCON_P8ROUNDS(s);
275568
+ if (nr == 6) ASCON_P6ROUNDS(s);
275569
+ }
275570
+
275571
+ #elif !ASCON_INLINE_PERM && ASCON_UNROLL_LOOPS
275572
+
275573
+ void ASCON_P12(ascon_state_t* s);
275574
+ void ASCON_P8(ascon_state_t* s);
275575
+ void ASCON_P6(ascon_state_t* s);
275576
+
275577
+ forceinline void ASCON_P(ascon_state_t* s, int nr) {
275578
+ if (nr == 12) ASCON_P12(s);
275579
+ if (nr == 8) ASCON_P8(s);
275580
+ if (nr == 6) ASCON_P6(s);
275581
+ }
275582
+
275583
+ #elif ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS
275584
+
275585
+ forceinline void ASCON_P(ascon_state_t* s, int nr) { ASCON_PROUNDS(s, nr); }
275586
+
275587
+ #else /* !ASCON_INLINE_PERM && !ASCON_UNROLL_LOOPS */
275588
+
275589
+ void ASCON_P(ascon_state_t* s, int nr);
275590
+
275591
+ #endif
275592
+
275593
+ #endif /* PERMUTATIONS_H_ */
275594
+ /*** End of #include "permutations.h" ***/
275595
+
275596
+ /* #include "printstate.h" */
275597
+
275598
+
275599
+ #if !ASCON_INLINE_MODE
275600
+ #undef forceinline
275601
+ #define forceinline
275602
+ #endif
275603
+
275604
+ #ifdef ASCON_AEAD_RATE
275605
+
275606
+ forceinline void ascon_loadkey(ascon_key_t* key, const uint8_t* k) {
275607
+ #if CRYPTO_KEYBYTES == 16
275608
+ key->x[0] = ASCON_LOAD(k, 8);
275609
+ key->x[1] = ASCON_LOAD(k + 8, 8);
275610
+ #else /* CRYPTO_KEYBYTES == 20 */
275611
+ key->x[0] = ASCON_KEYROT(0, ASCON_LOADBYTES(k, 4));
275612
+ key->x[1] = ASCON_LOADBYTES(k + 4, 8);
275613
+ key->x[2] = ASCON_LOADBYTES(k + 12, 8);
275614
+ #endif
275615
+ }
275616
+
275617
+ forceinline void ascon_initaead(ascon_state_t* s, const ascon_key_t* key,
275618
+ const uint8_t* npub) {
275619
+ #if CRYPTO_KEYBYTES == 16
275620
+ if (ASCON_AEAD_RATE == 8) s->x[0] = ASCON_128_IV;
275621
+ if (ASCON_AEAD_RATE == 16) s->x[0] = ASCON_128A_IV;
275622
+ s->x[1] = key->x[0];
275623
+ s->x[2] = key->x[1];
275624
+ #else /* CRYPTO_KEYBYTES == 20 */
275625
+ s->x[0] = key->x[0] ^ ASCON_80PQ_IV;
275626
+ s->x[1] = key->x[1];
275627
+ s->x[2] = key->x[2];
275628
+ #endif
275629
+ s->x[3] = ASCON_LOAD(npub, 8);
275630
+ s->x[4] = ASCON_LOAD(npub + 8, 8);
275631
+ ascon_printstate("init 1st key xor", s);
275632
+ ASCON_P(s, 12);
275633
+ #if CRYPTO_KEYBYTES == 16
275634
+ s->x[3] ^= key->x[0];
275635
+ s->x[4] ^= key->x[1];
275636
+ #else /* CRYPTO_KEYBYTES == 20 */
275637
+ s->x[2] ^= key->x[0];
275638
+ s->x[3] ^= key->x[1];
275639
+ s->x[4] ^= key->x[2];
275640
+ #endif
275641
+ ascon_printstate("init 2nd key xor", s);
275642
+ }
275643
+
275644
+ forceinline void ascon_adata(ascon_state_t* s, const uint8_t* ad,
275645
+ uint64_t adlen) {
275646
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275647
+ if (adlen) {
275648
+ /* full associated data blocks */
275649
+ while (adlen >= ASCON_AEAD_RATE) {
275650
+ s->x[0] ^= ASCON_LOAD(ad, 8);
275651
+ if (ASCON_AEAD_RATE == 16) s->x[1] ^= ASCON_LOAD(ad + 8, 8);
275652
+ ascon_printstate("absorb adata", s);
275653
+ ASCON_P(s, nr);
275654
+ ad += ASCON_AEAD_RATE;
275655
+ adlen -= ASCON_AEAD_RATE;
275656
+ }
275657
+ /* final associated data block */
275658
+ uint64_t* px = &s->x[0];
275659
+ if (ASCON_AEAD_RATE == 16 && adlen >= 8) {
275660
+ s->x[0] ^= ASCON_LOAD(ad, 8);
275661
+ px = &s->x[1];
275662
+ ad += 8;
275663
+ adlen -= 8;
275664
+ }
275665
+ *px ^= ASCON_PAD(adlen);
275666
+ if (adlen) *px ^= ASCON_LOADBYTES(ad, adlen);
275667
+ ascon_printstate("pad adata", s);
275668
+ ASCON_P(s, nr);
275669
+ }
275670
+ /* domain separation */
275671
+ s->x[4] ^= 1;
275672
+ ascon_printstate("domain separation", s);
275673
+ }
275674
+
275675
+ forceinline void ascon_encrypt(ascon_state_t* s, uint8_t* c, const uint8_t* m,
275676
+ uint64_t mlen) {
275677
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275678
+ /* full plaintext blocks */
275679
+ while (mlen >= ASCON_AEAD_RATE) {
275680
+ s->x[0] ^= ASCON_LOAD(m, 8);
275681
+ ASCON_STORE(c, s->x[0], 8);
275682
+ if (ASCON_AEAD_RATE == 16) {
275683
+ s->x[1] ^= ASCON_LOAD(m + 8, 8);
275684
+ ASCON_STORE(c + 8, s->x[1], 8);
275685
+ }
275686
+ ascon_printstate("absorb plaintext", s);
275687
+ ASCON_P(s, nr);
275688
+ m += ASCON_AEAD_RATE;
275689
+ c += ASCON_AEAD_RATE;
275690
+ mlen -= ASCON_AEAD_RATE;
275691
+ }
275692
+ /* final plaintext block */
275693
+ uint64_t* px = &s->x[0];
275694
+ if (ASCON_AEAD_RATE == 16 && mlen >= 8) {
275695
+ s->x[0] ^= ASCON_LOAD(m, 8);
275696
+ ASCON_STORE(c, s->x[0], 8);
275697
+ px = &s->x[1];
275698
+ m += 8;
275699
+ c += 8;
275700
+ mlen -= 8;
275701
+ }
275702
+ *px ^= ASCON_PAD(mlen);
275703
+ if (mlen) {
275704
+ *px ^= ASCON_LOADBYTES(m, mlen);
275705
+ ASCON_STOREBYTES(c, *px, mlen);
275706
+ }
275707
+ ascon_printstate("pad plaintext", s);
275708
+ }
275709
+
275710
+ forceinline void ascon_decrypt(ascon_state_t* s, uint8_t* m, const uint8_t* c,
275711
+ uint64_t clen) {
275712
+ const int nr = (ASCON_AEAD_RATE == 8) ? 6 : 8;
275713
+ /* full ciphertext blocks */
275714
+ while (clen >= ASCON_AEAD_RATE) {
275715
+ uint64_t cx = ASCON_LOAD(c, 8);
275716
+ s->x[0] ^= cx;
275717
+ ASCON_STORE(m, s->x[0], 8);
275718
+ s->x[0] = cx;
275719
+ if (ASCON_AEAD_RATE == 16) {
275720
+ cx = ASCON_LOAD(c + 8, 8);
275721
+ s->x[1] ^= cx;
275722
+ ASCON_STORE(m + 8, s->x[1], 8);
275723
+ s->x[1] = cx;
275724
+ }
275725
+ ascon_printstate("insert ciphertext", s);
275726
+ ASCON_P(s, nr);
275727
+ m += ASCON_AEAD_RATE;
275728
+ c += ASCON_AEAD_RATE;
275729
+ clen -= ASCON_AEAD_RATE;
275730
+ }
275731
+ /* final ciphertext block */
275732
+ uint64_t* px = &s->x[0];
275733
+ if (ASCON_AEAD_RATE == 16 && clen >= 8) {
275734
+ uint64_t cx = ASCON_LOAD(c, 8);
275735
+ s->x[0] ^= cx;
275736
+ ASCON_STORE(m, s->x[0], 8);
275737
+ s->x[0] = cx;
275738
+ px = &s->x[1];
275739
+ m += 8;
275740
+ c += 8;
275741
+ clen -= 8;
275742
+ }
275743
+ *px ^= ASCON_PAD(clen);
275744
+ if (clen) {
275745
+ uint64_t cx = ASCON_LOADBYTES(c, clen);
275746
+ *px ^= cx;
275747
+ ASCON_STOREBYTES(m, *px, clen);
275748
+ *px = ASCON_CLEAR(*px, clen);
275749
+ *px ^= cx;
275750
+ }
275751
+ ascon_printstate("pad ciphertext", s);
275752
+ }
275753
+
275754
+ forceinline void ascon_final(ascon_state_t* s, const ascon_key_t* key) {
275755
+ #if CRYPTO_KEYBYTES == 16
275756
+ if (ASCON_AEAD_RATE == 8) {
275757
+ s->x[1] ^= key->x[0];
275758
+ s->x[2] ^= key->x[1];
275759
+ } else {
275760
+ s->x[2] ^= key->x[0];
275761
+ s->x[3] ^= key->x[1];
275762
+ }
275763
+ #else /* CRYPTO_KEYBYTES == 20 */
275764
+ s->x[1] ^= KEYROT(key->x[0], key->x[1]);
275765
+ s->x[2] ^= KEYROT(key->x[1], key->x[2]);
275766
+ s->x[3] ^= KEYROT(key->x[2], 0);
275767
+ #endif
275768
+ ascon_printstate("final 1st key xor", s);
275769
+ ASCON_P(s, 12);
275770
+ #if CRYPTO_KEYBYTES == 16
275771
+ s->x[3] ^= key->x[0];
275772
+ s->x[4] ^= key->x[1];
275773
+ #else /* CRYPTO_KEYBYTES == 20 */
275774
+ s->x[3] ^= key->x[1];
275775
+ s->x[4] ^= key->x[2];
275776
+ #endif
275777
+ ascon_printstate("final 2nd key xor", s);
275778
+ }
275779
+
275780
+ int ascon_aead_encrypt(uint8_t* ctext,
275781
+ uint8_t tag[ASCON_AEAD_TAG_LEN],
275782
+ const uint8_t* mtext, uint64_t mlen,
275783
+ const uint8_t* ad, uint64_t adlen,
275784
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275785
+ const uint8_t k[ASCON_AEAD_KEY_LEN])
275786
+ {
275787
+ ascon_state_t s;
275788
+ /* perform ascon computation */
275789
+ ascon_key_t key;
275790
+ ascon_loadkey(&key, k);
275791
+ ascon_initaead(&s, &key, nonce);
275792
+ ascon_adata(&s, ad, adlen);
275793
+ ascon_encrypt(&s, ctext, mtext, mlen);
275794
+ ascon_final(&s, &key);
275795
+ /* set tag */
275796
+ ASCON_STOREBYTES(tag, s.x[3], 8);
275797
+ ASCON_STOREBYTES(tag + 8, s.x[4], 8);
275798
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275799
+ sqlite3mcSecureZeroMemory(&key, sizeof(ascon_key_t));
275800
+ return 0;
275801
+ }
275802
+
275803
+ int ascon_aead_decrypt(uint8_t* mtext,
275804
+ const uint8_t* ctext, uint64_t clen,
275805
+ const uint8_t* ad, uint64_t adlen,
275806
+ const uint8_t tag[ASCON_AEAD_TAG_LEN],
275807
+ const uint8_t nonce[ASCON_AEAD_NONCE_LEN],
275808
+ const uint8_t k[ASCON_AEAD_KEY_LEN])
275809
+ {
275810
+ int rc = 0;
275811
+ ascon_state_t s;
275812
+ if (clen < CRYPTO_ABYTES) return -1;
275813
+ /* perform ascon computation */
275814
+ ascon_key_t key;
275815
+ ascon_loadkey(&key, k);
275816
+ ascon_initaead(&s, &key, nonce);
275817
+ ascon_adata(&s, ad, adlen);
275818
+ ascon_decrypt(&s, mtext, ctext, clen);
275819
+ ascon_final(&s, &key);
275820
+ /* verify tag (should be constant time, check compiler output) */
275821
+ s.x[3] ^= ASCON_LOADBYTES(tag, 8);
275822
+ s.x[4] ^= ASCON_LOADBYTES(tag + 8, 8);
275823
+ rc = ASCON_NOTZERO(s.x[3], s.x[4]);
275824
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275825
+ sqlite3mcSecureZeroMemory(&key, sizeof(ascon_key_t));
275826
+ return rc;
275827
+ }
275828
+
275829
+ #endif
275830
+ /*** End of #include "ascon/aead.c" ***/
275831
+
275832
+ /* #include "ascon/hash.c" */
275833
+ /*** Begin of #include "ascon/hash.c" ***/
275834
+ /*
275835
+ ** Name: hash.c
275836
+ ** Purpose: Hash algorithm with Ascon
275837
+ ** Based on: Public domain Ascon reference implementation
275838
+ ** and optimized variants for 32- and 64-bit
275839
+ ** (see https://github.com/ascon/ascon-c)
275840
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275841
+ ** Modified by: Ulrich Telle
275842
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275843
+ ** License: MIT
275844
+ */
275845
+
275846
+ /* #include "api.h" */
275847
+
275848
+ /* #include "ascon.h" */
275849
+
275850
+ /* #include "crypto_hash.h" */
275851
+ /*** Begin of #include "crypto_hash.h" ***/
275852
+ /*
275853
+ ** Name: hash.c
275854
+ ** Purpose: API definition for Hash algorithm with Ascon
275855
+ ** Based on: Public domain Ascon reference implementation
275856
+ ** and optimized variants for 32- and 64-bit
275857
+ ** (see https://github.com/ascon/ascon-c)
275858
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275859
+ ** Modified by: Ulrich Telle
275860
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275861
+ ** License: MIT
275862
+ */
275863
+
275864
+ #ifndef CRYPTO_HASH_H
275865
+ #define CRYPTO_HASH_H
275866
+
275867
+ #include <stddef.h>
275868
+
275869
+ /*
275870
+ ** Derives hash value using ASCON-HASH.
275871
+ **
275872
+ ** \param out Output buffer for hash with fixed length of ASCON_HASH_BYTES
275873
+ ** \param in Buffer with input data
275874
+ ** \param passwordlen Length of input data in bytes
275875
+ */
275876
+ int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen);
275877
+
275878
+ #endif
275879
+ /*** End of #include "crypto_hash.h" ***/
275880
+
275881
+ /* #include "permutations.h" */
275882
+
275883
+ /* #include "printstate.h" */
275884
+
275885
+
275886
+ #if !ASCON_INLINE_MODE
275887
+ #undef forceinline
275888
+ #define forceinline
275889
+ #endif
275890
+
275891
+ #ifdef ASCON_HASH_BYTES
275892
+
275893
+ forceinline void ascon_inithash(ascon_state_t* s) {
275894
+ int i;
275895
+ /* initialize */
275896
+ #ifdef ASCON_PRINT_STATE
275897
+ #if ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 12
275898
+ s->x[0] = ASCON_HASH_IV;
275899
+ #elif ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 8
275900
+ s->x[0] = ASCON_HASHA_IV;
275901
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 12
275902
+ s->x[0] = ASCON_XOF_IV;
275903
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 8
275904
+ s->x[0] = ASCON_XOFA_IV;
275905
+ #endif
275906
+ for (i = 1; i < 5; ++i) s->x[i] = 0;
275907
+ ascon_printstate("initial value", s);
275908
+ ASCON_P(s, 12);
275909
+ #endif
275910
+ #if ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 12
275911
+ const uint64_t iv[5] = {ASCON_HASH_IV0, ASCON_HASH_IV1, ASCON_HASH_IV2,
275912
+ ASCON_HASH_IV3, ASCON_HASH_IV4};
275913
+ #elif ASCON_HASH_BYTES == 32 && ASCON_HASH_ROUNDS == 8
275914
+ const uint64_t iv[5] = {ASCON_HASHA_IV0, ASCON_HASHA_IV1, ASCON_HASHA_IV2,
275915
+ ASCON_HASHA_IV3, ASCON_HASHA_IV4};
275916
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 12
275917
+ const uint64_t iv[5] = {ASCON_XOF_IV0, ASCON_XOF_IV1, ASCON_XOF_IV2,
275918
+ ASCON_XOF_IV3, ASCON_XOF_IV4};
275919
+ #elif ASCON_HASH_BYTES == 0 && ASCON_HASH_ROUNDS == 8
275920
+ const uint64_t iv[5] = {ASCON_XOFA_IV0, ASCON_XOFA_IV1, ASCON_XOFA_IV2,
275921
+ ASCON_XOFA_IV3, ASCON_XOFA_IV4};
275922
+ #endif
275923
+ for (i = 0; i < 5; ++i) s->x[i] = (iv[i]);
275924
+ ascon_printstate("initialization", s);
275925
+ }
275926
+
275927
+ forceinline void ascon_absorb(ascon_state_t* s, const uint8_t* in,
275928
+ uint64_t inlen) {
275929
+ /* absorb full plaintext blocks */
275930
+ while (inlen >= ASCON_HASH_RATE) {
275931
+ s->x[0] ^= ASCON_LOAD(in, 8);
275932
+ ascon_printstate("absorb plaintext", s);
275933
+ ASCON_P(s, ASCON_HASH_ROUNDS);
275934
+ in += ASCON_HASH_RATE;
275935
+ inlen -= ASCON_HASH_RATE;
275936
+ }
275937
+ /* absorb final plaintext block */
275938
+ s->x[0] ^= ASCON_LOADBYTES(in, inlen);
275939
+ s->x[0] ^= ASCON_PAD(inlen);
275940
+ ascon_printstate("pad plaintext", s);
275941
+ }
275942
+
275943
+ forceinline void ascon_squeeze(ascon_state_t* s, uint8_t* out,
275944
+ uint64_t outlen) {
275945
+ /* squeeze full output blocks */
275946
+ ASCON_P(s, 12);
275947
+ while (outlen > ASCON_HASH_RATE) {
275948
+ ASCON_STORE(out, s->x[0], 8);
275949
+ ascon_printstate("squeeze output", s);
275950
+ ASCON_P(s, ASCON_HASH_ROUNDS);
275951
+ out += ASCON_HASH_RATE;
275952
+ outlen -= ASCON_HASH_RATE;
275953
+ }
275954
+ /* squeeze final output block */
275955
+ ASCON_STOREBYTES(out, s->x[0], outlen);
275956
+ ascon_printstate("squeeze output", s);
275957
+ }
275958
+
275959
+ int ascon_hash(uint8_t* out, const uint8_t* in, uint64_t inlen)
275960
+ {
275961
+ ascon_state_t s;
275962
+ ascon_inithash(&s);
275963
+ ascon_absorb(&s, in, inlen);
275964
+ ascon_squeeze(&s, out, ASCON_HASH_BYTES);
275965
+ sqlite3mcSecureZeroMemory(&s, sizeof(ascon_state_t));
275966
+ return 0;
275967
+ }
275968
+
275969
+ #endif
275970
+ /*** End of #include "ascon/hash.c" ***/
275971
+
275972
+ /* #include "ascon/pbkdf2.c" */
275973
+ /*** Begin of #include "ascon/pbkdf2.c" ***/
275974
+ /*
275975
+ ** Name: pbkdf2.c
275976
+ ** Purpose: Implementation of PBKDF2 algoritm with Ascon
275977
+ ** Based on: Public domain Ascon reference implementation
275978
+ ** and optimized variants for 32- and 64-bit
275979
+ ** (see https://github.com/ascon/ascon-c)
275980
+ ** and the paper "Additional Modes for ASCON Version 1.1"
275981
+ ** by Rhys Weatherley, Southern Storm Software, Pty Ltd.
275982
+ ** Remarks: API functions adapted for use in SQLite3 Multiple Ciphers
275983
+ ** Created by: Ulrich Telle
275984
+ ** Copyright: (c) 2023-2023 Ulrich Telle
275985
+ ** License: MIT
275986
+ */
275987
+
275988
+ #define ASCON_HASH_SIZE 32
275989
+ #define ASCON_PBKDF2_SIZE 32
275990
+
275991
+ void ascon_pbkdf2_init(ascon_state_t* state, const char* functionName,
275992
+ const unsigned char* custom, uint32_t customlen, uint32_t outlen)
275993
+ {
275994
+ /* Format the initial block with the function name and output length */
275995
+ uint8_t initial[ASCON_HASH_SIZE];
275996
+ size_t fnLength = functionName ? strlen(functionName) : 0;
275997
+
275998
+ if (fnLength == 0)
275999
+ {
276000
+ /* No function name specified */
276001
+ memset(initial, 0, ASCON_HASH_SIZE);
276002
+ }
276003
+ else if (fnLength <= ASCON_HASH_SIZE)
276004
+ {
276005
+ /* Pad the function name with zeroes */
276006
+ memcpy(initial, functionName, fnLength);
276007
+ memset(initial + fnLength, 0, ASCON_HASH_SIZE - fnLength);
276008
+ }
276009
+ else
276010
+ {
276011
+ ascon_hash(initial, (const uint8_t*) functionName, fnLength);
276012
+ }
276013
+
276014
+ state->x[0] = ASCON_HASH_IV;
276015
+ state->x[1] = ASCON_LOAD(initial, 8);
276016
+ state->x[2] = ASCON_LOAD(initial + 8, 8);
276017
+ state->x[3] = ASCON_LOAD(initial + 16, 8);
276018
+ state->x[4] = ASCON_LOAD(initial + 24, 8);
276019
+ ASCON_P(state, 12);
276020
+
276021
+ if (customlen > 0)
276022
+ {
276023
+ ascon_absorb(state, custom, customlen);
276024
+ ASCON_P(state, 12);
276025
+ /* domain separation */
276026
+ state->x[4] ^= 1;
276027
+ }
276028
+ }
276029
+
276030
+ /*
276031
+ * Implementation of the "F" function from RFC 8018, section 5.2
276032
+ *
276033
+ * Note: Instead of HMAC like in RFC 8018, use the following PRF:
276034
+ * PRF(P, X) = ASCON-cXOF(X, 256, "PBKDF2", P)
276035
+ */
276036
+ static void ascon_pbkdf2_f(ascon_state_t* state,
276037
+ uint8_t* T, /*uint8_t* U,*/
276038
+ const uint8_t* salt, uint32_t saltlen,
276039
+ uint32_t count, uint32_t blocknum)
276040
+ {
276041
+ uint32_t asconSaltLen = (saltlen < ASCON_SALT_LEN) ? saltlen : ASCON_SALT_LEN;
276042
+ uint8_t temp[ASCON_SALT_LEN+4];
276043
+ ascon_state_t state2;
276044
+ int j;
276045
+
276046
+ memset(temp, 0, ASCON_SALT_LEN);
276047
+ memcpy(temp, salt, asconSaltLen);
276048
+ STORE32_BE(temp+ASCON_SALT_LEN, blocknum);
276049
+
276050
+ /* Copy initial state */
276051
+ for (j = 0; j < 5; ++j) state2.x[j] = state->x[j];
276052
+
276053
+ ascon_absorb(&state2, temp, ASCON_SALT_LEN+4);
276054
+ ascon_squeeze(&state2, T, ASCON_PBKDF2_SIZE);
276055
+ sqlite3mcSecureZeroMemory(temp, sizeof(temp));
276056
+
276057
+ if (count > 1)
276058
+ {
276059
+ uint8_t U[ASCON_PBKDF2_SIZE];
276060
+ memcpy(U, T, ASCON_PBKDF2_SIZE);
276061
+ while (count > 1)
276062
+ {
276063
+ uint8_t* dst = T;
276064
+ uint8_t* src = U;
276065
+ uint32_t len = ASCON_PBKDF2_SIZE;
276066
+ /* Copy initial state */
276067
+ for (j = 0; j < 5; ++j) state2.x[j] = state->x[j];
276068
+ /* Absorb U */
276069
+ ascon_absorb(&state2, U, ASCON_PBKDF2_SIZE);
276070
+ /* Squeeze next U */
276071
+ ascon_squeeze(&state2, U, ASCON_PBKDF2_SIZE);
276072
+ /* XOR T with U */
276073
+ while (len > 0)
276074
+ {
276075
+ *dst++ ^= *src++;
276076
+ --len;
276077
+ }
276078
+ --count;
276079
+ }
276080
+ sqlite3mcSecureZeroMemory(U, sizeof(U));
276081
+ }
276082
+ sqlite3mcSecureZeroMemory(&state2, sizeof(ascon_state_t));
276083
+ }
276084
+
276085
+ void ascon_pbkdf2(uint8_t* out, uint32_t outlen,
276086
+ const uint8_t* password, uint32_t passwordlen,
276087
+ const uint8_t* salt, uint32_t saltlen, uint32_t count)
276088
+ {
276089
+ ascon_state_t state;
276090
+ uint32_t blocknum = 1;
276091
+ ascon_pbkdf2_init(&state, "PBKDF2", password, passwordlen, ASCON_PBKDF2_SIZE);
276092
+ while (outlen > 0)
276093
+ {
276094
+ if (outlen >= ASCON_PBKDF2_SIZE)
276095
+ {
276096
+ ascon_pbkdf2_f(&state, out, /*U,*/ salt, saltlen, count, blocknum);
276097
+ out += ASCON_PBKDF2_SIZE;
276098
+ outlen -= ASCON_PBKDF2_SIZE;
276099
+ }
276100
+ else
276101
+ {
276102
+ uint8_t T[ASCON_PBKDF2_SIZE];
276103
+ ascon_pbkdf2_f(&state, T, /*U,*/ salt, saltlen, count, blocknum);
276104
+ memcpy(out, T, outlen);
276105
+ sqlite3mcSecureZeroMemory(T, sizeof(T));
276106
+ break;
276107
+ }
276108
+ ++blocknum;
276109
+ }
276110
+ }
276111
+ /*** End of #include "ascon/pbkdf2.c" ***/
276112
+
276113
+
276114
+ SQLITE_PRIVATE CipherParams mcAscon128Params[] =
276115
+ {
276116
+ { "kdf_iter", ASCON128_KDF_ITER_DEFAULT, ASCON128_KDF_ITER_DEFAULT, 1, 0x7fffffff },
276117
+ CIPHER_PARAMS_SENTINEL
276118
+ };
276119
+
276120
+ #define KEYLENGTH_ASCON128 32
276121
+ #define SALTLENGTH_ASCON128 16
276122
+ #define PAGE_NONCE_LEN_ASCON128 16
276123
+ #define PAGE_TAG_LEN_ASCON128 16
276124
+ #define PAGE_RESERVED_ASCON128 (PAGE_NONCE_LEN_ASCON128 + PAGE_TAG_LEN_ASCON128)
276125
+
276126
+ typedef struct _ascon128Cipher
276127
+ {
276128
+ int m_kdfIter;
276129
+ int m_keyLength;
276130
+ uint8_t m_key[KEYLENGTH_ASCON128];
276131
+ uint8_t m_salt[SALTLENGTH_ASCON128];
276132
+ } Ascon128Cipher;
276133
+
276134
+ static void*
276135
+ AllocateAscon128Cipher(sqlite3* db)
276136
+ {
276137
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) sqlite3_malloc(sizeof(Ascon128Cipher));
276138
+ if (ascon128Cipher != NULL)
276139
+ {
276140
+ memset(ascon128Cipher, 0, sizeof(Ascon128Cipher));
276141
+ ascon128Cipher->m_keyLength = KEYLENGTH_ASCON128;
276142
+ memset(ascon128Cipher->m_key, 0, KEYLENGTH_ASCON128);
276143
+ memset(ascon128Cipher->m_salt, 0, SALTLENGTH_ASCON128);
276144
+ }
276145
+ if (ascon128Cipher != NULL)
276146
+ {
276147
+ CipherParams* cipherParams = sqlite3mcGetCipherParams(db, CIPHER_NAME_ASCON128);
276148
+ ascon128Cipher->m_kdfIter = sqlite3mcGetCipherParameter(cipherParams, "kdf_iter");
276149
+ }
276150
+ return ascon128Cipher;
276151
+ }
276152
+
276153
+ static void
276154
+ FreeAscon128Cipher(void* cipher)
276155
+ {
276156
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276157
+ memset(ascon128Cipher, 0, sizeof(Ascon128Cipher));
276158
+ sqlite3_free(ascon128Cipher);
276159
+ }
276160
+
276161
+ static void
276162
+ CloneAscon128Cipher(void* cipherTo, void* cipherFrom)
276163
+ {
276164
+ Ascon128Cipher* ascon128CipherTo = (Ascon128Cipher*) cipherTo;
276165
+ Ascon128Cipher* ascon128CipherFrom = (Ascon128Cipher*) cipherFrom;
276166
+ ascon128CipherTo->m_kdfIter = ascon128CipherFrom->m_kdfIter;
276167
+ ascon128CipherTo->m_keyLength = ascon128CipherFrom->m_keyLength;
276168
+ memcpy(ascon128CipherTo->m_key, ascon128CipherFrom->m_key, KEYLENGTH_ASCON128);
276169
+ memcpy(ascon128CipherTo->m_salt, ascon128CipherFrom->m_salt, SALTLENGTH_ASCON128);
276170
+ }
276171
+
276172
+ static int
276173
+ GetLegacyAscon128Cipher(void* cipher)
276174
+ {
276175
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*)cipher;
276176
+ return 0;
276177
+ }
276178
+
276179
+ static int
276180
+ GetPageSizeAscon128Cipher(void* cipher)
276181
+ {
276182
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276183
+ int pageSize = 0;
276184
+ return pageSize;
276185
+ }
276186
+
276187
+ static int
276188
+ GetReservedAscon128Cipher(void* cipher)
276189
+ {
276190
+ return PAGE_RESERVED_ASCON128;
276191
+ }
276192
+
276193
+ static unsigned char*
276194
+ GetSaltAscon128Cipher(void* cipher)
276195
+ {
276196
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276197
+ return ascon128Cipher->m_salt;
276198
+ }
276199
+
276200
+ static void
276201
+ GenerateKeyAscon128Cipher(void* cipher, BtShared* pBt, char* userPassword, int passwordLength, int rekey, unsigned char* cipherSalt)
276202
+ {
276203
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276204
+ int bypass = 0;
276205
+
276206
+ Pager *pPager = pBt->pPager;
276207
+ sqlite3_file* fd = (isOpen(pPager->fd)) ? pPager->fd : NULL;
276208
+
276209
+ int keyOnly = 1;
276210
+ if (rekey || fd == NULL || sqlite3OsRead(fd, ascon128Cipher->m_salt, SALTLENGTH_ASCON128, 0) != SQLITE_OK)
276211
+ {
276212
+ chacha20_rng(ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276213
+ keyOnly = 0;
276214
+ }
276215
+ else if (cipherSalt != NULL)
276216
+ {
276217
+ memcpy(ascon128Cipher->m_salt, cipherSalt, SALTLENGTH_ASCON128);
276218
+ }
276219
+
276220
+ /* Bypass key derivation if the key string starts with "raw:" */
276221
+ if (passwordLength > 4 && !memcmp(userPassword, "raw:", 4))
276222
+ {
276223
+ const int nRaw = passwordLength - 4;
276224
+ const unsigned char* zRaw = (const unsigned char*) userPassword + 4;
276225
+ switch (nRaw)
276226
+ {
276227
+ /* Binary key (and salt) */
276228
+ case KEYLENGTH_ASCON128 + SALTLENGTH_ASCON128:
276229
+ if (!keyOnly)
276230
+ {
276231
+ memcpy(ascon128Cipher->m_salt, zRaw + KEYLENGTH_ASCON128, SALTLENGTH_ASCON128);
276232
+ }
276233
+ /* fall-through */
276234
+ case KEYLENGTH_ASCON128:
276235
+ memcpy(ascon128Cipher->m_key, zRaw, KEYLENGTH_ASCON128);
276236
+ bypass = 1;
276237
+ break;
276238
+
276239
+ /* Hex-encoded key */
276240
+ case 2 * KEYLENGTH_ASCON128:
276241
+ if (sqlite3mcIsHexKey(zRaw, nRaw) != 0)
276242
+ {
276243
+ sqlite3mcConvertHex2Bin(zRaw, nRaw, ascon128Cipher->m_key);
276244
+ bypass = 1;
276245
+ }
276246
+ break;
276247
+
276248
+ /* Hex-encoded key and salt */
276249
+ case 2 * (KEYLENGTH_ASCON128 + SALTLENGTH_ASCON128):
276250
+ if (sqlite3mcIsHexKey(zRaw, nRaw) != 0)
276251
+ {
276252
+ sqlite3mcConvertHex2Bin(zRaw, 2 * KEYLENGTH_ASCON128, ascon128Cipher->m_key);
276253
+ if (!keyOnly)
276254
+ {
276255
+ sqlite3mcConvertHex2Bin(zRaw + 2 * KEYLENGTH_ASCON128, 2 * SALTLENGTH_ASCON128, ascon128Cipher->m_salt);
276256
+ }
276257
+ bypass = 1;
276258
+ }
276259
+ break;
276260
+
276261
+ default:
276262
+ break;
276263
+ }
276264
+ }
276265
+
276266
+ if (!bypass)
276267
+ {
276268
+ ascon_pbkdf2(ascon128Cipher->m_key, KEYLENGTH_ASCON128,
276269
+ (const uint8_t*) userPassword, passwordLength,
276270
+ ascon128Cipher->m_salt, SALTLENGTH_ASCON128, ascon128Cipher->m_kdfIter);
276271
+ }
276272
+ SQLITE3MC_DEBUG_LOG("generate: codec=%p pFile=%p\n", ascon128Cipher, fd);
276273
+ SQLITE3MC_DEBUG_HEX("generate key:", ascon128Cipher->m_key, KEYLENGTH_ASCON128);
276274
+ SQLITE3MC_DEBUG_HEX("generate salt:", ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276275
+ }
276276
+
276277
+ static int
276278
+ AsconGenOtk(uint8_t* out, const uint8_t* key, const uint8_t* nonce, int page)
276279
+ {
276280
+ ascon_state_t s;
276281
+ uint8_t temp[KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128+4];
276282
+ memcpy(temp, key, KEYLENGTH_ASCON128);
276283
+ memcpy(temp+KEYLENGTH_ASCON128, nonce, PAGE_NONCE_LEN_ASCON128);
276284
+ STORE32_BE(temp+KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128, page);
276285
+ ascon_inithash(&s);
276286
+ ascon_absorb(&s, temp, KEYLENGTH_ASCON128+PAGE_NONCE_LEN_ASCON128+4);
276287
+ ascon_squeeze(&s, out, ASCON_HASH_BYTES);
276288
+ sqlite3mcSecureZeroMemory(temp, sizeof(temp));
276289
+ return 0;
276290
+ }
276291
+
276292
+ static int
276293
+ EncryptPageAscon128Cipher(void* cipher, int page, unsigned char* data, int len, int reserved)
276294
+ {
276295
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276296
+ int rc = SQLITE_OK;
276297
+ int nReserved = (reserved == 0) ? 0 : GetReservedAscon128Cipher(cipher);
276298
+ int n = len - nReserved;
276299
+ uint64_t mlen = n;
276300
+
276301
+ /* Generate one-time keys */
276302
+ uint8_t otk[ASCON_HASH_BYTES];
276303
+ int offset;
276304
+
276305
+ /* Check whether number of required reserved bytes and actually reserved bytes match */
276306
+ if (nReserved > reserved)
276307
+ {
276308
+ return SQLITE_CORRUPT;
276309
+ }
276310
+
276311
+ if (nReserved > 0)
276312
+ {
276313
+ /* Encrypt and authenticate */
276314
+ memset(otk, 0, ASCON_HASH_BYTES);
276315
+ /* Generate nonce */
276316
+ chacha20_rng(data + n + PAGE_TAG_LEN_ASCON128, PAGE_NONCE_LEN_ASCON128);
276317
+ AsconGenOtk(otk, ascon128Cipher->m_key, data + n + PAGE_TAG_LEN_ASCON128, page);
276318
+
276319
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276320
+ ascon_aead_encrypt(data + offset, data + n, data + offset, mlen - offset,
276321
+ NULL /* ad */, 0 /* adlen*/,
276322
+ data + n + PAGE_TAG_LEN_ASCON128, otk);
276323
+ if (page == 1)
276324
+ {
276325
+ memcpy(data, ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276326
+ }
276327
+ }
276328
+ else
276329
+ {
276330
+ /* Encrypt only */
276331
+ uint8_t nonce[PAGE_NONCE_LEN_ASCON128];
276332
+ uint8_t dummyTag[PAGE_TAG_LEN_ASCON128];
276333
+ memset(dummyTag, 0, PAGE_TAG_LEN_ASCON128);
276334
+ memset(otk, 0, ASCON_HASH_BYTES);
276335
+ sqlite3mcGenerateInitialVector(page, nonce);
276336
+ AsconGenOtk(otk, ascon128Cipher->m_key, nonce, page);
276337
+
276338
+ /* Encrypt */
276339
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276340
+ ascon_aead_encrypt(data + offset, dummyTag, data + offset, mlen - offset,
276341
+ NULL /* ad */, 0 /* adlen*/,
276342
+ nonce, otk);
276343
+ if (page == 1)
276344
+ {
276345
+ memcpy(data, ascon128Cipher->m_salt, SALTLENGTH_ASCON128);
276346
+ }
276347
+ }
276348
+
276349
+ return rc;
276350
+ }
276351
+
276352
+ static int
276353
+ DecryptPageAscon128Cipher(void* cipher, int page, unsigned char* data, int len, int reserved, int hmacCheck)
276354
+ {
276355
+ Ascon128Cipher* ascon128Cipher = (Ascon128Cipher*) cipher;
276356
+ int rc = SQLITE_OK;
276357
+ int nReserved = (reserved == 0) ? 0 : GetReservedAscon128Cipher(cipher);
276358
+ int n = len - nReserved;
276359
+ uint64_t clen = n;
276360
+ int tagOk;
276361
+
276362
+ /* Generate one-time keys */
276363
+ uint8_t otk[ASCON_HASH_BYTES];
276364
+ int offset;
276365
+
276366
+ /* Check whether number of required reserved bytes and actually reserved bytes match */
276367
+ if (nReserved > reserved)
276368
+ {
276369
+ return (page == 1) ? SQLITE_NOTADB : SQLITE_CORRUPT;
276370
+ }
276371
+
276372
+ if (nReserved > 0)
276373
+ {
276374
+ /* Decrypt and verify MAC */
276375
+ memset(otk, 0, ASCON_HASH_BYTES);
276376
+ AsconGenOtk(otk, ascon128Cipher->m_key, data + n + PAGE_TAG_LEN_ASCON128, page);
276377
+
276378
+ /* Determine MAC and decrypt */
276379
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276380
+ tagOk = ascon_aead_decrypt(data + offset, data + offset, clen - offset,
276381
+ NULL /* ad */, 0 /* adlen */,
276382
+ data + n, data + n + PAGE_TAG_LEN_ASCON128, otk);
276383
+ if (hmacCheck != 0)
276384
+ {
276385
+ /* Verify the MAC */
276386
+ if (tagOk != 0)
276387
+ {
276388
+ SQLITE3MC_DEBUG_LOG("decrypt: codec=%p page=%d\n", ascon128Cipher, page);
276389
+ SQLITE3MC_DEBUG_HEX("decrypt key:", ascon128Cipher->m_key, 32);
276390
+ SQLITE3MC_DEBUG_HEX("decrypt otk:", otk, 64);
276391
+ SQLITE3MC_DEBUG_HEX("decrypt data+00:", data, 16);
276392
+ SQLITE3MC_DEBUG_HEX("decrypt data+24:", data + 24, 16);
276393
+ SQLITE3MC_DEBUG_HEX("decrypt data+n:", data + n, 16);
276394
+ SQLITE3MC_DEBUG_HEX("decrypt tag r:", data + n + PAGE_NONCE_LEN_ASCON128, PAGE_TAG_LEN_ASCON128);
276395
+ SQLITE3MC_DEBUG_HEX("decrypt tag c:", tag, PAGE_TAG_LEN_ASCON128);
276396
+ /* Bad MAC */
276397
+ rc = (page == 1) ? SQLITE_NOTADB : SQLITE_CORRUPT;
276398
+ }
276399
+ }
276400
+ if (page == 1 && rc == SQLITE_OK)
276401
+ {
276402
+ memcpy(data, SQLITE_FILE_HEADER, 16);
276403
+ }
276404
+ }
276405
+ else
276406
+ {
276407
+ /* Decrypt only */
276408
+ uint8_t nonce[PAGE_NONCE_LEN_ASCON128];
276409
+ uint8_t dummyTag[PAGE_TAG_LEN_ASCON128];
276410
+ memset(dummyTag, 0, PAGE_TAG_LEN_ASCON128);
276411
+ memset(otk, 0, ASCON_HASH_BYTES);
276412
+ sqlite3mcGenerateInitialVector(page, nonce);
276413
+ AsconGenOtk(otk, ascon128Cipher->m_key, nonce, page);
276414
+
276415
+ /* Decrypt */
276416
+ offset = (page == 1) ? CIPHER_PAGE1_OFFSET : 0;
276417
+ tagOk = ascon_aead_decrypt(data + offset, data + offset, clen - offset,
276418
+ NULL /* ad */, 0 /* adlen */,
276419
+ dummyTag, nonce, otk);
276420
+ if (page == 1)
276421
+ {
276422
+ memcpy(data, SQLITE_FILE_HEADER, 16);
276423
+ }
276424
+ }
276425
+
276426
+ return rc;
276427
+ }
276428
+
276429
+ SQLITE_PRIVATE const CipherDescriptor mcAscon128Descriptor =
276430
+ {
276431
+ CIPHER_NAME_ASCON128,
276432
+ AllocateAscon128Cipher,
276433
+ FreeAscon128Cipher,
276434
+ CloneAscon128Cipher,
276435
+ GetLegacyAscon128Cipher,
276436
+ GetPageSizeAscon128Cipher,
276437
+ GetReservedAscon128Cipher,
276438
+ GetSaltAscon128Cipher,
276439
+ GenerateKeyAscon128Cipher,
276440
+ EncryptPageAscon128Cipher,
276441
+ DecryptPageAscon128Cipher
276442
+ };
276443
+ #endif
276444
+ /*** End of #include "cipher_ascon.c" ***/
276445
+
274710
276446
  /* #include "cipher_common.c" */
274711
276447
  /*** Begin of #include "cipher_common.c" ***/
274712
276448
  /*
@@ -276638,7 +278374,7 @@ sqlite3mcBtreeSetPageSize(Btree* p, int pageSize, int nReserve, int iFix)
276638
278374
  ** Change 4: Call sqlite3mcBtreeSetPageSize instead of sqlite3BtreeSetPageSize for main database
276639
278375
  ** (sqlite3mcBtreeSetPageSize allows to reduce the number of reserved bytes)
276640
278376
  **
276641
- ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.44.0 amalgamation.
278377
+ ** This code is generated by the script rekeyvacuum.sh from SQLite version 3.44.1 amalgamation.
276642
278378
  */
276643
278379
  SQLITE_PRIVATE SQLITE_NOINLINE int sqlite3mcRunVacuumForRekey(
276644
278380
  char **pzErrMsg, /* Write error message here */
@@ -277501,6 +279237,33 @@ sqlite3_rekey(sqlite3 *db, const void *zKey, int nKey)
277501
279237
  ** License: MIT
277502
279238
  */
277503
279239
 
279240
+ /* For memset, memset_s */
279241
+ #include <string.h>
279242
+
279243
+ #ifdef _WIN32
279244
+ /* For SecureZeroMemory */
279245
+ #include <windows.h>
279246
+ #include <winbase.h>
279247
+ #endif
279248
+
279249
+ SQLITE_PRIVATE void sqlite3mcSecureZeroMemory(void* v, size_t n)
279250
+ {
279251
+ #ifdef _WIN32
279252
+ SecureZeroMemory(v, n);
279253
+ #elif defined(__DARWIN__) || defined(__STDC_LIB_EXT1__)
279254
+ /* memset_s() is available since OS X 10.9, */
279255
+ /* and may be available on other platforms. */
279256
+ memset_s(v, n, 0, n);
279257
+ #elif defined(__OpenBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 11)
279258
+ /* Non-standard function */
279259
+ explicit_bzero(v, n);
279260
+ #else
279261
+ /* Generic implementation based on volatile pointers */
279262
+ static void* (* const volatile memset_sec)(void*, int, size_t) = &memset;
279263
+ memset_sec(v, 0, n);
279264
+ #endif
279265
+ }
279266
+
277504
279267
  #if SQLITE3MC_SECURE_MEMORY
277505
279268
 
277506
279269
  /* Flag indicating whether securing memory allocations is initialized */
@@ -277571,7 +279334,7 @@ static void mcMemoryFree(void* pPrior)
277571
279334
  mcRandomFill((char*) pPrior, nSize)
277572
279335
  #else
277573
279336
  int nSize = mcMemorySize(pPrior);
277574
- memset(pPrior, 0, nSize);
279337
+ sqlite3mcSecureZeroMemory(pPrior, 0, nSize);
277575
279338
  #endif
277576
279339
  }
277577
279340
  mcDefaultMemoryMethods.xFree(pPrior);
@@ -281689,7 +283452,7 @@ SQLITE_EXTENSION_INIT1
281689
283452
 
281690
283453
  #undef LONGDOUBLE_CONSTANT
281691
283454
  #undef LONGDOUBLE_TYPE
281692
- #if defined(__GNUC__) && defined(_WIN64)
283455
+ #if defined(SQLITE_USE_QUADMATH) && defined(__GNUC__) && defined(_WIN64)
281693
283456
  #include <quadmath.h>
281694
283457
  #define LONGDOUBLE_TYPE __float128
281695
283458
  #define LONGDOUBLE_CONSTANT(x) x##Q
@@ -283214,7 +284977,7 @@ static int vsvtabColumn(
283214
284977
  {
283215
284978
  LONGDOUBLE_TYPE dv, fp, ip;
283216
284979
 
283217
- #if defined(__GNUC__) && defined(_WIN64)
284980
+ #if defined(SQLITE_USE_QUADMATH) && defined(__GNUC__) && defined(_WIN64)
283218
284981
  if (!hasExtended) hasExtended = 1;
283219
284982
  #else
283220
284983
  if (!hasExtended) {
@@ -301391,6 +303154,12 @@ sqlite3mc_initialize(const char* arg)
301391
303154
  rc = sqlite3mcRegisterCipher(&mcRC4Descriptor, mcRC4Params, (CODEC_TYPE_RC4 == CODEC_TYPE));
301392
303155
  }
301393
303156
  #endif
303157
+ #if HAVE_CIPHER_ASCON128
303158
+ if (rc == SQLITE_OK)
303159
+ {
303160
+ rc = sqlite3mcRegisterCipher(&mcAscon128Descriptor, mcAscon128Params, (CODEC_TYPE_ASCON128 == CODEC_TYPE));
303161
+ }
303162
+ #endif
301394
303163
 
301395
303164
  /*
301396
303165
  ** Initialize and register MultiCipher VFS as default VFS