X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fd0_blind_id.git;a=blobdiff_plain;f=d0_bignum-gmp.c;h=1567d4287071957be7bf8dc9837d26111505830b;hp=6a2f703e2d69796903fd198d95e2de6f25bfd05e;hb=d9643e105daab19d374d432f3a123ce0d51a3aa2;hpb=a9a4ac1407a715fe29713380953904de440d9f50 diff --git a/d0_bignum-gmp.c b/d0_bignum-gmp.c index 6a2f703..1567d42 100644 --- a/d0_bignum-gmp.c +++ b/d0_bignum-gmp.c @@ -17,6 +17,11 @@ License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef WIN32 +#include +#include +#endif + #include "d0_bignum.h" #include @@ -33,26 +38,68 @@ static d0_bignum_t temp; #include #include -void d0_bignum_INITIALIZE(void) + +WARN_UNUSED_RESULT BOOL d0_bignum_INITIALIZE(void) { FILE *f; + BOOL ret = 1; + unsigned char buf[256]; d0_bignum_init(&temp); gmp_randinit_mt(RANDSTATE); gmp_randseed_ui(RANDSTATE, time(NULL)); + * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data +#ifdef WIN32 + { + HCRYPTPROV hCryptProv; + if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) + { + if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0])) + { + fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n"); + ret = 0; + } + CryptReleaseContext(hCryptProv, 0); + } + else if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_NEWKEYSET)) + { + if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0])) + { + fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n"); + ret = 0; + } + CryptReleaseContext(hCryptProv, 0); + } + else + { + fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n"); + ret = 0; + } + } +#else f = fopen("/dev/urandom", "rb"); if(!f) f = fopen("/dev/random", "rb"); if(f) { - unsigned char buf[256]; setbuf(f, NULL); - if(fread(buf, sizeof(buf), 1, f) == 1) + if(fread(buf, sizeof(buf), 1, f) != 1) { - mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf); - gmp_randseed(RANDSTATE, temp.z); + fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n"); + ret = 0; } fclose(f); } + else + { + fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n"); + ret = 0; + } +#endif + + mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf); + gmp_randseed(RANDSTATE, temp.z); + + return ret; } void d0_bignum_SHUTDOWN(void)