1 /* $OpenBSD: sha1.c,v 1.12 2003/07/21 20:37:08 millert Exp $ */
2
3 /*
4 * SHA-1 in C
5 * By Steve Reid <steve@edmweb.com>
6 * 100% Public Domain
7 *
8 * Test Vectors (from FIPS PUB 180-1)
9 * "abc"
10 * A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
11 * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
12 * 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
13 * A million repetitions of "a"
14 * 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
15 */
16
17 #if defined(LIBC_SCCS) && !defined(lint)
18 static char rcsid[] = "$OpenBSD: sha1.c,v 1.12 2003/07/21 20:37:08 millert Exp $";
19 #endif /* LIBC_SCCS and not lint */
20
21 #define SHA1HANDSOFF /* Copies data before messing with it. */
22
23 #include <sys/param.h>
24 #include <sys/types.h>
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <string.h>
31 #include <sha1.h>
32
33 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
34
35 /*
36 * blk0() and blk() perform the initial expand.
37 * I got the idea of expanding during the round function from SSLeay
38 */
39 #if BYTE_ORDER == LITTLE_ENDIAN
40 # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
41 |(rol(block->l[i],8)&0x00FF00FF))
42 #else
43 # define blk0(i) block->l[i]
44 #endif
45 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
46 ^block->l[(i+2)&15]^block->l[i&15],1))
47
48 /*
49 * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
50 */
51 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
52 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
53 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
54 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
55 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
56
57 typedef union {
58 u_char c[64];
59 u_int l[16];
60 } CHAR64LONG16;
61
62 #ifdef __sparc_v9__
63 static void do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
64 static void do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
65 static void do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
66 static void do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *);
67
68 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
69 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
70 #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
71 #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
72 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
73
74 static void
do_R01(u_int32_t * a,u_int32_t * b,u_int32_t * c,u_int32_t * d,u_int32_t * e,CHAR64LONG16 * block)75 do_R01(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
76 {
77 nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2); nR0(c,d,e,a,b, 3);
78 nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5); nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7);
79 nR0(c,d,e,a,b, 8); nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
80 nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14); nR0(a,b,c,d,e,15);
81 nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17); nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
82 }
83
84 static void
do_R2(u_int32_t * a,u_int32_t * b,u_int32_t * c,u_int32_t * d,u_int32_t * e,CHAR64LONG16 * block)85 do_R2(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
86 {
87 nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22); nR2(c,d,e,a,b,23);
88 nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25); nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27);
89 nR2(c,d,e,a,b,28); nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
90 nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34); nR2(a,b,c,d,e,35);
91 nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37); nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
92 }
93
94 static void
do_R3(u_int32_t * a,u_int32_t * b,u_int32_t * c,u_int32_t * d,u_int32_t * e,CHAR64LONG16 * block)95 do_R3(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
96 {
97 nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42); nR3(c,d,e,a,b,43);
98 nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45); nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47);
99 nR3(c,d,e,a,b,48); nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
100 nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54); nR3(a,b,c,d,e,55);
101 nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57); nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
102 }
103
104 static void
do_R4(u_int32_t * a,u_int32_t * b,u_int32_t * c,u_int32_t * d,u_int32_t * e,CHAR64LONG16 * block)105 do_R4(u_int32_t *a, u_int32_t *b, u_int32_t *c, u_int32_t *d, u_int32_t *e, CHAR64LONG16 *block)
106 {
107 nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62); nR4(c,d,e,a,b,63);
108 nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65); nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67);
109 nR4(c,d,e,a,b,68); nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
110 nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74); nR4(a,b,c,d,e,75);
111 nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77); nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
112 }
113 #endif
114
115 /*
116 * Hash a single 512-bit block. This is the core of the algorithm.
117 */
118 void
SHA1Transform(u_int32_t state[5],const u_char buffer[64])119 SHA1Transform(u_int32_t state[5], const u_char buffer[64])
120 {
121 u_int32_t a, b, c, d, e;
122 CHAR64LONG16 *block;
123
124 #ifdef SHA1HANDSOFF
125 CHAR64LONG16 workspace;
126 block = &workspace;
127 (void)memcpy(block, buffer, 64);
128 #else
129 block = (CHAR64LONG16 *)buffer;
130 #endif
131
132 /* Copy context->state[] to working vars */
133 a = state[0];
134 b = state[1];
135 c = state[2];
136 d = state[3];
137 e = state[4];
138
139 #ifdef __sparc_v9__
140 do_R01(&a, &b, &c, &d, &e, block);
141 do_R2(&a, &b, &c, &d, &e, block);
142 do_R3(&a, &b, &c, &d, &e, block);
143 do_R4(&a, &b, &c, &d, &e, block);
144 #else
145 /* 4 rounds of 20 operations each. Loop unrolled. */
146 R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
147 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
148 R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
149 R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
150 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
151 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
152 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
153 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
154 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
155 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
156 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
157 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
158 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
159 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
160 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
161 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
162 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
163 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
164 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
165 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
166 #endif
167
168 /* Add the working vars back into context.state[] */
169 state[0] += a;
170 state[1] += b;
171 state[2] += c;
172 state[3] += d;
173 state[4] += e;
174
175 /* Wipe variables */
176 a = b = c = d = e = 0;
177 }
178
179
180 /*
181 * SHA1Init - Initialize new context
182 */
183 void
SHA1Init(SHA1_CTX * context)184 SHA1Init(SHA1_CTX *context)
185 {
186
187 /* SHA1 initialization constants */
188 context->state[0] = 0x67452301;
189 context->state[1] = 0xEFCDAB89;
190 context->state[2] = 0x98BADCFE;
191 context->state[3] = 0x10325476;
192 context->state[4] = 0xC3D2E1F0;
193 context->count[0] = context->count[1] = 0;
194 }
195
196
197 /*
198 * Run your data through this.
199 */
200 void
SHA1Update(SHA1_CTX * context,const u_char * data,u_int len)201 SHA1Update(SHA1_CTX *context, const u_char *data, u_int len)
202 {
203 u_int i, j;
204
205 j = context->count[0];
206 if ((context->count[0] += len << 3) < j)
207 context->count[1] += (len>>29)+1;
208 j = (j >> 3) & 63;
209 if ((j + len) > 63) {
210 (void)memcpy(&context->buffer[j], data, (i = 64-j));
211 SHA1Transform(context->state, context->buffer);
212 for ( ; i + 63 < len; i += 64)
213 SHA1Transform(context->state, &data[i]);
214 j = 0;
215 } else {
216 i = 0;
217 }
218 (void)memcpy(&context->buffer[j], &data[i], len - i);
219 }
220
221
222 /*
223 * Add padding and return the message digest.
224 */
225 void
SHA1Final(u_char digest[20],SHA1_CTX * context)226 SHA1Final(u_char digest[20], SHA1_CTX *context)
227 {
228 u_int i;
229 u_char finalcount[8];
230
231 for (i = 0; i < 8; i++) {
232 finalcount[i] = (u_char)((context->count[(i >= 4 ? 0 : 1)]
233 >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
234 }
235 SHA1Update(context, (u_char *)"\200", 1);
236 while ((context->count[0] & 504) != 448)
237 SHA1Update(context, (u_char *)"\0", 1);
238 SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
239
240 if (digest) {
241 for (i = 0; i < 20; i++)
242 digest[i] = (u_char)
243 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
244 }
245 }
246