This source file includes following definitions.
- sha1_init_ctx
- set_uint32
- sha1_read_ctx
- sha1_finish_ctx
- sha1_buffer
- sha1_process_bytes
- sha1_process_block
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 #include <config.h>
25
26
27 #if HAVE_OPENSSL_SHA1
28 # define GL_OPENSSL_INLINE _GL_EXTERN_INLINE
29 #endif
30 #include "sha1.h"
31
32 #include <stdalign.h>
33 #include <stdint.h>
34 #include <string.h>
35
36 #include <byteswap.h>
37 #ifdef WORDS_BIGENDIAN
38 # define SWAP(n) (n)
39 #else
40 # define SWAP(n) bswap_32 (n)
41 #endif
42
43 #if ! HAVE_OPENSSL_SHA1
44
45
46
47 static const unsigned char fillbuf[64] = { 0x80, 0 };
48
49
50
51
52
53 void
54 sha1_init_ctx (struct sha1_ctx *ctx)
55 {
56 ctx->A = 0x67452301;
57 ctx->B = 0xefcdab89;
58 ctx->C = 0x98badcfe;
59 ctx->D = 0x10325476;
60 ctx->E = 0xc3d2e1f0;
61
62 ctx->total[0] = ctx->total[1] = 0;
63 ctx->buflen = 0;
64 }
65
66
67
68
69 static void
70 set_uint32 (char *cp, uint32_t v)
71 {
72 memcpy (cp, &v, sizeof v);
73 }
74
75
76
77 void *
78 sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf)
79 {
80 char *r = resbuf;
81 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
82 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
83 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
84 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
85 set_uint32 (r + 4 * sizeof ctx->E, SWAP (ctx->E));
86
87 return resbuf;
88 }
89
90
91
92 void *
93 sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf)
94 {
95
96 uint32_t bytes = ctx->buflen;
97 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
98
99
100 ctx->total[0] += bytes;
101 if (ctx->total[0] < bytes)
102 ++ctx->total[1];
103
104
105 ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
106 ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3);
107
108 memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
109
110
111 sha1_process_block (ctx->buffer, size * 4, ctx);
112
113 return sha1_read_ctx (ctx, resbuf);
114 }
115
116
117
118
119
120 void *
121 sha1_buffer (const char *buffer, size_t len, void *resblock)
122 {
123 struct sha1_ctx ctx;
124
125
126 sha1_init_ctx (&ctx);
127
128
129 sha1_process_bytes (buffer, len, &ctx);
130
131
132 return sha1_finish_ctx (&ctx, resblock);
133 }
134
135 void
136 sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx)
137 {
138
139
140 if (ctx->buflen != 0)
141 {
142 size_t left_over = ctx->buflen;
143 size_t add = 128 - left_over > len ? len : 128 - left_over;
144
145 memcpy (&((char *) ctx->buffer)[left_over], buffer, add);
146 ctx->buflen += add;
147
148 if (ctx->buflen > 64)
149 {
150 sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
151
152 ctx->buflen &= 63;
153
154
155 memcpy (ctx->buffer,
156 &((char *) ctx->buffer)[(left_over + add) & ~63],
157 ctx->buflen);
158 }
159
160 buffer = (const char *) buffer + add;
161 len -= add;
162 }
163
164
165 if (len >= 64)
166 {
167 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
168 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
169 if (UNALIGNED_P (buffer))
170 while (len > 64)
171 {
172 sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
173 buffer = (const char *) buffer + 64;
174 len -= 64;
175 }
176 else
177 #endif
178 {
179 sha1_process_block (buffer, len & ~63, ctx);
180 buffer = (const char *) buffer + (len & ~63);
181 len &= 63;
182 }
183 }
184
185
186 if (len > 0)
187 {
188 size_t left_over = ctx->buflen;
189
190 memcpy (&((char *) ctx->buffer)[left_over], buffer, len);
191 left_over += len;
192 if (left_over >= 64)
193 {
194 sha1_process_block (ctx->buffer, 64, ctx);
195 left_over -= 64;
196
197
198 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
199 }
200 ctx->buflen = left_over;
201 }
202 }
203
204
205
206
207 #define K1 0x5a827999
208 #define K2 0x6ed9eba1
209 #define K3 0x8f1bbcdc
210 #define K4 0xca62c1d6
211
212
213 #define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) )
214 #define F2(B,C,D) (B ^ C ^ D)
215 #define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) )
216 #define F4(B,C,D) (B ^ C ^ D)
217
218
219
220
221
222 void
223 sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx)
224 {
225 const uint32_t *words = buffer;
226 size_t nwords = len / sizeof (uint32_t);
227 const uint32_t *endp = words + nwords;
228 uint32_t x[16];
229 uint32_t a = ctx->A;
230 uint32_t b = ctx->B;
231 uint32_t c = ctx->C;
232 uint32_t d = ctx->D;
233 uint32_t e = ctx->E;
234 uint32_t lolen = len;
235
236
237
238
239 ctx->total[0] += lolen;
240 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
241
242 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
243
244 #define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \
245 ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \
246 , (x[I&0x0f] = rol(tm, 1)) )
247
248 #define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \
249 + F( B, C, D ) \
250 + K \
251 + M; \
252 B = rol( B, 30 ); \
253 } while(0)
254
255 while (words < endp)
256 {
257 uint32_t tm;
258 int t;
259 for (t = 0; t < 16; t++)
260 {
261 x[t] = SWAP (*words);
262 words++;
263 }
264
265 R( a, b, c, d, e, F1, K1, x[ 0] );
266 R( e, a, b, c, d, F1, K1, x[ 1] );
267 R( d, e, a, b, c, F1, K1, x[ 2] );
268 R( c, d, e, a, b, F1, K1, x[ 3] );
269 R( b, c, d, e, a, F1, K1, x[ 4] );
270 R( a, b, c, d, e, F1, K1, x[ 5] );
271 R( e, a, b, c, d, F1, K1, x[ 6] );
272 R( d, e, a, b, c, F1, K1, x[ 7] );
273 R( c, d, e, a, b, F1, K1, x[ 8] );
274 R( b, c, d, e, a, F1, K1, x[ 9] );
275 R( a, b, c, d, e, F1, K1, x[10] );
276 R( e, a, b, c, d, F1, K1, x[11] );
277 R( d, e, a, b, c, F1, K1, x[12] );
278 R( c, d, e, a, b, F1, K1, x[13] );
279 R( b, c, d, e, a, F1, K1, x[14] );
280 R( a, b, c, d, e, F1, K1, x[15] );
281 R( e, a, b, c, d, F1, K1, M(16) );
282 R( d, e, a, b, c, F1, K1, M(17) );
283 R( c, d, e, a, b, F1, K1, M(18) );
284 R( b, c, d, e, a, F1, K1, M(19) );
285 R( a, b, c, d, e, F2, K2, M(20) );
286 R( e, a, b, c, d, F2, K2, M(21) );
287 R( d, e, a, b, c, F2, K2, M(22) );
288 R( c, d, e, a, b, F2, K2, M(23) );
289 R( b, c, d, e, a, F2, K2, M(24) );
290 R( a, b, c, d, e, F2, K2, M(25) );
291 R( e, a, b, c, d, F2, K2, M(26) );
292 R( d, e, a, b, c, F2, K2, M(27) );
293 R( c, d, e, a, b, F2, K2, M(28) );
294 R( b, c, d, e, a, F2, K2, M(29) );
295 R( a, b, c, d, e, F2, K2, M(30) );
296 R( e, a, b, c, d, F2, K2, M(31) );
297 R( d, e, a, b, c, F2, K2, M(32) );
298 R( c, d, e, a, b, F2, K2, M(33) );
299 R( b, c, d, e, a, F2, K2, M(34) );
300 R( a, b, c, d, e, F2, K2, M(35) );
301 R( e, a, b, c, d, F2, K2, M(36) );
302 R( d, e, a, b, c, F2, K2, M(37) );
303 R( c, d, e, a, b, F2, K2, M(38) );
304 R( b, c, d, e, a, F2, K2, M(39) );
305 R( a, b, c, d, e, F3, K3, M(40) );
306 R( e, a, b, c, d, F3, K3, M(41) );
307 R( d, e, a, b, c, F3, K3, M(42) );
308 R( c, d, e, a, b, F3, K3, M(43) );
309 R( b, c, d, e, a, F3, K3, M(44) );
310 R( a, b, c, d, e, F3, K3, M(45) );
311 R( e, a, b, c, d, F3, K3, M(46) );
312 R( d, e, a, b, c, F3, K3, M(47) );
313 R( c, d, e, a, b, F3, K3, M(48) );
314 R( b, c, d, e, a, F3, K3, M(49) );
315 R( a, b, c, d, e, F3, K3, M(50) );
316 R( e, a, b, c, d, F3, K3, M(51) );
317 R( d, e, a, b, c, F3, K3, M(52) );
318 R( c, d, e, a, b, F3, K3, M(53) );
319 R( b, c, d, e, a, F3, K3, M(54) );
320 R( a, b, c, d, e, F3, K3, M(55) );
321 R( e, a, b, c, d, F3, K3, M(56) );
322 R( d, e, a, b, c, F3, K3, M(57) );
323 R( c, d, e, a, b, F3, K3, M(58) );
324 R( b, c, d, e, a, F3, K3, M(59) );
325 R( a, b, c, d, e, F4, K4, M(60) );
326 R( e, a, b, c, d, F4, K4, M(61) );
327 R( d, e, a, b, c, F4, K4, M(62) );
328 R( c, d, e, a, b, F4, K4, M(63) );
329 R( b, c, d, e, a, F4, K4, M(64) );
330 R( a, b, c, d, e, F4, K4, M(65) );
331 R( e, a, b, c, d, F4, K4, M(66) );
332 R( d, e, a, b, c, F4, K4, M(67) );
333 R( c, d, e, a, b, F4, K4, M(68) );
334 R( b, c, d, e, a, F4, K4, M(69) );
335 R( a, b, c, d, e, F4, K4, M(70) );
336 R( e, a, b, c, d, F4, K4, M(71) );
337 R( d, e, a, b, c, F4, K4, M(72) );
338 R( c, d, e, a, b, F4, K4, M(73) );
339 R( b, c, d, e, a, F4, K4, M(74) );
340 R( a, b, c, d, e, F4, K4, M(75) );
341 R( e, a, b, c, d, F4, K4, M(76) );
342 R( d, e, a, b, c, F4, K4, M(77) );
343 R( c, d, e, a, b, F4, K4, M(78) );
344 R( b, c, d, e, a, F4, K4, M(79) );
345
346 a = ctx->A += a;
347 b = ctx->B += b;
348 c = ctx->C += c;
349 d = ctx->D += d;
350 e = ctx->E += e;
351 }
352 }
353
354 #endif
355
356
357
358
359
360
361