srandom.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #ifndef SRANDOM_H
  19. #define SRANDOM_H
  20. #include "random.h" // for the helper RNG
  21. //
  22. // SecureRandomClass - Generate random values that are suitable for use in cryptographic
  23. // applications (under UNIX at least).
  24. //
  25. // NOTE: The seed generation under windows isn't nearly as strong as under UNIX due to
  26. // the lack of /dev/random. If you have a good random source you can call 'Add_Seeds'
  27. // to improve the quality of the seed value.
  28. //
  29. // Currently the random seed generation under windows is piss-poor so make sure and call
  30. // Add_Seeds with some interesting data if you're going to use this!
  31. //
  32. // The seed values should be unguessable by an outside viewer and the random values have
  33. // good distribution properties.
  34. //
  35. class SecureRandomClass
  36. {
  37. public:
  38. SecureRandomClass();
  39. ~SecureRandomClass();
  40. unsigned long Randval(); // get a 32 bit random value
  41. // Add randomness to the seed pool
  42. void Add_Seeds(unsigned char *values, int length);
  43. private:
  44. void Generate_Seed(void); // Generate the inital seed
  45. enum
  46. {
  47. SeedLength = 16, // bytes in random seed
  48. SHADigestBytes = 20 // length of SHA hash
  49. };
  50. static bool Initialized; // has the seed been initialized?
  51. static unsigned char Seeds[SeedLength]; // random seed values
  52. static unsigned int RandomCache[SHADigestBytes / sizeof(unsigned int)];
  53. static int RandomCacheEntries;
  54. static unsigned int Counter;
  55. static Random3Class RandomHelper;
  56. };
  57. #endif