sha1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*! \file
  2. *
  3. * \brief Based on the RFC 6234
  4. *
  5. * Copyright (c) 2011 IETF Trust and the persons identified as
  6. * authors of the code. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or
  9. * without modification, are permitted provided that the following
  10. * conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above
  13. * copyright notice, this list of conditions and
  14. * the following disclaimer.
  15. *
  16. * - Redistributions in binary form must reproduce the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer in the documentation and/or other materials provided
  19. * with the distribution.
  20. *
  21. * - Neither the name of Internet Society, IETF or IETF Trust, nor
  22. * the names of specific contributors, may be used to endorse or
  23. * promote products derived from this software without specific
  24. * prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  27. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  28. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  31. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  37. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  38. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * Description:
  41. * This file implements the Secure Hash Algorithm SHA-1
  42. * as defined in the U.S. National Institute of Standards
  43. * and Technology Federal Information Processing Standards
  44. * Publication (FIPS PUB) 180-3 published in October 2008
  45. * and formerly defined in its predecessors, FIPS PUB 180-1
  46. * and FIP PUB 180-2.
  47. *
  48. * A combined document showing all algorithms is available at
  49. * http://csrc.nist.gov/publications/fips/
  50. * fips180-3/fips180-3_final.pdf
  51. *
  52. * The SHA-1 algorithm produces a 160-bit message digest for a
  53. * given data stream that can serve as a means of providing a
  54. * "fingerprint" for a message.
  55. *
  56. * Portability Issues:
  57. * SHA-1 is defined in terms of 32-bit "words". This code
  58. * uses <stdint.h> (included via "sha.h") to define 32- and
  59. * 8-bit unsigned integer types. If your C compiler does
  60. * not support 32-bit unsigned integers, this code is not
  61. * appropriate.
  62. *
  63. * Caveats:
  64. * SHA-1 is designed to work with messages less than 2^64 bits
  65. * long. This implementation uses SHA1Input() to hash the bits
  66. * that are a multiple of the size of an 8-bit octet, and then
  67. * optionally uses SHA1FinalBits() to hash the final few bits of
  68. * the input.
  69. */
  70. #include <asterisk/sha1.h>
  71. /*! Define the SHA1 circular left shift macro */
  72. #define SHA1_ROTL(bits,word) \
  73. (((word) << (bits)) | ((word) >> (32-(bits))))
  74. /*
  75. * Add "length" to the length.
  76. * Set Corrupted when overflow has occurred.
  77. */
  78. static uint32_t addTemp;
  79. #define SHA1AddLength(context, length) \
  80. (addTemp = (context)->Length_Low, \
  81. (context)->Corrupted = \
  82. (((context)->Length_Low += (length)) < addTemp) && \
  83. (++(context)->Length_High == 0) ? shaInputTooLong \
  84. : (context)->Corrupted )
  85. /* Local Function Prototypes */
  86. static void SHA1ProcessMessageBlock(SHA1Context * context);
  87. static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte);
  88. static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte);
  89. /*!
  90. * \brief SHA1Reset
  91. * \param context the context to be reset.
  92. * This function will initialize the SHA1Context in preparation
  93. * for computing a new SHA1 message digest.
  94. * \return sha Error Code.
  95. */
  96. int SHA1Reset(SHA1Context *context)
  97. {
  98. if (!context) {
  99. return shaNull;
  100. }
  101. context->Length_High = context->Length_Low = 0;
  102. context->Message_Block_Index = 0;
  103. /* Initial Hash Values: FIPS 180-3 section 5.3.1 */
  104. context->Intermediate_Hash[0] = 0x67452301;
  105. context->Intermediate_Hash[1] = 0xEFCDAB89;
  106. context->Intermediate_Hash[2] = 0x98BADCFE;
  107. context->Intermediate_Hash[3] = 0x10325476;
  108. context->Intermediate_Hash[4] = 0xC3D2E1F0;
  109. context->Computed = 0;
  110. context->Corrupted = shaSuccess;
  111. return shaSuccess;
  112. }
  113. /*!
  114. * \brief SHA1Input
  115. * \param context [in/out] The SHA context to update
  116. * \param message_array [in] An array of characters representing the next portion of
  117. * the message.
  118. * \param length [in] The length of the message in message_array.
  119. * This function accepts an array of octets as the next portion
  120. * of the message.
  121. * \return sha Error Code.
  122. */
  123. int SHA1Input(SHA1Context *context,
  124. const uint8_t *message_array, unsigned length)
  125. {
  126. if (!context) {
  127. return shaNull;
  128. }
  129. if (!length) {
  130. return shaSuccess;
  131. }
  132. if (!message_array) {
  133. return shaNull;
  134. }
  135. if (context->Computed) {
  136. context->Corrupted = shaStateError;
  137. return shaStateError;
  138. }
  139. if (context->Corrupted) {
  140. return context->Corrupted;
  141. }
  142. while (length--) {
  143. context->Message_Block[context->Message_Block_Index++] =
  144. *message_array;
  145. if ((SHA1AddLength(context, 8) == shaSuccess) &&
  146. (context->Message_Block_Index == SHA1_Message_Block_Size))
  147. SHA1ProcessMessageBlock(context);
  148. message_array++;
  149. }
  150. return context->Corrupted;
  151. }
  152. /*!
  153. * \brief SHA1FinalBits Add in any final bits of the message.
  154. *
  155. * \param context [in/out] The SHA context to update.
  156. * \param message_bits [in] The final bits of the message, in the upper portion of the
  157. * byte. (Use 0b###00000 instead of 0b00000### to input the
  158. * three bits ###.)
  159. * \param length [in] * The number of bits in message_bits, between 1 and 7.
  160. * \returns sha Error Code.
  161. */
  162. int SHA1FinalBits(SHA1Context * context, uint8_t message_bits,
  163. unsigned int length)
  164. {
  165. static uint8_t masks[8] = {
  166. /* 0 0b00000000 */ 0x00, /* 1 0b10000000 */ 0x80,
  167. /* 2 0b11000000 */ 0xC0, /* 3 0b11100000 */ 0xE0,
  168. /* 4 0b11110000 */ 0xF0, /* 5 0b11111000 */ 0xF8,
  169. /* 6 0b11111100 */ 0xFC, /* 7 0b11111110 */ 0xFE
  170. };
  171. static uint8_t markbit[8] = {
  172. /* 0 0b10000000 */ 0x80, /* 1 0b01000000 */ 0x40,
  173. /* 2 0b00100000 */ 0x20, /* 3 0b00010000 */ 0x10,
  174. /* 4 0b00001000 */ 0x08, /* 5 0b00000100 */ 0x04,
  175. /* 6 0b00000010 */ 0x02, /* 7 0b00000001 */ 0x01
  176. };
  177. if (!context)
  178. return shaNull;
  179. if (!length)
  180. return shaSuccess;
  181. if (context->Corrupted)
  182. return context->Corrupted;
  183. if (context->Computed)
  184. return context->Corrupted = shaStateError;
  185. if (length >= 8)
  186. return context->Corrupted = shaBadParam;
  187. SHA1AddLength(context, length);
  188. SHA1Finalize(context,
  189. (uint8_t) ((message_bits & masks[length]) |
  190. markbit[length]));
  191. return context->Corrupted;
  192. }
  193. /*
  194. * \brief SHA1Result Returns the resulting 160-bit digest
  195. * \param context [in/out] The SHA context to update.
  196. * \param Message_Digest [out] Where the digest is returned.
  197. *
  198. * This function will return the 160-bit message digest
  199. * into the Message_Digest array provided by the caller.
  200. * \note The first octet of hash is stored in the element with index 0,
  201. * the last octet of hash in the element with index 19.
  202. * \returns sha Error Code.
  203. */
  204. int SHA1Result(SHA1Context * context, uint8_t Message_Digest[SHA1HashSize])
  205. {
  206. int i;
  207. if (!context) {
  208. return shaNull;
  209. }
  210. if (!Message_Digest) {
  211. return shaNull;
  212. }
  213. if (context->Corrupted) {
  214. return context->Corrupted;
  215. }
  216. if (!context->Computed) {
  217. SHA1Finalize(context, 0x80);
  218. }
  219. for (i = 0; i < SHA1HashSize; ++i) {
  220. Message_Digest[i] = (uint8_t) (context->Intermediate_Hash[i >> 2]
  221. >> (8 * (3 - (i & 0x03))));
  222. }
  223. return shaSuccess;
  224. }
  225. /*!
  226. * \brief Process the next 512 bits of the message stored in the Message_Block array.
  227. * \param context [in/out] The SHA context to update
  228. * \note Many of the variable names in this code, especially the
  229. * single character names, were used because those were the
  230. * names used in the publication.
  231. * \returns nothing.
  232. */
  233. static void SHA1ProcessMessageBlock(SHA1Context *context)
  234. {
  235. /* Constants defined in FIPS 180-3, section 4.2.1 */
  236. const uint32_t K[4] = {
  237. 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6
  238. };
  239. int t; /* Loop counter */
  240. uint32_t temp; /* Temporary word value */
  241. uint32_t W[80]; /* Word sequence */
  242. uint32_t A, B, C, D, E; /* Word buffers */
  243. /*
  244. * Initialize the first 16 words in the array W
  245. */
  246. for (t = 0; t < 16; t++) {
  247. W[t] = ((uint32_t) context->Message_Block[t * 4]) << 24;
  248. W[t] |= ((uint32_t) context->Message_Block[t * 4 + 1]) << 16;
  249. W[t] |= ((uint32_t) context->Message_Block[t * 4 + 2]) << 8;
  250. W[t] |= ((uint32_t) context->Message_Block[t * 4 + 3]);
  251. }
  252. for (t = 16; t < 80; t++) {
  253. W[t] = SHA1_ROTL(1, W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
  254. }
  255. A = context->Intermediate_Hash[0];
  256. B = context->Intermediate_Hash[1];
  257. C = context->Intermediate_Hash[2];
  258. D = context->Intermediate_Hash[3];
  259. E = context->Intermediate_Hash[4];
  260. for (t = 0; t < 20; t++) {
  261. temp = SHA1_ROTL(5, A) + SHA_Ch(B, C, D) + E + W[t] + K[0];
  262. E = D;
  263. D = C;
  264. C = SHA1_ROTL(30, B);
  265. B = A;
  266. A = temp;
  267. }
  268. for (t = 20; t < 40; t++) {
  269. temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[1];
  270. E = D;
  271. D = C;
  272. C = SHA1_ROTL(30, B);
  273. B = A;
  274. A = temp;
  275. }
  276. for (t = 40; t < 60; t++) {
  277. temp = SHA1_ROTL(5, A) + SHA_Maj(B, C, D) + E + W[t] + K[2];
  278. E = D;
  279. D = C;
  280. C = SHA1_ROTL(30, B);
  281. B = A;
  282. A = temp;
  283. }
  284. for (t = 60; t < 80; t++) {
  285. temp = SHA1_ROTL(5, A) + SHA_Parity(B, C, D) + E + W[t] + K[3];
  286. E = D;
  287. D = C;
  288. C = SHA1_ROTL(30, B);
  289. B = A;
  290. A = temp;
  291. }
  292. context->Intermediate_Hash[0] += A;
  293. context->Intermediate_Hash[1] += B;
  294. context->Intermediate_Hash[2] += C;
  295. context->Intermediate_Hash[3] += D;
  296. context->Intermediate_Hash[4] += E;
  297. context->Message_Block_Index = 0;
  298. }
  299. /*!
  300. * \brief This helper function finishes off the digest calculations.
  301. * \param context [in/out] The context to pad.
  302. * \param Pad_Byte [in] The last byte to add to the message block
  303. * before the 0-padding and length. This will contain the last
  304. * bits of the message followed by another single bit. If the
  305. * message was an exact multiple of 8-bits long, Pad_Byte will
  306. * be 0x80.
  307. * \returns sha Error Code.
  308. */
  309. static void SHA1Finalize(SHA1Context * context, uint8_t Pad_Byte)
  310. {
  311. int i;
  312. SHA1PadMessage(context, Pad_Byte);
  313. /* message may be sensitive, clear it out */
  314. for (i = 0; i < SHA1_Message_Block_Size; ++i) {
  315. context->Message_Block[i] = 0;
  316. }
  317. context->Length_High = 0; /* and clear length */
  318. context->Length_Low = 0;
  319. context->Computed = 1;
  320. }
  321. /*!
  322. * \brief Pad message to be 512 bits.
  323. * \param context [in/out] The context to pad.
  324. * \param Pad_Byte [in] Last padding byte.
  325. *
  326. * According to the standard, the message must be padded to the next
  327. * even multiple of 512 bits. The first padding bit must be a '1'.
  328. * The last 64 bits represent the length of the original message.
  329. * All bits in between should be 0. This helper function will pad
  330. * the message according to those rules by filling the Message_Block
  331. * array accordingly. When it returns, it can be assumed that the
  332. * message digest has been computed.
  333. *
  334. * \returns nothing.
  335. */
  336. static void SHA1PadMessage(SHA1Context * context, uint8_t Pad_Byte)
  337. {
  338. /*
  339. * Check to see if the current message block is too small to hold
  340. * the initial padding bits and length. If so, we will pad the
  341. * block, process it, and then continue padding into a second
  342. * block.
  343. */
  344. if (context->Message_Block_Index >= (SHA1_Message_Block_Size - 8)) {
  345. context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
  346. while (context->Message_Block_Index < SHA1_Message_Block_Size) {
  347. context->Message_Block[context->Message_Block_Index++] = 0;
  348. }
  349. SHA1ProcessMessageBlock(context);
  350. } else
  351. context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
  352. while (context->Message_Block_Index < (SHA1_Message_Block_Size - 8)) {
  353. context->Message_Block[context->Message_Block_Index++] = 0;
  354. }
  355. /*
  356. * Store the message length as the last 8 octets
  357. */
  358. context->Message_Block[56] = (uint8_t) (context->Length_High >> 24);
  359. context->Message_Block[57] = (uint8_t) (context->Length_High >> 16);
  360. context->Message_Block[58] = (uint8_t) (context->Length_High >> 8);
  361. context->Message_Block[59] = (uint8_t) (context->Length_High);
  362. context->Message_Block[60] = (uint8_t) (context->Length_Low >> 24);
  363. context->Message_Block[61] = (uint8_t) (context->Length_Low >> 16);
  364. context->Message_Block[62] = (uint8_t) (context->Length_Low >> 8);
  365. context->Message_Block[63] = (uint8_t) (context->Length_Low);
  366. SHA1ProcessMessageBlock(context);
  367. }