sha1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*! \file
  2. *
  3. * \brief Based on the RFC 3174
  4. *
  5. * Full Copyright Statement
  6. *
  7. * Copyright (C) The Internet Society (2001). All Rights Reserved.
  8. *
  9. * This document and translations of it may be copied and furnished to
  10. * others, and derivative works that comment on or otherwise explain it
  11. * or assist in its implementation may be prepared, copied, published
  12. * and distributed, in whole or in part, without restriction of any
  13. * kind, provided that the above copyright notice and this paragraph are
  14. * included on all such copies and derivative works. However, this
  15. * document itself may not be modified in any way, such as by removing
  16. * the copyright notice or references to the Internet Society or other
  17. * Internet organizations, except as needed for the purpose of
  18. * developing Internet standards in which case the procedures for
  19. * copyrights defined in the Internet Standards process must be
  20. * followed, or as required to translate it into languages other than
  21. * English.
  22. *
  23. * The limited permissions granted above are perpetual and will not be
  24. * revoked by the Internet Society or its successors or assigns.
  25. * This document and the information contained herein is provided on an
  26. * "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  27. * TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  28. * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  29. * HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  31. *
  32. *
  33. *
  34. * Description:
  35. * This file implements the Secure Hashing Algorithm 1 as
  36. * defined in FIPS PUB 180-1 published April 17, 1995.
  37. *
  38. * The SHA-1, produces a 160-bit message digest for a given
  39. * data stream. It should take about 2**n steps to find a
  40. * message with the same digest as a given message and
  41. * 2**(n/2) to find any two messages with the same digest,
  42. * when n is the digest size in bits. Therefore, this
  43. * algorithm can serve as a means of providing a
  44. * "fingerprint" for a message.
  45. *
  46. * Portability Issues:
  47. * SHA-1 is defined in terms of 32-bit "words". This code
  48. * uses <stdint.h> (included via "sha1.h" to define 32 and 8
  49. * bit unsigned integer types. If your C compiler does not
  50. * support 32 bit unsigned integers, this code is not
  51. * appropriate.
  52. *
  53. * Caveats:
  54. * SHA-1 is designed to work with messages less than 2^64 bits
  55. * long. Although SHA-1 allows a message digest to be generated
  56. * for messages of any number of bits less than 2^64, this
  57. * implementation only works with messages with a length that is
  58. * a multiple of the size of an 8-bit character.
  59. *
  60. */
  61. #include "asterisk.h"
  62. #include "asterisk/sha1.h"
  63. /*! Define the SHA1 circular left shift macro */
  64. #define SHA1CircularShift(bits,word) \
  65. (((word) << (bits)) | ((word) >> (32-(bits))))
  66. /* Local Function Prototyptes */
  67. void SHA1PadMessage(SHA1Context *);
  68. void SHA1ProcessMessageBlock(SHA1Context *);
  69. /*!
  70. * \brief SHA1Reset
  71. * \param context the context to be reset.
  72. * This function will initialize the SHA1Context in preparation
  73. * for computing a new SHA1 message digest.
  74. * \return sha Error Code.
  75. */
  76. int SHA1Reset(SHA1Context *context)
  77. {
  78. if (!context) {
  79. return shaNull;
  80. }
  81. context->Length_Low = 0;
  82. context->Length_High = 0;
  83. context->Message_Block_Index = 0;
  84. context->Intermediate_Hash[0] = 0x67452301;
  85. context->Intermediate_Hash[1] = 0xEFCDAB89;
  86. context->Intermediate_Hash[2] = 0x98BADCFE;
  87. context->Intermediate_Hash[3] = 0x10325476;
  88. context->Intermediate_Hash[4] = 0xC3D2E1F0;
  89. context->Computed = 0;
  90. context->Corrupted = 0;
  91. return shaSuccess;
  92. }
  93. /*!
  94. * \brief SHA1Result
  95. * \param context [in/out] The context to use to calculate the SHA-1 hash.
  96. * \param Message_Digest [out] Where the digest is returned.
  97. * This function will return the 160-bit message digest into the
  98. * Message_Digest array provided by the caller.
  99. * \note The first octet of hash is stored in the 0th element,
  100. * the last octet of hash in the 19th element.
  101. * \return sha Error Code.
  102. */
  103. int SHA1Result( SHA1Context *context,
  104. uint8_t Message_Digest[SHA1HashSize])
  105. {
  106. int i;
  107. if (!context || !Message_Digest) {
  108. return shaNull;
  109. }
  110. if (context->Corrupted) {
  111. return context->Corrupted;
  112. }
  113. if (!context->Computed) {
  114. SHA1PadMessage(context);
  115. for (i = 0; i < 64; ++i) {
  116. /* message may be sensitive, clear it out */
  117. context->Message_Block[i] = 0;
  118. }
  119. context->Length_Low = 0; /* and clear length */
  120. context->Length_High = 0;
  121. context->Computed = 1;
  122. }
  123. for (i = 0; i < SHA1HashSize; ++i) {
  124. Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
  125. }
  126. return shaSuccess;
  127. }
  128. /*!
  129. * \brief SHA1Input
  130. * \param context [in/out] The SHA context to update
  131. * \param message_array [in] An array of characters representing the next portion of
  132. * the message.
  133. * \param length [in] The length of the message in message_array.
  134. * This function accepts an array of octets as the next portion
  135. * of the message.
  136. * \return sha Error Code.
  137. */
  138. int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
  139. {
  140. if (!length) {
  141. return shaSuccess;
  142. }
  143. if (!context || !message_array) {
  144. return shaNull;
  145. }
  146. if (context->Computed) {
  147. context->Corrupted = shaStateError;
  148. return shaStateError;
  149. }
  150. if (context->Corrupted) {
  151. return context->Corrupted;
  152. }
  153. while (length-- && !context->Corrupted) {
  154. context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
  155. context->Length_Low += 8;
  156. if (context->Length_Low == 0) {
  157. context->Length_High++;
  158. if (context->Length_High == 0) {
  159. /* Message is too long */
  160. context->Corrupted = 1;
  161. }
  162. }
  163. if (context->Message_Block_Index == 64) {
  164. SHA1ProcessMessageBlock(context);
  165. }
  166. message_array++;
  167. }
  168. return shaSuccess;
  169. }
  170. /*!
  171. * \brief Process the next 512 bits of the message stored in the Message_Block array.
  172. * \param context [in/out] The SHA context to update
  173. * \note Many of the variable names in this code, especially the
  174. * single character names, were used because those were the
  175. * names used in the publication.
  176. * \returns nothing.
  177. */
  178. void SHA1ProcessMessageBlock(SHA1Context *context)
  179. {
  180. const uint32_t K[] = { /* Constants defined in SHA-1 */
  181. 0x5A827999,
  182. 0x6ED9EBA1,
  183. 0x8F1BBCDC,
  184. 0xCA62C1D6
  185. };
  186. int t; /* Loop counter */
  187. uint32_t temp; /* Temporary word value */
  188. uint32_t W[80]; /* Word sequence */
  189. uint32_t A, B, C, D, E; /* Word buffers */
  190. /*
  191. * Initialize the first 16 words in the array W
  192. */
  193. for (t = 0; t < 16; t++) {
  194. W[t] = context->Message_Block[t * 4] << 24;
  195. W[t] |= context->Message_Block[t * 4 + 1] << 16;
  196. W[t] |= context->Message_Block[t * 4 + 2] << 8;
  197. W[t] |= context->Message_Block[t * 4 + 3];
  198. }
  199. for (t = 16; t < 80; t++) {
  200. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  201. }
  202. A = context->Intermediate_Hash[0];
  203. B = context->Intermediate_Hash[1];
  204. C = context->Intermediate_Hash[2];
  205. D = context->Intermediate_Hash[3];
  206. E = context->Intermediate_Hash[4];
  207. for (t = 0; t < 20; t++) {
  208. temp = SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  209. E = D;
  210. D = C;
  211. C = SHA1CircularShift(30,B);
  212. B = A;
  213. A = temp;
  214. }
  215. for (t = 20; t < 40; t++) {
  216. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  217. E = D;
  218. D = C;
  219. C = SHA1CircularShift(30,B);
  220. B = A;
  221. A = temp;
  222. }
  223. for (t = 40; t < 60; t++) {
  224. temp = SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  225. E = D;
  226. D = C;
  227. C = SHA1CircularShift(30,B);
  228. B = A;
  229. A = temp;
  230. }
  231. for (t = 60; t < 80; t++) {
  232. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  233. E = D;
  234. D = C;
  235. C = SHA1CircularShift(30,B);
  236. B = A;
  237. A = temp;
  238. }
  239. context->Intermediate_Hash[0] += A;
  240. context->Intermediate_Hash[1] += B;
  241. context->Intermediate_Hash[2] += C;
  242. context->Intermediate_Hash[3] += D;
  243. context->Intermediate_Hash[4] += E;
  244. context->Message_Block_Index = 0;
  245. }
  246. /*!
  247. * \brief Pad message to be 512 bits.
  248. * \param context [in/out] The context to pad.
  249. *
  250. * According to the standard, the message must be padded to an even
  251. * 512 bits. The first padding bit must be a '1'. The last 64
  252. * bits represent the length of the original message. All bits in
  253. * between should be 0. This function will pad the message
  254. * according to those rules by filling the Message_Block array
  255. * accordingly. It will also call the ProcessMessageBlock function
  256. * provided appropriately. When it returns, it can be assumed that
  257. * the message digest has been computed.
  258. *
  259. * \returns nothing.
  260. */
  261. void SHA1PadMessage(SHA1Context *context)
  262. {
  263. /*
  264. * Check to see if the current message block is too small to hold
  265. * the initial padding bits and length. If so, we will pad the
  266. * block, process it, and then continue padding into a second
  267. * block.
  268. */
  269. if (context->Message_Block_Index > 55) {
  270. context->Message_Block[context->Message_Block_Index++] = 0x80;
  271. while (context->Message_Block_Index < 64) {
  272. context->Message_Block[context->Message_Block_Index++] = 0;
  273. }
  274. SHA1ProcessMessageBlock(context);
  275. while (context->Message_Block_Index < 56) {
  276. context->Message_Block[context->Message_Block_Index++] = 0;
  277. }
  278. } else {
  279. context->Message_Block[context->Message_Block_Index++] = 0x80;
  280. while (context->Message_Block_Index < 56) {
  281. context->Message_Block[context->Message_Block_Index++] = 0;
  282. }
  283. }
  284. /*
  285. * Store the message length as the last 8 octets
  286. */
  287. context->Message_Block[56] = context->Length_High >> 24;
  288. context->Message_Block[57] = context->Length_High >> 16;
  289. context->Message_Block[58] = context->Length_High >> 8;
  290. context->Message_Block[59] = context->Length_High;
  291. context->Message_Block[60] = context->Length_Low >> 24;
  292. context->Message_Block[61] = context->Length_Low >> 16;
  293. context->Message_Block[62] = context->Length_Low >> 8;
  294. context->Message_Block[63] = context->Length_Low;
  295. SHA1ProcessMessageBlock(context);
  296. }