Add better constants for trampoline buffer

This commit is contained in:
Vincent Hanquez 2017-02-14 17:51:40 +00:00
parent c342d28436
commit 7286cb832a
2 changed files with 19 additions and 2 deletions

View File

@ -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);

View File

@ -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);