From 2d75b0d091b9e3ea93ee42cb271d230f4da2d4a5 Mon Sep 17 00:00:00 2001 From: TheTank20 Date: Thu, 24 Jul 2025 23:52:33 -0500 Subject: [PATCH] Free the RNG implementation --- src/libumskt/libumskt.cpp | 74 +++++++++++++++++++++++++++++++++++---- src/libumskt/libumskt.h | 9 ++++- 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/src/libumskt/libumskt.cpp b/src/libumskt/libumskt.cpp index 753b2f7..2952b97 100644 --- a/src/libumskt/libumskt.cpp +++ b/src/libumskt/libumskt.cpp @@ -59,7 +59,33 @@ FNEXPORT int PIDGEN2_GenerateOEM(char* year, char* day, char* oem, char* keyout) return PIDGEN2::GenerateOEM(year, day, oem, keyout); } -// RNG utility functions +// RNG implementation +std::mt19937_64& UMSKT::get_rng() { + static std::mt19937_64 rng = []() { + // Seed the generator with multiple entropy sources + std::random_device rd; + std::array seed_data; + + // Mix in random_device entropy + std::generate(seed_data.begin(), seed_data.end(), std::ref(rd)); + + // Mix in high-resolution time + auto now = std::chrono::high_resolution_clock::now(); + auto nanos = std::chrono::duration_cast( + now.time_since_epoch() + ).count(); + seed_data[0] ^= static_cast(nanos); + + // Create a seed sequence + std::seed_seq seq(seed_data.begin(), seed_data.end()); + + // Initialize RNG with the seed sequence + std::mt19937_64 generator(seq); + return generator; + }(); + return rng; +} + int UMSKT::umskt_rand_bytes(unsigned char *buf, int num) { #if UMSKT_RNG_DJGPP // DOS-compatible RNG using DJGPP's random() function @@ -89,14 +115,20 @@ int UMSKT::umskt_rand_bytes(unsigned char *buf, int num) { } return 1; #else - // Use OpenSSL's RAND_bytes for non-DOS systems - return RAND_bytes(buf, num); + // Use C++ std::uniform_int_distribution for better randomness + std::uniform_int_distribution dist(0, 255); + auto& rng = get_rng(); + + for (int i = 0; i < num; i++) { + buf[i] = static_cast(dist(rng)); + } + return 1; #endif } int UMSKT::umskt_bn_rand(BIGNUM *rnd, int bits, int top, int bottom) { #if UMSKT_RNG_DJGPP - // DOS-compatible RNG implementation for BIGNUMs + // Keep existing DOS-compatible implementation unsigned char *buf = (unsigned char *)malloc((bits + 7) / 8); if (!buf) return 0; @@ -129,7 +161,37 @@ int UMSKT::umskt_bn_rand(BIGNUM *rnd, int bits, int top, int bottom) { return 1; #else - // Use OpenSSL's BN_rand for non-DOS systems - return BN_rand(rnd, bits, top, bottom); + // Generate random bytes using C++ RNG + unsigned char *buf = (unsigned char *)malloc((bits + 7) / 8); + if (!buf) return 0; + + // Generate random bytes + umskt_rand_bytes(buf, (bits + 7) / 8); + + // Convert to BIGNUM + if (!BN_bin2bn(buf, (bits + 7) / 8, rnd)) { + free(buf); + return 0; + } + + free(buf); + + // Apply top/bottom constraints + if (top != -1) { + if (top) { + if (bits == 0) { + BN_zero(rnd); + return 1; + } + BN_set_bit(rnd, bits - 1); + } + BN_mask_bits(rnd, bits); + } + + if (bottom) { + BN_set_bit(rnd, 0); + } + + return 1; #endif } diff --git a/src/libumskt/libumskt.h b/src/libumskt/libumskt.h index 293ca0a..d30cf89 100644 --- a/src/libumskt/libumskt.h +++ b/src/libumskt/libumskt.h @@ -28,12 +28,16 @@ #include #include #include +#include +#include +#include +#include #include #include #include #include -#include +// Remove openssl/rand.h since we're replacing it #include #include @@ -75,6 +79,9 @@ extern "C" { #endif class UMSKT { +private: + static std::mt19937_64& get_rng(); + public: static std::FILE* debug; class PIDGEN2;