sha1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. *
  3. * 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/sha1.h"
  62. /*
  63. * Define the SHA1 circular left shift macro
  64. */
  65. #define SHA1CircularShift(bits,word) \
  66. (((word) << (bits)) | ((word) >> (32-(bits))))
  67. /* Local Function Prototyptes */
  68. void SHA1PadMessage(SHA1Context *);
  69. void SHA1ProcessMessageBlock(SHA1Context *);
  70. /*
  71. * SHA1Reset
  72. *
  73. * Description:
  74. * This function will initialize the SHA1Context in preparation
  75. * for computing a new SHA1 message digest.
  76. *
  77. * Parameters:
  78. * context: [in/out]
  79. * The context to reset.
  80. *
  81. * Returns:
  82. * sha Error Code.
  83. *
  84. */
  85. int SHA1Reset(SHA1Context *context)
  86. {
  87. if (!context) {
  88. return shaNull;
  89. }
  90. context->Length_Low = 0;
  91. context->Length_High = 0;
  92. context->Message_Block_Index = 0;
  93. context->Intermediate_Hash[0] = 0x67452301;
  94. context->Intermediate_Hash[1] = 0xEFCDAB89;
  95. context->Intermediate_Hash[2] = 0x98BADCFE;
  96. context->Intermediate_Hash[3] = 0x10325476;
  97. context->Intermediate_Hash[4] = 0xC3D2E1F0;
  98. context->Computed = 0;
  99. context->Corrupted = 0;
  100. return shaSuccess;
  101. }
  102. /*
  103. * SHA1Result
  104. *
  105. * Description:
  106. * This function will return the 160-bit message digest into the
  107. * Message_Digest array provided by the caller.
  108. * NOTE: The first octet of hash is stored in the 0th element,
  109. * the last octet of hash in the 19th element.
  110. *
  111. * Parameters:
  112. * context: [in/out]
  113. * The context to use to calculate the SHA-1 hash.
  114. * Message_Digest: [out]
  115. * Where the digest is returned.
  116. *
  117. * Returns:
  118. * sha Error Code.
  119. *
  120. */
  121. int SHA1Result( SHA1Context *context,
  122. uint8_t Message_Digest[SHA1HashSize])
  123. {
  124. int i;
  125. if (!context || !Message_Digest) {
  126. return shaNull;
  127. }
  128. if (context->Corrupted) {
  129. return context->Corrupted;
  130. }
  131. if (!context->Computed) {
  132. SHA1PadMessage(context);
  133. for (i = 0; i < 64; ++i) {
  134. /* message may be sensitive, clear it out */
  135. context->Message_Block[i] = 0;
  136. }
  137. context->Length_Low = 0; /* and clear length */
  138. context->Length_High = 0;
  139. context->Computed = 1;
  140. }
  141. for (i = 0; i < SHA1HashSize; ++i) {
  142. Message_Digest[i] = context->Intermediate_Hash[i >> 2] >> 8 * ( 3 - ( i & 0x03 ) );
  143. }
  144. return shaSuccess;
  145. }
  146. /*
  147. * SHA1Input
  148. *
  149. * Description:
  150. * This function accepts an array of octets as the next portion
  151. * of the message.
  152. *
  153. * Parameters:
  154. * context: [in/out]
  155. * The SHA context to update
  156. * message_array: [in]
  157. * An array of characters representing the next portion of
  158. * the message.
  159. * length: [in]
  160. * The length of the message in message_array
  161. *
  162. * Returns:
  163. * sha Error Code.
  164. *
  165. */
  166. int SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length)
  167. {
  168. if (!length) {
  169. return shaSuccess;
  170. }
  171. if (!context || !message_array) {
  172. return shaNull;
  173. }
  174. if (context->Computed) {
  175. context->Corrupted = shaStateError;
  176. return shaStateError;
  177. }
  178. if (context->Corrupted) {
  179. return context->Corrupted;
  180. }
  181. while (length-- && !context->Corrupted) {
  182. context->Message_Block[context->Message_Block_Index++] = (*message_array & 0xFF);
  183. context->Length_Low += 8;
  184. if (context->Length_Low == 0) {
  185. context->Length_High++;
  186. if (context->Length_High == 0) {
  187. /* Message is too long */
  188. context->Corrupted = 1;
  189. }
  190. }
  191. if (context->Message_Block_Index == 64) {
  192. SHA1ProcessMessageBlock(context);
  193. }
  194. message_array++;
  195. }
  196. return shaSuccess;
  197. }
  198. /*
  199. * SHA1ProcessMessageBlock
  200. *
  201. * Description:
  202. * This function will process the next 512 bits of the message
  203. * stored in the Message_Block array.
  204. *
  205. * Parameters:
  206. * None.
  207. *
  208. * Returns:
  209. * Nothing.
  210. *
  211. * Comments:
  212. * Many of the variable names in this code, especially the
  213. * single character names, were used because those were the
  214. * names used in the publication.
  215. *
  216. *
  217. */
  218. void SHA1ProcessMessageBlock(SHA1Context *context)
  219. {
  220. const uint32_t K[] = { /* Constants defined in SHA-1 */
  221. 0x5A827999,
  222. 0x6ED9EBA1,
  223. 0x8F1BBCDC,
  224. 0xCA62C1D6
  225. };
  226. int t; /* Loop counter */
  227. uint32_t temp; /* Temporary word value */
  228. uint32_t W[80]; /* Word sequence */
  229. uint32_t A, B, C, D, E; /* Word buffers */
  230. /*
  231. * Initialize the first 16 words in the array W
  232. */
  233. for (t = 0; t < 16; t++) {
  234. W[t] = context->Message_Block[t * 4] << 24;
  235. W[t] |= context->Message_Block[t * 4 + 1] << 16;
  236. W[t] |= context->Message_Block[t * 4 + 2] << 8;
  237. W[t] |= context->Message_Block[t * 4 + 3];
  238. }
  239. for (t = 16; t < 80; t++) {
  240. W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  241. }
  242. A = context->Intermediate_Hash[0];
  243. B = context->Intermediate_Hash[1];
  244. C = context->Intermediate_Hash[2];
  245. D = context->Intermediate_Hash[3];
  246. E = context->Intermediate_Hash[4];
  247. for (t = 0; t < 20; t++) {
  248. temp = SHA1CircularShift(5,A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  249. E = D;
  250. D = C;
  251. C = SHA1CircularShift(30,B);
  252. B = A;
  253. A = temp;
  254. }
  255. for (t = 20; t < 40; t++) {
  256. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
  257. E = D;
  258. D = C;
  259. C = SHA1CircularShift(30,B);
  260. B = A;
  261. A = temp;
  262. }
  263. for (t = 40; t < 60; t++) {
  264. temp = SHA1CircularShift(5,A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  265. E = D;
  266. D = C;
  267. C = SHA1CircularShift(30,B);
  268. B = A;
  269. A = temp;
  270. }
  271. for (t = 60; t < 80; t++) {
  272. temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
  273. E = D;
  274. D = C;
  275. C = SHA1CircularShift(30,B);
  276. B = A;
  277. A = temp;
  278. }
  279. context->Intermediate_Hash[0] += A;
  280. context->Intermediate_Hash[1] += B;
  281. context->Intermediate_Hash[2] += C;
  282. context->Intermediate_Hash[3] += D;
  283. context->Intermediate_Hash[4] += E;
  284. context->Message_Block_Index = 0;
  285. }
  286. /*
  287. * SHA1PadMessage
  288. *
  289. * Description:
  290. * According to the standard, the message must be padded to an even
  291. * 512 bits. The first padding bit must be a '1'. The last 64
  292. * bits represent the length of the original message. All bits in
  293. * between should be 0. This function will pad the message
  294. * according to those rules by filling the Message_Block array
  295. * accordingly. It will also call the ProcessMessageBlock function
  296. * provided appropriately. When it returns, it can be assumed that
  297. * the message digest has been computed.
  298. *
  299. * Parameters:
  300. * context: [in/out]
  301. * The context to pad
  302. * ProcessMessageBlock: [in]
  303. * The appropriate SHA*ProcessMessageBlock function
  304. * Returns:
  305. * Nothing.
  306. *
  307. */
  308. void SHA1PadMessage(SHA1Context *context)
  309. {
  310. /*
  311. * Check to see if the current message block is too small to hold
  312. * the initial padding bits and length. If so, we will pad the
  313. * block, process it, and then continue padding into a second
  314. * block.
  315. */
  316. if (context->Message_Block_Index > 55) {
  317. context->Message_Block[context->Message_Block_Index++] = 0x80;
  318. while (context->Message_Block_Index < 64) {
  319. context->Message_Block[context->Message_Block_Index++] = 0;
  320. }
  321. SHA1ProcessMessageBlock(context);
  322. while (context->Message_Block_Index < 56) {
  323. context->Message_Block[context->Message_Block_Index++] = 0;
  324. }
  325. } else {
  326. context->Message_Block[context->Message_Block_Index++] = 0x80;
  327. while (context->Message_Block_Index < 56) {
  328. context->Message_Block[context->Message_Block_Index++] = 0;
  329. }
  330. }
  331. /*
  332. * Store the message length as the last 8 octets
  333. */
  334. context->Message_Block[56] = context->Length_High >> 24;
  335. context->Message_Block[57] = context->Length_High >> 16;
  336. context->Message_Block[58] = context->Length_High >> 8;
  337. context->Message_Block[59] = context->Length_High;
  338. context->Message_Block[60] = context->Length_Low >> 24;
  339. context->Message_Block[61] = context->Length_Low >> 16;
  340. context->Message_Block[62] = context->Length_Low >> 8;
  341. context->Message_Block[63] = context->Length_Low;
  342. SHA1ProcessMessageBlock(context);
  343. }