From 92b2cb464abb670c1891ed2dfb6024771dc669f4 Mon Sep 17 00:00:00 2001 From: Michael Hamburg Date: Mon, 8 May 2017 13:22:54 -0700 Subject: [PATCH] sha3 functions can now return an error (likely to be ignored) if output length is to large. Also add shake###_output --- src/GENERATED/include/decaf/shake.h | 17 ++++++++++++----- src/public_include/decaf/shake.h | 17 ++++++++++++----- src/shake.c | 5 +++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/GENERATED/include/decaf/shake.h b/src/GENERATED/include/decaf/shake.h index 7f92dda..881889d 100644 --- a/src/GENERATED/include/decaf/shake.h +++ b/src/GENERATED/include/decaf/shake.h @@ -136,7 +136,7 @@ void decaf_sponge_destroy ( * @param [in] outlen The length of the output data. * @param [in] params The parameters of the sponge hash. */ -void decaf_sponge_hash ( +decaf_error_t decaf_sponge_hash ( const uint8_t *in, size_t inlen, uint8_t *out, @@ -163,6 +163,9 @@ void decaf_sponge_hash ( decaf_sha3_output(sponge->s, out, outlen); \ decaf_sponge_init(sponge->s, &DECAF_SHAKE##n##_params_s); \ } \ + static inline void DECAF_NONNULL decaf_shake##n##_output(decaf_shake##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + decaf_sha3_output(sponge->s, out, outlen); \ + } \ static inline void DECAF_NONNULL decaf_shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHAKE##n##_params_s); \ } \ @@ -182,12 +185,16 @@ void decaf_sponge_hash ( static inline void DECAF_NONNULL decaf_sha3_##n##_update(decaf_sha3_##n##_ctx_t sponge, const uint8_t *in, size_t inlen ) { \ decaf_sha3_update(sponge->s, in, inlen); \ } \ - static inline void DECAF_NONNULL decaf_sha3_##n##_final(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ - decaf_sha3_output(sponge->s, out, outlen); \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_final(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + decaf_error_t ret = decaf_sha3_output(sponge->s, out, outlen); \ decaf_sponge_init(sponge->s, &DECAF_SHA3_##n##_params_s); \ + return ret; \ + } \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_output(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + return decaf_sha3_output(sponge->s, out, outlen); \ } \ - static inline void DECAF_NONNULL decaf_sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ - decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHA3_##n##_params_s); \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ + return decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHA3_##n##_params_s); \ } \ static inline void DECAF_NONNULL decaf_sha3_##n##_destroy(decaf_sha3_##n##_ctx_t sponge) { \ decaf_sponge_destroy(sponge->s); \ diff --git a/src/public_include/decaf/shake.h b/src/public_include/decaf/shake.h index 7f92dda..881889d 100644 --- a/src/public_include/decaf/shake.h +++ b/src/public_include/decaf/shake.h @@ -136,7 +136,7 @@ void decaf_sponge_destroy ( * @param [in] outlen The length of the output data. * @param [in] params The parameters of the sponge hash. */ -void decaf_sponge_hash ( +decaf_error_t decaf_sponge_hash ( const uint8_t *in, size_t inlen, uint8_t *out, @@ -163,6 +163,9 @@ void decaf_sponge_hash ( decaf_sha3_output(sponge->s, out, outlen); \ decaf_sponge_init(sponge->s, &DECAF_SHAKE##n##_params_s); \ } \ + static inline void DECAF_NONNULL decaf_shake##n##_output(decaf_shake##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + decaf_sha3_output(sponge->s, out, outlen); \ + } \ static inline void DECAF_NONNULL decaf_shake##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHAKE##n##_params_s); \ } \ @@ -182,12 +185,16 @@ void decaf_sponge_hash ( static inline void DECAF_NONNULL decaf_sha3_##n##_update(decaf_sha3_##n##_ctx_t sponge, const uint8_t *in, size_t inlen ) { \ decaf_sha3_update(sponge->s, in, inlen); \ } \ - static inline void DECAF_NONNULL decaf_sha3_##n##_final(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ - decaf_sha3_output(sponge->s, out, outlen); \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_final(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + decaf_error_t ret = decaf_sha3_output(sponge->s, out, outlen); \ decaf_sponge_init(sponge->s, &DECAF_SHA3_##n##_params_s); \ + return ret; \ + } \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_output(decaf_sha3_##n##_ctx_t sponge, uint8_t *out, size_t outlen ) { \ + return decaf_sha3_output(sponge->s, out, outlen); \ } \ - static inline void DECAF_NONNULL decaf_sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ - decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHA3_##n##_params_s); \ + static inline decaf_error_t DECAF_NONNULL decaf_sha3_##n##_hash(uint8_t *out, size_t outlen, const uint8_t *in, size_t inlen) { \ + return decaf_sponge_hash(in,inlen,out,outlen,&DECAF_SHA3_##n##_params_s); \ } \ static inline void DECAF_NONNULL decaf_sha3_##n##_destroy(decaf_sha3_##n##_ctx_t sponge) { \ decaf_sponge_destroy(sponge->s); \ diff --git a/src/shake.c b/src/shake.c index dba72dc..75ecf0b 100644 --- a/src/shake.c +++ b/src/shake.c @@ -187,7 +187,7 @@ void decaf_sponge_init ( decaf_sponge->params->position = 0; } -void decaf_sponge_hash ( +decaf_error_t decaf_sponge_hash ( const uint8_t *in, size_t inlen, uint8_t *out, @@ -197,8 +197,9 @@ void decaf_sponge_hash ( decaf_keccak_sponge_t decaf_sponge; decaf_sponge_init(decaf_sponge, params); decaf_sha3_update(decaf_sponge, in, inlen); - decaf_sha3_output(decaf_sponge, out, outlen); + decaf_error_t ret = decaf_sha3_output(decaf_sponge, out, outlen); decaf_sponge_destroy(decaf_sponge); + return ret; } #define DEFSHAKE(n) \