sha1.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #include "rar.hpp"
  2. /*
  3. SHA-1 in C
  4. By Steve Reid <steve@edmweb.com>
  5. 100% Public Domain
  6. Test Vectors (from FIPS PUB 180-1)
  7. "abc"
  8. A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
  9. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  10. 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
  11. A million repetitions of "a"
  12. 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
  13. */
  14. #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
  15. #if defined(_M_IX86) || defined(_M_I86) || defined(__alpha)
  16. #define LITTLE_ENDIAN
  17. #else
  18. #error "LITTLE_ENDIAN or BIG_ENDIAN must be defined"
  19. #endif
  20. #endif
  21. /* #define SHA1HANDSOFF * Copies data before messing with it. */
  22. #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
  23. /* blk0() and blk() perform the initial expand. */
  24. /* I got the idea of expanding during the round function from SSLeay */
  25. #ifdef LITTLE_ENDIAN
  26. #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
  27. |(rol(block->l[i],8)&0x00FF00FF))
  28. #else
  29. #define blk0(i) block->l[i]
  30. #endif
  31. #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
  32. ^block->l[(i+2)&15]^block->l[i&15],1))
  33. /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
  34. #define R0(v,w,x,y,z,i) {z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);}
  35. #define R1(v,w,x,y,z,i) {z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);}
  36. #define R2(v,w,x,y,z,i) {z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);}
  37. #define R3(v,w,x,y,z,i) {z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);}
  38. #define R4(v,w,x,y,z,i) {z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);}
  39. #ifdef _MSC_VER
  40. #pragma optimize( "", off )
  41. // We need to disable the optimization to really wipe these variables.
  42. #endif
  43. static void wipevars(uint32 &a,uint32 &b,uint32 &c,uint32 &d,uint32 &e)
  44. {
  45. // Wipe used variables for safety reason.
  46. a=b=c=d=e=0;
  47. }
  48. #ifdef _MSC_VER
  49. #pragma optimize( "", on )
  50. #endif
  51. /* Hash a single 512-bit block. This is the core of the algorithm. */
  52. void SHA1Transform(uint32 state[5], unsigned char workspace[64], unsigned char buffer[64], bool handsoff)
  53. {
  54. #ifndef SFX_MODULE
  55. uint32 a, b, c, d, e;
  56. #endif
  57. typedef union {
  58. unsigned char c[64];
  59. uint32 l[16];
  60. } CHAR64LONG16;
  61. CHAR64LONG16* block;
  62. if (handsoff)
  63. {
  64. block = (CHAR64LONG16*)workspace;
  65. memcpy(block, buffer, 64);
  66. }
  67. else
  68. block = (CHAR64LONG16*)buffer;
  69. #ifdef SFX_MODULE
  70. static int pos[80][5];
  71. static bool pinit=false;
  72. if (!pinit)
  73. {
  74. for (int I=0,P=0;I<80;I++,P=(P ? P-1:4))
  75. {
  76. pos[I][0]=P;
  77. pos[I][1]=(P+1)%5;
  78. pos[I][2]=(P+2)%5;
  79. pos[I][3]=(P+3)%5;
  80. pos[I][4]=(P+4)%5;
  81. }
  82. pinit=true;
  83. }
  84. uint32 s[5];
  85. for (int I=0;I<sizeof(s)/sizeof(s[0]);I++)
  86. s[I]=state[I];
  87. for (int I=0;I<16;I++)
  88. R0(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
  89. for (int I=16;I<20;I++)
  90. R1(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
  91. for (int I=20;I<40;I++)
  92. R2(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
  93. for (int I=40;I<60;I++)
  94. R3(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
  95. for (int I=60;I<80;I++)
  96. R4(s[pos[I][0]],s[pos[I][1]],s[pos[I][2]],s[pos[I][3]],s[pos[I][4]],I);
  97. for (int I=0;I<sizeof(s)/sizeof(s[0]);I++)
  98. state[I]+=s[I];
  99. #else
  100. /* Copy context->state[] to working vars */
  101. a = state[0];
  102. b = state[1];
  103. c = state[2];
  104. d = state[3];
  105. e = state[4];
  106. /* 4 rounds of 20 operations each. Loop unrolled. */
  107. 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);
  108. 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);
  109. 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);
  110. 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);
  111. 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);
  112. 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);
  113. 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);
  114. 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);
  115. 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);
  116. 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);
  117. 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);
  118. 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);
  119. 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);
  120. 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);
  121. 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);
  122. 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);
  123. 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);
  124. 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);
  125. 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);
  126. 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);
  127. /* Add the working vars back into context.state[] */
  128. state[0] += a;
  129. state[1] += b;
  130. state[2] += c;
  131. state[3] += d;
  132. state[4] += e;
  133. /* Wipe variables */
  134. // Such wipe method does not work in optimizing compilers.
  135. // a = b = c = d = e = 0;
  136. // memset(&a,0,sizeof(a));
  137. wipevars(a,b,c,d,e);
  138. #endif
  139. }
  140. /* Initialize new context */
  141. void hash_initial(hash_context* context)
  142. {
  143. /* SHA1 initialization constants */
  144. context->state[0] = 0x67452301;
  145. context->state[1] = 0xEFCDAB89;
  146. context->state[2] = 0x98BADCFE;
  147. context->state[3] = 0x10325476;
  148. context->state[4] = 0xC3D2E1F0;
  149. context->count[0] = context->count[1] = 0;
  150. }
  151. /* Run your data through this. */
  152. void hash_process( hash_context * context, unsigned char * data, size_t len,
  153. bool handsoff )
  154. {
  155. unsigned int i, j;
  156. uint blen = ((uint)len)<<3;
  157. j = (context->count[0] >> 3) & 63;
  158. if ((context->count[0] += blen) < blen ) context->count[1]++;
  159. context->count[1] += (uint32)(len >> 29);
  160. if ((j + len) > 63) {
  161. memcpy(&context->buffer[j], data, (i = 64-j));
  162. SHA1Transform(context->state, context->workspace, context->buffer, handsoff);
  163. for ( ; i + 63 < len; i += 64) {
  164. #ifdef ALLOW_NOT_ALIGNED_INT
  165. SHA1Transform(context->state, context->workspace, &data[i], handsoff);
  166. #else
  167. unsigned char buffer[64];
  168. memcpy(buffer,data+i,sizeof(buffer));
  169. SHA1Transform(context->state, context->workspace, buffer, handsoff);
  170. memcpy(data+i,buffer,sizeof(buffer));
  171. #endif
  172. #ifdef BIG_ENDIAN
  173. if (!handsoff)
  174. {
  175. unsigned char *d=data+i;
  176. for (int k=0;k<64;k+=4)
  177. {
  178. byte b0=d[k],b1=d[k+1];
  179. d[k]=d[k+3];
  180. d[k+1]=d[k+2];
  181. d[k+2]=b1;
  182. d[k+3]=b0;
  183. }
  184. }
  185. #endif
  186. }
  187. j = 0;
  188. }
  189. else i = 0;
  190. if (len > i)
  191. memcpy(&context->buffer[j], &data[i], len - i);
  192. }
  193. /* Add padding and return the message digest. */
  194. void hash_final( hash_context* context, uint32 digest[5], bool handsoff)
  195. {
  196. uint i, j;
  197. unsigned char finalcount[8];
  198. for (i = 0; i < 8; i++) {
  199. finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)]
  200. >> ((3-(i & 3)) * 8) ) & 255); /* Endian independent */
  201. }
  202. unsigned char ch=(unsigned char)'\200';
  203. hash_process(context, &ch, 1, handsoff);
  204. while ((context->count[0] & 504) != 448) {
  205. ch=0;
  206. hash_process(context, &ch, 1, handsoff);
  207. }
  208. hash_process(context, finalcount, 8, handsoff); /* Should cause a SHA1Transform() */
  209. for (i = 0; i < 5; i++) {
  210. digest[i] = context->state[i] & 0xffffffff;
  211. }
  212. /* Wipe variables */
  213. memset(&i,0,sizeof(i));
  214. memset(&j,0,sizeof(j));
  215. memset(context->buffer, 0, 64);
  216. memset(context->state, 0, 20);
  217. memset(context->count, 0, 8);
  218. memset(&finalcount, 0, 8);
  219. if (handsoff)
  220. memset(context->workspace,0,sizeof(context->workspace)); // Wipe the temporary buffer.
  221. // SHA1Transform(context->state, context->workspace, context->buffer, true); /* make SHA1Transform overwrite it's own static vars */
  222. }