rndstraw.cpp 20 KB

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