1 /*--------------------------------------------------------------------
2  * Symbols referenced in this file:
3  * - sha1_result
4  * - sha1_pad
5  * - sha1_step
6  * - _K
7  * - sha1_init
8  * - sha1_loop
9  *--------------------------------------------------------------------
10  */
11 
12 /*	   $KAME: sha1.c,v 1.3 2000/02/22 14:01:18 itojun Exp $    */
13 
14 /*
15  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
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  * contrib/pgcrypto/sha1.c
43  */
44 /*
45  * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
46  * based on: http://www.itl.nist.gov/fipspubs/fip180-1.htm
47  * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
48  */
49 
50 #include "postgres.h"
51 
52 #include <sys/param.h>
53 
54 #include "sha1.h"
55 
56 /* constant table */
57 static __thread uint32 _K[] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};
58 
59 
60 #define K(t)	_K[(t) / 20]
61 
62 #define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
63 #define F1(b, c, d) (((b) ^ (c)) ^ (d))
64 #define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
65 #define F3(b, c, d) (((b) ^ (c)) ^ (d))
66 
67 #define S(n, x)		(((x) << (n)) | ((x) >> (32 - (n))))
68 
69 #define H(n)	(ctxt->h.b32[(n)])
70 #define COUNT	(ctxt->count)
71 #define BCOUNT	(ctxt->c.b64[0] / 8)
72 #define W(n)	(ctxt->m.b32[(n)])
73 
74 #define PUTBYTE(x) \
75 do { \
76 	ctxt->m.b8[(COUNT % 64)] = (x);		\
77 	COUNT++;				\
78 	COUNT %= 64;				\
79 	ctxt->c.b64[0] += 8;			\
80 	if (COUNT % 64 == 0)			\
81 		sha1_step(ctxt);		\
82 } while (0)
83 
84 #define PUTPAD(x) \
85 do { \
86 	ctxt->m.b8[(COUNT % 64)] = (x);		\
87 	COUNT++;				\
88 	COUNT %= 64;				\
89 	if (COUNT % 64 == 0)			\
90 		sha1_step(ctxt);		\
91 } while (0)
92 
93 static void sha1_step(struct sha1_ctxt *);
94 
95 static void
sha1_step(struct sha1_ctxt * ctxt)96 sha1_step(struct sha1_ctxt *ctxt)
97 {
98 	uint32		a,
99 				b,
100 				c,
101 				d,
102 				e;
103 	size_t		t,
104 				s;
105 	uint32		tmp;
106 
107 #ifndef WORDS_BIGENDIAN
108 	struct sha1_ctxt tctxt;
109 
110 	memmove(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
111 	ctxt->m.b8[0] = tctxt.m.b8[3];
112 	ctxt->m.b8[1] = tctxt.m.b8[2];
113 	ctxt->m.b8[2] = tctxt.m.b8[1];
114 	ctxt->m.b8[3] = tctxt.m.b8[0];
115 	ctxt->m.b8[4] = tctxt.m.b8[7];
116 	ctxt->m.b8[5] = tctxt.m.b8[6];
117 	ctxt->m.b8[6] = tctxt.m.b8[5];
118 	ctxt->m.b8[7] = tctxt.m.b8[4];
119 	ctxt->m.b8[8] = tctxt.m.b8[11];
120 	ctxt->m.b8[9] = tctxt.m.b8[10];
121 	ctxt->m.b8[10] = tctxt.m.b8[9];
122 	ctxt->m.b8[11] = tctxt.m.b8[8];
123 	ctxt->m.b8[12] = tctxt.m.b8[15];
124 	ctxt->m.b8[13] = tctxt.m.b8[14];
125 	ctxt->m.b8[14] = tctxt.m.b8[13];
126 	ctxt->m.b8[15] = tctxt.m.b8[12];
127 	ctxt->m.b8[16] = tctxt.m.b8[19];
128 	ctxt->m.b8[17] = tctxt.m.b8[18];
129 	ctxt->m.b8[18] = tctxt.m.b8[17];
130 	ctxt->m.b8[19] = tctxt.m.b8[16];
131 	ctxt->m.b8[20] = tctxt.m.b8[23];
132 	ctxt->m.b8[21] = tctxt.m.b8[22];
133 	ctxt->m.b8[22] = tctxt.m.b8[21];
134 	ctxt->m.b8[23] = tctxt.m.b8[20];
135 	ctxt->m.b8[24] = tctxt.m.b8[27];
136 	ctxt->m.b8[25] = tctxt.m.b8[26];
137 	ctxt->m.b8[26] = tctxt.m.b8[25];
138 	ctxt->m.b8[27] = tctxt.m.b8[24];
139 	ctxt->m.b8[28] = tctxt.m.b8[31];
140 	ctxt->m.b8[29] = tctxt.m.b8[30];
141 	ctxt->m.b8[30] = tctxt.m.b8[29];
142 	ctxt->m.b8[31] = tctxt.m.b8[28];
143 	ctxt->m.b8[32] = tctxt.m.b8[35];
144 	ctxt->m.b8[33] = tctxt.m.b8[34];
145 	ctxt->m.b8[34] = tctxt.m.b8[33];
146 	ctxt->m.b8[35] = tctxt.m.b8[32];
147 	ctxt->m.b8[36] = tctxt.m.b8[39];
148 	ctxt->m.b8[37] = tctxt.m.b8[38];
149 	ctxt->m.b8[38] = tctxt.m.b8[37];
150 	ctxt->m.b8[39] = tctxt.m.b8[36];
151 	ctxt->m.b8[40] = tctxt.m.b8[43];
152 	ctxt->m.b8[41] = tctxt.m.b8[42];
153 	ctxt->m.b8[42] = tctxt.m.b8[41];
154 	ctxt->m.b8[43] = tctxt.m.b8[40];
155 	ctxt->m.b8[44] = tctxt.m.b8[47];
156 	ctxt->m.b8[45] = tctxt.m.b8[46];
157 	ctxt->m.b8[46] = tctxt.m.b8[45];
158 	ctxt->m.b8[47] = tctxt.m.b8[44];
159 	ctxt->m.b8[48] = tctxt.m.b8[51];
160 	ctxt->m.b8[49] = tctxt.m.b8[50];
161 	ctxt->m.b8[50] = tctxt.m.b8[49];
162 	ctxt->m.b8[51] = tctxt.m.b8[48];
163 	ctxt->m.b8[52] = tctxt.m.b8[55];
164 	ctxt->m.b8[53] = tctxt.m.b8[54];
165 	ctxt->m.b8[54] = tctxt.m.b8[53];
166 	ctxt->m.b8[55] = tctxt.m.b8[52];
167 	ctxt->m.b8[56] = tctxt.m.b8[59];
168 	ctxt->m.b8[57] = tctxt.m.b8[58];
169 	ctxt->m.b8[58] = tctxt.m.b8[57];
170 	ctxt->m.b8[59] = tctxt.m.b8[56];
171 	ctxt->m.b8[60] = tctxt.m.b8[63];
172 	ctxt->m.b8[61] = tctxt.m.b8[62];
173 	ctxt->m.b8[62] = tctxt.m.b8[61];
174 	ctxt->m.b8[63] = tctxt.m.b8[60];
175 #endif
176 
177 	a = H(0);
178 	b = H(1);
179 	c = H(2);
180 	d = H(3);
181 	e = H(4);
182 
183 	for (t = 0; t < 20; t++)
184 	{
185 		s = t & 0x0f;
186 		if (t >= 16)
187 			W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
188 		tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
189 		e = d;
190 		d = c;
191 		c = S(30, b);
192 		b = a;
193 		a = tmp;
194 	}
195 	for (t = 20; t < 40; t++)
196 	{
197 		s = t & 0x0f;
198 		W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
199 		tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
200 		e = d;
201 		d = c;
202 		c = S(30, b);
203 		b = a;
204 		a = tmp;
205 	}
206 	for (t = 40; t < 60; t++)
207 	{
208 		s = t & 0x0f;
209 		W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
210 		tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
211 		e = d;
212 		d = c;
213 		c = S(30, b);
214 		b = a;
215 		a = tmp;
216 	}
217 	for (t = 60; t < 80; t++)
218 	{
219 		s = t & 0x0f;
220 		W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
221 		tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
222 		e = d;
223 		d = c;
224 		c = S(30, b);
225 		b = a;
226 		a = tmp;
227 	}
228 
229 	H(0) = H(0) + a;
230 	H(1) = H(1) + b;
231 	H(2) = H(2) + c;
232 	H(3) = H(3) + d;
233 	H(4) = H(4) + e;
234 
235 	memset(&ctxt->m.b8[0], 0, 64);
236 }
237 
238 /*------------------------------------------------------------*/
239 
240 void
sha1_init(struct sha1_ctxt * ctxt)241 sha1_init(struct sha1_ctxt *ctxt)
242 {
243 	memset(ctxt, 0, sizeof(struct sha1_ctxt));
244 	H(0) = 0x67452301;
245 	H(1) = 0xefcdab89;
246 	H(2) = 0x98badcfe;
247 	H(3) = 0x10325476;
248 	H(4) = 0xc3d2e1f0;
249 }
250 
251 void
sha1_pad(struct sha1_ctxt * ctxt)252 sha1_pad(struct sha1_ctxt *ctxt)
253 {
254 	size_t		padlen;			/* pad length in bytes */
255 	size_t		padstart;
256 
257 	PUTPAD(0x80);
258 
259 	padstart = COUNT % 64;
260 	padlen = 64 - padstart;
261 	if (padlen < 8)
262 	{
263 		memset(&ctxt->m.b8[padstart], 0, padlen);
264 		COUNT += padlen;
265 		COUNT %= 64;
266 		sha1_step(ctxt);
267 		padstart = COUNT % 64;	/* should be 0 */
268 		padlen = 64 - padstart; /* should be 64 */
269 	}
270 	memset(&ctxt->m.b8[padstart], 0, padlen - 8);
271 	COUNT += (padlen - 8);
272 	COUNT %= 64;
273 #ifdef WORDS_BIGENDIAN
274 	PUTPAD(ctxt->c.b8[0]);
275 	PUTPAD(ctxt->c.b8[1]);
276 	PUTPAD(ctxt->c.b8[2]);
277 	PUTPAD(ctxt->c.b8[3]);
278 	PUTPAD(ctxt->c.b8[4]);
279 	PUTPAD(ctxt->c.b8[5]);
280 	PUTPAD(ctxt->c.b8[6]);
281 	PUTPAD(ctxt->c.b8[7]);
282 #else
283 	PUTPAD(ctxt->c.b8[7]);
284 	PUTPAD(ctxt->c.b8[6]);
285 	PUTPAD(ctxt->c.b8[5]);
286 	PUTPAD(ctxt->c.b8[4]);
287 	PUTPAD(ctxt->c.b8[3]);
288 	PUTPAD(ctxt->c.b8[2]);
289 	PUTPAD(ctxt->c.b8[1]);
290 	PUTPAD(ctxt->c.b8[0]);
291 #endif
292 }
293 
294 void
sha1_loop(struct sha1_ctxt * ctxt,const uint8 * input0,size_t len)295 sha1_loop(struct sha1_ctxt *ctxt, const uint8 *input0, size_t len)
296 {
297 	const uint8 *input;
298 	size_t		gaplen;
299 	size_t		gapstart;
300 	size_t		off;
301 	size_t		copysiz;
302 
303 	input = (const uint8 *) input0;
304 	off = 0;
305 
306 	while (off < len)
307 	{
308 		gapstart = COUNT % 64;
309 		gaplen = 64 - gapstart;
310 
311 		copysiz = (gaplen < len - off) ? gaplen : len - off;
312 		memmove(&ctxt->m.b8[gapstart], &input[off], copysiz);
313 		COUNT += copysiz;
314 		COUNT %= 64;
315 		ctxt->c.b64[0] += copysiz * 8;
316 		if (COUNT % 64 == 0)
317 			sha1_step(ctxt);
318 		off += copysiz;
319 	}
320 }
321 
322 void
sha1_result(struct sha1_ctxt * ctxt,uint8 * digest0)323 sha1_result(struct sha1_ctxt *ctxt, uint8 *digest0)
324 {
325 	uint8	   *digest;
326 
327 	digest = (uint8 *) digest0;
328 	sha1_pad(ctxt);
329 #ifdef WORDS_BIGENDIAN
330 	memmove(digest, &ctxt->h.b8[0], 20);
331 #else
332 	digest[0] = ctxt->h.b8[3];
333 	digest[1] = ctxt->h.b8[2];
334 	digest[2] = ctxt->h.b8[1];
335 	digest[3] = ctxt->h.b8[0];
336 	digest[4] = ctxt->h.b8[7];
337 	digest[5] = ctxt->h.b8[6];
338 	digest[6] = ctxt->h.b8[5];
339 	digest[7] = ctxt->h.b8[4];
340 	digest[8] = ctxt->h.b8[11];
341 	digest[9] = ctxt->h.b8[10];
342 	digest[10] = ctxt->h.b8[9];
343 	digest[11] = ctxt->h.b8[8];
344 	digest[12] = ctxt->h.b8[15];
345 	digest[13] = ctxt->h.b8[14];
346 	digest[14] = ctxt->h.b8[13];
347 	digest[15] = ctxt->h.b8[12];
348 	digest[16] = ctxt->h.b8[19];
349 	digest[17] = ctxt->h.b8[18];
350 	digest[18] = ctxt->h.b8[17];
351 	digest[19] = ctxt->h.b8[16];
352 #endif
353 }
354