BLOWFISH.H 4.6 KB

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