xref: /freebsd/contrib/llvm-project/llvm/lib/Support/SHA256.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
1fe6060f1SDimitry Andric //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
2fe6060f1SDimitry Andric //
3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fe6060f1SDimitry Andric //
7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8fe6060f1SDimitry Andric /*
9fe6060f1SDimitry Andric  *  The SHA-256 Secure Hash Standard was published by NIST in 2002.
10fe6060f1SDimitry Andric  *
11fe6060f1SDimitry Andric  *  http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
12fe6060f1SDimitry Andric  *
13fe6060f1SDimitry Andric  *   The implementation is based on nacl's sha256 implementation [0] and LLVM's
14fe6060f1SDimitry Andric  *  pre-exsiting SHA1 code [1].
15fe6060f1SDimitry Andric  *
16fe6060f1SDimitry Andric  *   [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
17fe6060f1SDimitry Andric  *       code)
18fe6060f1SDimitry Andric  *   [1] llvm/lib/Support/SHA1.{h,cpp}
19fe6060f1SDimitry Andric  */
20fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
21fe6060f1SDimitry Andric 
22fe6060f1SDimitry Andric #include "llvm/Support/SHA256.h"
23fe6060f1SDimitry Andric #include "llvm/ADT/ArrayRef.h"
24fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h"
25fe6060f1SDimitry Andric #include "llvm/Support/Endian.h"
26bdd1243dSDimitry Andric #include "llvm/Support/SwapByteOrder.h"
27fe6060f1SDimitry Andric #include <string.h>
28fe6060f1SDimitry Andric 
29fe6060f1SDimitry Andric namespace llvm {
30fe6060f1SDimitry Andric 
31fe6060f1SDimitry Andric #define SHR(x, c) ((x) >> (c))
32fe6060f1SDimitry Andric #define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
33fe6060f1SDimitry Andric 
34fe6060f1SDimitry Andric #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
35fe6060f1SDimitry Andric #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
36fe6060f1SDimitry Andric 
37fe6060f1SDimitry Andric #define SIGMA_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
38fe6060f1SDimitry Andric #define SIGMA_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
39fe6060f1SDimitry Andric 
40fe6060f1SDimitry Andric #define SIGMA_2(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
41fe6060f1SDimitry Andric #define SIGMA_3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
42fe6060f1SDimitry Andric 
43fe6060f1SDimitry Andric #define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k)                    \
44fe6060f1SDimitry Andric   do {                                                                         \
45fe6060f1SDimitry Andric     H += SIGMA_1(E) + CH(E, F, G) + M1 + k;                                    \
46fe6060f1SDimitry Andric     D += H;                                                                    \
47fe6060f1SDimitry Andric     H += SIGMA_0(A) + MAJ(A, B, C);                                            \
48fe6060f1SDimitry Andric     M1 += SIGMA_2(M2) + M3 + SIGMA_3(M4);                                      \
49fe6060f1SDimitry Andric   } while (0);
50fe6060f1SDimitry Andric 
51fe6060f1SDimitry Andric void SHA256::init() {
52fe6060f1SDimitry Andric   InternalState.State[0] = 0x6A09E667;
53fe6060f1SDimitry Andric   InternalState.State[1] = 0xBB67AE85;
54fe6060f1SDimitry Andric   InternalState.State[2] = 0x3C6EF372;
55fe6060f1SDimitry Andric   InternalState.State[3] = 0xA54FF53A;
56fe6060f1SDimitry Andric   InternalState.State[4] = 0x510E527F;
57fe6060f1SDimitry Andric   InternalState.State[5] = 0x9B05688C;
58fe6060f1SDimitry Andric   InternalState.State[6] = 0x1F83D9AB;
59fe6060f1SDimitry Andric   InternalState.State[7] = 0x5BE0CD19;
60fe6060f1SDimitry Andric   InternalState.ByteCount = 0;
61fe6060f1SDimitry Andric   InternalState.BufferOffset = 0;
62fe6060f1SDimitry Andric }
63fe6060f1SDimitry Andric 
64fe6060f1SDimitry Andric void SHA256::hashBlock() {
65fe6060f1SDimitry Andric   uint32_t A = InternalState.State[0];
66fe6060f1SDimitry Andric   uint32_t B = InternalState.State[1];
67fe6060f1SDimitry Andric   uint32_t C = InternalState.State[2];
68fe6060f1SDimitry Andric   uint32_t D = InternalState.State[3];
69fe6060f1SDimitry Andric   uint32_t E = InternalState.State[4];
70fe6060f1SDimitry Andric   uint32_t F = InternalState.State[5];
71fe6060f1SDimitry Andric   uint32_t G = InternalState.State[6];
72fe6060f1SDimitry Andric   uint32_t H = InternalState.State[7];
73fe6060f1SDimitry Andric 
74fe6060f1SDimitry Andric   uint32_t W00 = InternalState.Buffer.L[0];
75fe6060f1SDimitry Andric   uint32_t W01 = InternalState.Buffer.L[1];
76fe6060f1SDimitry Andric   uint32_t W02 = InternalState.Buffer.L[2];
77fe6060f1SDimitry Andric   uint32_t W03 = InternalState.Buffer.L[3];
78fe6060f1SDimitry Andric   uint32_t W04 = InternalState.Buffer.L[4];
79fe6060f1SDimitry Andric   uint32_t W05 = InternalState.Buffer.L[5];
80fe6060f1SDimitry Andric   uint32_t W06 = InternalState.Buffer.L[6];
81fe6060f1SDimitry Andric   uint32_t W07 = InternalState.Buffer.L[7];
82fe6060f1SDimitry Andric   uint32_t W08 = InternalState.Buffer.L[8];
83fe6060f1SDimitry Andric   uint32_t W09 = InternalState.Buffer.L[9];
84fe6060f1SDimitry Andric   uint32_t W10 = InternalState.Buffer.L[10];
85fe6060f1SDimitry Andric   uint32_t W11 = InternalState.Buffer.L[11];
86fe6060f1SDimitry Andric   uint32_t W12 = InternalState.Buffer.L[12];
87fe6060f1SDimitry Andric   uint32_t W13 = InternalState.Buffer.L[13];
88fe6060f1SDimitry Andric   uint32_t W14 = InternalState.Buffer.L[14];
89fe6060f1SDimitry Andric   uint32_t W15 = InternalState.Buffer.L[15];
90fe6060f1SDimitry Andric 
91fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
92fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
93fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
94fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
95fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
96fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
97fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
98fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
99fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
100fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
101fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
102fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
103fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
104fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
105fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
106fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
107fe6060f1SDimitry Andric 
108fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
109fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
110fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
111fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
112fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
113fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
114fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
115fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
116fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
117fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
118fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
119fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
120fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
121fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
122fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
123fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
124fe6060f1SDimitry Andric 
125fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
126fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
127fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
128fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
129fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
130fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
131fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
132fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
133fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
134fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
135fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
136fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
137fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
138fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
139fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
140fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
141fe6060f1SDimitry Andric 
142fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
143fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
144fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
145fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
146fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
147fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
148fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
149fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
150fe6060f1SDimitry Andric   F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
151fe6060f1SDimitry Andric   F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
152fe6060f1SDimitry Andric   F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
153fe6060f1SDimitry Andric   F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
154fe6060f1SDimitry Andric   F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
155fe6060f1SDimitry Andric   F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
156fe6060f1SDimitry Andric   F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
157fe6060f1SDimitry Andric   F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
158fe6060f1SDimitry Andric 
159fe6060f1SDimitry Andric   InternalState.State[0] += A;
160fe6060f1SDimitry Andric   InternalState.State[1] += B;
161fe6060f1SDimitry Andric   InternalState.State[2] += C;
162fe6060f1SDimitry Andric   InternalState.State[3] += D;
163fe6060f1SDimitry Andric   InternalState.State[4] += E;
164fe6060f1SDimitry Andric   InternalState.State[5] += F;
165fe6060f1SDimitry Andric   InternalState.State[6] += G;
166fe6060f1SDimitry Andric   InternalState.State[7] += H;
167fe6060f1SDimitry Andric }
168fe6060f1SDimitry Andric 
169fe6060f1SDimitry Andric void SHA256::addUncounted(uint8_t Data) {
170bdd1243dSDimitry Andric   if constexpr (sys::IsBigEndianHost)
171fe6060f1SDimitry Andric     InternalState.Buffer.C[InternalState.BufferOffset] = Data;
172bdd1243dSDimitry Andric   else
173fe6060f1SDimitry Andric     InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
174fe6060f1SDimitry Andric 
175fe6060f1SDimitry Andric   InternalState.BufferOffset++;
176fe6060f1SDimitry Andric   if (InternalState.BufferOffset == BLOCK_LENGTH) {
177fe6060f1SDimitry Andric     hashBlock();
178fe6060f1SDimitry Andric     InternalState.BufferOffset = 0;
179fe6060f1SDimitry Andric   }
180fe6060f1SDimitry Andric }
181fe6060f1SDimitry Andric 
182fe6060f1SDimitry Andric void SHA256::writebyte(uint8_t Data) {
183fe6060f1SDimitry Andric   ++InternalState.ByteCount;
184fe6060f1SDimitry Andric   addUncounted(Data);
185fe6060f1SDimitry Andric }
186fe6060f1SDimitry Andric 
187fe6060f1SDimitry Andric void SHA256::update(ArrayRef<uint8_t> Data) {
188fe6060f1SDimitry Andric   InternalState.ByteCount += Data.size();
189fe6060f1SDimitry Andric 
190fe6060f1SDimitry Andric   // Finish the current block.
191fe6060f1SDimitry Andric   if (InternalState.BufferOffset > 0) {
192fe6060f1SDimitry Andric     const size_t Remainder = std::min<size_t>(
193fe6060f1SDimitry Andric         Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);
194fe6060f1SDimitry Andric     for (size_t I = 0; I < Remainder; ++I)
195fe6060f1SDimitry Andric       addUncounted(Data[I]);
196fe6060f1SDimitry Andric     Data = Data.drop_front(Remainder);
197fe6060f1SDimitry Andric   }
198fe6060f1SDimitry Andric 
199fe6060f1SDimitry Andric   // Fast buffer filling for large inputs.
200fe6060f1SDimitry Andric   while (Data.size() >= BLOCK_LENGTH) {
201fe6060f1SDimitry Andric     assert(InternalState.BufferOffset == 0);
202bdd1243dSDimitry Andric     static_assert(BLOCK_LENGTH % 4 == 0);
203fe6060f1SDimitry Andric     constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;
204fe6060f1SDimitry Andric     for (size_t I = 0; I < BLOCK_LENGTH_32; ++I)
205fe6060f1SDimitry Andric       InternalState.Buffer.L[I] = support::endian::read32be(&Data[I * 4]);
206fe6060f1SDimitry Andric     hashBlock();
207fe6060f1SDimitry Andric     Data = Data.drop_front(BLOCK_LENGTH);
208fe6060f1SDimitry Andric   }
209fe6060f1SDimitry Andric 
210fe6060f1SDimitry Andric   // Finish the remainder.
211fe6060f1SDimitry Andric   for (uint8_t C : Data)
212fe6060f1SDimitry Andric     addUncounted(C);
213fe6060f1SDimitry Andric }
214fe6060f1SDimitry Andric 
215fe6060f1SDimitry Andric void SHA256::update(StringRef Str) {
216fe6060f1SDimitry Andric   update(
217fe6060f1SDimitry Andric       ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()), Str.size()));
218fe6060f1SDimitry Andric }
219fe6060f1SDimitry Andric 
220fe6060f1SDimitry Andric void SHA256::pad() {
221fe6060f1SDimitry Andric   // Implement SHA-2 padding (fips180-2 5.1.1)
222fe6060f1SDimitry Andric 
223fe6060f1SDimitry Andric   // Pad with 0x80 followed by 0x00 until the end of the block
224fe6060f1SDimitry Andric   addUncounted(0x80);
225fe6060f1SDimitry Andric   while (InternalState.BufferOffset != 56)
226fe6060f1SDimitry Andric     addUncounted(0x00);
227fe6060f1SDimitry Andric 
228fe6060f1SDimitry Andric   uint64_t len = InternalState.ByteCount << 3; // bit size
229fe6060f1SDimitry Andric 
230fe6060f1SDimitry Andric   // Append length in the last 8 bytes big edian encoded
231fe6060f1SDimitry Andric   addUncounted(len >> 56);
232fe6060f1SDimitry Andric   addUncounted(len >> 48);
233fe6060f1SDimitry Andric   addUncounted(len >> 40);
234fe6060f1SDimitry Andric   addUncounted(len >> 32);
235fe6060f1SDimitry Andric   addUncounted(len >> 24);
236fe6060f1SDimitry Andric   addUncounted(len >> 16);
237fe6060f1SDimitry Andric   addUncounted(len >> 8);
238fe6060f1SDimitry Andric   addUncounted(len);
239fe6060f1SDimitry Andric }
240fe6060f1SDimitry Andric 
24181ad6265SDimitry Andric void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
242fe6060f1SDimitry Andric   // Pad to complete the last block
243fe6060f1SDimitry Andric   pad();
244fe6060f1SDimitry Andric 
245bdd1243dSDimitry Andric   if constexpr (sys::IsBigEndianHost) {
246fe6060f1SDimitry Andric     // Just copy the current state
247fe6060f1SDimitry Andric     for (int i = 0; i < 8; i++) {
248fe6060f1SDimitry Andric       HashResult[i] = InternalState.State[i];
249fe6060f1SDimitry Andric     }
250bdd1243dSDimitry Andric   } else {
251fe6060f1SDimitry Andric     // Swap byte order back
252fe6060f1SDimitry Andric     for (int i = 0; i < 8; i++) {
253*5f757f3fSDimitry Andric       HashResult[i] = llvm::byteswap(InternalState.State[i]);
254fe6060f1SDimitry Andric     }
255bdd1243dSDimitry Andric   }
256fe6060f1SDimitry Andric }
257fe6060f1SDimitry Andric 
25881ad6265SDimitry Andric std::array<uint8_t, 32> SHA256::final() {
25981ad6265SDimitry Andric   union {
26081ad6265SDimitry Andric     std::array<uint32_t, HASH_LENGTH / 4> HashResult;
26181ad6265SDimitry Andric     std::array<uint8_t, HASH_LENGTH> ReturnResult;
26281ad6265SDimitry Andric   };
263bdd1243dSDimitry Andric   static_assert(sizeof(HashResult) == sizeof(ReturnResult));
26481ad6265SDimitry Andric   final(HashResult);
26581ad6265SDimitry Andric   return ReturnResult;
26681ad6265SDimitry Andric }
26781ad6265SDimitry Andric 
26881ad6265SDimitry Andric std::array<uint8_t, 32> SHA256::result() {
269fe6060f1SDimitry Andric   auto StateToRestore = InternalState;
270fe6060f1SDimitry Andric 
271fe6060f1SDimitry Andric   auto Hash = final();
272fe6060f1SDimitry Andric 
273fe6060f1SDimitry Andric   // Restore the state
274fe6060f1SDimitry Andric   InternalState = StateToRestore;
275fe6060f1SDimitry Andric 
276fe6060f1SDimitry Andric   // Return pointer to hash (32 characters)
277fe6060f1SDimitry Andric   return Hash;
278fe6060f1SDimitry Andric }
279fe6060f1SDimitry Andric 
280fe6060f1SDimitry Andric std::array<uint8_t, 32> SHA256::hash(ArrayRef<uint8_t> Data) {
281fe6060f1SDimitry Andric   SHA256 Hash;
282fe6060f1SDimitry Andric   Hash.update(Data);
28381ad6265SDimitry Andric   return Hash.final();
284fe6060f1SDimitry Andric }
285fe6060f1SDimitry Andric 
286fe6060f1SDimitry Andric } // namespace llvm
287