aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2010-10-17 03:21:51 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2010-10-17 03:21:51 +0200
commitf6dacc23ff495cbdd3f1baf5985ded21a7e4a9c9 (patch)
treeb4c56ec45f126d37244e2b125c7c3faeb0a20298
parent36ab585f68295487a0973bde86bcb0ab7577a8ff (diff)
downloadbusybox-w32-f6dacc23ff495cbdd3f1baf5985ded21a7e4a9c9.tar.gz
busybox-w32-f6dacc23ff495cbdd3f1baf5985ded21a7e4a9c9.tar.bz2
busybox-w32-f6dacc23ff495cbdd3f1baf5985ded21a7e4a9c9.zip
bring md5 and sha1 names closer. no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--include/libbb.h4
-rw-r--r--libbb/md5.c76
-rw-r--r--libbb/sha1.c16
3 files changed, 44 insertions, 52 deletions
diff --git a/include/libbb.h b/include/libbb.h
index d05b2d48a..dec4e3af5 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1539,8 +1539,8 @@ typedef struct md5_ctx_t {
1539 uint32_t B; 1539 uint32_t B;
1540 uint32_t C; 1540 uint32_t C;
1541 uint32_t D; 1541 uint32_t D;
1542 uint64_t total; 1542 uint64_t total64;
1543 char buffer[64]; 1543 char wbuffer[64];
1544} md5_ctx_t; 1544} md5_ctx_t;
1545#else 1545#else
1546/* libbb/md5prime.c uses a bit different one: */ 1546/* libbb/md5prime.c uses a bit different one: */
diff --git a/libbb/md5.c b/libbb/md5.c
index a1f0a923f..f5d083e3f 100644
--- a/libbb/md5.c
+++ b/libbb/md5.c
@@ -33,7 +33,7 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
33 ctx->B = 0xefcdab89; 33 ctx->B = 0xefcdab89;
34 ctx->C = 0x98badcfe; 34 ctx->C = 0x98badcfe;
35 ctx->D = 0x10325476; 35 ctx->D = 0x10325476;
36 ctx->total = 0; 36 ctx->total64 = 0;
37} 37}
38 38
39/* These are the four functions used in the four steps of the MD5 algorithm 39/* These are the four functions used in the four steps of the MD5 algorithm
@@ -48,8 +48,8 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
48 48
49#define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s)))) 49#define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s))))
50 50
51/* Hash a single block, 64 bytes long and 4-byte aligned. */ 51/* Hash a single block, 64 bytes long and 4-byte aligned */
52static void md5_hash_block(md5_ctx_t *ctx) 52static void md5_process_block64(md5_ctx_t *ctx)
53{ 53{
54#if MD5_SIZE_VS_SPEED > 0 54#if MD5_SIZE_VS_SPEED > 0
55 /* Before we start, one word to the strange constants. 55 /* Before we start, one word to the strange constants.
@@ -95,7 +95,7 @@ static void md5_hash_block(md5_ctx_t *ctx)
95 }; 95 };
96# endif 96# endif
97#endif 97#endif
98 const uint32_t *words = (const void*) ctx->buffer; 98 const uint32_t *words = (const void*) ctx->wbuffer;
99 99
100 uint32_t A = ctx->A; 100 uint32_t A = ctx->A;
101 uint32_t B = ctx->B; 101 uint32_t B = ctx->B;
@@ -354,29 +354,41 @@ static void md5_hash_block(md5_ctx_t *ctx)
354 ctx->D = D; 354 ctx->D = D;
355} 355}
356 356
357/* The first unused position in ctx->buffer: */
358#define BUFPOS(ctx) (((unsigned)ctx->total) & 63)
359
360/* Feed data through a temporary buffer to call md5_hash_aligned_block() 357/* Feed data through a temporary buffer to call md5_hash_aligned_block()
361 * with chunks of data that are 4-byte aligned and a multiple of 64 bytes. 358 * with chunks of data that are 4-byte aligned and a multiple of 64 bytes.
362 * This function's internal buffer remembers previous data until it has 64 359 * This function's internal buffer remembers previous data until it has 64
363 * bytes worth to pass on. Call md5_end() to flush this buffer. */ 360 * bytes worth to pass on. Call md5_end() to flush this buffer. */
364void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len) 361void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
365{ 362{
366#if 1 363 unsigned bufpos = ctx->total64 & 63;
367 /* Tiny bit smaller code */ 364 unsigned remaining;
368 unsigned bufpos = BUFPOS(ctx);
369 365
370 /* RFC 1321 specifies the possible length of the file up to 2^64 bits. 366 /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
371 * Here we only track the number of bytes. */ 367 * Here we only track the number of bytes. */
372 ctx->total += len; 368 ctx->total64 += len;
369#if 0
370 remaining = 64 - bufpos;
373 371
372 /* Hash whole blocks */
373 while (len >= remaining) {
374 memcpy(ctx->wbuffer + bufpos, buffer, remaining);
375 buffer = (const char *)buffer + remaining;
376 len -= remaining;
377 remaining = 64;
378 bufpos = 0;
379 md5_process_block64(ctx);
380 }
381
382 /* Save last, partial blosk */
383 memcpy(ctx->wbuffer + bufpos, buffer, len);
384#else
385 /* Tiny bit smaller code */
374 while (1) { 386 while (1) {
375 unsigned remaining = 64 - bufpos; 387 remaining = 64 - bufpos;
376 if (remaining > len) 388 if (remaining > len)
377 remaining = len; 389 remaining = len;
378 /* Copy data into aligned buffer. */ 390 /* Copy data into aligned buffer */
379 memcpy(ctx->buffer + bufpos, buffer, remaining); 391 memcpy(ctx->wbuffer + bufpos, buffer, remaining);
380 len -= remaining; 392 len -= remaining;
381 buffer = (const char *)buffer + remaining; 393 buffer = (const char *)buffer + remaining;
382 bufpos += remaining; 394 bufpos += remaining;
@@ -384,30 +396,10 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
384 bufpos -= 64; 396 bufpos -= 64;
385 if (bufpos != 0) 397 if (bufpos != 0)
386 break; 398 break;
387 /* Buffer is filled up, process it. */ 399 /* Buffer is filled up, process it */
388 md5_hash_block(ctx); 400 md5_process_block64(ctx);
389 /*bufpos = 0; - already is */ 401 /*bufpos = 0; - already is */
390 } 402 }
391#else
392 unsigned bufpos = BUFPOS(ctx);
393 unsigned add = 64 - bufpos;
394
395 /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
396 * Here we only track the number of bytes. */
397 ctx->total += len;
398
399 /* Hash whole blocks */
400 while (len >= add) {
401 memcpy(ctx->buffer + bufpos, buffer, add);
402 buffer = (const char *)buffer + add;
403 len -= add;
404 add = 64;
405 bufpos = 0;
406 md5_hash_block(ctx);
407 }
408
409 /* Save last, partial blosk */
410 memcpy(ctx->buffer + bufpos, buffer, len);
411#endif 403#endif
412} 404}
413 405
@@ -418,25 +410,25 @@ void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
418 */ 410 */
419void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf) 411void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf)
420{ 412{
421 unsigned bufpos = BUFPOS(ctx); 413 unsigned bufpos = ctx->total64 & 63;
422 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */ 414 /* Pad the buffer to the next 64-byte boundary with 0x80,0,0,0... */
423 ctx->buffer[bufpos++] = 0x80; 415 ctx->wbuffer[bufpos++] = 0x80;
424 416
425 /* This loop iterates either once or twice, no more, no less */ 417 /* This loop iterates either once or twice, no more, no less */
426 while (1) { 418 while (1) {
427 unsigned remaining = 64 - bufpos; 419 unsigned remaining = 64 - bufpos;
428 memset(ctx->buffer + bufpos, 0, remaining); 420 memset(ctx->wbuffer + bufpos, 0, remaining);
429 /* Do we have enough space for the length count? */ 421 /* Do we have enough space for the length count? */
430 if (remaining >= 8) { 422 if (remaining >= 8) {
431 /* Store the 64-bit counter of bits in the buffer in BE format */ 423 /* Store the 64-bit counter of bits in the buffer in BE format */
432 uint64_t t = ctx->total << 3; 424 uint64_t t = ctx->total64 << 3;
433 unsigned i; 425 unsigned i;
434 for (i = 0; i < 8; i++) { 426 for (i = 0; i < 8; i++) {
435 ctx->buffer[56 + i] = t; 427 ctx->wbuffer[56 + i] = t;
436 t >>= 8; 428 t >>= 8;
437 } 429 }
438 } 430 }
439 md5_hash_block(ctx); 431 md5_process_block64(ctx);
440 if (remaining >= 8) 432 if (remaining >= 8)
441 break; 433 break;
442 bufpos = 0; 434 bufpos = 0;
diff --git a/libbb/sha1.c b/libbb/sha1.c
index 3e61aff6d..d79291148 100644
--- a/libbb/sha1.c
+++ b/libbb/sha1.c
@@ -469,10 +469,10 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
469 469
470 /* This loop iterates either once or twice, no more, no less */ 470 /* This loop iterates either once or twice, no more, no less */
471 while (1) { 471 while (1) {
472 unsigned pad = 64 - bufpos; 472 unsigned remaining = 64 - bufpos;
473 memset(ctx->wbuffer + bufpos, 0, pad); 473 memset(ctx->wbuffer + bufpos, 0, remaining);
474 /* Do we have enough space for the length count? */ 474 /* Do we have enough space for the length count? */
475 if (pad >= 8) { 475 if (remaining >= 8) {
476 /* Store the 64-bit counter of bits in the buffer in BE format */ 476 /* Store the 64-bit counter of bits in the buffer in BE format */
477 uint64_t t = ctx->total64 << 3; 477 uint64_t t = ctx->total64 << 3;
478 t = hton64(t); 478 t = hton64(t);
@@ -480,7 +480,7 @@ void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
480 *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t; 480 *(uint64_t *) (&ctx->wbuffer[64 - 8]) = t;
481 } 481 }
482 ctx->process_block(ctx); 482 ctx->process_block(ctx);
483 if (pad >= 8) 483 if (remaining >= 8)
484 break; 484 break;
485 bufpos = 0; 485 bufpos = 0;
486 } 486 }
@@ -503,9 +503,9 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
503 ctx->wbuffer[bufpos++] = 0x80; 503 ctx->wbuffer[bufpos++] = 0x80;
504 504
505 while (1) { 505 while (1) {
506 unsigned pad = 128 - bufpos; 506 unsigned remaining = 128 - bufpos;
507 memset(ctx->wbuffer + bufpos, 0, pad); 507 memset(ctx->wbuffer + bufpos, 0, remaining);
508 if (pad >= 16) { 508 if (remaining >= 16) {
509 /* Store the 128-bit counter of bits in the buffer in BE format */ 509 /* Store the 128-bit counter of bits in the buffer in BE format */
510 uint64_t t; 510 uint64_t t;
511 t = ctx->total64[0] << 3; 511 t = ctx->total64[0] << 3;
@@ -516,7 +516,7 @@ void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
516 *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t; 516 *(uint64_t *) (&ctx->wbuffer[128 - 16]) = t;
517 } 517 }
518 sha512_process_block128(ctx); 518 sha512_process_block128(ctx);
519 if (pad >= 16) 519 if (remaining >= 16)
520 break; 520 break;
521 bufpos = 0; 521 bufpos = 0;
522 } 522 }