xref: /netbsd-src/external/bsd/wpa/dist/src/crypto/sha256-internal.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*
2  * SHA-256 hash implementation and interface functions
3  * Copyright (c) 2003-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14 
15 #include "includes.h"
16 
17 #include "common.h"
18 #include "sha256.h"
19 #include "crypto.h"
20 
21 #define SHA256_BLOCK_SIZE 64
22 
23 struct sha256_state {
24 	u64 length;
25 	u32 state[8], curlen;
26 	u8 buf[SHA256_BLOCK_SIZE];
27 };
28 
29 static void sha256_init(struct sha256_state *md);
30 static int sha256_process(struct sha256_state *md, const unsigned char *in,
31 			  unsigned long inlen);
32 static int sha256_done(struct sha256_state *md, unsigned char *out);
33 
34 
35 /**
36  * sha256_vector - SHA256 hash for data vector
37  * @num_elem: Number of elements in the data vector
38  * @addr: Pointers to the data areas
39  * @len: Lengths of the data blocks
40  * @mac: Buffer for the hash
41  * Returns: 0 on success, -1 of failure
42  */
43 int sha256_vector(size_t num_elem, const u8 *addr[], const size_t *len,
44 		  u8 *mac)
45 {
46 	struct sha256_state ctx;
47 	size_t i;
48 
49 	sha256_init(&ctx);
50 	for (i = 0; i < num_elem; i++)
51 		if (sha256_process(&ctx, addr[i], len[i]))
52 			return -1;
53 	if (sha256_done(&ctx, mac))
54 		return -1;
55 	return 0;
56 }
57 
58 
59 /* ===== start - public domain SHA256 implementation ===== */
60 
61 /* This is based on SHA256 implementation in LibTomCrypt that was released into
62  * public domain by Tom St Denis. */
63 
64 /* the K array */
65 static const unsigned long K[64] = {
66 	0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
67 	0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
68 	0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
69 	0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
70 	0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
71 	0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
72 	0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
73 	0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
74 	0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
75 	0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
76 	0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
77 	0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
78 	0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
79 };
80 
81 
82 /* Various logical functions */
83 #define RORc(x, y) \
84 ( ((((unsigned long) (x) & 0xFFFFFFFFUL) >> (unsigned long) ((y) & 31)) | \
85    ((unsigned long) (x) << (unsigned long) (32 - ((y) & 31)))) & 0xFFFFFFFFUL)
86 #define Ch(x,y,z)       (z ^ (x & (y ^ z)))
87 #define Maj(x,y,z)      (((x | y) & z) | (x & y))
88 #define S(x, n)         RORc((x), (n))
89 #define R(x, n)         (((x)&0xFFFFFFFFUL)>>(n))
90 #define Sigma0(x)       (S(x, 2) ^ S(x, 13) ^ S(x, 22))
91 #define Sigma1(x)       (S(x, 6) ^ S(x, 11) ^ S(x, 25))
92 #define Gamma0(x)       (S(x, 7) ^ S(x, 18) ^ R(x, 3))
93 #define Gamma1(x)       (S(x, 17) ^ S(x, 19) ^ R(x, 10))
94 #ifndef MIN
95 #define MIN(x, y) (((x) < (y)) ? (x) : (y))
96 #endif
97 
98 /* compress 512-bits */
99 static int sha256_compress(struct sha256_state *md, unsigned char *buf)
100 {
101 	u32 S[8], W[64], t0, t1;
102 	u32 t;
103 	int i;
104 
105 	/* copy state into S */
106 	for (i = 0; i < 8; i++) {
107 		S[i] = md->state[i];
108 	}
109 
110 	/* copy the state into 512-bits into W[0..15] */
111 	for (i = 0; i < 16; i++)
112 		W[i] = WPA_GET_BE32(buf + (4 * i));
113 
114 	/* fill W[16..63] */
115 	for (i = 16; i < 64; i++) {
116 		W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) +
117 			W[i - 16];
118 	}
119 
120 	/* Compress */
121 #define RND(a,b,c,d,e,f,g,h,i)                          \
122 	t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i];	\
123 	t1 = Sigma0(a) + Maj(a, b, c);			\
124 	d += t0;					\
125 	h  = t0 + t1;
126 
127 	for (i = 0; i < 64; ++i) {
128 		RND(S[0], S[1], S[2], S[3], S[4], S[5], S[6], S[7], i);
129 		t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
130 		S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
131 	}
132 
133 	/* feedback */
134 	for (i = 0; i < 8; i++) {
135 		md->state[i] = md->state[i] + S[i];
136 	}
137 	return 0;
138 }
139 
140 
141 /* Initialize the hash state */
142 static void sha256_init(struct sha256_state *md)
143 {
144 	md->curlen = 0;
145 	md->length = 0;
146 	md->state[0] = 0x6A09E667UL;
147 	md->state[1] = 0xBB67AE85UL;
148 	md->state[2] = 0x3C6EF372UL;
149 	md->state[3] = 0xA54FF53AUL;
150 	md->state[4] = 0x510E527FUL;
151 	md->state[5] = 0x9B05688CUL;
152 	md->state[6] = 0x1F83D9ABUL;
153 	md->state[7] = 0x5BE0CD19UL;
154 }
155 
156 /**
157    Process a block of memory though the hash
158    @param md     The hash state
159    @param in     The data to hash
160    @param inlen  The length of the data (octets)
161    @return CRYPT_OK if successful
162 */
163 static int sha256_process(struct sha256_state *md, const unsigned char *in,
164 			  unsigned long inlen)
165 {
166 	unsigned long n;
167 
168 	if (md->curlen >= sizeof(md->buf))
169 		return -1;
170 
171 	while (inlen > 0) {
172 		if (md->curlen == 0 && inlen >= SHA256_BLOCK_SIZE) {
173 			if (sha256_compress(md, (unsigned char *) in) < 0)
174 				return -1;
175 			md->length += SHA256_BLOCK_SIZE * 8;
176 			in += SHA256_BLOCK_SIZE;
177 			inlen -= SHA256_BLOCK_SIZE;
178 		} else {
179 			n = MIN(inlen, (SHA256_BLOCK_SIZE - md->curlen));
180 			os_memcpy(md->buf + md->curlen, in, n);
181 			md->curlen += n;
182 			in += n;
183 			inlen -= n;
184 			if (md->curlen == SHA256_BLOCK_SIZE) {
185 				if (sha256_compress(md, md->buf) < 0)
186 					return -1;
187 				md->length += 8 * SHA256_BLOCK_SIZE;
188 				md->curlen = 0;
189 			}
190 		}
191 	}
192 
193 	return 0;
194 }
195 
196 
197 /**
198    Terminate the hash to get the digest
199    @param md  The hash state
200    @param out [out] The destination of the hash (32 bytes)
201    @return CRYPT_OK if successful
202 */
203 static int sha256_done(struct sha256_state *md, unsigned char *out)
204 {
205 	int i;
206 
207 	if (md->curlen >= sizeof(md->buf))
208 		return -1;
209 
210 	/* increase the length of the message */
211 	md->length += md->curlen * 8;
212 
213 	/* append the '1' bit */
214 	md->buf[md->curlen++] = (unsigned char) 0x80;
215 
216 	/* if the length is currently above 56 bytes we append zeros
217 	 * then compress.  Then we can fall back to padding zeros and length
218 	 * encoding like normal.
219 	 */
220 	if (md->curlen > 56) {
221 		while (md->curlen < SHA256_BLOCK_SIZE) {
222 			md->buf[md->curlen++] = (unsigned char) 0;
223 		}
224 		sha256_compress(md, md->buf);
225 		md->curlen = 0;
226 	}
227 
228 	/* pad up to 56 bytes of zeroes */
229 	while (md->curlen < 56) {
230 		md->buf[md->curlen++] = (unsigned char) 0;
231 	}
232 
233 	/* store length */
234 	WPA_PUT_BE64(md->buf + 56, md->length);
235 	sha256_compress(md, md->buf);
236 
237 	/* copy output */
238 	for (i = 0; i < 8; i++)
239 		WPA_PUT_BE32(out + (4 * i), md->state[i]);
240 
241 	return 0;
242 }
243 
244 /* ===== end - public domain SHA256 implementation ===== */
245