SURFEX v8.1
General documentation of Surfex
sha256.c
Go to the documentation of this file.
1 /*
2  * SHA-256 hash in C
3  *
4  * Copyright (c) 2014 Project Nayuki
5  * http://www.nayuki.io/page/fast-sha2-hashes-in-x86-assembly
6  *
7  * (MIT License)
8  * Permission is hereby granted, free of charge, to any person obtaining a copy of
9  * this software and associated documentation files (the "Software"), to deal in
10  * the Software without restriction, including without limitation the rights to
11  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
12  * the Software, and to permit persons to whom the Software is furnished to do so,
13  * subject to the following conditions:
14  * - The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  * - The Software is provided "as is", without warranty of any kind, express or
17  * implied, including but not limited to the warranties of merchantability,
18  * fitness for a particular purpose and noninfringement. In no event shall the
19  * authors or copyright holders be liable for any claim, damages or other
20  * liability, whether in an action of contract, tort or otherwise, arising from,
21  * out of or in connection with the Software or the use or other dealings in the
22  * Software.
23  */
24 
25 #include <stdint.h>
26 
27 
28 void sha256_compress(uint32_t state[8], const uint8_t block[64]) {
29  // 32-bit right rotation
30  #define ROR(x, i) \
31  (((x) << (32 - (i))) | ((x) >> (i)))
32 
33  #define LOADSCHEDULE(i) \
34  schedule[i] = \
35  (uint32_t)block[i * 4 + 0] << 24 \
36  | (uint32_t)block[i * 4 + 1] << 16 \
37  | (uint32_t)block[i * 4 + 2] << 8 \
38  | (uint32_t)block[i * 4 + 3];
39 
40  #define SCHEDULE(i) \
41  schedule[i] = schedule[i - 16] + schedule[i - 7] \
42  + (ROR(schedule[i - 15], 7) ^ ROR(schedule[i - 15], 18) ^ (schedule[i - 15] >> 3)) \
43  + (ROR(schedule[i - 2], 17) ^ ROR(schedule[i - 2], 19) ^ (schedule[i - 2] >> 10));
44 
45  #define ROUND(a, b, c, d, e, f, g, h, i, k) \
46  h += (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + (g ^ (e & (f ^ g))) + UINT32_C(k) + schedule[i]; \
47  d += h; \
48  h += (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & (b | c)) | (b & c));
49 
50  uint32_t schedule[64];
51  LOADSCHEDULE( 0)
52  LOADSCHEDULE( 1)
53  LOADSCHEDULE( 2)
54  LOADSCHEDULE( 3)
55  LOADSCHEDULE( 4)
56  LOADSCHEDULE( 5)
57  LOADSCHEDULE( 6)
58  LOADSCHEDULE( 7)
59  LOADSCHEDULE( 8)
60  LOADSCHEDULE( 9)
61  LOADSCHEDULE(10)
62  LOADSCHEDULE(11)
63  LOADSCHEDULE(12)
64  LOADSCHEDULE(13)
65  LOADSCHEDULE(14)
66  LOADSCHEDULE(15)
67  SCHEDULE(16)
68  SCHEDULE(17)
69  SCHEDULE(18)
70  SCHEDULE(19)
71  SCHEDULE(20)
72  SCHEDULE(21)
73  SCHEDULE(22)
74  SCHEDULE(23)
75  SCHEDULE(24)
76  SCHEDULE(25)
77  SCHEDULE(26)
78  SCHEDULE(27)
79  SCHEDULE(28)
80  SCHEDULE(29)
81  SCHEDULE(30)
82  SCHEDULE(31)
83  SCHEDULE(32)
84  SCHEDULE(33)
85  SCHEDULE(34)
86  SCHEDULE(35)
87  SCHEDULE(36)
88  SCHEDULE(37)
89  SCHEDULE(38)
90  SCHEDULE(39)
91  SCHEDULE(40)
92  SCHEDULE(41)
93  SCHEDULE(42)
94  SCHEDULE(43)
95  SCHEDULE(44)
96  SCHEDULE(45)
97  SCHEDULE(46)
98  SCHEDULE(47)
99  SCHEDULE(48)
100  SCHEDULE(49)
101  SCHEDULE(50)
102  SCHEDULE(51)
103  SCHEDULE(52)
104  SCHEDULE(53)
105  SCHEDULE(54)
106  SCHEDULE(55)
107  SCHEDULE(56)
108  SCHEDULE(57)
109  SCHEDULE(58)
110  SCHEDULE(59)
111  SCHEDULE(60)
112  SCHEDULE(61)
113  SCHEDULE(62)
114  SCHEDULE(63)
115 
116  uint32_t a = state[0];
117  uint32_t b = state[1];
118  uint32_t c = state[2];
119  uint32_t d = state[3];
120  uint32_t e = state[4];
121  uint32_t f = state[5];
122  uint32_t g = state[6];
123  uint32_t h = state[7];
124  ROUND(a, b, c, d, e, f, g, h, 0, 0x428A2F98)
125  ROUND(h, a, b, c, d, e, f, g, 1, 0x71374491)
126  ROUND(g, h, a, b, c, d, e, f, 2, 0xB5C0FBCF)
127  ROUND(f, g, h, a, b, c, d, e, 3, 0xE9B5DBA5)
128  ROUND(e, f, g, h, a, b, c, d, 4, 0x3956C25B)
129  ROUND(d, e, f, g, h, a, b, c, 5, 0x59F111F1)
130  ROUND(c, d, e, f, g, h, a, b, 6, 0x923F82A4)
131  ROUND(b, c, d, e, f, g, h, a, 7, 0xAB1C5ED5)
132  ROUND(a, b, c, d, e, f, g, h, 8, 0xD807AA98)
133  ROUND(h, a, b, c, d, e, f, g, 9, 0x12835B01)
134  ROUND(g, h, a, b, c, d, e, f, 10, 0x243185BE)
135  ROUND(f, g, h, a, b, c, d, e, 11, 0x550C7DC3)
136  ROUND(e, f, g, h, a, b, c, d, 12, 0x72BE5D74)
137  ROUND(d, e, f, g, h, a, b, c, 13, 0x80DEB1FE)
138  ROUND(c, d, e, f, g, h, a, b, 14, 0x9BDC06A7)
139  ROUND(b, c, d, e, f, g, h, a, 15, 0xC19BF174)
140  ROUND(a, b, c, d, e, f, g, h, 16, 0xE49B69C1)
141  ROUND(h, a, b, c, d, e, f, g, 17, 0xEFBE4786)
142  ROUND(g, h, a, b, c, d, e, f, 18, 0x0FC19DC6)
143  ROUND(f, g, h, a, b, c, d, e, 19, 0x240CA1CC)
144  ROUND(e, f, g, h, a, b, c, d, 20, 0x2DE92C6F)
145  ROUND(d, e, f, g, h, a, b, c, 21, 0x4A7484AA)
146  ROUND(c, d, e, f, g, h, a, b, 22, 0x5CB0A9DC)
147  ROUND(b, c, d, e, f, g, h, a, 23, 0x76F988DA)
148  ROUND(a, b, c, d, e, f, g, h, 24, 0x983E5152)
149  ROUND(h, a, b, c, d, e, f, g, 25, 0xA831C66D)
150  ROUND(g, h, a, b, c, d, e, f, 26, 0xB00327C8)
151  ROUND(f, g, h, a, b, c, d, e, 27, 0xBF597FC7)
152  ROUND(e, f, g, h, a, b, c, d, 28, 0xC6E00BF3)
153  ROUND(d, e, f, g, h, a, b, c, 29, 0xD5A79147)
154  ROUND(c, d, e, f, g, h, a, b, 30, 0x06CA6351)
155  ROUND(b, c, d, e, f, g, h, a, 31, 0x14292967)
156  ROUND(a, b, c, d, e, f, g, h, 32, 0x27B70A85)
157  ROUND(h, a, b, c, d, e, f, g, 33, 0x2E1B2138)
158  ROUND(g, h, a, b, c, d, e, f, 34, 0x4D2C6DFC)
159  ROUND(f, g, h, a, b, c, d, e, 35, 0x53380D13)
160  ROUND(e, f, g, h, a, b, c, d, 36, 0x650A7354)
161  ROUND(d, e, f, g, h, a, b, c, 37, 0x766A0ABB)
162  ROUND(c, d, e, f, g, h, a, b, 38, 0x81C2C92E)
163  ROUND(b, c, d, e, f, g, h, a, 39, 0x92722C85)
164  ROUND(a, b, c, d, e, f, g, h, 40, 0xA2BFE8A1)
165  ROUND(h, a, b, c, d, e, f, g, 41, 0xA81A664B)
166  ROUND(g, h, a, b, c, d, e, f, 42, 0xC24B8B70)
167  ROUND(f, g, h, a, b, c, d, e, 43, 0xC76C51A3)
168  ROUND(e, f, g, h, a, b, c, d, 44, 0xD192E819)
169  ROUND(d, e, f, g, h, a, b, c, 45, 0xD6990624)
170  ROUND(c, d, e, f, g, h, a, b, 46, 0xF40E3585)
171  ROUND(b, c, d, e, f, g, h, a, 47, 0x106AA070)
172  ROUND(a, b, c, d, e, f, g, h, 48, 0x19A4C116)
173  ROUND(h, a, b, c, d, e, f, g, 49, 0x1E376C08)
174  ROUND(g, h, a, b, c, d, e, f, 50, 0x2748774C)
175  ROUND(f, g, h, a, b, c, d, e, 51, 0x34B0BCB5)
176  ROUND(e, f, g, h, a, b, c, d, 52, 0x391C0CB3)
177  ROUND(d, e, f, g, h, a, b, c, 53, 0x4ED8AA4A)
178  ROUND(c, d, e, f, g, h, a, b, 54, 0x5B9CCA4F)
179  ROUND(b, c, d, e, f, g, h, a, 55, 0x682E6FF3)
180  ROUND(a, b, c, d, e, f, g, h, 56, 0x748F82EE)
181  ROUND(h, a, b, c, d, e, f, g, 57, 0x78A5636F)
182  ROUND(g, h, a, b, c, d, e, f, 58, 0x84C87814)
183  ROUND(f, g, h, a, b, c, d, e, 59, 0x8CC70208)
184  ROUND(e, f, g, h, a, b, c, d, 60, 0x90BEFFFA)
185  ROUND(d, e, f, g, h, a, b, c, 61, 0xA4506CEB)
186  ROUND(c, d, e, f, g, h, a, b, 62, 0xBEF9A3F7)
187  ROUND(b, c, d, e, f, g, h, a, 63, 0xC67178F2)
188  state[0] += a;
189  state[1] += b;
190  state[2] += c;
191  state[3] += d;
192  state[4] += e;
193  state[5] += f;
194  state[6] += g;
195  state[7] += h;
196 }
197 
ERROR in a
Definition: ecsort_shared.h:90
void sha256_compress(uint32_t state[8], const uint8_t block[64])
Definition: sha256.c:28