sha.h 6.7 KB

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