sha1.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Based on shasum from http://www.netsw.org/crypto/hash/
  3. * Majorly hacked up to use Dr Brian Gladman's sha1 code
  4. *
  5. * Copyright (C) 2002 Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  6. * Copyright (C) 2003 Glenn L. McGrath
  7. * Copyright (C) 2003 Erik Andersen
  8. *
  9. * LICENSE TERMS
  10. *
  11. * The free distribution and use of this software in both source and binary
  12. * form is allowed (with or without changes) provided that:
  13. *
  14. * 1. distributions of this source code include the above copyright
  15. * notice, this list of conditions and the following disclaimer;
  16. *
  17. * 2. distributions in binary form include the above copyright
  18. * notice, this list of conditions and the following disclaimer
  19. * in the documentation and/or other associated materials;
  20. *
  21. * 3. the copyright holder's name is not used to endorse products
  22. * built using this software without specific written permission.
  23. *
  24. * ALTERNATIVELY, provided that this notice is retained in full, this product
  25. * may be distributed under the terms of the GNU General Public License (GPL),
  26. * in which case the provisions of the GPL apply INSTEAD OF those given above.
  27. *
  28. * DISCLAIMER
  29. *
  30. * This software is provided 'as is' with no explicit or implied warranties
  31. * in respect of its properties, including, but not limited to, correctness
  32. * and/or fitness for purpose.
  33. * ---------------------------------------------------------------------------
  34. * Issue Date: 10/11/2002
  35. *
  36. * This is a byte oriented version of SHA1 that operates on arrays of bytes
  37. * stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
  38. */
  39. #include <fcntl.h>
  40. #include <limits.h>
  41. #include <stdio.h>
  42. #include <stdint.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include "busybox.h"
  47. # define SHA1_BLOCK_SIZE 64
  48. # define SHA1_DIGEST_SIZE 20
  49. # define SHA1_HASH_SIZE SHA1_DIGEST_SIZE
  50. # define SHA2_GOOD 0
  51. # define SHA2_BAD 1
  52. # define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  53. # define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  54. /* reverse byte order in 32-bit words */
  55. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  56. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  57. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) | (y))))
  58. /* A normal version as set out in the FIPS. This version uses */
  59. /* partial loop unrolling and is optimised for the Pentium 4 */
  60. # define rnd(f,k) \
  61. t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
  62. e = d; d = c; c = rotl32(b, 30); b = t
  63. static void sha1_compile(sha1_ctx_t *ctx)
  64. {
  65. uint32_t w[80], i, a, b, c, d, e, t;
  66. /* note that words are compiled from the buffer into 32-bit */
  67. /* words in big-endian order so an order reversal is needed */
  68. /* here on little endian machines */
  69. for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
  70. w[i] = htonl(ctx->wbuf[i]);
  71. for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
  72. w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  73. a = ctx->hash[0];
  74. b = ctx->hash[1];
  75. c = ctx->hash[2];
  76. d = ctx->hash[3];
  77. e = ctx->hash[4];
  78. for (i = 0; i < 20; ++i) {
  79. rnd(ch, 0x5a827999);
  80. }
  81. for (i = 20; i < 40; ++i) {
  82. rnd(parity, 0x6ed9eba1);
  83. }
  84. for (i = 40; i < 60; ++i) {
  85. rnd(maj, 0x8f1bbcdc);
  86. }
  87. for (i = 60; i < 80; ++i) {
  88. rnd(parity, 0xca62c1d6);
  89. }
  90. ctx->hash[0] += a;
  91. ctx->hash[1] += b;
  92. ctx->hash[2] += c;
  93. ctx->hash[3] += d;
  94. ctx->hash[4] += e;
  95. }
  96. void sha1_begin(sha1_ctx_t *ctx)
  97. {
  98. ctx->count[0] = ctx->count[1] = 0;
  99. ctx->hash[0] = 0x67452301;
  100. ctx->hash[1] = 0xefcdab89;
  101. ctx->hash[2] = 0x98badcfe;
  102. ctx->hash[3] = 0x10325476;
  103. ctx->hash[4] = 0xc3d2e1f0;
  104. }
  105. /* SHA1 hash data in an array of bytes into hash buffer and call the */
  106. /* hash_compile function as required. */
  107. void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx)
  108. {
  109. uint32_t pos = (uint32_t) (ctx->count[0] & SHA1_MASK);
  110. uint32_t freeb = SHA1_BLOCK_SIZE - pos;
  111. const unsigned char *sp = data;
  112. if ((ctx->count[0] += length) < length)
  113. ++(ctx->count[1]);
  114. while (length >= freeb) { /* tranfer whole blocks while possible */
  115. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, freeb);
  116. sp += freeb;
  117. length -= freeb;
  118. freeb = SHA1_BLOCK_SIZE;
  119. pos = 0;
  120. sha1_compile(ctx);
  121. }
  122. memcpy(((unsigned char *) ctx->wbuf) + pos, sp, length);
  123. }
  124. void *sha1_end(void *resbuf, sha1_ctx_t *ctx)
  125. {
  126. /* SHA1 Final padding and digest calculation */
  127. #if BB_BIG_ENDIAN
  128. static uint32_t mask[4] = { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
  129. static uint32_t bits[4] = { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
  130. #else
  131. static uint32_t mask[4] = { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
  132. static uint32_t bits[4] = { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
  133. #endif /* __BYTE_ORDER */
  134. uint8_t *hval = resbuf;
  135. uint32_t i, cnt = (uint32_t) (ctx->count[0] & SHA1_MASK);
  136. /* mask out the rest of any partial 32-bit word and then set */
  137. /* the next byte to 0x80. On big-endian machines any bytes in */
  138. /* the buffer will be at the top end of 32 bit words, on little */
  139. /* endian machines they will be at the bottom. Hence the AND */
  140. /* and OR masks above are reversed for little endian systems */
  141. ctx->wbuf[cnt >> 2] =
  142. (ctx->wbuf[cnt >> 2] & mask[cnt & 3]) | bits[cnt & 3];
  143. /* we need 9 or more empty positions, one for the padding byte */
  144. /* (above) and eight for the length count. If there is not */
  145. /* enough space pad and empty the buffer */
  146. if (cnt > SHA1_BLOCK_SIZE - 9) {
  147. if (cnt < 60)
  148. ctx->wbuf[15] = 0;
  149. sha1_compile(ctx);
  150. cnt = 0;
  151. } else /* compute a word index for the empty buffer positions */
  152. cnt = (cnt >> 2) + 1;
  153. while (cnt < 14) /* and zero pad all but last two positions */
  154. ctx->wbuf[cnt++] = 0;
  155. /* assemble the eight byte counter in the buffer in big-endian */
  156. /* format */
  157. ctx->wbuf[14] = htonl((ctx->count[1] << 3) | (ctx->count[0] >> 29));
  158. ctx->wbuf[15] = htonl(ctx->count[0] << 3);
  159. sha1_compile(ctx);
  160. /* extract the hash value as bytes in case the hash buffer is */
  161. /* misaligned for 32-bit words */
  162. for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  163. hval[i] = (unsigned char) (ctx->hash[i >> 2] >> 8 * (~i & 3));
  164. return resbuf;
  165. }