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