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