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