xref: /minix/external/bsd/bind/dist/lib/isc/sha1.c (revision fb9c64b2)
1 /*	$NetBSD: sha1.c,v 1.8 2015/07/08 17:28:59 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /* Id */
21 
22 /*	NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp 	*/
23 /*	$OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $	*/
24 
25 /*! \file
26  * SHA-1 in C
27  * \author By Steve Reid <steve@edmweb.com>
28  * 100% Public Domain
29  * \verbatim
30  * Test Vectors (from FIPS PUB 180-1)
31  * "abc"
32  *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
33  * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
34  *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
35  * A million repetitions of "a"
36  *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
37  * \endverbatim
38  */
39 
40 #include "config.h"
41 
42 #include <isc/assertions.h>
43 #include <isc/platform.h>
44 #include <isc/sha1.h>
45 #include <isc/string.h>
46 #include <isc/types.h>
47 #include <isc/util.h>
48 
49 #if PKCS11CRYPTO
50 #include <pk11/internal.h>
51 #include <pk11/pk11.h>
52 #endif
53 
54 #ifdef ISC_PLATFORM_OPENSSLHASH
55 void
56 isc_sha1_init(isc_sha1_t *context)
57 {
58 	INSIST(context != NULL);
59 
60 	RUNTIME_CHECK(EVP_DigestInit(context, EVP_sha1()) == 1);
61 }
62 
63 void
64 isc_sha1_invalidate(isc_sha1_t *context) {
65 	EVP_MD_CTX_cleanup(context);
66 }
67 
68 void
69 isc_sha1_update(isc_sha1_t *context, const unsigned char *data,
70 		unsigned int len)
71 {
72 	INSIST(context != 0);
73 	INSIST(data != 0);
74 
75 	RUNTIME_CHECK(EVP_DigestUpdate(context,
76 				       (const void *) data,
77 				       (size_t) len) == 1);
78 }
79 
80 void
81 isc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
82 	INSIST(digest != 0);
83 	INSIST(context != 0);
84 
85 	RUNTIME_CHECK(EVP_DigestFinal(context, digest, NULL) == 1);
86 }
87 
88 #elif PKCS11CRYPTO
89 
90 void
91 isc_sha1_init(isc_sha1_t *ctx) {
92 	CK_RV rv;
93 	CK_MECHANISM mech = { CKM_SHA_1, NULL, 0 };
94 
95 	RUNTIME_CHECK(pk11_get_session(ctx, OP_DIGEST, ISC_TRUE, ISC_FALSE,
96 				       ISC_FALSE, NULL, 0) == ISC_R_SUCCESS);
97 	PK11_FATALCHECK(pkcs_C_DigestInit, (ctx->session, &mech));
98 }
99 
100 void
101 isc_sha1_invalidate(isc_sha1_t *ctx) {
102 	CK_BYTE garbage[ISC_SHA1_DIGESTLENGTH];
103 	CK_ULONG len = ISC_SHA1_DIGESTLENGTH;
104 
105 	if (ctx->handle == NULL)
106 		return;
107 	(void) pkcs_C_DigestFinal(ctx->session, garbage, &len);
108 	memset(garbage, 0, sizeof(garbage));
109 	pk11_return_session(ctx);
110 }
111 
112 void
113 isc_sha1_update(isc_sha1_t *ctx, const unsigned char *buf, unsigned int len) {
114 	CK_RV rv;
115 	CK_BYTE_PTR pPart;
116 
117 	DE_CONST(buf, pPart);
118 	PK11_FATALCHECK(pkcs_C_DigestUpdate,
119 			(ctx->session, pPart, (CK_ULONG) len));
120 }
121 
122 void
123 isc_sha1_final(isc_sha1_t *ctx, unsigned char *digest) {
124 	CK_RV rv;
125 	CK_ULONG len = ISC_SHA1_DIGESTLENGTH;
126 
127 	PK11_FATALCHECK(pkcs_C_DigestFinal,
128 			(ctx->session, (CK_BYTE_PTR) digest, &len));
129 	pk11_return_session(ctx);
130 }
131 
132 #else
133 
134 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
135 
136 /*@{*/
137 /*!
138  * blk0() and blk() perform the initial expand.
139  * I got the idea of expanding during the round function from SSLeay
140  */
141 #if !defined(WORDS_BIGENDIAN)
142 # define blk0(i) \
143 	(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
144 	 | (rol(block->l[i], 8) & 0x00FF00FF))
145 #else
146 # define blk0(i) block->l[i]
147 #endif
148 #define blk(i) \
149 	(block->l[i & 15] = rol(block->l[(i + 13) & 15] \
150 				^ block->l[(i + 8) & 15] \
151 				^ block->l[(i + 2) & 15] \
152 				^ block->l[i & 15], 1))
153 
154 /*@}*/
155 /*@{*/
156 /*!
157  * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
158  */
159 #define R0(v,w,x,y,z,i) \
160 	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
161 	w = rol(w, 30);
162 #define R1(v,w,x,y,z,i) \
163 	z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
164 	w = rol(w, 30);
165 #define R2(v,w,x,y,z,i) \
166 	z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
167 	w = rol(w, 30);
168 #define R3(v,w,x,y,z,i) \
169 	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
170 	w = rol(w, 30);
171 #define R4(v,w,x,y,z,i) \
172 	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
173 	w = rol(w, 30);
174 
175 /*@}*/
176 
177 typedef union {
178 	unsigned char c[64];
179 	unsigned int l[16];
180 } CHAR64LONG16;
181 
182 #ifdef __sparc_v9__
183 static void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
184 		   isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
185 static void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
186 		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
187 static void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
188 		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
189 static void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
190 		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
191 
192 #define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
193 #define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
194 #define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
195 #define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
196 #define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
197 
198 static void
199 do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
200        isc_uint32_t *e, CHAR64LONG16 *block)
201 {
202 	nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2);
203 	nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5);
204 	nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8);
205 	nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
206 	nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14);
207 	nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17);
208 	nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
209 }
210 
211 static void
212 do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
213       isc_uint32_t *e, CHAR64LONG16 *block)
214 {
215 	nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22);
216 	nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25);
217 	nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28);
218 	nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
219 	nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34);
220 	nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37);
221 	nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
222 }
223 
224 static void
225 do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
226       isc_uint32_t *e, CHAR64LONG16 *block)
227 {
228 	nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42);
229 	nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45);
230 	nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48);
231 	nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
232 	nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54);
233 	nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57);
234 	nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
235 }
236 
237 static void
238 do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
239       isc_uint32_t *e, CHAR64LONG16 *block)
240 {
241 	nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62);
242 	nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65);
243 	nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68);
244 	nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
245 	nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74);
246 	nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77);
247 	nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
248 }
249 #endif
250 
251 /*!
252  * Hash a single 512-bit block. This is the core of the algorithm.
253  */
254 static void
255 transform(isc_uint32_t state[5], const unsigned char buffer[64]) {
256 	isc_uint32_t a, b, c, d, e;
257 	CHAR64LONG16 *block;
258 	CHAR64LONG16 workspace;
259 
260 	INSIST(buffer != NULL);
261 	INSIST(state != NULL);
262 
263 	block = &workspace;
264 	(void)memmove(block, buffer, 64);
265 
266 	/* Copy context->state[] to working vars */
267 	a = state[0];
268 	b = state[1];
269 	c = state[2];
270 	d = state[3];
271 	e = state[4];
272 
273 #ifdef __sparc_v9__
274 	do_R01(&a, &b, &c, &d, &e, block);
275 	do_R2(&a, &b, &c, &d, &e, block);
276 	do_R3(&a, &b, &c, &d, &e, block);
277 	do_R4(&a, &b, &c, &d, &e, block);
278 #else
279 	/* 4 rounds of 20 operations each. Loop unrolled. */
280 	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);
281 	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);
282 	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);
283 	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);
284 	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);
285 	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);
286 	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);
287 	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);
288 	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);
289 	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);
290 	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);
291 	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);
292 	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);
293 	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);
294 	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);
295 	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);
296 	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);
297 	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);
298 	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);
299 	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);
300 #endif
301 
302 	/* Add the working vars back into context.state[] */
303 	state[0] += a;
304 	state[1] += b;
305 	state[2] += c;
306 	state[3] += d;
307 	state[4] += e;
308 
309 	/* Wipe variables */
310 	a = b = c = d = e = 0;
311 	/* Avoid compiler warnings */
312 	POST(a); POST(b); POST(c); POST(d); POST(e);
313 }
314 
315 
316 /*!
317  * isc_sha1_init - Initialize new context
318  */
319 void
320 isc_sha1_init(isc_sha1_t *context)
321 {
322 	INSIST(context != NULL);
323 
324 	/* SHA1 initialization constants */
325 	context->state[0] = 0x67452301;
326 	context->state[1] = 0xEFCDAB89;
327 	context->state[2] = 0x98BADCFE;
328 	context->state[3] = 0x10325476;
329 	context->state[4] = 0xC3D2E1F0;
330 	context->count[0] = 0;
331 	context->count[1] = 0;
332 }
333 
334 void
335 isc_sha1_invalidate(isc_sha1_t *context) {
336 	memset(context, 0, sizeof(isc_sha1_t));
337 }
338 
339 /*!
340  * Run your data through this.
341  */
342 void
343 isc_sha1_update(isc_sha1_t *context, const unsigned char *data,
344 		unsigned int len)
345 {
346 	unsigned int i, j;
347 
348 	INSIST(context != 0);
349 	INSIST(data != 0);
350 
351 	j = context->count[0];
352 	if ((context->count[0] += len << 3) < j)
353 		context->count[1] += (len >> 29) + 1;
354 	j = (j >> 3) & 63;
355 	if ((j + len) > 63) {
356 		(void)memmove(&context->buffer[j], data, (i = 64 - j));
357 		transform(context->state, context->buffer);
358 		for (; i + 63 < len; i += 64)
359 			transform(context->state, &data[i]);
360 		j = 0;
361 	} else {
362 		i = 0;
363 	}
364 
365 	(void)memmove(&context->buffer[j], &data[i], len - i);
366 }
367 
368 
369 /*!
370  * Add padding and return the message digest.
371  */
372 
373 static const unsigned char final_200 = 128;
374 static const unsigned char final_0 = 0;
375 
376 void
377 isc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
378 	unsigned int i;
379 	unsigned char finalcount[8];
380 
381 	INSIST(digest != 0);
382 	INSIST(context != 0);
383 
384 	for (i = 0; i < 8; i++) {
385 		/* Endian independent */
386 		finalcount[i] = (unsigned char)
387 			((context->count[(i >= 4 ? 0 : 1)]
388 			  >> ((3 - (i & 3)) * 8)) & 255);
389 	}
390 
391 	isc_sha1_update(context, &final_200, 1);
392 	while ((context->count[0] & 504) != 448)
393 		isc_sha1_update(context, &final_0, 1);
394 	/* The next Update should cause a transform() */
395 	isc_sha1_update(context, finalcount, 8);
396 
397 	if (digest) {
398 		for (i = 0; i < 20; i++)
399 			digest[i] = (unsigned char)
400 				((context->state[i >> 2]
401 				  >> ((3 - (i & 3)) * 8)) & 255);
402 	}
403 
404 	memset(context, 0, sizeof(isc_sha1_t));
405 }
406 #endif
407