[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
This commit is contained in:
parent
ad285be68c
commit
69f9d225eb
@ -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
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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 */
|
||||
};
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 */
|
||||
};
|
||||
|
||||
|
||||
@ -196,14 +196,11 @@ void cryptonite_sha512_finalize(struct sha512_ctx *ctx, uint8_t *out)
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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++)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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++)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)])
|
||||
|
||||
Loading…
Reference in New Issue
Block a user