SHA.CPP 16 KB

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