xref: /aosp_15_r20/external/selinux/libsemanage/src/sha256.h (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //  WjCryptLib_Sha256
3 //
4 //  Implementation of SHA256 hash function.
5 //  Original author: Tom St Denis, [email protected], http://libtom.org
6 //  Modified by WaterJuice retaining Public Domain license.
7 //
8 //  This is free and unencumbered software released into the public domain - June 2013 waterjuice.org
9 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10 
11 #pragma once
12 
13 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
14 //  IMPORTS
15 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 
17 #include <stdint.h>
18 #include <stdio.h>
19 
20 typedef struct
21 {
22     uint64_t    length;
23     uint32_t    state[8];
24     uint32_t    curlen;
25     uint8_t     buf[64];
26 } Sha256Context;
27 
28 #define SHA256_HASH_SIZE           ( 256 / 8 )
29 
30 typedef struct
31 {
32     uint8_t      bytes [SHA256_HASH_SIZE];
33 } SHA256_HASH;
34 
35 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
36 //  PUBLIC FUNCTIONS
37 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
38 
39 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
40 //  Sha256Initialise
41 //
42 //  Initialises a SHA256 Context. Use this to initialise/reset a context.
43 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
44 void
45     Sha256Initialise
46     (
47         Sha256Context*      Context         // [out]
48     );
49 
50 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
51 //  Sha256Update
52 //
53 //  Adds data to the SHA256 context. This will process the data and update the internal state of the context. Keep on
54 //  calling this function until all the data has been added. Then call Sha256Finalise to calculate the hash.
55 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
56 void
57     Sha256Update
58     (
59         Sha256Context*      Context,        // [in out]
60         void const*         Buffer,         // [in]
61         uint32_t            BufferSize      // [in]
62     );
63 
64 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
65 //  Sha256Finalise
66 //
67 //  Performs the final calculation of the hash and returns the digest (32 byte buffer containing 256bit hash). After
68 //  calling this, Sha256Initialised must be used to reuse the context.
69 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
70 void
71     Sha256Finalise
72     (
73         Sha256Context*      Context,        // [in out]
74         SHA256_HASH*        Digest          // [out]
75     );
76 
77 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
78 //  Sha256Calculate
79 //
80 //  Combines Sha256Initialise, Sha256Update, and Sha256Finalise into one function. Calculates the SHA256 hash of the
81 //  buffer.
82 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
83 void
84     Sha256Calculate
85     (
86         void  const*        Buffer,         // [in]
87         uint32_t            BufferSize,     // [in]
88         SHA256_HASH*        Digest          // [in]
89     );
90