Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* This file is copied from the libsrs2 sources */
  2. /* Modified by Timo Röhling <timo.roehling@gmx.de> */
  3. /* NIST Secure Hash Algorithm */
  4. /* Borrowed from SHA1.xs by Gisle Ass */
  5. /* heavily modified by Uwe Hollerbach <uh@alumni.caltech edu> */
  6. /* from Peter C. Gutmann's implementation as found in */
  7. /* Applied Cryptography by Bruce Schneier */
  8. /* Further modifications to include the "UNRAVEL" stuff, below */
  9. /* HMAC functions by Shevek <srs@anarres.org> for inclusion in
  10. * libsrs2, under GPL-2 or BSD license. Combine this lot in any way
  11. * you think will stand up in court. I hope my intent is clear. */
  12. /* This code is in the public domain */
  13. /* Useful defines & typedefs */
  14. #include <stdarg.h>
  15. #include <time.h> /* time */
  16. #include <sys/types.h> /* tyepdefs */
  17. #include <sys/time.h> /* timeval / timezone struct */
  18. #include <string.h> /* memcpy, strcpy, memset */
  19. #include "srs2.h"
  20. #ifdef SIZEOF_UNSIGNED_LONG
  21. #if SIZEOF_UNSIGNED_LONG < 4
  22. #error "SHA1 requires an unsigned long of at least 32 bits"
  23. #endif
  24. #endif
  25. #ifdef WORDS_BIGENDIAN
  26. #define BYTEORDER 0x4321
  27. #else
  28. #define BYTEORDER 0x1234
  29. #endif
  30. /* UNRAVEL should be fastest & biggest */
  31. /* UNROLL_LOOPS should be just as big, but slightly slower */
  32. /* both undefined should be smallest and slowest */
  33. #define SHA_VERSION 1
  34. #define UNRAVEL
  35. /* #define UNROLL_LOOPS */
  36. /* SHA f()-functions */
  37. #define f1(x,y,z) ((x & y) | (~x & z))
  38. #define f2(x,y,z) (x ^ y ^ z)
  39. #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
  40. #define f4(x,y,z) (x ^ y ^ z)
  41. /* SHA constants */
  42. #define CONST1 0x5a827999L
  43. #define CONST2 0x6ed9eba1L
  44. #define CONST3 0x8f1bbcdcL
  45. #define CONST4 0xca62c1d6L
  46. /* truncate to 32 bits -- should be a null op on 32-bit machines */
  47. #define T32(x) ((x) & 0xffffffffL)
  48. /* 32-bit rotate */
  49. #define R32(x,n) T32(((x << n) | (x >> (32 - n))))
  50. /* the generic case, for when the overall rotation is not unraveled */
  51. #define FG(n) \
  52. T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); \
  53. E = D; D = C; C = R32(B,30); B = A; A = T
  54. /* specific cases, for when the overall rotation is unraveled */
  55. #define FA(n) \
  56. T = T32(R32(A,5) + f##n(B,C,D) + E + *WP++ + CONST##n); B = R32(B,30)
  57. #define FB(n) \
  58. E = T32(R32(T,5) + f##n(A,B,C) + D + *WP++ + CONST##n); A = R32(A,30)
  59. #define FC(n) \
  60. D = T32(R32(E,5) + f##n(T,A,B) + C + *WP++ + CONST##n); T = R32(T,30)
  61. #define FD(n) \
  62. C = T32(R32(D,5) + f##n(E,T,A) + B + *WP++ + CONST##n); E = R32(E,30)
  63. #define FE(n) \
  64. B = T32(R32(C,5) + f##n(D,E,T) + A + *WP++ + CONST##n); D = R32(D,30)
  65. #define FT(n) \
  66. A = T32(R32(B,5) + f##n(C,D,E) + T + *WP++ + CONST##n); C = R32(C,30)
  67. static void sha_transform(SHA_INFO *sha_info)
  68. {
  69. int i;
  70. sha_byte *dp;
  71. ULONG T, A, B, C, D, E, W[80], *WP;
  72. dp = sha_info->data;
  73. /*
  74. the following makes sure that at least one code block below is
  75. traversed or an error is reported, without the necessity for nested
  76. preprocessor if/else/endif blocks, which are a great pain in the
  77. nether regions of the anatomy...
  78. */
  79. #undef SWAP_DONE
  80. #if BYTEORDER == 0x1234
  81. #define SWAP_DONE
  82. /* assert(sizeof(ULONG) == 4); */
  83. for (i = 0; i < 16; ++i) {
  84. T = *((ULONG *) dp);
  85. dp += 4;
  86. W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
  87. ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
  88. }
  89. #endif
  90. #if BYTEORDER == 0x4321
  91. #define SWAP_DONE
  92. /* assert(sizeof(ULONG) == 4); */
  93. for (i = 0; i < 16; ++i) {
  94. T = *((ULONG *) dp);
  95. dp += 4;
  96. W[i] = T32(T);
  97. }
  98. #endif
  99. #if BYTEORDER == 0x12345678
  100. #define SWAP_DONE
  101. /* assert(sizeof(ULONG) == 8); */
  102. for (i = 0; i < 16; i += 2) {
  103. T = *((ULONG *) dp);
  104. dp += 8;
  105. W[i] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
  106. ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
  107. T >>= 32;
  108. W[i+1] = ((T << 24) & 0xff000000) | ((T << 8) & 0x00ff0000) |
  109. ((T >> 8) & 0x0000ff00) | ((T >> 24) & 0x000000ff);
  110. }
  111. #endif
  112. #if BYTEORDER == 0x87654321
  113. #define SWAP_DONE
  114. /* assert(sizeof(ULONG) == 8); */
  115. for (i = 0; i < 16; i += 2) {
  116. T = *((ULONG *) dp);
  117. dp += 8;
  118. W[i] = T32(T >> 32);
  119. W[i+1] = T32(T);
  120. }
  121. #endif
  122. #ifndef SWAP_DONE
  123. #error Unknown byte order -- you need to add code here
  124. #endif /* SWAP_DONE */
  125. for (i = 16; i < 80; ++i) {
  126. W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
  127. #if (SHA_VERSION == 1)
  128. W[i] = R32(W[i], 1);
  129. #endif /* SHA_VERSION */
  130. }
  131. A = sha_info->digest[0];
  132. B = sha_info->digest[1];
  133. C = sha_info->digest[2];
  134. D = sha_info->digest[3];
  135. E = sha_info->digest[4];
  136. WP = W;
  137. #ifdef UNRAVEL
  138. FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1); FC(1); FD(1);
  139. FE(1); FT(1); FA(1); FB(1); FC(1); FD(1); FE(1); FT(1); FA(1); FB(1);
  140. FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2); FE(2); FT(2);
  141. FA(2); FB(2); FC(2); FD(2); FE(2); FT(2); FA(2); FB(2); FC(2); FD(2);
  142. FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3); FA(3); FB(3);
  143. FC(3); FD(3); FE(3); FT(3); FA(3); FB(3); FC(3); FD(3); FE(3); FT(3);
  144. FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4); FC(4); FD(4);
  145. FE(4); FT(4); FA(4); FB(4); FC(4); FD(4); FE(4); FT(4); FA(4); FB(4);
  146. sha_info->digest[0] = T32(sha_info->digest[0] + E);
  147. sha_info->digest[1] = T32(sha_info->digest[1] + T);
  148. sha_info->digest[2] = T32(sha_info->digest[2] + A);
  149. sha_info->digest[3] = T32(sha_info->digest[3] + B);
  150. sha_info->digest[4] = T32(sha_info->digest[4] + C);
  151. #else /* !UNRAVEL */
  152. #ifdef UNROLL_LOOPS
  153. FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
  154. FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1); FG(1);
  155. FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
  156. FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2); FG(2);
  157. FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
  158. FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3); FG(3);
  159. FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
  160. FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4); FG(4);
  161. #else /* !UNROLL_LOOPS */
  162. for (i = 0; i < 20; ++i) { FG(1); }
  163. for (i = 20; i < 40; ++i) { FG(2); }
  164. for (i = 40; i < 60; ++i) { FG(3); }
  165. for (i = 60; i < 80; ++i) { FG(4); }
  166. #endif /* !UNROLL_LOOPS */
  167. sha_info->digest[0] = T32(sha_info->digest[0] + A);
  168. sha_info->digest[1] = T32(sha_info->digest[1] + B);
  169. sha_info->digest[2] = T32(sha_info->digest[2] + C);
  170. sha_info->digest[3] = T32(sha_info->digest[3] + D);
  171. sha_info->digest[4] = T32(sha_info->digest[4] + E);
  172. #endif /* !UNRAVEL */
  173. }
  174. /* initialize the SHA digest */
  175. static void
  176. sha_init(SHA_INFO *sha_info)
  177. {
  178. sha_info->digest[0] = 0x67452301L;
  179. sha_info->digest[1] = 0xefcdab89L;
  180. sha_info->digest[2] = 0x98badcfeL;
  181. sha_info->digest[3] = 0x10325476L;
  182. sha_info->digest[4] = 0xc3d2e1f0L;
  183. sha_info->count_lo = 0L;
  184. sha_info->count_hi = 0L;
  185. sha_info->local = 0;
  186. }
  187. /* update the SHA digest */
  188. static void
  189. sha_update(SHA_INFO *sha_info, sha_byte *buffer, int count)
  190. {
  191. int i;
  192. ULONG clo;
  193. clo = T32(sha_info->count_lo + ((ULONG) count << 3));
  194. if (clo < sha_info->count_lo) {
  195. ++sha_info->count_hi;
  196. }
  197. sha_info->count_lo = clo;
  198. sha_info->count_hi += (ULONG) count >> 29;
  199. if (sha_info->local) {
  200. i = SHA_BLOCKSIZE - sha_info->local;
  201. if (i > count) {
  202. i = count;
  203. }
  204. memcpy(((sha_byte *) sha_info->data) + sha_info->local, buffer, i);
  205. count -= i;
  206. buffer += i;
  207. sha_info->local += i;
  208. if (sha_info->local == SHA_BLOCKSIZE) {
  209. sha_transform(sha_info);
  210. } else {
  211. return;
  212. }
  213. }
  214. while (count >= SHA_BLOCKSIZE) {
  215. memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
  216. buffer += SHA_BLOCKSIZE;
  217. count -= SHA_BLOCKSIZE;
  218. sha_transform(sha_info);
  219. }
  220. memcpy(sha_info->data, buffer, count);
  221. sha_info->local = count;
  222. }
  223. static void
  224. sha_transform_and_copy(unsigned char digest[20], SHA_INFO *sha_info)
  225. {
  226. sha_transform(sha_info);
  227. digest[ 0] = (unsigned char) ((sha_info->digest[0] >> 24) & 0xff);
  228. digest[ 1] = (unsigned char) ((sha_info->digest[0] >> 16) & 0xff);
  229. digest[ 2] = (unsigned char) ((sha_info->digest[0] >> 8) & 0xff);
  230. digest[ 3] = (unsigned char) ((sha_info->digest[0] ) & 0xff);
  231. digest[ 4] = (unsigned char) ((sha_info->digest[1] >> 24) & 0xff);
  232. digest[ 5] = (unsigned char) ((sha_info->digest[1] >> 16) & 0xff);
  233. digest[ 6] = (unsigned char) ((sha_info->digest[1] >> 8) & 0xff);
  234. digest[ 7] = (unsigned char) ((sha_info->digest[1] ) & 0xff);
  235. digest[ 8] = (unsigned char) ((sha_info->digest[2] >> 24) & 0xff);
  236. digest[ 9] = (unsigned char) ((sha_info->digest[2] >> 16) & 0xff);
  237. digest[10] = (unsigned char) ((sha_info->digest[2] >> 8) & 0xff);
  238. digest[11] = (unsigned char) ((sha_info->digest[2] ) & 0xff);
  239. digest[12] = (unsigned char) ((sha_info->digest[3] >> 24) & 0xff);
  240. digest[13] = (unsigned char) ((sha_info->digest[3] >> 16) & 0xff);
  241. digest[14] = (unsigned char) ((sha_info->digest[3] >> 8) & 0xff);
  242. digest[15] = (unsigned char) ((sha_info->digest[3] ) & 0xff);
  243. digest[16] = (unsigned char) ((sha_info->digest[4] >> 24) & 0xff);
  244. digest[17] = (unsigned char) ((sha_info->digest[4] >> 16) & 0xff);
  245. digest[18] = (unsigned char) ((sha_info->digest[4] >> 8) & 0xff);
  246. digest[19] = (unsigned char) ((sha_info->digest[4] ) & 0xff);
  247. }
  248. /* finish computing the SHA digest */
  249. static void
  250. sha_final(unsigned char digest[20], SHA_INFO *sha_info)
  251. {
  252. int count;
  253. ULONG lo_bit_count, hi_bit_count;
  254. lo_bit_count = sha_info->count_lo;
  255. hi_bit_count = sha_info->count_hi;
  256. count = (int) ((lo_bit_count >> 3) & 0x3f);
  257. ((sha_byte *) sha_info->data)[count++] = 0x80;
  258. if (count > SHA_BLOCKSIZE - 8) {
  259. memset(((sha_byte *) sha_info->data) + count, 0, SHA_BLOCKSIZE - count);
  260. sha_transform(sha_info);
  261. memset((sha_byte *) sha_info->data, 0, SHA_BLOCKSIZE - 8);
  262. } else {
  263. memset(((sha_byte *) sha_info->data) + count, 0,
  264. SHA_BLOCKSIZE - 8 - count);
  265. }
  266. sha_info->data[56] = (hi_bit_count >> 24) & 0xff;
  267. sha_info->data[57] = (hi_bit_count >> 16) & 0xff;
  268. sha_info->data[58] = (hi_bit_count >> 8) & 0xff;
  269. sha_info->data[59] = (hi_bit_count >> 0) & 0xff;
  270. sha_info->data[60] = (lo_bit_count >> 24) & 0xff;
  271. sha_info->data[61] = (lo_bit_count >> 16) & 0xff;
  272. sha_info->data[62] = (lo_bit_count >> 8) & 0xff;
  273. sha_info->data[63] = (lo_bit_count >> 0) & 0xff;
  274. sha_transform_and_copy(digest, sha_info);
  275. }
  276. /********************************************************************/
  277. /*
  278. SHA_INFO ctx;
  279. unsigned char *data;
  280. STRLEN len;
  281. unsigned char digeststr[20];
  282. sha_init(&ctx);
  283. for (i = 0; i < items; i++) {
  284. data = (unsigned char *)(SvPVbyte(ST(i), len));
  285. sha_update(&ctx, data, len);
  286. }
  287. sha_final(digeststr, &ctx);
  288. */
  289. static void
  290. sha_digest(char *out, char *data, int len)
  291. {
  292. SHA_INFO ctx;
  293. sha_init(&ctx);
  294. sha_update(&ctx, (sha_byte*)data, len);
  295. sha_final((sha_byte*)out, &ctx);
  296. }
  297. void
  298. srs_hmac_init(srs_hmac_ctx_t *ctx, char *secret, int len)
  299. {
  300. char sbuf[SHA_BLOCKSIZE];
  301. int i;
  302. if (len > SHA_BLOCKSIZE) {
  303. sha_digest(sbuf, secret, len);
  304. secret = sbuf;
  305. len = strlen(sbuf); /* SHA_BLOCKSIZE? */
  306. }
  307. memset(ctx->ipad, 0x36, SHA_BLOCKSIZE);
  308. memset(ctx->opad, 0x5c, SHA_BLOCKSIZE);
  309. for (i = 0; i < len; i++) {
  310. ctx->ipad[i] ^= secret[i];
  311. ctx->opad[i] ^= secret[i];
  312. }
  313. memset(sbuf, 0, SHA_BLOCKSIZE);
  314. sha_init(&ctx->sctx);
  315. sha_update(&ctx->sctx, (sha_byte*)ctx->ipad, SHA_BLOCKSIZE);
  316. }
  317. void
  318. srs_hmac_update(srs_hmac_ctx_t *ctx, char *data, int len)
  319. {
  320. sha_update(&ctx->sctx, (sha_byte*)data, len);
  321. }
  322. void
  323. srs_hmac_fini(srs_hmac_ctx_t *ctx, char *out)
  324. {
  325. sha_byte buf[SHA_DIGESTSIZE + 1];
  326. sha_final(buf, &ctx->sctx);
  327. sha_init(&ctx->sctx);
  328. sha_update(&ctx->sctx, (sha_byte*)ctx->opad, SHA_BLOCKSIZE);
  329. sha_update(&ctx->sctx, buf, SHA_DIGESTSIZE);
  330. sha_final((sha_byte*)out, &ctx->sctx);
  331. }