|
8 | 8 | #include "pubkey.h" |
9 | 9 | #include "util.h" |
10 | 10 | #include "utilstrencodings.h" |
| 11 | +#include "random.h" |
11 | 12 |
|
12 | 13 | #include <openssl/aes.h> |
13 | 14 | #include <openssl/sha.h> |
@@ -49,14 +50,25 @@ void ComputePassfactor(std::string ownersalt, uint256 prefactor, uint256& passfa |
49 | 50 | bool ComputePasspoint(uint256 passfactor, CPubKey& passpoint) |
50 | 51 | { |
51 | 52 | size_t clen = 65; |
52 | | - secp256k1_context *ctx = NULL; |
| 53 | + secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 54 | + assert(ctx != nullptr); |
| 55 | + { |
| 56 | + // Pass in a random blinding seed to the secp256k1 context. |
| 57 | + std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32); |
| 58 | + GetRandBytes(vseed.data(), 32); |
| 59 | + bool ret = secp256k1_context_randomize(ctx, vseed.data()); |
| 60 | + assert(ret); |
| 61 | + } |
53 | 62 | secp256k1_pubkey pubkey; |
54 | 63 |
|
55 | 64 | //passpoint is the ec_mult of passfactor on secp256k1 |
56 | | - if (!secp256k1_ec_pubkey_create(ctx, &pubkey, passfactor.begin())) |
| 65 | + if (!secp256k1_ec_pubkey_create(ctx, &pubkey, passfactor.begin())) { |
| 66 | + secp256k1_context_destroy(ctx); |
57 | 67 | return false; |
| 68 | + } |
58 | 69 |
|
59 | 70 | secp256k1_ec_pubkey_serialize(ctx, (unsigned char*)passpoint.begin(), &clen, &pubkey, SECP256K1_EC_COMPRESSED); |
| 71 | + secp256k1_context_destroy(ctx); |
60 | 72 |
|
61 | 73 | if (passpoint.size() != clen) |
62 | 74 | return false; |
@@ -241,10 +253,21 @@ bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey, uint2 |
241 | 253 | ComputeFactorB(seedB, factorB); |
242 | 254 |
|
243 | 255 | //multiply passfactor by factorb mod N to yield the priv key |
244 | | - secp256k1_context *ctx = NULL; |
| 256 | + secp256k1_context *ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 257 | + assert(ctx != nullptr); |
| 258 | + { |
| 259 | + // Pass in a random blinding seed to the secp256k1 context. |
| 260 | + std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32); |
| 261 | + GetRandBytes(vseed.data(), 32); |
| 262 | + bool ret = secp256k1_context_randomize(ctx, vseed.data()); |
| 263 | + assert(ret); |
| 264 | + } |
245 | 265 | privKey = factorB; |
246 | | - if (!secp256k1_ec_privkey_tweak_mul(ctx, privKey.begin(), passfactor.begin())) |
| 266 | + if (!secp256k1_ec_privkey_tweak_mul(ctx, privKey.begin(), passfactor.begin())) { |
| 267 | + secp256k1_context_destroy(ctx); |
247 | 268 | return false; |
| 269 | + } |
| 270 | + secp256k1_context_destroy(ctx); |
248 | 271 |
|
249 | 272 | //double check that the address hash matches our final privkey |
250 | 273 | CKey k; |
|
0 commit comments