PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/cyassl-1.2.0/ctaocrypt/src/sha.c

https://bitbucket.org/mkramer/cyassl
C | 203 lines | 135 code | 40 blank | 28 comment | 11 complexity | 66c3282d5066ba491338a584e8809ebe MD5 | raw file
Possible License(s): GPL-2.0
  1. /* sha.c
  2. *
  3. * Copyright (C) 2006-2009 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 "sha.h"
  22. #ifdef NO_INLINE
  23. #include "misc.h"
  24. #else
  25. #include "misc.c"
  26. #endif
  27. #include <string.h>
  28. #include <assert.h>
  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 InitSha(Sha* sha)
  36. {
  37. sha->digest[0] = 0x67452301L;
  38. sha->digest[1] = 0xEFCDAB89L;
  39. sha->digest[2] = 0x98BADCFEL;
  40. sha->digest[3] = 0x10325476L;
  41. sha->digest[4] = 0xC3D2E1F0L;
  42. sha->buffLen = 0;
  43. sha->loLen = 0;
  44. sha->hiLen = 0;
  45. }
  46. #define f1(x,y,z) (z^(x &(y^z)))
  47. #define f2(x,y,z) (x^y^z)
  48. #define f3(x,y,z) ((x&y)|(z&(x|y)))
  49. #define f4(x,y,z) (x^y^z)
  50. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  51. #define R0(v,w,x,y,z,i) z+= f1(w,x,y) + W[i] + 0x5A827999+ rotlFixed(v,5); w = rotlFixed(w,30);
  52. #define R1(v,w,x,y,z,i) z+= f1(w,x,y) + W[i] + 0x5A827999+ rotlFixed(v,5); w = rotlFixed(w,30);
  53. #define R2(v,w,x,y,z,i) z+= f2(w,x,y) + W[i] + 0x6ED9EBA1+ rotlFixed(v,5); w = rotlFixed(w,30);
  54. #define R3(v,w,x,y,z,i) z+= f3(w,x,y) + W[i] + 0x8F1BBCDC+ rotlFixed(v,5); w = rotlFixed(w,30);
  55. #define R4(v,w,x,y,z,i) z+= f4(w,x,y) + W[i] + 0xCA62C1D6+ rotlFixed(v,5); w = rotlFixed(w,30);
  56. static void Transform(Sha* sha)
  57. {
  58. word32 W[80], i;
  59. /* Copy context->state[] to working vars */
  60. word32 a = sha->digest[0];
  61. word32 b = sha->digest[1];
  62. word32 c = sha->digest[2];
  63. word32 d = sha->digest[3];
  64. word32 e = sha->digest[4];
  65. for (i = 0; i < 16; i++)
  66. W[i] = sha->buffer[i];
  67. for (i = 16; i < 80; i++)
  68. W[i] = rotlFixed(W[i-3]^W[i-8]^W[i-14]^W[i-16],1);
  69. /* 4 rounds of 20 operations each. */
  70. for (i = 0; i < 20; ) {
  71. R0(a,b,c,d,e,i); i++;
  72. R0(e,a,b,c,d,i); i++;
  73. R0(d,e,a,b,c,i); i++;
  74. R0(c,d,e,a,b,i); i++;
  75. R0(b,c,d,e,a,i); i++;
  76. }
  77. for (i = 20; i < 40; ) {
  78. R2(a,b,c,d,e,i); i++;
  79. R2(e,a,b,c,d,i); i++;
  80. R2(d,e,a,b,c,i); i++;
  81. R2(c,d,e,a,b,i); i++;
  82. R2(b,c,d,e,a,i); i++;
  83. }
  84. for (i = 40; i < 60; ) {
  85. R3(a,b,c,d,e,i); i++;
  86. R3(e,a,b,c,d,i); i++;
  87. R3(d,e,a,b,c,i); i++;
  88. R3(c,d,e,a,b,i); i++;
  89. R3(b,c,d,e,a,i); i++;
  90. }
  91. for (i = 60; i < 80; ) {
  92. R4(a,b,c,d,e,i); i++;
  93. R4(e,a,b,c,d,i); i++;
  94. R4(d,e,a,b,c,i); i++;
  95. R4(c,d,e,a,b,i); i++;
  96. R4(b,c,d,e,a,i); i++;
  97. }
  98. /* Add the working vars back into digest state[] */
  99. sha->digest[0] += a;
  100. sha->digest[1] += b;
  101. sha->digest[2] += c;
  102. sha->digest[3] += d;
  103. sha->digest[4] += e;
  104. }
  105. static INLINE void AddLength(Sha* sha, word32 len)
  106. {
  107. word32 tmp = sha->loLen;
  108. if ( (sha->loLen += len) < tmp)
  109. sha->hiLen++; /* carry low to high */
  110. }
  111. void ShaUpdate(Sha* sha, const byte* data, word32 len)
  112. {
  113. /* do block size increments */
  114. byte* local = (byte*)sha->buffer;
  115. while (len) {
  116. word32 add = min(len, SHA_BLOCK_SIZE - sha->buffLen);
  117. memcpy(&local[sha->buffLen], data, add);
  118. sha->buffLen += add;
  119. data += add;
  120. len -= add;
  121. if (sha->buffLen == SHA_BLOCK_SIZE) {
  122. #ifdef LITTLE_ENDIAN_ORDER
  123. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  124. #endif
  125. Transform(sha);
  126. AddLength(sha, SHA_BLOCK_SIZE);
  127. sha->buffLen = 0;
  128. }
  129. }
  130. }
  131. void ShaFinal(Sha* sha, byte* hash)
  132. {
  133. byte* local = (byte*)sha->buffer;
  134. AddLength(sha, sha->buffLen); /* before adding pads */
  135. local[sha->buffLen++] = 0x80; /* add 1 */
  136. /* pad with zeros */
  137. if (sha->buffLen > SHA_PAD_SIZE) {
  138. memset(&local[sha->buffLen], 0, SHA_BLOCK_SIZE - sha->buffLen);
  139. sha->buffLen += SHA_BLOCK_SIZE - sha->buffLen;
  140. #ifdef LITTLE_ENDIAN_ORDER
  141. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  142. #endif
  143. Transform(sha);
  144. sha->buffLen = 0;
  145. }
  146. memset(&local[sha->buffLen], 0, SHA_PAD_SIZE - sha->buffLen);
  147. /* put lengths in bits */
  148. sha->loLen = sha->loLen << 3;
  149. sha->hiLen = (sha->loLen >> (8*sizeof(sha->loLen) - 3)) +
  150. (sha->hiLen << 3);
  151. /* store lengths */
  152. #ifdef LITTLE_ENDIAN_ORDER
  153. ByteReverseBytes(local, local, SHA_BLOCK_SIZE);
  154. #endif
  155. memcpy(&local[SHA_PAD_SIZE], &sha->hiLen, sizeof(word32));
  156. memcpy(&local[SHA_PAD_SIZE + sizeof(word32)], &sha->loLen, sizeof(word32));
  157. Transform(sha);
  158. #ifdef LITTLE_ENDIAN_ORDER
  159. ByteReverseWords(sha->digest, sha->digest, SHA_DIGEST_SIZE);
  160. #endif
  161. memcpy(hash, sha->digest, SHA_DIGEST_SIZE);
  162. InitSha(sha); /* reset state */
  163. }