CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

SHA.H (7010B)


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