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

* sha.h: Added declarations for sha224. Some are aliases for the

corresponding sha256 definition.

* sha256.c (sha256_digest): Use _nettle_write_be32.
(sha224_init): New function.
(sha224_digest): New function.

Rev: nettle/sha.h:1.6
Rev: nettle/sha256.c:1.5
parent af89b978
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,8 @@ extern "C" { ...@@ -36,6 +36,8 @@ extern "C" {
#define sha1_init nettle_sha1_init #define sha1_init nettle_sha1_init
#define sha1_update nettle_sha1_update #define sha1_update nettle_sha1_update
#define sha1_digest nettle_sha1_digest #define sha1_digest nettle_sha1_digest
#define sha224_init nettle_sha224_init
#define sha224_digest nettle_sha224_digest
#define sha256_init nettle_sha256_init #define sha256_init nettle_sha256_init
#define sha256_update nettle_sha256_update #define sha256_update nettle_sha256_update
#define sha256_digest nettle_sha256_digest #define sha256_digest nettle_sha256_digest
...@@ -114,6 +116,24 @@ sha256_digest(struct sha256_ctx *ctx, ...@@ -114,6 +116,24 @@ sha256_digest(struct sha256_ctx *ctx,
void void
_nettle_sha256_compress(uint32_t *state, const uint8_t *data, const uint32_t *k); _nettle_sha256_compress(uint32_t *state, const uint8_t *data, const uint32_t *k);
/* SHA224, a truncated SHA256 with different initial state. */
#define SHA224_DIGEST_SIZE 28
#define SHA224_DATA_SIZE SHA256_DATA_SIZE
#define sha224_ctx sha256_ctx
void
sha224_init(struct sha256_ctx *ctx);
#define sha224_update nettle_sha256_update
void
sha224_digest(struct sha256_ctx *ctx,
unsigned length,
uint8_t *digest);
/* SHA512 */ /* SHA512 */
#define SHA512_DIGEST_SIZE 64 #define SHA512_DIGEST_SIZE 64
...@@ -149,9 +169,8 @@ sha512_digest(struct sha512_ctx *ctx, ...@@ -149,9 +169,8 @@ sha512_digest(struct sha512_ctx *ctx,
void void
_nettle_sha512_compress(uint64_t *state, const uint8_t *data, const uint64_t *k); _nettle_sha512_compress(uint64_t *state, const uint8_t *data, const uint64_t *k);
/* SHA384. */
/* This is the same algorithm as SHA512, but with different initial /* SHA384, a truncated SHA512 with different initial state. */
state and truncated output. */
#define SHA384_DIGEST_SIZE 48 #define SHA384_DIGEST_SIZE 48
#define SHA384_DATA_SIZE SHA512_DATA_SIZE #define SHA384_DATA_SIZE SHA512_DATA_SIZE
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include "sha.h" #include "sha.h"
#include "macros.h" #include "macros.h"
#include "nettle-write.h"
/* Generated by the shadata program. */ /* Generated by the shadata program. */
static const uint32_t static const uint32_t
...@@ -172,42 +173,42 @@ sha256_digest(struct sha256_ctx *ctx, ...@@ -172,42 +173,42 @@ sha256_digest(struct sha256_ctx *ctx,
unsigned length, unsigned length,
uint8_t *digest) uint8_t *digest)
{ {
unsigned i;
unsigned words;
unsigned leftover;
assert(length <= SHA256_DIGEST_SIZE); assert(length <= SHA256_DIGEST_SIZE);
sha256_final(ctx); sha256_final(ctx);
_nettle_write_be32(length, digest, ctx->state);
sha256_init(ctx);
}
words = length / 4; /* sha224 variant. FIXME: Move to seperate file? */
leftover = length % 4;
for (i = 0; i < words; i++, digest += 4)
WRITE_UINT32(digest, ctx->state[i]);
if (leftover) void
sha224_init(struct sha256_ctx *ctx)
{
/* Initial values. I's unclear how they are chosen. */
static const uint32_t H0[_SHA256_DIGEST_LENGTH] =
{ {
uint32_t word; 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
unsigned j = leftover; 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
};
assert(i < _SHA256_DIGEST_LENGTH); memcpy(ctx->state, H0, sizeof(H0));
word = ctx->state[i]; /* Initialize bit count */
ctx->count_low = ctx->count_high = 0;
switch (leftover) /* Initialize buffer */
{ ctx->index = 0;
default:
abort();
case 3:
digest[--j] = (word >> 8) & 0xff;
/* Fall through */
case 2:
digest[--j] = (word >> 16) & 0xff;
/* Fall through */
case 1:
digest[--j] = (word >> 24) & 0xff;
}
} }
sha256_init(ctx);
void
sha224_digest(struct sha256_ctx *ctx,
unsigned length,
uint8_t *digest)
{
assert(length <= SHA224_DIGEST_SIZE);
sha256_final(ctx);
_nettle_write_be32(length, digest, ctx->state);
sha224_init(ctx);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment