From 69f9d225eba3264306fdecbf44fbb82383e610a2 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Thu, 19 Nov 2015 11:52:21 +0000 Subject: [PATCH] [hash] trim hash algorithm with multiple output size. The output size is now passed by parameter to the finalize function instead of being stored in the context. that simplify quite a bit the passing of this parameter --- Crypto/Hash/SHA512t.hs | 4 ++-- cbits/cryptonite_keccak.c | 5 ++--- cbits/cryptonite_keccak.h | 4 +--- cbits/cryptonite_sha3.c | 3 +-- cbits/cryptonite_sha3.h | 4 +--- cbits/cryptonite_sha512.c | 15 ++++++--------- cbits/cryptonite_sha512.h | 14 +++----------- cbits/cryptonite_skein256.c | 5 ++--- cbits/cryptonite_skein256.h | 3 +-- cbits/cryptonite_skein512.c | 3 +-- cbits/cryptonite_skein512.h | 3 +-- gen/Gen.hs | 6 +++--- 12 files changed, 24 insertions(+), 45 deletions(-) diff --git a/Crypto/Hash/SHA512t.hs b/Crypto/Hash/SHA512t.hs index e9e6ac7..685343f 100644 --- a/Crypto/Hash/SHA512t.hs +++ b/Crypto/Hash/SHA512t.hs @@ -25,7 +25,7 @@ data SHA512t_224 = SHA512t_224 instance HashAlgorithm SHA512t_224 where hashBlockSize _ = 128 hashDigestSize _ = 28 - hashInternalContextSize _ = 264 + hashInternalContextSize _ = 256 hashInternalInit p = c_sha512t_init p 224 hashInternalUpdate = c_sha512t_update hashInternalFinalize p = c_sha512t_finalize p 224 @@ -37,7 +37,7 @@ data SHA512t_256 = SHA512t_256 instance HashAlgorithm SHA512t_256 where hashBlockSize _ = 128 hashDigestSize _ = 32 - hashInternalContextSize _ = 264 + hashInternalContextSize _ = 256 hashInternalInit p = c_sha512t_init p 256 hashInternalUpdate = c_sha512t_update hashInternalFinalize p = c_sha512t_finalize p 256 diff --git a/cbits/cryptonite_keccak.c b/cbits/cryptonite_keccak.c index 5e13551..3a9d0b4 100644 --- a/cbits/cryptonite_keccak.c +++ b/cbits/cryptonite_keccak.c @@ -100,8 +100,7 @@ static inline void keccak_do_chunk(uint64_t state[25], uint64_t buf[], int bufsz void cryptonite_keccak_init(struct keccak_ctx *ctx, uint32_t hashlen) { memset(ctx, 0, sizeof(*ctx)); - ctx->hashlen = hashlen / 8; - ctx->bufsz = 200 - 2 * ctx->hashlen; + ctx->bufsz = 200 - 2 * (hashlen / 8); } void cryptonite_keccak_update(struct keccak_ctx *ctx, uint8_t *data, uint32_t len) @@ -155,5 +154,5 @@ void cryptonite_keccak_finalize(struct keccak_ctx *ctx, uint32_t hashlen, uint8_ /* output */ cpu_to_le64_array(w, ctx->state, 25); - memcpy(out, w, ctx->hashlen); + memcpy(out, w, hashlen / 8); } diff --git a/cbits/cryptonite_keccak.h b/cbits/cryptonite_keccak.h index c83f4b6..466b66d 100644 --- a/cbits/cryptonite_keccak.h +++ b/cbits/cryptonite_keccak.h @@ -28,11 +28,9 @@ struct keccak_ctx { - uint32_t hashlen; /* in bytes */ uint32_t bufindex; - uint64_t state[25]; uint32_t bufsz; - uint32_t _padding; + uint64_t state[25]; uint8_t buf[144]; /* minimum SHA3-224, otherwise buffer need increases */ }; diff --git a/cbits/cryptonite_sha3.c b/cbits/cryptonite_sha3.c index 923150a..319405e 100644 --- a/cbits/cryptonite_sha3.c +++ b/cbits/cryptonite_sha3.c @@ -100,8 +100,7 @@ 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) { memset(ctx, 0, sizeof(*ctx)); - ctx->hashlen = hashlen / 8; - ctx->bufsz = 200 - 2 * ctx->hashlen; + ctx->bufsz = 200 - 2 * (hashlen / 8); } void cryptonite_sha3_update(struct sha3_ctx *ctx, const uint8_t *data, uint32_t len) diff --git a/cbits/cryptonite_sha3.h b/cbits/cryptonite_sha3.h index 1f78674..83aa731 100644 --- a/cbits/cryptonite_sha3.h +++ b/cbits/cryptonite_sha3.h @@ -28,11 +28,9 @@ struct sha3_ctx { - uint32_t hashlen; /* in bytes */ uint32_t bufindex; - uint64_t state[25]; uint32_t bufsz; - uint32_t _padding; + uint64_t state[25]; uint8_t buf[144]; /* minimum SHA3-224, otherwise buffer need increases */ }; diff --git a/cbits/cryptonite_sha512.c b/cbits/cryptonite_sha512.c index 6adf028..75ec791 100644 --- a/cbits/cryptonite_sha512.c +++ b/cbits/cryptonite_sha512.c @@ -196,14 +196,11 @@ void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out) #include -void cryptonite_sha512t_init(struct sha512t_ctx *tctx, uint32_t hashlen) +void cryptonite_sha512t_init(struct sha512_ctx *ctx, uint32_t hashlen) { - struct sha512_ctx *ctx = &tctx->ctx; memset(ctx, 0, sizeof(*ctx)); if (hashlen >= 512) return; - tctx->t = hashlen; - switch (hashlen) { case 224: ctx->h[0] = 0x8c3d37c819544da2ULL; @@ -246,16 +243,16 @@ void cryptonite_sha512t_init(struct sha512t_ctx *tctx, uint32_t hashlen) } } -void cryptonite_sha512t_update(struct sha512t_ctx *ctx, const uint8_t *data, uint32_t len) +void cryptonite_sha512t_update(struct sha512_ctx *ctx, const uint8_t *data, uint32_t len) { - return cryptonite_sha512_update(&ctx->ctx, data, len); + return cryptonite_sha512_update(ctx, data, len); } -void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint32_t hashlen, uint8_t *out) +void cryptonite_sha512t_finalize(struct sha512_ctx *ctx, uint32_t hashlen, uint8_t *out) { uint8_t intermediate[SHA512_DIGEST_SIZE]; - cryptonite_sha512_finalize(&ctx->ctx, intermediate); - memcpy(out, intermediate, ctx->t / 8); + cryptonite_sha512_finalize(ctx, intermediate); + memcpy(out, intermediate, hashlen / 8); } diff --git a/cbits/cryptonite_sha512.h b/cbits/cryptonite_sha512.h index a762584..85801fe 100644 --- a/cbits/cryptonite_sha512.h +++ b/cbits/cryptonite_sha512.h @@ -33,12 +33,6 @@ struct sha512_ctx uint64_t h[8]; }; -struct sha512t_ctx -{ - struct sha512_ctx ctx; - uint64_t t; /* the custom t (e.g. 224 for SHA512/224) */ -}; - #define sha384_ctx sha512_ctx #define SHA384_DIGEST_SIZE 64 @@ -47,8 +41,6 @@ struct sha512t_ctx #define SHA512_DIGEST_SIZE 64 #define SHA512_CTX_SIZE sizeof(struct sha512_ctx) -#define SHA512t_CTX_SIZE sizeof(struct sha512t_ctx) - void cryptonite_sha384_init(struct sha384_ctx *ctx); void cryptonite_sha384_update(struct sha384_ctx *ctx, const uint8_t *data, uint32_t len); void cryptonite_sha384_finalize(struct sha384_ctx *ctx, uint8_t *out); @@ -58,8 +50,8 @@ void cryptonite_sha512_update(struct sha512_ctx *ctx, const uint8_t *data, uint3 void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out); /* only multiples of 8 are supported as valid t values */ -void cryptonite_sha512t_init(struct sha512t_ctx *ctx, uint32_t hashlen); -void cryptonite_sha512t_update(struct sha512t_ctx *ctx, const uint8_t *data, uint32_t len); -void cryptonite_sha512t_finalize(struct sha512t_ctx *ctx, uint32_t hashlen, uint8_t *out); +void cryptonite_sha512t_init(struct sha512_ctx *ctx, uint32_t hashlen); +void cryptonite_sha512t_update(struct sha512_ctx *ctx, const uint8_t *data, uint32_t len); +void cryptonite_sha512t_finalize(struct sha512_ctx *ctx, uint32_t hashlen, uint8_t *out); #endif diff --git a/cbits/cryptonite_skein256.c b/cbits/cryptonite_skein256.c index d851898..404a32a 100644 --- a/cbits/cryptonite_skein256.c +++ b/cbits/cryptonite_skein256.c @@ -108,7 +108,6 @@ void cryptonite_skein256_init(struct skein256_ctx *ctx, uint32_t hashlen) uint64_t buf[4]; memset(ctx, 0, sizeof(*ctx)); - ctx->hashlen = (hashlen + 7) >> 3; SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_CFG)); memset(buf, '\0', sizeof(buf)); @@ -170,8 +169,8 @@ void cryptonite_skein256_finalize(struct skein256_ctx *ctx, uint32_t hashlen, ui memset(ctx->buf, '\0', 32); - /* make sure we have a 8 bit rounded value */ - outsize = ctx->hashlen; + /* make sure we have a 8 bit up rounded value */ + outsize = (hashlen + 7) >> 3; /* backup h[0--4] */ for (j = 0; j < 4; j++) diff --git a/cbits/cryptonite_skein256.h b/cbits/cryptonite_skein256.h index f23b43c..6f8005a 100644 --- a/cbits/cryptonite_skein256.h +++ b/cbits/cryptonite_skein256.h @@ -28,12 +28,11 @@ struct skein256_ctx { - uint32_t hashlen; - uint32_t bufindex; uint8_t buf[32]; uint64_t h[4]; uint64_t t0; uint64_t t1; + uint32_t bufindex; }; #define SKEIN256_CTX_SIZE sizeof(struct skein256_ctx) diff --git a/cbits/cryptonite_skein512.c b/cbits/cryptonite_skein512.c index 8f411ff..867336b 100644 --- a/cbits/cryptonite_skein512.c +++ b/cbits/cryptonite_skein512.c @@ -126,7 +126,6 @@ void cryptonite_skein512_init(struct skein512_ctx *ctx, uint32_t hashlen) uint64_t buf[8]; memset(ctx, 0, sizeof(*ctx)); - ctx->hashlen = (hashlen + 7) >> 3; SET_TYPE(ctx, FLAG_FIRST | FLAG_FINAL | FLAG_TYPE(TYPE_CFG)); memset(buf, '\0', sizeof(buf)); @@ -189,7 +188,7 @@ void cryptonite_skein512_finalize(struct skein512_ctx *ctx, uint32_t hashlen, ui memset(ctx->buf, '\0', 64); /* make sure we have a 8 bit rounded value */ - outsize = ctx->hashlen; + outsize = (hashlen + 7) >> 3; /* backup h[0--7] */ for (j = 0; j < 8; j++) diff --git a/cbits/cryptonite_skein512.h b/cbits/cryptonite_skein512.h index e3f4ab6..d7e4fb5 100644 --- a/cbits/cryptonite_skein512.h +++ b/cbits/cryptonite_skein512.h @@ -28,12 +28,11 @@ struct skein512_ctx { - uint32_t hashlen; /* in bytes, typically 384/8, 512/8 */ - uint32_t bufindex; uint8_t buf[64]; uint64_t h[8]; uint64_t t0; uint64_t t1; + uint32_t bufindex; }; #define SKEIN512_CTX_SIZE sizeof(struct skein512_ctx) diff --git a/gen/Gen.hs b/gen/Gen.hs index 88e1cd3..cc4449f 100644 --- a/gen/Gen.hs +++ b/gen/Gen.hs @@ -65,9 +65,9 @@ hashModules = , GenHashModule "SHA256" "sha256.h" "sha256" 192 (HashSimple 256 64) , GenHashModule "SHA384" "sha512.h" "sha384" 256 (HashSimple 384 128) , GenHashModule "SHA512" "sha512.h" "sha512" 256 (HashSimple 512 128) - , GenHashModule "SHA512t" "sha512.h" "sha512t" 264 (HashMulti [(224,128),(256,128)]) - , GenHashModule "Keccak" "keccak.h" "keccak" 360 (HashMulti [(224,144),(256,136),(384,104),(512,72)]) - , GenHashModule "SHA3" "sha3.h" "sha3" 360 (HashMulti [(224,144),(256,136),(384,104),(512,72)]) + , GenHashModule "SHA512t" "sha512.h" "sha512t" 256 (HashMulti [(224,128),(256,128)]) + , GenHashModule "Keccak" "keccak.h" "keccak" 352 (HashMulti [(224,144),(256,136),(384,104),(512,72)]) + , GenHashModule "SHA3" "sha3.h" "sha3" 352 (HashMulti [(224,144),(256,136),(384,104),(512,72)]) , GenHashModule "RIPEMD160" "ripemd.h" "ripemd160" 128 (HashSimple 160 64) , GenHashModule "Skein256" "skein256.h" "skein256" 96 (HashMulti [(224,32),(256,32)]) , GenHashModule "Skein512" "skein512.h" "skein512" 160 (HashMulti [(224,64),(256,64),(384,64),(512,64)])