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