sha1.c 5.1 KB

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