pkpipe.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/pkpipe.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 PKPIPE_H
  36. #define PKPIPE_H
  37. #include "blowpipe.h"
  38. #include "pipe.h"
  39. #include "pk.h"
  40. #include "rndstraw.h"
  41. /*
  42. ** This pipe will encrypt/decrypt the data stream. The data is encrypted by generating a
  43. ** symetric key that is then encrypted using the public key system. This symetric key is then
  44. ** used to encrypt the remaining data.
  45. */
  46. class PKPipe : public Pipe
  47. {
  48. public:
  49. typedef enum CryptControl {
  50. ENCRYPT,
  51. DECRYPT
  52. } CryptControl;
  53. PKPipe(CryptControl control, RandomStraw & rnd);
  54. virtual void Put_To(Pipe * pipe);
  55. virtual void Put_To(Pipe & pipe) {Put_To(&pipe);}
  56. // Feed data through for processing.
  57. virtual int Put(void const * source, int length);
  58. // Submit key for encryption/decryption.
  59. void Key(PKey const * key);
  60. private:
  61. enum {
  62. BLOWFISH_KEY_SIZE=BlowfishEngine::MAX_KEY_LENGTH,
  63. MAX_KEY_BLOCK_SIZE=256 // Maximum size of pk encrypted blowfish key.
  64. };
  65. /*
  66. ** This flag indicates whether the PK (fetch blowfish key) phase is
  67. ** in progress or not.
  68. */
  69. bool IsGettingKey;
  70. /*
  71. ** This is the random straw that is needed to generate the
  72. ** blowfish key.
  73. */
  74. RandomStraw & Rand;
  75. /*
  76. ** This is the attached blowfish pipe. After the blowfish key has been
  77. ** decrypted, then the PK processor goes dormant and the blowfish processor
  78. ** takes over the data flow.
  79. */
  80. BlowPipe BF;
  81. /*
  82. ** Controls the method of processing the data stream.
  83. */
  84. CryptControl Control;
  85. /*
  86. ** Pointer to the key to use for encryption/decryption. The actual process
  87. ** performed is controlled by the Control member. A key can be used for
  88. ** either encryption or decryption -- it makes no difference. However, whichever
  89. ** process is performed, the opposite process must be performed using the
  90. ** other key.
  91. */
  92. PKey const * CipherKey;
  93. /*
  94. ** This is the staging buffer for the block of data. This block must be as large as
  95. ** the largest possible key size or the largest blowfish key (whichever is greater).
  96. */
  97. char Buffer[MAX_KEY_BLOCK_SIZE];
  98. /*
  99. ** The working counter that holds the number of bytes in the staging buffer.
  100. */
  101. int Counter;
  102. /*
  103. ** This records the number of bytes remaining in the current block. This
  104. ** will be the number of bytes left to accumulate before the block can be
  105. ** processed either for encryption or decryption.
  106. */
  107. int BytesLeft;
  108. int Encrypted_Key_Length(void) const;
  109. int Plain_Key_Length(void) const;
  110. PKPipe(PKPipe & rvalue);
  111. PKPipe & operator = (PKPipe const & pipe);
  112. };
  113. #endif