sha.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* sha.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. #include "ctc_sha.h"
  22. #ifdef NO_INLINE
  23. #include "misc.h"
  24. #else
  25. #include "misc.c"
  26. #endif
  27. #ifndef min
  28. static INLINE word32 min(word32 a, word32 b)
  29. {
  30. return a > b ? b : a;
  31. }
  32. #endif /* min */
  33. void InitSha(Sha* sha)
  34. {
  35. sha->digest[0] = 0x67452301L;
  36. sha->digest[1] = 0xEFCDAB89L;
  37. sha->digest[2] = 0x98BADCFEL;
  38. sha->digest[3] = 0x10325476L;
  39. sha->digest[4] = 0xC3D2E1F0L;
  40. sha->buffLen = 0;
  41. sha->loLen = 0;
  42. sha->hiLen = 0;
  43. }
  44. #define blk0(i) (W[i] = sha->buffer[i])
  45. #define blk1(i) (W[i&15] = \
  46. rotlFixed(W[(i+13)&15]^W[(i+8)&15]^W[(i+2)&15]^W[i&15],1))
  47. #define f1(x,y,z) (z^(x &(y^z)))
  48. #define f2(x,y,z) (x^y^z)
  49. #define f3(x,y,z) ((x&y)|(z&(x|y)))
  50. #define f4(x,y,z) (x^y^z)
  51. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  52. #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + blk0(i) + 0x5A827999+ \
  53. rotlFixed(v,5); w = rotlFixed(w,30);
  54. #define R1(v,w,x,y,z,i) z+= f1(w,x,y) + blk1(i) + 0x5A827999+ \
  55. rotlFixed(v,5); w = rotlFixed(w,30);
  56. #define R2(v,w,x,y,z,i) z+= f2(w,x,y) + blk1(i) + 0x6ED9EBA1+ \
  57. rotlFixed(v,5); w = rotlFixed(w,30);
  58. #define R3(v,w,x,y,z,i) z+= f3(w,x,y) + blk1(i) + 0x8F1BBCDC+ \
  59. rotlFixed(v,5); w = rotlFixed(w,30);
  60. #define R4(v,w,x,y,z,i) z+= f4(w,x,y) + blk1(i) + 0xCA62C1D6+ \
  61. rotlFixed(v,5); w = rotlFixed(w,30);
  62. static void Transform(Sha* sha)
  63. {
  64. word32 W[SHA_BLOCK_SIZE / sizeof(word32)];
  65. /* Copy context->state[] to working vars */
  66. word32 a = sha->digest[0];
  67. word32 b = sha->digest[1];
  68. word32 c = sha->digest[2];
  69. word32 d = sha->digest[3];
  70. word32 e = sha->digest[4];
  71. /* nearly 1 K bigger in code size but 25% faster */
  72. /* 4 rounds of 20 operations each. Loop unrolled. */
  73. R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
  74. R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
  75. R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
  76. R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
  77. R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
  78. R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
  79. R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
  80. R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
  81. R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
  82. R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
  83. R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
  84. R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
  85. R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
  86. R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
  87. R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
  88. R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
  89. R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
  90. R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
  91. R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
  92. R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
  93. /* Add the working vars back into digest state[] */
  94. sha->digest[0] += a;
  95. sha->digest[1] += b;
  96. sha->digest[2] += c;
  97. sha->digest[3] += d;
  98. sha->digest[4] += e;
  99. }
  100. static INLINE void AddLength(Sha* sha, word32 len)
  101. {
  102. word32 tmp = sha->loLen;
  103. if ( (sha->loLen += len) < tmp)
  104. sha->hiLen++; /* carry low to high */
  105. }
  106. void ShaUpdate(Sha* sha, const byte* data, word32 len)
  107. {
  108. /* do block size increments */
  109. byte* local = (byte*)sha->buffer;
  110. while (len) {
  111. word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
  112. XMEMCPY(&local[sha->buffLen], data, add);
  113. sha->buffLen += add;
  114. data += add;
  115. len -= add;
  116. if (sha->buffLen == SHA_BLOCK_SIZE) {
  117. #ifdef LITTLE_ENDIAN_ORDER
  118. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  119. #endif
  120. Transform(sha);
  121. AddLength(sha, SHA_BLOCK_SIZE);
  122. sha->buffLen = 0;
  123. }
  124. }
  125. }
  126. void ShaFinal(Sha* sha, byte* hash)
  127. {
  128. byte* local = (byte*)sha->buffer;
  129. AddLength(sha, sha->buffLen); /* before adding pads */
  130. local[sha->buffLen++] = 0x80; /* add 1 */
  131. /* pad with zeros */
  132. if (sha->buffLen > SHA_PAD_SIZE) {
  133. XMEMSET(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
  134. sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
  135. #ifdef LITTLE_ENDIAN_ORDER
  136. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  137. #endif
  138. Transform(sha);
  139. sha->buffLen = 0;
  140. }
  141. XMEMSET(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
  142. /* put lengths in bits */
  143. sha->loLen = sha->loLen << 3;
  144. sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
  145. (sha->hiLen << 3);
  146. /* store lengths */
  147. #ifdef LITTLE_ENDIAN_ORDER
  148. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  149. #endif
  150. /* ! length ordering dependent on digest endian type ! */
  151. XMEMCPY(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
  152. XMEMCPY(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
  153. Transform(sha);
  154. #ifdef LITTLE_ENDIAN_ORDER
  155. ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
  156. #endif
  157. XMEMCPY(hash, sha->digest, SHA_DIGEST_SIZE);
  158. InitSha(sha); /* reset state */
  159. }