Skip to content

Commit

Permalink
fix: added in a secure allocator for random private keys (dashpay#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumExplorer authored Mar 24, 2022
1 parent 7c0dfe3 commit 6ba05b8
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/privatekey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ PrivateKey PrivateKey::FromByteVector(const std::vector<uint8_t> 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<uint8_t> ret(buf, buf + 32);
return PrivateKey::FromBytes(Bytes(ret), true);
bn_t *r = Util::SecAlloc<bn_t>(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() {
Expand Down

0 comments on commit 6ba05b8

Please sign in to comment.