CnC_Remastered_Collection

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

BLOWFISH.H (4784B)


      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/BLOWFISH.H 1     3/03/97 10:24a 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 : BLOWFISH.H                                                   *
     24  *                                                                                             *
     25  *                   Programmer : Joe L. Bostic                                                *
     26  *                                                                                             *
     27  *                   Start Date : 04/14/96                                                     *
     28  *                                                                                             *
     29  *                  Last Update : April 14, 1996 [JLB]                                         *
     30  *                                                                                             *
     31  *---------------------------------------------------------------------------------------------*
     32  * Functions:                                                                                  *
     33  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     34 
     35 #ifndef BLOWFISH_H
     36 #define BLOWFISH_H
     37 
     38 #include	<limits.h>
     39 
     40 
     41 /*
     42 **	The "bool" integral type was defined by the C++ committee in
     43 **	November of '94. Until the compiler supports this, use the following
     44 **	definition.
     45 */
     46 #ifndef __BORLANDC__
     47 #ifndef TRUE_FALSE_DEFINED
     48 #define TRUE_FALSE_DEFINED
     49 enum {false=0,true=1};
     50 typedef int bool;
     51 #endif
     52 #endif
     53 
     54 
     55 /*
     56 **	This engine will process data blocks by encryption and decryption.
     57 **	The "Blowfish" algorithm is in the public domain. It uses
     58 **	a Feistal network (similar to IDEA). It has no known
     59 **	weaknesses, but is still relatively new. Blowfish is particularly strong
     60 **	against brute force attacks. It is also quite strong against linear and
     61 **	differential cryptanalysis. Its weakness is that it takes a relatively
     62 **	long time to set up with a new key (1/100th of a second on a P6-200).
     63 **	The time to set up a key is equivalent to encrypting 4240 bytes.
     64 */
     65 class BlowfishEngine {
     66 	public:
     67 		BlowfishEngine(void) : IsKeyed(false) {}
     68 		~BlowfishEngine(void);
     69 
     70 		void Submit_Key(void const * key, int length);
     71 
     72 		int Encrypt(void const * plaintext, int length, void * cyphertext);
     73 		int Decrypt(void const * cyphertext, int length, void * plaintext);
     74 
     75 		/*
     76 		**	This is the maximum key length supported.
     77 		*/
     78 		enum {MAX_KEY_LENGTH=56};
     79 
     80 	private:
     81 		bool IsKeyed;
     82 
     83 		void Sub_Key_Encrypt(unsigned long & left, unsigned long & right);
     84 
     85 		void Process_Block(void const * plaintext, void * cyphertext, unsigned long const * ptable);
     86 		void Initialize_Tables(void);
     87 
     88 		enum {
     89 			ROUNDS = 16,		// Feistal round count (16 is standard).
     90 			BYTES_PER_BLOCK=8	// The number of bytes in each cypher block (don't change).
     91 		};
     92 
     93 		/*
     94 		**	Initialization data for sub keys. The initial values are constant and
     95 		**	filled with a number generated from pi. Thus they are not random but
     96 		**	they don't hold a weak pattern either.
     97 		*/
     98 		static unsigned long const P_Init[(int)ROUNDS+2];
     99 		static unsigned long const S_Init[4][UCHAR_MAX+1];
    100 
    101 		/*
    102 		**	Permutation tables for encryption and decryption.
    103 		*/
    104  		unsigned long P_Encrypt[(int)ROUNDS+2];
    105  		unsigned long P_Decrypt[(int)ROUNDS+2];
    106 
    107 		/*
    108 		**	S-Box tables (four).
    109 		*/
    110 		unsigned long bf_S[4][UCHAR_MAX+1];
    111 };
    112 
    113 #endif
    114