sha512.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* sha512.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include "sha512.h"
  22. #include "api.h"
  23. #define XMEMSET memset
  24. #define XMEMCPY memcpy
  25. #ifndef rotlFixed64
  26. static inline word64 rotlFixed64(word64 x, word64 y)
  27. {
  28. return (x << y) | (x >> (sizeof(y) * 8 - y));
  29. }
  30. #endif /* rotlFixed64 */
  31. #ifndef rotrFixed64
  32. static inline word64 rotrFixed64(word64 x, word64 y)
  33. {
  34. return (x >> y) | (x << (sizeof(y) * 8 - y));
  35. }
  36. #endif /* rotrFixed */
  37. #ifndef min
  38. static inline word32 min(word32 a, word32 b)
  39. {
  40. return a > b ? b : a;
  41. }
  42. #endif /* min64 */
  43. int SHA512Init(SHA512 * sha512)
  44. {
  45. sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
  46. sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
  47. sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
  48. sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
  49. sha512->digest[4] = W64LIT(0x510e527fade682d1);
  50. sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
  51. sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
  52. sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
  53. sha512->buffLen = 0;
  54. sha512->loLen = 0;
  55. sha512->hiLen = 0;
  56. return 0;
  57. }
  58. static const word64 K512[80] = {
  59. W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
  60. W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
  61. W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
  62. W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
  63. W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
  64. W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
  65. W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
  66. W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
  67. W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
  68. W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
  69. W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
  70. W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
  71. W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
  72. W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
  73. W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
  74. W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
  75. W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
  76. W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
  77. W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
  78. W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
  79. W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
  80. W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
  81. W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
  82. W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
  83. W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
  84. W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
  85. W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
  86. W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
  87. W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
  88. W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
  89. W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
  90. W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
  91. W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
  92. W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
  93. W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
  94. W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
  95. W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
  96. W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
  97. W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
  98. W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
  99. };
  100. #define blk0(i) (W[i] = sha512->buffer[i])
  101. #define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
  102. #define Ch(x,y,z) (z^(x&(y^z)))
  103. #define Maj(x,y,z) ((x&y)|(z&(x|y)))
  104. #define a(i) T[(0-i)&7]
  105. #define b(i) T[(1-i)&7]
  106. #define c(i) T[(2-i)&7]
  107. #define d(i) T[(3-i)&7]
  108. #define e(i) T[(4-i)&7]
  109. #define f(i) T[(5-i)&7]
  110. #define g(i) T[(6-i)&7]
  111. #define h(i) T[(7-i)&7]
  112. #define S0(x) (rotrFixed64(x,28)^rotrFixed64(x,34)^rotrFixed64(x,39))
  113. #define S1(x) (rotrFixed64(x,14)^rotrFixed64(x,18)^rotrFixed64(x,41))
  114. #define s0(x) (rotrFixed64(x,1)^rotrFixed64(x,8)^(x>>7))
  115. #define s1(x) (rotrFixed64(x,19)^rotrFixed64(x,61)^(x>>6))
  116. #define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
  117. d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
  118. #define blk384(i) (W[i] = sha384->buffer[i])
  119. #define R2(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk384(i));\
  120. d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
  121. static int Transform(SHA512 * sha512)
  122. {
  123. const word64* K = K512;
  124. word32 j;
  125. word64 T[8];
  126. #ifdef CYASSL_SMALL_STACK
  127. word64* W;
  128. W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  129. if (W == NULL)
  130. return MEMORY_E;
  131. #else
  132. word64 W[16];
  133. #endif
  134. /* Copy digest to working vars */
  135. XMEMCPY(T, sha512->digest, sizeof(T));
  136. #ifdef USE_SLOW_SHA2
  137. /* over twice as small, but 50% slower */
  138. /* 80 operations, not unrolled */
  139. for (j = 0; j < 80; j += 16) {
  140. int m;
  141. for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
  142. R(m);
  143. }
  144. }
  145. #else
  146. /* 80 operations, partially loop unrolled */
  147. for (j = 0; j < 80; j += 16) {
  148. R( 0); R( 1); R( 2); R( 3);
  149. R( 4); R( 5); R( 6); R( 7);
  150. R( 8); R( 9); R(10); R(11);
  151. R(12); R(13); R(14); R(15);
  152. }
  153. #endif /* USE_SLOW_SHA2 */
  154. /* Add the working vars back into digest */
  155. sha512->digest[0] += a(0);
  156. sha512->digest[1] += b(0);
  157. sha512->digest[2] += c(0);
  158. sha512->digest[3] += d(0);
  159. sha512->digest[4] += e(0);
  160. sha512->digest[5] += f(0);
  161. sha512->digest[6] += g(0);
  162. sha512->digest[7] += h(0);
  163. /* Wipe variables */
  164. XMEMSET(W, 0, sizeof(word64) * 16);
  165. XMEMSET(T, 0, sizeof(T));
  166. #ifdef CYASSL_SMALL_STACK
  167. XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  168. #endif
  169. return 0;
  170. }
  171. static inline void AddLength(SHA512 * sha512, word32 len)
  172. {
  173. word32 tmp = sha512->loLen;
  174. if ( (sha512->loLen += len) < tmp)
  175. sha512->hiLen++; /* carry low to high */
  176. }
  177. int SHA512Update(SHA512 * sha512, const byte * data, word32 len)
  178. {
  179. /* do block size increments */
  180. byte * local = (byte *)sha512->buffer;
  181. while (len) {
  182. word32 add = min(len, SHA512_BLOCK_SIZE - sha512->buffLen);
  183. XMEMCPY(&local[sha512->buffLen], data, add);
  184. sha512->buffLen += add;
  185. data += add;
  186. len -= add;
  187. if (sha512->buffLen == SHA512_BLOCK_SIZE) {
  188. int ret;
  189. #ifdef LITTLE_ENDIAN_ORDER
  190. ByteReverseWords64(sha512->buffer, sha512->buffer,
  191. SHA512_BLOCK_SIZE);
  192. #endif
  193. ret = Transform(sha512);
  194. if (ret != 0)
  195. return ret;
  196. AddLength(sha512, SHA512_BLOCK_SIZE);
  197. sha512->buffLen = 0;
  198. }
  199. }
  200. return 0;
  201. }
  202. int SHA512Final(SHA512 * sha512, byte * hash)
  203. {
  204. byte * local = (byte*)sha512->buffer;
  205. int ret;
  206. AddLength(sha512, sha512->buffLen); /* before adding pads */
  207. local[sha512->buffLen++] = 0x80; /* add 1 */
  208. /* pad with zeros */
  209. if (sha512->buffLen > SHA512_PAD_SIZE) {
  210. XMEMSET(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE -sha512->buffLen);
  211. sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen;
  212. #ifdef LITTLE_ENDIAN_ORDER
  213. ByteReverseWords64(sha512->buffer,sha512->buffer,SHA512_BLOCK_SIZE);
  214. #endif
  215. ret = Transform(sha512);
  216. if (ret != 0)
  217. return ret;
  218. sha512->buffLen = 0;
  219. }
  220. XMEMSET(&local[sha512->buffLen], 0, SHA512_PAD_SIZE - sha512->buffLen);
  221. /* put lengths in bits */
  222. sha512->hiLen = (sha512->loLen >> (8*sizeof(sha512->loLen) - 3)) +
  223. (sha512->hiLen << 3);
  224. sha512->loLen = sha512->loLen << 3;
  225. /* store lengths */
  226. #ifdef LITTLE_ENDIAN_ORDER
  227. ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE);
  228. #endif
  229. /* ! length ordering dependent on digest endian type ! */
  230. sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
  231. sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
  232. ret = Transform(sha512);
  233. if (ret != 0)
  234. return ret;
  235. #ifdef LITTLE_ENDIAN_ORDER
  236. ByteReverseWords64(sha512->digest, sha512->digest, SHA512_DIGEST_SIZE);
  237. #endif
  238. XMEMCPY(hash, sha512->digest, SHA512_DIGEST_SIZE);
  239. return SHA512Init(sha512); /* reset state */
  240. }
  241. int SHA512Hash(const byte * data, word32 len, byte * hash)
  242. {
  243. int ret = 0;
  244. #ifdef CYASSL_SMALL_STACK
  245. SHA512* sha512;
  246. #else
  247. SHA512 sha512[1];
  248. #endif
  249. #ifdef CYASSL_SMALL_STACK
  250. sha512 = (SHA512*)XMALLOC(sizeof(SHA512), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  251. if (sha512 == NULL)
  252. return MEMORY_E;
  253. #endif
  254. if ((ret = SHA512Init(sha512)) != 0) {
  255. printf("SHA512Init failed");
  256. }
  257. else if ((ret = SHA512Update(sha512, data, len)) != 0) {
  258. printf("SHA512Update failed");
  259. }
  260. else if ((ret = SHA512Final(sha512, hash)) != 0) {
  261. printf("SHA512Final failed");
  262. }
  263. #ifdef CYASSL_SMALL_STACK
  264. XFREE(sha512, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  265. #endif
  266. return ret;
  267. }