RNDSTRAW.CPP 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/RNDSTRAW.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. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : RNDSTRAW.CPP *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 07/04/96 *
  26. * *
  27. * Last Update : July 10, 1996 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * RandomStraw::Get -- Fetch random data. *
  32. * RandomStraw::RandomStraw -- Constructor for the random straw class. *
  33. * RandomStraw::Reset -- Reset the data to known initial state. *
  34. * RandomStraw::Scramble_Seed -- Masks any coorelation between the seed bits. *
  35. * RandomStraw::Seed_Bit -- Add a random bit to the accumulated seed value. *
  36. * RandomStraw::Seed_Bits_Needed -- Fetches the number of seed bits needed. *
  37. * RandomStraw::Seed_Byte -- Submit 8 bits to the random number seed. *
  38. * RandomStraw::Seed_Long -- Submit 32 bits to the random number seed. *
  39. * RandomStraw::Seed_Short -- Submit 16 bits to the random number seed. *
  40. * RandomStraw::~RandomStraw -- Destructor for random straw class. *
  41. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  42. #include <limits.h>
  43. #include <string.h>
  44. #include "rndstraw.h"
  45. #include "sha.h"
  46. /***********************************************************************************************
  47. * RandomStraw::RandomStraw -- Constructor for the random straw class. *
  48. * *
  49. * This will initialize the random straw into a known state. The initial state is useless *
  50. * for cryptographic purposes. It must be seeded before it should be used. *
  51. * *
  52. * INPUT: none *
  53. * *
  54. * OUTPUT: none *
  55. * *
  56. * WARNINGS: none *
  57. * *
  58. * HISTORY: *
  59. * 07/10/1996 JLB : Created. *
  60. *=============================================================================================*/
  61. RandomStraw::RandomStraw(void) :
  62. SeedBits(0),
  63. Current(0)
  64. {
  65. Reset();
  66. }
  67. /***********************************************************************************************
  68. * RandomStraw::~RandomStraw -- Destructor for random straw class. *
  69. * *
  70. * This destructor will clear out the seed data so that there won't be any sensitive *
  71. * information left over in the memory this object occupied. *
  72. * *
  73. * INPUT: none *
  74. * *
  75. * OUTPUT: none *
  76. * *
  77. * WARNINGS: none *
  78. * *
  79. * HISTORY: *
  80. * 07/10/1996 JLB : Created. *
  81. *=============================================================================================*/
  82. RandomStraw::~RandomStraw(void)
  83. {
  84. Reset();
  85. }
  86. /***********************************************************************************************
  87. * RandomStraw::Reset -- Reset the data to known initial state. *
  88. * *
  89. * This routine is functionally equivalent to performing an placement new on the object. It *
  90. * clears out all the seed data. *
  91. * *
  92. * INPUT: none *
  93. * *
  94. * OUTPUT: none *
  95. * *
  96. * WARNINGS: You must reseed it before fetching random numbers. *
  97. * *
  98. * HISTORY: *
  99. * 07/10/1996 JLB : Created. *
  100. *=============================================================================================*/
  101. void RandomStraw::Reset(void)
  102. {
  103. SeedBits = 0;
  104. Current = 0;
  105. memset(Random, '\0', sizeof(Random));
  106. }
  107. /***********************************************************************************************
  108. * RandomStraw::Seed_Bits_Needed -- Fetches the number of seed bits needed. *
  109. * *
  110. * This routine is used when seeding the random straw. Keep feeding random bits to this *
  111. * class until the Seed_Bits_Needed value returns zero. Random bits should be gathered from *
  112. * sources external to the immediate program. Examples would be the system clock (good for *
  113. * a few bits), the delay between player keystrokes (good for a few bits per keystroke), *
  114. * etc. When gathering random bits from integers, use the low order bits (since they *
  115. * characteristically are less predictable and more 'random' than the others). *
  116. * *
  117. * INPUT: none *
  118. * *
  119. * OUTPUT: Returns with the number of bits required in order to fully seed this random *
  120. * number generator. *
  121. * *
  122. * WARNINGS: Even if this routine returns zero, you are still allowed and encouraged to feed *
  123. * more random bits when the opportunity arrises. *
  124. * *
  125. * HISTORY: *
  126. * 07/10/1996 JLB : Created. *
  127. *=============================================================================================*/
  128. int RandomStraw::Seed_Bits_Needed(void) const
  129. {
  130. const int total = sizeof(Random) * CHAR_BIT;
  131. if (SeedBits < total) {
  132. return(total - SeedBits);
  133. }
  134. return(0);
  135. }
  136. /***********************************************************************************************
  137. * RandomStraw::Seed_Bit -- Add a random bit to the accumulated seed value. *
  138. * *
  139. * This routine should be called to feed a single random bit to the random straw object. *
  140. * You must feed as many bits as requested by the Seed_Bits_Needed() function. Submitting *
  141. * additional bits is not only allowed, but encouraged. *
  142. * *
  143. * INPUT: seed -- The bit (lowest order bit) to feed to the random number seed. *
  144. * *
  145. * OUTPUT: none *
  146. * *
  147. * WARNINGS: none *
  148. * *
  149. * HISTORY: *
  150. * 07/10/1996 JLB : Created. *
  151. *=============================================================================================*/
  152. void RandomStraw::Seed_Bit(int seed)
  153. {
  154. char * ptr = ((char *)&Random[0]) + ((SeedBits / CHAR_BIT) % sizeof(Random));
  155. char frac = (char)(1 << (SeedBits & (CHAR_BIT-1)));
  156. if (seed & 0x01) {
  157. *ptr ^= frac;
  158. }
  159. SeedBits++;
  160. if (SeedBits == (sizeof(Random) * CHAR_BIT)) {
  161. Scramble_Seed();
  162. }
  163. }
  164. /***********************************************************************************************
  165. * RandomStraw::Seed_Byte -- Submit 8 bits to the random number seed. *
  166. * *
  167. * This will submit 8 bits (as specified) to the random number seed. *
  168. * *
  169. * INPUT: seed -- The seed bits to submit. *
  170. * *
  171. * OUTPUT: none *
  172. * *
  173. * WARNINGS: none *
  174. * *
  175. * HISTORY: *
  176. * 07/10/1996 JLB : Created. *
  177. *=============================================================================================*/
  178. void RandomStraw::Seed_Byte(char seed)
  179. {
  180. for (int index = 0; index < CHAR_BIT; index++) {
  181. Seed_Bit(seed);
  182. seed >>= 1;
  183. }
  184. }
  185. /***********************************************************************************************
  186. * RandomStraw::Seed_Short -- Submit 16 bits to the random number seed. *
  187. * *
  188. * This will submit 16 bits to the accumulated seed. *
  189. * *
  190. * INPUT: seed -- The 16 bits to submit to the seed. *
  191. * *
  192. * OUTPUT: none *
  193. * *
  194. * WARNINGS: none *
  195. * *
  196. * HISTORY: *
  197. * 07/10/1996 JLB : Created. *
  198. *=============================================================================================*/
  199. void RandomStraw::Seed_Short(short seed)
  200. {
  201. for (int index = 0; index < (sizeof(seed)*CHAR_BIT); index++) {
  202. Seed_Bit(seed);
  203. seed >>= 1;
  204. }
  205. }
  206. /***********************************************************************************************
  207. * RandomStraw::Seed_Long -- Submit 32 bits to the random number seed. *
  208. * *
  209. * This will submit a full 32 bits to the accumulated seed value. *
  210. * *
  211. * INPUT: seed -- The 32 bits to submit. *
  212. * *
  213. * OUTPUT: none *
  214. * *
  215. * WARNINGS: none *
  216. * *
  217. * HISTORY: *
  218. * 07/10/1996 JLB : Created. *
  219. *=============================================================================================*/
  220. void RandomStraw::Seed_Long(long seed)
  221. {
  222. for (int index = 0; index < (sizeof(seed)*CHAR_BIT); index++) {
  223. Seed_Bit(seed);
  224. seed >>= 1;
  225. }
  226. }
  227. /***********************************************************************************************
  228. * RandomStraw::Scramble_Seed -- Masks any coorelation between the seed bits. *
  229. * *
  230. * This routine is called when a full set of seed bits has been accumulated. It will take *
  231. * the existing seed and scramble the bits. An effective way of doing this is to use the *
  232. * Secure Hash Algorithm. It is necessary to have this routine because there might be *
  233. * some coorelation in the seed bits. Even more important is that this routine will result *
  234. * in every bit of the seed having a scrambling effect on every other bit. *
  235. * *
  236. * INPUT: none *
  237. * *
  238. * OUTPUT: none *
  239. * *
  240. * WARNINGS: none *
  241. * *
  242. * HISTORY: *
  243. * 07/10/1996 JLB : Created. *
  244. *=============================================================================================*/
  245. void RandomStraw::Scramble_Seed(void)
  246. {
  247. SHAEngine sha;
  248. for (int index = 0; index < sizeof(Random); index++) {
  249. char digest[20];
  250. sha.Hash(&Random[0], sizeof(Random));
  251. sha.Result(digest);
  252. int tocopy = sizeof(digest) < (sizeof(Random)-index) ? sizeof(digest) : (sizeof(Random)-index);
  253. memmove(((char *)&Random[0]) + index, digest, tocopy);
  254. }
  255. }
  256. /***********************************************************************************************
  257. * RandomStraw::Get -- Fetch random data. *
  258. * *
  259. * This routine will fetch random data and store it into the buffer specified. *
  260. * *
  261. * INPUT: source -- Pointer to the buffer to fill with random data. *
  262. * *
  263. * length -- The number of bytes to store into the buffer. *
  264. * *
  265. * OUTPUT: Returns with the actual number of bytes stored into the buffer. This will always *
  266. * be the number of bytes requested since random numbers are unexhaustible. *
  267. * *
  268. * WARNINGS: none *
  269. * *
  270. * HISTORY: *
  271. * 07/04/1996 JLB : Created. *
  272. * 07/10/1996 JLB : Revamped to make cryptographically secure. *
  273. *=============================================================================================*/
  274. int RandomStraw::Get(void * source, int slen)
  275. {
  276. if (source == NULL || slen < 1) {
  277. return(Straw::Get(source, slen));
  278. }
  279. int total = 0;
  280. while (slen > 0) {
  281. *(char *)source = (char)Random[Current++];
  282. Current = Current % (sizeof(Random) / sizeof(Random[0]));
  283. source = (char*)source + sizeof(char);
  284. slen--;
  285. total++;
  286. }
  287. return(total);
  288. }