PKPIPE.CPP 16 KB

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