sha1.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 "os_port.h"
  36. #include "crypto.h"
  37. /*
  38. * Define the SHA1 circular left shift macro
  39. */
  40. #define SHA1CircularShift(bits,word) \
  41. (((word) << (bits)) | ((word) >> (32-(bits))))
  42. /* ----- static functions ----- */
  43. static void SHA1PadMessage(SHA1_CTX *ctx);
  44. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx);
  45. /**
  46. * Initialize the SHA1 context
  47. */
  48. void SHA1_Init(SHA1_CTX *ctx)
  49. {
  50. ctx->Length_Low = 0;
  51. ctx->Length_High = 0;
  52. ctx->Message_Block_Index = 0;
  53. ctx->Intermediate_Hash[0] = 0x67452301;
  54. ctx->Intermediate_Hash[1] = 0xEFCDAB89;
  55. ctx->Intermediate_Hash[2] = 0x98BADCFE;
  56. ctx->Intermediate_Hash[3] = 0x10325476;
  57. ctx->Intermediate_Hash[4] = 0xC3D2E1F0;
  58. }
  59. /**
  60. * Accepts an array of octets as the next portion of the message.
  61. */
  62. void SHA1_Update(SHA1_CTX *ctx, const uint8_t *msg, int len)
  63. {
  64. while (len--)
  65. {
  66. ctx->Message_Block[ctx->Message_Block_Index++] = (*msg & 0xFF);
  67. ctx->Length_Low += 8;
  68. if (ctx->Length_Low == 0)
  69. ctx->Length_High++;
  70. if (ctx->Message_Block_Index == 64)
  71. SHA1ProcessMessageBlock(ctx);
  72. msg++;
  73. }
  74. }
  75. /**
  76. * Return the 160-bit message digest into the user's array
  77. */
  78. void SHA1_Final(uint8_t *digest, SHA1_CTX *ctx)
  79. {
  80. int i;
  81. SHA1PadMessage(ctx);
  82. memset(ctx->Message_Block, 0, 64);
  83. ctx->Length_Low = 0; /* and clear length */
  84. ctx->Length_High = 0;
  85. for (i = 0; i < SHA1_SIZE; i++)
  86. {
  87. digest[i] = ctx->Intermediate_Hash[i>>2] >> 8 * ( 3 - ( i & 0x03 ) );
  88. }
  89. }
  90. /**
  91. * Process the next 512 bits of the message stored in the array.
  92. */
  93. static void SHA1ProcessMessageBlock(SHA1_CTX *ctx)
  94. {
  95. const uint32_t K[] = { /* Constants defined in SHA-1 */
  96. 0x5A827999,
  97. 0x6ED9EBA1,
  98. 0x8F1BBCDC,
  99. 0xCA62C1D6
  100. };
  101. int t; /* Loop counter */
  102. uint32_t temp; /* Temporary word value */
  103. uint32_t W[80]; /* Word sequence */
  104. uint32_t A, B, C, D, E; /* Word buffers */
  105. /*
  106. * Initialize the first 16 words in the array W
  107. */
  108. for (t = 0; t < 16; t++)
  109. {
  110. W[t] = ctx->Message_Block[t * 4] << 24;
  111. W[t] |= ctx->Message_Block[t * 4 + 1] << 16;
  112. W[t] |= ctx->Message_Block[t * 4 + 2] << 8;
  113. W[t] |= ctx->Message_Block[t * 4 + 3];
  114. }
  115. for (t = 16; t < 80; t++)
  116. {
  117. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  118. }
  119. A = ctx->Intermediate_Hash[0];
  120. B = ctx->Intermediate_Hash[1];
  121. C = ctx->Intermediate_Hash[2];
  122. D = ctx->Intermediate_Hash[3];
  123. E = ctx->Intermediate_Hash[4];
  124. for (t = 0; t < 20; t++)
  125. {
  126. temp = SHA1CircularShift(5,A) +
  127. ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  128. E = D;
  129. D = C;
  130. C = SHA1CircularShift(30,B);
  131. B = A;
  132. A = temp;
  133. }
  134. for (t = 20; t < 40; t++)
  135. {
  136. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  137. E = D;
  138. D = C;
  139. C = SHA1CircularShift(30,B);
  140. B = A;
  141. A = temp;
  142. }
  143. for (t = 40; t < 60; t++)
  144. {
  145. temp = SHA1CircularShift(5,A) +
  146. ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  147. E = D;
  148. D = C;
  149. C = SHA1CircularShift(30,B);
  150. B = A;
  151. A = temp;
  152. }
  153. for (t = 60; t < 80; t++)
  154. {
  155. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  156. E = D;
  157. D = C;
  158. C = SHA1CircularShift(30,B);
  159. B = A;
  160. A = temp;
  161. }
  162. ctx->Intermediate_Hash[0] += A;
  163. ctx->Intermediate_Hash[1] += B;
  164. ctx->Intermediate_Hash[2] += C;
  165. ctx->Intermediate_Hash[3] += D;
  166. ctx->Intermediate_Hash[4] += E;
  167. ctx->Message_Block_Index = 0;
  168. }
  169. /*
  170. * According to the standard, the message must be padded to an even
  171. * 512 bits. The first padding bit must be a '1'. The last 64
  172. * bits represent the length of the original message. All bits in
  173. * between should be 0. This function will pad the message
  174. * according to those rules by filling the Message_Block array
  175. * accordingly. It will also call the ProcessMessageBlock function
  176. * provided appropriately. When it returns, it can be assumed that
  177. * the message digest has been computed.
  178. *
  179. * @param ctx [in, out] The SHA1 context
  180. */
  181. static void SHA1PadMessage(SHA1_CTX *ctx)
  182. {
  183. /*
  184. * Check to see if the current message block is too small to hold
  185. * the initial padding bits and length. If so, we will pad the
  186. * block, process it, and then continue padding into a second
  187. * block.
  188. */
  189. if (ctx->Message_Block_Index > 55)
  190. {
  191. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  192. while(ctx->Message_Block_Index < 64)
  193. {
  194. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  195. }
  196. SHA1ProcessMessageBlock(ctx);
  197. while (ctx->Message_Block_Index < 56)
  198. {
  199. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  200. }
  201. }
  202. else
  203. {
  204. ctx->Message_Block[ctx->Message_Block_Index++] = 0x80;
  205. while(ctx->Message_Block_Index < 56)
  206. {
  207. ctx->Message_Block[ctx->Message_Block_Index++] = 0;
  208. }
  209. }
  210. /*
  211. * Store the message length as the last 8 octets
  212. */
  213. ctx->Message_Block[56] = ctx->Length_High >> 24;
  214. ctx->Message_Block[57] = ctx->Length_High >> 16;
  215. ctx->Message_Block[58] = ctx->Length_High >> 8;
  216. ctx->Message_Block[59] = ctx->Length_High;
  217. ctx->Message_Block[60] = ctx->Length_Low >> 24;
  218. ctx->Message_Block[61] = ctx->Length_Low >> 16;
  219. ctx->Message_Block[62] = ctx->Length_Low >> 8;
  220. ctx->Message_Block[63] = ctx->Length_Low;
  221. SHA1ProcessMessageBlock(ctx);
  222. }