From 9529769b6002eaaa44d293762a3cec159d628206 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 8 Jan 2022 11:20:27 +0000 Subject: [PATCH] Windows PuTTYgen: make state->entropy into a strbuf. This offloads the memory management into centralised code which is better at it than the ad-hoc code here. Now I don't have to predict in advance how much memory the entropy will consume, and can change that decision on the fly. --- windows/puttygen.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/windows/puttygen.c b/windows/puttygen.c index 7bc8f494..30e85c58 100644 --- a/windows/puttygen.c +++ b/windows/puttygen.c @@ -633,7 +633,8 @@ struct MainDlgState { bool collecting_entropy; bool generation_thread_exists; bool key_exists; - int entropy_got, entropy_required, entropy_size; + int entropy_got, entropy_required; + strbuf *entropy; int key_bits, curve_bits; bool ssh2; keytype keytype; @@ -642,7 +643,6 @@ struct MainDlgState { FingerprintType fptype; char **commentptr; /* points to key.comment or ssh2key.comment */ ssh2_userkey ssh2key; - unsigned *entropy; union { RSAKey key; struct dsa_key dsakey; @@ -1409,18 +1409,17 @@ static INT_PTR CALLBACK MainDlgProc(HWND hwnd, UINT msg, state = (struct MainDlgState *) GetWindowLongPtr(hwnd, GWLP_USERDATA); if (state->collecting_entropy && state->entropy && state->entropy_got < state->entropy_required) { - state->entropy[state->entropy_got++] = lParam; - state->entropy[state->entropy_got++] = GetMessageTime(); + put_uint32(state->entropy, lParam); + put_uint32(state->entropy, GetMessageTime()); + state->entropy_got += 2; SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, state->entropy_got, 0); if (state->entropy_got >= state->entropy_required) { /* * Seed the entropy pool */ - random_reseed( - make_ptrlen(state->entropy, state->entropy_size)); - smemclr(state->entropy, state->entropy_size); - sfree(state->entropy); + random_reseed(ptrlen_from_strbuf(state->entropy)); + strbuf_free(state->entropy); state->collecting_entropy = false; start_generating_key(hwnd, state); @@ -1638,9 +1637,7 @@ static INT_PTR CALLBACK MainDlgProc(HWND hwnd, UINT msg, state->collecting_entropy = true; state->entropy_got = 0; - state->entropy_size = (state->entropy_required * - sizeof(unsigned)); - state->entropy = snewn(state->entropy_required, unsigned); + state->entropy = strbuf_new_nm(); SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, MAKELPARAM(0, state->entropy_required));