SHA.H 6.8 KB

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