2
0

sha1.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  2. *
  3. * LibTomCrypt is a library that provides various cryptographic
  4. * algorithms in a highly modular and flexible manner.
  5. *
  6. * The library is free for all purposes without any express
  7. * guarantee it works.
  8. *
  9. * Tom St Denis, [email protected], http://libtomcrypt.org
  10. */
  11. /* SHA1 code by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef SHA1
  14. const struct _hash_descriptor sha1_desc =
  15. {
  16. "sha1",
  17. 2,
  18. 20,
  19. 64,
  20. /* DER identifier */
  21. { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E,
  22. 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14 },
  23. 15,
  24. &sha1_init,
  25. &sha1_process,
  26. &sha1_done,
  27. &sha1_test
  28. };
  29. #define F0(x,y,z) (z ^ (x & (y ^ z)))
  30. #define F1(x,y,z) (x ^ y ^ z)
  31. #define F2(x,y,z) ((x & y) | (z & (x | y)))
  32. #define F3(x,y,z) (x ^ y ^ z)
  33. #ifdef CLEAN_STACK
  34. static int _sha1_compress(hash_state *md, unsigned char *buf)
  35. #else
  36. static int sha1_compress(hash_state *md, unsigned char *buf)
  37. #endif
  38. {
  39. ulong32 a,b,c,d,e,W[80],i;
  40. #ifdef SMALL_CODE
  41. ulong32 t;
  42. #endif
  43. /* copy the state into 512-bits into W[0..15] */
  44. for (i = 0; i < 16; i++) {
  45. LOAD32H(W[i], buf + (4*i));
  46. }
  47. /* copy state */
  48. a = md->sha1.state[0];
  49. b = md->sha1.state[1];
  50. c = md->sha1.state[2];
  51. d = md->sha1.state[3];
  52. e = md->sha1.state[4];
  53. /* expand it */
  54. for (i = 16; i < 80; i++) {
  55. W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
  56. }
  57. /* compress */
  58. /* round one */
  59. #define FF0(a,b,c,d,e,i) e = (ROL(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROL(b, 30);
  60. #define FF1(a,b,c,d,e,i) e = (ROL(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROL(b, 30);
  61. #define FF2(a,b,c,d,e,i) e = (ROL(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROL(b, 30);
  62. #define FF3(a,b,c,d,e,i) e = (ROL(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROL(b, 30);
  63. #ifdef SMALL_CODE
  64. for (i = 0; i < 20; ) {
  65. FF0(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
  66. }
  67. for (; i < 40; ) {
  68. FF1(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
  69. }
  70. for (; i < 60; ) {
  71. FF2(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
  72. }
  73. for (; i < 80; ) {
  74. FF3(a,b,c,d,e,i++); t = e; e = d; d = c; c = b; b = a; a = t;
  75. }
  76. #else
  77. for (i = 0; i < 20; ) {
  78. FF0(a,b,c,d,e,i++);
  79. FF0(e,a,b,c,d,i++);
  80. FF0(d,e,a,b,c,i++);
  81. FF0(c,d,e,a,b,i++);
  82. FF0(b,c,d,e,a,i++);
  83. }
  84. /* round two */
  85. for (; i < 40; ) {
  86. FF1(a,b,c,d,e,i++);
  87. FF1(e,a,b,c,d,i++);
  88. FF1(d,e,a,b,c,i++);
  89. FF1(c,d,e,a,b,i++);
  90. FF1(b,c,d,e,a,i++);
  91. }
  92. /* round three */
  93. for (; i < 60; ) {
  94. FF2(a,b,c,d,e,i++);
  95. FF2(e,a,b,c,d,i++);
  96. FF2(d,e,a,b,c,i++);
  97. FF2(c,d,e,a,b,i++);
  98. FF2(b,c,d,e,a,i++);
  99. }
  100. /* round four */
  101. for (; i < 80; ) {
  102. FF3(a,b,c,d,e,i++);
  103. FF3(e,a,b,c,d,i++);
  104. FF3(d,e,a,b,c,i++);
  105. FF3(c,d,e,a,b,i++);
  106. FF3(b,c,d,e,a,i++);
  107. }
  108. #endif
  109. #undef FF0
  110. #undef FF1
  111. #undef FF2
  112. #undef FF3
  113. /* store */
  114. md->sha1.state[0] = md->sha1.state[0] + a;
  115. md->sha1.state[1] = md->sha1.state[1] + b;
  116. md->sha1.state[2] = md->sha1.state[2] + c;
  117. md->sha1.state[3] = md->sha1.state[3] + d;
  118. md->sha1.state[4] = md->sha1.state[4] + e;
  119. return CRYPT_OK;
  120. }
  121. #ifdef CLEAN_STACK
  122. static int sha1_compress(hash_state *md, unsigned char *buf)
  123. {
  124. int err;
  125. err = _sha1_compress(md, buf);
  126. burn_stack(sizeof(ulong32) * 87);
  127. return err;
  128. }
  129. #endif
  130. int sha1_init(hash_state * md)
  131. {
  132. _ARGCHK(md != NULL);
  133. md->sha1.state[0] = 0x67452301UL;
  134. md->sha1.state[1] = 0xefcdab89UL;
  135. md->sha1.state[2] = 0x98badcfeUL;
  136. md->sha1.state[3] = 0x10325476UL;
  137. md->sha1.state[4] = 0xc3d2e1f0UL;
  138. md->sha1.curlen = 0;
  139. md->sha1.length = 0;
  140. return CRYPT_OK;
  141. }
  142. HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
  143. int sha1_done(hash_state * md, unsigned char *hash)
  144. {
  145. int i;
  146. _ARGCHK(md != NULL);
  147. _ARGCHK(hash != NULL);
  148. if (md->sha1.curlen >= sizeof(md->sha1.buf)) {
  149. return CRYPT_INVALID_ARG;
  150. }
  151. /* increase the length of the message */
  152. md->sha1.length += md->sha1.curlen * 8;
  153. /* append the '1' bit */
  154. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
  155. /* if the length is currently above 56 bytes we append zeros
  156. * then compress. Then we can fall back to padding zeros and length
  157. * encoding like normal.
  158. */
  159. if (md->sha1.curlen > 56) {
  160. while (md->sha1.curlen < 64) {
  161. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  162. }
  163. sha1_compress(md, md->sha1.buf);
  164. md->sha1.curlen = 0;
  165. }
  166. /* pad upto 56 bytes of zeroes */
  167. while (md->sha1.curlen < 56) {
  168. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  169. }
  170. /* store length */
  171. STORE64H(md->sha1.length, md->sha1.buf+56);
  172. sha1_compress(md, md->sha1.buf);
  173. /* copy output */
  174. for (i = 0; i < 5; i++) {
  175. STORE32H(md->sha1.state[i], hash+(4*i));
  176. }
  177. #ifdef CLEAN_STACK
  178. zeromem(md, sizeof(hash_state));
  179. #endif
  180. return CRYPT_OK;
  181. }
  182. int sha1_test(void)
  183. {
  184. #ifndef LTC_TEST
  185. return CRYPT_NOP;
  186. #else
  187. static const struct {
  188. char *msg;
  189. unsigned char hash[20];
  190. } tests[] = {
  191. { "abc",
  192. { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
  193. 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
  194. 0x9c, 0xd0, 0xd8, 0x9d }
  195. },
  196. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  197. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
  198. 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
  199. 0xE5, 0x46, 0x70, 0xF1 }
  200. }
  201. };
  202. int i;
  203. unsigned char tmp[20];
  204. hash_state md;
  205. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  206. sha1_init(&md);
  207. sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  208. sha1_done(&md, tmp);
  209. if (memcmp(tmp, tests[i].hash, 20) != 0) {
  210. return CRYPT_FAIL_TESTVECTOR;
  211. }
  212. }
  213. return CRYPT_OK;
  214. #endif
  215. }
  216. #endif