sha1.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* sha1.cpp
  2. Copyright (c) 2005 Michael D. Leonhard
  3. http://tamale.net/
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #include "sha1.h"
  25. // print out memory in hexadecimal
  26. void SHA1::hexPrinter( unsigned char* c, int l )
  27. {
  28. assert( c );
  29. assert( l > 0 );
  30. while( l > 0 )
  31. {
  32. printf( " %02x", *c );
  33. l--;
  34. c++;
  35. }
  36. }
  37. // circular left bit rotation. MSB wraps around to LSB
  38. Uint32 SHA1::lrot( Uint32 x, int bits )
  39. {
  40. return (x<<bits) | (x>>(32 - bits));
  41. };
  42. // Save a 32-bit unsigned integer to memory, in big-endian order
  43. void SHA1::storeBigEndianUint32( unsigned char* byte, Uint32 num )
  44. {
  45. assert( byte );
  46. byte[0] = (unsigned char)(num>>24);
  47. byte[1] = (unsigned char)(num>>16);
  48. byte[2] = (unsigned char)(num>>8);
  49. byte[3] = (unsigned char)num;
  50. }
  51. // Constructor *******************************************************
  52. SHA1::SHA1()
  53. {
  54. // make sure that the data type is the right size
  55. assert( sizeof( Uint32 ) * 5 == 20 );
  56. // initialize
  57. H0 = 0x67452301;
  58. H1 = 0xefcdab89;
  59. H2 = 0x98badcfe;
  60. H3 = 0x10325476;
  61. H4 = 0xc3d2e1f0;
  62. unprocessedBytes = 0;
  63. size = 0;
  64. }
  65. // Destructor ********************************************************
  66. SHA1::~SHA1()
  67. {
  68. // erase data
  69. H0 = H1 = H2 = H3 = H4 = 0;
  70. for( int c = 0; c < 64; c++ ) bytes[c] = 0;
  71. unprocessedBytes = size = 0;
  72. }
  73. // process ***********************************************************
  74. void SHA1::process()
  75. {
  76. assert( unprocessedBytes == 64 );
  77. //printf( "process: " ); hexPrinter( bytes, 64 ); printf( "\n" );
  78. int t;
  79. Uint32 a, b, c, d, e, K, f, W[80];
  80. // starting values
  81. a = H0;
  82. b = H1;
  83. c = H2;
  84. d = H3;
  85. e = H4;
  86. // copy and expand the message block
  87. for( t = 0; t < 16; t++ ) W[t] = (bytes[t*4] << 24)
  88. +(bytes[t*4 + 1] << 16)
  89. +(bytes[t*4 + 2] << 8)
  90. + bytes[t*4 + 3];
  91. for(; t< 80; t++ ) W[t] = lrot( W[t-3]^W[t-8]^W[t-14]^W[t-16], 1 );
  92. /* main loop */
  93. Uint32 temp;
  94. for( t = 0; t < 80; t++ )
  95. {
  96. if( t < 20 ) {
  97. K = 0x5a827999;
  98. f = (b & c) | ((b ^ 0xFFFFFFFF) & d);//TODO: try using ~
  99. } else if( t < 40 ) {
  100. K = 0x6ed9eba1;
  101. f = b ^ c ^ d;
  102. } else if( t < 60 ) {
  103. K = 0x8f1bbcdc;
  104. f = (b & c) | (b & d) | (c & d);
  105. } else {
  106. K = 0xca62c1d6;
  107. f = b ^ c ^ d;
  108. }
  109. temp = lrot(a,5) + f + e + W[t] + K;
  110. e = d;
  111. d = c;
  112. c = lrot(b,30);
  113. b = a;
  114. a = temp;
  115. //printf( "t=%d %08x %08x %08x %08x %08x\n",t,a,b,c,d,e );
  116. }
  117. /* add variables */
  118. H0 += a;
  119. H1 += b;
  120. H2 += c;
  121. H3 += d;
  122. H4 += e;
  123. //printf( "Current: %08x %08x %08x %08x %08x\n",H0,H1,H2,H3,H4 );
  124. /* all bytes have been processed */
  125. unprocessedBytes = 0;
  126. }
  127. // addBytes **********************************************************
  128. void SHA1::addBytes( const char* data, int num )
  129. {
  130. assert( data );
  131. assert( num >= 0 );
  132. // add these bytes to the running total
  133. size += num;
  134. // repeat until all data is processed
  135. while( num > 0 )
  136. {
  137. // number of bytes required to complete block
  138. int needed = 64 - unprocessedBytes;
  139. assert( needed > 0 );
  140. // number of bytes to copy (use smaller of two)
  141. int toCopy = (num < needed) ? num : needed;
  142. // Copy the bytes
  143. memcpy( bytes + unprocessedBytes, data, toCopy );
  144. // Bytes have been copied
  145. num -= toCopy;
  146. data += toCopy;
  147. unprocessedBytes += toCopy;
  148. // there is a full block
  149. if( unprocessedBytes == 64 ) process();
  150. }
  151. }
  152. // digest ************************************************************
  153. unsigned char* SHA1::getDigest()
  154. {
  155. // save the message size
  156. Uint32 totalBitsL = size << 3;
  157. Uint32 totalBitsH = size >> 29;
  158. // add 0x80 to the message
  159. addBytes( "\x80", 1 );
  160. unsigned char footer[64] = {
  161. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  162. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  163. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  164. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  165. // block has no room for 8-byte filesize, so finish it
  166. if( unprocessedBytes > 56 )
  167. addBytes( (char*)footer, 64 - unprocessedBytes);
  168. assert( unprocessedBytes <= 56 );
  169. // how many zeros do we need
  170. int neededZeros = 56 - unprocessedBytes;
  171. // store file size (in bits) in big-endian format
  172. storeBigEndianUint32( footer + neededZeros , totalBitsH );
  173. storeBigEndianUint32( footer + neededZeros + 4, totalBitsL );
  174. // finish the final block
  175. addBytes( (char*)footer, neededZeros + 8 );
  176. // allocate memory for the digest bytes
  177. unsigned char* digest = (unsigned char*)malloc( 20 );
  178. // copy the digest bytes
  179. storeBigEndianUint32( digest, H0 );
  180. storeBigEndianUint32( digest + 4, H1 );
  181. storeBigEndianUint32( digest + 8, H2 );
  182. storeBigEndianUint32( digest + 12, H3 );
  183. storeBigEndianUint32( digest + 16, H4 );
  184. // return the digest
  185. return digest;
  186. }