sha1.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 tarball for details.
  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. #include <fcntl.h>
  19. #include <limits.h>
  20. #include <stdio.h>
  21. #include <stdint.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include "libbb.h"
  26. #define SHA1_BLOCK_SIZE 64
  27. #define SHA1_DIGEST_SIZE 20
  28. #define SHA1_HASH_SIZE SHA1_DIGEST_SIZE
  29. #define SHA2_GOOD 0
  30. #define SHA2_BAD 1
  31. #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  32. #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  33. /* reverse byte order in 32-bit words */
  34. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  35. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  36. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  37. /* A normal version as set out in the FIPS. This version uses */
  38. /* partial loop unrolling and is optimised for the Pentium 4 */
  39. #define rnd(f,k) \
  40. do { \
  41. t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
  42. e = d; d = c; c = rotl32(b, 30); b = t; \
  43. } while (0)
  44. static void sha1_compile(sha1_ctx_t *ctx)
  45. {
  46. uint32_t w[80], i, a, b, c, d, e, t;
  47. /* note that words are compiled from the buffer into 32-bit */
  48. /* words in big-endian order so an order reversal is needed */
  49. /* here on little endian machines */
  50. for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
  51. w[i] = htonl(ctx->wbuf[i]);
  52. for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
  53. w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  54. a = ctx->hash[0];
  55. b = ctx->hash[1];
  56. c = ctx->hash[2];
  57. d = ctx->hash[3];
  58. e = ctx->hash[4];
  59. for (i = 0; i < 20; ++i) {
  60. rnd(ch, 0x5a827999);
  61. }
  62. for (i = 20; i < 40; ++i) {
  63. rnd(parity, 0x6ed9eba1);
  64. }
  65. for (i = 40; i < 60; ++i) {
  66. rnd(maj, 0x8f1bbcdc);
  67. }
  68. for (i = 60; i < 80; ++i) {
  69. rnd(parity, 0xca62c1d6);
  70. }
  71. ctx->hash[0] += a;
  72. ctx->hash[1] += b;
  73. ctx->hash[2] += c;
  74. ctx->hash[3] += d;
  75. ctx->hash[4] += e;
  76. }
  77. void sha1_begin(sha1_ctx_t *ctx)
  78. {
  79. ctx->count[0] = ctx->count[1] = 0;
  80. ctx->hash[0] = 0x67452301;
  81. ctx->hash[1] = 0xefcdab89;
  82. ctx->hash[2] = 0x98badcfe;
  83. ctx->hash[3] = 0x10325476;
  84. ctx->hash[4] = 0xc3d2e1f0;
  85. }
  86. /* SHA1 hash data in an array of bytes into hash buffer and call the */
  87. /* hash_compile function as required. */
  88. void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx)
  89. {
  90. uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
  91. uint32_t freeb = SHA1_BLOCK_SIZE - pos;
  92. const unsigned char *sp = data;
  93. if ((ctx->count[0] += length) < length)
  94. ++(ctx->count[1]);
  95. while (length >= freeb) { /* tranfer whole blocks while possible */
  96. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
  97. sp += freeb;
  98. length -= freeb;
  99. freeb = SHA1_BLOCK_SIZE;
  100. pos = 0;
  101. sha1_compile(ctx);
  102. }
  103. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length);
  104. }
  105. void *sha1_end(void *resbuf, sha1_ctx_t *ctx)
  106. {
  107. /* SHA1 Final padding and digest calculation */
  108. #if BB_BIG_ENDIAN
  109. static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
  110. static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
  111. #else
  112. static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
  113. static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
  114. #endif
  115. uint8_t *hval = resbuf;
  116. uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
  117. /* mask out the rest of any partial 32-bit word and then set */
  118. /* the next byte to 0x80. On big-endian machines any bytes in */
  119. /* the buffer will be at the top end of 32 bit words, on little */
  120. /* endian machines they will be at the bottom. Hence the AND */
  121. /* and OR masks above are reversed for little endian systems */
  122. ctx->wbuf[cnt >> 2] =
  123. (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
  124. /* we need 9 or more empty positions, one for the padding byte */
  125. /* (above) and eight for the length count. If there is not */
  126. /* enough space pad and empty the buffer */
  127. if (cnt > SHA1_BLOCK_SIZE - 9) {
  128. if (cnt < 60)
  129. ctx->wbuf[15] = 0;
  130. sha1_compile(ctx);
  131. cnt = 0;
  132. } else /* compute a word index for the empty buffer positions */
  133. cnt = (cnt >> 2) + 1;
  134. while (cnt < 14) /* and zero pad all but last two positions */
  135. ctx->wbuf[cnt++] = 0;
  136. /* assemble the eight byte counter in the buffer in big-endian */
  137. /* format */
  138. ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29));
  139. ctx->wbuf[15] = htonl(ctx->count[0] << 3);
  140. sha1_compile(ctx);
  141. /* extract the hash value as bytes in case the hash buffer is */
  142. /* misaligned for 32-bit words */
  143. for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  144. hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
  145. return resbuf;
  146. }