xref: /aosp_15_r20/external/vboot_reference/firmware/2lib/2sha256.c (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2021 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /* SHA-256 and SHA-512 implementation based on code by Oliver Gay
7  * <[email protected]> under a BSD-style license. See below.
8  */
9 
10 /*
11  * FIPS 180-2 SHA-224/256/384/512 implementation
12  * Last update: 02/02/2007
13  * Issue date:  04/30/2005
14  *
15  * Copyright (C) 2005, 2007 Olivier Gay <[email protected]>
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  * 3. Neither the name of the project nor the names of its contributors
27  *    may be used to endorse or promote products derived from this software
28  *    without specific prior written permission.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
31  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
34  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40  * SUCH DAMAGE.
41  */
42 
43 #include "2common.h"
44 #include "2sha.h"
45 #include "2sha_private.h"
46 #include "2sysincludes.h"
47 
48 #define SHFR(x, n)    (x >> n)
49 #define ROTR(x, n)   ((x >> n) | (x << ((sizeof(x) << 3) - n)))
50 #define ROTL(x, n)   ((x << n) | (x >> ((sizeof(x) << 3) - n)))
51 #define CH(x, y, z)  ((x & y) ^ (~x & z))
52 #define MAJ(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
53 
54 #define SHA256_F1(x) (ROTR(x,  2) ^ ROTR(x, 13) ^ ROTR(x, 22))
55 #define SHA256_F2(x) (ROTR(x,  6) ^ ROTR(x, 11) ^ ROTR(x, 25))
56 #define SHA256_F3(x) (ROTR(x,  7) ^ ROTR(x, 18) ^ SHFR(x,  3))
57 #define SHA256_F4(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ SHFR(x, 10))
58 
59 /* Macros used for loops unrolling */
60 
61 #define SHA256_SCR(i)						\
62 	{							\
63 		w[i] =  SHA256_F4(w[i -  2]) + w[i -  7]	\
64 			+ SHA256_F3(w[i - 15]) + w[i - 16];	\
65 	}
66 
67 #define SHA256_EXP(a, b, c, d, e, f, g, h, j)				\
68 	{								\
69 		t1 = wv[h] + SHA256_F2(wv[e]) + CH(wv[e], wv[f], wv[g]) \
70 			+ vb2_sha256_k[j] + w[j];				\
71 		t2 = SHA256_F1(wv[a]) + MAJ(wv[a], wv[b], wv[c]);       \
72 		wv[d] += t1;                                            \
73 		wv[h] = t1 + t2;                                        \
74 	}
75 
76 const uint32_t vb2_sha256_h0[8] = {
77 	0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
78 	0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
79 };
80 
81 static const uint32_t sha224_h0[8] = {
82 	0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
83 	0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
84 };
85 
86 const uint32_t vb2_sha256_k[64] = {
87 	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
88 	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
89 	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
90 	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
91 	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
92 	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
93 	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
94 	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
95 	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
96 	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
97 	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
98 	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
99 	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
100 	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
101 	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
102 	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
103 };
104 
105 /* SHA-256 implementation */
vb2_sha256_init(struct vb2_sha256_context * ctx,enum vb2_hash_algorithm algo)106 void vb2_sha256_init(struct vb2_sha256_context *ctx,
107 		     enum vb2_hash_algorithm algo)
108 {
109 	const uint32_t *h0 = algo == VB2_HASH_SHA224 ? sha224_h0 : vb2_sha256_h0;
110 
111 #ifndef UNROLL_LOOPS
112 	int i;
113 	for (i = 0; i < 8; i++) {
114 		ctx->h[i] = h0[i];
115 	}
116 #else
117 	ctx->h[0] = h0[0]; ctx->h[1] = h0[1];
118 	ctx->h[2] = h0[2]; ctx->h[3] = h0[3];
119 	ctx->h[4] = h0[4]; ctx->h[5] = h0[5];
120 	ctx->h[6] = h0[6]; ctx->h[7] = h0[7];
121 #endif /* !UNROLL_LOOPS */
122 
123 	ctx->size = 0;
124 	ctx->total_size = 0;
125 }
126 
vb2_sha256_transform(struct vb2_sha256_context * ctx,const uint8_t * message,unsigned int block_nb)127 static void vb2_sha256_transform(struct vb2_sha256_context *ctx,
128 				 const uint8_t *message,
129 				 unsigned int block_nb)
130 {
131 	/* Note that these arrays use 72*4=288 bytes of stack */
132 	uint32_t w[64];
133 	uint32_t wv[8];
134 	uint32_t t1, t2;
135 	const unsigned char *sub_block;
136 	int i;
137 
138 #ifndef UNROLL_LOOPS
139 	int j;
140 #endif
141 
142 	for (i = 0; i < (int) block_nb; i++) {
143 		sub_block = message + (i << 6);
144 
145 #ifndef UNROLL_LOOPS
146 		for (j = 0; j < 16; j++) {
147 			PACK32(&sub_block[j << 2], &w[j]);
148 		}
149 
150 		for (j = 16; j < 64; j++) {
151 			SHA256_SCR(j);
152 		}
153 
154 		for (j = 0; j < 8; j++) {
155 			wv[j] = ctx->h[j];
156 		}
157 
158 		for (j = 0; j < 64; j++) {
159 			t1 = wv[7] + SHA256_F2(wv[4]) + CH(wv[4], wv[5], wv[6])
160 				+ vb2_sha256_k[j] + w[j];
161 			t2 = SHA256_F1(wv[0]) + MAJ(wv[0], wv[1], wv[2]);
162 			wv[7] = wv[6];
163 			wv[6] = wv[5];
164 			wv[5] = wv[4];
165 			wv[4] = wv[3] + t1;
166 			wv[3] = wv[2];
167 			wv[2] = wv[1];
168 			wv[1] = wv[0];
169 			wv[0] = t1 + t2;
170 		}
171 
172 		for (j = 0; j < 8; j++) {
173 			ctx->h[j] += wv[j];
174 		}
175 #else
176 		PACK32(&sub_block[ 0], &w[ 0]); PACK32(&sub_block[ 4], &w[ 1]);
177 		PACK32(&sub_block[ 8], &w[ 2]); PACK32(&sub_block[12], &w[ 3]);
178 		PACK32(&sub_block[16], &w[ 4]); PACK32(&sub_block[20], &w[ 5]);
179 		PACK32(&sub_block[24], &w[ 6]); PACK32(&sub_block[28], &w[ 7]);
180 		PACK32(&sub_block[32], &w[ 8]); PACK32(&sub_block[36], &w[ 9]);
181 		PACK32(&sub_block[40], &w[10]); PACK32(&sub_block[44], &w[11]);
182 		PACK32(&sub_block[48], &w[12]); PACK32(&sub_block[52], &w[13]);
183 		PACK32(&sub_block[56], &w[14]); PACK32(&sub_block[60], &w[15]);
184 
185 		SHA256_SCR(16); SHA256_SCR(17); SHA256_SCR(18); SHA256_SCR(19);
186 		SHA256_SCR(20); SHA256_SCR(21); SHA256_SCR(22); SHA256_SCR(23);
187 		SHA256_SCR(24); SHA256_SCR(25); SHA256_SCR(26); SHA256_SCR(27);
188 		SHA256_SCR(28); SHA256_SCR(29); SHA256_SCR(30); SHA256_SCR(31);
189 		SHA256_SCR(32); SHA256_SCR(33); SHA256_SCR(34); SHA256_SCR(35);
190 		SHA256_SCR(36); SHA256_SCR(37); SHA256_SCR(38); SHA256_SCR(39);
191 		SHA256_SCR(40); SHA256_SCR(41); SHA256_SCR(42); SHA256_SCR(43);
192 		SHA256_SCR(44); SHA256_SCR(45); SHA256_SCR(46); SHA256_SCR(47);
193 		SHA256_SCR(48); SHA256_SCR(49); SHA256_SCR(50); SHA256_SCR(51);
194 		SHA256_SCR(52); SHA256_SCR(53); SHA256_SCR(54); SHA256_SCR(55);
195 		SHA256_SCR(56); SHA256_SCR(57); SHA256_SCR(58); SHA256_SCR(59);
196 		SHA256_SCR(60); SHA256_SCR(61); SHA256_SCR(62); SHA256_SCR(63);
197 
198 		wv[0] = ctx->h[0]; wv[1] = ctx->h[1];
199 		wv[2] = ctx->h[2]; wv[3] = ctx->h[3];
200 		wv[4] = ctx->h[4]; wv[5] = ctx->h[5];
201 		wv[6] = ctx->h[6]; wv[7] = ctx->h[7];
202 
203 		SHA256_EXP(0,1,2,3,4,5,6,7, 0); SHA256_EXP(7,0,1,2,3,4,5,6, 1);
204 		SHA256_EXP(6,7,0,1,2,3,4,5, 2); SHA256_EXP(5,6,7,0,1,2,3,4, 3);
205 		SHA256_EXP(4,5,6,7,0,1,2,3, 4); SHA256_EXP(3,4,5,6,7,0,1,2, 5);
206 		SHA256_EXP(2,3,4,5,6,7,0,1, 6); SHA256_EXP(1,2,3,4,5,6,7,0, 7);
207 		SHA256_EXP(0,1,2,3,4,5,6,7, 8); SHA256_EXP(7,0,1,2,3,4,5,6, 9);
208 		SHA256_EXP(6,7,0,1,2,3,4,5,10); SHA256_EXP(5,6,7,0,1,2,3,4,11);
209 		SHA256_EXP(4,5,6,7,0,1,2,3,12); SHA256_EXP(3,4,5,6,7,0,1,2,13);
210 		SHA256_EXP(2,3,4,5,6,7,0,1,14); SHA256_EXP(1,2,3,4,5,6,7,0,15);
211 		SHA256_EXP(0,1,2,3,4,5,6,7,16); SHA256_EXP(7,0,1,2,3,4,5,6,17);
212 		SHA256_EXP(6,7,0,1,2,3,4,5,18); SHA256_EXP(5,6,7,0,1,2,3,4,19);
213 		SHA256_EXP(4,5,6,7,0,1,2,3,20); SHA256_EXP(3,4,5,6,7,0,1,2,21);
214 		SHA256_EXP(2,3,4,5,6,7,0,1,22); SHA256_EXP(1,2,3,4,5,6,7,0,23);
215 		SHA256_EXP(0,1,2,3,4,5,6,7,24); SHA256_EXP(7,0,1,2,3,4,5,6,25);
216 		SHA256_EXP(6,7,0,1,2,3,4,5,26); SHA256_EXP(5,6,7,0,1,2,3,4,27);
217 		SHA256_EXP(4,5,6,7,0,1,2,3,28); SHA256_EXP(3,4,5,6,7,0,1,2,29);
218 		SHA256_EXP(2,3,4,5,6,7,0,1,30); SHA256_EXP(1,2,3,4,5,6,7,0,31);
219 		SHA256_EXP(0,1,2,3,4,5,6,7,32); SHA256_EXP(7,0,1,2,3,4,5,6,33);
220 		SHA256_EXP(6,7,0,1,2,3,4,5,34); SHA256_EXP(5,6,7,0,1,2,3,4,35);
221 		SHA256_EXP(4,5,6,7,0,1,2,3,36); SHA256_EXP(3,4,5,6,7,0,1,2,37);
222 		SHA256_EXP(2,3,4,5,6,7,0,1,38); SHA256_EXP(1,2,3,4,5,6,7,0,39);
223 		SHA256_EXP(0,1,2,3,4,5,6,7,40); SHA256_EXP(7,0,1,2,3,4,5,6,41);
224 		SHA256_EXP(6,7,0,1,2,3,4,5,42); SHA256_EXP(5,6,7,0,1,2,3,4,43);
225 		SHA256_EXP(4,5,6,7,0,1,2,3,44); SHA256_EXP(3,4,5,6,7,0,1,2,45);
226 		SHA256_EXP(2,3,4,5,6,7,0,1,46); SHA256_EXP(1,2,3,4,5,6,7,0,47);
227 		SHA256_EXP(0,1,2,3,4,5,6,7,48); SHA256_EXP(7,0,1,2,3,4,5,6,49);
228 		SHA256_EXP(6,7,0,1,2,3,4,5,50); SHA256_EXP(5,6,7,0,1,2,3,4,51);
229 		SHA256_EXP(4,5,6,7,0,1,2,3,52); SHA256_EXP(3,4,5,6,7,0,1,2,53);
230 		SHA256_EXP(2,3,4,5,6,7,0,1,54); SHA256_EXP(1,2,3,4,5,6,7,0,55);
231 		SHA256_EXP(0,1,2,3,4,5,6,7,56); SHA256_EXP(7,0,1,2,3,4,5,6,57);
232 		SHA256_EXP(6,7,0,1,2,3,4,5,58); SHA256_EXP(5,6,7,0,1,2,3,4,59);
233 		SHA256_EXP(4,5,6,7,0,1,2,3,60); SHA256_EXP(3,4,5,6,7,0,1,2,61);
234 		SHA256_EXP(2,3,4,5,6,7,0,1,62); SHA256_EXP(1,2,3,4,5,6,7,0,63);
235 
236 		ctx->h[0] += wv[0]; ctx->h[1] += wv[1];
237 		ctx->h[2] += wv[2]; ctx->h[3] += wv[3];
238 		ctx->h[4] += wv[4]; ctx->h[5] += wv[5];
239 		ctx->h[6] += wv[6]; ctx->h[7] += wv[7];
240 #endif /* !UNROLL_LOOPS */
241 	}
242 }
243 
vb2_sha256_update(struct vb2_sha256_context * ctx,const uint8_t * data,uint32_t size)244 void vb2_sha256_update(struct vb2_sha256_context *ctx,
245 		       const uint8_t *data,
246 		       uint32_t size)
247 {
248 	unsigned int block_nb;
249 	unsigned int new_size, rem_size, tmp_size;
250 	const uint8_t *shifted_data;
251 
252 	tmp_size = VB2_SHA256_BLOCK_SIZE - ctx->size;
253 	rem_size = size < tmp_size ? size : tmp_size;
254 
255 	memcpy(&ctx->block[ctx->size], data, rem_size);
256 
257 	if (ctx->size + size < VB2_SHA256_BLOCK_SIZE) {
258 		ctx->size += size;
259 		return;
260 	}
261 
262 	new_size = size - rem_size;
263 	block_nb = new_size / VB2_SHA256_BLOCK_SIZE;
264 
265 	shifted_data = data + rem_size;
266 
267 	vb2_sha256_transform(ctx, ctx->block, 1);
268 	vb2_sha256_transform(ctx, shifted_data, block_nb);
269 
270 	rem_size = new_size % VB2_SHA256_BLOCK_SIZE;
271 
272 	memcpy(ctx->block, &shifted_data[block_nb << 6],
273 	       rem_size);
274 
275 	ctx->size = rem_size;
276 	ctx->total_size += (block_nb + 1) << 6;
277 }
278 
vb2_sha256_finalize(struct vb2_sha256_context * ctx,uint8_t * digest,enum vb2_hash_algorithm algo)279 void vb2_sha256_finalize(struct vb2_sha256_context *ctx, uint8_t *digest,
280 			 enum vb2_hash_algorithm algo)
281 {
282 	unsigned int block_nb;
283 	unsigned int pm_size;
284 	unsigned int size_b;
285 #ifndef UNROLL_LOOPS
286 	int i;
287 #endif
288 
289 	block_nb = (1 + ((VB2_SHA256_BLOCK_SIZE - 9)
290 			 < (ctx->size % VB2_SHA256_BLOCK_SIZE)));
291 
292 	size_b = (ctx->total_size + ctx->size) << 3;
293 	pm_size = block_nb << 6;
294 
295 	memset(ctx->block + ctx->size, 0, pm_size - ctx->size);
296 	ctx->block[ctx->size] = 0x80;
297 	UNPACK32(size_b, ctx->block + pm_size - 4);
298 
299 	vb2_sha256_transform(ctx, ctx->block, block_nb);
300 
301 #ifndef UNROLL_LOOPS
302 	for (i = 0 ; i < (algo == VB2_HASH_SHA224 ? 7 : 8); i++) {
303 		UNPACK32(ctx->h[i], &digest[i << 2]);
304 	}
305 #else
306 	UNPACK32(ctx->h[0], &digest[ 0]);
307 	UNPACK32(ctx->h[1], &digest[ 4]);
308 	UNPACK32(ctx->h[2], &digest[ 8]);
309 	UNPACK32(ctx->h[3], &digest[12]);
310 	UNPACK32(ctx->h[4], &digest[16]);
311 	UNPACK32(ctx->h[5], &digest[20]);
312 	UNPACK32(ctx->h[6], &digest[24]);
313 	if (algo != VB2_HASH_SHA224) {
314 		UNPACK32(ctx->h[7], &digest[28]);
315 	}
316 #endif /* !UNROLL_LOOPS */
317 }
318 
vb2_sha256_extend(const uint8_t * from,const uint8_t * by,uint8_t * to)319 void vb2_sha256_extend(const uint8_t *from, const uint8_t *by, uint8_t *to)
320 {
321 	struct vb2_sha256_context dc;
322 	int i;
323 
324 	for (i = 0; i < 8; i++) {
325 		 PACK32(from, &dc.h[i]);
326 		 from += 4;
327 	}
328 
329 	vb2_sha256_transform(&dc, by, 1);
330 
331 	for (i = 0; i < 8; i++) {
332 		 UNPACK32(dc.h[i], to);
333 		 to += 4;
334 	}
335 }
336