xref: /openbsd-src/lib/libcrypto/sha/sha1.c (revision 9cb045229698d08f09fbd7cf9ae7f3b9b8f8b848)
1*9cb04522Stb /* $OpenBSD: sha1.c,v 1.15 2024/06/01 07:36:16 tb Exp $ */
244f1c734Sjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
344f1c734Sjsing  * All rights reserved.
444f1c734Sjsing  *
544f1c734Sjsing  * This package is an SSL implementation written
644f1c734Sjsing  * by Eric Young (eay@cryptsoft.com).
744f1c734Sjsing  * The implementation was written so as to conform with Netscapes SSL.
844f1c734Sjsing  *
944f1c734Sjsing  * This library is free for commercial and non-commercial use as long as
1044f1c734Sjsing  * the following conditions are aheared to.  The following conditions
1144f1c734Sjsing  * apply to all code found in this distribution, be it the RC4, RSA,
1244f1c734Sjsing  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1344f1c734Sjsing  * included with this distribution is covered by the same copyright terms
1444f1c734Sjsing  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1544f1c734Sjsing  *
1644f1c734Sjsing  * Copyright remains Eric Young's, and as such any Copyright notices in
1744f1c734Sjsing  * the code are not to be removed.
1844f1c734Sjsing  * If this package is used in a product, Eric Young should be given attribution
1944f1c734Sjsing  * as the author of the parts of the library used.
2044f1c734Sjsing  * This can be in the form of a textual message at program startup or
2144f1c734Sjsing  * in documentation (online or textual) provided with the package.
2244f1c734Sjsing  *
2344f1c734Sjsing  * Redistribution and use in source and binary forms, with or without
2444f1c734Sjsing  * modification, are permitted provided that the following conditions
2544f1c734Sjsing  * are met:
2644f1c734Sjsing  * 1. Redistributions of source code must retain the copyright
2744f1c734Sjsing  *    notice, this list of conditions and the following disclaimer.
2844f1c734Sjsing  * 2. Redistributions in binary form must reproduce the above copyright
2944f1c734Sjsing  *    notice, this list of conditions and the following disclaimer in the
3044f1c734Sjsing  *    documentation and/or other materials provided with the distribution.
3144f1c734Sjsing  * 3. All advertising materials mentioning features or use of this software
3244f1c734Sjsing  *    must display the following acknowledgement:
3344f1c734Sjsing  *    "This product includes cryptographic software written by
3444f1c734Sjsing  *     Eric Young (eay@cryptsoft.com)"
3544f1c734Sjsing  *    The word 'cryptographic' can be left out if the rouines from the library
3644f1c734Sjsing  *    being used are not cryptographic related :-).
3744f1c734Sjsing  * 4. If you include any Windows specific code (or a derivative thereof) from
3844f1c734Sjsing  *    the apps directory (application code) you must include an acknowledgement:
3944f1c734Sjsing  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4044f1c734Sjsing  *
4144f1c734Sjsing  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4244f1c734Sjsing  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4344f1c734Sjsing  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4444f1c734Sjsing  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4544f1c734Sjsing  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4644f1c734Sjsing  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4744f1c734Sjsing  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4844f1c734Sjsing  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4944f1c734Sjsing  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5044f1c734Sjsing  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5144f1c734Sjsing  * SUCH DAMAGE.
5244f1c734Sjsing  *
5344f1c734Sjsing  * The licence and distribution terms for any publically available version or
5444f1c734Sjsing  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5544f1c734Sjsing  * copied and put under another distribution licence
5644f1c734Sjsing  * [including the GNU Public Licence.]
5744f1c734Sjsing  */
5844f1c734Sjsing 
5944f1c734Sjsing #include <stdlib.h>
6044f1c734Sjsing #include <string.h>
6144f1c734Sjsing 
6244f1c734Sjsing #include <openssl/opensslconf.h>
6344f1c734Sjsing 
6444f1c734Sjsing #include <openssl/crypto.h>
6544f1c734Sjsing #include <openssl/sha.h>
6644f1c734Sjsing 
67c2de78a7Sjsing #include "crypto_internal.h"
68c2de78a7Sjsing 
6944f1c734Sjsing #if !defined(OPENSSL_NO_SHA1) && !defined(OPENSSL_NO_SHA)
7044f1c734Sjsing 
71c2de78a7Sjsing /* Ensure that SHA_LONG and uint32_t are equivalent sizes. */
72c2de78a7Sjsing CTASSERT(sizeof(SHA_LONG) == sizeof(uint32_t));
73c2de78a7Sjsing 
74a407cbb3Sjsing #ifdef SHA1_ASM
75a407cbb3Sjsing void sha1_block_data_order(SHA_CTX *ctx, const void *p, size_t num);
76a407cbb3Sjsing #endif
7744f1c734Sjsing 
7844f1c734Sjsing #ifndef SHA1_ASM
79a407cbb3Sjsing static inline SHA_LONG
Ch(SHA_LONG x,SHA_LONG y,SHA_LONG z)80a407cbb3Sjsing Ch(SHA_LONG x, SHA_LONG y, SHA_LONG z)
81a407cbb3Sjsing {
82a407cbb3Sjsing 	return (x & y) ^ (~x & z);
83a407cbb3Sjsing }
8444f1c734Sjsing 
85a407cbb3Sjsing static inline SHA_LONG
Parity(SHA_LONG x,SHA_LONG y,SHA_LONG z)86a407cbb3Sjsing Parity(SHA_LONG x, SHA_LONG y, SHA_LONG z)
87a407cbb3Sjsing {
88a407cbb3Sjsing 	return x ^ y ^ z;
89a407cbb3Sjsing }
90cbefc5ebSjsing 
91a407cbb3Sjsing static inline SHA_LONG
Maj(SHA_LONG x,SHA_LONG y,SHA_LONG z)92a407cbb3Sjsing Maj(SHA_LONG x, SHA_LONG y, SHA_LONG z)
93a407cbb3Sjsing {
94a407cbb3Sjsing 	return (x & y) ^ (x & z) ^ (y & z);
95a407cbb3Sjsing }
9644f1c734Sjsing 
97a407cbb3Sjsing static inline void
sha1_msg_schedule_update(SHA_LONG * W0,SHA_LONG W2,SHA_LONG W8,SHA_LONG W13)98a407cbb3Sjsing sha1_msg_schedule_update(SHA_LONG *W0, SHA_LONG W2, SHA_LONG W8, SHA_LONG W13)
99a407cbb3Sjsing {
100a407cbb3Sjsing 	*W0 = crypto_rol_u32(W13 ^ W8 ^ W2 ^ *W0, 1);
101a407cbb3Sjsing }
10244f1c734Sjsing 
103a407cbb3Sjsing static inline void
sha1_round1(SHA_LONG * a,SHA_LONG * b,SHA_LONG * c,SHA_LONG * d,SHA_LONG * e,SHA_LONG Wt)104a407cbb3Sjsing sha1_round1(SHA_LONG *a, SHA_LONG *b, SHA_LONG *c, SHA_LONG *d, SHA_LONG *e,
105a407cbb3Sjsing     SHA_LONG Wt)
106a407cbb3Sjsing {
107a407cbb3Sjsing 	SHA_LONG Kt, T;
10844f1c734Sjsing 
109a407cbb3Sjsing 	Kt = 0x5a827999UL;
110a407cbb3Sjsing 	T = crypto_rol_u32(*a, 5) + Ch(*b, *c, *d) + *e + Kt + Wt;
11144f1c734Sjsing 
112a407cbb3Sjsing 	*e = *d;
113a407cbb3Sjsing 	*d = *c;
114a407cbb3Sjsing 	*c = crypto_rol_u32(*b, 30);
115a407cbb3Sjsing 	*b = *a;
116a407cbb3Sjsing 	*a = T;
117a407cbb3Sjsing }
11844f1c734Sjsing 
119a407cbb3Sjsing static inline void
sha1_round2(SHA_LONG * a,SHA_LONG * b,SHA_LONG * c,SHA_LONG * d,SHA_LONG * e,SHA_LONG Wt)120a407cbb3Sjsing sha1_round2(SHA_LONG *a, SHA_LONG *b, SHA_LONG *c, SHA_LONG *d, SHA_LONG *e,
121a407cbb3Sjsing     SHA_LONG Wt)
122a407cbb3Sjsing {
123a407cbb3Sjsing 	SHA_LONG Kt, T;
12444f1c734Sjsing 
125a407cbb3Sjsing 	Kt = 0x6ed9eba1UL;
126a407cbb3Sjsing 	T = crypto_rol_u32(*a, 5) + Parity(*b, *c, *d) + *e + Kt + Wt;
12744f1c734Sjsing 
128a407cbb3Sjsing 	*e = *d;
129a407cbb3Sjsing 	*d = *c;
130a407cbb3Sjsing 	*c = crypto_rol_u32(*b, 30);
131a407cbb3Sjsing 	*b = *a;
132a407cbb3Sjsing 	*a = T;
133a407cbb3Sjsing }
13444f1c734Sjsing 
135a407cbb3Sjsing static inline void
sha1_round3(SHA_LONG * a,SHA_LONG * b,SHA_LONG * c,SHA_LONG * d,SHA_LONG * e,SHA_LONG Wt)136a407cbb3Sjsing sha1_round3(SHA_LONG *a, SHA_LONG *b, SHA_LONG *c, SHA_LONG *d, SHA_LONG *e,
137a407cbb3Sjsing     SHA_LONG Wt)
138a407cbb3Sjsing {
139a407cbb3Sjsing 	SHA_LONG Kt, T;
14044f1c734Sjsing 
141a407cbb3Sjsing 	Kt = 0x8f1bbcdcUL;
142a407cbb3Sjsing 	T = crypto_rol_u32(*a, 5) + Maj(*b, *c, *d) + *e + Kt + Wt;
14344f1c734Sjsing 
144a407cbb3Sjsing 	*e = *d;
145a407cbb3Sjsing 	*d = *c;
146a407cbb3Sjsing 	*c = crypto_rol_u32(*b, 30);
147a407cbb3Sjsing 	*b = *a;
148a407cbb3Sjsing 	*a = T;
149a407cbb3Sjsing }
150a407cbb3Sjsing 
151a407cbb3Sjsing static inline void
sha1_round4(SHA_LONG * a,SHA_LONG * b,SHA_LONG * c,SHA_LONG * d,SHA_LONG * e,SHA_LONG Wt)152a407cbb3Sjsing sha1_round4(SHA_LONG *a, SHA_LONG *b, SHA_LONG *c, SHA_LONG *d, SHA_LONG *e,
153a407cbb3Sjsing     SHA_LONG Wt)
154a407cbb3Sjsing {
155a407cbb3Sjsing 	SHA_LONG Kt, T;
156a407cbb3Sjsing 
157a407cbb3Sjsing 	Kt = 0xca62c1d6UL;
158a407cbb3Sjsing 	T = crypto_rol_u32(*a, 5) + Parity(*b, *c, *d) + *e + Kt + Wt;
159a407cbb3Sjsing 
160a407cbb3Sjsing 	*e = *d;
161a407cbb3Sjsing 	*d = *c;
162a407cbb3Sjsing 	*c = crypto_rol_u32(*b, 30);
163a407cbb3Sjsing 	*b = *a;
164a407cbb3Sjsing 	*a = T;
165a407cbb3Sjsing }
166a407cbb3Sjsing 
16744f1c734Sjsing static void
sha1_block_data_order(SHA_CTX * ctx,const void * _in,size_t num)168a407cbb3Sjsing sha1_block_data_order(SHA_CTX *ctx, const void *_in, size_t num)
16944f1c734Sjsing {
170c2de78a7Sjsing 	const uint8_t *in = _in;
171c2de78a7Sjsing 	const SHA_LONG *in32;
172a407cbb3Sjsing 	unsigned int a, b, c, d, e;
173cd67cc31Sjsing 	unsigned int X0, X1, X2, X3, X4, X5, X6, X7,
17444f1c734Sjsing 	    X8, X9, X10, X11, X12, X13, X14, X15;
17544f1c734Sjsing 
176c2de78a7Sjsing 	while (num--) {
177a407cbb3Sjsing 		a = ctx->h0;
178a407cbb3Sjsing 		b = ctx->h1;
179a407cbb3Sjsing 		c = ctx->h2;
180a407cbb3Sjsing 		d = ctx->h3;
181a407cbb3Sjsing 		e = ctx->h4;
18244f1c734Sjsing 
183c2de78a7Sjsing 		if ((size_t)in % 4 == 0) {
184c2de78a7Sjsing 			/* Input is 32 bit aligned. */
185c2de78a7Sjsing 			in32 = (const SHA_LONG *)in;
186c2de78a7Sjsing 			X0 = be32toh(in32[0]);
187c2de78a7Sjsing 			X1 = be32toh(in32[1]);
188c2de78a7Sjsing 			X2 = be32toh(in32[2]);
189c2de78a7Sjsing 			X3 = be32toh(in32[3]);
190c2de78a7Sjsing 			X4 = be32toh(in32[4]);
191c2de78a7Sjsing 			X5 = be32toh(in32[5]);
192c2de78a7Sjsing 			X6 = be32toh(in32[6]);
193c2de78a7Sjsing 			X7 = be32toh(in32[7]);
194c2de78a7Sjsing 			X8 = be32toh(in32[8]);
195c2de78a7Sjsing 			X9 = be32toh(in32[9]);
196c2de78a7Sjsing 			X10 = be32toh(in32[10]);
197c2de78a7Sjsing 			X11 = be32toh(in32[11]);
198c2de78a7Sjsing 			X12 = be32toh(in32[12]);
199c2de78a7Sjsing 			X13 = be32toh(in32[13]);
200c2de78a7Sjsing 			X14 = be32toh(in32[14]);
201c2de78a7Sjsing 			X15 = be32toh(in32[15]);
20244f1c734Sjsing 		} else {
203c2de78a7Sjsing 			/* Input is not 32 bit aligned. */
204c2de78a7Sjsing 			X0 = crypto_load_be32toh(&in[0 * 4]);
205c2de78a7Sjsing 			X1 = crypto_load_be32toh(&in[1 * 4]);
206c2de78a7Sjsing 			X2 = crypto_load_be32toh(&in[2 * 4]);
207c2de78a7Sjsing 			X3 = crypto_load_be32toh(&in[3 * 4]);
208c2de78a7Sjsing 			X4 = crypto_load_be32toh(&in[4 * 4]);
209c2de78a7Sjsing 			X5 = crypto_load_be32toh(&in[5 * 4]);
210c2de78a7Sjsing 			X6 = crypto_load_be32toh(&in[6 * 4]);
211c2de78a7Sjsing 			X7 = crypto_load_be32toh(&in[7 * 4]);
212c2de78a7Sjsing 			X8 = crypto_load_be32toh(&in[8 * 4]);
213c2de78a7Sjsing 			X9 = crypto_load_be32toh(&in[9 * 4]);
214c2de78a7Sjsing 			X10 = crypto_load_be32toh(&in[10 * 4]);
215c2de78a7Sjsing 			X11 = crypto_load_be32toh(&in[11 * 4]);
216c2de78a7Sjsing 			X12 = crypto_load_be32toh(&in[12 * 4]);
217c2de78a7Sjsing 			X13 = crypto_load_be32toh(&in[13 * 4]);
218c2de78a7Sjsing 			X14 = crypto_load_be32toh(&in[14 * 4]);
219c2de78a7Sjsing 			X15 = crypto_load_be32toh(&in[15 * 4]);
220c2de78a7Sjsing 		}
221c2de78a7Sjsing 		in += SHA_CBLOCK;
222c2de78a7Sjsing 
223a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X0);
224a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X1);
225a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X2);
226a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X3);
227a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X4);
228a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X5);
229a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X6);
230a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X7);
231a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X8);
232a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X9);
233a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X10);
234a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X11);
235a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X12);
236a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X13);
237a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X14);
238a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X15);
23944f1c734Sjsing 
240a407cbb3Sjsing 		sha1_msg_schedule_update(&X0, X2, X8, X13);
241a407cbb3Sjsing 		sha1_msg_schedule_update(&X1, X3, X9, X14);
242a407cbb3Sjsing 		sha1_msg_schedule_update(&X2, X4, X10, X15);
243a407cbb3Sjsing 		sha1_msg_schedule_update(&X3, X5, X11, X0);
244a407cbb3Sjsing 		sha1_msg_schedule_update(&X4, X6, X12, X1);
245a407cbb3Sjsing 		sha1_msg_schedule_update(&X5, X7, X13, X2);
246a407cbb3Sjsing 		sha1_msg_schedule_update(&X6, X8, X14, X3);
247a407cbb3Sjsing 		sha1_msg_schedule_update(&X7, X9, X15, X4);
248a407cbb3Sjsing 		sha1_msg_schedule_update(&X8, X10, X0, X5);
249a407cbb3Sjsing 		sha1_msg_schedule_update(&X9, X11, X1, X6);
250a407cbb3Sjsing 		sha1_msg_schedule_update(&X10, X12, X2, X7);
251a407cbb3Sjsing 		sha1_msg_schedule_update(&X11, X13, X3, X8);
252a407cbb3Sjsing 		sha1_msg_schedule_update(&X12, X14, X4, X9);
253a407cbb3Sjsing 		sha1_msg_schedule_update(&X13, X15, X5, X10);
254a407cbb3Sjsing 		sha1_msg_schedule_update(&X14, X0, X6, X11);
255a407cbb3Sjsing 		sha1_msg_schedule_update(&X15, X1, X7, X12);
25644f1c734Sjsing 
257a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X0);
258a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X1);
259a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X2);
260a407cbb3Sjsing 		sha1_round1(&a, &b, &c, &d, &e, X3);
261a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X4);
262a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X5);
263a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X6);
264a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X7);
265a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X8);
266a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X9);
267a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X10);
268a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X11);
269a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X12);
270a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X13);
271a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X14);
272a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X15);
27344f1c734Sjsing 
274a407cbb3Sjsing 		sha1_msg_schedule_update(&X0, X2, X8, X13);
275a407cbb3Sjsing 		sha1_msg_schedule_update(&X1, X3, X9, X14);
276a407cbb3Sjsing 		sha1_msg_schedule_update(&X2, X4, X10, X15);
277a407cbb3Sjsing 		sha1_msg_schedule_update(&X3, X5, X11, X0);
278a407cbb3Sjsing 		sha1_msg_schedule_update(&X4, X6, X12, X1);
279a407cbb3Sjsing 		sha1_msg_schedule_update(&X5, X7, X13, X2);
280a407cbb3Sjsing 		sha1_msg_schedule_update(&X6, X8, X14, X3);
281a407cbb3Sjsing 		sha1_msg_schedule_update(&X7, X9, X15, X4);
282a407cbb3Sjsing 		sha1_msg_schedule_update(&X8, X10, X0, X5);
283a407cbb3Sjsing 		sha1_msg_schedule_update(&X9, X11, X1, X6);
284a407cbb3Sjsing 		sha1_msg_schedule_update(&X10, X12, X2, X7);
285a407cbb3Sjsing 		sha1_msg_schedule_update(&X11, X13, X3, X8);
286a407cbb3Sjsing 		sha1_msg_schedule_update(&X12, X14, X4, X9);
287a407cbb3Sjsing 		sha1_msg_schedule_update(&X13, X15, X5, X10);
288a407cbb3Sjsing 		sha1_msg_schedule_update(&X14, X0, X6, X11);
289a407cbb3Sjsing 		sha1_msg_schedule_update(&X15, X1, X7, X12);
29044f1c734Sjsing 
291a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X0);
292a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X1);
293a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X2);
294a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X3);
295a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X4);
296a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X5);
297a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X6);
298a407cbb3Sjsing 		sha1_round2(&a, &b, &c, &d, &e, X7);
299a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X8);
300a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X9);
301a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X10);
302a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X11);
303a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X12);
304a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X13);
305a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X14);
306a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X15);
30744f1c734Sjsing 
308a407cbb3Sjsing 		sha1_msg_schedule_update(&X0, X2, X8, X13);
309a407cbb3Sjsing 		sha1_msg_schedule_update(&X1, X3, X9, X14);
310a407cbb3Sjsing 		sha1_msg_schedule_update(&X2, X4, X10, X15);
311a407cbb3Sjsing 		sha1_msg_schedule_update(&X3, X5, X11, X0);
312a407cbb3Sjsing 		sha1_msg_schedule_update(&X4, X6, X12, X1);
313a407cbb3Sjsing 		sha1_msg_schedule_update(&X5, X7, X13, X2);
314a407cbb3Sjsing 		sha1_msg_schedule_update(&X6, X8, X14, X3);
315a407cbb3Sjsing 		sha1_msg_schedule_update(&X7, X9, X15, X4);
316a407cbb3Sjsing 		sha1_msg_schedule_update(&X8, X10, X0, X5);
317a407cbb3Sjsing 		sha1_msg_schedule_update(&X9, X11, X1, X6);
318a407cbb3Sjsing 		sha1_msg_schedule_update(&X10, X12, X2, X7);
319a407cbb3Sjsing 		sha1_msg_schedule_update(&X11, X13, X3, X8);
320a407cbb3Sjsing 		sha1_msg_schedule_update(&X12, X14, X4, X9);
321a407cbb3Sjsing 		sha1_msg_schedule_update(&X13, X15, X5, X10);
322a407cbb3Sjsing 		sha1_msg_schedule_update(&X14, X0, X6, X11);
323a407cbb3Sjsing 		sha1_msg_schedule_update(&X15, X1, X7, X12);
32444f1c734Sjsing 
325a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X0);
326a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X1);
327a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X2);
328a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X3);
329a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X4);
330a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X5);
331a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X6);
332a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X7);
333a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X8);
334a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X9);
335a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X10);
336a407cbb3Sjsing 		sha1_round3(&a, &b, &c, &d, &e, X11);
337a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X12);
338a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X13);
339a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X14);
340a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X15);
341a407cbb3Sjsing 
342a407cbb3Sjsing 		sha1_msg_schedule_update(&X0, X2, X8, X13);
343a407cbb3Sjsing 		sha1_msg_schedule_update(&X1, X3, X9, X14);
344a407cbb3Sjsing 		sha1_msg_schedule_update(&X2, X4, X10, X15);
345a407cbb3Sjsing 		sha1_msg_schedule_update(&X3, X5, X11, X0);
346a407cbb3Sjsing 		sha1_msg_schedule_update(&X4, X6, X12, X1);
347a407cbb3Sjsing 		sha1_msg_schedule_update(&X5, X7, X13, X2);
348a407cbb3Sjsing 		sha1_msg_schedule_update(&X6, X8, X14, X3);
349a407cbb3Sjsing 		sha1_msg_schedule_update(&X7, X9, X15, X4);
350a407cbb3Sjsing 		sha1_msg_schedule_update(&X8, X10, X0, X5);
351a407cbb3Sjsing 		sha1_msg_schedule_update(&X9, X11, X1, X6);
352a407cbb3Sjsing 		sha1_msg_schedule_update(&X10, X12, X2, X7);
353a407cbb3Sjsing 		sha1_msg_schedule_update(&X11, X13, X3, X8);
354a407cbb3Sjsing 		sha1_msg_schedule_update(&X12, X14, X4, X9);
355a407cbb3Sjsing 		sha1_msg_schedule_update(&X13, X15, X5, X10);
356a407cbb3Sjsing 		sha1_msg_schedule_update(&X14, X0, X6, X11);
357a407cbb3Sjsing 		sha1_msg_schedule_update(&X15, X1, X7, X12);
358a407cbb3Sjsing 
359a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X0);
360a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X1);
361a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X2);
362a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X3);
363a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X4);
364a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X5);
365a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X6);
366a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X7);
367a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X8);
368a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X9);
369a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X10);
370a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X11);
371a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X12);
372a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X13);
373a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X14);
374a407cbb3Sjsing 		sha1_round4(&a, &b, &c, &d, &e, X15);
375a407cbb3Sjsing 
376a407cbb3Sjsing 		ctx->h0 += a;
377a407cbb3Sjsing 		ctx->h1 += b;
378a407cbb3Sjsing 		ctx->h2 += c;
379a407cbb3Sjsing 		ctx->h3 += d;
380a407cbb3Sjsing 		ctx->h4 += e;
38144f1c734Sjsing 	}
38244f1c734Sjsing }
38344f1c734Sjsing #endif
38444f1c734Sjsing 
385eb6cfd0bSjsing int
SHA1_Init(SHA_CTX * c)386eb6cfd0bSjsing SHA1_Init(SHA_CTX *c)
387eb6cfd0bSjsing {
388eb6cfd0bSjsing 	memset(c, 0, sizeof(*c));
389eb6cfd0bSjsing 
390eb6cfd0bSjsing 	c->h0 = 0x67452301UL;
391eb6cfd0bSjsing 	c->h1 = 0xefcdab89UL;
392eb6cfd0bSjsing 	c->h2 = 0x98badcfeUL;
393eb6cfd0bSjsing 	c->h3 = 0x10325476UL;
394eb6cfd0bSjsing 	c->h4 = 0xc3d2e1f0UL;
395eb6cfd0bSjsing 
396eb6cfd0bSjsing 	return 1;
397eb6cfd0bSjsing }
39865be244dSbeck LCRYPTO_ALIAS(SHA1_Init);
399eb6cfd0bSjsing 
400eb6cfd0bSjsing int
SHA1_Update(SHA_CTX * c,const void * data_,size_t len)401eb6cfd0bSjsing SHA1_Update(SHA_CTX *c, const void *data_, size_t len)
402eb6cfd0bSjsing {
403eb6cfd0bSjsing 	const unsigned char *data = data_;
404eb6cfd0bSjsing 	unsigned char *p;
405eb6cfd0bSjsing 	SHA_LONG l;
406eb6cfd0bSjsing 	size_t n;
407eb6cfd0bSjsing 
408eb6cfd0bSjsing 	if (len == 0)
409eb6cfd0bSjsing 		return 1;
410eb6cfd0bSjsing 
411eb6cfd0bSjsing 	l = (c->Nl + (((SHA_LONG)len) << 3))&0xffffffffUL;
412eb6cfd0bSjsing 	/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
413eb6cfd0bSjsing 	 * Wei Dai <weidai@eskimo.com> for pointing it out. */
414eb6cfd0bSjsing 	if (l < c->Nl) /* overflow */
415eb6cfd0bSjsing 		c->Nh++;
416eb6cfd0bSjsing 	c->Nh+=(SHA_LONG)(len>>29);	/* might cause compiler warning on 16-bit */
417eb6cfd0bSjsing 	c->Nl = l;
418eb6cfd0bSjsing 
419eb6cfd0bSjsing 	n = c->num;
420eb6cfd0bSjsing 	if (n != 0) {
421eb6cfd0bSjsing 		p = (unsigned char *)c->data;
422eb6cfd0bSjsing 
423eb6cfd0bSjsing 		if (len >= SHA_CBLOCK || len + n >= SHA_CBLOCK) {
424eb6cfd0bSjsing 			memcpy(p + n, data, SHA_CBLOCK - n);
425eb6cfd0bSjsing 			sha1_block_data_order(c, p, 1);
426eb6cfd0bSjsing 			n = SHA_CBLOCK - n;
427eb6cfd0bSjsing 			data += n;
428eb6cfd0bSjsing 			len -= n;
429eb6cfd0bSjsing 			c->num = 0;
430eb6cfd0bSjsing 			memset(p,0,SHA_CBLOCK);	/* keep it zeroed */
431eb6cfd0bSjsing 		} else {
432eb6cfd0bSjsing 			memcpy(p + n, data, len);
433eb6cfd0bSjsing 			c->num += (unsigned int)len;
434eb6cfd0bSjsing 			return 1;
435eb6cfd0bSjsing 		}
436eb6cfd0bSjsing 	}
437eb6cfd0bSjsing 
438eb6cfd0bSjsing 	n = len/SHA_CBLOCK;
439eb6cfd0bSjsing 	if (n > 0) {
440eb6cfd0bSjsing 		sha1_block_data_order(c, data, n);
441eb6cfd0bSjsing 		n    *= SHA_CBLOCK;
442eb6cfd0bSjsing 		data += n;
443eb6cfd0bSjsing 		len -= n;
444eb6cfd0bSjsing 	}
445eb6cfd0bSjsing 
446eb6cfd0bSjsing 	if (len != 0) {
447eb6cfd0bSjsing 		p = (unsigned char *)c->data;
448eb6cfd0bSjsing 		c->num = (unsigned int)len;
449eb6cfd0bSjsing 		memcpy(p, data, len);
450eb6cfd0bSjsing 	}
451eb6cfd0bSjsing 	return 1;
452eb6cfd0bSjsing }
45365be244dSbeck LCRYPTO_ALIAS(SHA1_Update);
454eb6cfd0bSjsing 
455eb6cfd0bSjsing void
SHA1_Transform(SHA_CTX * c,const unsigned char * data)456eb6cfd0bSjsing SHA1_Transform(SHA_CTX *c, const unsigned char *data)
457eb6cfd0bSjsing {
458eb6cfd0bSjsing 	sha1_block_data_order(c, data, 1);
459eb6cfd0bSjsing }
46065be244dSbeck LCRYPTO_ALIAS(SHA1_Transform);
461eb6cfd0bSjsing 
462eb6cfd0bSjsing int
SHA1_Final(unsigned char * md,SHA_CTX * c)463eb6cfd0bSjsing SHA1_Final(unsigned char *md, SHA_CTX *c)
464eb6cfd0bSjsing {
465eb6cfd0bSjsing 	unsigned char *p = (unsigned char *)c->data;
466eb6cfd0bSjsing 	size_t n = c->num;
467eb6cfd0bSjsing 
468eb6cfd0bSjsing 	p[n] = 0x80; /* there is always room for one */
469eb6cfd0bSjsing 	n++;
470eb6cfd0bSjsing 
471eb6cfd0bSjsing 	if (n > (SHA_CBLOCK - 8)) {
472eb6cfd0bSjsing 		memset(p + n, 0, SHA_CBLOCK - n);
473eb6cfd0bSjsing 		n = 0;
474eb6cfd0bSjsing 		sha1_block_data_order(c, p, 1);
475eb6cfd0bSjsing 	}
476eb6cfd0bSjsing 
477c2de78a7Sjsing 	memset(p + n, 0, SHA_CBLOCK - 8 - n);
478c2de78a7Sjsing 	c->data[SHA_LBLOCK - 2] = htobe32(c->Nh);
479c2de78a7Sjsing 	c->data[SHA_LBLOCK - 1] = htobe32(c->Nl);
480c2de78a7Sjsing 
481eb6cfd0bSjsing 	sha1_block_data_order(c, p, 1);
482eb6cfd0bSjsing 	c->num = 0;
483eb6cfd0bSjsing 	memset(p, 0, SHA_CBLOCK);
484eb6cfd0bSjsing 
485c2de78a7Sjsing 	crypto_store_htobe32(&md[0 * 4], c->h0);
486c2de78a7Sjsing 	crypto_store_htobe32(&md[1 * 4], c->h1);
487c2de78a7Sjsing 	crypto_store_htobe32(&md[2 * 4], c->h2);
488c2de78a7Sjsing 	crypto_store_htobe32(&md[3 * 4], c->h3);
489c2de78a7Sjsing 	crypto_store_htobe32(&md[4 * 4], c->h4);
490eb6cfd0bSjsing 
491eb6cfd0bSjsing 	return 1;
492eb6cfd0bSjsing }
49365be244dSbeck LCRYPTO_ALIAS(SHA1_Final);
494eb6cfd0bSjsing 
49544f1c734Sjsing unsigned char *
SHA1(const unsigned char * d,size_t n,unsigned char * md)49644f1c734Sjsing SHA1(const unsigned char *d, size_t n, unsigned char *md)
49744f1c734Sjsing {
49844f1c734Sjsing 	SHA_CTX c;
49944f1c734Sjsing 
50044f1c734Sjsing 	if (!SHA1_Init(&c))
50144f1c734Sjsing 		return NULL;
50244f1c734Sjsing 	SHA1_Update(&c, d, n);
50344f1c734Sjsing 	SHA1_Final(md, &c);
50444f1c734Sjsing 
50544f1c734Sjsing 	explicit_bzero(&c, sizeof(c));
50644f1c734Sjsing 
50744f1c734Sjsing 	return (md);
50844f1c734Sjsing }
50965be244dSbeck LCRYPTO_ALIAS(SHA1);
51044f1c734Sjsing 
51144f1c734Sjsing #endif
512