pkpipe.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.CPP $*
  25. * *
  26. * $Author:: Greg_h $*
  27. * *
  28. * $Modtime:: 7/22/97 11:37a $*
  29. * *
  30. * $Revision:: 1 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * PKPipe::Encrypted_Key_Length -- Fetch the encrypted key length. *
  35. * PKPipe::Key -- Submit a key to enable processing of data flow. *
  36. * PKPipe::PKPipe -- Constructor for the public key pipe object. *
  37. * PKPipe::Plain_Key_Length -- Returns the number of bytes to encrypt key. *
  38. * PKPipe::Put -- Submit data to the pipe for processing. *
  39. * PKPipe::Put_To -- Chains one pipe to another. *
  40. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  41. #include "always.h"
  42. #include "pkpipe.h"
  43. #include <string.h>
  44. /***********************************************************************************************
  45. * PKPipe::PKPipe -- Constructor for the public key pipe object. *
  46. * *
  47. * This will construct the public key pipe object. *
  48. * *
  49. * INPUT: control -- The method used to process the data flow (encrypt or decrypt). *
  50. * *
  51. * rnd -- Reference to a random number generate used to create the internal *
  52. * blowfish key. *
  53. * *
  54. * OUTPUT: none *
  55. * *
  56. * WARNINGS: none *
  57. * *
  58. * HISTORY: *
  59. * 07/11/1996 JLB : Created. *
  60. *=============================================================================================*/
  61. PKPipe::PKPipe(CryptControl control, RandomStraw & rnd) :
  62. IsGettingKey(true),
  63. Rand(rnd),
  64. BF((control == ENCRYPT) ? BlowPipe::ENCRYPT : BlowPipe::DECRYPT),
  65. Control(control),
  66. CipherKey(NULL),
  67. Counter(0),
  68. BytesLeft(0)
  69. {
  70. }
  71. /***********************************************************************************************
  72. * PKPipe::Put_To -- Chains one pipe to another. *
  73. * *
  74. * This handles linking of one pipe to this pipe. Data will flow from this PKPipe to the *
  75. * pipe segment specified. Special handling is done so that piping actually flows to the *
  76. * embedded blowfish pipe and then flows to the designated pipe. *
  77. * *
  78. * INPUT: pipe -- Pointer to the pipe that this pipe segment is to send data to. *
  79. * *
  80. * OUTPUT: none *
  81. * *
  82. * WARNINGS: none *
  83. * *
  84. * HISTORY: *
  85. * 07/12/1996 JLB : Created. *
  86. *=============================================================================================*/
  87. void PKPipe::Put_To(Pipe * pipe)
  88. {
  89. if (BF.ChainTo != pipe) {
  90. if (pipe != NULL && pipe->ChainFrom != NULL) {
  91. pipe->ChainFrom->Put_To(NULL);
  92. pipe->ChainFrom = NULL;
  93. }
  94. if (BF.ChainTo != NULL) {
  95. BF.ChainTo->ChainFrom = NULL;
  96. }
  97. BF.ChainTo = pipe;
  98. if (pipe != NULL) {
  99. pipe->ChainFrom = &BF;
  100. }
  101. BF.ChainFrom = this;
  102. ChainTo = &BF;
  103. }
  104. }
  105. /***********************************************************************************************
  106. * PKPipe::Key -- Submit a key to enable processing of data flow. *
  107. * *
  108. * This routine must be called with a valid key pointer in order for encryption/description *
  109. * to be performed on the data stream. Prior to calling this routine or after calling this *
  110. * routine with a NULL pointer, the data stream will pass through this pipe without *
  111. * modification. *
  112. * *
  113. * INPUT: key -- Pointer to the key to use for processing. Pass NULL if process is to be *
  114. * terminated. *
  115. * *
  116. * OUTPUT: none *
  117. * *
  118. * WARNINGS: none *
  119. * *
  120. * HISTORY: *
  121. * 07/07/1996 JLB : Created. *
  122. *=============================================================================================*/
  123. void PKPipe::Key(PKey const * key)
  124. {
  125. if (key == NULL) {
  126. Flush();
  127. IsGettingKey = false;
  128. }
  129. CipherKey = key;
  130. if (CipherKey != NULL) {
  131. IsGettingKey = true;
  132. if (Control == DECRYPT) {
  133. Counter = BytesLeft = Encrypted_Key_Length();
  134. }
  135. }
  136. }
  137. /***********************************************************************************************
  138. * PKPipe::Put -- Submit data to the pipe for processing. *
  139. * *
  140. * This routine (if processing as been enabled by a previous key submission) will *
  141. * encrypt or decrypt the data stream that passes through it. When encrypting, the data *
  142. * stream will increase in size by about 10% (bit it varies according to the key used). *
  143. * *
  144. * INPUT: source -- Pointer to the data to be submitted to the pipe stream. *
  145. * *
  146. * length -- The number of bytes submitted. *
  147. * *
  148. * OUTPUT: Returns with the actual number of byte output at the final end of the pipe. *
  149. * *
  150. * WARNINGS: none *
  151. * *
  152. * HISTORY: *
  153. * 07/07/1996 JLB : Created. *
  154. *=============================================================================================*/
  155. int PKPipe::Put(void const * source, int length)
  156. {
  157. /*
  158. ** If the parameter seem illegal, then pass the pipe request to the
  159. ** next pipe in the chain and let them deal with it.
  160. */
  161. if (source == NULL || length < 1 || CipherKey == NULL) {
  162. return(Pipe::Put(source, length));
  163. }
  164. int total = 0;
  165. /*
  166. ** Perform a special process if the this is the first part of the data flow. The special
  167. ** key must be processed first. After this initial key processing, the rest of the data flow
  168. ** is processed by the blowfish pipe and ignored by the PKPipe.
  169. */
  170. if (IsGettingKey) {
  171. /*
  172. ** When encrypting, first make the key block and then pass the data through the
  173. ** normal blowfish processor.
  174. */
  175. if (Control == ENCRYPT) {
  176. /*
  177. ** Generate the largest blowfish key possible.
  178. */
  179. char buffer[MAX_KEY_BLOCK_SIZE];
  180. memset(buffer, '\0', sizeof(buffer));
  181. Rand.Get(buffer, BLOWFISH_KEY_SIZE);
  182. /*
  183. ** Encrypt the blowfish key (along with any necessary pad bytes).
  184. */
  185. int didput = CipherKey->Encrypt(buffer, Plain_Key_Length(), Buffer);
  186. total += Pipe::Put(Buffer, didput);
  187. BF.Key(buffer, BLOWFISH_KEY_SIZE);
  188. IsGettingKey = false;
  189. } else {
  190. /*
  191. ** First try to accumulate a full key.
  192. */
  193. int toget = (BytesLeft < length) ? BytesLeft : length;
  194. memmove(&Buffer[Counter-BytesLeft], source, toget);
  195. length -= toget;
  196. BytesLeft -= toget;
  197. source = (char *)source + toget;
  198. /*
  199. ** If a full key has been accumulated, then decrypt it and feed the
  200. ** key to the blowfish engine.
  201. */
  202. if (BytesLeft == 0) {
  203. char buffer[MAX_KEY_BLOCK_SIZE];
  204. CipherKey->Decrypt(Buffer, Counter, buffer);
  205. BF.Key(buffer, BLOWFISH_KEY_SIZE);
  206. IsGettingKey = false;
  207. }
  208. }
  209. }
  210. /*
  211. ** If there are any remaining bytes to pipe through, then
  212. ** pipe them through now -- they will be processed by the
  213. ** blowfish engine.
  214. */
  215. total += Pipe::Put(source, length);
  216. return(total);
  217. }
  218. /***********************************************************************************************
  219. * PKPipe::Encrypted_Key_Length -- Fetch the encrypted key length. *
  220. * *
  221. * This returns the total number of bytes (after encryption) that the blowfish key will *
  222. * consume. It should be possible to get a block of this size, then pass it to the *
  223. * public key decrypter and the result will be the full blowfish key. *
  224. * *
  225. * INPUT: none *
  226. * *
  227. * OUTPUT: Returns with the number of bytes that the encrypted blowfish key required. *
  228. * *
  229. * WARNINGS: none *
  230. * *
  231. * HISTORY: *
  232. * 07/11/1996 JLB : Created. *
  233. *=============================================================================================*/
  234. int PKPipe::Encrypted_Key_Length(void) const
  235. {
  236. if (CipherKey == NULL) return(0);
  237. return(CipherKey->Block_Count(BLOWFISH_KEY_SIZE) * CipherKey->Crypt_Block_Size());
  238. }
  239. /***********************************************************************************************
  240. * PKPipe::Plain_Key_Length -- Returns the number of bytes to encrypt key. *
  241. * *
  242. * This is the number of plain (unencrypted) bytes that the blowfish key will take up. This *
  243. * is actually the number of plain blocks minimum that can contain the full blowfish *
  244. * key. The public key cryptography system encrypts in whole blocks only. *
  245. * *
  246. * INPUT: none *
  247. * *
  248. * OUTPUT: Returns with the total number of bytes that will contain the full blowfish key *
  249. * and still be an even block size for the public key cryptography process. *
  250. * *
  251. * WARNINGS: This value is probably be larger than the actual blowfish key length. *
  252. * *
  253. * HISTORY: *
  254. * 07/11/1996 JLB : Created. *
  255. *=============================================================================================*/
  256. int PKPipe::Plain_Key_Length(void) const
  257. {
  258. if (CipherKey == NULL) return(0);
  259. return(CipherKey->Block_Count(BLOWFISH_KEY_SIZE) * CipherKey->Plain_Block_Size());
  260. }