1135446Strhodes/*
2262706Serwin * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2012, 2014  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 2000, 2001, 2003  Internet Software Consortium.
4135446Strhodes *
5193149Sdougb * Permission to use, copy, modify, and/or distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18234010Sdougb/* $Id$ */
19135446Strhodes
20135446Strhodes/*	$NetBSD: sha1.c,v 1.5 2000/01/22 22:19:14 mycroft Exp $	*/
21135446Strhodes/*	$OpenBSD: sha1.c,v 1.9 1997/07/23 21:12:32 kstailey Exp $	*/
22135446Strhodes
23170222Sdougb/*! \file
24135446Strhodes * SHA-1 in C
25170222Sdougb * \author By Steve Reid <steve@edmweb.com>
26135446Strhodes * 100% Public Domain
27170222Sdougb * \verbatim
28135446Strhodes * Test Vectors (from FIPS PUB 180-1)
29135446Strhodes * "abc"
30135446Strhodes *   A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
31135446Strhodes * "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
32135446Strhodes *   84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
33135446Strhodes * A million repetitions of "a"
34135446Strhodes *   34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
35170222Sdougb * \endverbatim
36135446Strhodes */
37135446Strhodes
38135446Strhodes#include "config.h"
39135446Strhodes
40135446Strhodes#include <isc/assertions.h>
41224092Sdougb#include <isc/platform.h>
42135446Strhodes#include <isc/sha1.h>
43135446Strhodes#include <isc/string.h>
44135446Strhodes#include <isc/types.h>
45135446Strhodes#include <isc/util.h>
46135446Strhodes
47224092Sdougb#ifdef ISC_PLATFORM_OPENSSLHASH
48224092Sdougb
49224092Sdougbvoid
50224092Sdougbisc_sha1_init(isc_sha1_t *context)
51224092Sdougb{
52224092Sdougb	INSIST(context != NULL);
53224092Sdougb
54224092Sdougb	EVP_DigestInit(context, EVP_sha1());
55224092Sdougb}
56224092Sdougb
57224092Sdougbvoid
58224092Sdougbisc_sha1_invalidate(isc_sha1_t *context) {
59224092Sdougb	EVP_MD_CTX_cleanup(context);
60224092Sdougb}
61224092Sdougb
62224092Sdougbvoid
63224092Sdougbisc_sha1_update(isc_sha1_t *context, const unsigned char *data,
64224092Sdougb		unsigned int len)
65224092Sdougb{
66224092Sdougb	INSIST(context != 0);
67224092Sdougb	INSIST(data != 0);
68224092Sdougb
69224092Sdougb	EVP_DigestUpdate(context, (const void *) data, (size_t) len);
70224092Sdougb}
71224092Sdougb
72224092Sdougbvoid
73224092Sdougbisc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
74224092Sdougb	INSIST(digest != 0);
75224092Sdougb	INSIST(context != 0);
76224092Sdougb
77224092Sdougb	EVP_DigestFinal(context, digest, NULL);
78224092Sdougb}
79224092Sdougb
80224092Sdougb#else
81224092Sdougb
82135446Strhodes#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
83135446Strhodes
84170222Sdougb/*@{*/
85170222Sdougb/*!
86135446Strhodes * blk0() and blk() perform the initial expand.
87135446Strhodes * I got the idea of expanding during the round function from SSLeay
88135446Strhodes */
89135446Strhodes#if !defined(WORDS_BIGENDIAN)
90135446Strhodes# define blk0(i) \
91135446Strhodes	(block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) \
92135446Strhodes	 | (rol(block->l[i], 8) & 0x00FF00FF))
93135446Strhodes#else
94135446Strhodes# define blk0(i) block->l[i]
95135446Strhodes#endif
96135446Strhodes#define blk(i) \
97135446Strhodes	(block->l[i & 15] = rol(block->l[(i + 13) & 15] \
98135446Strhodes				^ block->l[(i + 8) & 15] \
99135446Strhodes				^ block->l[(i + 2) & 15] \
100135446Strhodes				^ block->l[i & 15], 1))
101135446Strhodes
102170222Sdougb/*@}*/
103170222Sdougb/*@{*/
104170222Sdougb/*!
105135446Strhodes * (R0+R1), R2, R3, R4 are the different operations (rounds) used in SHA1
106135446Strhodes */
107135446Strhodes#define R0(v,w,x,y,z,i) \
108135446Strhodes	z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
109135446Strhodes	w = rol(w, 30);
110135446Strhodes#define R1(v,w,x,y,z,i) \
111135446Strhodes	z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
112135446Strhodes	w = rol(w, 30);
113135446Strhodes#define R2(v,w,x,y,z,i) \
114135446Strhodes	z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
115135446Strhodes	w = rol(w, 30);
116135446Strhodes#define R3(v,w,x,y,z,i) \
117135446Strhodes	z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
118135446Strhodes	w = rol(w, 30);
119135446Strhodes#define R4(v,w,x,y,z,i) \
120135446Strhodes	z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
121135446Strhodes	w = rol(w, 30);
122135446Strhodes
123170222Sdougb/*@}*/
124170222Sdougb
125135446Strhodestypedef union {
126135446Strhodes	unsigned char c[64];
127135446Strhodes	unsigned int l[16];
128135446Strhodes} CHAR64LONG16;
129135446Strhodes
130135446Strhodes#ifdef __sparc_v9__
131135446Strhodesstatic void do_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
132135446Strhodes		   isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
133135446Strhodesstatic void do_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
134135446Strhodes		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
135135446Strhodesstatic void do_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
136135446Strhodes		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
137135446Strhodesstatic void do_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c,
138135446Strhodes		  isc_uint32_t *d, isc_uint32_t *e, CHAR64LONG16 *);
139135446Strhodes
140135446Strhodes#define nR0(v,w,x,y,z,i) R0(*v,*w,*x,*y,*z,i)
141135446Strhodes#define nR1(v,w,x,y,z,i) R1(*v,*w,*x,*y,*z,i)
142135446Strhodes#define nR2(v,w,x,y,z,i) R2(*v,*w,*x,*y,*z,i)
143135446Strhodes#define nR3(v,w,x,y,z,i) R3(*v,*w,*x,*y,*z,i)
144135446Strhodes#define nR4(v,w,x,y,z,i) R4(*v,*w,*x,*y,*z,i)
145135446Strhodes
146135446Strhodesstatic void
147135446Strhodesdo_R01(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
148135446Strhodes       isc_uint32_t *e, CHAR64LONG16 *block)
149135446Strhodes{
150135446Strhodes	nR0(a,b,c,d,e, 0); nR0(e,a,b,c,d, 1); nR0(d,e,a,b,c, 2);
151135446Strhodes	nR0(c,d,e,a,b, 3); nR0(b,c,d,e,a, 4); nR0(a,b,c,d,e, 5);
152135446Strhodes	nR0(e,a,b,c,d, 6); nR0(d,e,a,b,c, 7); nR0(c,d,e,a,b, 8);
153135446Strhodes	nR0(b,c,d,e,a, 9); nR0(a,b,c,d,e,10); nR0(e,a,b,c,d,11);
154135446Strhodes	nR0(d,e,a,b,c,12); nR0(c,d,e,a,b,13); nR0(b,c,d,e,a,14);
155135446Strhodes	nR0(a,b,c,d,e,15); nR1(e,a,b,c,d,16); nR1(d,e,a,b,c,17);
156135446Strhodes	nR1(c,d,e,a,b,18); nR1(b,c,d,e,a,19);
157135446Strhodes}
158135446Strhodes
159135446Strhodesstatic void
160135446Strhodesdo_R2(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
161135446Strhodes      isc_uint32_t *e, CHAR64LONG16 *block)
162135446Strhodes{
163135446Strhodes	nR2(a,b,c,d,e,20); nR2(e,a,b,c,d,21); nR2(d,e,a,b,c,22);
164135446Strhodes	nR2(c,d,e,a,b,23); nR2(b,c,d,e,a,24); nR2(a,b,c,d,e,25);
165135446Strhodes	nR2(e,a,b,c,d,26); nR2(d,e,a,b,c,27); nR2(c,d,e,a,b,28);
166135446Strhodes	nR2(b,c,d,e,a,29); nR2(a,b,c,d,e,30); nR2(e,a,b,c,d,31);
167135446Strhodes	nR2(d,e,a,b,c,32); nR2(c,d,e,a,b,33); nR2(b,c,d,e,a,34);
168135446Strhodes	nR2(a,b,c,d,e,35); nR2(e,a,b,c,d,36); nR2(d,e,a,b,c,37);
169135446Strhodes	nR2(c,d,e,a,b,38); nR2(b,c,d,e,a,39);
170135446Strhodes}
171135446Strhodes
172135446Strhodesstatic void
173135446Strhodesdo_R3(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
174135446Strhodes      isc_uint32_t *e, CHAR64LONG16 *block)
175135446Strhodes{
176135446Strhodes	nR3(a,b,c,d,e,40); nR3(e,a,b,c,d,41); nR3(d,e,a,b,c,42);
177135446Strhodes	nR3(c,d,e,a,b,43); nR3(b,c,d,e,a,44); nR3(a,b,c,d,e,45);
178135446Strhodes	nR3(e,a,b,c,d,46); nR3(d,e,a,b,c,47); nR3(c,d,e,a,b,48);
179135446Strhodes	nR3(b,c,d,e,a,49); nR3(a,b,c,d,e,50); nR3(e,a,b,c,d,51);
180135446Strhodes	nR3(d,e,a,b,c,52); nR3(c,d,e,a,b,53); nR3(b,c,d,e,a,54);
181135446Strhodes	nR3(a,b,c,d,e,55); nR3(e,a,b,c,d,56); nR3(d,e,a,b,c,57);
182135446Strhodes	nR3(c,d,e,a,b,58); nR3(b,c,d,e,a,59);
183135446Strhodes}
184135446Strhodes
185135446Strhodesstatic void
186135446Strhodesdo_R4(isc_uint32_t *a, isc_uint32_t *b, isc_uint32_t *c, isc_uint32_t *d,
187135446Strhodes      isc_uint32_t *e, CHAR64LONG16 *block)
188135446Strhodes{
189135446Strhodes	nR4(a,b,c,d,e,60); nR4(e,a,b,c,d,61); nR4(d,e,a,b,c,62);
190135446Strhodes	nR4(c,d,e,a,b,63); nR4(b,c,d,e,a,64); nR4(a,b,c,d,e,65);
191135446Strhodes	nR4(e,a,b,c,d,66); nR4(d,e,a,b,c,67); nR4(c,d,e,a,b,68);
192135446Strhodes	nR4(b,c,d,e,a,69); nR4(a,b,c,d,e,70); nR4(e,a,b,c,d,71);
193135446Strhodes	nR4(d,e,a,b,c,72); nR4(c,d,e,a,b,73); nR4(b,c,d,e,a,74);
194135446Strhodes	nR4(a,b,c,d,e,75); nR4(e,a,b,c,d,76); nR4(d,e,a,b,c,77);
195135446Strhodes	nR4(c,d,e,a,b,78); nR4(b,c,d,e,a,79);
196135446Strhodes}
197135446Strhodes#endif
198135446Strhodes
199170222Sdougb/*!
200135446Strhodes * Hash a single 512-bit block. This is the core of the algorithm.
201135446Strhodes */
202135446Strhodesstatic void
203135446Strhodestransform(isc_uint32_t state[5], const unsigned char buffer[64]) {
204135446Strhodes	isc_uint32_t a, b, c, d, e;
205135446Strhodes	CHAR64LONG16 *block;
206135446Strhodes	CHAR64LONG16 workspace;
207135446Strhodes
208135446Strhodes	INSIST(buffer != NULL);
209135446Strhodes	INSIST(state != NULL);
210135446Strhodes
211135446Strhodes	block = &workspace;
212262706Serwin	(void)memmove(block, buffer, 64);
213135446Strhodes
214135446Strhodes	/* Copy context->state[] to working vars */
215135446Strhodes	a = state[0];
216135446Strhodes	b = state[1];
217135446Strhodes	c = state[2];
218135446Strhodes	d = state[3];
219135446Strhodes	e = state[4];
220135446Strhodes
221135446Strhodes#ifdef __sparc_v9__
222135446Strhodes	do_R01(&a, &b, &c, &d, &e, block);
223135446Strhodes	do_R2(&a, &b, &c, &d, &e, block);
224135446Strhodes	do_R3(&a, &b, &c, &d, &e, block);
225135446Strhodes	do_R4(&a, &b, &c, &d, &e, block);
226135446Strhodes#else
227135446Strhodes	/* 4 rounds of 20 operations each. Loop unrolled. */
228135446Strhodes	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);
229135446Strhodes	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);
230135446Strhodes	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);
231135446Strhodes	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);
232135446Strhodes	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);
233135446Strhodes	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);
234135446Strhodes	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);
235135446Strhodes	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);
236135446Strhodes	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);
237135446Strhodes	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);
238135446Strhodes	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);
239135446Strhodes	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);
240135446Strhodes	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);
241135446Strhodes	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);
242135446Strhodes	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);
243135446Strhodes	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);
244135446Strhodes	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);
245135446Strhodes	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);
246135446Strhodes	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);
247135446Strhodes	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);
248135446Strhodes#endif
249135446Strhodes
250135446Strhodes	/* Add the working vars back into context.state[] */
251135446Strhodes	state[0] += a;
252135446Strhodes	state[1] += b;
253135446Strhodes	state[2] += c;
254135446Strhodes	state[3] += d;
255135446Strhodes	state[4] += e;
256135446Strhodes
257135446Strhodes	/* Wipe variables */
258135446Strhodes	a = b = c = d = e = 0;
259225361Sdougb	/* Avoid compiler warnings */
260225361Sdougb	POST(a); POST(b); POST(c); POST(d); POST(e);
261135446Strhodes}
262135446Strhodes
263135446Strhodes
264170222Sdougb/*!
265135446Strhodes * isc_sha1_init - Initialize new context
266135446Strhodes */
267135446Strhodesvoid
268135446Strhodesisc_sha1_init(isc_sha1_t *context)
269135446Strhodes{
270135446Strhodes	INSIST(context != NULL);
271135446Strhodes
272135446Strhodes	/* SHA1 initialization constants */
273135446Strhodes	context->state[0] = 0x67452301;
274135446Strhodes	context->state[1] = 0xEFCDAB89;
275135446Strhodes	context->state[2] = 0x98BADCFE;
276135446Strhodes	context->state[3] = 0x10325476;
277135446Strhodes	context->state[4] = 0xC3D2E1F0;
278135446Strhodes	context->count[0] = 0;
279135446Strhodes	context->count[1] = 0;
280135446Strhodes}
281135446Strhodes
282135446Strhodesvoid
283135446Strhodesisc_sha1_invalidate(isc_sha1_t *context) {
284135446Strhodes	memset(context, 0, sizeof(isc_sha1_t));
285135446Strhodes}
286135446Strhodes
287170222Sdougb/*!
288135446Strhodes * Run your data through this.
289135446Strhodes */
290135446Strhodesvoid
291135446Strhodesisc_sha1_update(isc_sha1_t *context, const unsigned char *data,
292135446Strhodes		unsigned int len)
293135446Strhodes{
294135446Strhodes	unsigned int i, j;
295135446Strhodes
296135446Strhodes	INSIST(context != 0);
297135446Strhodes	INSIST(data != 0);
298135446Strhodes
299135446Strhodes	j = context->count[0];
300135446Strhodes	if ((context->count[0] += len << 3) < j)
301135446Strhodes		context->count[1] += (len >> 29) + 1;
302135446Strhodes	j = (j >> 3) & 63;
303135446Strhodes	if ((j + len) > 63) {
304262706Serwin		(void)memmove(&context->buffer[j], data, (i = 64 - j));
305135446Strhodes		transform(context->state, context->buffer);
306135446Strhodes		for (; i + 63 < len; i += 64)
307135446Strhodes			transform(context->state, &data[i]);
308135446Strhodes		j = 0;
309135446Strhodes	} else {
310135446Strhodes		i = 0;
311135446Strhodes	}
312135446Strhodes
313262706Serwin	(void)memmove(&context->buffer[j], &data[i], len - i);
314135446Strhodes}
315135446Strhodes
316135446Strhodes
317170222Sdougb/*!
318135446Strhodes * Add padding and return the message digest.
319135446Strhodes */
320135446Strhodes
321135446Strhodesstatic const unsigned char final_200 = 128;
322135446Strhodesstatic const unsigned char final_0 = 0;
323135446Strhodes
324135446Strhodesvoid
325135446Strhodesisc_sha1_final(isc_sha1_t *context, unsigned char *digest) {
326135446Strhodes	unsigned int i;
327135446Strhodes	unsigned char finalcount[8];
328135446Strhodes
329135446Strhodes	INSIST(digest != 0);
330135446Strhodes	INSIST(context != 0);
331135446Strhodes
332135446Strhodes	for (i = 0; i < 8; i++) {
333135446Strhodes		/* Endian independent */
334135446Strhodes		finalcount[i] = (unsigned char)
335135446Strhodes			((context->count[(i >= 4 ? 0 : 1)]
336135446Strhodes			  >> ((3 - (i & 3)) * 8)) & 255);
337135446Strhodes	}
338135446Strhodes
339135446Strhodes	isc_sha1_update(context, &final_200, 1);
340135446Strhodes	while ((context->count[0] & 504) != 448)
341135446Strhodes		isc_sha1_update(context, &final_0, 1);
342135446Strhodes	/* The next Update should cause a transform() */
343135446Strhodes	isc_sha1_update(context, finalcount, 8);
344135446Strhodes
345135446Strhodes	if (digest) {
346135446Strhodes		for (i = 0; i < 20; i++)
347135446Strhodes			digest[i] = (unsigned char)
348135446Strhodes				((context->state[i >> 2]
349135446Strhodes				  >> ((3 - (i & 3)) * 8)) & 255);
350135446Strhodes	}
351135446Strhodes
352135446Strhodes	memset(context, 0, sizeof(isc_sha1_t));
353135446Strhodes}
354224092Sdougb#endif
355