diff --git a/src/sqlite3.c b/src/sqlite3.c index 98c26cfb886f797e5ad452e400f3657a4c50f5ee..d37a8edfbd0bfa869909df69eebe10ab2f445b32 100644 --- a/src/sqlite3.c +++ b/src/sqlite3.c @@ -238192,6 +238192,7 @@ typedef struct{ int reserveSize; int hmacAlgo; int rekeyHmacAlgo; + int attachHmacAlog; }CodecConstant; typedef struct{ @@ -238292,8 +238293,8 @@ CODEC_STATIC int opensslGetInitVectorSize(void *cipher){ return EVP_CIPHER_iv_length((EVP_CIPHER *)cipher); } -#define CIPHER_HMAC_ALGORITHM_SHA1 0 -#define CIPHER_HMAC_ALGORITHM_SHA256 1 +#define CIPHER_HMAC_ALGORITHM_SHA1 1 +#define CIPHER_HMAC_ALGORITHM_SHA256 2 #define DEFAULT_HMAC_ALGORITHM CIPHER_HMAC_ALGORITHM_SHA1 @@ -238767,6 +238768,11 @@ CODEC_STATIC int sqlite3CodecSetHmacAlgorithm(KeyContext *keyCtx, int hmacAlgo){ return SQLITE_OK; } +CODEC_STATIC int sqlite3CodecSetAttachedHmacAlgorithm(KeyContext *keyCtx, int hmacAlgo){ + keyCtx->codecConst.attachedHmacAlgo = hmacAlgo; + return SQLITE_OK; +} + // You should clear output before you call this function CODEC_STATIC int sqlite3CodecCopyKeyContext(KeyContext *input, KeyContext *output){ errno_t rc = memcpy_s(output, sizeof(KeyContext), input, KEY_CONTEXT_HEAD_SIZE); @@ -238790,7 +238796,7 @@ CODEC_STATIC int sqlite3CodecCopyKeyContext(KeyContext *input, KeyContext *outpu // You should clear key context before you call this function #ifdef SQLITE_CODEC_ATTACH_CHANGED -CODEC_STATIC int sqlite3CodecInitKeyContext(KeyContext *keyCtx, const void *zKey, int nKey, int attachFlag){ +CODEC_STATIC int sqlite3CodecInitKeyContext(KeyContext *keyCtx, const void *zKey, int nKey, int attachFlag, int hmacAlgo){ #else CODEC_STATIC int sqlite3CodecInitKeyContext(KeyContext *keyCtx, const void *zKey, int nKey){ #endif @@ -238799,15 +238805,19 @@ CODEC_STATIC int sqlite3CodecInitKeyContext(KeyContext *keyCtx, const void *zKey if( attachFlag!=0 ){ rc = sqlite3CodecSetCodecConstant(keyCtx, sqlite3CodecGetDefaultAttachCipher()); rc += sqlite3CodecSetIter(keyCtx, sqlite3CodecGetDefaultAttachKdfIter()); + if( hmacAlgo!=0 ){ + rc += sqlite3CodecSetHmacAlgorithm(keyCtx, hmacAlgo); + } }else{ rc = sqlite3CodecSetCodecConstant(keyCtx, DEFAULT_CIPHER); rc += sqlite3CodecSetIter(keyCtx, DEFAULT_ITER); + rc += sqlite3CodecSetHmacAlgorithm(keyCtx, DEFAULT_HMAC_ALGORITHM); } #else rc = sqlite3CodecSetCodecConstant(keyCtx, DEFAULT_CIPHER); rc += sqlite3CodecSetIter(keyCtx, DEFAULT_ITER); -#endif rc += sqlite3CodecSetHmacAlgorithm(keyCtx, DEFAULT_HMAC_ALGORITHM); +#endif keyCtx->codecConst.rekeyHmacAlgo = DEFAULT_HMAC_ALGORITHM; rc += sqlite3CodecSetPassword(keyCtx, zKey, nKey); if(rc != SQLITE_OK){ @@ -238838,7 +238848,8 @@ CODEC_STATIC void sqlite3CodecFreeContext(CodecContext *ctx){ return; } #ifdef SQLITE_CODEC_ATTACH_CHANGED -CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey, int attachFlag){ +CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey, int nDb){ + int attachFlag = (nDb > 1) ? 1 : 0; #else CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void *zKey, int nKey){ #endif @@ -238860,7 +238871,8 @@ CODEC_STATIC int sqlite3CodecInitContext(CodecContext *ctx, Btree *p, const void return SQLITE_ERROR; } #ifdef SQLITE_CODEC_ATTACH_CHANGED - int rc = sqlite3CodecInitKeyContext(ctx->readCtx, zKey, nKey, attachFlag); + int attachHmacAlog = (CodecContext *)sqlite3PagerGetCodec(sqlite3BtreePager(p->db.aDb[0].pBt))->readCtx->attachHmacAlog; + int rc = sqlite3CodecInitKeyContext(ctx->readCtx, zKey, nKey, attachFlag, attachHmacAlog); #else int rc = sqlite3CodecInitKeyContext(ctx->readCtx, zKey, nKey); #endif @@ -239155,8 +239167,7 @@ int sqlite3CodecAttach(sqlite3* db, int nDb, const void *pKey, int nKey){ } sqlite3_mutex_enter(db->mutex); #ifdef SQLITE_CODEC_ATTACH_CHANGED - int attachFlag = (nDb > 1) ? 1 : 0; - int rc = sqlite3CodecInitContext(ctx, p, pKey, nKey, attachFlag); + int rc = sqlite3CodecInitContext(ctx, p, pKey, nKey, nDb); #else int rc = sqlite3CodecInitContext(ctx, p, pKey, nKey); #endif @@ -239356,6 +239367,25 @@ int sqlite3CodecPragma(sqlite3 *db, int iDb, Parse *parse, const char *zLeft, co sqlite3CodecReturnPragmaResult(parse, "codec_hmac_algo", CIPHER_HMAC_ALGORITHM_NAME_SHA256); } } + }else if(sqlite3StrICmp(zLeft, "attach_codec_hmac_algo") == 0){ + if(zRight){ + sqlite3_mutex_enter(db->mutex); + if(sqlite3_stricmp(zRight, CIPHER_HMAC_ALGORITHM_NAME_SHA1) == 0){ + (void)sqlite3CodecSetAttachedHmacAlgorithm(ctx->readCtx, CIPHER_HMAC_ALGORITHM_SHA1); + }else if(sqlite3_stricmp(zRight, CIPHER_HMAC_ALGORITHM_NAME_SHA256) == 0){ + (void)sqlite3CodecSetAttachedHmacAlgorithm(ctx->readCtx, CIPHER_HMAC_ALGORITHM_SHA256); + }else{ + sqlite3_mutex_leave(db->mutex); + return 0; + } + sqlite3_mutex_leave(db->mutex); + }else{ + if(ctx->writeCtx->codecConst.attachedHmacAlgo == CIPHER_HMAC_ALGORITHM_SHA1){ + sqlite3CodecReturnPragmaResult(parse, "attach_codec_hmac_algo", CIPHER_HMAC_ALGORITHM_NAME_SHA1); + }else if(ctx->writeCtx->codecConst.attachedHmacAlgo == CIPHER_HMAC_ALGORITHM_SHA256){ + sqlite3CodecReturnPragmaResult(parse, "attach_codec_hmac_algo", CIPHER_HMAC_ALGORITHM_NAME_SHA256); + } + } }else if(sqlite3StrICmp(zLeft, "codec_rekey_hmac_algo") == 0){ if(zRight){ sqlite3_mutex_enter(db->mutex);