1656ead1fScynecx //====- SHA256.cpp - SHA256 implementation ---*- C++ -* ======//
2656ead1fScynecx //
3656ead1fScynecx // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4656ead1fScynecx // See https://llvm.org/LICENSE.txt for license information.
5656ead1fScynecx // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6656ead1fScynecx //
7656ead1fScynecx //===----------------------------------------------------------------------===//
8656ead1fScynecx /*
9656ead1fScynecx * The SHA-256 Secure Hash Standard was published by NIST in 2002.
10656ead1fScynecx *
11656ead1fScynecx * http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
12656ead1fScynecx *
13656ead1fScynecx * The implementation is based on nacl's sha256 implementation [0] and LLVM's
14656ead1fScynecx * pre-exsiting SHA1 code [1].
15656ead1fScynecx *
16656ead1fScynecx * [0] https://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 (public domain
17656ead1fScynecx * code)
18656ead1fScynecx * [1] llvm/lib/Support/SHA1.{h,cpp}
19656ead1fScynecx */
20656ead1fScynecx //===----------------------------------------------------------------------===//
21656ead1fScynecx
22656ead1fScynecx #include "llvm/Support/SHA256.h"
23656ead1fScynecx #include "llvm/ADT/ArrayRef.h"
24656ead1fScynecx #include "llvm/ADT/StringRef.h"
25656ead1fScynecx #include "llvm/Support/Endian.h"
2653d234eaSArchibald Elliott #include "llvm/Support/SwapByteOrder.h"
27656ead1fScynecx #include <string.h>
28656ead1fScynecx
29656ead1fScynecx namespace llvm {
30656ead1fScynecx
31656ead1fScynecx #define SHR(x, c) ((x) >> (c))
32656ead1fScynecx #define ROTR(x, n) (((x) >> n) | ((x) << (32 - (n))))
33656ead1fScynecx
34656ead1fScynecx #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z)))
35656ead1fScynecx #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
36656ead1fScynecx
37656ead1fScynecx #define SIGMA_0(x) (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
38656ead1fScynecx #define SIGMA_1(x) (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
39656ead1fScynecx
40656ead1fScynecx #define SIGMA_2(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10))
41656ead1fScynecx #define SIGMA_3(x) (ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3))
42656ead1fScynecx
43656ead1fScynecx #define F_EXPAND(A, B, C, D, E, F, G, H, M1, M2, M3, M4, k) \
44656ead1fScynecx do { \
45656ead1fScynecx H += SIGMA_1(E) + CH(E, F, G) + M1 + k; \
46656ead1fScynecx D += H; \
47656ead1fScynecx H += SIGMA_0(A) + MAJ(A, B, C); \
48656ead1fScynecx M1 += SIGMA_2(M2) + M3 + SIGMA_3(M4); \
49656ead1fScynecx } while (0);
50656ead1fScynecx
init()51656ead1fScynecx void SHA256::init() {
52656ead1fScynecx InternalState.State[0] = 0x6A09E667;
53656ead1fScynecx InternalState.State[1] = 0xBB67AE85;
54656ead1fScynecx InternalState.State[2] = 0x3C6EF372;
55656ead1fScynecx InternalState.State[3] = 0xA54FF53A;
56656ead1fScynecx InternalState.State[4] = 0x510E527F;
57656ead1fScynecx InternalState.State[5] = 0x9B05688C;
58656ead1fScynecx InternalState.State[6] = 0x1F83D9AB;
59656ead1fScynecx InternalState.State[7] = 0x5BE0CD19;
60656ead1fScynecx InternalState.ByteCount = 0;
61656ead1fScynecx InternalState.BufferOffset = 0;
62656ead1fScynecx }
63656ead1fScynecx
hashBlock()64656ead1fScynecx void SHA256::hashBlock() {
65656ead1fScynecx uint32_t A = InternalState.State[0];
66656ead1fScynecx uint32_t B = InternalState.State[1];
67656ead1fScynecx uint32_t C = InternalState.State[2];
68656ead1fScynecx uint32_t D = InternalState.State[3];
69656ead1fScynecx uint32_t E = InternalState.State[4];
70656ead1fScynecx uint32_t F = InternalState.State[5];
71656ead1fScynecx uint32_t G = InternalState.State[6];
72656ead1fScynecx uint32_t H = InternalState.State[7];
73656ead1fScynecx
74656ead1fScynecx uint32_t W00 = InternalState.Buffer.L[0];
75656ead1fScynecx uint32_t W01 = InternalState.Buffer.L[1];
76656ead1fScynecx uint32_t W02 = InternalState.Buffer.L[2];
77656ead1fScynecx uint32_t W03 = InternalState.Buffer.L[3];
78656ead1fScynecx uint32_t W04 = InternalState.Buffer.L[4];
79656ead1fScynecx uint32_t W05 = InternalState.Buffer.L[5];
80656ead1fScynecx uint32_t W06 = InternalState.Buffer.L[6];
81656ead1fScynecx uint32_t W07 = InternalState.Buffer.L[7];
82656ead1fScynecx uint32_t W08 = InternalState.Buffer.L[8];
83656ead1fScynecx uint32_t W09 = InternalState.Buffer.L[9];
84656ead1fScynecx uint32_t W10 = InternalState.Buffer.L[10];
85656ead1fScynecx uint32_t W11 = InternalState.Buffer.L[11];
86656ead1fScynecx uint32_t W12 = InternalState.Buffer.L[12];
87656ead1fScynecx uint32_t W13 = InternalState.Buffer.L[13];
88656ead1fScynecx uint32_t W14 = InternalState.Buffer.L[14];
89656ead1fScynecx uint32_t W15 = InternalState.Buffer.L[15];
90656ead1fScynecx
91656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
92656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
93656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
94656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
95656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
96656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
97656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
98656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
99656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
100656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
101656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
102656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
103656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
104656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
105656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
106656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
107656ead1fScynecx
108656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
109656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
110656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
111656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
112656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
113656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
114656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
115656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
116656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
117656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
118656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
119656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
120656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
121656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
122656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
123656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
124656ead1fScynecx
125656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
126656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
127656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
128656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
129656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
130656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
131656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
132656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
133656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
134656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
135656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
136656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
137656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
138656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
139656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
140656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
141656ead1fScynecx
142656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
143656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
144656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
145656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
146656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
147656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
148656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
149656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
150656ead1fScynecx F_EXPAND(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
151656ead1fScynecx F_EXPAND(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
152656ead1fScynecx F_EXPAND(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
153656ead1fScynecx F_EXPAND(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
154656ead1fScynecx F_EXPAND(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
155656ead1fScynecx F_EXPAND(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
156656ead1fScynecx F_EXPAND(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
157656ead1fScynecx F_EXPAND(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
158656ead1fScynecx
159656ead1fScynecx InternalState.State[0] += A;
160656ead1fScynecx InternalState.State[1] += B;
161656ead1fScynecx InternalState.State[2] += C;
162656ead1fScynecx InternalState.State[3] += D;
163656ead1fScynecx InternalState.State[4] += E;
164656ead1fScynecx InternalState.State[5] += F;
165656ead1fScynecx InternalState.State[6] += G;
166656ead1fScynecx InternalState.State[7] += H;
167656ead1fScynecx }
168656ead1fScynecx
addUncounted(uint8_t Data)169656ead1fScynecx void SHA256::addUncounted(uint8_t Data) {
17053d234eaSArchibald Elliott if constexpr (sys::IsBigEndianHost)
171656ead1fScynecx InternalState.Buffer.C[InternalState.BufferOffset] = Data;
17253d234eaSArchibald Elliott else
173656ead1fScynecx InternalState.Buffer.C[InternalState.BufferOffset ^ 3] = Data;
174656ead1fScynecx
175656ead1fScynecx InternalState.BufferOffset++;
176656ead1fScynecx if (InternalState.BufferOffset == BLOCK_LENGTH) {
177656ead1fScynecx hashBlock();
178656ead1fScynecx InternalState.BufferOffset = 0;
179656ead1fScynecx }
180656ead1fScynecx }
181656ead1fScynecx
writebyte(uint8_t Data)182656ead1fScynecx void SHA256::writebyte(uint8_t Data) {
183656ead1fScynecx ++InternalState.ByteCount;
184656ead1fScynecx addUncounted(Data);
185656ead1fScynecx }
186656ead1fScynecx
update(ArrayRef<uint8_t> Data)187656ead1fScynecx void SHA256::update(ArrayRef<uint8_t> Data) {
188656ead1fScynecx InternalState.ByteCount += Data.size();
189656ead1fScynecx
190656ead1fScynecx // Finish the current block.
191656ead1fScynecx if (InternalState.BufferOffset > 0) {
192656ead1fScynecx const size_t Remainder = std::min<size_t>(
193656ead1fScynecx Data.size(), BLOCK_LENGTH - InternalState.BufferOffset);
194656ead1fScynecx for (size_t I = 0; I < Remainder; ++I)
195656ead1fScynecx addUncounted(Data[I]);
196656ead1fScynecx Data = Data.drop_front(Remainder);
197656ead1fScynecx }
198656ead1fScynecx
199656ead1fScynecx // Fast buffer filling for large inputs.
200656ead1fScynecx while (Data.size() >= BLOCK_LENGTH) {
201656ead1fScynecx assert(InternalState.BufferOffset == 0);
20232aa35b5SKazu Hirata static_assert(BLOCK_LENGTH % 4 == 0);
203656ead1fScynecx constexpr size_t BLOCK_LENGTH_32 = BLOCK_LENGTH / 4;
204656ead1fScynecx for (size_t I = 0; I < BLOCK_LENGTH_32; ++I)
205656ead1fScynecx InternalState.Buffer.L[I] = support::endian::read32be(&Data[I * 4]);
206656ead1fScynecx hashBlock();
207656ead1fScynecx Data = Data.drop_front(BLOCK_LENGTH);
208656ead1fScynecx }
209656ead1fScynecx
210656ead1fScynecx // Finish the remainder.
211656ead1fScynecx for (uint8_t C : Data)
212656ead1fScynecx addUncounted(C);
213656ead1fScynecx }
214656ead1fScynecx
update(StringRef Str)215656ead1fScynecx void SHA256::update(StringRef Str) {
216656ead1fScynecx update(
217656ead1fScynecx ArrayRef<uint8_t>((uint8_t *)const_cast<char *>(Str.data()), Str.size()));
218656ead1fScynecx }
219656ead1fScynecx
pad()220656ead1fScynecx void SHA256::pad() {
221656ead1fScynecx // Implement SHA-2 padding (fips180-2 5.1.1)
222656ead1fScynecx
223656ead1fScynecx // Pad with 0x80 followed by 0x00 until the end of the block
224656ead1fScynecx addUncounted(0x80);
225656ead1fScynecx while (InternalState.BufferOffset != 56)
226656ead1fScynecx addUncounted(0x00);
227656ead1fScynecx
228656ead1fScynecx uint64_t len = InternalState.ByteCount << 3; // bit size
229656ead1fScynecx
230656ead1fScynecx // Append length in the last 8 bytes big edian encoded
231656ead1fScynecx addUncounted(len >> 56);
232656ead1fScynecx addUncounted(len >> 48);
233656ead1fScynecx addUncounted(len >> 40);
234656ead1fScynecx addUncounted(len >> 32);
235656ead1fScynecx addUncounted(len >> 24);
236656ead1fScynecx addUncounted(len >> 16);
237656ead1fScynecx addUncounted(len >> 8);
238656ead1fScynecx addUncounted(len);
239656ead1fScynecx }
240656ead1fScynecx
final(std::array<uint32_t,HASH_LENGTH/4> & HashResult)241330268baSArgyrios Kyrtzidis void SHA256::final(std::array<uint32_t, HASH_LENGTH / 4> &HashResult) {
242656ead1fScynecx // Pad to complete the last block
243656ead1fScynecx pad();
244656ead1fScynecx
24553d234eaSArchibald Elliott if constexpr (sys::IsBigEndianHost) {
246656ead1fScynecx // Just copy the current state
247656ead1fScynecx for (int i = 0; i < 8; i++) {
248656ead1fScynecx HashResult[i] = InternalState.State[i];
249656ead1fScynecx }
25053d234eaSArchibald Elliott } else {
251656ead1fScynecx // Swap byte order back
252656ead1fScynecx for (int i = 0; i < 8; i++) {
253*10dff21fSKazu Hirata HashResult[i] = llvm::byteswap(InternalState.State[i]);
254656ead1fScynecx }
25553d234eaSArchibald Elliott }
256656ead1fScynecx }
257656ead1fScynecx
final()258330268baSArgyrios Kyrtzidis std::array<uint8_t, 32> SHA256::final() {
259330268baSArgyrios Kyrtzidis union {
260330268baSArgyrios Kyrtzidis std::array<uint32_t, HASH_LENGTH / 4> HashResult;
261330268baSArgyrios Kyrtzidis std::array<uint8_t, HASH_LENGTH> ReturnResult;
262330268baSArgyrios Kyrtzidis };
26332aa35b5SKazu Hirata static_assert(sizeof(HashResult) == sizeof(ReturnResult));
264330268baSArgyrios Kyrtzidis final(HashResult);
265330268baSArgyrios Kyrtzidis return ReturnResult;
266330268baSArgyrios Kyrtzidis }
267330268baSArgyrios Kyrtzidis
result()268330268baSArgyrios Kyrtzidis std::array<uint8_t, 32> SHA256::result() {
269656ead1fScynecx auto StateToRestore = InternalState;
270656ead1fScynecx
271656ead1fScynecx auto Hash = final();
272656ead1fScynecx
273656ead1fScynecx // Restore the state
274656ead1fScynecx InternalState = StateToRestore;
275656ead1fScynecx
276656ead1fScynecx // Return pointer to hash (32 characters)
277656ead1fScynecx return Hash;
278656ead1fScynecx }
279656ead1fScynecx
hash(ArrayRef<uint8_t> Data)280656ead1fScynecx std::array<uint8_t, 32> SHA256::hash(ArrayRef<uint8_t> Data) {
281656ead1fScynecx SHA256 Hash;
282656ead1fScynecx Hash.update(Data);
283330268baSArgyrios Kyrtzidis return Hash.final();
284656ead1fScynecx }
285656ead1fScynecx
286656ead1fScynecx } // namespace llvm
287