1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-04-18 03:28:07 -05:00

Been meaning to get round to this for a while: use CryptGenRandom to

gather extra entropy at Windows PuTTY startup time. (It's only used as
one of the inputs to PuTTY's internal entropy pool, so nobody is
required to trust it.)

[originally from svn r9941]
This commit is contained in:
Simon Tatham 2013-07-20 08:34:54 +00:00
parent b426872219
commit 8902fef445

View File

@ -9,10 +9,18 @@
#include "ssh.h" #include "ssh.h"
#include "storage.h" #include "storage.h"
#include <wincrypt.h>
DECL_WINDOWS_FUNCTION(static, BOOL, CryptAcquireContextA,
(HCRYPTPROV *, LPCTSTR, LPCTSTR, DWORD, DWORD));
DECL_WINDOWS_FUNCTION(static, BOOL, CryptGenRandom,
(HCRYPTPROV, DWORD, BYTE *));
DECL_WINDOWS_FUNCTION(static, BOOL, CryptReleaseContext,
(HCRYPTPROV, DWORD));
static HMODULE wincrypt_module = NULL;
/* /*
* This function is called once, at PuTTY startup, and will do some * This function is called once, at PuTTY startup.
* seriously silly things like listing directories and getting disk
* free space and a process snapshot.
*/ */
void noise_get_heavy(void (*func) (void *, int)) void noise_get_heavy(void (*func) (void *, int))
@ -20,6 +28,7 @@ void noise_get_heavy(void (*func) (void *, int))
HANDLE srch; HANDLE srch;
WIN32_FIND_DATA finddata; WIN32_FIND_DATA finddata;
DWORD pid; DWORD pid;
HCRYPTPROV crypt_provider;
char winpath[MAX_PATH + 3]; char winpath[MAX_PATH + 3];
GetWindowsDirectory(winpath, sizeof(winpath)); GetWindowsDirectory(winpath, sizeof(winpath));
@ -35,6 +44,24 @@ void noise_get_heavy(void (*func) (void *, int))
pid = GetCurrentProcessId(); pid = GetCurrentProcessId();
func(&pid, sizeof(pid)); func(&pid, sizeof(pid));
if (!wincrypt_module) {
wincrypt_module = load_system32_dll("advapi32.dll");
GET_WINDOWS_FUNCTION(wincrypt_module, CryptAcquireContextA);
GET_WINDOWS_FUNCTION(wincrypt_module, CryptGenRandom);
GET_WINDOWS_FUNCTION(wincrypt_module, CryptReleaseContext);
}
if (wincrypt_module && p_CryptAcquireContextA &&
p_CryptGenRandom && p_CryptReleaseContext &&
p_CryptAcquireContextA(&crypt_provider, NULL, NULL, PROV_RSA_FULL,
CRYPT_VERIFYCONTEXT)) {
BYTE buf[32];
if (p_CryptGenRandom(crypt_provider, 32, buf)) {
func(buf, sizeof(buf));
}
p_CryptReleaseContext(crypt_provider, 0);
}
read_random_seed(func); read_random_seed(func);
/* Update the seed immediately, in case another instance uses it. */ /* Update the seed immediately, in case another instance uses it. */
random_save_seed(); random_save_seed();