xref: /aosp_15_r20/external/erofs-utils/lib/sha256.c (revision 33b1fccf6a0fada2c2875d400ed01119b7676ee5)
1 // SPDX-License-Identifier: Unlicense
2 /*
3  * sha256.c --- The sha256 algorithm
4  *
5  * (copied from LibTomCrypt with adaption.)
6  */
7 #include "sha256.h"
8 #include <string.h>
9 
10 /* This is based on SHA256 implementation in LibTomCrypt that was released into
11  * public domain by Tom St Denis. */
12 /* the K array */
13 static const unsigned long K[64] = {
14 	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
15 	0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
16 	0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
17 	0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
18 	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
19 	0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
20 	0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
21 	0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
22 	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
23 	0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
24 	0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
25 	0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
26 	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
27 };
28 /* Various logical functions */
29 #define RORc(x, y) \
30 ( ((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \
31    ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
32 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
33 #define Maj(x,y,z)      (((x | y) & z) | (x & y))
34 #define S(x, n)         RORc((x), (n))
35 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
36 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
37 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
38 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
39 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
40 #ifndef MIN
41 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
42 #endif
43 
44 #define STORE64H(x, y) \
45 	do { \
46 		(y)[0] = (unsigned char)(((x)>>56)&255);\
47 		(y)[1] = (unsigned char)(((x)>>48)&255);\
48 		(y)[2] = (unsigned char)(((x)>>40)&255);\
49 		(y)[3] = (unsigned char)(((x)>>32)&255);\
50 		(y)[4] = (unsigned char)(((x)>>24)&255);\
51 		(y)[5] = (unsigned char)(((x)>>16)&255);\
52 		(y)[6] = (unsigned char)(((x)>>8)&255);\
53 		(y)[7] = (unsigned char)((x)&255); } while(0)
54 
55 #define STORE32H(x, y)                                                                     \
56   do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
57        (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
58 
59 #define LOAD32H(x, y)                            \
60   do { x = ((u32)((y)[0] & 255)<<24) | \
61            ((u32)((y)[1] & 255)<<16) | \
62            ((u32)((y)[2] & 255)<<8)  | \
63            ((u32)((y)[3] & 255)); } while(0)
64 
65 /* compress 512-bits */
sha256_compress(struct sha256_state * md,unsigned char * buf)66 static int sha256_compress(struct sha256_state *md, unsigned char *buf)
67 {
68 	u32 S[8], W[64], t0, t1;
69 	u32 t;
70 	int i;
71 	/* copy state into S */
72 	for (i = 0; i < 8; i++) {
73 		S[i] = md->state[i];
74 	}
75 	/* copy the state into 512-bits into W[0..15] */
76 	for (i = 0; i < 16; i++)
77 		LOAD32H(W[i], buf + (4 * i));
78 	/* fill W[16..63] */
79 	for (i = 16; i < 64; i++) {
80 		W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
81 			W[i - 16];
82 	}
83 	/* Compress */
84 #define RND(a,b,c,d,e,f,g,h,i)                          \
85 	t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];	\
86 	t1 = Sigma0(a) + Maj(a, b, c);			\
87 	d += t0;					\
88 	h  = t0 + t1;
89 	for (i = 0; i < 64; ++i) {
90 		RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
91 		t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
92 		S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
93 	}
94 	/* feedback */
95 	for (i = 0; i < 8; i++) {
96 		md->state[i] = md->state[i] + S[i];
97 	}
98 	return 0;
99 }
100 /* Initialize the hash state */
erofs_sha256_init(struct sha256_state * md)101 void erofs_sha256_init(struct sha256_state *md)
102 {
103 	md->curlen = 0;
104 	md->length = 0;
105 	md->state[0] = 0x6A09E667UL;
106 	md->state[1] = 0xBB67AE85UL;
107 	md->state[2] = 0x3C6EF372UL;
108 	md->state[3] = 0xA54FF53AUL;
109 	md->state[4] = 0x510E527FUL;
110 	md->state[5] = 0x9B05688CUL;
111 	md->state[6] = 0x1F83D9ABUL;
112 	md->state[7] = 0x5BE0CD19UL;
113 }
114 /**
115    Process a block of memory though the hash
116    @param md     The hash state
117    @param in     The data to hash
118    @param inlen  The length of the data (octets)
119    @return CRYPT_OK if successful
120 */
erofs_sha256_process(struct sha256_state * md,const unsigned char * in,unsigned long inlen)121 int erofs_sha256_process(struct sha256_state *md,
122 		const unsigned char *in, unsigned long inlen)
123 {
124 	unsigned long n;
125 #define block_size 64
126 	if (md->curlen > sizeof(md->buf))
127 		return -1;
128 	while (inlen > 0) {
129 		if (md->curlen == 0 && inlen >= block_size) {
130 			if (sha256_compress(md, (unsigned char *) in) < 0)
131 				return -1;
132 			md->length += block_size * 8;
133 			in += block_size;
134 			inlen -= block_size;
135 		} else {
136 			n = MIN(inlen, (block_size - md->curlen));
137 			memcpy(md->buf + md->curlen, in, n);
138 			md->curlen += n;
139 			in += n;
140 			inlen -= n;
141 			if (md->curlen == block_size) {
142 				if (sha256_compress(md, md->buf) < 0)
143 					return -1;
144 				md->length += 8 * block_size;
145 				md->curlen = 0;
146 			}
147 		}
148 	}
149 	return 0;
150 }
151 /**
152    Terminate the hash to get the digest
153    @param md  The hash state
154    @param out [out] The destination of the hash (32 bytes)
155    @return CRYPT_OK if successful
156 */
erofs_sha256_done(struct sha256_state * md,unsigned char * out)157 int erofs_sha256_done(struct sha256_state *md, unsigned char *out)
158 {
159 	int i;
160 	if (md->curlen >= sizeof(md->buf))
161 		return -1;
162 	/* increase the length of the message */
163 	md->length += md->curlen * 8;
164 	/* append the '1' bit */
165 	md->buf[md->curlen++] = (unsigned char) 0x80;
166 	/* if the length is currently above 56 bytes we append zeros
167 	 * then compress.  Then we can fall back to padding zeros and length
168 	 * encoding like normal.
169 	 */
170 	if (md->curlen > 56) {
171 		while (md->curlen < 64) {
172 			md->buf[md->curlen++] = (unsigned char) 0;
173 		}
174 		sha256_compress(md, md->buf);
175 		md->curlen = 0;
176 	}
177 	/* pad upto 56 bytes of zeroes */
178 	while (md->curlen < 56) {
179 		md->buf[md->curlen++] = (unsigned char) 0;
180 	}
181 	/* store length */
182 	STORE64H(md->length, md->buf+56);
183 	sha256_compress(md, md->buf);
184 	/* copy output */
185 	for (i = 0; i < 8; i++)
186 		STORE32H(md->state[i], out + (4 * i));
187 	return 0;
188 }
189 
erofs_sha256(const unsigned char * in,unsigned long in_size,unsigned char out[32])190 void erofs_sha256(const unsigned char *in, unsigned long in_size,
191 		  unsigned char out[32])
192 {
193 	struct sha256_state md;
194 
195 	erofs_sha256_init(&md);
196 	erofs_sha256_process(&md, in, in_size);
197 	erofs_sha256_done(&md, out);
198 }
199 
200 #ifdef UNITTEST
201 #include <stdio.h>
202 
203 static const struct {
204 	char *msg;
205 	unsigned char hash[32];
206 } tests[] = {
207 	{ "",
208 	  { 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
209 	    0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
210 	    0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
211 	    0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 }
212 	},
213 	{ "abc",
214 	  { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
215 	    0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
216 	    0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
217 	    0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
218 	},
219 	{ "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
220 	  { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
221 	    0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
222 	    0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
223 	    0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
224 	},
225 };
226 
main(int argc,char ** argv)227 int main(int argc, char **argv)
228 {
229 	int i;
230 	int errors = 0;
231 	unsigned char tmp[32];
232 
233 	for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
234 		unsigned char *msg = (unsigned char *) tests[i].msg;
235 		int len = strlen(tests[i].msg);
236 
237 		erofs_sha256(msg, len, tmp);
238 		printf("SHA256 test message %d: ", i);
239 		if (memcmp(tmp, tests[i].hash, 32) != 0) {
240 			printf("FAILED\n");
241 			errors++;
242 		} else
243 			printf("OK\n");
244 	}
245 	return errors;
246 }
247 
248 #endif /* UNITTEST */
249