sha1.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 void _sha1_compress(hash_state *md, unsigned char *buf)
  35. #else
  36. static void 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. }
  120. #ifdef CLEAN_STACK
  121. static void sha1_compress(hash_state *md, unsigned char *buf)
  122. {
  123. _sha1_compress(md, buf);
  124. burn_stack(sizeof(ulong32) * 87);
  125. }
  126. #endif
  127. void sha1_init(hash_state * md)
  128. {
  129. _ARGCHK(md != NULL);
  130. md->sha1.state[0] = 0x67452301UL;
  131. md->sha1.state[1] = 0xefcdab89UL;
  132. md->sha1.state[2] = 0x98badcfeUL;
  133. md->sha1.state[3] = 0x10325476UL;
  134. md->sha1.state[4] = 0xc3d2e1f0UL;
  135. md->sha1.curlen = 0;
  136. md->sha1.length = 0;
  137. }
  138. HASH_PROCESS(sha1_process, sha1_compress, sha1, 64)
  139. int sha1_done(hash_state * md, unsigned char *hash)
  140. {
  141. int i;
  142. _ARGCHK(md != NULL);
  143. _ARGCHK(hash != NULL);
  144. if (md->sha1.curlen >= sizeof(md->sha1.buf)) {
  145. return CRYPT_INVALID_ARG;
  146. }
  147. /* increase the length of the message */
  148. md->sha1.length += md->sha1.curlen * 8;
  149. /* append the '1' bit */
  150. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0x80;
  151. /* if the length is currently above 56 bytes we append zeros
  152. * then compress. Then we can fall back to padding zeros and length
  153. * encoding like normal.
  154. */
  155. if (md->sha1.curlen > 56) {
  156. while (md->sha1.curlen < 64) {
  157. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  158. }
  159. sha1_compress(md, md->sha1.buf);
  160. md->sha1.curlen = 0;
  161. }
  162. /* pad upto 56 bytes of zeroes */
  163. while (md->sha1.curlen < 56) {
  164. md->sha1.buf[md->sha1.curlen++] = (unsigned char)0;
  165. }
  166. /* store length */
  167. STORE64H(md->sha1.length, md->sha1.buf+56);
  168. sha1_compress(md, md->sha1.buf);
  169. /* copy output */
  170. for (i = 0; i < 5; i++) {
  171. STORE32H(md->sha1.state[i], hash+(4*i));
  172. }
  173. #ifdef CLEAN_STACK
  174. zeromem(md, sizeof(hash_state));
  175. #endif
  176. return CRYPT_OK;
  177. }
  178. int sha1_test(void)
  179. {
  180. #ifndef LTC_TEST
  181. return CRYPT_NOP;
  182. #else
  183. static const struct {
  184. char *msg;
  185. unsigned char hash[20];
  186. } tests[] = {
  187. { "abc",
  188. { 0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
  189. 0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
  190. 0x9c, 0xd0, 0xd8, 0x9d }
  191. },
  192. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  193. { 0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
  194. 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
  195. 0xE5, 0x46, 0x70, 0xF1 }
  196. }
  197. };
  198. int i;
  199. unsigned char tmp[20];
  200. hash_state md;
  201. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  202. sha1_init(&md);
  203. sha1_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  204. sha1_done(&md, tmp);
  205. if (memcmp(tmp, tests[i].hash, 20) != 0) {
  206. return CRYPT_FAIL_TESTVECTOR;
  207. }
  208. }
  209. return CRYPT_OK;
  210. #endif
  211. }
  212. #endif