BLWSTRAW.CPP 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/BLWSTRAW.CPP 1 3/03/97 10:24a 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. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : BLWSTRAW.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 07/02/96 *
  30. * *
  31. * Last Update : July 3, 1996 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * BlowStraw::Get -- Fetch a block of data from the straw. *
  36. * BlowStraw::Key -- Submit a key to the Blowfish straw. *
  37. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  38. #include "blwstraw.h"
  39. #include <string.h>
  40. #include <assert.h>
  41. /***********************************************************************************************
  42. * BlowStraw::Get -- Fetch a block of data from the straw. *
  43. * *
  44. * This routine will take a block of data from the straw and process it according to the *
  45. * encrypt/decrypt flag and the key supplied. Prior to a key be supplied, the data passes *
  46. * through this straw unchanged. *
  47. * *
  48. * INPUT: source -- Pointer to the buffer to hold the data being requested. *
  49. * *
  50. * length -- The length of the data being requested. *
  51. * *
  52. * OUTPUT: Returns with the actual number of bytes stored into the buffer. If the number *
  53. * returned is less than the number requested, then this indicates that the data *
  54. * source has been exhausted. *
  55. * *
  56. * WARNINGS: none *
  57. * *
  58. * HISTORY: *
  59. * 07/03/1996 JLB : Created. *
  60. *=============================================================================================*/
  61. int BlowStraw::Get(void * source, int slen)
  62. {
  63. /*
  64. ** Verify the parameter for legality.
  65. */
  66. if (source == NULL || slen <= 0) {
  67. return(0);
  68. }
  69. /*
  70. ** If there is no blowfish engine present, then merely pass the data through
  71. ** unchanged.
  72. */
  73. if (BF == NULL) {
  74. return(Straw::Get(source, slen));
  75. }
  76. int total = 0;
  77. while (slen > 0) {
  78. /*
  79. ** If there are any left over bytes in the buffer, pass them
  80. ** through first.
  81. */
  82. if (Counter > 0) {
  83. int sublen = (slen < Counter) ? slen : Counter;
  84. memmove(source, &Buffer[sizeof(Buffer)-Counter], sublen);
  85. Counter -= sublen;
  86. source = ((char *)source) + sublen;
  87. slen -= sublen;
  88. total += sublen;
  89. }
  90. if (slen == 0) break;
  91. /*
  92. ** Fetch and encrypt/decrypt the next block.
  93. */
  94. int incount = Straw::Get(Buffer, sizeof(Buffer));
  95. if (incount == 0) break;
  96. /*
  97. ** Only full blocks are processed. Partial blocks are
  98. ** merely passed through unchanged.
  99. */
  100. if (incount == sizeof(Buffer)) {
  101. if (Control == DECRYPT) {
  102. BF->Decrypt(Buffer, incount, Buffer);
  103. } else {
  104. BF->Encrypt(Buffer, incount, Buffer);
  105. }
  106. } else {
  107. memmove(&Buffer[sizeof(Buffer)-incount], Buffer, incount);
  108. }
  109. Counter = incount;
  110. }
  111. /*
  112. ** Return with the total number of bytes placed into the buffer.
  113. */
  114. return(total);
  115. }
  116. /***********************************************************************************************
  117. * BlowStraw::Key -- Submit a key to the Blowfish straw. *
  118. * *
  119. * This will take the key specified and use it to process the data that flows through this *
  120. * straw segment. Prior to a key being submitted, the data will flow through unchanged. *
  121. * *
  122. * INPUT: key -- Pointer to the key to submit. *
  123. * *
  124. * length-- The length of the key. The length must not exceed 56 bytes. *
  125. * *
  126. * OUTPUT: none *
  127. * *
  128. * WARNINGS: none *
  129. * *
  130. * HISTORY: *
  131. * 07/03/1996 JLB : Created. *
  132. *=============================================================================================*/
  133. void BlowStraw::Key(void const * key, int length)
  134. {
  135. /*
  136. ** Create the blowfish engine if one isn't already present.
  137. */
  138. if (BF == NULL) {
  139. BF = new BlowfishEngine;
  140. }
  141. assert(BF != NULL);
  142. if (BF != NULL) {
  143. BF->Submit_Key(key, length);
  144. }
  145. }