1 #pragma prototyped
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 #define sha1_description "FIPS 180-1 SHA-1 secure hash algorithm 1."
18 #define sha1_options	"[+(version)?sha1 (FIPS 180-1) 1996-09-26]\
19 			 [+(author)?Steve Reid <steve@edmweb.com>]"
20 #define sha1_match	"sha1|SHA1|sha-1|SHA-1"
21 #define sha1_scale	0
22 
23 #define sha1_padding	md5_pad
24 
25 typedef struct Sha1_s
26 {
27 	_SUM_PUBLIC_
28 	_SUM_PRIVATE_
29 	uint32_t	count[2];
30 	uint32_t	state[5];
31 	uint8_t		buffer[64];
32 	uint8_t		digest[20];
33 	uint8_t		digest_sum[20];
34 } Sha1_t;
35 
36 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
37 
38 /*
39  * blk0() and blk() perform the initial expand.
40  * I got the idea of expanding during the round function from SSLeay
41  */
42 #if _ast_intswap
43 # define blk0(i) \
44 	(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
45 	 | (rol(block->l[i], 8) & 0x00FF00FF))
46 #else
47 # define blk0(i) block->l[i]
48 #endif
49 #define blk(i) \
50 	(block->l[i & 15] = rol(block->l[(i + 13) & 15] \
51 				^ block->l[(i + 8) & 15] \
52 				^ block->l[(i + 2) & 15] \
53 				^ block->l[i & 15], 1))
54 
55 /*
56  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
57  */
58 #define R0(v,w,x,y,z,i) \
59 	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
60 	w = rol(w, 30);
61 #define R1(v,w,x,y,z,i) \
62 	z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
63 	w = rol(w, 30);
64 #define R2(v,w,x,y,z,i) \
65 	z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
66 	w = rol(w, 30);
67 #define R3(v,w,x,y,z,i) \
68 	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
69 	w = rol(w, 30);
70 #define R4(v,w,x,y,z,i) \
71 	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
72 	w = rol(w, 30);
73 
74 typedef union {
75 	unsigned char c[64];
76 	unsigned int l[16];
77 } CHAR64LONG16;
78 
79 #ifdef __sparc_v9__
80 static void do_R01(uint32_t *a, uint32_t *b, uint32_t *c,
81 		   uint32_t *d, uint32_t *e, CHAR64LONG16 *);
82 static void do_R2(uint32_t *a, uint32_t *b, uint32_t *c,
83 		  uint32_t *d, uint32_t *e, CHAR64LONG16 *);
84 static void do_R3(uint32_t *a, uint32_t *b, uint32_t *c,
85 		  uint32_t *d, uint32_t *e, CHAR64LONG16 *);
86 static void do_R4(uint32_t *a, uint32_t *b, uint32_t *c,
87 		  uint32_t *d, uint32_t *e, CHAR64LONG16 *);
88 
89 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
90 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
91 #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
92 #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
93 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
94 
95 static void
do_R01(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)96 do_R01(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
97        uint32_t *e, CHAR64LONG16 *block)
98 {
99 	nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2);
100 	nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5);
101 	nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8);
102 	nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
103 	nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14);
104 	nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17);
105 	nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
106 }
107 
108 static void
do_R2(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)109 do_R2(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
110       uint32_t *e, CHAR64LONG16 *block)
111 {
112 	nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22);
113 	nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25);
114 	nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28);
115 	nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
116 	nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34);
117 	nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37);
118 	nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
119 }
120 
121 static void
do_R3(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)122 do_R3(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
123       uint32_t *e, CHAR64LONG16 *block)
124 {
125 	nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42);
126 	nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45);
127 	nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48);
128 	nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
129 	nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54);
130 	nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57);
131 	nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
132 }
133 
134 static void
do_R4(uint32_t * a,uint32_t * b,uint32_t * c,uint32_t * d,uint32_t * e,CHAR64LONG16 * block)135 do_R4(uint32_t *a, uint32_t *b, uint32_t *c, uint32_t *d,
136       uint32_t *e, CHAR64LONG16 *block)
137 {
138 	nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62);
139 	nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65);
140 	nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68);
141 	nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
142 	nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74);
143 	nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77);
144 	nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
145 }
146 #endif
147 
148 /*
149  * Hash a single 512-bit block. This is the core of the algorithm.
150  */
151 static void
sha1_transform(uint32_t state[5],const unsigned char buffer[64])152 sha1_transform(uint32_t state[5], const unsigned char buffer[64]) {
153 	uint32_t a, b, c, d, e;
154 	CHAR64LONG16 *block;
155 	CHAR64LONG16 workspace;
156 
157 	block = &workspace;
158 	(void)memcpy(block, buffer, 64);
159 
160 	/* Copy sha->state[] to working vars */
161 	a = state[0];
162 	b = state[1];
163 	c = state[2];
164 	d = state[3];
165 	e = state[4];
166 
167 #ifdef __sparc_v9__
168 	do_R01(&a, &b, &c, &d, &e, block);
169 	do_R2(&a, &b, &c, &d, &e, block);
170 	do_R3(&a, &b, &c, &d, &e, block);
171 	do_R4(&a, &b, &c, &d, &e, block);
172 #else
173 	/* 4 rounds of 20 operations each. Loop unrolled. */
174 	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);
175 	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);
176 	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);
177 	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);
178 	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);
179 	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);
180 	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);
181 	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);
182 	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);
183 	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);
184 	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);
185 	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);
186 	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);
187 	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);
188 	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);
189 	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);
190 	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);
191 	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);
192 	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);
193 	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);
194 #endif
195 
196 	/* Add the working vars back into context.state[] */
197 	state[0] += a;
198 	state[1] += b;
199 	state[2] += c;
200 	state[3] += d;
201 	state[4] += e;
202 
203 	/* Wipe variables */
204 	a = b = c = d = e = 0;
205 }
206 
207 static int
sha1_block(register Sum_t * p,const void * s,size_t len)208 sha1_block(register Sum_t* p, const void* s, size_t len)
209 {
210 	Sha1_t*		sha = (Sha1_t*)p;
211 	uint8_t*	data = (uint8_t*)s;
212 	unsigned int	i, j;
213 
214 	if (len) {
215 		j = sha->count[0];
216 		if ((sha->count[0] += len << 3) < j)
217 			sha->count[1] += (len >> 29) + 1;
218 		j = (j >> 3) & 63;
219 		if ((j + len) > 63) {
220 			(void)memcpy(&sha->buffer[j], data, (i = 64 - j));
221 			sha1_transform(sha->state, sha->buffer);
222 			for ( ; i + 63 < len; i += 64)
223 				sha1_transform(sha->state, &data[i]);
224 			j = 0;
225 		} else {
226 			i = 0;
227 		}
228 
229 		(void)memcpy(&sha->buffer[j], &data[i], len - i);
230 	}
231 	return 0;
232 }
233 
234 static int
sha1_init(Sum_t * p)235 sha1_init(Sum_t* p)
236 {
237 	register Sha1_t*	sha = (Sha1_t*)p;
238 
239 	sha->count[0] = sha->count[1] = 0;
240 	sha->state[0] = 0x67452301;
241 	sha->state[1] = 0xEFCDAB89;
242 	sha->state[2] = 0x98BADCFE;
243 	sha->state[3] = 0x10325476;
244 	sha->state[4] = 0xC3D2E1F0;
245 
246 	return 0;
247 }
248 
249 static Sum_t*
sha1_open(const Method_t * method,const char * name)250 sha1_open(const Method_t* method, const char* name)
251 {
252 	Sha1_t*	sha;
253 
254 	if (sha = newof(0, Sha1_t, 1, 0))
255 	{
256 		sha->method = (Method_t*)method;
257 		sha->name = name;
258 		sha1_init((Sum_t*)sha);
259 	}
260 	return (Sum_t*)sha;
261 }
262 
263 /*
264  * Add padding and return the message digest.
265  */
266 
267 static const unsigned char final_200 = 128;
268 static const unsigned char final_0 = 0;
269 
270 static int
sha1_done(Sum_t * p)271 sha1_done(Sum_t* p)
272 {
273 	Sha1_t*	sha = (Sha1_t*)p;
274 	unsigned int i;
275 	unsigned char finalcount[8];
276 
277 	for (i = 0; i < 8; i++) {
278 		/* Endian independent */
279 		finalcount[i] = (unsigned char)
280 			((sha->count[(i >= 4 ? 0 : 1)]
281 			  >> ((3 - (i & 3)) * 8)) & 255);
282 	}
283 
284 	sha1_block(p, &final_200, 1);
285 	while ((sha->count[0] & 504) != 448)
286 		sha1_block(p, &final_0, 1);
287 	/* The next Update should cause a sha1_transform() */
288 	sha1_block(p, finalcount, 8);
289 
290 	for (i = 0; i < elementsof(sha->digest); i++)
291 	{
292 		sha->digest[i] = (unsigned char)((sha->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
293 		sha->digest_sum[i] ^= sha->digest[i];
294 	}
295 	memset(sha->count, 0, sizeof(sha->count));
296 	memset(sha->state, 0, sizeof(sha->state));
297 	memset(sha->buffer, 0, sizeof(sha->buffer));
298 	return 0;
299 }
300 
301 static int
sha1_print(Sum_t * p,Sfio_t * sp,register int flags,size_t scale)302 sha1_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
303 {
304 	register Sha1_t*	sha = (Sha1_t*)p;
305 	register unsigned char*	d;
306 	register int		n;
307 
308 	d = (flags & SUM_TOTAL) ? sha->digest_sum : sha->digest;
309 	for (n = 0; n < elementsof(sha->digest); n++)
310 		sfprintf(sp, "%02x", d[n]);
311 	return 0;
312 }
313 
314 static int
sha1_data(Sum_t * p,Sumdata_t * data)315 sha1_data(Sum_t* p, Sumdata_t* data)
316 {
317 	register Sha1_t*	sha = (Sha1_t*)p;
318 
319 	data->size = elementsof(sha->digest);
320 	data->num = 0;
321 	data->buf = sha->digest;
322 	return 0;
323 }
324