1 /* SHA256-based Unix crypt implementation.
2 Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>.  */
3 
4 #include "sha256crypt.h"
5 
6 #ifdef __linux__
7 	#include <endian.h>
8 #elif __hpux
9 /* Nothing to do in HP-UX */
10 #elif _AIX
11 /* Nothing to do in AIX */
12 #else
13 	#if defined(ZBX_OLD_SOLARIS)
14 		#include <sys/isa_defs.h>
15 	#else
16 		#include <machine/endian.h>
17 	#endif
18 #endif
19 #include <errno.h>
20 #include <limits.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/param.h>
26 #include <sys/types.h>
27 
28 
29 /* Structure to save state of computation between the single steps.  */
30 struct sha256_ctx
31 {
32 	uint32_t H[8];
33 
34 	uint32_t total[2];
35 	uint32_t buflen;
36 	char buffer[128];	/* NB: always correctly aligned for uint32_t.  */
37 };
38 
39 
40 #if __BYTE_ORDER == __LITTLE_ENDIAN
41 # define SWAP(n) \
42 	(((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
43 #else
44 # define SWAP(n) (n)
45 #endif
46 
47 
48 /* This array contains the bytes used to pad the buffer to the next
49 64-byte boundary.  (FIPS 180-2:5.1.1)  */
50 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ...  */ };
51 
52 
53 /* Constants for SHA256 from FIPS 180-2:4.2.2.  */
54 static const uint32_t K[64] =
55 {
56 	0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
57 	0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
58 	0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
59 	0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
60 	0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
61 	0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
62 	0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
63 	0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
64 	0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
65 	0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
66 	0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
67 	0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
68 	0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
69 	0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
70 	0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
71 	0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
72 };
73 
74 
75 /* Process LEN bytes of BUFFER, accumulating context into CTX.
76 It is assumed that LEN % 64 == 0.  */
77 static void
sha256_process_block(const void * buffer,size_t len,struct sha256_ctx * ctx)78 sha256_process_block (const void *buffer, size_t len, struct sha256_ctx *ctx)
79 {
80 	const uint32_t *words = buffer;
81 	size_t nwords = len / sizeof (uint32_t);
82 	uint32_t a = ctx->H[0];
83 	uint32_t b = ctx->H[1];
84 	uint32_t c = ctx->H[2];
85 	uint32_t d = ctx->H[3];
86 	uint32_t e = ctx->H[4];
87 	uint32_t f = ctx->H[5];
88 	uint32_t g = ctx->H[6];
89 	uint32_t h = ctx->H[7];
90 
91 /* First increment the byte count.  FIPS 180-2 specifies the possible
92 length of the file up to 2^64 bits.  Here we only compute the
93 number of bytes.  Do a double word increment.  */
94 	ctx->total[0] += len;
95 	if (ctx->total[0] < len)
96 		++ctx->total[1];
97 
98 /* Process all bytes in the buffer with 64 bytes in each round of
99 the loop.  */
100 	while (nwords > 0)
101 	{
102 		uint32_t W[64];
103 		uint32_t a_save = a;
104 		uint32_t b_save = b;
105 		uint32_t c_save = c;
106 		uint32_t d_save = d;
107 		uint32_t e_save = e;
108 		uint32_t f_save = f;
109 		uint32_t g_save = g;
110 		uint32_t h_save = h;
111 
112 /* Operators defined in FIPS 180-2:4.1.2.  */
113 #define Ch(x, y, z) ((x & y) ^ (~x & z))
114 #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z))
115 #define S0(x) (CYCLIC (x, 2) ^ CYCLIC (x, 13) ^ CYCLIC (x, 22))
116 #define S1(x) (CYCLIC (x, 6) ^ CYCLIC (x, 11) ^ CYCLIC (x, 25))
117 #define R0(x) (CYCLIC (x, 7) ^ CYCLIC (x, 18) ^ (x >> 3))
118 #define R1(x) (CYCLIC (x, 17) ^ CYCLIC (x, 19) ^ (x >> 10))
119 
120 /* It is unfortunate that C does not provide an operator for
121 cyclic rotation.  Hope the C compiler is smart enough.  */
122 #define CYCLIC(w, s) ((w >> s) | (w << (32 - s)))
123 
124 		unsigned int t = 0;
125 
126 		/* Compute the message schedule according to FIPS 180-2:6.2.2 step 2.  */
127 		for (t = 0; t < 16; ++t)
128 		{
129 			W[t] = SWAP (*words);
130 			++words;
131 		}
132 
133 		for (t = 16; t < 64; ++t)
134 			W[t] = R1 (W[t - 2]) + W[t - 7] + R0 (W[t - 15]) + W[t - 16];
135 
136 		/* The actual computation according to FIPS 180-2:6.2.2 step 3.  */
137 		for (t = 0; t < 64; ++t)
138 		{
139 			uint32_t T1 = h + S1 (e) + Ch (e, f, g) + K[t] + W[t];
140 			uint32_t T2 = S0 (a) + Maj (a, b, c);
141 			h = g;
142 			g = f;
143 			f = e;
144 			e = d + T1;
145 			d = c;
146 			c = b;
147 			b = a;
148 			a = T1 + T2;
149 		}
150 
151 		/* Add the starting values of the context according to FIPS 180-2:6.2.2
152 		 * step 4.  */
153 		a += a_save;
154 		b += b_save;
155 		c += c_save;
156 		d += d_save;
157 		e += e_save;
158 		f += f_save;
159 		g += g_save;
160 		h += h_save;
161 
162 		/* Prepare for the next round.  */
163 		nwords -= 16;
164 	}
165 
166 	/* Put checksum in context given as argument.  */
167 	ctx->H[0] = a;
168 	ctx->H[1] = b;
169 	ctx->H[2] = c;
170 	ctx->H[3] = d;
171 	ctx->H[4] = e;
172 	ctx->H[5] = f;
173 	ctx->H[6] = g;
174 	ctx->H[7] = h;
175 }
176 
177 
178 /* Initialize structure containing state of computation.
179 (FIPS 180-2:5.3.2)  */
180 static void
sha256_init_ctx(struct sha256_ctx * ctx)181 sha256_init_ctx (struct sha256_ctx *ctx)
182 {
183 	ctx->H[0] = 0x6a09e667;
184 	ctx->H[1] = 0xbb67ae85;
185 	ctx->H[2] = 0x3c6ef372;
186 	ctx->H[3] = 0xa54ff53a;
187 	ctx->H[4] = 0x510e527f;
188 	ctx->H[5] = 0x9b05688c;
189 	ctx->H[6] = 0x1f83d9ab;
190 	ctx->H[7] = 0x5be0cd19;
191 
192 	ctx->total[0] = ctx->total[1] = 0;
193 	ctx->buflen = 0;
194 }
195 
196 
197 /* Process the remaining bytes in the internal buffer and the usual
198 prolog according to the standard and write the result to RESBUF.
199 
200 IMPORTANT: On some systems it is required that RESBUF is correctly
201 aligned for a 32 bits value.  */
202 static void *
sha256_finish_ctx(struct sha256_ctx * ctx,void * resbuf)203 sha256_finish_ctx (struct sha256_ctx *ctx, void *resbuf)
204 {
205 	/* Take yet unprocessed bytes into account.  */
206 	uint32_t bytes = ctx->buflen;
207 	size_t pad;
208 	unsigned int i = 0;
209 
210 	/* Now count remaining bytes.  */
211 	ctx->total[0] += bytes;
212 	if (ctx->total[0] < bytes)
213 		++ctx->total[1];
214 
215 	pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
216 	memcpy (&ctx->buffer[bytes], fillbuf, pad);
217 
218 	/* Put the 64-bit file length in *bits* at the end of the buffer.  */
219 	*(uint32_t *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 3);
220 	*(uint32_t *) &ctx->buffer[bytes + pad] = SWAP ((ctx->total[1] << 3) |
221 							(ctx->total[0] >> 29));
222 
223 	/* Process last bytes.  */
224 	sha256_process_block (ctx->buffer, bytes + pad + 8, ctx);
225 
226 	/* Put result from CTX in first 32 bytes following RESBUF.  */
227 	for (i = 0; i < 8; ++i)
228 		((uint32_t *) resbuf)[i] = SWAP (ctx->H[i]);
229 
230 	return resbuf;
231 }
232 
233 
234 static void
sha256_process_bytes(const void * buffer,size_t len,struct sha256_ctx * ctx)235 sha256_process_bytes (const void *buffer, size_t len, struct sha256_ctx *ctx)
236 {
237 	/* When we already have some bits in our internal buffer concatenate
238 	both inputs first.  */
239 	if (ctx->buflen != 0)
240 	{
241 		size_t left_over = ctx->buflen;
242 		size_t add = 128 - left_over > len ? len : 128 - left_over;
243 
244 		memcpy (&ctx->buffer[left_over], buffer, add);
245 		ctx->buflen += add;
246 
247 		if (ctx->buflen > 64)
248 		{
249 			sha256_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
250 
251 			ctx->buflen &= 63;
252 			/* The regions in the following copy operation cannot overlap.  */
253 			memcpy (ctx->buffer, &ctx->buffer[(left_over + add) & ~63],
254 					ctx->buflen);
255 	}
256 
257 		buffer = (const char *) buffer + add;
258 		len -= add;
259 	}
260 
261 	/* Process available complete blocks.  */
262 	if (len >= 64)
263 	{
264 /* To check alignment gcc has an appropriate operator.  Other
265 compilers don't.  */
266 #if __GNUC__ >= 2
267 # define UNALIGNED_P(p) (((uintptr_t) p) % __alignof__ (uint32_t) != 0)
268 #else
269 # define UNALIGNED_P(p) (((uintptr_t) p) % sizeof (uint32_t) != 0)
270 #endif
271 		if (UNALIGNED_P (buffer))
272 			while (len > 64)
273 			{
274 				sha256_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
275 				buffer = (const char *) buffer + 64;
276 				len -= 64;
277 			}
278 		else
279 		{
280 			sha256_process_block (buffer, len & ~63, ctx);
281 			buffer = (const char *) buffer + (len & ~63);
282 			len &= 63;
283 		}
284 	}
285 
286 	/* Move remaining bytes into internal buffer.  */
287 	if (len > 0)
288 	{
289 		size_t left_over = ctx->buflen;
290 
291 		memcpy (&ctx->buffer[left_over], buffer, len);
292 		left_over += len;
293 		if (left_over >= 64)
294 		{
295 			sha256_process_block (ctx->buffer, 64, ctx);
296 			left_over -= 64;
297 			memcpy (ctx->buffer, &ctx->buffer[64], left_over);
298 		}
299 		ctx->buflen = left_over;
300 	}
301 }
302 
zbx_sha256_hash(const char * in,char * out)303 void	zbx_sha256_hash(const char *in, char *out)
304 {
305 	struct	sha256_ctx ctx;
306 	sha256_init_ctx (&ctx);
307 	sha256_process_bytes (in, strlen (in), &ctx);
308 	sha256_finish_ctx (&ctx, out);
309 }
310 
311