sha1.c 8.8 KB

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