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