Blame SOURCES/0045-semodule-libsemanage-move-module-hashing-into-libsem.patch

e0b08f
From 066007029b3dd250305d7fac0bfd53aa1e4543cf Mon Sep 17 00:00:00 2001
e0b08f
From: Ondrej Mosnacek <omosnace@redhat.com>
e0b08f
Date: Thu, 3 Feb 2022 17:53:23 +0100
e0b08f
Subject: [PATCH] semodule,libsemanage: move module hashing into libsemanage
e0b08f
e0b08f
The main goal of this move is to have the SHA-256 implementation under
e0b08f
libsemanage, since upcoming patches will make use of SHA-256 for a
e0b08f
different (but similar) purpose in libsemanage. Having the hashing code
e0b08f
in libsemanage will reduce code duplication and allow for easier hash
e0b08f
algorithm upgrade in the future.
e0b08f
e0b08f
Note that libselinux currently also contains a hash function
e0b08f
implementation (for yet another different purpose). This patch doesn't
e0b08f
make any effort to address that duplicity yet.
e0b08f
e0b08f
This patch also changes the format of the hash string printed by
e0b08f
semodule to include the name of the hash. The intent is to avoid
e0b08f
ambiguity and potential collisions when the algorithm is potentially
e0b08f
changed in the future.
e0b08f
e0b08f
Signed-off-by: Ondrej Mosnacek <omosnace@redhat.com>
e0b08f
---
e0b08f
 policycoreutils/semodule/Makefile   |   2 +-
e0b08f
 policycoreutils/semodule/semodule.c |  53 ++---
e0b08f
 policycoreutils/semodule/sha256.c   | 294 ----------------------------
e0b08f
 policycoreutils/semodule/sha256.h   |  89 ---------
e0b08f
 4 files changed, 17 insertions(+), 421 deletions(-)
e0b08f
 delete mode 100644 policycoreutils/semodule/sha256.c
e0b08f
 delete mode 100644 policycoreutils/semodule/sha256.h
e0b08f
e0b08f
diff --git a/policycoreutils/semodule/Makefile b/policycoreutils/semodule/Makefile
e0b08f
index 9875ac38..73801e48 100644
e0b08f
--- a/policycoreutils/semodule/Makefile
e0b08f
+++ b/policycoreutils/semodule/Makefile
e0b08f
@@ -6,7 +6,7 @@ MANDIR = $(PREFIX)/share/man
e0b08f
 
e0b08f
 CFLAGS ?= -Werror -Wall -W
e0b08f
 override LDLIBS += -lsepol -lselinux -lsemanage
e0b08f
-SEMODULE_OBJS = semodule.o sha256.o
e0b08f
+SEMODULE_OBJS = semodule.o
e0b08f
 
e0b08f
 all: semodule genhomedircon
e0b08f
 
e0b08f
diff --git a/policycoreutils/semodule/semodule.c b/policycoreutils/semodule/semodule.c
e0b08f
index dc227058..243b1add 100644
e0b08f
--- a/policycoreutils/semodule/semodule.c
e0b08f
+++ b/policycoreutils/semodule/semodule.c
e0b08f
@@ -24,8 +24,6 @@
e0b08f
 
e0b08f
 #include <semanage/modules.h>
e0b08f
 
e0b08f
-#include "sha256.h"
e0b08f
-
e0b08f
 enum client_modes {
e0b08f
 	NO_MODE, INSTALL_M, REMOVE_M, EXTRACT_M, CIL_M, HLL_M,
e0b08f
 	LIST_M, RELOAD, PRIORITY_M, ENABLE_M, DISABLE_M
e0b08f
@@ -347,60 +345,38 @@ static void parse_command_line(int argc, char **argv)
e0b08f
 
e0b08f
 /* Get module checksum */
e0b08f
 static char *hash_module_data(const char *module_name, const int prio) {
e0b08f
-	semanage_module_info_t *extract_info = NULL;
e0b08f
 	semanage_module_key_t *modkey = NULL;
e0b08f
-	Sha256Context context;
e0b08f
-	uint8_t sha256_hash[SHA256_HASH_SIZE];
e0b08f
-	char *sha256_buf = NULL;
e0b08f
-	void *data;
e0b08f
-	size_t data_len = 0, i;
e0b08f
+	char *hash_str = NULL;
e0b08f
+	void *hash = NULL;
e0b08f
+	size_t hash_len = 0;
e0b08f
 	int result;
e0b08f
 
e0b08f
 	result = semanage_module_key_create(sh, &modkey);
e0b08f
 	if (result != 0) {
e0b08f
-		goto cleanup_extract;
e0b08f
+		goto cleanup;
e0b08f
 	}
e0b08f
 
e0b08f
 	result = semanage_module_key_set_name(sh, modkey, module_name);
e0b08f
 	if (result != 0) {
e0b08f
-		goto cleanup_extract;
e0b08f
+		goto cleanup;
e0b08f
 	}
e0b08f
 
e0b08f
 	result = semanage_module_key_set_priority(sh, modkey, prio);
e0b08f
 	if (result != 0) {
e0b08f
-		goto cleanup_extract;
e0b08f
+		goto cleanup;
e0b08f
 	}
e0b08f
 
e0b08f
-	result = semanage_module_extract(sh, modkey, 1, &data, &data_len,
e0b08f
-									 &extract_info);
e0b08f
+	result = semanage_module_compute_checksum(sh, modkey, 1, &hash_str,
e0b08f
+						  &hash_len);
e0b08f
 	if (result != 0) {
e0b08f
-		goto cleanup_extract;
e0b08f
-	}
e0b08f
-
e0b08f
-	Sha256Initialise(&context);
e0b08f
-	Sha256Update(&context, data, data_len);
e0b08f
-
e0b08f
-	Sha256Finalise(&context, (SHA256_HASH *)sha256_hash);
e0b08f
-
e0b08f
-	sha256_buf = calloc(1, SHA256_HASH_SIZE * 2 + 1);
e0b08f
-
e0b08f
-	if (sha256_buf == NULL)
e0b08f
-		goto cleanup_extract;
e0b08f
-
e0b08f
-	for (i = 0; i < SHA256_HASH_SIZE; i++) {
e0b08f
-		sprintf((&sha256_buf[i * 2]), "%02x", sha256_hash[i]);
e0b08f
+		goto cleanup;
e0b08f
 	}
e0b08f
-	sha256_buf[i * 2] = 0;
e0b08f
 
e0b08f
-cleanup_extract:
e0b08f
-	if (data_len > 0) {
e0b08f
-		munmap(data, data_len);
e0b08f
-	}
e0b08f
-	semanage_module_info_destroy(sh, extract_info);
e0b08f
-	free(extract_info);
e0b08f
+cleanup:
e0b08f
+	free(hash);
e0b08f
 	semanage_module_key_destroy(sh, modkey);
e0b08f
 	free(modkey);
e0b08f
-	return sha256_buf;
e0b08f
+	return hash_str;
e0b08f
 }
e0b08f
 
e0b08f
 int main(int argc, char *argv[])
e0b08f
@@ -667,7 +643,10 @@ cleanup_extract:
e0b08f
 					/* fixed width columns */
e0b08f
 					column[0] = sizeof("000") - 1;
e0b08f
 					column[3] = sizeof("disabled") - 1;
e0b08f
-					column[4] = 64; /* SHA256_HASH_SIZE * 2 */
e0b08f
+
e0b08f
+					result = semanage_module_compute_checksum(sh, NULL, 0, NULL,
e0b08f
+										  &column[4]);
e0b08f
+					if (result != 0) goto cleanup_list;
e0b08f
 
e0b08f
 					/* variable width columns */
e0b08f
 					const char *tmp = NULL;
e0b08f
diff --git a/policycoreutils/semodule/sha256.c b/policycoreutils/semodule/sha256.c
e0b08f
deleted file mode 100644
e0b08f
index fe2aeef0..00000000
e0b08f
--- a/policycoreutils/semodule/sha256.c
e0b08f
+++ /dev/null
e0b08f
@@ -1,294 +0,0 @@
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  WjCryptLib_Sha256
e0b08f
-//
e0b08f
-//  Implementation of SHA256 hash function.
e0b08f
-//  Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
e0b08f
-//  Modified by WaterJuice retaining Public Domain license.
e0b08f
-//
e0b08f
-//  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  IMPORTS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-#include "sha256.h"
e0b08f
-#include <memory.h>
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  MACROS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-#define ror(value, bits) (((value) >> (bits)) | ((value) << (32 - (bits))))
e0b08f
-
e0b08f
-#define MIN(x, y) ( ((x)<(y))?(x):(y) )
e0b08f
-
e0b08f
-#define STORE32H(x, y)                                                                     \
e0b08f
-     { (y)[0] = (uint8_t)(((x)>>24)&255); (y)[1] = (uint8_t)(((x)>>16)&255);   \
e0b08f
-       (y)[2] = (uint8_t)(((x)>>8)&255); (y)[3] = (uint8_t)((x)&255); }
e0b08f
-
e0b08f
-#define LOAD32H(x, y)                            \
e0b08f
-     { x = ((uint32_t)((y)[0] & 255)<<24) | \
e0b08f
-           ((uint32_t)((y)[1] & 255)<<16) | \
e0b08f
-           ((uint32_t)((y)[2] & 255)<<8)  | \
e0b08f
-           ((uint32_t)((y)[3] & 255)); }
e0b08f
-
e0b08f
-#define STORE64H(x, y)                                                                     \
e0b08f
-   { (y)[0] = (uint8_t)(((x)>>56)&255); (y)[1] = (uint8_t)(((x)>>48)&255);     \
e0b08f
-     (y)[2] = (uint8_t)(((x)>>40)&255); (y)[3] = (uint8_t)(((x)>>32)&255);     \
e0b08f
-     (y)[4] = (uint8_t)(((x)>>24)&255); (y)[5] = (uint8_t)(((x)>>16)&255);     \
e0b08f
-     (y)[6] = (uint8_t)(((x)>>8)&255); (y)[7] = (uint8_t)((x)&255); }
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  CONSTANTS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-// The K array
e0b08f
-static const uint32_t K[64] = {
e0b08f
-    0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
e0b08f
-    0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
e0b08f
-    0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
e0b08f
-    0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
e0b08f
-    0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
e0b08f
-    0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
e0b08f
-    0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
e0b08f
-    0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
e0b08f
-    0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
e0b08f
-    0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
e0b08f
-    0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
e0b08f
-    0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
e0b08f
-    0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
e0b08f
-};
e0b08f
-
e0b08f
-#define BLOCK_SIZE          64
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  INTERNAL FUNCTIONS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-// Various logical functions
e0b08f
-#define Ch( x, y, z )     (z ^ (x & (y ^ z)))
e0b08f
-#define Maj( x, y, z )    (((x | y) & z) | (x & y))
e0b08f
-#define S( x, n )         ror((x),(n))
e0b08f
-#define R( x, n )         (((x)&0xFFFFFFFFUL)>>(n))
e0b08f
-#define Sigma0( x )       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
e0b08f
-#define Sigma1( x )       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
e0b08f
-#define Gamma0( x )       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
e0b08f
-#define Gamma1( x )       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
e0b08f
-
e0b08f
-#define Sha256Round( a, b, c, d, e, f, g, h, i )       \
e0b08f
-     t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];   \
e0b08f
-     t1 = Sigma0(a) + Maj(a, b, c);                    \
e0b08f
-     d += t0;                                          \
e0b08f
-     h  = t0 + t1;
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  TransformFunction
e0b08f
-//
e0b08f
-//  Compress 512-bits
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-static
e0b08f
-void
e0b08f
-    TransformFunction
e0b08f
-    (
e0b08f
-        Sha256Context*      Context,
e0b08f
-        uint8_t const*      Buffer
e0b08f
-    )
e0b08f
-{
e0b08f
-    uint32_t    S[8];
e0b08f
-    uint32_t    W[64];
e0b08f
-    uint32_t    t0;
e0b08f
-    uint32_t    t1;
e0b08f
-    uint32_t    t;
e0b08f
-    int         i;
e0b08f
-
e0b08f
-    // Copy state into S
e0b08f
-    for( i=0; i<8; i++ )
e0b08f
-    {
e0b08f
-        S[i] = Context->state[i];
e0b08f
-    }
e0b08f
-
e0b08f
-    // Copy the state into 512-bits into W[0..15]
e0b08f
-    for( i=0; i<16; i++ )
e0b08f
-    {
e0b08f
-        LOAD32H( W[i], Buffer + (4*i) );
e0b08f
-    }
e0b08f
-
e0b08f
-    // Fill W[16..63]
e0b08f
-    for( i=16; i<64; i++ )
e0b08f
-    {
e0b08f
-        W[i] = Gamma1( W[i-2]) + W[i-7] + Gamma0( W[i-15] ) + W[i-16];
e0b08f
-    }
e0b08f
-
e0b08f
-    // Compress
e0b08f
-    for( i=0; i<64; i++ )
e0b08f
-    {
e0b08f
-        Sha256Round( S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i );
e0b08f
-        t = S[7];
e0b08f
-        S[7] = S[6];
e0b08f
-        S[6] = S[5];
e0b08f
-        S[5] = S[4];
e0b08f
-        S[4] = S[3];
e0b08f
-        S[3] = S[2];
e0b08f
-        S[2] = S[1];
e0b08f
-        S[1] = S[0];
e0b08f
-        S[0] = t;
e0b08f
-    }
e0b08f
-
e0b08f
-    // Feedback
e0b08f
-    for( i=0; i<8; i++ )
e0b08f
-    {
e0b08f
-        Context->state[i] = Context->state[i] + S[i];
e0b08f
-    }
e0b08f
-}
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  PUBLIC FUNCTIONS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Initialise
e0b08f
-//
e0b08f
-//  Initialises a SHA256 Context. Use this to initialise/reset a context.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Initialise
e0b08f
-    (
e0b08f
-        Sha256Context*      Context         // [out]
e0b08f
-    )
e0b08f
-{
e0b08f
-    Context->curlen = 0;
e0b08f
-    Context->length = 0;
e0b08f
-    Context->state[0] = 0x6A09E667UL;
e0b08f
-    Context->state[1] = 0xBB67AE85UL;
e0b08f
-    Context->state[2] = 0x3C6EF372UL;
e0b08f
-    Context->state[3] = 0xA54FF53AUL;
e0b08f
-    Context->state[4] = 0x510E527FUL;
e0b08f
-    Context->state[5] = 0x9B05688CUL;
e0b08f
-    Context->state[6] = 0x1F83D9ABUL;
e0b08f
-    Context->state[7] = 0x5BE0CD19UL;
e0b08f
-}
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Update
e0b08f
-//
e0b08f
-//  Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
e0b08f
-//  calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Update
e0b08f
-    (
e0b08f
-        Sha256Context*      Context,        // [in out]
e0b08f
-        void const*         Buffer,         // [in]
e0b08f
-        uint32_t            BufferSize      // [in]
e0b08f
-    )
e0b08f
-{
e0b08f
-    uint32_t n;
e0b08f
-
e0b08f
-    if( Context->curlen > sizeof(Context->buf) )
e0b08f
-    {
e0b08f
-       return;
e0b08f
-    }
e0b08f
-
e0b08f
-    while( BufferSize > 0 )
e0b08f
-    {
e0b08f
-        if( Context->curlen == 0 && BufferSize >= BLOCK_SIZE )
e0b08f
-        {
e0b08f
-           TransformFunction( Context, (uint8_t*)Buffer );
e0b08f
-           Context->length += BLOCK_SIZE * 8;
e0b08f
-           Buffer = (uint8_t*)Buffer + BLOCK_SIZE;
e0b08f
-           BufferSize -= BLOCK_SIZE;
e0b08f
-        }
e0b08f
-        else
e0b08f
-        {
e0b08f
-           n = MIN( BufferSize, (BLOCK_SIZE - Context->curlen) );
e0b08f
-           memcpy( Context->buf + Context->curlen, Buffer, (size_t)n );
e0b08f
-           Context->curlen += n;
e0b08f
-           Buffer = (uint8_t*)Buffer + n;
e0b08f
-           BufferSize -= n;
e0b08f
-           if( Context->curlen == BLOCK_SIZE )
e0b08f
-           {
e0b08f
-              TransformFunction( Context, Context->buf );
e0b08f
-              Context->length += 8*BLOCK_SIZE;
e0b08f
-              Context->curlen = 0;
e0b08f
-           }
e0b08f
-       }
e0b08f
-    }
e0b08f
-}
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Finalise
e0b08f
-//
e0b08f
-//  Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
e0b08f
-//  calling this, Sha256Initialised must be used to reuse the context.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Finalise
e0b08f
-    (
e0b08f
-        Sha256Context*      Context,        // [in out]
e0b08f
-        SHA256_HASH*        Digest          // [out]
e0b08f
-    )
e0b08f
-{
e0b08f
-    int i;
e0b08f
-
e0b08f
-    if( Context->curlen >= sizeof(Context->buf) )
e0b08f
-    {
e0b08f
-       return;
e0b08f
-    }
e0b08f
-
e0b08f
-    // Increase the length of the message
e0b08f
-    Context->length += Context->curlen * 8;
e0b08f
-
e0b08f
-    // Append the '1' bit
e0b08f
-    Context->buf[Context->curlen++] = (uint8_t)0x80;
e0b08f
-
e0b08f
-    // if the length is currently above 56 bytes we append zeros
e0b08f
-    // then compress.  Then we can fall back to padding zeros and length
e0b08f
-    // encoding like normal.
e0b08f
-    if( Context->curlen > 56 )
e0b08f
-    {
e0b08f
-        while( Context->curlen < 64 )
e0b08f
-        {
e0b08f
-            Context->buf[Context->curlen++] = (uint8_t)0;
e0b08f
-        }
e0b08f
-        TransformFunction(Context, Context->buf);
e0b08f
-        Context->curlen = 0;
e0b08f
-    }
e0b08f
-
e0b08f
-    // Pad up to 56 bytes of zeroes
e0b08f
-    while( Context->curlen < 56 )
e0b08f
-    {
e0b08f
-        Context->buf[Context->curlen++] = (uint8_t)0;
e0b08f
-    }
e0b08f
-
e0b08f
-    // Store length
e0b08f
-    STORE64H( Context->length, Context->buf+56 );
e0b08f
-    TransformFunction( Context, Context->buf );
e0b08f
-
e0b08f
-    // Copy output
e0b08f
-    for( i=0; i<8; i++ )
e0b08f
-    {
e0b08f
-        STORE32H( Context->state[i], Digest->bytes+(4*i) );
e0b08f
-    }
e0b08f
-}
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Calculate
e0b08f
-//
e0b08f
-//  Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
e0b08f
-//  buffer.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Calculate
e0b08f
-    (
e0b08f
-        void  const*        Buffer,         // [in]
e0b08f
-        uint32_t            BufferSize,     // [in]
e0b08f
-        SHA256_HASH*        Digest          // [in]
e0b08f
-    )
e0b08f
-{
e0b08f
-    Sha256Context context;
e0b08f
-
e0b08f
-    Sha256Initialise( &context );
e0b08f
-    Sha256Update( &context, Buffer, BufferSize );
e0b08f
-    Sha256Finalise( &context, Digest );
e0b08f
-}
e0b08f
diff --git a/policycoreutils/semodule/sha256.h b/policycoreutils/semodule/sha256.h
e0b08f
deleted file mode 100644
e0b08f
index 406ed869..00000000
e0b08f
--- a/policycoreutils/semodule/sha256.h
e0b08f
+++ /dev/null
e0b08f
@@ -1,89 +0,0 @@
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  WjCryptLib_Sha256
e0b08f
-//
e0b08f
-//  Implementation of SHA256 hash function.
e0b08f
-//  Original author: Tom St Denis, tomstdenis@gmail.com, http://libtom.org
e0b08f
-//  Modified by WaterJuice retaining Public Domain license.
e0b08f
-//
e0b08f
-//  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-#pragma once
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  IMPORTS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-#include <stdint.h>
e0b08f
-#include <stdio.h>
e0b08f
-
e0b08f
-typedef struct
e0b08f
-{
e0b08f
-    uint64_t    length;
e0b08f
-    uint32_t    state[8];
e0b08f
-    uint32_t    curlen;
e0b08f
-    uint8_t     buf[64];
e0b08f
-} Sha256Context;
e0b08f
-
e0b08f
-#define SHA256_HASH_SIZE           ( 256 / 8 )
e0b08f
-
e0b08f
-typedef struct
e0b08f
-{
e0b08f
-    uint8_t      bytes [SHA256_HASH_SIZE];
e0b08f
-} SHA256_HASH;
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  PUBLIC FUNCTIONS
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Initialise
e0b08f
-//
e0b08f
-//  Initialises a SHA256 Context. Use this to initialise/reset a context.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Initialise
e0b08f
-    (
e0b08f
-        Sha256Context*      Context         // [out]
e0b08f
-    );
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Update
e0b08f
-//
e0b08f
-//  Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
e0b08f
-//  calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Update
e0b08f
-    (
e0b08f
-        Sha256Context*      Context,        // [in out]
e0b08f
-        void const*         Buffer,         // [in]
e0b08f
-        uint32_t            BufferSize      // [in]
e0b08f
-    );
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Finalise
e0b08f
-//
e0b08f
-//  Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
e0b08f
-//  calling this, Sha256Initialised must be used to reuse the context.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Finalise
e0b08f
-    (
e0b08f
-        Sha256Context*      Context,        // [in out]
e0b08f
-        SHA256_HASH*        Digest          // [out]
e0b08f
-    );
e0b08f
-
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-//  Sha256Calculate
e0b08f
-//
e0b08f
-//  Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
e0b08f
-//  buffer.
e0b08f
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
e0b08f
-void
e0b08f
-    Sha256Calculate
e0b08f
-    (
e0b08f
-        void  const*        Buffer,         // [in]
e0b08f
-        uint32_t            BufferSize,     // [in]
e0b08f
-        SHA256_HASH*        Digest          // [in]
e0b08f
-    );
e0b08f
-- 
e0b08f
2.30.2
e0b08f