PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/elements/ipsec/sha1_impl.cc

https://github.com/bhesmans/click
C++ | 656 lines | 514 code | 57 blank | 85 comment | 47 complexity | e5734a642eeb8e91bedc06699219c495 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
  1. /* crypto/sha/sha1dgst.c */
  2. /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include "sha1_impl.hh"
  59. #define ULONG unsigned long
  60. #define UCHAR unsigned char
  61. #define UINT unsigned int
  62. #undef c2nl
  63. #define c2nl(c,l) (l =(((unsigned long)(*((c)++)))<<24), \
  64. l|=(((unsigned long)(*((c)++)))<<16), \
  65. l|=(((unsigned long)(*((c)++)))<< 8), \
  66. l|=(((unsigned long)(*((c)++))) ))
  67. #undef p_c2nl
  68. #define p_c2nl(c,l,n) { \
  69. switch (n) { \
  70. case 0: l =((unsigned long)(*((c)++)))<<24; \
  71. case 1: l|=((unsigned long)(*((c)++)))<<16; \
  72. case 2: l|=((unsigned long)(*((c)++)))<< 8; \
  73. case 3: l|=((unsigned long)(*((c)++))); \
  74. } \
  75. }
  76. #undef c2nl_p
  77. /* NOTE the pointer is not incremented at the end of this */
  78. #define c2nl_p(c,l,n) { \
  79. l=0; \
  80. (c)+=n; \
  81. switch (n) { \
  82. case 3: l =((unsigned long)(*(--(c))))<< 8; \
  83. case 2: l|=((unsigned long)(*(--(c))))<<16; \
  84. case 1: l|=((unsigned long)(*(--(c))))<<24; \
  85. } \
  86. }
  87. #undef p_c2nl_p
  88. #define p_c2nl_p(c,l,sc,len) { \
  89. switch (sc) \
  90. { \
  91. case 0: l =((unsigned long)(*((c)++)))<<24; \
  92. if (--len == 0) break; \
  93. case 1: l|=((unsigned long)(*((c)++)))<<16; \
  94. if (--len == 0) break; \
  95. case 2: l|=((unsigned long)(*((c)++)))<< 8; \
  96. } \
  97. }
  98. #undef nl2c
  99. #define nl2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
  100. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  101. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  102. *((c)++)=(unsigned char)(((l) )&0xff))
  103. #undef c2l
  104. #define c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \
  105. l|=(((unsigned long)(*((c)++)))<< 8), \
  106. l|=(((unsigned long)(*((c)++)))<<16), \
  107. l|=(((unsigned long)(*((c)++)))<<24))
  108. #undef p_c2l
  109. #define p_c2l(c,l,n) { \
  110. switch (n) { \
  111. case 0: l =((unsigned long)(*((c)++))); \
  112. case 1: l|=((unsigned long)(*((c)++)))<< 8; \
  113. case 2: l|=((unsigned long)(*((c)++)))<<16; \
  114. case 3: l|=((unsigned long)(*((c)++)))<<24; \
  115. } \
  116. }
  117. #undef c2l_p
  118. /* NOTE the pointer is not incremented at the end of this */
  119. #define c2l_p(c,l,n) { \
  120. l=0; \
  121. (c)+=n; \
  122. switch (n) { \
  123. case 3: l =((unsigned long)(*(--(c))))<<16; \
  124. case 2: l|=((unsigned long)(*(--(c))))<< 8; \
  125. case 1: l|=((unsigned long)(*(--(c)))); \
  126. } \
  127. }
  128. #undef p_c2l_p
  129. #define p_c2l_p(c,l,sc,len) { \
  130. switch (sc) \
  131. { \
  132. case 0: l =((unsigned long)(*((c)++))); \
  133. if (--len == 0) break; \
  134. case 1: l|=((unsigned long)(*((c)++)))<< 8; \
  135. if (--len == 0) break; \
  136. case 2: l|=((unsigned long)(*((c)++)))<<16; \
  137. } \
  138. }
  139. #undef l2c
  140. #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
  141. *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  142. *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  143. *((c)++)=(unsigned char)(((l)>>24)&0xff))
  144. #undef ROTATE
  145. #if defined(WIN32)
  146. #define ROTATE(a,n) _lrotl(a,n)
  147. #else
  148. #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  149. #endif
  150. /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
  151. #if defined(WIN32)
  152. /* 5 instructions with rotate instruction, else 9 */
  153. #define Endian_Reverse32(a) \
  154. { \
  155. unsigned long l=(a); \
  156. (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
  157. }
  158. #else
  159. /* 6 instructions with rotate instruction, else 8 */
  160. #define Endian_Reverse32(a) \
  161. { \
  162. unsigned long l=(a); \
  163. l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
  164. (a)=ROTATE(l,16L); \
  165. }
  166. #endif
  167. /* As pointed out by Wei Dai <weidai@eskimo.com>, F() below can be
  168. * simplified to the code in F_00_19. Wei attributes these optimisations
  169. * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
  170. * #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z)))
  171. * I've just become aware of another tweak to be made, again from Wei Dai,
  172. * in F_40_59, (x&a)|(y&a) -> (x|y)&a
  173. */
  174. #define F_00_19(b,c,d) ((((c) ^ (d)) & (b)) ^ (d))
  175. #define F_20_39(b,c,d) ((b) ^ (c) ^ (d))
  176. #define F_40_59(b,c,d) (((b) & (c)) | (((b)|(c)) & (d)))
  177. #define F_60_79(b,c,d) F_20_39(b,c,d)
  178. #ifdef SHA_0
  179. #undef Xupdate
  180. #define Xupdate(a,i,ia,ib,ic,id) X[(i)&0x0f]=(a)=\
  181. (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);
  182. #endif
  183. #ifdef SHA_1
  184. #undef Xupdate
  185. #define Xupdate(a,i,ia,ib,ic,id) (a)=\
  186. (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);\
  187. X[(i)&0x0f]=(a)=ROTATE((a),1);
  188. #endif
  189. #define BODY_00_15(i,a,b,c,d,e,f,xa) \
  190. (f)=xa[i]+(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
  191. (b)=ROTATE((b),30);
  192. #define BODY_16_19(i,a,b,c,d,e,f,xa,xb,xc,xd) \
  193. Xupdate(f,i,xa,xb,xc,xd); \
  194. (f)+=(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
  195. (b)=ROTATE((b),30);
  196. #define BODY_20_31(i,a,b,c,d,e,f,xa,xb,xc,xd) \
  197. Xupdate(f,i,xa,xb,xc,xd); \
  198. (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
  199. (b)=ROTATE((b),30);
  200. #define BODY_32_39(i,a,b,c,d,e,f,xa) \
  201. Xupdate(f,i,xa,xa,xa,xa); \
  202. (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
  203. (b)=ROTATE((b),30);
  204. #define BODY_40_59(i,a,b,c,d,e,f,xa) \
  205. Xupdate(f,i,xa,xa,xa,xa); \
  206. (f)+=(e)+K_40_59+ROTATE((a),5)+F_40_59((b),(c),(d)); \
  207. (b)=ROTATE((b),30);
  208. #define BODY_60_79(i,a,b,c,d,e,f,xa) \
  209. Xupdate(f,i,xa,xa,xa,xa); \
  210. (f)=X[(i)&0x0f]+(e)+K_60_79+ROTATE((a),5)+F_60_79((b),(c),(d)); \
  211. (b)=ROTATE((b),30);
  212. const char SHA1_version[] = "SHA1 part of SSLeay 0.8.2b 08-Jan-1998";
  213. /* Implemented from SHA-1 document - The Secure Hash Algorithm
  214. */
  215. #define INIT_DATA_h0 (unsigned long)0x67452301L
  216. #define INIT_DATA_h1 (unsigned long)0xefcdab89L
  217. #define INIT_DATA_h2 (unsigned long)0x98badcfeL
  218. #define INIT_DATA_h3 (unsigned long)0x10325476L
  219. #define INIT_DATA_h4 (unsigned long)0xc3d2e1f0L
  220. #define K_00_19 0x5a827999L
  221. #define K_20_39 0x6ed9eba1L
  222. #define K_40_59 0x8f1bbcdcL
  223. #define K_60_79 0xca62c1d6L
  224. #ifndef NOPROTO
  225. # ifdef SHA1_ASM
  226. void sha1_block_x86 (SHA1_ctx * c, register unsigned long *p, int num);
  227. # define sha1_block sha1_block_x86
  228. # else
  229. void sha1_block (SHA1_ctx * c, register unsigned long *p, int num);
  230. # endif
  231. #else
  232. # ifdef SHA1_ASM
  233. void sha1_block_x86 ();
  234. # define sha1_block sha1_block_x86
  235. # else
  236. void sha1_block ();
  237. # endif
  238. #endif
  239. #if defined(L_ENDIAN) && defined(SHA1_ASM)
  240. # define M_c2nl c2l
  241. # define M_p_c2nl p_c2l
  242. # define M_c2nl_p c2l_p
  243. # define M_p_c2nl_p p_c2l_p
  244. # define M_nl2c l2c
  245. #else
  246. # define M_c2nl c2nl
  247. # define M_p_c2nl p_c2nl
  248. # define M_c2nl_p c2nl_p
  249. # define M_p_c2nl_p p_c2nl_p
  250. # define M_nl2c nl2c
  251. #endif
  252. void
  253. SHA1_init (SHA1_ctx * c)
  254. {
  255. c->h0 = INIT_DATA_h0;
  256. c->h1 = INIT_DATA_h1;
  257. c->h2 = INIT_DATA_h2;
  258. c->h3 = INIT_DATA_h3;
  259. c->h4 = INIT_DATA_h4;
  260. c->Nl = 0;
  261. c->Nh = 0;
  262. c->num = 0;
  263. }
  264. void
  265. SHA1_update (SHA1_ctx * c, register unsigned char *data, unsigned long len)
  266. {
  267. register ULONG *p;
  268. int ew, ec, sw, sc;
  269. ULONG l;
  270. if (len == 0)
  271. return;
  272. l = (c->Nl + (len << 3)) & 0xffffffffL;
  273. if (l < c->Nl) /* overflow */
  274. c->Nh++;
  275. c->Nh += (len >> 29);
  276. c->Nl = l;
  277. if (c->num != 0)
  278. {
  279. p = c->data;
  280. sw = c->num >> 2;
  281. sc = c->num & 0x03;
  282. if ((c->num + len) >= SHA_CBLOCK)
  283. {
  284. l = p[sw];
  285. M_p_c2nl (data, l, sc);
  286. p[sw++] = l;
  287. for (; sw < SHA_LBLOCK; sw++)
  288. {
  289. M_c2nl (data, l);
  290. p[sw] = l;
  291. }
  292. len -= (SHA_CBLOCK - c->num);
  293. sha1_block (c, p, 64);
  294. c->num = 0;
  295. /* drop through and do the rest */
  296. }
  297. else
  298. {
  299. c->num += (int) len;
  300. if ((sc + len) < 4) /* ugly, add char's to a word */
  301. {
  302. l = p[sw];
  303. M_p_c2nl_p (data, l, sc, len);
  304. p[sw] = l;
  305. }
  306. else
  307. {
  308. ew = (c->num >> 2);
  309. ec = (c->num & 0x03);
  310. l = p[sw];
  311. M_p_c2nl (data, l, sc);
  312. p[sw++] = l;
  313. for (; sw < ew; sw++)
  314. {
  315. M_c2nl (data, l);
  316. p[sw] = l;
  317. }
  318. if (ec)
  319. {
  320. M_c2nl_p (data, l, ec);
  321. p[sw] = l;
  322. }
  323. }
  324. return;
  325. }
  326. }
  327. /* We can only do the following code for assember, the reason
  328. * being that the sha1_block 'C' version changes the values
  329. * in the 'data' array. The assember code avoids this and
  330. * copies it to a local array. I should be able to do this for
  331. * the C version as well....
  332. */
  333. #if 1
  334. #if defined(B_ENDIAN) || defined(SHA1_ASM)
  335. if ((((unsigned int) data) % sizeof (ULONG)) == 0)
  336. {
  337. sw = len / SHA_CBLOCK;
  338. if (sw)
  339. {
  340. sw *= SHA_CBLOCK;
  341. sha1_block (c, (ULONG *) data, sw);
  342. data += sw;
  343. len -= sw;
  344. }
  345. }
  346. #endif
  347. #endif
  348. /* we now can process the input data in blocks of SHA_CBLOCK
  349. * chars and save the leftovers to c->data. */
  350. p = c->data;
  351. while (len >= SHA_CBLOCK)
  352. {
  353. #if defined(B_ENDIAN) || defined(L_ENDIAN)
  354. if (p != (unsigned long *) data)
  355. memcpy (p, data, SHA_CBLOCK);
  356. data += SHA_CBLOCK;
  357. # ifdef L_ENDIAN
  358. # ifndef SHA1_ASM /* Will not happen */
  359. for (sw = (SHA_LBLOCK / 4); sw; sw--)
  360. {
  361. Endian_Reverse32 (p[0]);
  362. Endian_Reverse32 (p[1]);
  363. Endian_Reverse32 (p[2]);
  364. Endian_Reverse32 (p[3]);
  365. p += 4;
  366. }
  367. p = c->data;
  368. # endif
  369. # endif
  370. #else
  371. for (sw = (SHA_BLOCK / 4); sw; sw--)
  372. {
  373. M_c2nl (data, l);
  374. *(p++) = l;
  375. M_c2nl (data, l);
  376. *(p++) = l;
  377. M_c2nl (data, l);
  378. *(p++) = l;
  379. M_c2nl (data, l);
  380. *(p++) = l;
  381. }
  382. p = c->data;
  383. #endif
  384. sha1_block (c, p, 64);
  385. len -= SHA_CBLOCK;
  386. }
  387. ec = (int) len;
  388. c->num = ec;
  389. ew = (ec >> 2);
  390. ec &= 0x03;
  391. for (sw = 0; sw < ew; sw++)
  392. {
  393. M_c2nl (data, l);
  394. p[sw] = l;
  395. }
  396. M_c2nl_p (data, l, ec);
  397. p[sw] = l;
  398. }
  399. void
  400. SHA1_transform (SHA1_ctx *c, unsigned char *b)
  401. {
  402. ULONG p[16];
  403. #ifndef B_ENDIAN
  404. ULONG *q;
  405. int i;
  406. #endif
  407. #if defined(B_ENDIAN) || defined(L_ENDIAN)
  408. memcpy (p, b, 64);
  409. #ifdef L_ENDIAN
  410. q = p;
  411. for (i = (SHA_LBLOCK / 4); i; i--)
  412. {
  413. Endian_Reverse32 (q[0]);
  414. Endian_Reverse32 (q[1]);
  415. Endian_Reverse32 (q[2]);
  416. Endian_Reverse32 (q[3]);
  417. q += 4;
  418. }
  419. #endif
  420. #else
  421. q = p;
  422. for (i = (SHA_LBLOCK / 4); i; i--)
  423. {
  424. ULONG l;
  425. c2nl (b, l);
  426. *(q++) = l;
  427. c2nl (b, l);
  428. *(q++) = l;
  429. c2nl (b, l);
  430. *(q++) = l;
  431. c2nl (b, l);
  432. *(q++) = l;
  433. }
  434. #endif
  435. sha1_block (c, p, 64);
  436. }
  437. #ifndef SHA1_ASM
  438. void
  439. sha1_block (SHA1_ctx *c, register unsigned long *W, int num)
  440. {
  441. register ULONG A, B, C, D, E, T;
  442. ULONG X[16];
  443. A = c->h0;
  444. B = c->h1;
  445. C = c->h2;
  446. D = c->h3;
  447. E = c->h4;
  448. for (;;)
  449. {
  450. BODY_00_15 (0, A, B, C, D, E, T, W);
  451. BODY_00_15 (1, T, A, B, C, D, E, W);
  452. BODY_00_15 (2, E, T, A, B, C, D, W);
  453. BODY_00_15 (3, D, E, T, A, B, C, W);
  454. BODY_00_15 (4, C, D, E, T, A, B, W);
  455. BODY_00_15 (5, B, C, D, E, T, A, W);
  456. BODY_00_15 (6, A, B, C, D, E, T, W);
  457. BODY_00_15 (7, T, A, B, C, D, E, W);
  458. BODY_00_15 (8, E, T, A, B, C, D, W);
  459. BODY_00_15 (9, D, E, T, A, B, C, W);
  460. BODY_00_15 (10, C, D, E, T, A, B, W);
  461. BODY_00_15 (11, B, C, D, E, T, A, W);
  462. BODY_00_15 (12, A, B, C, D, E, T, W);
  463. BODY_00_15 (13, T, A, B, C, D, E, W);
  464. BODY_00_15 (14, E, T, A, B, C, D, W);
  465. BODY_00_15 (15, D, E, T, A, B, C, W);
  466. BODY_16_19 (16, C, D, E, T, A, B, W, W, W, W);
  467. BODY_16_19 (17, B, C, D, E, T, A, W, W, W, W);
  468. BODY_16_19 (18, A, B, C, D, E, T, W, W, W, W);
  469. BODY_16_19 (19, T, A, B, C, D, E, W, W, W, X);
  470. BODY_20_31 (20, E, T, A, B, C, D, W, W, W, X);
  471. BODY_20_31 (21, D, E, T, A, B, C, W, W, W, X);
  472. BODY_20_31 (22, C, D, E, T, A, B, W, W, W, X);
  473. BODY_20_31 (23, B, C, D, E, T, A, W, W, W, X);
  474. BODY_20_31 (24, A, B, C, D, E, T, W, W, X, X);
  475. BODY_20_31 (25, T, A, B, C, D, E, W, W, X, X);
  476. BODY_20_31 (26, E, T, A, B, C, D, W, W, X, X);
  477. BODY_20_31 (27, D, E, T, A, B, C, W, W, X, X);
  478. BODY_20_31 (28, C, D, E, T, A, B, W, W, X, X);
  479. BODY_20_31 (29, B, C, D, E, T, A, W, W, X, X);
  480. BODY_20_31 (30, A, B, C, D, E, T, W, X, X, X);
  481. BODY_20_31 (31, T, A, B, C, D, E, W, X, X, X);
  482. BODY_32_39 (32, E, T, A, B, C, D, X);
  483. BODY_32_39 (33, D, E, T, A, B, C, X);
  484. BODY_32_39 (34, C, D, E, T, A, B, X);
  485. BODY_32_39 (35, B, C, D, E, T, A, X);
  486. BODY_32_39 (36, A, B, C, D, E, T, X);
  487. BODY_32_39 (37, T, A, B, C, D, E, X);
  488. BODY_32_39 (38, E, T, A, B, C, D, X);
  489. BODY_32_39 (39, D, E, T, A, B, C, X);
  490. BODY_40_59 (40, C, D, E, T, A, B, X);
  491. BODY_40_59 (41, B, C, D, E, T, A, X);
  492. BODY_40_59 (42, A, B, C, D, E, T, X);
  493. BODY_40_59 (43, T, A, B, C, D, E, X);
  494. BODY_40_59 (44, E, T, A, B, C, D, X);
  495. BODY_40_59 (45, D, E, T, A, B, C, X);
  496. BODY_40_59 (46, C, D, E, T, A, B, X);
  497. BODY_40_59 (47, B, C, D, E, T, A, X);
  498. BODY_40_59 (48, A, B, C, D, E, T, X);
  499. BODY_40_59 (49, T, A, B, C, D, E, X);
  500. BODY_40_59 (50, E, T, A, B, C, D, X);
  501. BODY_40_59 (51, D, E, T, A, B, C, X);
  502. BODY_40_59 (52, C, D, E, T, A, B, X);
  503. BODY_40_59 (53, B, C, D, E, T, A, X);
  504. BODY_40_59 (54, A, B, C, D, E, T, X);
  505. BODY_40_59 (55, T, A, B, C, D, E, X);
  506. BODY_40_59 (56, E, T, A, B, C, D, X);
  507. BODY_40_59 (57, D, E, T, A, B, C, X);
  508. BODY_40_59 (58, C, D, E, T, A, B, X);
  509. BODY_40_59 (59, B, C, D, E, T, A, X);
  510. BODY_60_79 (60, A, B, C, D, E, T, X);
  511. BODY_60_79 (61, T, A, B, C, D, E, X);
  512. BODY_60_79 (62, E, T, A, B, C, D, X);
  513. BODY_60_79 (63, D, E, T, A, B, C, X);
  514. BODY_60_79 (64, C, D, E, T, A, B, X);
  515. BODY_60_79 (65, B, C, D, E, T, A, X);
  516. BODY_60_79 (66, A, B, C, D, E, T, X);
  517. BODY_60_79 (67, T, A, B, C, D, E, X);
  518. BODY_60_79 (68, E, T, A, B, C, D, X);
  519. BODY_60_79 (69, D, E, T, A, B, C, X);
  520. BODY_60_79 (70, C, D, E, T, A, B, X);
  521. BODY_60_79 (71, B, C, D, E, T, A, X);
  522. BODY_60_79 (72, A, B, C, D, E, T, X);
  523. BODY_60_79 (73, T, A, B, C, D, E, X);
  524. BODY_60_79 (74, E, T, A, B, C, D, X);
  525. BODY_60_79 (75, D, E, T, A, B, C, X);
  526. BODY_60_79 (76, C, D, E, T, A, B, X);
  527. BODY_60_79 (77, B, C, D, E, T, A, X);
  528. BODY_60_79 (78, A, B, C, D, E, T, X);
  529. BODY_60_79 (79, T, A, B, C, D, E, X);
  530. c->h0 = (c->h0 + E) & 0xffffffffL;
  531. c->h1 = (c->h1 + T) & 0xffffffffL;
  532. c->h2 = (c->h2 + A) & 0xffffffffL;
  533. c->h3 = (c->h3 + B) & 0xffffffffL;
  534. c->h4 = (c->h4 + C) & 0xffffffffL;
  535. num -= 64;
  536. if (num <= 0)
  537. break;
  538. A = c->h0;
  539. B = c->h1;
  540. C = c->h2;
  541. D = c->h3;
  542. E = c->h4;
  543. W += 16;
  544. }
  545. }
  546. #endif
  547. void
  548. SHA1_final (unsigned char *md, SHA1_ctx *c)
  549. {
  550. register int i, j;
  551. register ULONG l;
  552. register ULONG *p;
  553. static unsigned char end[4] = { 0x80, 0x00, 0x00, 0x00 };
  554. unsigned char *cp = end;
  555. /* c->num should definitly have room for at least one more byte. */
  556. p = c->data;
  557. j = c->num;
  558. i = j >> 2;
  559. #ifdef PURIFY
  560. if ((j & 0x03) == 0)
  561. p[i] = 0;
  562. #endif
  563. l = p[i];
  564. M_p_c2nl (cp, l, j & 0x03);
  565. p[i] = l;
  566. i++;
  567. /* i is the next 'undefined word' */
  568. if (c->num >= SHA_LAST_BLOCK)
  569. {
  570. for (; i < SHA_LBLOCK; i++)
  571. p[i] = 0;
  572. sha1_block (c, p, 64);
  573. i = 0;
  574. }
  575. for (; i < (SHA_LBLOCK - 2); i++)
  576. p[i] = 0;
  577. p[SHA_LBLOCK - 2] = c->Nh;
  578. p[SHA_LBLOCK - 1] = c->Nl;
  579. #if defined(L_ENDIAN) && defined(SHA1_ASM)
  580. Endian_Reverse32 (p[SHA_LBLOCK - 2]);
  581. Endian_Reverse32 (p[SHA_LBLOCK - 1]);
  582. #endif
  583. sha1_block (c, p, 64);
  584. cp = md;
  585. l = c->h0;
  586. nl2c (l, cp);
  587. l = c->h1;
  588. nl2c (l, cp);
  589. l = c->h2;
  590. nl2c (l, cp);
  591. l = c->h3;
  592. nl2c (l, cp);
  593. l = c->h4;
  594. nl2c (l, cp);
  595. /* clear stuff, sha1_block may be leaving some stuff on the stack
  596. * but I'm not worried :-) */
  597. c->num = 0;
  598. /* memset((char *)&c,0,sizeof(c));*/
  599. }