SHA.CPP 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/SHA.CPP 1 3/03/97 10:25a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : SHA.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/03/96 *
  26. * *
  27. * Last Update : July 3, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * SHAEngine::Result -- Fetch the current digest. *
  32. * SHAEngine::Hash -- Process an arbitrarily long data block. *
  33. * SHAEngine::Process_Partial -- Helper routine to process any partially accumulated data blo*
  34. * SHAEngine::Process_Block -- Process a full data block into the hash accumulator. *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #include <stdlib.h>
  37. //#include <iostream.h>
  38. #include "sha.h"
  39. #if !defined(__BORLANDC__) && !defined(min)
  40. #define min(a, b) ((a)<(b))?(a):(b)
  41. #endif
  42. /***********************************************************************************************
  43. * SHAEngine::Process_Partial -- Helper routine to process any partially accumulated data bloc *
  44. * *
  45. * This routine will see if there is a partial block already accumulated in the holding *
  46. * buffer. If so, then the data is fetched from the source such that a full buffer is *
  47. * accumulated and then processed. If there is insufficient data to fill the buffer, then *
  48. * it accumulates what data it can and then returns so that this routine can be called *
  49. * again later. *
  50. * *
  51. * INPUT: data -- Reference to a pointer to the data. This pointer will be modified if *
  52. * this routine consumes any of the data in the buffer. *
  53. * *
  54. * length-- Reference to the length of the data available. If this routine consumes *
  55. * any of the data, then this length value will be modified. *
  56. * *
  57. * OUTPUT: none *
  58. * *
  59. * WARNINGS: none *
  60. * *
  61. * HISTORY: *
  62. * 07/03/1996 JLB : Created. *
  63. *=============================================================================================*/
  64. void SHAEngine::Process_Partial(void const * & data, long & length)
  65. {
  66. if (length == 0 || data == NULL) return;
  67. /*
  68. ** If there is no partial buffer and the source is greater than
  69. ** a source block size, then partial processing is unnecessary.
  70. ** Bail out in this case.
  71. */
  72. if (PartialCount == 0 && length >= SRC_BLOCK_SIZE) return;
  73. /*
  74. ** Attach as many bytes as possible from the source data into
  75. ** the staging buffer.
  76. */
  77. int add_count = min((int)length, SRC_BLOCK_SIZE - PartialCount);
  78. memcpy(&Partial[PartialCount], data, add_count);
  79. data = ((char const *&)data) + add_count;
  80. PartialCount += add_count;
  81. length -= add_count;
  82. /*
  83. ** If a full staging buffer has been accumulated, then process
  84. ** the staging buffer and then bail.
  85. */
  86. if (PartialCount == SRC_BLOCK_SIZE) {
  87. Process_Block(&Partial[0], Acc);
  88. Length += (long)SRC_BLOCK_SIZE;
  89. PartialCount = 0;
  90. }
  91. }
  92. /***********************************************************************************************
  93. * SHAEngine::Hash -- Process an arbitrarily long data block. *
  94. * *
  95. * This is the main access routine to the SHA engine. It will take the arbitrarily long *
  96. * data block and process it. The hash value is accumulated with any previous calls to *
  97. * this routine. *
  98. * *
  99. * INPUT: data -- Pointer to the data block to process. *
  100. * *
  101. * length -- The number of bytes to process. *
  102. * *
  103. * OUTPUT: none *
  104. * *
  105. * WARNINGS: none *
  106. * *
  107. * HISTORY: *
  108. * 07/03/1996 JLB : Created. *
  109. *=============================================================================================*/
  110. void SHAEngine::Hash(void const * data, long length)
  111. {
  112. IsCached = false;
  113. /*
  114. ** Check for and handle any smaller-than-512bit blocks. This can
  115. ** result in all of the source data submitted to this routine to be
  116. ** consumed at this point.
  117. */
  118. Process_Partial(data, length);
  119. /*
  120. ** If there is no more source data to process, then bail. Speed reasons.
  121. */
  122. if (length == 0) return;
  123. /*
  124. ** First process all the whole blocks available in the source data.
  125. */
  126. long blocks = (length / SRC_BLOCK_SIZE);
  127. long const * source = (long const *)data;
  128. for (int bcount = 0; bcount < blocks; bcount++) {
  129. Process_Block(source, Acc);
  130. Length += (long)SRC_BLOCK_SIZE;
  131. source += SRC_BLOCK_SIZE/sizeof(long);
  132. length -= (long)SRC_BLOCK_SIZE;
  133. }
  134. /*
  135. ** Process any remainder bytes. This data is stored in the source
  136. ** accumulator buffer for future processing.
  137. */
  138. data = source;
  139. Process_Partial(data, length);
  140. }
  141. #define Reverse_LONG(a) ((a>>24)&0x000000FFL) | ((a>>8)&0x0000FF00L) | ((a<<8)&0x00FF0000L) | ((a<<24)&0xFF000000L)
  142. /***********************************************************************************************
  143. * SHAEngine::Result -- Fetch the current digest. *
  144. * *
  145. * This routine will return the digest as it currently stands. *
  146. * *
  147. * INPUT: pointer -- Pointer to the buffer that will hold the digest -- 20 bytes. *
  148. * *
  149. * OUTPUT: Returns with the number of bytes copied into the buffer. This will always be *
  150. * 20. *
  151. * *
  152. * WARNINGS: none *
  153. * *
  154. * HISTORY: *
  155. * 07/03/1996 JLB : Created. *
  156. *=============================================================================================*/
  157. int SHAEngine::Result(void * result) const
  158. {
  159. /*
  160. ** If the final hash result has already been calculated for the
  161. ** current data state, then immediately return with the precalculated
  162. ** value.
  163. */
  164. if (IsCached) {
  165. memcpy(result, &FinalResult, sizeof(FinalResult));
  166. }
  167. long length = Length + PartialCount;
  168. int partialcount = PartialCount;
  169. char partial[SRC_BLOCK_SIZE];
  170. memcpy(partial, Partial, sizeof(Partial));
  171. /*
  172. ** Cap the end of the source data stream with a 1 bit.
  173. */
  174. partial[partialcount] = (char)0x80;
  175. /*
  176. ** Determine if there is insufficient room to append the
  177. ** data length number to the hash source. If not, then
  178. ** fill out the rest of the accumulator and flush it to
  179. ** the hash so that there will be room for the final
  180. ** count value.
  181. */
  182. SHADigest acc = Acc;
  183. if ((SRC_BLOCK_SIZE - partialcount) < 9) {
  184. if (partialcount+1 < SRC_BLOCK_SIZE) {
  185. memset(&partial[partialcount+1], '\0', SRC_BLOCK_SIZE - (partialcount+1));
  186. }
  187. Process_Block(&partial[0], acc);
  188. partialcount = 0;
  189. } else {
  190. partialcount++;
  191. }
  192. /*
  193. ** Put the length of the source data as a 64 bit integer in the
  194. ** last 8 bytes of the pseudo-source data.
  195. */
  196. memset(&partial[partialcount], '\0', SRC_BLOCK_SIZE - partialcount);
  197. *(long *)(&partial[SRC_BLOCK_SIZE-4]) = Reverse_LONG((length*8));
  198. Process_Block(&partial[0], acc);
  199. memcpy((char *)&FinalResult, &acc, sizeof(acc));
  200. for (int index = 0; index < sizeof(FinalResult)/sizeof(long); index++) {
  201. // for (int index = 0; index < SRC_BLOCK_SIZE/sizeof(long); index++) {
  202. (long &)FinalResult.Long[index] = Reverse_LONG(FinalResult.Long[index]);
  203. }
  204. (bool&)IsCached = true;
  205. memcpy(result, &FinalResult, sizeof(FinalResult));
  206. return(sizeof(FinalResult));
  207. }
  208. /*
  209. ** This pragma to turn off the warning "Conversion may lose significant digits" is to
  210. ** work around a bug within the Borland compiler. It will give this warning when the
  211. ** _rotl() function is called but will NOT give the warning when the _lrotl() function
  212. ** is called even though they both have the same parameters and declaration attributes.
  213. */
  214. //#pragma warn -sig
  215. template<class T>
  216. T _rotl(T X, int n)
  217. {
  218. return(T)( ( X << n ) | ( (unsigned)X >> ((sizeof(T)*8) - n) ) );
  219. }
  220. //unsigned long _RTLENTRY _rotl(unsigned long X, int n)
  221. //{
  222. // return(unsigned long)( (unsigned long)( (unsigned long)( (unsigned long)X ) << (int)n ) | (unsigned long)( ((unsigned long) X ) >> ( (int)((int)(sizeof(long)*(long)8) - (long)n) ) ) );
  223. //}
  224. void memrev(char * buffer, size_t length);
  225. /***********************************************************************************************
  226. * SHAEngine::Process_Block -- Process a full data block into the hash accumulator. *
  227. * *
  228. * This helper routine is called when a full block of data is available for processing *
  229. * into the hash. *
  230. * *
  231. * INPUT: source -- Pointer to the block of data to process. *
  232. * *
  233. * acc -- Reference to the hash accumulator that this hash step will be *
  234. * accumulated into. *
  235. * *
  236. * OUTPUT: none *
  237. * *
  238. * WARNINGS: none *
  239. * *
  240. * HISTORY: *
  241. * 07/03/1996 JLB : Created. *
  242. *=============================================================================================*/
  243. void SHAEngine::Process_Block(void const * source, SHADigest & acc) const
  244. {
  245. /*
  246. ** The hash is generated by performing operations on a
  247. ** block of generated/seeded data.
  248. */
  249. long block[PROC_BLOCK_SIZE/sizeof(long)];
  250. /*
  251. ** Expand the source data into a large 80 * 32bit buffer. This is the working
  252. ** data that will be transformed by the secure hash algorithm.
  253. */
  254. long const * data = (long const *)source;
  255. int index;
  256. for (index = 0; index < SRC_BLOCK_SIZE/sizeof(long); index++) {
  257. block[index] = Reverse_LONG(data[index]);
  258. }
  259. for (index = SRC_BLOCK_SIZE/sizeof(long); index < PROC_BLOCK_SIZE/sizeof(long); index++) {
  260. // block[index] = _rotl(block[(index-3)&15] ^ block[(index-8)&15] ^ block[(index-14)&15] ^ block[(index-16)&15], 1);
  261. block[index] = _rotl(block[index-3] ^ block[index-8] ^ block[index-14] ^ block[index-16], 1);
  262. }
  263. /*
  264. ** This is the core algorithm of the Secure Hash Algorithm. It is a block
  265. ** transformation of 512 bit source data with a 2560 bit intermediate buffer.
  266. */
  267. SHADigest alt = acc;
  268. for (index = 0; index < PROC_BLOCK_SIZE/sizeof(long); index++) {
  269. long temp = _rotl(alt.Long[0], 5) + Do_Function(index, alt.Long[1], alt.Long[2], alt.Long[3]) + alt.Long[4] + block[index] + Get_Constant(index);
  270. alt.Long[4] = alt.Long[3];
  271. alt.Long[3] = alt.Long[2];
  272. alt.Long[2] = _rotl(alt.Long[1], 30);
  273. alt.Long[1] = alt.Long[0];
  274. alt.Long[0] = temp;
  275. }
  276. acc.Long[0] += alt.Long[0];
  277. acc.Long[1] += alt.Long[1];
  278. acc.Long[2] += alt.Long[2];
  279. acc.Long[3] += alt.Long[3];
  280. acc.Long[4] += alt.Long[4];
  281. }