summaryrefslogtreecommitdiffstats
path: root/cpukit/libmd/sha256c.c
diff options
context:
space:
mode:
authorConrad Meyer <cem@FreeBSD.org>2018-07-09 07:24:05 +0000
committerSebastian Huber <sebastian.huber@embedded-brains.de>2022-09-08 16:14:56 +0200
commitefd19aa7c15d88ceff9587c5c9d535aecda7963b (patch)
treef353cba478b38f9a1bccac9f88599fb00d8420b1 /cpukit/libmd/sha256c.c
parentFix C++ includability of crypto headers with static array sizes (diff)
downloadrtems-efd19aa7c15d88ceff9587c5c9d535aecda7963b.tar.bz2
Implement SHA2-224 submode of SHA2-256
Like SHA2-384:SHA2-512, SHA2-224 is simply a truncated SHA2-256 with a different initial vector. Add to round out the complete basic SHA2 family.
Diffstat (limited to 'cpukit/libmd/sha256c.c')
-rw-r--r--cpukit/libmd/sha256c.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/cpukit/libmd/sha256c.c b/cpukit/libmd/sha256c.c
index 197de94586..7fd568a2ab 100644
--- a/cpukit/libmd/sha256c.c
+++ b/cpukit/libmd/sha256c.c
@@ -32,6 +32,7 @@ __FBSDID("$FreeBSD$");
#include <string.h>
+#include "sha224.h"
#include "sha256.h"
#if BYTE_ORDER == BIG_ENDIAN
@@ -296,3 +297,53 @@ SHA256_Final(unsigned char digest[static SHA256_DIGEST_LENGTH], SHA256_CTX *ctx)
/* Clear the context state */
memset(ctx, 0, sizeof(*ctx));
}
+
+/*** SHA-224: *********************************************************/
+/*
+ * the SHA224 and SHA256 transforms are identical
+ */
+
+/* SHA-224 initialization. Begins a SHA-224 operation. */
+void
+SHA224_Init(SHA224_CTX * ctx)
+{
+
+ /* Zero bits processed so far */
+ ctx->count = 0;
+
+ /* Magic initialization constants */
+ ctx->state[0] = 0xC1059ED8;
+ ctx->state[1] = 0x367CD507;
+ ctx->state[2] = 0x3070DD17;
+ ctx->state[3] = 0xF70E5939;
+ ctx->state[4] = 0xFFC00B31;
+ ctx->state[5] = 0x68581511;
+ ctx->state[6] = 0x64f98FA7;
+ ctx->state[7] = 0xBEFA4FA4;
+}
+
+/* Add bytes into the SHA-224 hash */
+void
+SHA224_Update(SHA224_CTX * ctx, const void *in, size_t len)
+{
+
+ SHA256_Update((SHA256_CTX *)ctx, in, len);
+}
+
+/*
+ * SHA-224 finalization. Pads the input data, exports the hash value,
+ * and clears the context state.
+ */
+void
+SHA224_Final(unsigned char digest[static SHA224_DIGEST_LENGTH], SHA224_CTX *ctx)
+{
+
+ /* Add padding */
+ SHA256_Pad((SHA256_CTX *)ctx);
+
+ /* Write the hash */
+ be32enc_vect(digest, ctx->state, SHA224_DIGEST_LENGTH);
+
+ /* Clear the context state */
+ explicit_bzero(ctx, sizeof(*ctx));
+}