diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-18 10:38:18 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-18 10:38:18 +0200 |
commit | 06f719fd79fe15ce6fd5431bc58fcb22851de24d (patch) | |
tree | 2fc468c9a73e7a83440bbff24fb636a671ccb1cd /libbb/sha1.c | |
parent | 5fe2f863b9cee5ab0e7ac873538bce48846dbad8 (diff) | |
download | busybox-w32-06f719fd79fe15ce6fd5431bc58fcb22851de24d.tar.gz busybox-w32-06f719fd79fe15ce6fd5431bc58fcb22851de24d.tar.bz2 busybox-w32-06f719fd79fe15ce6fd5431bc58fcb22851de24d.zip |
libbb: rename hash source files. no code changes
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
Diffstat (limited to 'libbb/sha1.c')
-rw-r--r-- | libbb/sha1.c | 530 |
1 files changed, 0 insertions, 530 deletions
diff --git a/libbb/sha1.c b/libbb/sha1.c deleted file mode 100644 index d79291148..000000000 --- a/libbb/sha1.c +++ /dev/null | |||
@@ -1,530 +0,0 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Based on shasum from http://www.netsw.org/crypto/hash/ | ||
4 | * Majorly hacked up to use Dr Brian Gladman's sha1 code | ||
5 | * | ||
6 | * Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK. | ||
7 | * Copyright (C) 2003 Glenn L. McGrath | ||
8 | * Copyright (C) 2003 Erik Andersen | ||
9 | * | ||
10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. | ||
11 | * | ||
12 | * --------------------------------------------------------------------------- | ||
13 | * Issue Date: 10/11/2002 | ||
14 | * | ||
15 | * This is a byte oriented version of SHA1 that operates on arrays of bytes | ||
16 | * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor | ||
17 | * | ||
18 | * --------------------------------------------------------------------------- | ||
19 | * | ||
20 | * SHA256 and SHA512 parts are: | ||
21 | * Released into the Public Domain by Ulrich Drepper <drepper@redhat.com>. | ||
22 | * Shrank by Denys Vlasenko. | ||
23 | * | ||
24 | * --------------------------------------------------------------------------- | ||
25 | * | ||
26 | * The best way to test random blocksizes is to go to coreutils/md5_sha1_sum.c | ||
27 | * and replace "4096" with something like "2000 + time(NULL) % 2097", | ||
28 | * then rebuild and compare "shaNNNsum bigfile" results. | ||
29 | */ | ||
30 | |||
31 | #include "libbb.h" | ||
32 | |||
33 | /* gcc 4.2.1 optimizes rotr64 better with inline than with macro | ||
34 | * (for rotX32, there is no difference). Why? My guess is that | ||
35 | * macro requires clever common subexpression elimination heuristics | ||
36 | * in gcc, while inline basically forces it to happen. | ||
37 | */ | ||
38 | //#define rotl32(x,n) (((x) << (n)) | ((x) >> (32 - (n)))) | ||
39 | static ALWAYS_INLINE uint32_t rotl32(uint32_t x, unsigned n) | ||
40 | { | ||
41 | return (x << n) | (x >> (32 - n)); | ||
42 | } | ||
43 | //#define rotr32(x,n) (((x) >> (n)) | ((x) << (32 - (n)))) | ||
44 | static ALWAYS_INLINE uint32_t rotr32(uint32_t x, unsigned n) | ||
45 | { | ||
46 | return (x >> n) | (x << (32 - n)); | ||
47 | } | ||
48 | /* rotr64 in needed for sha512 only: */ | ||
49 | //#define rotr64(x,n) (((x) >> (n)) | ((x) << (64 - (n)))) | ||
50 | static ALWAYS_INLINE uint64_t rotr64(uint64_t x, unsigned n) | ||
51 | { | ||
52 | return (x >> n) | (x << (64 - n)); | ||
53 | } | ||
54 | #if BB_LITTLE_ENDIAN | ||
55 | /* ALWAYS_INLINE below would hurt code size, using plain inline: */ | ||
56 | static inline uint64_t hton64(uint64_t v) | ||
57 | { | ||
58 | return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32); | ||
59 | } | ||
60 | #else | ||
61 | #define hton64(v) (v) | ||
62 | #endif | ||
63 | #define ntoh64(v) hton64(v) | ||
64 | |||
65 | |||
66 | /* Some arch headers have conflicting defines */ | ||
67 | #undef ch | ||
68 | #undef parity | ||
69 | #undef maj | ||
70 | #undef rnd | ||
71 | |||
72 | static void FAST_FUNC sha1_process_block64(sha1_ctx_t *ctx) | ||
73 | { | ||
74 | unsigned t; | ||
75 | uint32_t W[80], a, b, c, d, e; | ||
76 | const uint32_t *words = (uint32_t*) ctx->wbuffer; | ||
77 | |||
78 | for (t = 0; t < 16; ++t) | ||
79 | W[t] = ntohl(words[t]); | ||
80 | for (/*t = 16*/; t < 80; ++t) { | ||
81 | uint32_t T = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]; | ||
82 | W[t] = rotl32(T, 1); | ||
83 | } | ||
84 | |||
85 | a = ctx->hash[0]; | ||
86 | b = ctx->hash[1]; | ||
87 | c = ctx->hash[2]; | ||
88 | d = ctx->hash[3]; | ||
89 | e = ctx->hash[4]; | ||
90 | |||
91 | /* Reverse byte order in 32-bit words */ | ||
92 | #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z)))) | ||
93 | #define parity(x,y,z) ((x) ^ (y) ^ (z)) | ||
94 | #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y)))) | ||
95 | /* A normal version as set out in the FIPS. This version uses */ | ||
96 | /* partial loop unrolling and is optimised for the Pentium 4 */ | ||
97 | #define rnd(f,k) \ | ||
98 | do { \ | ||
99 | uint32_t T = a; \ | ||
100 | a = rotl32(a, 5) + f(b, c, d) + e + k + W[t]; \ | ||
101 | e = d; \ | ||
102 | d = c; \ | ||
103 | c = rotl32(b, 30); \ | ||
104 | b = T; \ | ||
105 | } while (0) | ||
106 | |||
107 | for (t = 0; t < 20; ++t) | ||
108 | rnd(ch, 0x5a827999); | ||
109 | |||
110 | for (/*t = 20*/; t < 40; ++t) | ||
111 | rnd(parity, 0x6ed9eba1); | ||
112 | |||
113 | for (/*t = 40*/; t < 60; ++t) | ||
114 | rnd(maj, 0x8f1bbcdc); | ||
115 | |||
116 | for (/*t = 60*/; t < 80; ++t) | ||
117 | rnd(parity, 0xca62c1d6); | ||
118 | #undef ch | ||
119 | #undef parity | ||
120 | #undef maj | ||
121 | #undef rnd | ||
122 | |||
123 | ctx->hash[0] += a; | ||
124 | ctx->hash[1] += b; | ||
125 | ctx->hash[2] += c; | ||
126 | ctx->hash[3] += d; | ||
127 | ctx->hash[4] += e; | ||
128 | } | ||
129 | |||
130 | /* Constants for SHA512 from FIPS 180-2:4.2.3. | ||
131 | * SHA256 constants from FIPS 180-2:4.2.2 | ||
132 | * are the most significant half of first 64 elements | ||
133 | * of the same array. | ||
134 | */ | ||
135 | static const uint64_t sha_K[80] = { | ||
136 | 0x428a2f98d728ae22ULL, 0x7137449123ef65cdULL, | ||
137 | 0xb5c0fbcfec4d3b2fULL, 0xe9b5dba58189dbbcULL, | ||
138 | 0x3956c25bf348b538ULL, 0x59f111f1b605d019ULL, | ||
139 | 0x923f82a4af194f9bULL, 0xab1c5ed5da6d8118ULL, | ||
140 | 0xd807aa98a3030242ULL, 0x12835b0145706fbeULL, | ||
141 | 0x243185be4ee4b28cULL, 0x550c7dc3d5ffb4e2ULL, | ||
142 | 0x72be5d74f27b896fULL, 0x80deb1fe3b1696b1ULL, | ||
143 | 0x9bdc06a725c71235ULL, 0xc19bf174cf692694ULL, | ||
144 | 0xe49b69c19ef14ad2ULL, 0xefbe4786384f25e3ULL, | ||
145 | 0x0fc19dc68b8cd5b5ULL, 0x240ca1cc77ac9c65ULL, | ||
146 | 0x2de92c6f592b0275ULL, 0x4a7484aa6ea6e483ULL, | ||
147 | 0x5cb0a9dcbd41fbd4ULL, 0x76f988da831153b5ULL, | ||
148 | 0x983e5152ee66dfabULL, 0xa831c66d2db43210ULL, | ||
149 | 0xb00327c898fb213fULL, 0xbf597fc7beef0ee4ULL, | ||
150 | 0xc6e00bf33da88fc2ULL, 0xd5a79147930aa725ULL, | ||
151 | 0x06ca6351e003826fULL, 0x142929670a0e6e70ULL, | ||
152 | 0x27b70a8546d22ffcULL, 0x2e1b21385c26c926ULL, | ||
153 | 0x4d2c6dfc5ac42aedULL, 0x53380d139d95b3dfULL, | ||
154 | 0x650a73548baf63deULL, 0x766a0abb3c77b2a8ULL, | ||
155 | 0x81c2c92e47edaee6ULL, 0x92722c851482353bULL, | ||
156 | 0xa2bfe8a14cf10364ULL, 0xa81a664bbc423001ULL, | ||
157 | 0xc24b8b70d0f89791ULL, 0xc76c51a30654be30ULL, | ||
158 | 0xd192e819d6ef5218ULL, 0xd69906245565a910ULL, | ||
159 | 0xf40e35855771202aULL, 0x106aa07032bbd1b8ULL, | ||
160 | 0x19a4c116b8d2d0c8ULL, 0x1e376c085141ab53ULL, | ||
161 | 0x2748774cdf8eeb99ULL, 0x34b0bcb5e19b48a8ULL, | ||
162 | 0x391c0cb3c5c95a63ULL, 0x4ed8aa4ae3418acbULL, | ||
163 | 0x5b9cca4f7763e373ULL, 0x682e6ff3d6b2b8a3ULL, | ||
164 | 0x748f82ee5defb2fcULL, 0x78a5636f43172f60ULL, | ||
165 | 0x84c87814a1f0ab72ULL, 0x8cc702081a6439ecULL, | ||
166 | 0x90befffa23631e28ULL, 0xa4506cebde82bde9ULL, | ||
167 | 0xbef9a3f7b2c67915ULL, 0xc67178f2e372532bULL, | ||
168 | 0xca273eceea26619cULL, 0xd186b8c721c0c207ULL, /* [64]+ are used for sha512 only */ | ||
169 | 0xeada7dd6cde0eb1eULL, 0xf57d4f7fee6ed178ULL, | ||
170 | 0x06f067aa72176fbaULL, 0x0a637dc5a2c898a6ULL, | ||
171 | 0x113f9804bef90daeULL, 0x1b710b35131c471bULL, | ||
172 | 0x28db77f523047d84ULL, 0x32caab7b40c72493ULL, | ||
173 | 0x3c9ebe0a15c9bebcULL, 0x431d67c49c100d4cULL, | ||
174 | 0x4cc5d4becb3e42b6ULL, 0x597f299cfc657e2aULL, | ||
175 | 0x5fcb6fab3ad6faecULL, 0x6c44198c4a475817ULL | ||
176 | }; | ||
177 | |||
178 | #undef Ch | ||
179 | #undef Maj | ||
180 | #undef S0 | ||
181 | #undef S1 | ||
182 | #undef R0 | ||
183 | #undef R1 | ||
184 | |||
185 | static void FAST_FUNC sha256_process_block64(sha256_ctx_t *ctx) | ||
186 | { | ||
187 | unsigned t; | ||
188 | uint32_t W[64], a, b, c, d, e, f, g, h; | ||
189 | const uint32_t *words = (uint32_t*) ctx->wbuffer; | ||
190 | |||
191 | /* Operators defined in FIPS 180-2:4.1.2. */ | ||
192 | #define Ch(x, y, z) ((x & y) ^ (~x & z)) | ||
193 | #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) | ||
194 | #define S0(x) (rotr32(x, 2) ^ rotr32(x, 13) ^ rotr32(x, 22)) | ||
195 | #define S1(x) (rotr32(x, 6) ^ rotr32(x, 11) ^ rotr32(x, 25)) | ||
196 | #define R0(x) (rotr32(x, 7) ^ rotr32(x, 18) ^ (x >> 3)) | ||
197 | #define R1(x) (rotr32(x, 17) ^ rotr32(x, 19) ^ (x >> 10)) | ||
198 | |||
199 | /* Compute the message schedule according to FIPS 180-2:6.2.2 step 2. */ | ||
200 | for (t = 0; t < 16; ++t) | ||
201 | W[t] = ntohl(words[t]); | ||
202 | for (/*t = 16*/; t < 64; ++t) | ||
203 | W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; | ||
204 | |||
205 | a = ctx->hash[0]; | ||
206 | b = ctx->hash[1]; | ||
207 | c = ctx->hash[2]; | ||
208 | d = ctx->hash[3]; | ||
209 | e = ctx->hash[4]; | ||
210 | f = ctx->hash[5]; | ||
211 | g = ctx->hash[6]; | ||
212 | h = ctx->hash[7]; | ||
213 | |||
214 | /* The actual computation according to FIPS 180-2:6.2.2 step 3. */ | ||
215 | for (t = 0; t < 64; ++t) { | ||
216 | /* Need to fetch upper half of sha_K[t] | ||
217 | * (I hope compiler is clever enough to just fetch | ||
218 | * upper half) | ||
219 | */ | ||
220 | uint32_t K_t = sha_K[t] >> 32; | ||
221 | uint32_t T1 = h + S1(e) + Ch(e, f, g) + K_t + W[t]; | ||
222 | uint32_t T2 = S0(a) + Maj(a, b, c); | ||
223 | h = g; | ||
224 | g = f; | ||
225 | f = e; | ||
226 | e = d + T1; | ||
227 | d = c; | ||
228 | c = b; | ||
229 | b = a; | ||
230 | a = T1 + T2; | ||
231 | } | ||
232 | #undef Ch | ||
233 | #undef Maj | ||
234 | #undef S0 | ||
235 | #undef S1 | ||
236 | #undef R0 | ||
237 | #undef R1 | ||
238 | /* Add the starting values of the context according to FIPS 180-2:6.2.2 | ||
239 | step 4. */ | ||
240 | ctx->hash[0] += a; | ||
241 | ctx->hash[1] += b; | ||
242 | ctx->hash[2] += c; | ||
243 | ctx->hash[3] += d; | ||
244 | ctx->hash[4] += e; | ||
245 | ctx->hash[5] += f; | ||
246 | ctx->hash[6] += g; | ||
247 | ctx->hash[7] += h; | ||
248 | } | ||
249 | |||
250 | static void FAST_FUNC sha512_process_block128(sha512_ctx_t *ctx) | ||
251 | { | ||
252 | unsigned t; | ||
253 | uint64_t W[80]; | ||
254 | /* On i386, having assignments here (not later as sha256 does) | ||
255 | * produces 99 bytes smaller code with gcc 4.3.1 | ||
256 | */ | ||
257 | uint64_t a = ctx->hash[0]; | ||
258 | uint64_t b = ctx->hash[1]; | ||
259 | uint64_t c = ctx->hash[2]; | ||
260 | uint64_t d = ctx->hash[3]; | ||
261 | uint64_t e = ctx->hash[4]; | ||
262 | uint64_t f = ctx->hash[5]; | ||
263 | uint64_t g = ctx->hash[6]; | ||
264 | uint64_t h = ctx->hash[7]; | ||
265 | const uint64_t *words = (uint64_t*) ctx->wbuffer; | ||
266 | |||
267 | /* Operators defined in FIPS 180-2:4.1.2. */ | ||
268 | #define Ch(x, y, z) ((x & y) ^ (~x & z)) | ||
269 | #define Maj(x, y, z) ((x & y) ^ (x & z) ^ (y & z)) | ||
270 | #define S0(x) (rotr64(x, 28) ^ rotr64(x, 34) ^ rotr64(x, 39)) | ||
271 | #define S1(x) (rotr64(x, 14) ^ rotr64(x, 18) ^ rotr64(x, 41)) | ||
272 | #define R0(x) (rotr64(x, 1) ^ rotr64(x, 8) ^ (x >> 7)) | ||
273 | #define R1(x) (rotr64(x, 19) ^ rotr64(x, 61) ^ (x >> 6)) | ||
274 | |||
275 | /* Compute the message schedule according to FIPS 180-2:6.3.2 step 2. */ | ||
276 | for (t = 0; t < 16; ++t) | ||
277 | W[t] = ntoh64(words[t]); | ||
278 | for (/*t = 16*/; t < 80; ++t) | ||
279 | W[t] = R1(W[t - 2]) + W[t - 7] + R0(W[t - 15]) + W[t - 16]; | ||
280 | |||
281 | /* The actual computation according to FIPS 180-2:6.3.2 step 3. */ | ||
282 | for (t = 0; t < 80; ++t) { | ||
283 | uint64_t T1 = h + S1(e) + Ch(e, f, g) + sha_K[t] + W[t]; | ||
284 | uint64_t T2 = S0(a) + Maj(a, b, c); | ||
285 | h = g; | ||
286 | g = f; | ||
287 | f = e; | ||
288 | e = d + T1; | ||
289 | d = c; | ||
290 | c = b; | ||
291 | b = a; | ||
292 | a = T1 + T2; | ||
293 | } | ||
294 | #undef Ch | ||
295 | #undef Maj | ||
296 | #undef S0 | ||
297 | #undef S1 | ||
298 | #undef R0 | ||
299 | #undef R1 | ||
300 | /* Add the starting values of the context according to FIPS 180-2:6.3.2 | ||
301 | step 4. */ | ||
302 | ctx->hash[0] += a; | ||
303 | ctx->hash[1] += b; | ||
304 | ctx->hash[2] += c; | ||
305 | ctx->hash[3] += d; | ||
306 | ctx->hash[4] += e; | ||
307 | ctx->hash[5] += f; | ||
308 | ctx->hash[6] += g; | ||
309 | ctx->hash[7] += h; | ||
310 | } | ||
311 | |||
312 | |||
313 | void FAST_FUNC sha1_begin(sha1_ctx_t *ctx) | ||
314 | { | ||
315 | ctx->hash[0] = 0x67452301; | ||
316 | ctx->hash[1] = 0xefcdab89; | ||
317 | ctx->hash[2] = 0x98badcfe; | ||
318 | ctx->hash[3] = 0x10325476; | ||
319 | ctx->hash[4] = 0xc3d2e1f0; | ||
320 | ctx->total64 = 0; | ||
321 | ctx->process_block = sha1_process_block64; | ||
322 | } | ||
323 | |||
324 | static const uint32_t init256[] = { | ||
325 | 0x6a09e667, | ||
326 | 0xbb67ae85, | ||
327 | 0x3c6ef372, | ||
328 | 0xa54ff53a, | ||
329 | 0x510e527f, | ||
330 | 0x9b05688c, | ||
331 | 0x1f83d9ab, | ||
332 | 0x5be0cd19, | ||
333 | 0, | ||
334 | 0, | ||
335 | }; | ||
336 | static const uint32_t init512_lo[] = { | ||
337 | 0xf3bcc908, | ||
338 | 0x84caa73b, | ||
339 | 0xfe94f82b, | ||
340 | 0x5f1d36f1, | ||
341 | 0xade682d1, | ||
342 | 0x2b3e6c1f, | ||
343 | 0xfb41bd6b, | ||
344 | 0x137e2179, | ||
345 | 0, | ||
346 | 0, | ||
347 | }; | ||
348 | |||
349 | /* Initialize structure containing state of computation. | ||
350 | (FIPS 180-2:5.3.2) */ | ||
351 | void FAST_FUNC sha256_begin(sha256_ctx_t *ctx) | ||
352 | { | ||
353 | memcpy(ctx->hash, init256, sizeof(init256)); | ||
354 | /*ctx->total64 = 0; - done by extending init256 with two 32-bit zeros */ | ||
355 | ctx->process_block = sha256_process_block64; | ||
356 | } | ||
357 | |||
358 | /* Initialize structure containing state of computation. | ||
359 | (FIPS 180-2:5.3.3) */ | ||
360 | void FAST_FUNC sha512_begin(sha512_ctx_t *ctx) | ||
361 | { | ||
362 | int i; | ||
363 | /* Two extra iterations zero out ctx->total64[] */ | ||
364 | for (i = 0; i < 8+2; i++) | ||
365 | ctx->hash[i] = ((uint64_t)(init256[i]) << 32) + init512_lo[i]; | ||
366 | /*ctx->total64[0] = ctx->total64[1] = 0; - already done */ | ||
367 | } | ||
368 | |||
369 | |||
370 | /* Used also for sha256 */ | ||
371 | void FAST_FUNC sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len) | ||
372 | { | ||
373 | unsigned bufpos = ctx->total64 & 63; | ||
374 | unsigned remaining; | ||
375 | |||
376 | ctx->total64 += len; | ||
377 | #if 0 | ||
378 | remaining = 64 - bufpos; | ||
379 | |||
380 | /* Hash whole blocks */ | ||
381 | while (len >= remaining) { | ||
382 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
383 | buffer = (const char *)buffer + remaining; | ||
384 | len -= remaining; | ||
385 | remaining = 64; | ||
386 | bufpos = 0; | ||
387 | ctx->process_block(ctx); | ||
388 | } | ||
389 | |||
390 | /* Save last, partial blosk */ | ||
391 | memcpy(ctx->wbuffer + bufpos, buffer, len); | ||
392 | #else | ||
393 | /* Tiny bit smaller code */ | ||
394 | while (1) { | ||
395 | remaining = 64 - bufpos; | ||
396 | if (remaining > len) | ||
397 | remaining = len; | ||
398 | /* Copy data into aligned buffer */ | ||
399 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
400 | len -= remaining; | ||
401 | buffer = (const char *)buffer + remaining; | ||
402 | bufpos += remaining; | ||
403 | /* clever way to do "if (bufpos != 64) break; ... ; bufpos = 0;" */ | ||
404 | bufpos -= 64; | ||
405 | if (bufpos != 0) | ||
406 | break; | ||
407 | /* Buffer is filled up, process it */ | ||
408 | ctx->process_block(ctx); | ||
409 | /*bufpos = 0; - already is */ | ||
410 | } | ||
411 | #endif | ||
412 | } | ||
413 | |||
414 | void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) | ||
415 | { | ||
416 | unsigned bufpos = ctx->total64[0] & 127; | ||
417 | unsigned remaining; | ||
418 | |||
419 | /* First increment the byte count. FIPS 180-2 specifies the possible | ||
420 | length of the file up to 2^128 _bits_. | ||
421 | We compute the number of _bytes_ and convert to bits later. */ | ||
422 | ctx->total64[0] += len; | ||
423 | if (ctx->total64[0] < len) | ||
424 | ctx->total64[1]++; | ||
425 | #if 0 | ||
426 | remaining = 128 - bufpos; | ||
427 | |||
428 | /* Hash whole blocks */ | ||
429 | while (len >= remaining) { | ||
430 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
431 | buffer = (const char *)buffer + remaining; | ||
432 | len -= remaining; | ||
433 | remaining = 128; | ||
434 | bufpos = 0; | ||
435 | sha512_process_block128(ctx); | ||
436 | } | ||
437 | |||
438 | /* Save last, partial blosk */ | ||
439 | memcpy(ctx->wbuffer + bufpos, buffer, len); | ||
440 | #else | ||
441 | while (1) { | ||
442 | remaining = 128 - bufpos; | ||
443 | if (remaining > len) | ||
444 | remaining = len; | ||
445 | /* Copy data into aligned buffer */ | ||
446 | memcpy(ctx->wbuffer + bufpos, buffer, remaining); | ||
447 | len -= remaining; | ||
448 | buffer = (const char *)buffer + remaining; | ||
449 | bufpos += remaining; | ||
450 | /* clever way to do "if (bufpos != 128) break; ... ; bufpos = 0;" */ | ||
451 | bufpos -= 128; | ||
452 | if (bufpos != 0) | ||
453 | break; | ||
454 | /* Buffer is filled up, process it */ | ||
455 | sha512_process_block128(ctx); | ||
456 | /*bufpos = 0; - already is */ | ||
457 | } | ||
458 | #endif | ||
459 | } | ||
460 | |||
461 | |||
462 | /* Used also for sha256 */ | ||
463 | void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf) | ||
464 | { | ||
465 | unsigned bufpos = ctx->total64 & 63; | ||
466 | |||
467 | /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ | ||
468 | ctx->wbuffer[bufpos++] = 0x80; | ||
469 | |||
470 | /* This loop iterates either once or twice, no more, no less */ | ||
471 | while (1) { | ||
472 | unsigned remaining = 64 - bufpos; | ||
473 | memset(ctx->wbuffer + bufpos, 0, remaining); | ||
474 | /* Do we have enough space for the length count? */ | ||
475 | if (remaining >= 8) { | ||
476 | /* Store the 64-bit counter of bits in the buffer in BE format */ | ||
477 | uint64_t t = ctx->total64 << 3; | ||
478 | t = hton64(t); | ||
479 | /* wbuffer is suitably aligned for this */ | ||
480 | *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; | ||
481 | } | ||
482 | ctx->process_block(ctx); | ||
483 | if (remaining >= 8) | ||
484 | break; | ||
485 | bufpos = 0; | ||
486 | } | ||
487 | |||
488 | bufpos = (ctx->process_block == sha1_process_block64) ? 5 : 8; | ||
489 | /* This way we do not impose alignment constraints on resbuf: */ | ||
490 | if (BB_LITTLE_ENDIAN) { | ||
491 | unsigned i; | ||
492 | for (i = 0; i < bufpos; ++i) | ||
493 | ctx->hash[i] = htonl(ctx->hash[i]); | ||
494 | } | ||
495 | memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * bufpos); | ||
496 | } | ||
497 | |||
498 | void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf) | ||
499 | { | ||
500 | unsigned bufpos = ctx->total64[0] & 127; | ||
501 | |||
502 | /* Pad the buffer to the next 128-byte boundary with 0x80,0,0,0... */ | ||
503 | ctx->wbuffer[bufpos++] = 0x80; | ||
504 | |||
505 | while (1) { | ||
506 | unsigned remaining = 128 - bufpos; | ||
507 | memset(ctx->wbuffer + bufpos, 0, remaining); | ||
508 | if (remaining >= 16) { | ||
509 | /* Store the 128-bit counter of bits in the buffer in BE format */ | ||
510 | uint64_t t; | ||
511 | t = ctx->total64[0] << 3; | ||
512 | t = hton64(t); | ||
513 | *(uint64_t *) (&ctx->wbuffer[128 - 8]) = t; | ||
514 | t = (ctx->total64[1] << 3) | (ctx->total64[0] >> 61); | ||
515 | t = hton64(t); | ||
516 | *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; | ||
517 | } | ||
518 | sha512_process_block128(ctx); | ||
519 | if (remaining >= 16) | ||
520 | break; | ||
521 | bufpos = 0; | ||
522 | } | ||
523 | |||
524 | if (BB_LITTLE_ENDIAN) { | ||
525 | unsigned i; | ||
526 | for (i = 0; i < ARRAY_SIZE(ctx->hash); ++i) | ||
527 | ctx->hash[i] = hton64(ctx->hash[i]); | ||
528 | } | ||
529 | memcpy(resbuf, ctx->hash, sizeof(ctx->hash)); | ||
530 | } | ||