SHA.H 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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.H 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.H *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 04/26/96 *
  30. * *
  31. * Last Update : April 26, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #ifndef SHA_H
  37. #define SHA_H
  38. /*
  39. ** The "bool" integral type was defined by the C++ committee in
  40. ** November of '94. Until the compiler supports this, use the following
  41. ** definition.
  42. */
  43. #ifndef __BORLANDC__
  44. #ifndef TRUE_FALSE_DEFINED
  45. #define TRUE_FALSE_DEFINED
  46. enum {false=0,true=1};
  47. typedef int bool;
  48. #endif
  49. #endif
  50. #include <stdio.h>
  51. #include <string.h>
  52. #include <stdlib.h>
  53. #include <new.h>
  54. /*
  55. ** This implements the Secure Hash Algorithm. It is a cryptographically
  56. ** secure hash with no known weaknesses. It generates a 160 bit hash
  57. ** result given an arbitrary length data source.
  58. */
  59. class SHAEngine
  60. {
  61. public:
  62. SHAEngine(void) : IsCached(false), Length(0), PartialCount(0) {
  63. Acc.Long[0] = SA;
  64. Acc.Long[1] = SB;
  65. Acc.Long[2] = SC;
  66. Acc.Long[3] = SD;
  67. Acc.Long[4] = SE;
  68. };
  69. void Init(void) {
  70. new ((void*)this) SHAEngine;
  71. };
  72. // Fetch result as if source data were to stop now.
  73. int Result(void * result) const;
  74. void Hash(void const * data, long length);
  75. static int Digest_Size(void) {return(sizeof(SHADigest));}
  76. private:
  77. typedef union {
  78. unsigned long Long[5];
  79. unsigned char Char[20];
  80. } SHADigest;
  81. /*
  82. ** This holds the calculated final result. It is cached
  83. ** here to avoid the overhead of recalculating it over
  84. ** multiple sequential requests.
  85. */
  86. bool IsCached;
  87. SHADigest FinalResult;
  88. enum {
  89. // These are the initial seeds to the block accumulators.
  90. SA=0x67452301L,
  91. SB=0xefcdab89L,
  92. SC=0x98badcfeL,
  93. SD=0x10325476L,
  94. SE=0xc3d2e1f0L,
  95. // These are the constants used in the block transformation.
  96. K1=0x5a827999L, // t=0..19 2^(1/2)/4
  97. K2=0x6ed9eba1L, // t=20..39 3^(1/2)/4
  98. K3=0x8f1bbcdcL, // t=40..59 5^(1/2)/4
  99. K4=0xca62c1d6L, // t=60..79 10^(1/2)/4
  100. // Source data is grouped into blocks of this size.
  101. SRC_BLOCK_SIZE=16*sizeof(long),
  102. // Internal processing data is grouped into blocks this size.
  103. PROC_BLOCK_SIZE=80*sizeof(long)
  104. };
  105. long Get_Constant(int index) const {
  106. if (index < 20) return K1;
  107. if (index < 40) return K2;
  108. if (index < 60) return K3;
  109. return K4;
  110. };
  111. // Used for 0..19
  112. long Function1(long X, long Y, long Z) const {
  113. return(Z ^ ( X & ( Y ^ Z ) ) );
  114. };
  115. // Used for 20..39
  116. long Function2(long X, long Y, long Z) const {
  117. return( X ^ Y ^ Z );
  118. };
  119. // Used for 40..59
  120. long Function3(long X, long Y, long Z) const {
  121. return( (X & Y) | (Z & (X | Y) ) );
  122. };
  123. // Used for 60..79
  124. long Function4(long X, long Y, long Z) const {
  125. return( X ^ Y ^ Z );
  126. };
  127. long Do_Function(int index, long X, long Y, long Z) const {
  128. if (index < 20) return Function1(X, Y, Z);
  129. if (index < 40) return Function2(X, Y, Z);
  130. if (index < 60) return Function3(X, Y, Z);
  131. return Function4(X, Y, Z);
  132. };
  133. // Process a full source data block.
  134. void Process_Block(void const * source, SHADigest & acc) const;
  135. // Processes a partially filled source accumulator buffer.
  136. void Process_Partial(void const * & data, long & length);
  137. /*
  138. ** This is the running accumulator values. These values
  139. ** are updated by a block processing step that occurs
  140. ** every 512 bits of source data.
  141. */
  142. SHADigest Acc;
  143. /*
  144. ** This is the running length of the source data
  145. ** processed so far. This total is used to modify the
  146. ** resulting hash value as if it were appended to the end
  147. ** of the source data.
  148. */
  149. long Length;
  150. /*
  151. ** This holds any partial source block. Partial source blocks are
  152. ** a consequence of submitting less than block sized data chunks
  153. ** to the SHA Engine.
  154. */
  155. int PartialCount;
  156. char Partial[SRC_BLOCK_SIZE];
  157. };
  158. #define SHA_SOURCE1 "abc"
  159. #define SHA_DIGEST1a "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D"
  160. #define SHA_DIGEST1b "\x01\x64\xB8\xA9\x14\xCD\x2A\x5E\x74\xC4\xF7\xFF\x08\x2C\x4D\x97\xF1\xED\xF8\x80"
  161. #define SHA_SOURCE2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
  162. #define SHA_DIGEST2a "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1"
  163. #define SHA_DIGEST2b "\xD2\x51\x6E\xE1\xAC\xFA\x5B\xAF\x33\xDF\xC1\xC4\x71\xE4\x38\x44\x9E\xF1\x34\xC8"
  164. #define SHA_SOURCE3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  165. #define SHA_DIGEST3a "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F"
  166. #define SHA_DIGEST3b "\x32\x32\xAF\xFA\x48\x62\x8A\x26\x65\x3B\x5A\xAA\x44\x54\x1F\xD9\x0D\x69\x06\x03"
  167. #endif