From 70ef31ff5dccae6e4ab4ca16da8e48f826170244 Mon Sep 17 00:00:00 2001 From: Stephen Lombardo Date: Tue, 17 Jul 2012 16:45:02 -0400 Subject: [PATCH] add flag for normalizing pgno in HMAC check --- src/crypto.h | 5 +++-- src/crypto_impl.c | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/crypto.h b/src/crypto.h index 03afbfb..4faf1b5 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -57,10 +57,11 @@ #endif /* possible flags for cipher_ctx->flags */ -#define CIPHER_FLAG_HMAC 0x01 +#define CIPHER_FLAG_HMAC 0x01 +#define CIPHER_FLAG_LE_PGNO 0x02 #ifndef DEFAULT_CIPHER_FLAGS -#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC +#define DEFAULT_CIPHER_FLAGS CIPHER_FLAG_HMAC | CIPHER_FLAG_LE_PGNO #endif diff --git a/src/crypto_impl.c b/src/crypto_impl.c index d5c4b9e..77d3fef 100644 --- a/src/crypto_impl.c +++ b/src/crypto_impl.c @@ -512,7 +512,11 @@ int sqlcipher_page_hmac(cipher_ctx *ctx, Pgno pgno, unsigned char *in, int in_sz valid pages out of order in a database */ HMAC_Update(&ctx->hctx, in, in_sz); - HMAC_Update(&ctx->hctx, (const unsigned char*) pgno_le, sizeof(pgno_le)); + if(ctx->flags & CIPHER_FLAG_LE_PGNO) /* default compute hmac using little endian */ + HMAC_Update(&ctx->hctx, (const unsigned char*) pgno_le, sizeof(pgno_le)); + else /* legacy setting - compute using native byte ordering */ + HMAC_Update(&ctx->hctx, (const unsigned char*) &pgno, sizeof(pgno)); + HMAC_Final(&ctx->hctx, out, NULL); HMAC_CTX_cleanup(&ctx->hctx); return SQLITE_OK;