sha256.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* sha256.c
  2. *
  3. * Copyright (C) 2006-2011 Sawtooth Consulting Ltd.
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. /* code submitted by raphael.huck@efixo.com */
  22. #ifndef NO_SHA256
  23. #include "sha256.h"
  24. #ifdef NO_INLINE
  25. #include "misc.h"
  26. #else
  27. #include "misc.c"
  28. #endif
  29. #ifndef min
  30. static INLINE word32 min(word32 a, word32 b)
  31. {
  32. return a > b ? b : a;
  33. }
  34. #endif /* min */
  35. void InitSha256(Sha256* sha256)
  36. {
  37. sha256->digest[0] = 0x6A09E667L;
  38. sha256->digest[1] = 0xBB67AE85L;
  39. sha256->digest[2] = 0x3C6EF372L;
  40. sha256->digest[3] = 0xA54FF53AL;
  41. sha256->digest[4] = 0x510E527FL;
  42. sha256->digest[5] = 0x9B05688CL;
  43. sha256->digest[6] = 0x1F83D9ABL;
  44. sha256->digest[7] = 0x5BE0CD19L;
  45. sha256->buffLen = 0;
  46. sha256->loLen = 0;
  47. sha256->hiLen = 0;
  48. }
  49. static const word32 K[64] = {
  50. 0x428A2F98L, 0x71374491L, 0xB5C0FBCFL, 0xE9B5DBA5L, 0x3956C25BL,
  51. 0x59F111F1L, 0x923F82A4L, 0xAB1C5ED5L, 0xD807AA98L, 0x12835B01L,
  52. 0x243185BEL, 0x550C7DC3L, 0x72BE5D74L, 0x80DEB1FEL, 0x9BDC06A7L,
  53. 0xC19BF174L, 0xE49B69C1L, 0xEFBE4786L, 0x0FC19DC6L, 0x240CA1CCL,
  54. 0x2DE92C6FL, 0x4A7484AAL, 0x5CB0A9DCL, 0x76F988DAL, 0x983E5152L,
  55. 0xA831C66DL, 0xB00327C8L, 0xBF597FC7L, 0xC6E00BF3L, 0xD5A79147L,
  56. 0x06CA6351L, 0x14292967L, 0x27B70A85L, 0x2E1B2138L, 0x4D2C6DFCL,
  57. 0x53380D13L, 0x650A7354L, 0x766A0ABBL, 0x81C2C92EL, 0x92722C85L,
  58. 0xA2BFE8A1L, 0xA81A664BL, 0xC24B8B70L, 0xC76C51A3L, 0xD192E819L,
  59. 0xD6990624L, 0xF40E3585L, 0x106AA070L, 0x19A4C116L, 0x1E376C08L,
  60. 0x2748774CL, 0x34B0BCB5L, 0x391C0CB3L, 0x4ED8AA4AL, 0x5B9CCA4FL,
  61. 0x682E6FF3L, 0x748F82EEL, 0x78A5636FL, 0x84C87814L, 0x8CC70208L,
  62. 0x90BEFFFAL, 0xA4506CEBL, 0xBEF9A3F7L, 0xC67178F2L
  63. };
  64. #define Ch(x,y,z) (z ^ (x & (y ^ z)))
  65. #define Maj(x,y,z) (((x | y) & z) | (x & y))
  66. #define S(x, n) rotrFixed(x, n)
  67. #define R(x, n) (((x)&0xFFFFFFFFL)>>(n))
  68. #define Sigma0(x) (S(x, 2) ^ S(x, 13) ^ S(x, 22))
  69. #define Sigma1(x) (S(x, 6) ^ S(x, 11) ^ S(x, 25))
  70. #define Gamma0(x) (S(x, 7) ^ S(x, 18) ^ R(x, 3))
  71. #define Gamma1(x) (S(x, 17) ^ S(x, 19) ^ R(x, 10))
  72. #define RND(a,b,c,d,e,f,g,h,i) \
  73. t0 = h + Sigma1(e) + Ch(e, f, g) + K[i] + W[i]; \
  74. t1 = Sigma0(a) + Maj(a, b, c); \
  75. d += t0; \
  76. h = t0 + t1;
  77. static void Transform(Sha256* sha256)
  78. {
  79. word32 S[8], W[64], t0, t1;
  80. int i;
  81. /* Copy context->state[] to working vars */
  82. for (i = 0; i < 8; i++)
  83. S[i] = sha256->digest[i];
  84. for (i = 0; i < 16; i++)
  85. W[i] = sha256->buffer[i];
  86. for (i = 16; i < 64; i++)
  87. W[i] = Gamma1(W[i-2]) + W[i-7] + Gamma0(W[i-15]) + W[i-16];
  88. for (i = 0; i < 64; i += 8) {
  89. RND(S[0],S[1],S[2],S[3],S[4],S[5],S[6],S[7],i+0);
  90. RND(S[7],S[0],S[1],S[2],S[3],S[4],S[5],S[6],i+1);
  91. RND(S[6],S[7],S[0],S[1],S[2],S[3],S[4],S[5],i+2);
  92. RND(S[5],S[6],S[7],S[0],S[1],S[2],S[3],S[4],i+3);
  93. RND(S[4],S[5],S[6],S[7],S[0],S[1],S[2],S[3],i+4);
  94. RND(S[3],S[4],S[5],S[6],S[7],S[0],S[1],S[2],i+5);
  95. RND(S[2],S[3],S[4],S[5],S[6],S[7],S[0],S[1],i+6);
  96. RND(S[1],S[2],S[3],S[4],S[5],S[6],S[7],S[0],i+7);
  97. }
  98. /* Add the working vars back into digest state[] */
  99. for (i = 0; i < 8; i++) {
  100. sha256->digest[i] += S[i];
  101. }
  102. }
  103. static INLINE void AddLength(Sha256* sha256, word32 len)
  104. {
  105. word32 tmp = sha256->loLen;
  106. if ( (sha256->loLen += len) < tmp)
  107. sha256->hiLen++; /* carry low to high */
  108. }
  109. void Sha256Update(Sha256* sha256, const byte* data, word32 len)
  110. {
  111. /* do block size increments */
  112. byte* local = (byte*)sha256->buffer;
  113. while (len) {
  114. word32 add = min(len, SHA256_BLOCK_SIZE - sha256->buffLen);
  115. XMEMCPY(&local[sha256->buffLen], data, add);
  116. sha256->buffLen += add;
  117. data += add;
  118. len -= add;
  119. if (sha256->buffLen == SHA256_BLOCK_SIZE) {
  120. #ifdef LITTLE_ENDIAN_ORDER
  121. ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
  122. #endif
  123. Transform(sha256);
  124. AddLength(sha256, SHA256_BLOCK_SIZE);
  125. sha256->buffLen = 0;
  126. }
  127. }
  128. }
  129. void Sha256Final(Sha256* sha256, byte* hash)
  130. {
  131. byte* local = (byte*)sha256->buffer;
  132. AddLength(sha256, sha256->buffLen); /* before adding pads */
  133. local[sha256->buffLen++] = 0x80; /* add 1 */
  134. /* pad with zeros */
  135. if (sha256->buffLen > SHA256_PAD_SIZE) {
  136. XMEMSET(&local[sha256->buffLen], 0, SHA256_BLOCK_SIZE - sha256->buffLen);
  137. sha256->buffLen += SHA256_BLOCK_SIZE - sha256->buffLen;
  138. #ifdef LITTLE_ENDIAN_ORDER
  139. ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
  140. #endif
  141. Transform(sha256);
  142. sha256->buffLen = 0;
  143. }
  144. XMEMSET(&local[sha256->buffLen], 0, SHA256_PAD_SIZE - sha256->buffLen);
  145. /* put lengths in bits */
  146. sha256->loLen = sha256->loLen << 3;
  147. sha256->hiLen = (sha256->loLen >> (8*sizeof(sha256->loLen) - 3)) +
  148. (sha256->hiLen << 3);
  149. /* store lengths */
  150. #ifdef LITTLE_ENDIAN_ORDER
  151. ByteReverseBytes(local, local, SHA256_BLOCK_SIZE);
  152. #endif
  153. /* ! length ordering dependent on digest endian type ! */
  154. XMEMCPY(&local[SHA256_PAD_SIZE], &sha256->hiLen, sizeof(word32));
  155. XMEMCPY(&local[SHA256_PAD_SIZE + sizeof(word32)], &sha256->loLen,
  156. sizeof(word32));
  157. Transform(sha256);
  158. #ifdef LITTLE_ENDIAN_ORDER
  159. ByteReverseWords(sha256->digest, sha256->digest, SHA256_DIGEST_SIZE);
  160. #endif
  161. XMEMCPY(hash, sha256->digest, SHA256_DIGEST_SIZE);
  162. InitSha256(sha256); /* reset state */
  163. }
  164. #endif /* NO_SHA256 */