sha1.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Description:
  3. * This file implements the Secure Hash Signature Standard
  4. * algorithms as defined in the National Institute of Standards
  5. * and Technology Federal Information Processing Standards
  6. * Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2
  7. * published on August 1, 2002, and the FIPS PUB 180-2 Change
  8. * Notice published on February 28, 2004.
  9. *
  10. * A combined document showing all algorithms is available at
  11. * http://csrc.nist.gov/publications/fips/
  12. * fips180-2/fips180-2withchangenotice.pdf
  13. *
  14. * The SHA-1 algorithm produces a 160-bit message digest for a
  15. * given data stream. It should take about 2**n steps to find a
  16. * message with the same digest as a given message and
  17. * 2**(n/2) to find any two messages with the same digest,
  18. * when n is the digest size in bits. Therefore, this
  19. * algorithm can serve as a means of providing a
  20. * "fingerprint" for a message.
  21. *
  22. * Portability Issues:
  23. * SHA-1 is defined in terms of 32-bit "words". This code
  24. * uses <stdint.h> (included via "sha.h") to define 32 and 8
  25. * bit unsigned integer types. If your C compiler does not
  26. * support 32 bit unsigned integers, this code is not
  27. * appropriate.
  28. *
  29. * Caveats:
  30. * SHA-1 is designed to work with messages less than 2^64 bits
  31. * long. This implementation uses SHA1Input() to hash the bits
  32. * that are a multiple of the size of an 8-bit character, and then
  33. * uses SHA1FinalBits() to hash the final few bits of the input.
  34. *
  35. * Authorship:
  36. * This file is adapted from RFC 4634, by D. Eastlake et al.
  37. * Copyright (C) The Internet Society (2006).
  38. *
  39. * Permission is granted for all uses, commercial and non-commercial,
  40. * of the sample code found in Section 8. Royalty free license to
  41. * use, copy, modify and distribute the software found in Section 8 is
  42. * granted, provided that this document is identified in all material
  43. * mentioning or referencing this software, and provided that
  44. * redistributed derivative works do not contain misleading author or
  45. * version information.
  46. *
  47. * The authors make no representations concerning either the
  48. * merchantability of this software or the suitability of this
  49. * software for any particular purpose. It is provided "as is"
  50. * without express or implied warranty of any kind.
  51. *
  52. */
  53. #include <util/hash/sha1.h>
  54. namespace isc {
  55. namespace util {
  56. namespace hash {
  57. /* Local Function Prototyptes */
  58. static void SHA1Finalize(SHA1Context *, uint8_t Pad_Byte);
  59. static void SHA1PadMessage(SHA1Context *, uint8_t Pad_Byte);
  60. static void SHA1ProcessMessageBlock(SHA1Context *);
  61. /*
  62. * Define functions used by SHA1 hash
  63. */
  64. static inline uint32_t
  65. SHA_Ch(const uint32_t x, const uint32_t y, const uint32_t z) {
  66. return (((x) & ((y) ^ (z))) ^ (z));
  67. }
  68. static inline uint32_t
  69. SHA_Maj(const uint32_t x, const uint32_t y, const uint32_t z) {
  70. return (((x) & ((y) | (z))) | ((y) & (z)));
  71. }
  72. static inline uint32_t
  73. SHA_Parity(const uint32_t x, const uint32_t y, const uint32_t z) {
  74. return ((x) ^ (y) ^ (z));
  75. }
  76. static inline int
  77. SHA1CircularShift(uint8_t bits, uint32_t word) {
  78. return ((word << bits) | (word >> (32 - bits)));
  79. }
  80. static inline bool
  81. SHA1AddLength(SHA1Context *context, uint32_t length) {
  82. uint32_t addTemp = context->Length_Low;
  83. context->Length_Low += length;
  84. if (context->Length_Low < addTemp && ++context->Length_High == 0) {
  85. return (true);
  86. } else {
  87. return (false);
  88. }
  89. }
  90. /*
  91. * SHA1Reset
  92. *
  93. * Description:
  94. * This function will initialize the SHA1Context in preparation
  95. * for computing a new SHA1 message digest.
  96. *
  97. * Parameters:
  98. * context: [in/out]
  99. * The context to reset.
  100. *
  101. * Returns:
  102. * sha Error Code.
  103. *
  104. */
  105. int
  106. SHA1Reset(SHA1Context *context) {
  107. if (!context) {
  108. return (SHA_NULL);
  109. }
  110. context->Length_Low = 0;
  111. context->Length_High = 0;
  112. context->Message_Block_Index = 0;
  113. context->Intermediate_Hash[0] = 0x67452301;
  114. context->Intermediate_Hash[1] = 0xEFCDAB89;
  115. context->Intermediate_Hash[2] = 0x98BADCFE;
  116. context->Intermediate_Hash[3] = 0x10325476;
  117. context->Intermediate_Hash[4] = 0xC3D2E1F0;
  118. context->Computed = 0;
  119. context->Corrupted = 0;
  120. return (SHA_SUCCESS);
  121. }
  122. /*
  123. * SHA1Input
  124. *
  125. * Description:
  126. * This function accepts an array of octets as the next portion
  127. * of the message.
  128. *
  129. * Parameters:
  130. * context: [in/out]
  131. * The SHA context to update
  132. * message_array: [in]
  133. * An array of characters representing the next portion of
  134. * the message.
  135. * length: [in]
  136. * The length of the message in message_array
  137. *
  138. * Returns:
  139. * sha Error Code.
  140. *
  141. */
  142. int
  143. SHA1Input(SHA1Context *context, const uint8_t *message_array, unsigned length) {
  144. if (!length) {
  145. return (SHA_SUCCESS);
  146. }
  147. if (!context || !message_array) {
  148. return (SHA_NULL);
  149. }
  150. if (context->Computed) {
  151. context->Corrupted = SHA_STATEERROR;
  152. return (SHA_STATEERROR);
  153. }
  154. if (context->Corrupted) {
  155. return (context->Corrupted);
  156. }
  157. while(length-- && !context->Corrupted) {
  158. context->Message_Block[context->Message_Block_Index++] =
  159. (*message_array & 0xFF);
  160. if (!SHA1AddLength(context, 8) &&
  161. (context->Message_Block_Index == SHA1_BLOCKSIZE))
  162. {
  163. SHA1ProcessMessageBlock(context);
  164. }
  165. message_array++;
  166. }
  167. return (SHA_SUCCESS);
  168. }
  169. /*
  170. * SHA1FinalBits
  171. *
  172. * Description:
  173. * This function will add in any final bits of the message.
  174. *
  175. * Parameters:
  176. * context: [in/out]
  177. * The SHA context to update
  178. * message_bits: [in]
  179. * The final bits of the message, in the upper portion of the
  180. * byte. (Use 0b###00000 instead of 0b00000### to input the
  181. * three bits ###.)
  182. * length: [in]
  183. * The number of bits in message_bits, between 1 and 7.
  184. *
  185. * Returns:
  186. * sha Error Code.
  187. */
  188. int SHA1FinalBits(SHA1Context *context, const uint8_t message_bits,
  189. unsigned int length)
  190. {
  191. uint8_t masks[8] = {
  192. /* 0 0b00000000 */ 0x00,
  193. /* 1 0b10000000 */ 0x80,
  194. /* 2 0b11000000 */ 0xC0,
  195. /* 3 0b11100000 */ 0xE0,
  196. /* 4 0b11110000 */ 0xF0,
  197. /* 5 0b11111000 */ 0xF8,
  198. /* 6 0b11111100 */ 0xFC,
  199. /* 7 0b11111110 */ 0xFE
  200. };
  201. uint8_t markbit[8] = {
  202. /* 0 0b10000000 */ 0x80,
  203. /* 1 0b01000000 */ 0x40,
  204. /* 2 0b00100000 */ 0x20,
  205. /* 3 0b00010000 */ 0x10,
  206. /* 4 0b00001000 */ 0x08,
  207. /* 5 0b00000100 */ 0x04,
  208. /* 6 0b00000010 */ 0x02,
  209. /* 7 0b00000001 */ 0x01
  210. };
  211. if (!length) {
  212. return (SHA_SUCCESS);
  213. }
  214. if (!context) {
  215. return (SHA_NULL);
  216. }
  217. if (context->Computed || (length >= 8) || (length == 0)) {
  218. context->Corrupted = SHA_STATEERROR;
  219. return (SHA_STATEERROR);
  220. }
  221. if (context->Corrupted) {
  222. return (context->Corrupted);
  223. }
  224. SHA1AddLength(context, length);
  225. SHA1Finalize(context,
  226. (uint8_t) ((message_bits & masks[length]) | markbit[length]));
  227. return (SHA_SUCCESS);
  228. }
  229. /*
  230. * SHA1Result
  231. *
  232. * Description:
  233. * This function will return the 160-bit message digest into the
  234. * Message_Digest array provided by the caller.
  235. * NOTE: The first octet of hash is stored in the 0th element,
  236. * the last octet of hash in the 19th element.
  237. *
  238. * Parameters:
  239. * context: [in/out]
  240. * The context to use to calculate the SHA-1 hash.
  241. * Message_Digest: [out]
  242. * Where the digest is returned.
  243. *
  244. * Returns:
  245. * sha Error Code.
  246. *
  247. */
  248. int
  249. SHA1Result(SHA1Context *context, uint8_t Message_Digest[SHA1_HASHSIZE]) {
  250. int i;
  251. if (!context || !Message_Digest) {
  252. return (SHA_NULL);
  253. }
  254. if (context->Corrupted) {
  255. return (context->Corrupted);
  256. }
  257. if (!context->Computed) {
  258. SHA1Finalize(context, 0x80);
  259. }
  260. for(i = 0; i < SHA1_HASHSIZE; ++i) {
  261. Message_Digest[i] = context->Intermediate_Hash[i>>2]
  262. >> 8 * (3 - (i & 0x03));
  263. }
  264. return (SHA_SUCCESS);
  265. }
  266. /*
  267. * SHA1Finalize
  268. *
  269. * Description:
  270. * This helper function finishes off the digest calculations.
  271. *
  272. * Parameters:
  273. * context: [in/out]
  274. * The SHA context to update
  275. * Pad_Byte: [in]
  276. * The last byte to add to the digest before the 0-padding
  277. * and length. This will contain the last bits of the message
  278. * followed by another single bit. If the message was an
  279. * exact multiple of 8-bits long, Pad_Byte will be 0x80.
  280. *
  281. * Returns:
  282. * sha Error Code.
  283. *
  284. */
  285. static void SHA1Finalize(SHA1Context *context, uint8_t Pad_Byte) {
  286. int i;
  287. SHA1PadMessage(context, Pad_Byte);
  288. /* message may be sensitive, clear it out */
  289. for (i = 0; i < SHA1_BLOCKSIZE; ++i)
  290. context->Message_Block[i] = 0;
  291. context->Length_Low = 0; /* and clear length */
  292. context->Length_High = 0;
  293. context->Computed = 1;
  294. }
  295. /*
  296. * SHA1PadMessage
  297. *
  298. * Description:
  299. * According to the standard, the message must be padded to an even
  300. * 512 bits. The first padding bit must be a '1'. The last 64
  301. * bits represent the length of the original message. All bits in
  302. * between should be 0. This function will pad the message
  303. * according to those rules by filling the Message_Block array
  304. * accordingly. It will also call the ProcessMessageBlock function
  305. * provided appropriately. When it returns, it can be assumed that
  306. * the message digest has been computed.
  307. *
  308. * Parameters:
  309. * context: [in/out]
  310. * The context to pad
  311. * Pad_Byte: [in]
  312. * The last byte to add to the digest before the 0-padding
  313. * and length. This will contain the last bits of the message
  314. * followed by another single bit. If the message was an
  315. * exact multiple of 8-bits long, Pad_Byte will be 0x80.
  316. *
  317. * Returns:
  318. * Nothing.
  319. *
  320. */
  321. static void SHA1PadMessage(SHA1Context *context, uint8_t Pad_Byte) {
  322. /*
  323. * Check to see if the current message block is too small to hold
  324. * the initial padding bits and length. If so, we will pad the
  325. * block, process it, and then continue padding into a second
  326. * block.
  327. */
  328. if (context->Message_Block_Index >= (SHA1_BLOCKSIZE - 8)) {
  329. context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
  330. while (context->Message_Block_Index < SHA1_BLOCKSIZE) {
  331. context->Message_Block[context->Message_Block_Index++] = 0;
  332. }
  333. SHA1ProcessMessageBlock(context);
  334. } else
  335. context->Message_Block[context->Message_Block_Index++] = Pad_Byte;
  336. while (context->Message_Block_Index < (SHA1_BLOCKSIZE - 8))
  337. context->Message_Block[context->Message_Block_Index++] = 0;
  338. /*
  339. * Store the message length as the last 8 octets
  340. */
  341. context->Message_Block[56] = (uint8_t) (context->Length_High >> 24);
  342. context->Message_Block[57] = (uint8_t) (context->Length_High >> 16);
  343. context->Message_Block[58] = (uint8_t) (context->Length_High >> 8);
  344. context->Message_Block[59] = (uint8_t) (context->Length_High);
  345. context->Message_Block[60] = (uint8_t) (context->Length_Low >> 24);
  346. context->Message_Block[61] = (uint8_t) (context->Length_Low >> 16);
  347. context->Message_Block[62] = (uint8_t) (context->Length_Low >> 8);
  348. context->Message_Block[63] = (uint8_t) (context->Length_Low);
  349. SHA1ProcessMessageBlock(context);
  350. }
  351. /*
  352. * SHA1ProcessMessageBlock
  353. *
  354. * Description:
  355. * This helper function will process the next 512 bits of the
  356. * message stored in the Message_Block array.
  357. *
  358. * Parameters:
  359. * None.
  360. *
  361. * Returns:
  362. * Nothing.
  363. *
  364. * Comments:
  365. * Many of the variable names in this code, especially the
  366. * single character names, were used because those were the
  367. * names used in the publication.
  368. *
  369. *
  370. */
  371. static void
  372. SHA1ProcessMessageBlock(SHA1Context *context) {
  373. /* Constants defined in FIPS-180-2, section 4.2.1 */
  374. const uint32_t K[] = {
  375. 0x5A827999,
  376. 0x6ED9EBA1,
  377. 0x8F1BBCDC,
  378. 0xCA62C1D6
  379. };
  380. int t; /* Loop counter */
  381. uint32_t temp; /* Temporary word value */
  382. uint32_t W[80]; /* Word sequence */
  383. uint32_t A, B, C, D, E; /* Word buffers */
  384. /*
  385. * Initialize the first 16 words in the array W
  386. */
  387. for (t = 0; t < 16; t++) {
  388. W[t] = ((uint32_t)context->Message_Block[t * 4]) << 24;
  389. W[t] |= ((uint32_t)context->Message_Block[t * 4 + 1]) << 16;
  390. W[t] |= ((uint32_t)context->Message_Block[t * 4 + 2]) << 8;
  391. W[t] |= ((uint32_t)context->Message_Block[t * 4 + 3]);
  392. }
  393. for (t = 16; t < 80; t++) {
  394. W[t] = SHA1CircularShift(1, W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  395. }
  396. A = context->Intermediate_Hash[0];
  397. B = context->Intermediate_Hash[1];
  398. C = context->Intermediate_Hash[2];
  399. D = context->Intermediate_Hash[3];
  400. E = context->Intermediate_Hash[4];
  401. for (t = 0; t < 20; t++) {
  402. temp = SHA1CircularShift(5,A) + SHA_Ch(B, C, D) + E + W[t] + K[0];
  403. E = D;
  404. D = C;
  405. C = SHA1CircularShift(30,B);
  406. B = A;
  407. A = temp;
  408. }
  409. for (t = 20; t < 40; t++) {
  410. temp = SHA1CircularShift(5,A) + SHA_Parity(B, C, D) + E + W[t] + K[1];
  411. E = D;
  412. D = C;
  413. C = SHA1CircularShift(30,B);
  414. B = A;
  415. A = temp;
  416. }
  417. for (t = 40; t < 60; t++) {
  418. temp = SHA1CircularShift(5,A) + SHA_Maj(B, C, D) + E + W[t] + K[2];
  419. E = D;
  420. D = C;
  421. C = SHA1CircularShift(30,B);
  422. B = A;
  423. A = temp;
  424. }
  425. for (t = 60; t < 80; t++) {
  426. temp = SHA1CircularShift(5,A) + SHA_Parity(B, C, D) + E + W[t] + K[3];
  427. E = D;
  428. D = C;
  429. C = SHA1CircularShift(30,B);
  430. B = A;
  431. A = temp;
  432. }
  433. context->Intermediate_Hash[0] += A;
  434. context->Intermediate_Hash[1] += B;
  435. context->Intermediate_Hash[2] += C;
  436. context->Intermediate_Hash[3] += D;
  437. context->Intermediate_Hash[4] += E;
  438. context->Message_Block_Index = 0;
  439. }
  440. } // namespace hash
  441. } // namespace util
  442. } // namespace isc