From 6ba05b8a14c03987c829d9e6858e736257205d83 Mon Sep 17 00:00:00 2001 From: QuantumExplorer Date: Fri, 25 Mar 2022 01:34:34 +0700 Subject: [PATCH] fix: added in a secure allocator for random private keys (#31) --- src/privatekey.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/privatekey.cpp b/src/privatekey.cpp index 4c09f23f2695f1..865507dfc7d6df 100644 --- a/src/privatekey.cpp +++ b/src/privatekey.cpp @@ -80,13 +80,17 @@ PrivateKey PrivateKey::FromByteVector(const std::vector bytes, bool mod // Construct a private key from a bytearray. PrivateKey PrivateKey::RandomPrivateKey() { - uint8_t buf[32]; - bn_t r; - bn_new(r); - bn_rand(r, RLC_POS, 256); - bn_write_bin(buf, 32, r); - std::vector ret(buf, buf + 32); - return PrivateKey::FromBytes(Bytes(ret), true); + bn_t *r = Util::SecAlloc(1); + bn_new(*r); + bn_rand(*r, RLC_POS, 256); + PrivateKey k; + bn_copy(k.keydata, *r); + bn_t ord; + bn_new(ord); + g1_get_ord(ord); + bn_mod_basic(k.keydata, k.keydata, ord); + Util::SecFree(r); + return k; } PrivateKey::PrivateKey() {