You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sha1.c 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright(C) 2006 Cameron Rich
  3. *
  4. * This library is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation; either version 2.1 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /**
  19. * SHA1 implementation - as defined in FIPS PUB 180-1 published April 17, 1995.
  20. * This code was originally taken from RFC3174
  21. */
  22. #include <string.h>
  23. #include "crypto.h"
  24. /*
  25. * Define the SHA1 circular left shift macro
  26. */
  27. #define SHA1CircularShift(bits,word) \
  28. (((word) << (bits)) | ((word) >> (32-(bits))))
  29. /* ----- static functions ----- */
  30. static void SHA1PadMessage(SHA1_CTX *ctx);
  31. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx);
  32. /**
  33. * Initialize the SHA1 context
  34. */
  35. void SHA1Init(SHA1_CTX *ctx)
  36. {
  37. ctx->Length_Low = 0;
  38. ctx->Length_High = 0;
  39. ctx->Message_Block_Index = 0;
  40. ctx->Intermediate_Hash[0] = 0x67452301;
  41. ctx->Intermediate_Hash[1] = 0xEFCDAB89;
  42. ctx->Intermediate_Hash[2] = 0x98BADCFE;
  43. ctx->Intermediate_Hash[3] = 0x10325476;
  44. ctx->Intermediate_Hash[4] = 0xC3D2E1F0;
  45. }
  46. /**
  47. * Accepts an array of octets as the next portion of the message.
  48. */
  49. void SHA1Update(SHA1_CTX *ctx, const uint8_t *msg, int len)
  50. {
  51. while (len--)
  52. {
  53. ctx->Message_Block[ctx->Message_Block_Index++] = (*msg & 0xFF);
  54. ctx->Length_Low += 8;
  55. if (ctx->Length_Low == 0)
  56. {
  57. ctx->Length_High++;
  58. }
  59. if (ctx->Message_Block_Index == 64)
  60. {
  61. SHA1ProcessMessageBlock(ctx);
  62. }
  63. msg++;
  64. }
  65. }
  66. /**
  67. * Return the 160-bit message digest into the user's array
  68. */
  69. void SHA1Final(SHA1_CTX *ctx, uint8_t *digest)
  70. {
  71. int i;
  72. SHA1PadMessage(ctx);
  73. memset(ctx->Message_Block, 0, 64);
  74. ctx->Length_Low = 0; /* and clear length */
  75. ctx->Length_High = 0;
  76. for (i = 0; i < SHA1_SIZE; i++)
  77. {
  78. digest[i] = ctx->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) );
  79. }
  80. }
  81. /**
  82. * Process the next 512 bits of the message stored in the array.
  83. */
  84. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx)
  85. {
  86. const uint32_t K[] = { /* Constants defined in SHA-1 */
  87. 0x5A827999,
  88. 0x6ED9EBA1,
  89. 0x8F1BBCDC,
  90. 0xCA62C1D6
  91. };
  92. int t; /* Loop counter */
  93. uint32_t temp; /* Temporary word value */
  94. uint32_t W[80]; /* Word sequence */
  95. uint32_t A, B, C, D, E; /* Word buffers */
  96. /*
  97. * Initialize the first 16 words in the array W
  98. */
  99. for (t = 0; t < 16; t++)
  100. {
  101. W[t] = ctx->Message_Block[t * 4] << 24;
  102. W[t] |= ctx->Message_Block[t * 4 + 1] << 16;
  103. W[t] |= ctx->Message_Block[t * 4 + 2] << 8;
  104. W[t] |= ctx->Message_Block[t * 4 + 3];
  105. }
  106. for (t = 16; t < 80; t++)
  107. {
  108. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  109. }
  110. A = ctx->Intermediate_Hash[0];
  111. B = ctx->Intermediate_Hash[1];
  112. C = ctx->Intermediate_Hash[2];
  113. D = ctx->Intermediate_Hash[3];
  114. E = ctx->Intermediate_Hash[4];
  115. for (t = 0; t < 20; t++)
  116. {
  117. temp = SHA1CircularShift(5,A) +
  118. ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  119. E = D;
  120. D = C;
  121. C = SHA1CircularShift(30,B);
  122. B = A;
  123. A = temp;
  124. }
  125. for (t = 20; t < 40; t++)
  126. {
  127. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  128. E = D;
  129. D = C;
  130. C = SHA1CircularShift(30,B);
  131. B = A;
  132. A = temp;
  133. }
  134. for (t = 40; t < 60; t++)
  135. {
  136. temp = SHA1CircularShift(5,A) +
  137. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  138. E = D;
  139. D = C;
  140. C = SHA1CircularShift(30,B);
  141. B = A;
  142. A = temp;
  143. }
  144. for (t = 60; t < 80; t++)
  145. {
  146. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  147. E = D;
  148. D = C;
  149. C = SHA1CircularShift(30,B);
  150. B = A;
  151. A = temp;
  152. }
  153. ctx->Intermediate_Hash[0] += A;
  154. ctx->Intermediate_Hash[1] += B;
  155. ctx->Intermediate_Hash[2] += C;
  156. ctx->Intermediate_Hash[3] += D;
  157. ctx->Intermediate_Hash[4] += E;
  158. ctx->Message_Block_Index = 0;
  159. }
  160. /*
  161. * According to the standard, the message must be padded to an even
  162. * 512 bits. The first padding bit must be a '1'. The last 64
  163. * bits represent the length of the original message. All bits in
  164. * between should be 0. This function will pad the message
  165. * according to those rules by filling the Message_Block array
  166. * accordingly. It will also call the ProcessMessageBlock function
  167. * provided appropriately. When it returns, it can be assumed that
  168. * the message digest has been computed.
  169. *
  170. * @param ctx [in, out] The SHA1 context
  171. */
  172. static void SHA1PadMessage(SHA1_CTX *ctx)
  173. {
  174. /*
  175. * Check to see if the current message block is too small to hold
  176. * the initial padding bits and length. If so, we will pad the
  177. * block, process it, and then continue padding into a second
  178. * block.
  179. */
  180. if (ctx->Message_Block_Index > 55)
  181. {
  182. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  183. while(ctx->Message_Block_Index < 64)
  184. {
  185. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  186. }
  187. SHA1ProcessMessageBlock(ctx);
  188. while (ctx->Message_Block_Index < 56)
  189. {
  190. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  191. }
  192. }
  193. else
  194. {
  195. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  196. while(ctx->Message_Block_Index < 56)
  197. {
  198. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  199. }
  200. }
  201. /*
  202. * Store the message length as the last 8 octets
  203. */
  204. ctx->Message_Block[56] = ctx->Length_High >> 24;
  205. ctx->Message_Block[57] = ctx->Length_High >> 16;
  206. ctx->Message_Block[58] = ctx->Length_High >> 8;
  207. ctx->Message_Block[59] = ctx->Length_High;
  208. ctx->Message_Block[60] = ctx->Length_Low >> 24;
  209. ctx->Message_Block[61] = ctx->Length_Low >> 16;
  210. ctx->Message_Block[62] = ctx->Length_Low >> 8;
  211. ctx->Message_Block[63] = ctx->Length_Low;
  212. SHA1ProcessMessageBlock(ctx);
  213. }