Line | Count | Source |
1 | | /* |
2 | | SHA-1 in C |
3 | | By Steve Reid <steve@edmweb.com> |
4 | | 100% Public Domain |
5 | | |
6 | | Test Vectors (from FIPS PUB 180-1) |
7 | | "abc" |
8 | | A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
9 | | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" |
10 | | 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
11 | | A million repetitions of "a" |
12 | | 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
13 | | */ |
14 | | |
15 | | /* #define LITTLE_ENDIAN * This should be #define'd already, if true. */ |
16 | | /* #define SHA1HANDSOFF * Copies data before messing with it. */ |
17 | | |
18 | | #define SHA1HANDSOFF |
19 | | |
20 | | #if defined(__clang__) |
21 | | #elif defined(__GNUC__) |
22 | | #pragma GCC diagnostic push |
23 | | /* Ignore the case when SHA1Transform() called with 'char *', that code passed |
24 | | * buffer of 64 bytes anyway (at least now) */ |
25 | | #pragma GCC diagnostic ignored "-Wstringop-overread" |
26 | | #endif |
27 | | |
28 | | #include <stdio.h> |
29 | | #include <string.h> |
30 | | |
31 | | /* for uint32_t */ |
32 | | #include <stdint.h> |
33 | | |
34 | | #include "sha1.h" |
35 | | |
36 | | #include "util-internal.h" |
37 | | |
38 | 520k | #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits)))) |
39 | | |
40 | | /* blk0() and blk() perform the initial expand. */ |
41 | | /* I got the idea of expanding during the round function from SSLeay */ |
42 | | #if defined(LITTLE_ENDIAN) |
43 | | #define blk0(i) \ |
44 | 32.5k | (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | \ |
45 | 32.5k | (rol(block->l[i], 8) & 0x00FF00FF)) |
46 | | #elif defined(BIG_ENDIAN) |
47 | | #define blk0(i) block->l[i] |
48 | | #else |
49 | | #error "Endianness not defined!" |
50 | | #endif |
51 | | #define blk(i) \ |
52 | 130k | (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ \ |
53 | 130k | block->l[(i + 2) & 15] ^ block->l[i & 15], \ |
54 | 130k | 1)) |
55 | | |
56 | | /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ |
57 | | #define R0(v, w, x, y, z, i) \ |
58 | 32.5k | z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \ |
59 | 32.5k | w = rol(w, 30); |
60 | | #define R1(v, w, x, y, z, i) \ |
61 | 8.13k | z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \ |
62 | 8.13k | w = rol(w, 30); |
63 | | #define R2(v, w, x, y, z, i) \ |
64 | 40.6k | z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \ |
65 | 40.6k | w = rol(w, 30); |
66 | | #define R3(v, w, x, y, z, i) \ |
67 | 40.6k | z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \ |
68 | 40.6k | w = rol(w, 30); |
69 | | #define R4(v, w, x, y, z, i) \ |
70 | 40.6k | z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \ |
71 | 40.6k | w = rol(w, 30); |
72 | | |
73 | | typedef struct { |
74 | | uint32_t state[5]; |
75 | | uint32_t count[2]; |
76 | | unsigned char buffer[64]; |
77 | | } SHA1_CTX; |
78 | | |
79 | | static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]); |
80 | | |
81 | | static void SHA1Init(SHA1_CTX *context); |
82 | | |
83 | | static void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len); |
84 | | |
85 | | static void SHA1Final(unsigned char digest[20], SHA1_CTX *context); |
86 | | |
87 | | |
88 | | /* Hash a single 512-bit block. This is the core of the algorithm. */ |
89 | | |
90 | 2.03k | static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) { |
91 | 2.03k | uint32_t a, b, c, d, e; |
92 | | |
93 | 2.03k | typedef union { |
94 | 2.03k | unsigned char c[64]; |
95 | 2.03k | uint32_t l[16]; |
96 | 2.03k | } CHAR64LONG16; |
97 | | |
98 | 2.03k | #ifdef SHA1HANDSOFF |
99 | 2.03k | CHAR64LONG16 block[1]; /* use array to appear as a pointer */ |
100 | | |
101 | 2.03k | memcpy(block, buffer, 64); |
102 | | #else |
103 | | /* The following had better never be used because it causes the |
104 | | * pointer-to-const buffer to be cast into a pointer to non-const. |
105 | | * And the result is written through. I threw a "const" in, hoping |
106 | | * this will cause a diagnostic. |
107 | | */ |
108 | | CHAR64LONG16 *block = (const CHAR64LONG16 *)buffer; |
109 | | #endif |
110 | | /* Copy context->state[] to working vars */ |
111 | 2.03k | a = state[0]; |
112 | 2.03k | b = state[1]; |
113 | 2.03k | c = state[2]; |
114 | 2.03k | d = state[3]; |
115 | 2.03k | e = state[4]; |
116 | | /* 4 rounds of 20 operations each. Loop unrolled. */ |
117 | 2.03k | R0(a, b, c, d, e, 0); |
118 | 2.03k | R0(e, a, b, c, d, 1); |
119 | 2.03k | R0(d, e, a, b, c, 2); |
120 | 2.03k | R0(c, d, e, a, b, 3); |
121 | 2.03k | R0(b, c, d, e, a, 4); |
122 | 2.03k | R0(a, b, c, d, e, 5); |
123 | 2.03k | R0(e, a, b, c, d, 6); |
124 | 2.03k | R0(d, e, a, b, c, 7); |
125 | 2.03k | R0(c, d, e, a, b, 8); |
126 | 2.03k | R0(b, c, d, e, a, 9); |
127 | 2.03k | R0(a, b, c, d, e, 10); |
128 | 2.03k | R0(e, a, b, c, d, 11); |
129 | 2.03k | R0(d, e, a, b, c, 12); |
130 | 2.03k | R0(c, d, e, a, b, 13); |
131 | 2.03k | R0(b, c, d, e, a, 14); |
132 | 2.03k | R0(a, b, c, d, e, 15); |
133 | 2.03k | R1(e, a, b, c, d, 16); |
134 | 2.03k | R1(d, e, a, b, c, 17); |
135 | 2.03k | R1(c, d, e, a, b, 18); |
136 | 2.03k | R1(b, c, d, e, a, 19); |
137 | 2.03k | R2(a, b, c, d, e, 20); |
138 | 2.03k | R2(e, a, b, c, d, 21); |
139 | 2.03k | R2(d, e, a, b, c, 22); |
140 | 2.03k | R2(c, d, e, a, b, 23); |
141 | 2.03k | R2(b, c, d, e, a, 24); |
142 | 2.03k | R2(a, b, c, d, e, 25); |
143 | 2.03k | R2(e, a, b, c, d, 26); |
144 | 2.03k | R2(d, e, a, b, c, 27); |
145 | 2.03k | R2(c, d, e, a, b, 28); |
146 | 2.03k | R2(b, c, d, e, a, 29); |
147 | 2.03k | R2(a, b, c, d, e, 30); |
148 | 2.03k | R2(e, a, b, c, d, 31); |
149 | 2.03k | R2(d, e, a, b, c, 32); |
150 | 2.03k | R2(c, d, e, a, b, 33); |
151 | 2.03k | R2(b, c, d, e, a, 34); |
152 | 2.03k | R2(a, b, c, d, e, 35); |
153 | 2.03k | R2(e, a, b, c, d, 36); |
154 | 2.03k | R2(d, e, a, b, c, 37); |
155 | 2.03k | R2(c, d, e, a, b, 38); |
156 | 2.03k | R2(b, c, d, e, a, 39); |
157 | 2.03k | R3(a, b, c, d, e, 40); |
158 | 2.03k | R3(e, a, b, c, d, 41); |
159 | 2.03k | R3(d, e, a, b, c, 42); |
160 | 2.03k | R3(c, d, e, a, b, 43); |
161 | 2.03k | R3(b, c, d, e, a, 44); |
162 | 2.03k | R3(a, b, c, d, e, 45); |
163 | 2.03k | R3(e, a, b, c, d, 46); |
164 | 2.03k | R3(d, e, a, b, c, 47); |
165 | 2.03k | R3(c, d, e, a, b, 48); |
166 | 2.03k | R3(b, c, d, e, a, 49); |
167 | 2.03k | R3(a, b, c, d, e, 50); |
168 | 2.03k | R3(e, a, b, c, d, 51); |
169 | 2.03k | R3(d, e, a, b, c, 52); |
170 | 2.03k | R3(c, d, e, a, b, 53); |
171 | 2.03k | R3(b, c, d, e, a, 54); |
172 | 2.03k | R3(a, b, c, d, e, 55); |
173 | 2.03k | R3(e, a, b, c, d, 56); |
174 | 2.03k | R3(d, e, a, b, c, 57); |
175 | 2.03k | R3(c, d, e, a, b, 58); |
176 | 2.03k | R3(b, c, d, e, a, 59); |
177 | 2.03k | R4(a, b, c, d, e, 60); |
178 | 2.03k | R4(e, a, b, c, d, 61); |
179 | 2.03k | R4(d, e, a, b, c, 62); |
180 | 2.03k | R4(c, d, e, a, b, 63); |
181 | 2.03k | R4(b, c, d, e, a, 64); |
182 | 2.03k | R4(a, b, c, d, e, 65); |
183 | 2.03k | R4(e, a, b, c, d, 66); |
184 | 2.03k | R4(d, e, a, b, c, 67); |
185 | 2.03k | R4(c, d, e, a, b, 68); |
186 | 2.03k | R4(b, c, d, e, a, 69); |
187 | 2.03k | R4(a, b, c, d, e, 70); |
188 | 2.03k | R4(e, a, b, c, d, 71); |
189 | 2.03k | R4(d, e, a, b, c, 72); |
190 | 2.03k | R4(c, d, e, a, b, 73); |
191 | 2.03k | R4(b, c, d, e, a, 74); |
192 | 2.03k | R4(a, b, c, d, e, 75); |
193 | 2.03k | R4(e, a, b, c, d, 76); |
194 | 2.03k | R4(d, e, a, b, c, 77); |
195 | 2.03k | R4(c, d, e, a, b, 78); |
196 | 2.03k | R4(b, c, d, e, a, 79); |
197 | | /* Add the working vars back into context.state[] */ |
198 | 2.03k | state[0] += a; |
199 | 2.03k | state[1] += b; |
200 | 2.03k | state[2] += c; |
201 | 2.03k | state[3] += d; |
202 | 2.03k | state[4] += e; |
203 | 2.03k | #ifdef SHA1HANDSOFF |
204 | 2.03k | evutil_memclear_(block, sizeof(block)); |
205 | 2.03k | #endif |
206 | 2.03k | } |
207 | | |
208 | | /* SHA1Init - Initialize new context */ |
209 | | |
210 | 1.01k | static void SHA1Init(SHA1_CTX *context) { |
211 | | /* SHA1 initialization constants */ |
212 | 1.01k | context->state[0] = 0x67452301; |
213 | 1.01k | context->state[1] = 0xEFCDAB89; |
214 | 1.01k | context->state[2] = 0x98BADCFE; |
215 | 1.01k | context->state[3] = 0x10325476; |
216 | 1.01k | context->state[4] = 0xC3D2E1F0; |
217 | 1.01k | context->count[0] = context->count[1] = 0; |
218 | 1.01k | } |
219 | | |
220 | | /* Run your data through this. */ |
221 | | |
222 | 123k | static void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len) { |
223 | 123k | uint32_t i; |
224 | | |
225 | 123k | uint32_t j; |
226 | | |
227 | 123k | j = context->count[0]; |
228 | 123k | if ((context->count[0] += len << 3) < j) |
229 | 0 | context->count[1]++; |
230 | 123k | context->count[1] += (len >> 29); |
231 | 123k | j = (j >> 3) & 63; |
232 | 123k | if ((j + len) > 63) { |
233 | 2.03k | memcpy(&context->buffer[j], data, (i = 64 - j)); |
234 | 2.03k | SHA1Transform(context->state, context->buffer); |
235 | 2.03k | for (; i + 63 < len; i += 64) { |
236 | 0 | SHA1Transform(context->state, &data[i]); |
237 | 0 | } |
238 | 2.03k | j = 0; |
239 | 2.03k | } else |
240 | 121k | i = 0; |
241 | 123k | memcpy(&context->buffer[j], &data[i], len - i); |
242 | 123k | } |
243 | | |
244 | | /* Add padding and return the message digest. */ |
245 | | |
246 | 1.01k | static void SHA1Final(unsigned char digest[20], SHA1_CTX *context) { |
247 | 1.01k | unsigned i; |
248 | | |
249 | 1.01k | unsigned char finalcount[8]; |
250 | | |
251 | 1.01k | unsigned char c; |
252 | | |
253 | | #if 0 /* untested "improvement" by DHR */ |
254 | | /* Convert context->count to a sequence of bytes |
255 | | * in finalcount. Second element first, but |
256 | | * big-endian order within element. |
257 | | * But we do it all backwards. |
258 | | */ |
259 | | unsigned char *fcp = &finalcount[8]; |
260 | | |
261 | | for (i = 0; i < 2; i++) |
262 | | { |
263 | | uint32_t t = context->count[i]; |
264 | | |
265 | | int j; |
266 | | |
267 | | for (j = 0; j < 4; t >>= 8, j++) |
268 | | *--fcp = (unsigned char) t} |
269 | | #else |
270 | 9.15k | for (i = 0; i < 8; i++) { |
271 | 8.13k | finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> |
272 | 8.13k | ((3 - (i & 3)) * 8)) & |
273 | 8.13k | 255); /* Endian independent */ |
274 | 8.13k | } |
275 | 1.01k | #endif |
276 | 1.01k | c = 0200; |
277 | 1.01k | SHA1Update(context, &c, 1); |
278 | 61.0k | while ((context->count[0] & 504) != 448) { |
279 | 60.0k | c = 0000; |
280 | 60.0k | SHA1Update(context, &c, 1); |
281 | 60.0k | } |
282 | 1.01k | SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ |
283 | 21.3k | for (i = 0; i < 20; i++) { |
284 | 20.3k | digest[i] = |
285 | 20.3k | (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & |
286 | 20.3k | 255); |
287 | 20.3k | } |
288 | | /* Wipe variables */ |
289 | 1.01k | evutil_memclear_(context, sizeof(*context)); |
290 | 1.01k | evutil_memclear_(&finalcount, sizeof(finalcount)); |
291 | 1.01k | } |
292 | | |
293 | 1.01k | void builtin_SHA1(char *hash_out, const char *str, int len) { |
294 | 1.01k | SHA1_CTX ctx; |
295 | 1.01k | int ii; |
296 | | |
297 | 1.01k | SHA1Init(&ctx); |
298 | 62.0k | for (ii = 0; ii < len; ii += 1) |
299 | 61.0k | SHA1Update(&ctx, (const unsigned char *)str + ii, 1); |
300 | 1.01k | SHA1Final((unsigned char *)hash_out, &ctx); |
301 | 1.01k | } |