sha1.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
  4. LICENSE TERMS
  5. The free distribution and use of this software in both source and binary
  6. form is allowed (with or without changes) provided that:
  7. 1. distributions of this source code include the above copyright
  8. notice, this list of conditions and the following disclaimer;
  9. 2. distributions in binary form include the above copyright
  10. notice, this list of conditions and the following disclaimer
  11. in the documentation and/or other associated materials;
  12. 3. the copyright holder's name is not used to endorse products
  13. built using this software without specific written permission.
  14. ALTERNATIVELY, provided that this notice is retained in full, this product
  15. may be distributed under the terms of the GNU General Public License (GPL),
  16. in which case the provisions of the GPL apply INSTEAD OF those given above.
  17. DISCLAIMER
  18. This software is provided 'as is' with no explicit or implied warranties
  19. in respect of its properties, including, but not limited to, correctness
  20. and/or fitness for purpose.
  21. ---------------------------------------------------------------------------
  22. Issue Date: 01/08/2005
  23. This is a byte oriented version of SHA1 that operates on arrays of bytes
  24. stored in memory.
  25. */
  26. #include <string.h> /* for memcpy() etc. */
  27. #include "sha1.h"
  28. #include "brg_endian.h"
  29. #if defined(__cplusplus)
  30. extern "C"
  31. {
  32. #endif
  33. #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
  34. #pragma intrinsic(memcpy)
  35. #endif
  36. #if 0 && defined(_MSC_VER)
  37. #define rotl32 _lrotl
  38. #define rotr32 _lrotr
  39. #else
  40. #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  41. #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
  42. #endif
  43. #if !defined(bswap_32)
  44. #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
  45. #endif
  46. #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
  47. #define SWAP_BYTES
  48. #else
  49. #undef SWAP_BYTES
  50. #endif
  51. #if defined(SWAP_BYTES)
  52. #define bsw_32(p,n) \
  53. { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); }
  54. #else
  55. #define bsw_32(p,n)
  56. #endif
  57. #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  58. #if 0
  59. #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  60. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  61. #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  62. #else /* Discovered by Rich Schroeppel and Colin Plumb */
  63. #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
  64. #define parity(x,y,z) ((x) ^ (y) ^ (z))
  65. #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
  66. #endif
  67. /* Compile 64 bytes of hash data into SHA1 context. Note */
  68. /* that this routine assumes that the byte order in the */
  69. /* ctx->wbuf[] at this point is in such an order that low */
  70. /* address bytes in the ORIGINAL byte stream will go in */
  71. /* this buffer to the high end of 32-bit words on BOTH big */
  72. /* and little endian systems */
  73. #ifdef ARRAY
  74. #define q(v,n) v[n]
  75. #else
  76. #define q(v,n) v##n
  77. #endif
  78. #define one_cycle(v,a,b,c,d,e,f,k,h) \
  79. q(v,e) += rotr32(q(v,a),27) + \
  80. f(q(v,b),q(v,c),q(v,d)) + k + h; \
  81. q(v,b) = rotr32(q(v,b), 2)
  82. #define five_cycle(v,f,k,i) \
  83. one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
  84. one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
  85. one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
  86. one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
  87. one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
  88. VOID_RETURN sha1_compile(sha1_ctx ctx[1])
  89. { uint_32t *w = ctx->wbuf;
  90. #ifdef ARRAY
  91. uint_32t v[5];
  92. memcpy(v, ctx->hash, 5 * sizeof(uint_32t));
  93. #else
  94. uint_32t v0, v1, v2, v3, v4;
  95. v0 = ctx->hash[0]; v1 = ctx->hash[1];
  96. v2 = ctx->hash[2]; v3 = ctx->hash[3];
  97. v4 = ctx->hash[4];
  98. #endif
  99. #define hf(i) w[i]
  100. five_cycle(v, ch, 0x5a827999, 0);
  101. five_cycle(v, ch, 0x5a827999, 5);
  102. five_cycle(v, ch, 0x5a827999, 10);
  103. one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
  104. #undef hf
  105. #define hf(i) (w[(i) & 15] = rotl32( \
  106. w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
  107. ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
  108. one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
  109. one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
  110. one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
  111. one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
  112. five_cycle(v, parity, 0x6ed9eba1, 20);
  113. five_cycle(v, parity, 0x6ed9eba1, 25);
  114. five_cycle(v, parity, 0x6ed9eba1, 30);
  115. five_cycle(v, parity, 0x6ed9eba1, 35);
  116. five_cycle(v, maj, 0x8f1bbcdc, 40);
  117. five_cycle(v, maj, 0x8f1bbcdc, 45);
  118. five_cycle(v, maj, 0x8f1bbcdc, 50);
  119. five_cycle(v, maj, 0x8f1bbcdc, 55);
  120. five_cycle(v, parity, 0xca62c1d6, 60);
  121. five_cycle(v, parity, 0xca62c1d6, 65);
  122. five_cycle(v, parity, 0xca62c1d6, 70);
  123. five_cycle(v, parity, 0xca62c1d6, 75);
  124. #ifdef ARRAY
  125. ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
  126. ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
  127. ctx->hash[4] += v[4];
  128. #else
  129. ctx->hash[0] += v0; ctx->hash[1] += v1;
  130. ctx->hash[2] += v2; ctx->hash[3] += v3;
  131. ctx->hash[4] += v4;
  132. #endif
  133. }
  134. VOID_RETURN sha1_begin(sha1_ctx ctx[1])
  135. {
  136. ctx->count[0] = ctx->count[1] = 0;
  137. ctx->hash[0] = 0x67452301;
  138. ctx->hash[1] = 0xefcdab89;
  139. ctx->hash[2] = 0x98badcfe;
  140. ctx->hash[3] = 0x10325476;
  141. ctx->hash[4] = 0xc3d2e1f0;
  142. }
  143. /* SHA1 hash data in an array of bytes into hash buffer and */
  144. /* call the hash_compile function as required. */
  145. VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
  146. { uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK),
  147. space = SHA1_BLOCK_SIZE - pos;
  148. const unsigned char *sp = data;
  149. if((ctx->count[0] += len) < len)
  150. ++(ctx->count[1]);
  151. while(len >= space) /* tranfer whole blocks if possible */
  152. {
  153. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
  154. sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
  155. bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
  156. sha1_compile(ctx);
  157. }
  158. memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
  159. }
  160. /* SHA1 final padding and digest calculation */
  161. VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1])
  162. { uint_32t i = (uint_32t)(ctx->count[0] & SHA1_MASK);
  163. /* put bytes in the buffer in an order in which references to */
  164. /* 32-bit words will put bytes with lower addresses into the */
  165. /* top of 32 bit words on BOTH big and little endian machines */
  166. bsw_32(ctx->wbuf, (i + 3) >> 2);
  167. /* we now need to mask valid bytes and add the padding which is */
  168. /* a single 1 bit and as many zero bits as necessary. Note that */
  169. /* we can always add the first padding byte here because the */
  170. /* buffer always has at least one empty slot */
  171. ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
  172. ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
  173. /* we need 9 or more empty positions, one for the padding byte */
  174. /* (above) and eight for the length count. If there is not */
  175. /* enough space, pad and empty the buffer */
  176. if(i > SHA1_BLOCK_SIZE - 9)
  177. {
  178. if(i < 60) ctx->wbuf[15] = 0;
  179. sha1_compile(ctx);
  180. i = 0;
  181. }
  182. else /* compute a word index for the empty buffer positions */
  183. i = (i >> 2) + 1;
  184. while(i < 14) /* and zero pad all but last two positions */
  185. ctx->wbuf[i++] = 0;
  186. /* the following 32-bit length fields are assembled in the */
  187. /* wrong byte order on little endian machines but this is */
  188. /* corrected later since they are only ever used as 32-bit */
  189. /* word values. */
  190. ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
  191. ctx->wbuf[15] = ctx->count[0] << 3;
  192. sha1_compile(ctx);
  193. /* extract the hash value as bytes in case the hash buffer is */
  194. /* misaligned for 32-bit words */
  195. for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
  196. hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
  197. }
  198. VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
  199. { sha1_ctx cx[1];
  200. sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
  201. }
  202. #if defined(__cplusplus)
  203. }
  204. #endif