xref: /freebsd/contrib/bearssl/src/hash/sha2small.c (revision 2aaf9152a852aba9eb2036b95f4948ee77988826)
1 /*
2  * Copyright (c) 2016 Thomas Pornin <pornin@bolet.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include "inner.h"
26 
27 #define CH(X, Y, Z)    ((((Y) ^ (Z)) & (X)) ^ (Z))
28 #define MAJ(X, Y, Z)   (((Y) & (Z)) | (((Y) | (Z)) & (X)))
29 
30 #define ROTR(x, n)    (((uint32_t)(x) << (32 - (n))) | ((uint32_t)(x) >> (n)))
31 
32 #define BSG2_0(x)      (ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22))
33 #define BSG2_1(x)      (ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25))
34 #define SSG2_0(x)      (ROTR(x, 7) ^ ROTR(x, 18) ^ (uint32_t)((x) >> 3))
35 #define SSG2_1(x)      (ROTR(x, 17) ^ ROTR(x, 19) ^ (uint32_t)((x) >> 10))
36 
37 /* see inner.h */
38 const uint32_t br_sha224_IV[8] = {
39 	0xC1059ED8, 0x367CD507, 0x3070DD17, 0xF70E5939,
40 	0xFFC00B31, 0x68581511, 0x64F98FA7, 0xBEFA4FA4
41 };
42 
43 /* see inner.h */
44 const uint32_t br_sha256_IV[8] = {
45 	0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
46 	0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19
47 };
48 
49 static const uint32_t K[64] = {
50 	0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5,
51 	0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5,
52 	0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
53 	0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174,
54 	0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC,
55 	0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
56 	0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7,
57 	0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967,
58 	0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
59 	0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85,
60 	0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3,
61 	0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
62 	0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5,
63 	0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3,
64 	0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
65 	0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2
66 };
67 
68 /* see inner.h */
69 void
br_sha2small_round(const unsigned char * buf,uint32_t * val)70 br_sha2small_round(const unsigned char *buf, uint32_t *val)
71 {
72 
73 #define SHA2_STEP(A, B, C, D, E, F, G, H, j)   do { \
74 		uint32_t T1, T2; \
75 		T1 = H + BSG2_1(E) + CH(E, F, G) + K[j] + w[j]; \
76 		T2 = BSG2_0(A) + MAJ(A, B, C); \
77 		D += T1; \
78 		H = T1 + T2; \
79 	} while (0)
80 
81 	int i;
82 	uint32_t a, b, c, d, e, f, g, h;
83 	uint32_t w[64];
84 
85 	br_range_dec32be(w, 16, buf);
86 	for (i = 16; i < 64; i ++) {
87 		w[i] = SSG2_1(w[i - 2]) + w[i - 7]
88 			+ SSG2_0(w[i - 15]) + w[i - 16];
89 	}
90 	a = val[0];
91 	b = val[1];
92 	c = val[2];
93 	d = val[3];
94 	e = val[4];
95 	f = val[5];
96 	g = val[6];
97 	h = val[7];
98 	for (i = 0; i < 64; i += 8) {
99 		SHA2_STEP(a, b, c, d, e, f, g, h, i + 0);
100 		SHA2_STEP(h, a, b, c, d, e, f, g, i + 1);
101 		SHA2_STEP(g, h, a, b, c, d, e, f, i + 2);
102 		SHA2_STEP(f, g, h, a, b, c, d, e, i + 3);
103 		SHA2_STEP(e, f, g, h, a, b, c, d, i + 4);
104 		SHA2_STEP(d, e, f, g, h, a, b, c, i + 5);
105 		SHA2_STEP(c, d, e, f, g, h, a, b, i + 6);
106 		SHA2_STEP(b, c, d, e, f, g, h, a, i + 7);
107 	}
108 	val[0] += a;
109 	val[1] += b;
110 	val[2] += c;
111 	val[3] += d;
112 	val[4] += e;
113 	val[5] += f;
114 	val[6] += g;
115 	val[7] += h;
116 
117 #if 0
118 /* obsolete */
119 #define SHA2_MEXP1(pc)   do { \
120 		W[pc] = br_dec32be(buf + ((pc) << 2)); \
121 	} while (0)
122 
123 #define SHA2_MEXP2(pc)   do { \
124 		W[(pc) & 0x0F] = SSG2_1(W[((pc) - 2) & 0x0F]) \
125 			+ W[((pc) - 7) & 0x0F] \
126 			+ SSG2_0(W[((pc) - 15) & 0x0F]) + W[(pc) & 0x0F]; \
127 	} while (0)
128 
129 #define SHA2_STEPn(n, a, b, c, d, e, f, g, h, pc)   do { \
130 		uint32_t t1, t2; \
131 		SHA2_MEXP ## n(pc); \
132 		t1 = h + BSG2_1(e) + CH(e, f, g) \
133 			+ K[pcount + (pc)] + W[(pc) & 0x0F]; \
134 		t2 = BSG2_0(a) + MAJ(a, b, c); \
135 		d += t1; \
136 		h = t1 + t2; \
137 	} while (0)
138 
139 #define SHA2_STEP1(a, b, c, d, e, f, g, h, pc) \
140 	SHA2_STEPn(1, a, b, c, d, e, f, g, h, pc)
141 #define SHA2_STEP2(a, b, c, d, e, f, g, h, pc) \
142 	SHA2_STEPn(2, a, b, c, d, e, f, g, h, pc)
143 
144 	uint32_t A, B, C, D, E, F, G, H;
145 	uint32_t W[16];
146 	unsigned pcount;
147 
148 	A = val[0];
149 	B = val[1];
150 	C = val[2];
151 	D = val[3];
152 	E = val[4];
153 	F = val[5];
154 	G = val[6];
155 	H = val[7];
156 	pcount = 0;
157 	SHA2_STEP1(A, B, C, D, E, F, G, H,  0);
158 	SHA2_STEP1(H, A, B, C, D, E, F, G,  1);
159 	SHA2_STEP1(G, H, A, B, C, D, E, F,  2);
160 	SHA2_STEP1(F, G, H, A, B, C, D, E,  3);
161 	SHA2_STEP1(E, F, G, H, A, B, C, D,  4);
162 	SHA2_STEP1(D, E, F, G, H, A, B, C,  5);
163 	SHA2_STEP1(C, D, E, F, G, H, A, B,  6);
164 	SHA2_STEP1(B, C, D, E, F, G, H, A,  7);
165 	SHA2_STEP1(A, B, C, D, E, F, G, H,  8);
166 	SHA2_STEP1(H, A, B, C, D, E, F, G,  9);
167 	SHA2_STEP1(G, H, A, B, C, D, E, F, 10);
168 	SHA2_STEP1(F, G, H, A, B, C, D, E, 11);
169 	SHA2_STEP1(E, F, G, H, A, B, C, D, 12);
170 	SHA2_STEP1(D, E, F, G, H, A, B, C, 13);
171 	SHA2_STEP1(C, D, E, F, G, H, A, B, 14);
172 	SHA2_STEP1(B, C, D, E, F, G, H, A, 15);
173 	for (pcount = 16; pcount < 64; pcount += 16) {
174 		SHA2_STEP2(A, B, C, D, E, F, G, H,  0);
175 		SHA2_STEP2(H, A, B, C, D, E, F, G,  1);
176 		SHA2_STEP2(G, H, A, B, C, D, E, F,  2);
177 		SHA2_STEP2(F, G, H, A, B, C, D, E,  3);
178 		SHA2_STEP2(E, F, G, H, A, B, C, D,  4);
179 		SHA2_STEP2(D, E, F, G, H, A, B, C,  5);
180 		SHA2_STEP2(C, D, E, F, G, H, A, B,  6);
181 		SHA2_STEP2(B, C, D, E, F, G, H, A,  7);
182 		SHA2_STEP2(A, B, C, D, E, F, G, H,  8);
183 		SHA2_STEP2(H, A, B, C, D, E, F, G,  9);
184 		SHA2_STEP2(G, H, A, B, C, D, E, F, 10);
185 		SHA2_STEP2(F, G, H, A, B, C, D, E, 11);
186 		SHA2_STEP2(E, F, G, H, A, B, C, D, 12);
187 		SHA2_STEP2(D, E, F, G, H, A, B, C, 13);
188 		SHA2_STEP2(C, D, E, F, G, H, A, B, 14);
189 		SHA2_STEP2(B, C, D, E, F, G, H, A, 15);
190 	}
191 	val[0] += A;
192 	val[1] += B;
193 	val[2] += C;
194 	val[3] += D;
195 	val[4] += E;
196 	val[5] += F;
197 	val[6] += G;
198 	val[7] += H;
199 #endif
200 }
201 
202 static void
sha2small_update(br_sha224_context * cc,const void * data,size_t len)203 sha2small_update(br_sha224_context *cc, const void *data, size_t len)
204 {
205 	const unsigned char *buf;
206 	size_t ptr;
207 
208 	buf = data;
209 	ptr = (size_t)cc->count & 63;
210 	cc->count += (uint64_t)len;
211 	while (len > 0) {
212 		size_t clen;
213 
214 		clen = 64 - ptr;
215 		if (clen > len) {
216 			clen = len;
217 		}
218 		memcpy(cc->buf + ptr, buf, clen);
219 		ptr += clen;
220 		buf += clen;
221 		len -= clen;
222 		if (ptr == 64) {
223 			br_sha2small_round(cc->buf, cc->val);
224 			ptr = 0;
225 		}
226 	}
227 }
228 
229 static void
sha2small_out(const br_sha224_context * cc,void * dst,int num)230 sha2small_out(const br_sha224_context *cc, void *dst, int num)
231 {
232 	unsigned char buf[64];
233 	uint32_t val[8];
234 	size_t ptr;
235 
236 	ptr = (size_t)cc->count & 63;
237 	memcpy(buf, cc->buf, ptr);
238 	memcpy(val, cc->val, sizeof val);
239 	buf[ptr ++] = 0x80;
240 	if (ptr > 56) {
241 		memset(buf + ptr, 0, 64 - ptr);
242 		br_sha2small_round(buf, val);
243 		memset(buf, 0, 56);
244 	} else {
245 		memset(buf + ptr, 0, 56 - ptr);
246 	}
247 	br_enc64be(buf + 56, cc->count << 3);
248 	br_sha2small_round(buf, val);
249 	br_range_enc32be(dst, val, num);
250 }
251 
252 /* see bearssl.h */
253 void
br_sha224_init(br_sha224_context * cc)254 br_sha224_init(br_sha224_context *cc)
255 {
256 	cc->vtable = &br_sha224_vtable;
257 	memcpy(cc->val, br_sha224_IV, sizeof cc->val);
258 	cc->count = 0;
259 }
260 
261 /* see bearssl.h */
262 void
br_sha224_update(br_sha224_context * cc,const void * data,size_t len)263 br_sha224_update(br_sha224_context *cc, const void *data, size_t len)
264 {
265 	sha2small_update(cc, data, len);
266 }
267 
268 /* see bearssl.h */
269 void
br_sha224_out(const br_sha224_context * cc,void * dst)270 br_sha224_out(const br_sha224_context *cc, void *dst)
271 {
272 	sha2small_out(cc, dst, 7);
273 }
274 
275 /* see bearssl.h */
276 uint64_t
br_sha224_state(const br_sha224_context * cc,void * dst)277 br_sha224_state(const br_sha224_context *cc, void *dst)
278 {
279 	br_range_enc32be(dst, cc->val, 8);
280 	return cc->count;
281 }
282 
283 /* see bearssl.h */
284 void
br_sha224_set_state(br_sha224_context * cc,const void * stb,uint64_t count)285 br_sha224_set_state(br_sha224_context *cc, const void *stb, uint64_t count)
286 {
287 	br_range_dec32be(cc->val, 8, stb);
288 	cc->count = count;
289 }
290 
291 /* see bearssl.h */
292 void
br_sha256_init(br_sha256_context * cc)293 br_sha256_init(br_sha256_context *cc)
294 {
295 	cc->vtable = &br_sha256_vtable;
296 	memcpy(cc->val, br_sha256_IV, sizeof cc->val);
297 	cc->count = 0;
298 }
299 
300 /* see bearssl.h */
301 void
br_sha256_out(const br_sha256_context * cc,void * dst)302 br_sha256_out(const br_sha256_context *cc, void *dst)
303 {
304 	sha2small_out(cc, dst, 8);
305 }
306 
307 /* see bearssl.h */
308 const br_hash_class br_sha224_vtable = {
309 	sizeof(br_sha224_context),
310 	BR_HASHDESC_ID(br_sha224_ID)
311 		| BR_HASHDESC_OUT(28)
312 		| BR_HASHDESC_STATE(32)
313 		| BR_HASHDESC_LBLEN(6)
314 		| BR_HASHDESC_MD_PADDING
315 		| BR_HASHDESC_MD_PADDING_BE,
316 	(void (*)(const br_hash_class **))&br_sha224_init,
317 	(void (*)(const br_hash_class **,
318 		const void *, size_t))&br_sha224_update,
319 	(void (*)(const br_hash_class *const *, void *))&br_sha224_out,
320 	(uint64_t (*)(const br_hash_class *const *, void *))&br_sha224_state,
321 	(void (*)(const br_hash_class **, const void *, uint64_t))
322 		&br_sha224_set_state
323 };
324 
325 /* see bearssl.h */
326 const br_hash_class br_sha256_vtable = {
327 	sizeof(br_sha256_context),
328 	BR_HASHDESC_ID(br_sha256_ID)
329 		| BR_HASHDESC_OUT(32)
330 		| BR_HASHDESC_STATE(32)
331 		| BR_HASHDESC_LBLEN(6)
332 		| BR_HASHDESC_MD_PADDING
333 		| BR_HASHDESC_MD_PADDING_BE,
334 	(void (*)(const br_hash_class **))&br_sha256_init,
335 	(void (*)(const br_hash_class **,
336 		const void *, size_t))&br_sha256_update,
337 	(void (*)(const br_hash_class *const *, void *))&br_sha256_out,
338 	(uint64_t (*)(const br_hash_class *const *, void *))&br_sha256_state,
339 	(void (*)(const br_hash_class **, const void *, uint64_t))
340 		&br_sha256_set_state
341 };
342