sha256.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* sha256.c
  4. *
  5. * Copyright (C) 2006-2014 wolfSSL Inc.
  6. *
  7. * This file is part of CyaSSL.
  8. *
  9. * CyaSSL is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * CyaSSL is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include "sha256.h"
  24. #include "api.h"
  25. #define XMEMSET memset
  26. #define XMEMCPY memcpy
  27. #ifndef rotlFixed
  28. static inline word32 rotlFixed(word32 x, word32 y)
  29. {
  30. return (x << y) | (x >> (sizeof(y) * 8 - y));
  31. }
  32. #endif /* rotlFixed */
  33. #ifndef rotrFixed
  34. static inline word32 rotrFixed(word32 x, word32 y)
  35. {
  36. return (x >> y) | (x << (sizeof(y) * 8 - y));
  37. }
  38. #endif /* rotrFixed */
  39. #ifndef min
  40. static inline word32 min(word32 a, word32 b)
  41. {
  42. return a > b ? b : a;
  43. }
  44. #endif /* min */
  45. static inline word32 ByteReverseWord32(word32 value)
  46. {
  47. /* 6 instructions with rotate instruction, 8 without */
  48. value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
  49. return rotlFixed(value, 16U);
  50. }
  51. static inline void ByteReverseWords(word32 *out, const word32 *in,
  52. word32 byteCount)
  53. {
  54. word32 count = byteCount/(word32)sizeof(word32), i;
  55. for (i = 0; i < count; i++)
  56. out[i] = ByteReverseWord32(in[i]);
  57. }
  58. int SHA256Init(SHA256 *sha256)
  59. {
  60. sha256->digest[0] = 0x6A09E667L;
  61. sha256->digest[1] = 0xBB67AE85L;
  62. sha256->digest[2] = 0x3C6EF372L;
  63. sha256->digest[3] = 0xA54FF53AL;
  64. sha256->digest[4] = 0x510E527FL;
  65. sha256->digest[5] = 0x9B05688CL;
  66. sha256->digest[6] = 0x1F83D9ABL;
  67. sha256->digest[7] = 0x5BE0CD19L;
  68. sha256->buffLen = 0;
  69. sha256->loLen = 0;
  70. sha256->hiLen = 0;
  71. return 0;
  72. }
  73. #define XTRANSFORM(S,B) Transform((S))
  74. static const word32 K[64] = {
  75. 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
  76. 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
  77. 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
  78. 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
  79. 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
  80. 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
  81. 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
  82. 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
  83. 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
  84. 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
  85. 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
  86. 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
  87. 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
  88. };
  89. #define Ch(x,y,z) (z ^ (x & (y ^ z)))
  90. #define Maj(x,y,z) (((x | y) & z) | (x & y))
  91. #define S(x, n) rotrFixed(x, n)
  92. #define R(x, n) (((x)&0xFFFFFFFFU)>>(n))
  93. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  94. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  95. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  96. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  97. #define RND(a,b,c,d,e,f,g,h,i) \
  98. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  99. t1 = Sigma0(a) + Maj(a, b, c); \
  100. d += t0; \
  101. h = t0 + t1;
  102. static int Transform(SHA256 *sha256)
  103. {
  104. word32 S[8], t0, t1;
  105. int i;
  106. word32 W[64];
  107. /* Copy context->state[] to working vars */
  108. for (i = 0; i < 8; i++)
  109. S[i] = sha256->digest[i];
  110. for (i = 0; i < 16; i++)
  111. W[i] = sha256->buffer[i];
  112. for (i = 16; i < 64; i++)
  113. W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
  114. for (i = 0; i < 64; i += 8) {
  115. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
  116. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
  117. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
  118. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
  119. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
  120. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
  121. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
  122. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
  123. }
  124. /* Add the working vars back into digest state[] */
  125. for (i = 0; i < 8; i++) {
  126. sha256->digest[i] += S[i];
  127. }
  128. return 0;
  129. }
  130. static inline void AddLength(SHA256 *sha256, word32 len)
  131. {
  132. word32 tmp = sha256->loLen;
  133. if ( (sha256->loLen += len) < tmp)
  134. sha256->hiLen++; /* carry low to high */
  135. }
  136. int SHA256Update(SHA256 *sha256, const byte *data, word32 len)
  137. {
  138. /* do block size increments */
  139. byte *local = (byte*)sha256->buffer;
  140. while (len) {
  141. word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen);
  142. XMEMCPY(&local[sha256->buffLen], data, add);
  143. sha256->buffLen += add;
  144. data += add;
  145. len -= add;
  146. if (sha256->buffLen == SHA256_BLOCK_SIZE) {
  147. int ret;
  148. ByteReverseWords(sha256->buffer, sha256->buffer,
  149. SHA256_BLOCK_SIZE);
  150. ret = XTRANSFORM(sha256, local);
  151. if (ret != 0)
  152. return ret;
  153. AddLength(sha256, SHA256_BLOCK_SIZE);
  154. sha256->buffLen = 0;
  155. }
  156. }
  157. return 0;
  158. }
  159. int SHA256Final(SHA256 *sha256, byte *hash)
  160. {
  161. byte *local = (byte*)sha256->buffer;
  162. int ret;
  163. AddLength(sha256, sha256->buffLen); /* before adding pads */
  164. local[sha256->buffLen++] = 0x80; /* add 1 */
  165. /* pad with zeros */
  166. if (sha256->buffLen > SHA256_PAD_SIZE) {
  167. XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen);
  168. sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen;
  169. ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);
  170. ret = XTRANSFORM(sha256, local);
  171. if (ret != 0)
  172. return ret;
  173. sha256->buffLen = 0;
  174. }
  175. XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen);
  176. /* put lengths in bits */
  177. sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) +
  178. (sha256->hiLen << 3);
  179. sha256->loLen = sha256->loLen << 3;
  180. /* store lengths */
  181. ByteReverseWords(sha256->buffer, sha256->buffer, SHA256_BLOCK_SIZE);
  182. /* ! length ordering dependent on digest endian type ! */
  183. XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
  184. XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
  185. sizeof(word32));
  186. ret = XTRANSFORM(sha256, local);
  187. if (ret != 0)
  188. return ret;
  189. ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE);
  190. XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE);
  191. return SHA256Init(sha256); /* reset state */
  192. }
  193. int SHA256Hash(const byte * data, word32 len, byte * hash)
  194. {
  195. int ret = 0;
  196. SHA256 sha256;
  197. if ((ret = SHA256Init(&sha256)) != 0) {
  198. printf("SHA256Init failed");
  199. }
  200. else if ((ret = SHA256Update(&sha256, data, len)) != 0) {
  201. printf("SHA256Update failed");
  202. }
  203. else if ((ret = SHA256Final(&sha256, hash)) != 0) {
  204. printf("SHA256Final failed");
  205. }
  206. return ret;
  207. }