BLOWFISH.H 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/BLOWFISH.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** 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 ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : BLOWFISH.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 04/14/96 *
  26. * *
  27. * Last Update : April 14, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef BLOWFISH_H
  33. #define BLOWFISH_H
  34. #include <limits.h>
  35. /*
  36. ** The "bool" integral type was defined by the C++ committee in
  37. ** November of '94. Until the compiler supports this, use the following
  38. ** definition.
  39. */
  40. #ifndef __BORLANDC__
  41. #ifndef TRUE_FALSE_DEFINED
  42. #define TRUE_FALSE_DEFINED
  43. enum {false=0,true=1};
  44. typedef int bool;
  45. #endif
  46. #endif
  47. /*
  48. ** This engine will process data blocks by encryption and decryption.
  49. ** The "Blowfish" algorithm is in the public domain. It uses
  50. ** a Feistal network (similar to IDEA). It has no known
  51. ** weaknesses, but is still relatively new. Blowfish is particularly strong
  52. ** against brute force attacks. It is also quite strong against linear and
  53. ** differential cryptanalysis. Its weakness is that it takes a relatively
  54. ** long time to set up with a new key (1/100th of a second on a P6-200).
  55. ** The time to set up a key is equivalent to encrypting 4240 bytes.
  56. */
  57. class BlowfishEngine {
  58. public:
  59. BlowfishEngine(void) : IsKeyed(false) {}
  60. ~BlowfishEngine(void);
  61. void Submit_Key(void const * key, int length);
  62. int Encrypt(void const * plaintext, int length, void * cyphertext);
  63. int Decrypt(void const * cyphertext, int length, void * plaintext);
  64. /*
  65. ** This is the maximum key length supported.
  66. */
  67. enum {MAX_KEY_LENGTH=56};
  68. private:
  69. bool IsKeyed;
  70. void Sub_Key_Encrypt(unsigned long & left, unsigned long & right);
  71. void Process_Block(void const * plaintext, void * cyphertext, unsigned long const * ptable);
  72. void Initialize_Tables(void);
  73. enum {
  74. ROUNDS = 16, // Feistal round count (16 is standard).
  75. BYTES_PER_BLOCK=8 // The number of bytes in each cypher block (don't change).
  76. };
  77. /*
  78. ** Initialization data for sub keys. The initial values are constant and
  79. ** filled with a number generated from pi. Thus they are not random but
  80. ** they don't hold a weak pattern either.
  81. */
  82. static unsigned long const P_Init[(int)ROUNDS+2];
  83. static unsigned long const S_Init[4][UCHAR_MAX+1];
  84. /*
  85. ** Permutation tables for encryption and decryption.
  86. */
  87. unsigned long P_Encrypt[(int)ROUNDS+2];
  88. unsigned long P_Decrypt[(int)ROUNDS+2];
  89. /*
  90. ** S-Box tables (four).
  91. */
  92. unsigned long bf_S[4][UCHAR_MAX+1];
  93. };
  94. #endif