Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

optimise all left cryptohashes the same way as the previous commit.

all hashes here has a uint64 size, instead of the 2 uint32_t seen in reference
implementation. instead of appending the size in a 2 steps process, optimise it
into one single uint64 shift and swap.
  • Loading branch information...
commit 2bfd98529791072308274a56b19aac4d22f4f508 1 parent cb8d3de
@vincenthz authored
Showing with 12 additions and 16 deletions.
  1. +3 −4 cbits/md4.c
  2. +3 −4 cbits/md5.c
  3. +3 −4 cbits/sha1.c
  4. +3 −4 cbits/sha256.c
View
7 cbits/md4.c
@@ -142,12 +142,11 @@ void md4_update(struct md4_ctx *ctx, uint8_t *data, uint32_t len)
void md4_finalize(struct md4_ctx *ctx, uint8_t *out)
{
static uint8_t padding[64] = { 0x80, };
- uint32_t bits[2];
+ uint64_t bits;
uint32_t index, padlen;
/* add padding and update data with it */
- bits[0] = cpu_to_le32((uint32_t) (ctx->sz << 3));
- bits[1] = cpu_to_le32((uint32_t) (ctx->sz >> 29));
+ bits = cpu_to_le64(ctx->sz << 3);
/* pad out to 56 */
index = (uint32_t) (ctx->sz & 0x3f);
@@ -155,7 +154,7 @@ void md4_finalize(struct md4_ctx *ctx, uint8_t *out)
md4_update(ctx, padding, padlen);
/* append length */
- md4_update(ctx, (uint8_t *) bits, sizeof(bits));
+ md4_update(ctx, (uint8_t *) &bits, sizeof(bits));
/* output hash */
le32_to_cpu_array((uint32_t *) out, ctx->h, 4);
View
7 cbits/md5.c
@@ -155,13 +155,12 @@ void md5_update(struct md5_ctx *ctx, uint8_t *data, uint32_t len)
void md5_finalize(struct md5_ctx *ctx, uint8_t *out)
{
static uint8_t padding[64] = { 0x80, };
- uint32_t bits[2];
+ uint64_t bits;
uint32_t index, padlen;
uint32_t *p = (uint32_t *) out;
/* add padding and update data with it */
- bits[0] = cpu_to_le32((uint32_t) (ctx->sz << 3));
- bits[1] = cpu_to_le32((uint32_t) (ctx->sz >> 29));
+ bits = cpu_to_le64(ctx->sz << 3);
/* pad out to 56 */
index = (uint32_t) (ctx->sz & 0x3f);
@@ -169,7 +168,7 @@ void md5_finalize(struct md5_ctx *ctx, uint8_t *out)
md5_update(ctx, padding, padlen);
/* append length */
- md5_update(ctx, (uint8_t *) bits, sizeof(bits));
+ md5_update(ctx, (uint8_t *) &bits, sizeof(bits));
/* output hash */
p[0] = cpu_to_le32(ctx->h[0]);
View
7 cbits/sha1.c
@@ -199,13 +199,12 @@ void sha1_update(struct sha1_ctx *ctx, uint8_t *data, uint32_t len)
void sha1_finalize(struct sha1_ctx *ctx, uint8_t *out)
{
static uint8_t padding[64] = { 0x80, };
- uint32_t bits[2];
+ uint64_t bits;
uint32_t index, padlen;
uint32_t *p = (uint32_t *) out;
/* add padding and update data with it */
- bits[0] = cpu_to_be32((uint32_t) (ctx->sz >> 29));
- bits[1] = cpu_to_be32((uint32_t) (ctx->sz << 3));
+ bits = cpu_to_be64(ctx->sz << 3);
/* pad out to 56 */
index = (uint32_t) (ctx->sz & 0x3f);
@@ -213,7 +212,7 @@ void sha1_finalize(struct sha1_ctx *ctx, uint8_t *out)
sha1_update(ctx, padding, padlen);
/* append length */
- sha1_update(ctx, (uint8_t *) bits, sizeof(bits));
+ sha1_update(ctx, (uint8_t *) &bits, sizeof(bits));
/* output hash */
p[0] = cpu_to_be32(ctx->h[0]);
View
7 cbits/sha256.c
@@ -154,13 +154,12 @@ void sha224_finalize(struct sha224_ctx *ctx, uint8_t *out)
void sha256_finalize(struct sha256_ctx *ctx, uint8_t *out)
{
static uint8_t padding[64] = { 0x80, };
- uint32_t bits[2];
+ uint64_t bits;
uint32_t i, index, padlen;
uint32_t *p = (uint32_t *) out;
/* cpu -> big endian */
- bits[0] = cpu_to_be32((uint32_t) (ctx->sz >> 29));
- bits[1] = cpu_to_be32((uint32_t) (ctx->sz << 3));
+ bits = cpu_to_be64(ctx->sz << 3);
/* pad out to 56 */
index = (uint32_t) (ctx->sz & 0x3f);
@@ -168,7 +167,7 @@ void sha256_finalize(struct sha256_ctx *ctx, uint8_t *out)
sha256_update(ctx, padding, padlen);
/* append length */
- sha256_update(ctx, (uint8_t *) bits, sizeof(bits));
+ sha256_update(ctx, (uint8_t *) &bits, sizeof(bits));
/* store to digest */
for (i = 0; i < 8; i++)
Please sign in to comment.
Something went wrong with that request. Please try again.