31#define SHR(x, c) ((x) >> (c))
32#define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
34#define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
35#define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
37#define SIGMA_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
38#define SIGMA_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
40#define SIGMA_2(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
41#define SIGMA_3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
43#define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k) \
45 H += SIGMA_1(E) + CH(E, F, G) + M1 + k; \
47 H += SIGMA_0(A) + MAJ(A, B, C); \
48 M1 += SIGMA_2(M2) + M3 + SIGMA_3(M4); \
52 InternalState.State[0] = 0x6A09E667;
53 InternalState.State[1] = 0xBB67AE85;
54 InternalState.State[2] = 0x3C6EF372;
55 InternalState.State[3] = 0xA54FF53A;
56 InternalState.State[4] = 0x510E527F;
57 InternalState.State[5] = 0x9B05688C;
58 InternalState.State[6] = 0x1F83D9AB;
59 InternalState.State[7] = 0x5BE0CD19;
60 InternalState.ByteCount = 0;
61 InternalState.BufferOffset = 0;
64void SHA256::hashBlock() {
74 uint32_t W00 = InternalState.Buffer.L[0];
75 uint32_t W01 = InternalState.Buffer.L[1];
76 uint32_t W02 = InternalState.Buffer.L[2];
77 uint32_t W03 = InternalState.Buffer.L[3];
78 uint32_t W04 = InternalState.Buffer.L[4];
79 uint32_t W05 = InternalState.Buffer.L[5];
80 uint32_t W06 = InternalState.Buffer.L[6];
81 uint32_t W07 = InternalState.Buffer.L[7];
82 uint32_t W08 = InternalState.Buffer.L[8];
83 uint32_t W09 = InternalState.Buffer.L[9];
84 uint32_t W10 = InternalState.Buffer.L[10];
85 uint32_t W11 = InternalState.Buffer.L[11];
86 uint32_t W12 = InternalState.Buffer.L[12];
87 uint32_t W13 = InternalState.Buffer.L[13];
88 uint32_t W14 = InternalState.Buffer.L[14];
89 uint32_t W15 = InternalState.Buffer.L[15];
91 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W00, W14, W09, W01, 0x428A2F98);
92 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W01, W15, W10, W02, 0x71374491);
93 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W02, W00, W11, W03, 0xB5C0FBCF);
94 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W03, W01, W12, W04, 0xE9B5DBA5);
95 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W04, W02, W13, W05, 0x3956C25B);
96 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W05, W03, W14, W06, 0x59F111F1);
97 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W06, W04, W15, W07, 0x923F82A4);
98 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W07, W05, W00, W08, 0xAB1C5ED5);
99 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W08, W06, W01, W09, 0xD807AA98);
100 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W09, W07, W02, W10, 0x12835B01);
101 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W10, W08, W03, W11, 0x243185BE);
102 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W11, W09, W04, W12, 0x550C7DC3);
103 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W12, W10, W05, W13, 0x72BE5D74);
104 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W13, W11, W06, W14, 0x80DEB1FE);
105 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W14, W12, W07, W15, 0x9BDC06A7);
106 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W15, W13, W08, W00, 0xC19BF174);
108 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W00, W14, W09, W01, 0xE49B69C1);
109 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W01, W15, W10, W02, 0xEFBE4786);
110 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W02, W00, W11, W03, 0x0FC19DC6);
111 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W03, W01, W12, W04, 0x240CA1CC);
112 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W04, W02, W13, W05, 0x2DE92C6F);
113 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W05, W03, W14, W06, 0x4A7484AA);
114 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W06, W04, W15, W07, 0x5CB0A9DC);
115 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W07, W05, W00, W08, 0x76F988DA);
116 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W08, W06, W01, W09, 0x983E5152);
117 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W09, W07, W02, W10, 0xA831C66D);
118 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W10, W08, W03, W11, 0xB00327C8);
119 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W11, W09, W04, W12, 0xBF597FC7);
120 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W12, W10, W05, W13, 0xC6E00BF3);
121 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W13, W11, W06, W14, 0xD5A79147);
122 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W14, W12, W07, W15, 0x06CA6351);
123 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W15, W13, W08, W00, 0x14292967);
125 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W00, W14, W09, W01, 0x27B70A85);
126 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W01, W15, W10, W02, 0x2E1B2138);
127 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W02, W00, W11, W03, 0x4D2C6DFC);
128 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W03, W01, W12, W04, 0x53380D13);
129 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W04, W02, W13, W05, 0x650A7354);
130 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W05, W03, W14, W06, 0x766A0ABB);
131 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W06, W04, W15, W07, 0x81C2C92E);
132 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W07, W05, W00, W08, 0x92722C85);
133 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W08, W06, W01, W09, 0xA2BFE8A1);
134 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W09, W07, W02, W10, 0xA81A664B);
135 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W10, W08, W03, W11, 0xC24B8B70);
136 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W11, W09, W04, W12, 0xC76C51A3);
137 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W12, W10, W05, W13, 0xD192E819);
138 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W13, W11, W06, W14, 0xD6990624);
139 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W14, W12, W07, W15, 0xF40E3585);
140 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W15, W13, W08, W00, 0x106AA070);
142 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W00, W14, W09, W01, 0x19A4C116);
143 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W01, W15, W10, W02, 0x1E376C08);
144 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W02, W00, W11, W03, 0x2748774C);
145 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W03, W01, W12, W04, 0x34B0BCB5);
146 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W04, W02, W13, W05, 0x391C0CB3);
147 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W05, W03, W14, W06, 0x4ED8AA4A);
148 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W06, W04, W15, W07, 0x5B9CCA4F);
149 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W07, W05, W00, W08, 0x682E6FF3);
150 F_EXPAND(
A,
B,
C,
D,
E,
F,
G,
H, W08, W06, W01, W09, 0x748F82EE);
151 F_EXPAND(
H,
A,
B,
C,
D,
E,
F,
G, W09, W07, W02, W10, 0x78A5636F);
152 F_EXPAND(
G,
H,
A,
B,
C,
D,
E,
F, W10, W08, W03, W11, 0x84C87814);
153 F_EXPAND(
F,
G,
H,
A,
B,
C,
D,
E, W11, W09, W04, W12, 0x8CC70208);
154 F_EXPAND(
E,
F,
G,
H,
A,
B,
C,
D, W12, W10, W05, W13, 0x90BEFFFA);
155 F_EXPAND(
D,
E,
F,
G,
H,
A,
B,
C, W13, W11, W06, W14, 0xA4506CEB);
156 F_EXPAND(
C,
D,
E,
F,
G,
H,
A,
B, W14, W12, W07, W15, 0xBEF9A3F7);
157 F_EXPAND(
B,
C,
D,
E,
F,
G,
H,
A, W15, W13, W08, W00, 0xC67178F2);
159 InternalState.State[0] +=
A;
160 InternalState.State[1] +=
B;
161 InternalState.State[2] +=
C;
162 InternalState.State[3] +=
D;
163 InternalState.State[4] +=
E;
164 InternalState.State[5] +=
F;
165 InternalState.State[6] +=
G;
166 InternalState.State[7] +=
H;
169void SHA256::addUncounted(uint8_t
Data) {
171 InternalState.Buffer.C[InternalState.BufferOffset] =
Data;
173 InternalState.Buffer.C[InternalState.BufferOffset ^ 3] =
Data;
175 InternalState.BufferOffset++;
176 if (InternalState.BufferOffset == BLOCK_LENGTH) {
178 InternalState.BufferOffset = 0;
182void SHA256::writebyte(uint8_t
Data) {
183 ++InternalState.ByteCount;
188 InternalState.ByteCount +=
Data.size();
191 if (InternalState.BufferOffset > 0) {
192 const size_t Remainder = std::min<size_t>(
193 Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);
194 for (
size_t I = 0;
I < Remainder; ++
I)
195 addUncounted(
Data[
I]);
200 while (
Data.size() >= BLOCK_LENGTH) {
201 assert(InternalState.BufferOffset == 0);
202 static_assert(BLOCK_LENGTH % 4 == 0);
203 constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;
204 for (
size_t I = 0;
I < BLOCK_LENGTH_32; ++
I)
207 Data =
Data.drop_front(BLOCK_LENGTH);
211 for (uint8_t
C :
Data)
225 while (InternalState.BufferOffset != 56)
228 uint64_t len = InternalState.ByteCount << 3;
231 addUncounted(len >> 56);
232 addUncounted(len >> 48);
233 addUncounted(len >> 40);
234 addUncounted(len >> 32);
235 addUncounted(len >> 24);
236 addUncounted(len >> 16);
237 addUncounted(len >> 8);
241void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
247 for (
int i = 0; i < 8; i++) {
248 HashResult[i] = InternalState.State[i];
252 for (
int i = 0; i < 8; i++) {
260 std::array<
uint32_t, HASH_LENGTH / 4> HashResult;
261 std::array<uint8_t, HASH_LENGTH> ReturnResult;
263 static_assert(
sizeof(HashResult) ==
sizeof(ReturnResult));
269 auto StateToRestore = InternalState;
274 InternalState = StateToRestore;
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
void init()
Reinitialize the internal state.
std::array< uint8_t, 32 > final()
Return the current raw 256-bits SHA256 for the digested data since the last call to init().
static std::array< uint8_t, 32 > hash(ArrayRef< uint8_t > Data)
Returns a raw 256-bit SHA256 hash for the given data.
void update(ArrayRef< uint8_t > Data)
Digest more data.
std::array< uint8_t, 32 > result()
Return the current raw 256-bits SHA256 for the digested data since the last call to init().
StringRef - Represent a constant reference to a string, i.e.
uint32_t read32be(const void *P)
constexpr bool IsBigEndianHost
This is an optimization pass for GlobalISel generic memory operations.
constexpr T byteswap(T V) noexcept
Reverses the bytes in the given integer value V.