2
0

sha.h 6.6 KB

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