1 /* $KAME: sha1.c,v 1.3 2000/02/22 14:01:18 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * contrib/pgcrypto/sha1.c
32 */
33 /*
34 * FIPS pub 180-1: Secure Hash Algorithm (SHA-1)
35 * based on: http://www.itl.nist.gov/fipspubs/fip180-1.htm
36 * implemented by Jun-ichiro itojun Itoh <itojun@itojun.org>
37 */
38
39 #include "postgres.h"
40
41 #include <sys/param.h>
42
43 #include "sha1.h"
44
45 /* constant table */
46 static uint32 _K[] = {0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xca62c1d6};
47
48 #define K(t) _K[(t) / 20]
49
50 #define F0(b, c, d) (((b) & (c)) | ((~(b)) & (d)))
51 #define F1(b, c, d) (((b) ^ (c)) ^ (d))
52 #define F2(b, c, d) (((b) & (c)) | ((b) & (d)) | ((c) & (d)))
53 #define F3(b, c, d) (((b) ^ (c)) ^ (d))
54
55 #define S(n, x) (((x) << (n)) | ((x) >> (32 - (n))))
56
57 #define H(n) (ctxt->h.b32[(n)])
58 #define COUNT (ctxt->count)
59 #define BCOUNT (ctxt->c.b64[0] / 8)
60 #define W(n) (ctxt->m.b32[(n)])
61
62 #define PUTBYTE(x) \
63 do { \
64 ctxt->m.b8[(COUNT % 64)] = (x); \
65 COUNT++; \
66 COUNT %= 64; \
67 ctxt->c.b64[0] += 8; \
68 if (COUNT % 64 == 0) \
69 sha1_step(ctxt); \
70 } while (0)
71
72 #define PUTPAD(x) \
73 do { \
74 ctxt->m.b8[(COUNT % 64)] = (x); \
75 COUNT++; \
76 COUNT %= 64; \
77 if (COUNT % 64 == 0) \
78 sha1_step(ctxt); \
79 } while (0)
80
81 static void sha1_step(struct sha1_ctxt *);
82
83 static void
sha1_step(struct sha1_ctxt * ctxt)84 sha1_step(struct sha1_ctxt *ctxt)
85 {
86 uint32 a,
87 b,
88 c,
89 d,
90 e;
91 size_t t,
92 s;
93 uint32 tmp;
94
95 #ifndef WORDS_BIGENDIAN
96 struct sha1_ctxt tctxt;
97
98 memmove(&tctxt.m.b8[0], &ctxt->m.b8[0], 64);
99 ctxt->m.b8[0] = tctxt.m.b8[3];
100 ctxt->m.b8[1] = tctxt.m.b8[2];
101 ctxt->m.b8[2] = tctxt.m.b8[1];
102 ctxt->m.b8[3] = tctxt.m.b8[0];
103 ctxt->m.b8[4] = tctxt.m.b8[7];
104 ctxt->m.b8[5] = tctxt.m.b8[6];
105 ctxt->m.b8[6] = tctxt.m.b8[5];
106 ctxt->m.b8[7] = tctxt.m.b8[4];
107 ctxt->m.b8[8] = tctxt.m.b8[11];
108 ctxt->m.b8[9] = tctxt.m.b8[10];
109 ctxt->m.b8[10] = tctxt.m.b8[9];
110 ctxt->m.b8[11] = tctxt.m.b8[8];
111 ctxt->m.b8[12] = tctxt.m.b8[15];
112 ctxt->m.b8[13] = tctxt.m.b8[14];
113 ctxt->m.b8[14] = tctxt.m.b8[13];
114 ctxt->m.b8[15] = tctxt.m.b8[12];
115 ctxt->m.b8[16] = tctxt.m.b8[19];
116 ctxt->m.b8[17] = tctxt.m.b8[18];
117 ctxt->m.b8[18] = tctxt.m.b8[17];
118 ctxt->m.b8[19] = tctxt.m.b8[16];
119 ctxt->m.b8[20] = tctxt.m.b8[23];
120 ctxt->m.b8[21] = tctxt.m.b8[22];
121 ctxt->m.b8[22] = tctxt.m.b8[21];
122 ctxt->m.b8[23] = tctxt.m.b8[20];
123 ctxt->m.b8[24] = tctxt.m.b8[27];
124 ctxt->m.b8[25] = tctxt.m.b8[26];
125 ctxt->m.b8[26] = tctxt.m.b8[25];
126 ctxt->m.b8[27] = tctxt.m.b8[24];
127 ctxt->m.b8[28] = tctxt.m.b8[31];
128 ctxt->m.b8[29] = tctxt.m.b8[30];
129 ctxt->m.b8[30] = tctxt.m.b8[29];
130 ctxt->m.b8[31] = tctxt.m.b8[28];
131 ctxt->m.b8[32] = tctxt.m.b8[35];
132 ctxt->m.b8[33] = tctxt.m.b8[34];
133 ctxt->m.b8[34] = tctxt.m.b8[33];
134 ctxt->m.b8[35] = tctxt.m.b8[32];
135 ctxt->m.b8[36] = tctxt.m.b8[39];
136 ctxt->m.b8[37] = tctxt.m.b8[38];
137 ctxt->m.b8[38] = tctxt.m.b8[37];
138 ctxt->m.b8[39] = tctxt.m.b8[36];
139 ctxt->m.b8[40] = tctxt.m.b8[43];
140 ctxt->m.b8[41] = tctxt.m.b8[42];
141 ctxt->m.b8[42] = tctxt.m.b8[41];
142 ctxt->m.b8[43] = tctxt.m.b8[40];
143 ctxt->m.b8[44] = tctxt.m.b8[47];
144 ctxt->m.b8[45] = tctxt.m.b8[46];
145 ctxt->m.b8[46] = tctxt.m.b8[45];
146 ctxt->m.b8[47] = tctxt.m.b8[44];
147 ctxt->m.b8[48] = tctxt.m.b8[51];
148 ctxt->m.b8[49] = tctxt.m.b8[50];
149 ctxt->m.b8[50] = tctxt.m.b8[49];
150 ctxt->m.b8[51] = tctxt.m.b8[48];
151 ctxt->m.b8[52] = tctxt.m.b8[55];
152 ctxt->m.b8[53] = tctxt.m.b8[54];
153 ctxt->m.b8[54] = tctxt.m.b8[53];
154 ctxt->m.b8[55] = tctxt.m.b8[52];
155 ctxt->m.b8[56] = tctxt.m.b8[59];
156 ctxt->m.b8[57] = tctxt.m.b8[58];
157 ctxt->m.b8[58] = tctxt.m.b8[57];
158 ctxt->m.b8[59] = tctxt.m.b8[56];
159 ctxt->m.b8[60] = tctxt.m.b8[63];
160 ctxt->m.b8[61] = tctxt.m.b8[62];
161 ctxt->m.b8[62] = tctxt.m.b8[61];
162 ctxt->m.b8[63] = tctxt.m.b8[60];
163 #endif
164
165 a = H(0);
166 b = H(1);
167 c = H(2);
168 d = H(3);
169 e = H(4);
170
171 for (t = 0; t < 20; t++)
172 {
173 s = t & 0x0f;
174 if (t >= 16)
175 W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
176 tmp = S(5, a) + F0(b, c, d) + e + W(s) + K(t);
177 e = d;
178 d = c;
179 c = S(30, b);
180 b = a;
181 a = tmp;
182 }
183 for (t = 20; t < 40; t++)
184 {
185 s = t & 0x0f;
186 W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
187 tmp = S(5, a) + F1(b, c, d) + e + W(s) + K(t);
188 e = d;
189 d = c;
190 c = S(30, b);
191 b = a;
192 a = tmp;
193 }
194 for (t = 40; t < 60; t++)
195 {
196 s = t & 0x0f;
197 W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
198 tmp = S(5, a) + F2(b, c, d) + e + W(s) + K(t);
199 e = d;
200 d = c;
201 c = S(30, b);
202 b = a;
203 a = tmp;
204 }
205 for (t = 60; t < 80; t++)
206 {
207 s = t & 0x0f;
208 W(s) = S(1, W((s + 13) & 0x0f) ^ W((s + 8) & 0x0f) ^ W((s + 2) & 0x0f) ^ W(s));
209 tmp = S(5, a) + F3(b, c, d) + e + W(s) + K(t);
210 e = d;
211 d = c;
212 c = S(30, b);
213 b = a;
214 a = tmp;
215 }
216
217 H(0) = H(0) + a;
218 H(1) = H(1) + b;
219 H(2) = H(2) + c;
220 H(3) = H(3) + d;
221 H(4) = H(4) + e;
222
223 memset(&ctxt->m.b8[0], 0, 64);
224 }
225
226 /*------------------------------------------------------------*/
227
228 void
sha1_init(struct sha1_ctxt * ctxt)229 sha1_init(struct sha1_ctxt *ctxt)
230 {
231 memset(ctxt, 0, sizeof(struct sha1_ctxt));
232 H(0) = 0x67452301;
233 H(1) = 0xefcdab89;
234 H(2) = 0x98badcfe;
235 H(3) = 0x10325476;
236 H(4) = 0xc3d2e1f0;
237 }
238
239 void
sha1_pad(struct sha1_ctxt * ctxt)240 sha1_pad(struct sha1_ctxt *ctxt)
241 {
242 size_t padlen; /* pad length in bytes */
243 size_t padstart;
244
245 PUTPAD(0x80);
246
247 padstart = COUNT % 64;
248 padlen = 64 - padstart;
249 if (padlen < 8)
250 {
251 memset(&ctxt->m.b8[padstart], 0, padlen);
252 COUNT += padlen;
253 COUNT %= 64;
254 sha1_step(ctxt);
255 padstart = COUNT % 64; /* should be 0 */
256 padlen = 64 - padstart; /* should be 64 */
257 }
258 memset(&ctxt->m.b8[padstart], 0, padlen - 8);
259 COUNT += (padlen - 8);
260 COUNT %= 64;
261 #ifdef WORDS_BIGENDIAN
262 PUTPAD(ctxt->c.b8[0]);
263 PUTPAD(ctxt->c.b8[1]);
264 PUTPAD(ctxt->c.b8[2]);
265 PUTPAD(ctxt->c.b8[3]);
266 PUTPAD(ctxt->c.b8[4]);
267 PUTPAD(ctxt->c.b8[5]);
268 PUTPAD(ctxt->c.b8[6]);
269 PUTPAD(ctxt->c.b8[7]);
270 #else
271 PUTPAD(ctxt->c.b8[7]);
272 PUTPAD(ctxt->c.b8[6]);
273 PUTPAD(ctxt->c.b8[5]);
274 PUTPAD(ctxt->c.b8[4]);
275 PUTPAD(ctxt->c.b8[3]);
276 PUTPAD(ctxt->c.b8[2]);
277 PUTPAD(ctxt->c.b8[1]);
278 PUTPAD(ctxt->c.b8[0]);
279 #endif
280 }
281
282 void
sha1_loop(struct sha1_ctxt * ctxt,const uint8 * input0,size_t len)283 sha1_loop(struct sha1_ctxt *ctxt, const uint8 *input0, size_t len)
284 {
285 const uint8 *input;
286 size_t gaplen;
287 size_t gapstart;
288 size_t off;
289 size_t copysiz;
290
291 input = (const uint8 *) input0;
292 off = 0;
293
294 while (off < len)
295 {
296 gapstart = COUNT % 64;
297 gaplen = 64 - gapstart;
298
299 copysiz = (gaplen < len - off) ? gaplen : len - off;
300 memmove(&ctxt->m.b8[gapstart], &input[off], copysiz);
301 COUNT += copysiz;
302 COUNT %= 64;
303 ctxt->c.b64[0] += copysiz * 8;
304 if (COUNT % 64 == 0)
305 sha1_step(ctxt);
306 off += copysiz;
307 }
308 }
309
310 void
sha1_result(struct sha1_ctxt * ctxt,uint8 * digest0)311 sha1_result(struct sha1_ctxt *ctxt, uint8 *digest0)
312 {
313 uint8 *digest;
314
315 digest = (uint8 *) digest0;
316 sha1_pad(ctxt);
317 #ifdef WORDS_BIGENDIAN
318 memmove(digest, &ctxt->h.b8[0], 20);
319 #else
320 digest[0] = ctxt->h.b8[3];
321 digest[1] = ctxt->h.b8[2];
322 digest[2] = ctxt->h.b8[1];
323 digest[3] = ctxt->h.b8[0];
324 digest[4] = ctxt->h.b8[7];
325 digest[5] = ctxt->h.b8[6];
326 digest[6] = ctxt->h.b8[5];
327 digest[7] = ctxt->h.b8[4];
328 digest[8] = ctxt->h.b8[11];
329 digest[9] = ctxt->h.b8[10];
330 digest[10] = ctxt->h.b8[9];
331 digest[11] = ctxt->h.b8[8];
332 digest[12] = ctxt->h.b8[15];
333 digest[13] = ctxt->h.b8[14];
334 digest[14] = ctxt->h.b8[13];
335 digest[15] = ctxt->h.b8[12];
336 digest[16] = ctxt->h.b8[19];
337 digest[17] = ctxt->h.b8[18];
338 digest[18] = ctxt->h.b8[17];
339 digest[19] = ctxt->h.b8[16];
340 #endif
341 }
342