Only use secure random

And fail if no secure random could be found
master
est31 2016-04-09 23:26:38 +02:00
parent b9d057842f
commit a78f43854d
1 changed files with 11 additions and 19 deletions

30
srp.c
View File

@ -529,29 +529,21 @@ static SRP_Result fill_buff()
#ifdef WIN32 #ifdef WIN32
CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE*) g_rand_buff); return SRP_ERR;
CryptReleaseContext(wctx, 0); if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE*) g_rand_buff))
return SRP_ERR;
return SRP_OK; if (!CryptReleaseContext(wctx, 0))
return SRP_ERR;
#else #else
fp = fopen("/dev/urandom", "r"); fp = fopen("/dev/urandom", "r");
if (fp) { if (!fp)
fread(g_rand_buff, sizeof(g_rand_buff), 1, fp); return SRP_ERR;
fclose(fp);
} else { fread(g_rand_buff, sizeof(g_rand_buff), 1, fp);
srp_pcgrandom *r = (srp_pcgrandom *) srp_alloc(sizeof(srp_pcgrandom)); fclose(fp);
if (!r)
return SRP_ERR;
srp_pcgrandom_seed(r, time(NULL) ^ clock(), 0xda3e39cb94b95bdbULL);
size_t i = 0;
for (i = 0; i < RAND_BUFF_MAX; i++) {
g_rand_buff[i] = srp_pcgrandom_next(r);
}
srp_free(r);
}
#endif #endif
return SRP_OK; return SRP_OK;
} }