1 /**
2 * @file sha1.c SHA-1 in C
3 */
4
5 /*
6 By Steve Reid <sreid@sea-to-sky.net>
7 100% Public Domain
8
9 -----------------
10 Modified 7/98
11 By James H. Brown <jbrown@burgoyne.com>
12 Still 100% Public Domain
13
14 Corrected a problem which generated improper hash values on 16 bit machines
15 Routine SHA1Update changed from
16 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned int
17 len)
18 to
19 void SHA1Update(SHA1_CTX* context, unsigned char* data, unsigned
20 long len)
21
22 The 'len' parameter was declared an int which works fine on 32 bit machines.
23 However, on 16 bit machines an int is too small for the shifts being done
24 against
25 it. This caused the hash function to generate incorrect values if len was
26 greater than 8191 (8K - 1) due to the 'len << 3' on line 3 of SHA1Update().
27
28 Since the file IO in main() reads 16K at a time, any file 8K or larger would
29 be guaranteed to generate the wrong hash (e.g. Test Vector #3, a million
30 "a"s).
31
32 I also changed the declaration of variables i & j in SHA1Update to
33 unsigned long from unsigned int for the same reason.
34
35 These changes should make no difference to any 32 bit implementations since
36 an
37 int and a long are the same size in those environments.
38
39 --
40 I also corrected a few compiler warnings generated by Borland C.
41 1. Added #include <process.h> for exit() prototype
42 2. Removed unused variable 'j' in SHA1Final
43 3. Changed exit(0) to return(0) at end of main.
44
45 ALL changes I made can be located by searching for comments containing 'JHB'
46 -----------------
47 Modified 8/98
48 By Steve Reid <sreid@sea-to-sky.net>
49 Still 100% public domain
50
51 1- Removed #include <process.h> and used return() instead of exit()
52 2- Fixed overwriting of finalcount in SHA1Final() (discovered by Chris Hall)
53 3- Changed email address from steve@edmweb.com to sreid@sea-to-sky.net
54
55 -----------------
56 Modified 4/01
57 By Saul Kravitz <Saul.Kravitz@celera.com>
58 Still 100% PD
59 Modified to run on Compaq Alpha hardware.
60
61 -----------------
62 Modified 07/2002
63 By Ralph Giles <giles@artofcode.com>
64 Still 100% public domain
65 modified for use with stdint types, autoconf
66 code cleanup, removed attribution comments
67 switched SHA1Final() argument order for consistency
68 use SHA1_ prefix for public api
69 move public api to sha1.h
70 */
71
72 /*
73 Test Vectors (from FIPS PUB 180-1)
74 "abc"
75 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
76 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
77 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
78 A million repetitions of "a"
79 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
80 */
81
82 #define SHA1HANDSOFF 1
83
84 #ifdef HAVE_CONFIG_H
85 #include "config.h"
86 #endif
87
88 #include <stdio.h>
89 #include <string.h>
90 #include <re_types.h>
91 #include <re_sha.h>
92
93 void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64]);
94
95 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
96
97 #if defined (BYTE_ORDER) && defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
98 #define WORDS_BIGENDIAN 1
99 #endif
100 #ifdef _BIG_ENDIAN
101 #define WORDS_BIGENDIAN 1
102 #endif
103
104
105 /* blk0() and blk() perform the initial expand. */
106 /* I got the idea of expanding during the round function from SSLeay */
107 /* FIXME: can we do this in an endian-proof way? */
108 #ifdef WORDS_BIGENDIAN
109 #define blk0(i) block->l[i]
110 #else
111 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xff00ff00) \
112 |(rol(block->l[i],8)&0x00ff00ff))
113 #endif
114 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
115 ^block->l[(i+2)&15]^block->l[i&15],1))
116
117 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
118 #define R0(v,w,x,y,z,i) \
119 z+=((w&(x^y))^y)+blk0(i)+0x5a827999+rol(v,5);w=rol(w,30);
120 #define R1(v,w,x,y,z,i) \
121 z+=((w&(x^y))^y)+blk(i)+0x5a827999+rol(v,5);w=rol(w,30);
122 #define R2(v,w,x,y,z,i) \
123 z+=(w^x^y)+blk(i)+0x6ed9eba1+rol(v,5);w=rol(w,30);
124 #define R3(v,w,x,y,z,i) \
125 z+=(((w|x)&y)|(w&x))+blk(i)+0x8f1bbcdc+rol(v,5);w=rol(w,30);
126 #define R4(v,w,x,y,z,i) \
127 z+=(w^x^y)+blk(i)+0xca62c1d6+rol(v,5);w=rol(w,30);
128
129
130 /* Hash a single 512-bit block. This is the core of the algorithm. */
SHA1_Transform(uint32_t state[5],const uint8_t buffer[64])131 void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
132 {
133 uint32_t a, b, c, d, e;
134 typedef union {
135 uint8_t c[64];
136 uint32_t l[16];
137 } CHAR64LONG16;
138 CHAR64LONG16* block;
139
140 #ifdef SHA1HANDSOFF
141 CHAR64LONG16 workspace;
142 block = &workspace;
143 memcpy(block, buffer, 64);
144 #else
145 block = (CHAR64LONG16*)buffer;
146 #endif
147
148 /* Copy context->state[] to working vars */
149 a = state[0];
150 b = state[1];
151 c = state[2];
152 d = state[3];
153 e = state[4];
154
155 /* 4 rounds of 20 operations each. Loop unrolled. */
156 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);
157 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);
158 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);
159 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);
160 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);
161 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);
162 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);
163 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);
164 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);
165 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);
166 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);
167 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);
168 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);
169 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);
170 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);
171 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);
172 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);
173 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);
174 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);
175 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);
176
177 /* Add the working vars back into context.state[] */
178 state[0] += a;
179 state[1] += b;
180 state[2] += c;
181 state[3] += d;
182 state[4] += e;
183
184 /* Wipe variables */
185 a = b = c = d = e = 0;
186 }
187
188
189 /**
190 * Initialize new context
191 *
192 * @param context SHA1-Context
193 */
SHA1_Init(SHA1_CTX * context)194 void SHA1_Init(SHA1_CTX* context)
195 {
196 /* SHA1 initialization constants */
197 context->state[0] = 0x67452301;
198 context->state[1] = 0xefcdab89;
199 context->state[2] = 0x98badcfe;
200 context->state[3] = 0x10325476;
201 context->state[4] = 0xc3d2e1f0;
202 context->count[0] = context->count[1] = 0;
203 }
204
205
206 /**
207 * Run your data through this
208 *
209 * @param context SHA1-Context
210 * @param p Buffer to run SHA1 on
211 * @param len Number of bytes
212 */
SHA1_Update(SHA1_CTX * context,const void * p,size_t len)213 void SHA1_Update(SHA1_CTX* context, const void *p, size_t len)
214 {
215 const uint8_t* data = p;
216 size_t i, j;
217
218 j = (context->count[0] >> 3) & 63;
219 if ((context->count[0] += (uint32_t)(len << 3)) < (len << 3))
220 context->count[1]++;
221 context->count[1] += (uint32_t)(len >> 29);
222 if ((j + len) > 63) {
223 memcpy(&context->buffer[j], data, (i = 64-j));
224 SHA1_Transform(context->state, context->buffer);
225 for ( ; i + 63 < len; i += 64) {
226 SHA1_Transform(context->state, data + i);
227 }
228 j = 0;
229 }
230 else i = 0;
231 memcpy(&context->buffer[j], &data[i], len - i);
232 }
233
234
235 /**
236 * Add padding and return the message digest
237 *
238 * @param digest Generated message digest
239 * @param context SHA1-Context
240 */
SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE],SHA1_CTX * context)241 void SHA1_Final(uint8_t digest[SHA1_DIGEST_SIZE], SHA1_CTX* context)
242 {
243 uint32_t i;
244 uint8_t finalcount[8];
245
246 for (i = 0; i < 8; i++) {
247 finalcount[i] = (uint8_t)((context->count[(i >= 4 ? 0 : 1)]
248 >> ((3-(i & 3)) * 8) ) & 255);
249 }
250 SHA1_Update(context, (uint8_t *)"\200", 1);
251 while ((context->count[0] & 504) != 448) {
252 SHA1_Update(context, (uint8_t *)"\0", 1);
253 }
254 SHA1_Update(context, finalcount, 8); /* Should cause SHA1_Transform */
255 for (i = 0; i < SHA1_DIGEST_SIZE; i++) {
256 digest[i] = (uint8_t)
257 ((context->state[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
258 }
259
260 /* Wipe variables */
261 i = 0;
262 memset(context->buffer, 0, 64);
263 memset(context->state, 0, 20);
264 memset(context->count, 0, 8);
265 memset(finalcount, 0, 8); /* SWR */
266
267 #ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */
268 SHA1_Transform(context->state, context->buffer);
269 #endif
270 }
271