sctp_sha1.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
  5. * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
  6. * Copyright (c) 2008-2013, by Michael Tuexen. All rights reserved.
  7. * Copyright (c) 2013, by Lally Singh. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * a) Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. *
  15. * b) Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the distribution.
  18. *
  19. * c) Neither the name of Cisco Systems, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  25. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <netinet/sctp_sha1.h>
  36. #if defined(SCTP_USE_NSS_SHA1)
  37. /* A SHA-1 Digest is 160 bits, or 20 bytes */
  38. #define SHA_DIGEST_LENGTH (20)
  39. void
  40. sctp_sha1_init(struct sctp_sha1_context *ctx)
  41. {
  42. ctx->pk11_ctx = PK11_CreateDigestContext(SEC_OID_SHA1);
  43. PK11_DigestBegin(ctx->pk11_ctx);
  44. }
  45. void
  46. sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
  47. {
  48. PK11_DigestOp(ctx->pk11_ctx, ptr, siz);
  49. }
  50. void
  51. sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
  52. {
  53. unsigned int output_len = 0;
  54. PK11_DigestFinal(ctx->pk11_ctx, digest, &output_len, SHA_DIGEST_LENGTH);
  55. PK11_DestroyContext(ctx->pk11_ctx, PR_TRUE);
  56. }
  57. #elif defined(SCTP_USE_OPENSSL_SHA1)
  58. void
  59. sctp_sha1_init(struct sctp_sha1_context *ctx)
  60. {
  61. SHA1_Init(&ctx->sha_ctx);
  62. }
  63. void
  64. sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
  65. {
  66. SHA1_Update(&ctx->sha_ctx, ptr, (unsigned long)siz);
  67. }
  68. void
  69. sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
  70. {
  71. SHA1_Final(digest, &ctx->sha_ctx);
  72. }
  73. #elif defined(SCTP_USE_MBEDTLS_SHA1)
  74. void
  75. sctp_sha1_init(struct sctp_sha1_context *ctx)
  76. {
  77. mbedtls_sha1_init(&ctx->sha1_ctx);
  78. mbedtls_sha1_starts_ret(&ctx->sha1_ctx);
  79. }
  80. void
  81. sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
  82. {
  83. mbedtls_sha1_update_ret(&ctx->sha1_ctx, ptr, siz);
  84. }
  85. void
  86. sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
  87. {
  88. mbedtls_sha1_finish_ret(&ctx->sha1_ctx, digest);
  89. }
  90. #else
  91. #include <string.h>
  92. #if defined(_WIN32) && defined(__Userspace__)
  93. #include <winsock2.h>
  94. #elif !(defined(_WIN32) && !defined(__Userspace__))
  95. #include <arpa/inet.h>
  96. #endif
  97. #define F1(B,C,D) (((B & C) | ((~B) & D))) /* 0 <= t <= 19 */
  98. #define F2(B,C,D) (B ^ C ^ D) /* 20 <= t <= 39 */
  99. #define F3(B,C,D) ((B & C) | (B & D) | (C & D)) /* 40 <= t <= 59 */
  100. #define F4(B,C,D) (B ^ C ^ D) /* 600 <= t <= 79 */
  101. /* circular shift */
  102. #define CSHIFT(A,B) ((B << A) | (B >> (32-A)))
  103. #define K1 0x5a827999 /* 0 <= t <= 19 */
  104. #define K2 0x6ed9eba1 /* 20 <= t <= 39 */
  105. #define K3 0x8f1bbcdc /* 40 <= t <= 59 */
  106. #define K4 0xca62c1d6 /* 60 <= t <= 79 */
  107. #define H0INIT 0x67452301
  108. #define H1INIT 0xefcdab89
  109. #define H2INIT 0x98badcfe
  110. #define H3INIT 0x10325476
  111. #define H4INIT 0xc3d2e1f0
  112. void
  113. sctp_sha1_init(struct sctp_sha1_context *ctx)
  114. {
  115. /* Init the SHA-1 context structure */
  116. ctx->A = 0;
  117. ctx->B = 0;
  118. ctx->C = 0;
  119. ctx->D = 0;
  120. ctx->E = 0;
  121. ctx->H0 = H0INIT;
  122. ctx->H1 = H1INIT;
  123. ctx->H2 = H2INIT;
  124. ctx->H3 = H3INIT;
  125. ctx->H4 = H4INIT;
  126. ctx->TEMP = 0;
  127. memset(ctx->words, 0, sizeof(ctx->words));
  128. ctx->how_many_in_block = 0;
  129. ctx->running_total = 0;
  130. }
  131. static void
  132. sctp_sha1_process_a_block(struct sctp_sha1_context *ctx, unsigned int *block)
  133. {
  134. int i;
  135. /* init the W0-W15 to the block of words being hashed. */
  136. /* step a) */
  137. for (i = 0; i < 16; i++) {
  138. ctx->words[i] = ntohl(block[i]);
  139. }
  140. /* now init the rest based on the SHA-1 formula, step b) */
  141. for (i = 16; i < 80; i++) {
  142. ctx->words[i] = CSHIFT(1, ((ctx->words[(i - 3)]) ^
  143. (ctx->words[(i - 8)]) ^
  144. (ctx->words[(i - 14)]) ^
  145. (ctx->words[(i - 16)])));
  146. }
  147. /* step c) */
  148. ctx->A = ctx->H0;
  149. ctx->B = ctx->H1;
  150. ctx->C = ctx->H2;
  151. ctx->D = ctx->H3;
  152. ctx->E = ctx->H4;
  153. /* step d) */
  154. for (i = 0; i < 80; i++) {
  155. if (i < 20) {
  156. ctx->TEMP = ((CSHIFT(5, ctx->A)) +
  157. (F1(ctx->B, ctx->C, ctx->D)) +
  158. (ctx->E) +
  159. ctx->words[i] +
  160. K1);
  161. } else if (i < 40) {
  162. ctx->TEMP = ((CSHIFT(5, ctx->A)) +
  163. (F2(ctx->B, ctx->C, ctx->D)) +
  164. (ctx->E) +
  165. (ctx->words[i]) +
  166. K2);
  167. } else if (i < 60) {
  168. ctx->TEMP = ((CSHIFT(5, ctx->A)) +
  169. (F3(ctx->B, ctx->C, ctx->D)) +
  170. (ctx->E) +
  171. (ctx->words[i]) +
  172. K3);
  173. } else {
  174. ctx->TEMP = ((CSHIFT(5, ctx->A)) +
  175. (F4(ctx->B, ctx->C, ctx->D)) +
  176. (ctx->E) +
  177. (ctx->words[i]) +
  178. K4);
  179. }
  180. ctx->E = ctx->D;
  181. ctx->D = ctx->C;
  182. ctx->C = CSHIFT(30, ctx->B);
  183. ctx->B = ctx->A;
  184. ctx->A = ctx->TEMP;
  185. }
  186. /* step e) */
  187. ctx->H0 = (ctx->H0) + (ctx->A);
  188. ctx->H1 = (ctx->H1) + (ctx->B);
  189. ctx->H2 = (ctx->H2) + (ctx->C);
  190. ctx->H3 = (ctx->H3) + (ctx->D);
  191. ctx->H4 = (ctx->H4) + (ctx->E);
  192. }
  193. void
  194. sctp_sha1_update(struct sctp_sha1_context *ctx, const unsigned char *ptr, unsigned int siz)
  195. {
  196. unsigned int number_left, left_to_fill;
  197. number_left = siz;
  198. while (number_left > 0) {
  199. left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block;
  200. if (left_to_fill > number_left) {
  201. /* can only partially fill up this one */
  202. memcpy(&ctx->sha_block[ctx->how_many_in_block],
  203. ptr, number_left);
  204. ctx->how_many_in_block += number_left;
  205. ctx->running_total += number_left;
  206. break;
  207. } else {
  208. /* block is now full, process it */
  209. memcpy(&ctx->sha_block[ctx->how_many_in_block],
  210. ptr, left_to_fill);
  211. sctp_sha1_process_a_block(ctx,
  212. (unsigned int *)ctx->sha_block);
  213. number_left -= left_to_fill;
  214. ctx->running_total += left_to_fill;
  215. ctx->how_many_in_block = 0;
  216. ptr = (const unsigned char *)(ptr + left_to_fill);
  217. }
  218. }
  219. }
  220. void
  221. sctp_sha1_final(unsigned char *digest, struct sctp_sha1_context *ctx)
  222. {
  223. /*
  224. * if any left in block fill with padding and process. Then transfer
  225. * the digest to the pointer. At the last block some special rules
  226. * need to apply. We must add a 1 bit following the message, then we
  227. * pad with 0's. The total size is encoded as a 64 bit number at the
  228. * end. Now if the last buffer has more than 55 octets in it we
  229. * cannot fit the 64 bit number + 10000000 pad on the end and must
  230. * add the 10000000 pad, pad the rest of the message with 0's and
  231. * then create an all 0 message with just the 64 bit size at the end
  232. * and run this block through by itself. Also the 64 bit int must
  233. * be in network byte order.
  234. */
  235. int left_to_fill;
  236. unsigned int i, *ptr;
  237. if (ctx->how_many_in_block > 55) {
  238. /*
  239. * special case, we need to process two blocks here. One for
  240. * the current stuff plus possibly the pad. The other for
  241. * the size.
  242. */
  243. left_to_fill = sizeof(ctx->sha_block) - ctx->how_many_in_block;
  244. if (left_to_fill == 0) {
  245. /* Should not really happen but I am paranoid */
  246. sctp_sha1_process_a_block(ctx,
  247. (unsigned int *)ctx->sha_block);
  248. /* init last block, a bit different than the rest */
  249. ctx->sha_block[0] = '\x80';
  250. for (i = 1; i < sizeof(ctx->sha_block); i++) {
  251. ctx->sha_block[i] = 0x0;
  252. }
  253. } else if (left_to_fill == 1) {
  254. ctx->sha_block[ctx->how_many_in_block] = '\x80';
  255. sctp_sha1_process_a_block(ctx,
  256. (unsigned int *)ctx->sha_block);
  257. /* init last block */
  258. memset(ctx->sha_block, 0, sizeof(ctx->sha_block));
  259. } else {
  260. ctx->sha_block[ctx->how_many_in_block] = '\x80';
  261. for (i = (ctx->how_many_in_block + 1);
  262. i < sizeof(ctx->sha_block);
  263. i++) {
  264. ctx->sha_block[i] = 0x0;
  265. }
  266. sctp_sha1_process_a_block(ctx,
  267. (unsigned int *)ctx->sha_block);
  268. /* init last block */
  269. memset(ctx->sha_block, 0, sizeof(ctx->sha_block));
  270. }
  271. /* This is in bits so multiply by 8 */
  272. ctx->running_total *= 8;
  273. ptr = (unsigned int *)&ctx->sha_block[60];
  274. *ptr = htonl(ctx->running_total);
  275. sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block);
  276. } else {
  277. /*
  278. * easy case, we just pad this message to size - end with 0
  279. * add the magic 0x80 to the next word and then put the
  280. * network byte order size in the last spot and process the
  281. * block.
  282. */
  283. ctx->sha_block[ctx->how_many_in_block] = '\x80';
  284. for (i = (ctx->how_many_in_block + 1);
  285. i < sizeof(ctx->sha_block);
  286. i++) {
  287. ctx->sha_block[i] = 0x0;
  288. }
  289. /* get last int spot */
  290. ctx->running_total *= 8;
  291. ptr = (unsigned int *)&ctx->sha_block[60];
  292. *ptr = htonl(ctx->running_total);
  293. sctp_sha1_process_a_block(ctx, (unsigned int *)ctx->sha_block);
  294. }
  295. /* transfer the digest back to the user */
  296. digest[3] = (ctx->H0 & 0xff);
  297. digest[2] = ((ctx->H0 >> 8) & 0xff);
  298. digest[1] = ((ctx->H0 >> 16) & 0xff);
  299. digest[0] = ((ctx->H0 >> 24) & 0xff);
  300. digest[7] = (ctx->H1 & 0xff);
  301. digest[6] = ((ctx->H1 >> 8) & 0xff);
  302. digest[5] = ((ctx->H1 >> 16) & 0xff);
  303. digest[4] = ((ctx->H1 >> 24) & 0xff);
  304. digest[11] = (ctx->H2 & 0xff);
  305. digest[10] = ((ctx->H2 >> 8) & 0xff);
  306. digest[9] = ((ctx->H2 >> 16) & 0xff);
  307. digest[8] = ((ctx->H2 >> 24) & 0xff);
  308. digest[15] = (ctx->H3 & 0xff);
  309. digest[14] = ((ctx->H3 >> 8) & 0xff);
  310. digest[13] = ((ctx->H3 >> 16) & 0xff);
  311. digest[12] = ((ctx->H3 >> 24) & 0xff);
  312. digest[19] = (ctx->H4 & 0xff);
  313. digest[18] = ((ctx->H4 >> 8) & 0xff);
  314. digest[17] = ((ctx->H4 >> 16) & 0xff);
  315. digest[16] = ((ctx->H4 >> 24) & 0xff);
  316. }
  317. #endif