xref: /freebsd/lib/libmd/sha1c.c (revision d0b2dbfa)
1 /* crypto/sha/sha1dgst.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the routines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <sys/cdefs.h>
60 #include <sys/types.h>
61 
62 #include <stdio.h>
63 #include <string.h>
64 
65 #if 0
66 #include <machine/ansi.h>	/* we use the __ variants of bit-sized types */
67 #endif
68 #include <machine/endian.h>
69 
70 #undef  SHA_0
71 #define SHA_1
72 #include "sha.h"
73 #include "sha_locl.h"
74 
75 /*
76  * The assembly-language code is not position-independent, so don't
77  * try to use it in a shared library.
78  */
79 #ifdef PIC
80 #undef SHA1_ASM
81 #endif
82 
83 char *SHA1_version="SHA1 part of SSLeay 0.9.0b 11-Oct-1998";
84 
85 /* Implemented from SHA-1 document - The Secure Hash Algorithm
86  */
87 
88 #define INIT_DATA_h0 (unsigned long)0x67452301L
89 #define INIT_DATA_h1 (unsigned long)0xefcdab89L
90 #define INIT_DATA_h2 (unsigned long)0x98badcfeL
91 #define INIT_DATA_h3 (unsigned long)0x10325476L
92 #define INIT_DATA_h4 (unsigned long)0xc3d2e1f0L
93 
94 #define K_00_19	0x5a827999L
95 #define K_20_39 0x6ed9eba1L
96 #define K_40_59 0x8f1bbcdcL
97 #define K_60_79 0xca62c1d6L
98 
99 #ifndef NOPROTO
100 #  ifdef SHA1_ASM
101      void sha1_block_x86(SHA_CTX *c, const u_int32_t *p, int num);
102 #    define sha1_block sha1_block_x86
103 #  else
104      void sha1_block(SHA_CTX *c, const u_int32_t *p, int num);
105 #  endif
106 #else
107 #  ifdef SHA1_ASM
108      void sha1_block_x86();
109 #    define sha1_block sha1_block_x86
110 #  else
111      void sha1_block();
112 #  endif
113 #endif
114 
115 
116 #if BYTE_ORDER == LITTLE_ENDIAN && defined(SHA1_ASM)
117 #  define	M_c2nl 		c2l
118 #  define	M_p_c2nl 	p_c2l
119 #  define	M_c2nl_p	c2l_p
120 #  define	M_p_c2nl_p	p_c2l_p
121 #  define	M_nl2c		l2c
122 #else
123 #  define	M_c2nl 		c2nl
124 #  define	M_p_c2nl	p_c2nl
125 #  define	M_c2nl_p	c2nl_p
126 #  define	M_p_c2nl_p	p_c2nl_p
127 #  define	M_nl2c		nl2c
128 #endif
129 
130 void SHA1_Init(SHA_CTX *c)
131 	{
132 	c->h0=INIT_DATA_h0;
133 	c->h1=INIT_DATA_h1;
134 	c->h2=INIT_DATA_h2;
135 	c->h3=INIT_DATA_h3;
136 	c->h4=INIT_DATA_h4;
137 	c->Nl=0;
138 	c->Nh=0;
139 	c->num=0;
140 	}
141 
142 void
143 SHA1_Update(SHA_CTX *c, const void *in, size_t len)
144 {
145 	u_int32_t *p;
146 	int ew,ec,sw,sc;
147 	u_int32_t l;
148 	const unsigned char *data = in;
149 
150 	if (len == 0) return;
151 
152 	l=(c->Nl+(len<<3))&0xffffffffL;
153 	if (l < c->Nl) /* overflow */
154 		c->Nh++;
155 	c->Nh+=(len>>29);
156 	c->Nl=l;
157 
158 	if (c->num != 0)
159 		{
160 		p=c->data;
161 		sw=c->num>>2;
162 		sc=c->num&0x03;
163 
164 		if ((c->num+len) >= SHA_CBLOCK)
165 			{
166 			l= p[sw];
167 			M_p_c2nl(data,l,sc);
168 			p[sw++]=l;
169 			for (; sw<SHA_LBLOCK; sw++)
170 				{
171 				M_c2nl(data,l);
172 				p[sw]=l;
173 				}
174 			len-=(SHA_CBLOCK-c->num);
175 
176 			sha1_block(c,p,64);
177 			c->num=0;
178 			/* drop through and do the rest */
179 			}
180 		else
181 			{
182 			c->num+=(int)len;
183 			if ((sc+len) < 4) /* ugly, add char's to a word */
184 				{
185 				l= p[sw];
186 				M_p_c2nl_p(data,l,sc,len);
187 				p[sw]=l;
188 				}
189 			else
190 				{
191 				ew=(c->num>>2);
192 				ec=(c->num&0x03);
193 				l= p[sw];
194 				M_p_c2nl(data,l,sc);
195 				p[sw++]=l;
196 				for (; sw < ew; sw++)
197 					{ M_c2nl(data,l); p[sw]=l; }
198 				if (ec)
199 					{
200 					M_c2nl_p(data,l,ec);
201 					p[sw]=l;
202 					}
203 				}
204 			return;
205 			}
206 		}
207 	/* We can only do the following code for assember, the reason
208 	 * being that the sha1_block 'C' version changes the values
209 	 * in the 'data' array.  The assember code avoids this and
210 	 * copies it to a local array.  I should be able to do this for
211 	 * the C version as well....
212 	 */
213 #if 1
214 #if BYTE_ORDER == BIG_ENDIAN || defined(SHA1_ASM)
215 	if ((((unsigned int)data)%sizeof(u_int32_t)) == 0)
216 		{
217 		sw=len/SHA_CBLOCK;
218 		if (sw)
219 			{
220 			sw*=SHA_CBLOCK;
221 			sha1_block(c,(u_int32_t *)data,sw);
222 			data+=sw;
223 			len-=sw;
224 			}
225 		}
226 #endif
227 #endif
228 	/* we now can process the input data in blocks of SHA_CBLOCK
229 	 * chars and save the leftovers to c->data. */
230 	p=c->data;
231 	while (len >= SHA_CBLOCK)
232 		{
233 #if BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == LITTLE_ENDIAN
234 		if (p != (u_int32_t *)data)
235 			memcpy(p,data,SHA_CBLOCK);
236 		data+=SHA_CBLOCK;
237 #  if BYTE_ORDER == LITTLE_ENDIAN
238 #    ifndef SHA1_ASM /* Will not happen */
239 		for (sw=(SHA_LBLOCK/4); sw; sw--)
240 			{
241 			Endian_Reverse32(p[0]);
242 			Endian_Reverse32(p[1]);
243 			Endian_Reverse32(p[2]);
244 			Endian_Reverse32(p[3]);
245 			p+=4;
246 			}
247 		p=c->data;
248 #    endif
249 #  endif
250 #else
251 		for (sw=(SHA_BLOCK/4); sw; sw--)
252 			{
253 			M_c2nl(data,l); *(p++)=l;
254 			M_c2nl(data,l); *(p++)=l;
255 			M_c2nl(data,l); *(p++)=l;
256 			M_c2nl(data,l); *(p++)=l;
257 			}
258 		p=c->data;
259 #endif
260 		sha1_block(c,p,64);
261 		len-=SHA_CBLOCK;
262 		}
263 	ec=(int)len;
264 	c->num=ec;
265 	ew=(ec>>2);
266 	ec&=0x03;
267 
268 	for (sw=0; sw < ew; sw++)
269 		{ M_c2nl(data,l); p[sw]=l; }
270 	M_c2nl_p(data,l,ec);
271 	p[sw]=l;
272 	}
273 
274 void SHA1_Transform(SHA_CTX *c, unsigned char *b)
275 	{
276 	u_int32_t p[16];
277 #if BYTE_ORDER != BIG_ENDIAN
278 	u_int32_t *q;
279 	int i;
280 #endif
281 
282 #if BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == LITTLE_ENDIAN
283 	memcpy(p,b,64);
284 #if BYTE_ORDER == LITTLE_ENDIAN
285 	q=p;
286 	for (i=(SHA_LBLOCK/4); i; i--)
287 		{
288 		Endian_Reverse32(q[0]);
289 		Endian_Reverse32(q[1]);
290 		Endian_Reverse32(q[2]);
291 		Endian_Reverse32(q[3]);
292 		q+=4;
293 		}
294 #endif
295 #else
296 	q=p;
297 	for (i=(SHA_LBLOCK/4); i; i--)
298 		{
299 		u_int32_t l;
300 		c2nl(b,l); *(q++)=l;
301 		c2nl(b,l); *(q++)=l;
302 		c2nl(b,l); *(q++)=l;
303 		c2nl(b,l); *(q++)=l;
304 		}
305 #endif
306 	sha1_block(c,p,64);
307 	}
308 
309 #ifndef SHA1_ASM
310 
311 void
312 sha1_block(SHA_CTX *c, const u_int32_t *W, int num)
313 {
314 	u_int32_t A,B,C,D,E,T;
315 	u_int32_t X[16];
316 
317 	A=c->h0;
318 	B=c->h1;
319 	C=c->h2;
320 	D=c->h3;
321 	E=c->h4;
322 
323 	for (;;)
324 		{
325 	BODY_00_15( 0,A,B,C,D,E,T,W);
326 	BODY_00_15( 1,T,A,B,C,D,E,W);
327 	BODY_00_15( 2,E,T,A,B,C,D,W);
328 	BODY_00_15( 3,D,E,T,A,B,C,W);
329 	BODY_00_15( 4,C,D,E,T,A,B,W);
330 	BODY_00_15( 5,B,C,D,E,T,A,W);
331 	BODY_00_15( 6,A,B,C,D,E,T,W);
332 	BODY_00_15( 7,T,A,B,C,D,E,W);
333 	BODY_00_15( 8,E,T,A,B,C,D,W);
334 	BODY_00_15( 9,D,E,T,A,B,C,W);
335 	BODY_00_15(10,C,D,E,T,A,B,W);
336 	BODY_00_15(11,B,C,D,E,T,A,W);
337 	BODY_00_15(12,A,B,C,D,E,T,W);
338 	BODY_00_15(13,T,A,B,C,D,E,W);
339 	BODY_00_15(14,E,T,A,B,C,D,W);
340 	BODY_00_15(15,D,E,T,A,B,C,W);
341 	BODY_16_19(16,C,D,E,T,A,B,W,W,W,W);
342 	BODY_16_19(17,B,C,D,E,T,A,W,W,W,W);
343 	BODY_16_19(18,A,B,C,D,E,T,W,W,W,W);
344 	BODY_16_19(19,T,A,B,C,D,E,W,W,W,X);
345 
346 	BODY_20_31(20,E,T,A,B,C,D,W,W,W,X);
347 	BODY_20_31(21,D,E,T,A,B,C,W,W,W,X);
348 	BODY_20_31(22,C,D,E,T,A,B,W,W,W,X);
349 	BODY_20_31(23,B,C,D,E,T,A,W,W,W,X);
350 	BODY_20_31(24,A,B,C,D,E,T,W,W,X,X);
351 	BODY_20_31(25,T,A,B,C,D,E,W,W,X,X);
352 	BODY_20_31(26,E,T,A,B,C,D,W,W,X,X);
353 	BODY_20_31(27,D,E,T,A,B,C,W,W,X,X);
354 	BODY_20_31(28,C,D,E,T,A,B,W,W,X,X);
355 	BODY_20_31(29,B,C,D,E,T,A,W,W,X,X);
356 	BODY_20_31(30,A,B,C,D,E,T,W,X,X,X);
357 	BODY_20_31(31,T,A,B,C,D,E,W,X,X,X);
358 	BODY_32_39(32,E,T,A,B,C,D,X);
359 	BODY_32_39(33,D,E,T,A,B,C,X);
360 	BODY_32_39(34,C,D,E,T,A,B,X);
361 	BODY_32_39(35,B,C,D,E,T,A,X);
362 	BODY_32_39(36,A,B,C,D,E,T,X);
363 	BODY_32_39(37,T,A,B,C,D,E,X);
364 	BODY_32_39(38,E,T,A,B,C,D,X);
365 	BODY_32_39(39,D,E,T,A,B,C,X);
366 
367 	BODY_40_59(40,C,D,E,T,A,B,X);
368 	BODY_40_59(41,B,C,D,E,T,A,X);
369 	BODY_40_59(42,A,B,C,D,E,T,X);
370 	BODY_40_59(43,T,A,B,C,D,E,X);
371 	BODY_40_59(44,E,T,A,B,C,D,X);
372 	BODY_40_59(45,D,E,T,A,B,C,X);
373 	BODY_40_59(46,C,D,E,T,A,B,X);
374 	BODY_40_59(47,B,C,D,E,T,A,X);
375 	BODY_40_59(48,A,B,C,D,E,T,X);
376 	BODY_40_59(49,T,A,B,C,D,E,X);
377 	BODY_40_59(50,E,T,A,B,C,D,X);
378 	BODY_40_59(51,D,E,T,A,B,C,X);
379 	BODY_40_59(52,C,D,E,T,A,B,X);
380 	BODY_40_59(53,B,C,D,E,T,A,X);
381 	BODY_40_59(54,A,B,C,D,E,T,X);
382 	BODY_40_59(55,T,A,B,C,D,E,X);
383 	BODY_40_59(56,E,T,A,B,C,D,X);
384 	BODY_40_59(57,D,E,T,A,B,C,X);
385 	BODY_40_59(58,C,D,E,T,A,B,X);
386 	BODY_40_59(59,B,C,D,E,T,A,X);
387 
388 	BODY_60_79(60,A,B,C,D,E,T,X);
389 	BODY_60_79(61,T,A,B,C,D,E,X);
390 	BODY_60_79(62,E,T,A,B,C,D,X);
391 	BODY_60_79(63,D,E,T,A,B,C,X);
392 	BODY_60_79(64,C,D,E,T,A,B,X);
393 	BODY_60_79(65,B,C,D,E,T,A,X);
394 	BODY_60_79(66,A,B,C,D,E,T,X);
395 	BODY_60_79(67,T,A,B,C,D,E,X);
396 	BODY_60_79(68,E,T,A,B,C,D,X);
397 	BODY_60_79(69,D,E,T,A,B,C,X);
398 	BODY_60_79(70,C,D,E,T,A,B,X);
399 	BODY_60_79(71,B,C,D,E,T,A,X);
400 	BODY_60_79(72,A,B,C,D,E,T,X);
401 	BODY_60_79(73,T,A,B,C,D,E,X);
402 	BODY_60_79(74,E,T,A,B,C,D,X);
403 	BODY_60_79(75,D,E,T,A,B,C,X);
404 	BODY_60_79(76,C,D,E,T,A,B,X);
405 	BODY_60_79(77,B,C,D,E,T,A,X);
406 	BODY_60_79(78,A,B,C,D,E,T,X);
407 	BODY_60_79(79,T,A,B,C,D,E,X);
408 
409 	c->h0=(c->h0+E)&0xffffffffL;
410 	c->h1=(c->h1+T)&0xffffffffL;
411 	c->h2=(c->h2+A)&0xffffffffL;
412 	c->h3=(c->h3+B)&0xffffffffL;
413 	c->h4=(c->h4+C)&0xffffffffL;
414 
415 	num-=64;
416 	if (num <= 0) break;
417 
418 	A=c->h0;
419 	B=c->h1;
420 	C=c->h2;
421 	D=c->h3;
422 	E=c->h4;
423 
424 	W+=16;
425 		}
426 	}
427 #endif
428 
429 void SHA1_Final(unsigned char *md, SHA_CTX *c)
430 	{
431 	int i,j;
432 	u_int32_t l;
433 	u_int32_t *p;
434 	static unsigned char end[4]={0x80,0x00,0x00,0x00};
435 	unsigned char *cp=end;
436 
437 	/* c->num should definitly have room for at least one more byte. */
438 	p=c->data;
439 	j=c->num;
440 	i=j>>2;
441 #ifdef PURIFY
442 	if ((j&0x03) == 0) p[i]=0;
443 #endif
444 	l=p[i];
445 	M_p_c2nl(cp,l,j&0x03);
446 	p[i]=l;
447 	i++;
448 	/* i is the next 'undefined word' */
449 	if (c->num >= SHA_LAST_BLOCK)
450 		{
451 		for (; i<SHA_LBLOCK; i++)
452 			p[i]=0;
453 		sha1_block(c,p,64);
454 		i=0;
455 		}
456 	for (; i<(SHA_LBLOCK-2); i++)
457 		p[i]=0;
458 	p[SHA_LBLOCK-2]=c->Nh;
459 	p[SHA_LBLOCK-1]=c->Nl;
460 #if BYTE_ORDER == LITTLE_ENDIAN && defined(SHA1_ASM)
461 	Endian_Reverse32(p[SHA_LBLOCK-2]);
462 	Endian_Reverse32(p[SHA_LBLOCK-1]);
463 #endif
464 	sha1_block(c,p,64);
465 	cp=md;
466 	l=c->h0; nl2c(l,cp);
467 	l=c->h1; nl2c(l,cp);
468 	l=c->h2; nl2c(l,cp);
469 	l=c->h3; nl2c(l,cp);
470 	l=c->h4; nl2c(l,cp);
471 
472 	/* Clear the context state */
473 	explicit_bzero(&c, sizeof(c));
474 	}
475 
476 #ifdef WEAK_REFS
477 /* When building libmd, provide weak references. Note: this is not
478    activated in the context of compiling these sources for internal
479    use in libcrypt.
480  */
481 #undef SHA_Init
482 __weak_reference(_libmd_SHA_Init, SHA_Init);
483 #undef SHA_Update
484 __weak_reference(_libmd_SHA_Update, SHA_Update);
485 #undef SHA_Final
486 __weak_reference(_libmd_SHA_Final, SHA_Final);
487 #undef SHA_Transform
488 __weak_reference(_libmd_SHA_Transform, SHA_Transform);
489 #undef SHA_version
490 __weak_reference(_libmd_SHA_version, SHA_version);
491 #undef sha_block
492 __weak_reference(_libmd_sha_block, sha_block);
493 #undef SHA1_Init
494 __weak_reference(_libmd_SHA1_Init, SHA1_Init);
495 #undef SHA1_Update
496 __weak_reference(_libmd_SHA1_Update, SHA1_Update);
497 #undef SHA1_Final
498 __weak_reference(_libmd_SHA1_Final, SHA1_Final);
499 #undef SHA1_Transform
500 __weak_reference(_libmd_SHA1_Transform, SHA1_Transform);
501 #undef SHA1_version
502 __weak_reference(_libmd_SHA1_version, SHA1_version);
503 #undef sha1_block
504 __weak_reference(_libmd_sha1_block, sha1_block);
505 #endif
506