sha256.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. /* SHA256 by Tom St Denis */
  12. #include "mycrypt.h"
  13. #ifdef SHA256
  14. const struct _hash_descriptor sha256_desc =
  15. {
  16. "sha256",
  17. 0,
  18. 32,
  19. 64,
  20. /* DER identifier */
  21. { 0x30, 0x31, 0x30, 0x0D, 0x06, 0x09, 0x60, 0x86,
  22. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23. 0x00, 0x04, 0x20 },
  24. 19,
  25. &sha256_init,
  26. &sha256_process,
  27. &sha256_done,
  28. &sha256_test
  29. };
  30. #ifdef SMALL_CODE
  31. /* the K array */
  32. static const unsigned long K[64] = {
  33. 0x428a2f98UL, 0x71374491UL, 0xb5c0fbcfUL, 0xe9b5dba5UL, 0x3956c25bUL,
  34. 0x59f111f1UL, 0x923f82a4UL, 0xab1c5ed5UL, 0xd807aa98UL, 0x12835b01UL,
  35. 0x243185beUL, 0x550c7dc3UL, 0x72be5d74UL, 0x80deb1feUL, 0x9bdc06a7UL,
  36. 0xc19bf174UL, 0xe49b69c1UL, 0xefbe4786UL, 0x0fc19dc6UL, 0x240ca1ccUL,
  37. 0x2de92c6fUL, 0x4a7484aaUL, 0x5cb0a9dcUL, 0x76f988daUL, 0x983e5152UL,
  38. 0xa831c66dUL, 0xb00327c8UL, 0xbf597fc7UL, 0xc6e00bf3UL, 0xd5a79147UL,
  39. 0x06ca6351UL, 0x14292967UL, 0x27b70a85UL, 0x2e1b2138UL, 0x4d2c6dfcUL,
  40. 0x53380d13UL, 0x650a7354UL, 0x766a0abbUL, 0x81c2c92eUL, 0x92722c85UL,
  41. 0xa2bfe8a1UL, 0xa81a664bUL, 0xc24b8b70UL, 0xc76c51a3UL, 0xd192e819UL,
  42. 0xd6990624UL, 0xf40e3585UL, 0x106aa070UL, 0x19a4c116UL, 0x1e376c08UL,
  43. 0x2748774cUL, 0x34b0bcb5UL, 0x391c0cb3UL, 0x4ed8aa4aUL, 0x5b9cca4fUL,
  44. 0x682e6ff3UL, 0x748f82eeUL, 0x78a5636fUL, 0x84c87814UL, 0x8cc70208UL,
  45. 0x90befffaUL, 0xa4506cebUL, 0xbef9a3f7UL, 0xc67178f2UL
  46. };
  47. #endif
  48. /* Various logical functions */
  49. #define Ch(x,y,z) (z ^ (x & (y ^ z)))
  50. #define Maj(x,y,z) (((x | y) & z) | (x & y))
  51. #define S(x, n) ROR((x),(n))
  52. #define R(x, n) (((x)&0xFFFFFFFFUL)>>(n))
  53. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  54. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  55. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  56. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  57. /* compress 512-bits */
  58. #ifdef CLEAN_STACK
  59. static void _sha256_compress(hash_state * md, unsigned char *buf)
  60. #else
  61. static void sha256_compress(hash_state * md, unsigned char *buf)
  62. #endif
  63. {
  64. ulong32 S[8], W[64], t0, t1;
  65. #ifdef SMALL_CODE
  66. ulong32 t;
  67. #endif
  68. int i;
  69. /* copy state into S */
  70. for (i = 0; i < 8; i++) {
  71. S[i] = md->sha256.state[i];
  72. }
  73. /* copy the state into 512-bits into W[0..15] */
  74. for (i = 0; i < 16; i++) {
  75. LOAD32H(W[i], buf + (4*i));
  76. }
  77. /* fill W[16..63] */
  78. for (i = 16; i < 64; i++) {
  79. W[i] = Gamma1(W[i - 2]) + W[i - 7] + Gamma0(W[i - 15]) + W[i - 16];
  80. }
  81. /* Compress */
  82. #ifdef SMALL_CODE
  83. #define RND(a,b,c,d,e,f,g,h,i) \
  84. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  85. t1 = Sigma0(a) + Maj(a, b, c); \
  86. d += t0; \
  87. h = t0 + t1;
  88. for (i = 0; i < 64; ++i) {
  89. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i);
  90. t = S[7]; S[7] = S[6]; S[6] = S[5]; S[5] = S[4];
  91. S[4] = S[3]; S[3] = S[2]; S[2] = S[1]; S[1] = S[0]; S[0] = t;
  92. }
  93. #else
  94. #define RND(a,b,c,d,e,f,g,h,i,ki) \
  95. t0 = h + Sigma1(e) + Ch(e, f, g) + ki + W[i]; \
  96. t1 = Sigma0(a) + Maj(a, b, c); \
  97. d += t0; \
  98. h = t0 + t1;
  99. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],0,0x428a2f98);
  100. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],1,0x71374491);
  101. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],2,0xb5c0fbcf);
  102. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],3,0xe9b5dba5);
  103. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],4,0x3956c25b);
  104. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],5,0x59f111f1);
  105. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],6,0x923f82a4);
  106. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],7,0xab1c5ed5);
  107. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],8,0xd807aa98);
  108. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],9,0x12835b01);
  109. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],10,0x243185be);
  110. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],11,0x550c7dc3);
  111. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],12,0x72be5d74);
  112. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],13,0x80deb1fe);
  113. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],14,0x9bdc06a7);
  114. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],15,0xc19bf174);
  115. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],16,0xe49b69c1);
  116. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],17,0xefbe4786);
  117. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],18,0x0fc19dc6);
  118. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],19,0x240ca1cc);
  119. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],20,0x2de92c6f);
  120. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],21,0x4a7484aa);
  121. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],22,0x5cb0a9dc);
  122. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],23,0x76f988da);
  123. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],24,0x983e5152);
  124. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],25,0xa831c66d);
  125. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],26,0xb00327c8);
  126. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],27,0xbf597fc7);
  127. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],28,0xc6e00bf3);
  128. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],29,0xd5a79147);
  129. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],30,0x06ca6351);
  130. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],31,0x14292967);
  131. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],32,0x27b70a85);
  132. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],33,0x2e1b2138);
  133. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],34,0x4d2c6dfc);
  134. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],35,0x53380d13);
  135. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],36,0x650a7354);
  136. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],37,0x766a0abb);
  137. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],38,0x81c2c92e);
  138. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],39,0x92722c85);
  139. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],40,0xa2bfe8a1);
  140. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],41,0xa81a664b);
  141. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],42,0xc24b8b70);
  142. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],43,0xc76c51a3);
  143. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],44,0xd192e819);
  144. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],45,0xd6990624);
  145. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],46,0xf40e3585);
  146. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],47,0x106aa070);
  147. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],48,0x19a4c116);
  148. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],49,0x1e376c08);
  149. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],50,0x2748774c);
  150. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],51,0x34b0bcb5);
  151. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],52,0x391c0cb3);
  152. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],53,0x4ed8aa4a);
  153. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],54,0x5b9cca4f);
  154. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],55,0x682e6ff3);
  155. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],56,0x748f82ee);
  156. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],57,0x78a5636f);
  157. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],58,0x84c87814);
  158. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],59,0x8cc70208);
  159. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],60,0x90befffa);
  160. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],61,0xa4506ceb);
  161. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],62,0xbef9a3f7);
  162. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],63,0xc67178f2);
  163. #undef RND
  164. #endif
  165. /* feedback */
  166. for (i = 0; i < 8; i++) {
  167. md->sha256.state[i] = md->sha256.state[i] + S[i];
  168. }
  169. }
  170. #ifdef CLEAN_STACK
  171. static void sha256_compress(hash_state * md, unsigned char *buf)
  172. {
  173. _sha256_compress(md, buf);
  174. burn_stack(sizeof(ulong32) * 74);
  175. }
  176. #endif
  177. /* init the sha256 state */
  178. void sha256_init(hash_state * md)
  179. {
  180. _ARGCHK(md != NULL);
  181. md->sha256.curlen = 0;
  182. md->sha256.length = 0;
  183. md->sha256.state[0] = 0x6A09E667UL;
  184. md->sha256.state[1] = 0xBB67AE85UL;
  185. md->sha256.state[2] = 0x3C6EF372UL;
  186. md->sha256.state[3] = 0xA54FF53AUL;
  187. md->sha256.state[4] = 0x510E527FUL;
  188. md->sha256.state[5] = 0x9B05688CUL;
  189. md->sha256.state[6] = 0x1F83D9ABUL;
  190. md->sha256.state[7] = 0x5BE0CD19UL;
  191. }
  192. HASH_PROCESS(sha256_process, sha256_compress, sha256, 64)
  193. int sha256_done(hash_state * md, unsigned char *hash)
  194. {
  195. int i;
  196. _ARGCHK(md != NULL);
  197. _ARGCHK(hash != NULL);
  198. if (md->sha256.curlen >= sizeof(md->sha256.buf)) {
  199. return CRYPT_INVALID_ARG;
  200. }
  201. /* increase the length of the message */
  202. md->sha256.length += md->sha256.curlen * 8;
  203. /* append the '1' bit */
  204. md->sha256.buf[md->sha256.curlen++] = (unsigned char)0x80;
  205. /* if the length is currently above 56 bytes we append zeros
  206. * then compress. Then we can fall back to padding zeros and length
  207. * encoding like normal.
  208. */
  209. if (md->sha256.curlen > 56) {
  210. while (md->sha256.curlen < 64) {
  211. md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
  212. }
  213. sha256_compress(md, md->sha256.buf);
  214. md->sha256.curlen = 0;
  215. }
  216. /* pad upto 56 bytes of zeroes */
  217. while (md->sha256.curlen < 56) {
  218. md->sha256.buf[md->sha256.curlen++] = (unsigned char)0;
  219. }
  220. /* store length */
  221. STORE64H(md->sha256.length, md->sha256.buf+56);
  222. sha256_compress(md, md->sha256.buf);
  223. /* copy output */
  224. for (i = 0; i < 8; i++) {
  225. STORE32H(md->sha256.state[i], hash+(4*i));
  226. }
  227. #ifdef CLEAN_STACK
  228. zeromem(md, sizeof(hash_state));
  229. #endif
  230. return CRYPT_OK;
  231. }
  232. int sha256_test(void)
  233. {
  234. #ifndef LTC_TEST
  235. return CRYPT_NOP;
  236. #else
  237. static const struct {
  238. char *msg;
  239. unsigned char hash[32];
  240. } tests[] = {
  241. { "abc",
  242. { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
  243. 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
  244. 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
  245. 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }
  246. },
  247. { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  248. { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
  249. 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
  250. 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
  251. 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }
  252. },
  253. };
  254. int i;
  255. unsigned char tmp[32];
  256. hash_state md;
  257. for (i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) {
  258. sha256_init(&md);
  259. sha256_process(&md, (unsigned char*)tests[i].msg, (unsigned long)strlen(tests[i].msg));
  260. sha256_done(&md, tmp);
  261. if (memcmp(tmp, tests[i].hash, 32) != 0) {
  262. return CRYPT_FAIL_TESTVECTOR;
  263. }
  264. }
  265. return CRYPT_OK;
  266. #endif
  267. }
  268. #ifdef SHA224
  269. #include "sha224.c"
  270. #endif
  271. #endif