2
0

sha1.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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-2017, 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. #ifdef HAVE_CONFIG_H
  46. #include <config.h>
  47. #endif
  48. #include "sha1.h"
  49. srtp_debug_module_t srtp_mod_sha1 = {
  50. 0, /* debugging is off by default */
  51. "sha-1" /* printable module name */
  52. };
  53. /* SN == Rotate left N bits */
  54. #define S1(X) ((X << 1) | (X >> 31))
  55. #define S5(X) ((X << 5) | (X >> 27))
  56. #define S30(X) ((X << 30) | (X >> 2))
  57. #define f0(B, C, D) ((B & C) | (~B & D))
  58. #define f1(B, C, D) (B ^ C ^ D)
  59. #define f2(B, C, D) ((B & C) | (B & D) | (C & D))
  60. #define f3(B, C, D) (B ^ C ^ D)
  61. /*
  62. * nota bene: the variable K0 appears in the curses library, so we
  63. * give longer names to these variables to avoid spurious warnings
  64. * on systems that uses curses
  65. */
  66. uint32_t SHA_K0 = 0x5A827999; /* Kt for 0 <= t <= 19 */
  67. uint32_t SHA_K1 = 0x6ED9EBA1; /* Kt for 20 <= t <= 39 */
  68. uint32_t SHA_K2 = 0x8F1BBCDC; /* Kt for 40 <= t <= 59 */
  69. uint32_t SHA_K3 = 0xCA62C1D6; /* Kt for 60 <= t <= 79 */
  70. void srtp_sha1(const uint8_t *msg, int octets_in_msg, uint32_t hash_value[5])
  71. {
  72. srtp_sha1_ctx_t ctx;
  73. srtp_sha1_init(&ctx);
  74. srtp_sha1_update(&ctx, msg, octets_in_msg);
  75. srtp_sha1_final(&ctx, hash_value);
  76. }
  77. /*
  78. * srtp_sha1_core(M, H) computes the core compression function, where M is
  79. * the next part of the message (in network byte order) and H is the
  80. * intermediate state { H0, H1, ...} (in host byte order)
  81. *
  82. * this function does not do any of the padding required in the
  83. * complete SHA1 function
  84. *
  85. * this function is used in the SEAL 3.0 key setup routines
  86. * (crypto/cipher/seal.c)
  87. */
  88. void srtp_sha1_core(const uint32_t M[16], uint32_t hash_value[5])
  89. {
  90. uint32_t H0;
  91. uint32_t H1;
  92. uint32_t H2;
  93. uint32_t H3;
  94. uint32_t H4;
  95. uint32_t W[80];
  96. uint32_t A, B, C, D, E, TEMP;
  97. int t;
  98. /* copy hash_value into H0, H1, H2, H3, H4 */
  99. H0 = hash_value[0];
  100. H1 = hash_value[1];
  101. H2 = hash_value[2];
  102. H3 = hash_value[3];
  103. H4 = hash_value[4];
  104. /* copy/xor message into array */
  105. W[0] = be32_to_cpu(M[0]);
  106. W[1] = be32_to_cpu(M[1]);
  107. W[2] = be32_to_cpu(M[2]);
  108. W[3] = be32_to_cpu(M[3]);
  109. W[4] = be32_to_cpu(M[4]);
  110. W[5] = be32_to_cpu(M[5]);
  111. W[6] = be32_to_cpu(M[6]);
  112. W[7] = be32_to_cpu(M[7]);
  113. W[8] = be32_to_cpu(M[8]);
  114. W[9] = be32_to_cpu(M[9]);
  115. W[10] = be32_to_cpu(M[10]);
  116. W[11] = be32_to_cpu(M[11]);
  117. W[12] = be32_to_cpu(M[12]);
  118. W[13] = be32_to_cpu(M[13]);
  119. W[14] = be32_to_cpu(M[14]);
  120. W[15] = be32_to_cpu(M[15]);
  121. TEMP = W[13] ^ W[8] ^ W[2] ^ W[0];
  122. W[16] = S1(TEMP);
  123. TEMP = W[14] ^ W[9] ^ W[3] ^ W[1];
  124. W[17] = S1(TEMP);
  125. TEMP = W[15] ^ W[10] ^ W[4] ^ W[2];
  126. W[18] = S1(TEMP);
  127. TEMP = W[16] ^ W[11] ^ W[5] ^ W[3];
  128. W[19] = S1(TEMP);
  129. TEMP = W[17] ^ W[12] ^ W[6] ^ W[4];
  130. W[20] = S1(TEMP);
  131. TEMP = W[18] ^ W[13] ^ W[7] ^ W[5];
  132. W[21] = S1(TEMP);
  133. TEMP = W[19] ^ W[14] ^ W[8] ^ W[6];
  134. W[22] = S1(TEMP);
  135. TEMP = W[20] ^ W[15] ^ W[9] ^ W[7];
  136. W[23] = S1(TEMP);
  137. TEMP = W[21] ^ W[16] ^ W[10] ^ W[8];
  138. W[24] = S1(TEMP);
  139. TEMP = W[22] ^ W[17] ^ W[11] ^ W[9];
  140. W[25] = S1(TEMP);
  141. TEMP = W[23] ^ W[18] ^ W[12] ^ W[10];
  142. W[26] = S1(TEMP);
  143. TEMP = W[24] ^ W[19] ^ W[13] ^ W[11];
  144. W[27] = S1(TEMP);
  145. TEMP = W[25] ^ W[20] ^ W[14] ^ W[12];
  146. W[28] = S1(TEMP);
  147. TEMP = W[26] ^ W[21] ^ W[15] ^ W[13];
  148. W[29] = S1(TEMP);
  149. TEMP = W[27] ^ W[22] ^ W[16] ^ W[14];
  150. W[30] = S1(TEMP);
  151. TEMP = W[28] ^ W[23] ^ W[17] ^ W[15];
  152. W[31] = S1(TEMP);
  153. /* process the remainder of the array */
  154. for (t = 32; t < 80; t++) {
  155. TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
  156. W[t] = S1(TEMP);
  157. }
  158. A = H0;
  159. B = H1;
  160. C = H2;
  161. D = H3;
  162. E = H4;
  163. for (t = 0; t < 20; t++) {
  164. TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
  165. E = D;
  166. D = C;
  167. C = S30(B);
  168. B = A;
  169. A = TEMP;
  170. }
  171. for (; t < 40; t++) {
  172. TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
  173. E = D;
  174. D = C;
  175. C = S30(B);
  176. B = A;
  177. A = TEMP;
  178. }
  179. for (; t < 60; t++) {
  180. TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
  181. E = D;
  182. D = C;
  183. C = S30(B);
  184. B = A;
  185. A = TEMP;
  186. }
  187. for (; t < 80; t++) {
  188. TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
  189. E = D;
  190. D = C;
  191. C = S30(B);
  192. B = A;
  193. A = TEMP;
  194. }
  195. hash_value[0] = H0 + A;
  196. hash_value[1] = H1 + B;
  197. hash_value[2] = H2 + C;
  198. hash_value[3] = H3 + D;
  199. hash_value[4] = H4 + E;
  200. return;
  201. }
  202. void srtp_sha1_init(srtp_sha1_ctx_t *ctx)
  203. {
  204. /* initialize state vector */
  205. ctx->H[0] = 0x67452301;
  206. ctx->H[1] = 0xefcdab89;
  207. ctx->H[2] = 0x98badcfe;
  208. ctx->H[3] = 0x10325476;
  209. ctx->H[4] = 0xc3d2e1f0;
  210. /* indicate that message buffer is empty */
  211. ctx->octets_in_buffer = 0;
  212. /* reset message bit-count to zero */
  213. ctx->num_bits_in_msg = 0;
  214. }
  215. void srtp_sha1_update(srtp_sha1_ctx_t *ctx,
  216. const uint8_t *msg,
  217. int octets_in_msg)
  218. {
  219. int i;
  220. uint8_t *buf = (uint8_t *)ctx->M;
  221. /* update message bit-count */
  222. ctx->num_bits_in_msg += octets_in_msg * 8;
  223. /* loop over 16-word blocks of M */
  224. while (octets_in_msg > 0) {
  225. if (octets_in_msg + ctx->octets_in_buffer >= 64) {
  226. /*
  227. * copy words of M into msg buffer until that buffer is full,
  228. * converting them into host byte order as needed
  229. */
  230. octets_in_msg -= (64 - ctx->octets_in_buffer);
  231. for (i = ctx->octets_in_buffer; i < 64; i++) {
  232. buf[i] = *msg++;
  233. }
  234. ctx->octets_in_buffer = 0;
  235. /* process a whole block */
  236. debug_print0(srtp_mod_sha1, "(update) running srtp_sha1_core()");
  237. srtp_sha1_core(ctx->M, ctx->H);
  238. } else {
  239. debug_print0(srtp_mod_sha1,
  240. "(update) not running srtp_sha1_core()");
  241. for (i = ctx->octets_in_buffer;
  242. i < (ctx->octets_in_buffer + octets_in_msg); i++) {
  243. buf[i] = *msg++;
  244. }
  245. ctx->octets_in_buffer += octets_in_msg;
  246. octets_in_msg = 0;
  247. }
  248. }
  249. }
  250. /*
  251. * srtp_sha1_final(ctx, output) computes the result for ctx and copies it
  252. * into the twenty octets located at *output
  253. */
  254. void srtp_sha1_final(srtp_sha1_ctx_t *ctx, uint32_t *output)
  255. {
  256. uint32_t A, B, C, D, E, TEMP;
  257. uint32_t W[80];
  258. int i, t;
  259. /*
  260. * process the remaining octets_in_buffer, padding and terminating as
  261. * necessary
  262. */
  263. {
  264. int tail = ctx->octets_in_buffer % 4;
  265. /* copy/xor message into array */
  266. for (i = 0; i < (ctx->octets_in_buffer + 3) / 4; i++) {
  267. W[i] = be32_to_cpu(ctx->M[i]);
  268. }
  269. /* set the high bit of the octet immediately following the message */
  270. switch (tail) {
  271. case (3):
  272. W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffffff00) | 0x80;
  273. W[i] = 0x0;
  274. break;
  275. case (2):
  276. W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xffff0000) | 0x8000;
  277. W[i] = 0x0;
  278. break;
  279. case (1):
  280. W[i - 1] = (be32_to_cpu(ctx->M[i - 1]) & 0xff000000) | 0x800000;
  281. W[i] = 0x0;
  282. break;
  283. case (0):
  284. W[i] = 0x80000000;
  285. break;
  286. }
  287. /* zeroize remaining words */
  288. for (i++; i < 15; i++) {
  289. W[i] = 0x0;
  290. }
  291. /*
  292. * if there is room at the end of the word array, then set the
  293. * last word to the bit-length of the message; otherwise, set that
  294. * word to zero and then we need to do one more run of the
  295. * compression algo.
  296. */
  297. if (ctx->octets_in_buffer < 56) {
  298. W[15] = ctx->num_bits_in_msg;
  299. } else if (ctx->octets_in_buffer < 60) {
  300. W[15] = 0x0;
  301. }
  302. /* process the word array */
  303. for (t = 16; t < 80; t++) {
  304. TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
  305. W[t] = S1(TEMP);
  306. }
  307. A = ctx->H[0];
  308. B = ctx->H[1];
  309. C = ctx->H[2];
  310. D = ctx->H[3];
  311. E = ctx->H[4];
  312. for (t = 0; t < 20; t++) {
  313. TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
  314. E = D;
  315. D = C;
  316. C = S30(B);
  317. B = A;
  318. A = TEMP;
  319. }
  320. for (; t < 40; t++) {
  321. TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
  322. E = D;
  323. D = C;
  324. C = S30(B);
  325. B = A;
  326. A = TEMP;
  327. }
  328. for (; t < 60; t++) {
  329. TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
  330. E = D;
  331. D = C;
  332. C = S30(B);
  333. B = A;
  334. A = TEMP;
  335. }
  336. for (; t < 80; t++) {
  337. TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
  338. E = D;
  339. D = C;
  340. C = S30(B);
  341. B = A;
  342. A = TEMP;
  343. }
  344. ctx->H[0] += A;
  345. ctx->H[1] += B;
  346. ctx->H[2] += C;
  347. ctx->H[3] += D;
  348. ctx->H[4] += E;
  349. }
  350. debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core()");
  351. if (ctx->octets_in_buffer >= 56) {
  352. debug_print0(srtp_mod_sha1, "(final) running srtp_sha1_core() again");
  353. /* we need to do one final run of the compression algo */
  354. /*
  355. * set initial part of word array to zeros, and set the
  356. * final part to the number of bits in the message
  357. */
  358. for (i = 0; i < 15; i++) {
  359. W[i] = 0x0;
  360. }
  361. W[15] = ctx->num_bits_in_msg;
  362. /* process the word array */
  363. for (t = 16; t < 80; t++) {
  364. TEMP = W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16];
  365. W[t] = S1(TEMP);
  366. }
  367. A = ctx->H[0];
  368. B = ctx->H[1];
  369. C = ctx->H[2];
  370. D = ctx->H[3];
  371. E = ctx->H[4];
  372. for (t = 0; t < 20; t++) {
  373. TEMP = S5(A) + f0(B, C, D) + E + W[t] + SHA_K0;
  374. E = D;
  375. D = C;
  376. C = S30(B);
  377. B = A;
  378. A = TEMP;
  379. }
  380. for (; t < 40; t++) {
  381. TEMP = S5(A) + f1(B, C, D) + E + W[t] + SHA_K1;
  382. E = D;
  383. D = C;
  384. C = S30(B);
  385. B = A;
  386. A = TEMP;
  387. }
  388. for (; t < 60; t++) {
  389. TEMP = S5(A) + f2(B, C, D) + E + W[t] + SHA_K2;
  390. E = D;
  391. D = C;
  392. C = S30(B);
  393. B = A;
  394. A = TEMP;
  395. }
  396. for (; t < 80; t++) {
  397. TEMP = S5(A) + f3(B, C, D) + E + W[t] + SHA_K3;
  398. E = D;
  399. D = C;
  400. C = S30(B);
  401. B = A;
  402. A = TEMP;
  403. }
  404. ctx->H[0] += A;
  405. ctx->H[1] += B;
  406. ctx->H[2] += C;
  407. ctx->H[3] += D;
  408. ctx->H[4] += E;
  409. }
  410. /* copy result into output buffer */
  411. output[0] = be32_to_cpu(ctx->H[0]);
  412. output[1] = be32_to_cpu(ctx->H[1]);
  413. output[2] = be32_to_cpu(ctx->H[2]);
  414. output[3] = be32_to_cpu(ctx->H[3]);
  415. output[4] = be32_to_cpu(ctx->H[4]);
  416. /* indicate that message buffer in context is empty */
  417. ctx->octets_in_buffer = 0;
  418. return;
  419. }