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

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