Skip to content
Snippets Groups Projects
Commit 057bf08c authored by Niels Möller's avatar Niels Möller
Browse files

* sha256.c (sha256_update): Updated MD_UPDATE call for new

conventions.
(sha256_write_digest): Use MD_PAD rather than MD_FINAL, and insert
the length manually.
* sha512.c: Analogous changes.

Rev: nettle/sha256.c:1.7
Rev: nettle/sha512.c:1.6
parent 464d3f49
No related branches found
No related tags found
No related merge requests found
...@@ -62,7 +62,7 @@ K[64] = ...@@ -62,7 +62,7 @@ K[64] =
0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL, 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL,
}; };
#define COMPRESS(digest, data) (_nettle_sha256_compress((digest), (data), K)) #define COMPRESS(ctx, data) (_nettle_sha256_compress((ctx)->state, (data), K))
/* Initialize the SHA values */ /* Initialize the SHA values */
...@@ -89,7 +89,7 @@ void ...@@ -89,7 +89,7 @@ void
sha256_update(struct sha256_ctx *ctx, sha256_update(struct sha256_ctx *ctx,
unsigned length, const uint8_t *data) unsigned length, const uint8_t *data)
{ {
MD_UPDATE (ctx, length, data, COMPRESS); MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx));
} }
static void static void
...@@ -97,9 +97,23 @@ sha256_write_digest(struct sha256_ctx *ctx, ...@@ -97,9 +97,23 @@ sha256_write_digest(struct sha256_ctx *ctx,
unsigned length, unsigned length,
uint8_t *digest) uint8_t *digest)
{ {
uint32_t high, low;
assert(length <= SHA256_DIGEST_SIZE); assert(length <= SHA256_DIGEST_SIZE);
MD_FINAL(ctx, 32, 9, COMPRESS, WRITE_UINT32); MD_PAD(ctx, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */
high = (ctx->count_high << 9) | (ctx->count_low >> 23);
low = (ctx->count_low << 9) | (ctx->index << 3);
/* This is slightly inefficient, as the numbers are converted to
big-endian format, and will be converted back by the compression
function. It's probably not worth the effort to fix this. */
WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 8), high);
WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 4), low);
COMPRESS(ctx, ctx->block);
_nettle_write_be32(length, digest, ctx->state); _nettle_write_be32(length, digest, ctx->state);
} }
......
...@@ -104,7 +104,7 @@ K[80] = ...@@ -104,7 +104,7 @@ K[80] =
0x5FCB6FAB3AD6FAECULL,0x6C44198C4A475817ULL, 0x5FCB6FAB3AD6FAECULL,0x6C44198C4A475817ULL,
}; };
#define COMPRESS(digest, data) (_nettle_sha512_compress((digest), (data), K)) #define COMPRESS(ctx, data) (_nettle_sha512_compress((ctx)->state, (data), K))
void void
sha512_init(struct sha512_ctx *ctx) sha512_init(struct sha512_ctx *ctx)
...@@ -139,7 +139,7 @@ void ...@@ -139,7 +139,7 @@ void
sha512_update(struct sha512_ctx *ctx, sha512_update(struct sha512_ctx *ctx,
unsigned length, const uint8_t *data) unsigned length, const uint8_t *data)
{ {
MD_UPDATE (ctx, length, data, COMPRESS); MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx));
} }
static void static void
...@@ -147,15 +147,27 @@ sha512_write_digest(struct sha512_ctx *ctx, ...@@ -147,15 +147,27 @@ sha512_write_digest(struct sha512_ctx *ctx,
unsigned length, unsigned length,
uint8_t *digest) uint8_t *digest)
{ {
uint64_t high, low;
unsigned i; unsigned i;
unsigned words; unsigned words;
unsigned leftover; unsigned leftover;
assert(length <= SHA512_DIGEST_SIZE); assert(length <= SHA512_DIGEST_SIZE);
/* There are 1024 = 2^10 bits in one block */ MD_PAD(ctx, 16, COMPRESS);
MD_FINAL(ctx, 64, 10, COMPRESS, WRITE_UINT64);
/* There are 1024 = 2^10 bits in one block */
high = (ctx->count_high << 10) | (ctx->count_low >> 54);
low = (ctx->count_low << 10) | (ctx->index << 3);
/* This is slightly inefficient, as the numbers are converted to
big-endian format, and will be converted back by the compression
function. It's probably not worth the effort to fix this. */
WRITE_UINT64(ctx->block + (SHA512_DATA_SIZE - 16), high);
WRITE_UINT64(ctx->block + (SHA512_DATA_SIZE - 8), low);
COMPRESS(ctx, ctx->block);
words = length / 8; words = length / 8;
leftover = length % 8; leftover = length % 8;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment