BFISH.H 4.3 KB

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