PKPIPE.CPP 16 KB

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