From 7286cb832ad48977790ccdb0cee063a5d05d5d75 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 14 Feb 2017 17:51:40 +0000 Subject: [PATCH] Add better constants for trampoline buffer --- cbits/cryptonite_sha3.c | 5 +++-- cbits/cryptonite_sha3.h | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/cbits/cryptonite_sha3.c b/cbits/cryptonite_sha3.c index c9df6fb..e942fe6 100755 --- a/cbits/cryptonite_sha3.c +++ b/cbits/cryptonite_sha3.c @@ -104,7 +104,8 @@ static inline void sha3_do_chunk(uint64_t state[25], uint64_t buf[], int bufsz) */ void cryptonite_sha3_init(struct sha3_ctx *ctx, uint32_t hashlen) { - int bufsz = 200 - 2 * (hashlen / 8); + /* assert(hashlen >= SHA3_BITSIZE_MIN && hashlen <= SHA3_BITSIZE_MAX) */ + int bufsz = SHA3_BUF_SIZE(hashlen); memset(ctx, 0, sizeof(*ctx) + bufsz); ctx->bufsz = bufsz; } @@ -131,7 +132,7 @@ void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t } if (need_alignment(data, 8)) { - uint64_t tramp[200 - 2 * (128 / 8)]; + uint64_t tramp[SHA3_BUF_SIZE_MAX/8]; ASSERT_ALIGNMENT(tramp, 8); for (; len >= ctx->bufsz; len -= ctx->bufsz, data += ctx->bufsz) { memcpy(tramp, data, ctx->bufsz / 8); diff --git a/cbits/cryptonite_sha3.h b/cbits/cryptonite_sha3.h index 233f1e1..ca4e6f9 100755 --- a/cbits/cryptonite_sha3.h +++ b/cbits/cryptonite_sha3.h @@ -35,6 +35,22 @@ struct sha3_ctx }; #define SHA3_CTX_SIZE sizeof(struct sha3_ctx) +#define SHA3_CTX_BUF_MAX_SIZE (SHA3_CTX_SIZE + SHA3_BUF_SIZE_MAX) +#define SHA3_BITSIZE_MIN 128 +#define SHA3_BITSIZE_MAX 512 + +#define SHA3_BUF_SIZE(bitsize) (200 - 2 * ((bitsize) / 8)) + +#define SHA3_BUF_SIZE_MIN SHA3_BUF_SIZE(SHA3_BITSIZE_MAX) +#define SHA3_BUF_SIZE_MAX SHA3_BUF_SIZE(SHA3_BITSIZE_MIN) + +/* + * buffer size: + * + * 128 bits (shake 128 bits) => 200 - 2 * (128 / 8) = 200 - 2*16 = 200 - 32 = 168 bytes + * 224 bits (SHA3 224 bits) => 200 - 2 * (224 / 8) = 200 - 2*28 = 200 - 56 = 144 bytes + * 512 bits (SHA3 512 bits) => 200 - 2 * (512 / 8) = 200 - 2*64 = 200 - 128 = 72 bytes + */ void cryptonite_sha3_init(struct sha3_ctx *ctx, uint32_t hashlen); void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t len);