2
0

sha1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * sha1.c
  3. *
  4. * an implementation of the Secure Hash Algorithm v.1 (SHA-1),
  5. * specified in FIPS 180-1
  6. *
  7. * David A. McGrew
  8. * Cisco Systems, Inc.
  9. */
  10. /*
  11. *
  12. * Copyright (c) 2001-2006, Cisco Systems, Inc.
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions
  17. * are met:
  18. *
  19. * Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * Neither the name of the Cisco Systems, Inc. nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  34. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  35. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  36. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  37. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  38. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  42. * OF THE POSSIBILITY OF SUCH DAMAGE.
  43. *
  44. */
  45. #include "sha1.h"
  46. debug_module_t mod_sha1 = {
  47. 0, /* debugging is off by default */
  48. "sha-1" /* printable module name */
  49. };
  50. /* SN == Rotate left N bits */
  51. #define S1(X) ((X << 1) | (X >> 31))
  52. #define S5(X) ((X << 5) | (X >> 27))
  53. #define S30(X) ((X << 30) | (X >> 2))
  54. #define f0(B,C,D) ((B & C) | (~B & D))
  55. #define f1(B,C,D) (B ^ C ^ D)
  56. #define f2(B,C,D) ((B & C) | (B & D) | (C & D))
  57. #define f3(B,C,D) (B ^ C ^ D)
  58. /*
  59. * nota bene: the variable K0 appears in the curses library, so we
  60. * give longer names to these variables to avoid spurious warnings
  61. * on systems that uses curses
  62. */
  63. uint32_t SHA_K0 = 0x5A827999; /* Kt for 0 <= t <= 19 */
  64. uint32_t SHA_K1 = 0x6ED9EBA1; /* Kt for 20 <= t <= 39 */
  65. uint32_t SHA_K2 = 0x8F1BBCDC; /* Kt for 40 <= t <= 59 */
  66. uint32_t SHA_K3 = 0xCA62C1D6; /* Kt for 60 <= t <= 79 */
  67. void
  68. sha1(const uint8_t *msg, int octets_in_msg, uint32_t hash_value[5]) {
  69. sha1_ctx_t ctx;
  70. sha1_init(&ctx);
  71. sha1_update(&ctx, msg, octets_in_msg);
  72. sha1_final(&ctx, hash_value);
  73. }
  74. /*
  75. * sha1_core(M, H) computes the core compression function, where M is
  76. * the next part of the message (in network byte order) and H is the
  77. * intermediate state { H0, H1, ...} (in host byte order)
  78. *
  79. * this function does not do any of the padding required in the
  80. * complete SHA1 function
  81. *
  82. * this function is used in the SEAL 3.0 key setup routines
  83. * (crypto/cipher/seal.c)
  84. */
  85. void
  86. sha1_core(const uint32_t M[16], uint32_t hash_value[5]) {
  87. uint32_t H0;
  88. uint32_t H1;
  89. uint32_t H2;
  90. uint32_t H3;
  91. uint32_t H4;
  92. uint32_t W[80];
  93. uint32_t A, B, C, D, E, TEMP;
  94. int t;
  95. /* copy hash_value into H0, H1, H2, H3, H4 */
  96. H0 = hash_value[0];
  97. H1 = hash_value[1];
  98. H2 = hash_value[2];
  99. H3 = hash_value[3];
  100. H4 = hash_value[4];
  101. /* copy/xor message into array */
  102. W[0] = be32_to_cpu(M[0]);
  103. W[1] = be32_to_cpu(M[1]);
  104. W[2] = be32_to_cpu(M[2]);
  105. W[3] = be32_to_cpu(M[3]);
  106. W[4] = be32_to_cpu(M[4]);
  107. W[5] = be32_to_cpu(M[5]);
  108. W[6] = be32_to_cpu(M[6]);
  109. W[7] = be32_to_cpu(M[7]);
  110. W[8] = be32_to_cpu(M[8]);
  111. W[9] = be32_to_cpu(M[9]);
  112. W[10] = be32_to_cpu(M[10]);
  113. W[11] = be32_to_cpu(M[11]);
  114. W[12] = be32_to_cpu(M[12]);
  115. W[13] = be32_to_cpu(M[13]);
  116. W[14] = be32_to_cpu(M[14]);
  117. W[15] = be32_to_cpu(M[15]);
  118. TEMP = W[13] ^ W[8] ^ W[2] ^ W[0]; W[16] = S1(TEMP);
  119. TEMP = W[14] ^ W[9] ^ W[3] ^ W[1]; W[17] = S1(TEMP);
  120. TEMP = W[15] ^ W[10] ^ W[4] ^ W[2]; W[18] = S1(TEMP);
  121. TEMP = W[16] ^ W[11] ^ W[5] ^ W[3]; W[19] = S1(TEMP);
  122. TEMP = W[17] ^ W[12] ^ W[6] ^ W[4]; W[20] = S1(TEMP);
  123. TEMP = W[18] ^ W[13] ^ W[7] ^ W[5]; W[21] = S1(TEMP);
  124. TEMP = W[19] ^ W[14] ^ W[8] ^ W[6]; W[22] = S1(TEMP);
  125. TEMP = W[20] ^ W[15] ^ W[9] ^ W[7]; W[23] = S1(TEMP);
  126. TEMP = W[21] ^ W[16] ^ W[10] ^ W[8]; W[24] = S1(TEMP);
  127. TEMP = W[22] ^ W[17] ^ W[11] ^ W[9]; W[25] = S1(TEMP);
  128. TEMP = W[23] ^ W[18] ^ W[12] ^ W[10]; W[26] = S1(TEMP);
  129. TEMP = W[24] ^ W[19] ^ W[13] ^ W[11]; W[27] = S1(TEMP);
  130. TEMP = W[25] ^ W[20] ^ W[14] ^ W[12]; W[28] = S1(TEMP);
  131. TEMP = W[26] ^ W[21] ^ W[15] ^ W[13]; W[29] = S1(TEMP);
  132. TEMP = W[27] ^ W[22] ^ W[16] ^ W[14]; W[30] = S1(TEMP);
  133. TEMP = W[28] ^ W[23] ^ W[17] ^ W[15]; W[31] = S1(TEMP);
  134. /* process the remainder of the array */
  135. for (t=32; t < 80; t++) {
  136. TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
  137. W[t] = S1(TEMP);
  138. }
  139. A = H0; B = H1; C = H2; D = H3; E = H4;
  140. for (t=0; t < 20; t++) {
  141. TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
  142. E = D; D = C; C = S30(B); B = A; A = TEMP;
  143. }
  144. for ( ; t < 40; t++) {
  145. TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
  146. E = D; D = C; C = S30(B); B = A; A = TEMP;
  147. }
  148. for ( ; t < 60; t++) {
  149. TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
  150. E = D; D = C; C = S30(B); B = A; A = TEMP;
  151. }
  152. for ( ; t < 80; t++) {
  153. TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
  154. E = D; D = C; C = S30(B); B = A; A = TEMP;
  155. }
  156. hash_value[0] = H0 + A;
  157. hash_value[1] = H1 + B;
  158. hash_value[2] = H2 + C;
  159. hash_value[3] = H3 + D;
  160. hash_value[4] = H4 + E;
  161. return;
  162. }
  163. void
  164. sha1_init(sha1_ctx_t *ctx) {
  165. /* initialize state vector */
  166. ctx->H[0] = 0x67452301;
  167. ctx->H[1] = 0xefcdab89;
  168. ctx->H[2] = 0x98badcfe;
  169. ctx->H[3] = 0x10325476;
  170. ctx->H[4] = 0xc3d2e1f0;
  171. /* indicate that message buffer is empty */
  172. ctx->octets_in_buffer = 0;
  173. /* reset message bit-count to zero */
  174. ctx->num_bits_in_msg = 0;
  175. }
  176. void
  177. sha1_update(sha1_ctx_t *ctx, const uint8_t *msg, int octets_in_msg) {
  178. int i;
  179. uint8_t *buf = (uint8_t *)ctx->M;
  180. /* update message bit-count */
  181. ctx->num_bits_in_msg += octets_in_msg * 8;
  182. /* loop over 16-word blocks of M */
  183. while (octets_in_msg > 0) {
  184. if (octets_in_msg + ctx->octets_in_buffer >= 64) {
  185. /*
  186. * copy words of M into msg buffer until that buffer is full,
  187. * converting them into host byte order as needed
  188. */
  189. octets_in_msg -= (64 - ctx->octets_in_buffer);
  190. for (i=ctx->octets_in_buffer; i < 64; i++)
  191. buf[i] = *msg++;
  192. ctx->octets_in_buffer = 0;
  193. /* process a whole block */
  194. debug_print(mod_sha1, "(update) running sha1_core()", NULL);
  195. sha1_core(ctx->M, ctx->H);
  196. } else {
  197. debug_print(mod_sha1, "(update) not running sha1_core()", NULL);
  198. for (i=ctx->octets_in_buffer;
  199. i < (ctx->octets_in_buffer + octets_in_msg); i++)
  200. buf[i] = *msg++;
  201. ctx->octets_in_buffer += octets_in_msg;
  202. octets_in_msg = 0;
  203. }
  204. }
  205. }
  206. /*
  207. * sha1_final(ctx, output) computes the result for ctx and copies it
  208. * into the twenty octets located at *output
  209. */
  210. void
  211. sha1_final(sha1_ctx_t *ctx, uint32_t *output) {
  212. uint32_t A, B, C, D, E, TEMP;
  213. uint32_t W[80];
  214. int i, t;
  215. /*
  216. * process the remaining octets_in_buffer, padding and terminating as
  217. * necessary
  218. */
  219. {
  220. int tail = ctx->octets_in_buffer % 4;
  221. /* copy/xor message into array */
  222. for (i=0; i < (ctx->octets_in_buffer+3)/4; i++)
  223. W[i] = be32_to_cpu(ctx->M[i]);
  224. /* set the high bit of the octet immediately following the message */
  225. switch (tail) {
  226. case (3):
  227. W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffffff00) | 0x80;
  228. W[i] = 0x0;
  229. break;
  230. case (2):
  231. W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xffff0000) | 0x8000;
  232. W[i] = 0x0;
  233. break;
  234. case (1):
  235. W[i-1] = (be32_to_cpu(ctx->M[i-1]) & 0xff000000) | 0x800000;
  236. W[i] = 0x0;
  237. break;
  238. case (0):
  239. W[i] = 0x80000000;
  240. break;
  241. }
  242. /* zeroize remaining words */
  243. for (i++ ; i < 15; i++)
  244. W[i] = 0x0;
  245. /*
  246. * if there is room at the end of the word array, then set the
  247. * last word to the bit-length of the message; otherwise, set that
  248. * word to zero and then we need to do one more run of the
  249. * compression algo.
  250. */
  251. if (ctx->octets_in_buffer < 56)
  252. W[15] = ctx->num_bits_in_msg;
  253. else if (ctx->octets_in_buffer < 60)
  254. W[15] = 0x0;
  255. /* process the word array */
  256. for (t=16; t < 80; t++) {
  257. TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
  258. W[t] = S1(TEMP);
  259. }
  260. A = ctx->H[0];
  261. B = ctx->H[1];
  262. C = ctx->H[2];
  263. D = ctx->H[3];
  264. E = ctx->H[4];
  265. for (t=0; t < 20; t++) {
  266. TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
  267. E = D; D = C; C = S30(B); B = A; A = TEMP;
  268. }
  269. for ( ; t < 40; t++) {
  270. TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
  271. E = D; D = C; C = S30(B); B = A; A = TEMP;
  272. }
  273. for ( ; t < 60; t++) {
  274. TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
  275. E = D; D = C; C = S30(B); B = A; A = TEMP;
  276. }
  277. for ( ; t < 80; t++) {
  278. TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
  279. E = D; D = C; C = S30(B); B = A; A = TEMP;
  280. }
  281. ctx->H[0] += A;
  282. ctx->H[1] += B;
  283. ctx->H[2] += C;
  284. ctx->H[3] += D;
  285. ctx->H[4] += E;
  286. }
  287. debug_print(mod_sha1, "(final) running sha1_core()", NULL);
  288. if (ctx->octets_in_buffer >= 56) {
  289. debug_print(mod_sha1, "(final) running sha1_core() again", NULL);
  290. /* we need to do one final run of the compression algo */
  291. /*
  292. * set initial part of word array to zeros, and set the
  293. * final part to the number of bits in the message
  294. */
  295. for (i=0; i < 15; i++)
  296. W[i] = 0x0;
  297. W[15] = ctx->num_bits_in_msg;
  298. /* process the word array */
  299. for (t=16; t < 80; t++) {
  300. TEMP = W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16];
  301. W[t] = S1(TEMP);
  302. }
  303. A = ctx->H[0];
  304. B = ctx->H[1];
  305. C = ctx->H[2];
  306. D = ctx->H[3];
  307. E = ctx->H[4];
  308. for (t=0; t < 20; t++) {
  309. TEMP = S5(A) + f0(B,C,D) + E + W[t] + SHA_K0;
  310. E = D; D = C; C = S30(B); B = A; A = TEMP;
  311. }
  312. for ( ; t < 40; t++) {
  313. TEMP = S5(A) + f1(B,C,D) + E + W[t] + SHA_K1;
  314. E = D; D = C; C = S30(B); B = A; A = TEMP;
  315. }
  316. for ( ; t < 60; t++) {
  317. TEMP = S5(A) + f2(B,C,D) + E + W[t] + SHA_K2;
  318. E = D; D = C; C = S30(B); B = A; A = TEMP;
  319. }
  320. for ( ; t < 80; t++) {
  321. TEMP = S5(A) + f3(B,C,D) + E + W[t] + SHA_K3;
  322. E = D; D = C; C = S30(B); B = A; A = TEMP;
  323. }
  324. ctx->H[0] += A;
  325. ctx->H[1] += B;
  326. ctx->H[2] += C;
  327. ctx->H[3] += D;
  328. ctx->H[4] += E;
  329. }
  330. /* copy result into output buffer */
  331. output[0] = be32_to_cpu(ctx->H[0]);
  332. output[1] = be32_to_cpu(ctx->H[1]);
  333. output[2] = be32_to_cpu(ctx->H[2]);
  334. output[3] = be32_to_cpu(ctx->H[3]);
  335. output[4] = be32_to_cpu(ctx->H[4]);
  336. /* indicate that message buffer in context is empty */
  337. ctx->octets_in_buffer = 0;
  338. return;
  339. }