sha1.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * SHA1 implementation - as defined in FIPS PUB 180-1 published April 17, 1995.
  32. * This code was originally taken from RFC3174
  33. */
  34. #include <string.h>
  35. #include "crypto.h"
  36. /*
  37. * Define the SHA1 circular left shift macro
  38. */
  39. #define SHA1CircularShift(bits,word) \
  40. (((word) << (bits)) | ((word) >> (32-(bits))))
  41. /* ----- static functions ----- */
  42. static void SHA1PadMessage(SHA1_CTX *ctx);
  43. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx);
  44. /**
  45. * Initialize the SHA1 context
  46. */
  47. void SHA1_Init(SHA1_CTX *ctx)
  48. {
  49. ctx->Length_Low = 0;
  50. ctx->Length_High = 0;
  51. ctx->Message_Block_Index = 0;
  52. ctx->Intermediate_Hash[0] = 0x67452301;
  53. ctx->Intermediate_Hash[1] = 0xEFCDAB89;
  54. ctx->Intermediate_Hash[2] = 0x98BADCFE;
  55. ctx->Intermediate_Hash[3] = 0x10325476;
  56. ctx->Intermediate_Hash[4] = 0xC3D2E1F0;
  57. }
  58. /**
  59. * Accepts an array of octets as the next portion of the message.
  60. */
  61. void SHA1_Update(SHA1_CTX *ctx, const uint8_t *msg, int len)
  62. {
  63. while (len--)
  64. {
  65. ctx->Message_Block[ctx->Message_Block_Index++] = (*msg & 0xFF);
  66. ctx->Length_Low += 8;
  67. if (ctx->Length_Low == 0)
  68. ctx->Length_High++;
  69. if (ctx->Message_Block_Index == 64)
  70. SHA1ProcessMessageBlock(ctx);
  71. msg++;
  72. }
  73. }
  74. /**
  75. * Return the 160-bit message digest into the user's array
  76. */
  77. void SHA1_Final(uint8_t *digest, SHA1_CTX *ctx)
  78. {
  79. int i;
  80. SHA1PadMessage(ctx);
  81. memset(ctx->Message_Block, 0, 64);
  82. ctx->Length_Low = 0; /* and clear length */
  83. ctx->Length_High = 0;
  84. for (i = 0; i < SHA1_SIZE; i++)
  85. {
  86. digest[i] = ctx->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) );
  87. }
  88. }
  89. /**
  90. * Process the next 512 bits of the message stored in the array.
  91. */
  92. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx)
  93. {
  94. const uint32_t K[] = { /* Constants defined in SHA-1 */
  95. 0x5A827999,
  96. 0x6ED9EBA1,
  97. 0x8F1BBCDC,
  98. 0xCA62C1D6
  99. };
  100. int t; /* Loop counter */
  101. uint32_t temp; /* Temporary word value */
  102. uint32_t W[80]; /* Word sequence */
  103. uint32_t A, B, C, D, E; /* Word buffers */
  104. /*
  105. * Initialize the first 16 words in the array W
  106. */
  107. for (t = 0; t < 16; t++)
  108. {
  109. W[t] = ctx->Message_Block[t * 4] << 24;
  110. W[t] |= ctx->Message_Block[t * 4 + 1] << 16;
  111. W[t] |= ctx->Message_Block[t * 4 + 2] << 8;
  112. W[t] |= ctx->Message_Block[t * 4 + 3];
  113. }
  114. for (t = 16; t < 80; t++)
  115. {
  116. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  117. }
  118. A = ctx->Intermediate_Hash[0];
  119. B = ctx->Intermediate_Hash[1];
  120. C = ctx->Intermediate_Hash[2];
  121. D = ctx->Intermediate_Hash[3];
  122. E = ctx->Intermediate_Hash[4];
  123. for (t = 0; t < 20; t++)
  124. {
  125. temp = SHA1CircularShift(5,A) +
  126. ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  127. E = D;
  128. D = C;
  129. C = SHA1CircularShift(30,B);
  130. B = A;
  131. A = temp;
  132. }
  133. for (t = 20; t < 40; t++)
  134. {
  135. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  136. E = D;
  137. D = C;
  138. C = SHA1CircularShift(30,B);
  139. B = A;
  140. A = temp;
  141. }
  142. for (t = 40; t < 60; t++)
  143. {
  144. temp = SHA1CircularShift(5,A) +
  145. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  146. E = D;
  147. D = C;
  148. C = SHA1CircularShift(30,B);
  149. B = A;
  150. A = temp;
  151. }
  152. for (t = 60; t < 80; t++)
  153. {
  154. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  155. E = D;
  156. D = C;
  157. C = SHA1CircularShift(30,B);
  158. B = A;
  159. A = temp;
  160. }
  161. ctx->Intermediate_Hash[0] += A;
  162. ctx->Intermediate_Hash[1] += B;
  163. ctx->Intermediate_Hash[2] += C;
  164. ctx->Intermediate_Hash[3] += D;
  165. ctx->Intermediate_Hash[4] += E;
  166. ctx->Message_Block_Index = 0;
  167. }
  168. /*
  169. * According to the standard, the message must be padded to an even
  170. * 512 bits. The first padding bit must be a '1'. The last 64
  171. * bits represent the length of the original message. All bits in
  172. * between should be 0. This function will pad the message
  173. * according to those rules by filling the Message_Block array
  174. * accordingly. It will also call the ProcessMessageBlock function
  175. * provided appropriately. When it returns, it can be assumed that
  176. * the message digest has been computed.
  177. *
  178. * @param ctx [in, out] The SHA1 context
  179. */
  180. static void SHA1PadMessage(SHA1_CTX *ctx)
  181. {
  182. /*
  183. * Check to see if the current message block is too small to hold
  184. * the initial padding bits and length. If so, we will pad the
  185. * block, process it, and then continue padding into a second
  186. * block.
  187. */
  188. if (ctx->Message_Block_Index > 55)
  189. {
  190. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  191. while(ctx->Message_Block_Index < 64)
  192. {
  193. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  194. }
  195. SHA1ProcessMessageBlock(ctx);
  196. while (ctx->Message_Block_Index < 56)
  197. {
  198. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  199. }
  200. }
  201. else
  202. {
  203. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  204. while(ctx->Message_Block_Index < 56)
  205. {
  206. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  207. }
  208. }
  209. /*
  210. * Store the message length as the last 8 octets
  211. */
  212. ctx->Message_Block[56] = ctx->Length_High >> 24;
  213. ctx->Message_Block[57] = ctx->Length_High >> 16;
  214. ctx->Message_Block[58] = ctx->Length_High >> 8;
  215. ctx->Message_Block[59] = ctx->Length_High;
  216. ctx->Message_Block[60] = ctx->Length_Low >> 24;
  217. ctx->Message_Block[61] = ctx->Length_Low >> 16;
  218. ctx->Message_Block[62] = ctx->Length_Low >> 8;
  219. ctx->Message_Block[63] = ctx->Length_Low;
  220. SHA1ProcessMessageBlock(ctx);
  221. }