sha1.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. // /*
  2. // ---------------------------------------------------------------------------
  3. // Copyright (c) 2002, Dr Brian Gladman <brg@gladman.me.uk>, Worcester, UK.
  4. // All rights reserved.
  5. // LICENSE TERMS
  6. // The free distribution and use of this software in both source and binary
  7. // form is allowed (with or without changes) provided that:
  8. // 1. distributions of this source code include the above copyright
  9. // notice, this list of conditions and the following disclaimer;
  10. // 2. distributions in binary form include the above copyright
  11. // notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other associated materials;
  13. // 3. the copyright holder's name is not used to endorse products
  14. // built using this software without specific written permission.
  15. // ALTERNATIVELY, provided that this notice is retained in full, this product
  16. // may be distributed under the terms of the GNU General Public License (GPL),
  17. // in which case the provisions of the GPL apply INSTEAD OF those given above.
  18. // DISCLAIMER
  19. // This software is provided 'as is' with no explicit or implied warranties
  20. // in respect of its properties, including, but not limited to, correctness
  21. // and/or fitness for purpose.
  22. // ---------------------------------------------------------------------------
  23. // Issue Date: 24/01/2003
  24. // This is a byte oriented version of SHA1 that operates on arrays of bytes
  25. // stored in memory.
  26. // */
  27. // #include "com_BmcType.h"
  28. // #include "sha1.h"
  29. // #include "memory.h"
  30. // #include "Platform.h"
  31. // #include "coreTypes.h"
  32. // /*
  33. // To obtain the highest speed on processors with 32-bit words, this code
  34. // needs to determine the order in which bytes are packed into such words.
  35. // The following block of code is an attempt to capture the most obvious
  36. // ways in which various environemnts specify their endian definitions.
  37. // It may well fail, in which case the definitions will need to be set by
  38. // editing at the points marked **** EDIT HERE IF NECESSARY **** below.
  39. // */
  40. // #define SHA_LITTLE_ENDIAN 1
  41. // #define SHA_BIG_ENDIAN 2
  42. // #if IS_PLATFORM_X86() || IS_PLATFORM_ARM() || IS_PLATFORM_SH()
  43. // #define PLATFORM_BYTE_ORDER SHA_LITTLE_ENDIAN
  44. // #else
  45. // #define PLATFORM_BYTE_ORDER SHA_BIG_ENDIAN
  46. // #endif
  47. // #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
  48. // #if (PLATFORM_BYTE_ORDER == SHA_BIG_ENDIAN)
  49. // #define swap_b32(x) (x)
  50. // #elif defined(bswap_32)
  51. // #define swap_b32(x) bswap_32(x)
  52. // #else
  53. // #define swap_b32(x) ((rotl32((x), 8) & 0x00ff00ff) | (rotl32((x), 24) & 0xff00ff00))
  54. // #endif
  55. // #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
  56. // /* reverse byte order in 32-bit words */
  57. // #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
  58. // #define parity(x,y,z) ((x) ^ (y) ^ (z))
  59. // #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  60. // /* A normal version as set out in the FIPS */
  61. // #define rnd(f,k) \
  62. // t = a; a = rotl32(a,5) + f(b,c,d) + e + k + w[i]; \
  63. // e = d; d = c; c = rotl32(b, 30); b = t
  64. // #if (PLATFORM_BYTE_ORDER == SHA_LITTLE_ENDIAN)
  65. // static uint32 mask[4] =
  66. // { 0x00000000, 0x000000ff, 0x0000ffff, 0x00ffffff };
  67. // static uint32 bits[4] =
  68. // { 0x00000080, 0x00008000, 0x00800000, 0x80000000 };
  69. // #else
  70. // static uint32 mask[4] =
  71. // { 0x00000000, 0xff000000, 0xffff0000, 0xffffff00 };
  72. // static uint32 bits[4] =
  73. // { 0x80000000, 0x00800000, 0x00008000, 0x00000080 };
  74. // #endif
  75. // /*----------------------------------------
  76. // * sha1_compile
  77. // *----------------------------------------*/
  78. // void
  79. // sha1_compile ( Sha1_Ctx_T* pctx)
  80. // {
  81. // int i;
  82. // uint32 w [80], a, b, c, d, e, t;
  83. // /* note that words are compiled from the buffer into 32-bit */
  84. // /* words in big-endian order so an order reversal is needed */
  85. // /* here on little endian machines */
  86. // for (i = 0; i < SHA1_BLOCK_SIZE / 4; ++i)
  87. // {
  88. // w [i] = swap_b32 (pctx->wbuf [i]);
  89. // }
  90. // for (i = SHA1_BLOCK_SIZE / 4; i < 80; ++i)
  91. // {
  92. // w[i] = rotl32(w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16], 1);
  93. // }
  94. // a = pctx->hash [0];
  95. // b = pctx->hash [1];
  96. // c = pctx->hash [2];
  97. // d = pctx->hash [3];
  98. // e = pctx->hash [4];
  99. // for (i = 0; i < 20; ++i)
  100. // {
  101. // rnd (ch, 0x5a827999);
  102. // }
  103. // for (i = 20; i < 40; ++i)
  104. // {
  105. // rnd (parity, 0x6ed9eba1);
  106. // }
  107. // for (i = 40; i < 60; ++i)
  108. // {
  109. // rnd (maj, 0x8f1bbcdc);
  110. // }
  111. // for (i = 60; i < 80; ++i)
  112. // {
  113. // rnd (parity, 0xca62c1d6);
  114. // }
  115. // pctx->hash [0] += a;
  116. // pctx->hash [1] += b;
  117. // pctx->hash [2] += c;
  118. // pctx->hash [3] += d;
  119. // pctx->hash [4] += e;
  120. // }
  121. // /*----------------------------------------
  122. // * sha1_begin
  123. // *----------------------------------------*/
  124. // void
  125. // sha1_begin ( Sha1_Ctx_T* pctx)
  126. // {
  127. // pctx->count [0] = pctx->count [1] = 0;
  128. // pctx->hash [0] = 0x67452301;
  129. // pctx->hash [1] = 0xefcdab89;
  130. // pctx->hash [2] = 0x98badcfe;
  131. // pctx->hash [3] = 0x10325476;
  132. // pctx->hash [4] = 0xc3d2e1f0;
  133. // }
  134. // /*----------------------------------------
  135. // * sha1_hash
  136. // *----------------------------------------*/
  137. // void
  138. // sha1_hash ( INT8U* pdata, INT16U len, Sha1_Ctx_T* pctx)
  139. // {
  140. // /* SHA1 hash data in an array of bytes into hash buffer and */
  141. // /* call the hash_compile function as required. */
  142. // uint32 pos = (uint32)(pctx->count[0] & SHA1_MASK),
  143. // space = SHA1_BLOCK_SIZE - pos;
  144. // INT8U *sp = pdata;
  145. // if ((pctx->count[0] += len) < len) { ++(pctx->count[1]); }
  146. // while (len >= space) /* tranfer whole blocks if possible */
  147. // {
  148. // memcpy ((( INT8U*)pctx->wbuf) + pos, sp, space);
  149. // sp += space; len -= (INT16U)space; space = SHA1_BLOCK_SIZE; pos = 0;
  150. // sha1_compile (pctx);
  151. // }
  152. // //dispscont (pctx);
  153. // /*lint -e{803} conceivable data overrun */
  154. // /* there are two cases: the above while loop entered or not */
  155. // /* entered. If not entered, 'space = SHA1_BLOCK_SIZE - pos' */
  156. // /* and 'len < space' so that 'len + pos < SHA1_BLOCK_SIZE'. */
  157. // /* If entered, 'pos = 0', 'space = SHA1_BLOCK_SIZE' and */
  158. // /* 'len < space' so that 'pos + len < SHA1_BLOCK_SIZE'. In */
  159. // /* both cases, therefore, the memory copy is in the buffer */
  160. // memcpy ((( INT8U*)pctx->wbuf) + pos, sp, len);
  161. // }
  162. // /*----------------------------------------
  163. // * sha1_end
  164. // *----------------------------------------*/
  165. // void
  166. // sha1_end ( INT8U* phval, Sha1_Ctx_T* pctx)
  167. // {
  168. // uint32 i = (uint32)(pctx->count [0] & SHA1_MASK);
  169. // /* SHA1 final padding and digest calculation */
  170. // /* mask out the rest of any partial 32-bit word and then set */
  171. // /* the next byte to 0x80. On big-endian machines any bytes in */
  172. // /* the buffer will be at the top end of 32 bit words, on little */
  173. // /* endian machines they will be at the bottom. Hence the AND */
  174. // /* and OR masks above are reversed for little endian systems */
  175. // /* Note that we can always add the first padding byte at this */
  176. // /* point because the buffer always has at least one empty slot */
  177. // pctx->wbuf [(INT16U)(i >> 2)] = (pctx->wbuf [(INT16U)(i >> 2)] & mask [(INT16U)(i & 3)]) | bits [(INT16U)(i & 3)];
  178. // /* we need 9 or more empty positions, one for the padding byte */
  179. // /* (above) and eight for the length count. If there is not */
  180. // /* enough space pad and empty the buffer */
  181. // if (i > SHA1_BLOCK_SIZE - 9)
  182. // {
  183. // if (i < 60) { pctx->wbuf [15] = 0; }
  184. // sha1_compile (pctx);
  185. // i = 0;
  186. // }
  187. // else /* compute a word index for the empty buffer positions */
  188. // {
  189. // i = (i >> 2) + 1;
  190. // }
  191. // /* and zero pad all but last two positions */
  192. // while (i < 14) { pctx->wbuf[(INT16U)(i++)] = 0; }
  193. // /* assemble the eight byte counter in in big-endian format */
  194. // pctx->wbuf [14] = swap_b32 ((pctx->count[1] << 3) | (pctx->count[0] >> 29));
  195. // pctx->wbuf [15] = swap_b32 (pctx->count[0] << 3);
  196. // sha1_compile (pctx);
  197. // /* extract the hash value as bytes in case the hash buffer is */
  198. // /* misaligned for 32-bit words */
  199. // /*lint -e{504} unusual shift operation (unusually formed right argument) */
  200. // for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  201. // {
  202. // phval [(INT16U)(i)] = (INT8U)(pctx->hash [(INT16U)(i >> 2)] >> (8 * (~i & 3)));
  203. // }
  204. // }
  205. // /*----------------------------------------
  206. // * sha1
  207. // *----------------------------------------*/
  208. // void
  209. // sha1 ( INT8U* phval, INT8U* pdata, INT16U len)
  210. // {
  211. // Sha1_Ctx_T cx;
  212. // sha1_begin (&cx);
  213. // sha1_hash (pdata, len, &cx);
  214. // sha1_end (phval, &cx);
  215. // }