blowfish.h 4.5 KB

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