]>
Commit | Line | Data |
---|---|---|
00476268 | 1 | /* |
3ce8e121 KZ |
2 | * No copyright is claimed. This code is in the public domain; do with |
3 | * it what you wish. | |
4 | * | |
3ae2cb49 KZ |
5 | * SHA-1 in C by Steve Reid <steve@edmweb.com> |
6 | * 100% Public Domain | |
7 | * | |
8 | * Test Vectors (from FIPS PUB 180-1) | |
9 | * 1) "abc": A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D | |
10 | * 2) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq": 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 | |
11 | * 3) A million repetitions of "a": 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F | |
12 | */ | |
00476268 | 13 | |
3ae2cb49 | 14 | #define UL_SHA1HANDSOFF |
00476268 PP |
15 | |
16 | #include <stdio.h> | |
17 | #include <string.h> | |
00476268 PP |
18 | #include <stdint.h> |
19 | ||
20 | #include "sha1.h" | |
21 | ||
00476268 PP |
22 | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
23 | ||
24 | /* blk0() and blk() perform the initial expand. */ | |
83b418c1 | 25 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
3ae2cb49 | 26 | # define blk0(i) block->l[i] |
00476268 | 27 | #else |
4ff4b110 KZ |
28 | # define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \ |
29 | |(rol(block->l[i],8)&0x00FF00FF)) | |
00476268 | 30 | #endif |
3ae2cb49 | 31 | |
00476268 PP |
32 | #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \ |
33 | ^block->l[(i+2)&15]^block->l[i&15],1)) | |
34 | ||
35 | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ | |
36 | #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30); | |
37 | #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30); | |
38 | #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30); | |
39 | #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30); | |
40 | #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30); | |
41 | ||
00476268 PP |
42 | /* Hash a single 512-bit block. This is the core of the algorithm. */ |
43 | ||
3ae2cb49 | 44 | void ul_SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) |
00476268 | 45 | { |
3ae2cb49 | 46 | uint32_t a, b, c, d, e; |
00476268 | 47 | |
3ae2cb49 KZ |
48 | typedef union { |
49 | unsigned char c[64]; | |
50 | uint32_t l[16]; | |
51 | } CHAR64LONG16; | |
00476268 | 52 | |
3ae2cb49 KZ |
53 | #ifdef UL_SHA1HANDSOFF |
54 | CHAR64LONG16 block[1]; /* use array to appear as a pointer */ | |
00476268 | 55 | |
3ae2cb49 | 56 | memcpy(block, buffer, 64); |
00476268 | 57 | #else |
3ae2cb49 KZ |
58 | /* The following had better never be used because it causes the |
59 | * pointer-to-const buffer to be cast into a pointer to non-const. | |
60 | * And the result is written through. I threw a "const" in, hoping | |
61 | * this will cause a diagnostic. | |
62 | */ | |
63 | CHAR64LONG16 *block = (const CHAR64LONG16 *)buffer; | |
00476268 | 64 | #endif |
3ae2cb49 KZ |
65 | /* Copy context->state[] to working vars */ |
66 | a = state[0]; | |
67 | b = state[1]; | |
68 | c = state[2]; | |
69 | d = state[3]; | |
70 | e = state[4]; | |
71 | /* 4 rounds of 20 operations each. Loop unrolled. */ | |
72 | R0(a, b, c, d, e, 0); | |
73 | R0(e, a, b, c, d, 1); | |
74 | R0(d, e, a, b, c, 2); | |
75 | R0(c, d, e, a, b, 3); | |
76 | R0(b, c, d, e, a, 4); | |
77 | R0(a, b, c, d, e, 5); | |
78 | R0(e, a, b, c, d, 6); | |
79 | R0(d, e, a, b, c, 7); | |
80 | R0(c, d, e, a, b, 8); | |
81 | R0(b, c, d, e, a, 9); | |
82 | R0(a, b, c, d, e, 10); | |
83 | R0(e, a, b, c, d, 11); | |
84 | R0(d, e, a, b, c, 12); | |
85 | R0(c, d, e, a, b, 13); | |
86 | R0(b, c, d, e, a, 14); | |
87 | R0(a, b, c, d, e, 15); | |
88 | R1(e, a, b, c, d, 16); | |
89 | R1(d, e, a, b, c, 17); | |
90 | R1(c, d, e, a, b, 18); | |
91 | R1(b, c, d, e, a, 19); | |
92 | R2(a, b, c, d, e, 20); | |
93 | R2(e, a, b, c, d, 21); | |
94 | R2(d, e, a, b, c, 22); | |
95 | R2(c, d, e, a, b, 23); | |
96 | R2(b, c, d, e, a, 24); | |
97 | R2(a, b, c, d, e, 25); | |
98 | R2(e, a, b, c, d, 26); | |
99 | R2(d, e, a, b, c, 27); | |
100 | R2(c, d, e, a, b, 28); | |
101 | R2(b, c, d, e, a, 29); | |
102 | R2(a, b, c, d, e, 30); | |
103 | R2(e, a, b, c, d, 31); | |
104 | R2(d, e, a, b, c, 32); | |
105 | R2(c, d, e, a, b, 33); | |
106 | R2(b, c, d, e, a, 34); | |
107 | R2(a, b, c, d, e, 35); | |
108 | R2(e, a, b, c, d, 36); | |
109 | R2(d, e, a, b, c, 37); | |
110 | R2(c, d, e, a, b, 38); | |
111 | R2(b, c, d, e, a, 39); | |
112 | R3(a, b, c, d, e, 40); | |
113 | R3(e, a, b, c, d, 41); | |
114 | R3(d, e, a, b, c, 42); | |
115 | R3(c, d, e, a, b, 43); | |
116 | R3(b, c, d, e, a, 44); | |
117 | R3(a, b, c, d, e, 45); | |
118 | R3(e, a, b, c, d, 46); | |
119 | R3(d, e, a, b, c, 47); | |
120 | R3(c, d, e, a, b, 48); | |
121 | R3(b, c, d, e, a, 49); | |
122 | R3(a, b, c, d, e, 50); | |
123 | R3(e, a, b, c, d, 51); | |
124 | R3(d, e, a, b, c, 52); | |
125 | R3(c, d, e, a, b, 53); | |
126 | R3(b, c, d, e, a, 54); | |
127 | R3(a, b, c, d, e, 55); | |
128 | R3(e, a, b, c, d, 56); | |
129 | R3(d, e, a, b, c, 57); | |
130 | R3(c, d, e, a, b, 58); | |
131 | R3(b, c, d, e, a, 59); | |
132 | R4(a, b, c, d, e, 60); | |
133 | R4(e, a, b, c, d, 61); | |
134 | R4(d, e, a, b, c, 62); | |
135 | R4(c, d, e, a, b, 63); | |
136 | R4(b, c, d, e, a, 64); | |
137 | R4(a, b, c, d, e, 65); | |
138 | R4(e, a, b, c, d, 66); | |
139 | R4(d, e, a, b, c, 67); | |
140 | R4(c, d, e, a, b, 68); | |
141 | R4(b, c, d, e, a, 69); | |
142 | R4(a, b, c, d, e, 70); | |
143 | R4(e, a, b, c, d, 71); | |
144 | R4(d, e, a, b, c, 72); | |
145 | R4(c, d, e, a, b, 73); | |
146 | R4(b, c, d, e, a, 74); | |
147 | R4(a, b, c, d, e, 75); | |
148 | R4(e, a, b, c, d, 76); | |
149 | R4(d, e, a, b, c, 77); | |
150 | R4(c, d, e, a, b, 78); | |
151 | R4(b, c, d, e, a, 79); | |
152 | /* Add the working vars back into context.state[] */ | |
153 | state[0] += a; | |
154 | state[1] += b; | |
155 | state[2] += c; | |
156 | state[3] += d; | |
157 | state[4] += e; | |
158 | /* Wipe variables */ | |
4ebdd569 | 159 | #ifdef HAVE_EXPLICIT_BZERO |
a8902e4c TW |
160 | explicit_bzero(&a, sizeof(a)); |
161 | explicit_bzero(&b, sizeof(b)); | |
162 | explicit_bzero(&c, sizeof(c)); | |
163 | explicit_bzero(&d, sizeof(d)); | |
164 | explicit_bzero(&e, sizeof(e)); | |
4ebdd569 KZ |
165 | #else |
166 | a = b = c = d = e = 0; | |
167 | #endif | |
3ae2cb49 KZ |
168 | #ifdef UL_SHA1HANDSOFF |
169 | memset(block, '\0', sizeof(block)); | |
00476268 PP |
170 | #endif |
171 | } | |
172 | ||
00476268 PP |
173 | /* SHA1Init - Initialize new context */ |
174 | ||
3ae2cb49 | 175 | void ul_SHA1Init(UL_SHA1_CTX *context) |
00476268 | 176 | { |
3ae2cb49 KZ |
177 | /* SHA1 initialization constants */ |
178 | context->state[0] = 0x67452301; | |
179 | context->state[1] = 0xEFCDAB89; | |
180 | context->state[2] = 0x98BADCFE; | |
181 | context->state[3] = 0x10325476; | |
182 | context->state[4] = 0xC3D2E1F0; | |
183 | context->count[0] = context->count[1] = 0; | |
00476268 PP |
184 | } |
185 | ||
00476268 PP |
186 | /* Run your data through this. */ |
187 | ||
3ae2cb49 | 188 | void ul_SHA1Update(UL_SHA1_CTX *context, const unsigned char *data, uint32_t len) |
00476268 | 189 | { |
3ae2cb49 KZ |
190 | uint32_t i; |
191 | ||
192 | uint32_t j; | |
193 | ||
194 | j = context->count[0]; | |
195 | if ((context->count[0] += len << 3) < j) | |
196 | context->count[1]++; | |
197 | context->count[1] += (len >> 29); | |
198 | j = (j >> 3) & 63; | |
199 | if ((j + len) > 63) { | |
200 | memcpy(&context->buffer[j], data, (i = 64 - j)); | |
201 | ul_SHA1Transform(context->state, context->buffer); | |
202 | for (; i + 63 < len; i += 64) { | |
203 | ul_SHA1Transform(context->state, &data[i]); | |
204 | } | |
205 | j = 0; | |
206 | } else | |
207 | i = 0; | |
208 | memcpy(&context->buffer[j], &data[i], len - i); | |
00476268 PP |
209 | } |
210 | ||
00476268 PP |
211 | /* Add padding and return the message digest. */ |
212 | ||
3ae2cb49 | 213 | void ul_SHA1Final(unsigned char digest[20], UL_SHA1_CTX *context) |
00476268 | 214 | { |
3ae2cb49 | 215 | unsigned i; |
00476268 | 216 | |
3ae2cb49 | 217 | unsigned char finalcount[8]; |
00476268 | 218 | |
3ae2cb49 | 219 | unsigned char c; |
00476268 | 220 | |
3ae2cb49 KZ |
221 | #if 0 /* untested "improvement" by DHR */ |
222 | /* Convert context->count to a sequence of bytes | |
223 | * in finalcount. Second element first, but | |
224 | * big-endian order within element. | |
225 | * But we do it all backwards. | |
226 | */ | |
227 | unsigned char *fcp = &finalcount[8]; | |
00476268 | 228 | |
3ae2cb49 KZ |
229 | for (i = 0; i < 2; i++) { |
230 | uint32_t t = context->count[i]; | |
00476268 | 231 | |
3ae2cb49 | 232 | int j; |
00476268 | 233 | |
3ae2cb49 KZ |
234 | for (j = 0; j < 4; t >>= 8, j++) |
235 | *--fcp = (unsigned char)t} | |
00476268 | 236 | #else |
3ae2cb49 KZ |
237 | for (i = 0; i < 8; i++) { |
238 | finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */ | |
239 | } | |
00476268 | 240 | #endif |
3ae2cb49 KZ |
241 | c = 0200; |
242 | ul_SHA1Update(context, &c, 1); | |
243 | while ((context->count[0] & 504) != 448) { | |
244 | c = 0000; | |
245 | ul_SHA1Update(context, &c, 1); | |
246 | } | |
247 | ul_SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ | |
248 | for (i = 0; i < 20; i++) { | |
249 | digest[i] = (unsigned char) | |
250 | ((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255); | |
251 | } | |
252 | /* Wipe variables */ | |
253 | memset(context, '\0', sizeof(*context)); | |
254 | memset(&finalcount, '\0', sizeof(finalcount)); | |
00476268 PP |
255 | } |
256 | ||
3ae2cb49 | 257 | void ul_SHA1(char *hash_out, const char *str, unsigned len) |
00476268 | 258 | { |
3ae2cb49 KZ |
259 | UL_SHA1_CTX ctx; |
260 | unsigned int ii; | |
261 | ||
262 | ul_SHA1Init(&ctx); | |
263 | for (ii = 0; ii < len; ii += 1) | |
264 | ul_SHA1Update(&ctx, (const unsigned char *)str + ii, 1); | |
265 | ul_SHA1Final((unsigned char *)hash_out, &ctx); | |
266 | hash_out[20] = '\0'; | |
00476268 | 267 | } |