Browse Source

boost: remove CPrivKey dependency from CECKey

This allows CECKey to be used without directly depending on the secure
allocators
0.10
Cory Fields 10 years ago
parent
commit
e405aa48c7
  1. 33
      src/key.cpp

33
src/key.cpp

@ -179,19 +179,17 @@ public:
BN_clear_free(&bn); BN_clear_free(&bn);
} }
void GetPrivKey(CPrivKey &privkey, bool fCompressed) { int GetPrivKeySize(bool fCompressed) {
EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED); EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
int nSize = i2d_ECPrivateKey(pkey, NULL); return i2d_ECPrivateKey(pkey, NULL);
assert(nSize); }
privkey.resize(nSize); int GetPrivKey(unsigned char* privkey, bool fCompressed) {
unsigned char* pbegin = &privkey[0]; EC_KEY_set_conv_form(pkey, fCompressed ? POINT_CONVERSION_COMPRESSED : POINT_CONVERSION_UNCOMPRESSED);
int nSize2 = i2d_ECPrivateKey(pkey, &pbegin); return i2d_ECPrivateKey(pkey, &privkey);
assert(nSize == nSize2);
} }
bool SetPrivKey(const CPrivKey &privkey, bool fSkipCheck=false) { bool SetPrivKey(const unsigned char* privkey, size_t size, bool fSkipCheck=false) {
const unsigned char* pbegin = &privkey[0]; if (d2i_ECPrivateKey(&pkey, &privkey, size)) {
if (d2i_ECPrivateKey(&pkey, &pbegin, privkey.size())) {
if(fSkipCheck) if(fSkipCheck)
return true; return true;
@ -424,7 +422,7 @@ bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
return false; return false;
#else #else
CECKey key; CECKey key;
if (!key.SetPrivKey(privkey)) if (!key.SetPrivKey(&privkey[0], privkey.size()))
return false; return false;
key.GetSecretBytes(vch); key.GetSecretBytes(vch);
#endif #endif
@ -436,16 +434,21 @@ bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) {
CPrivKey CKey::GetPrivKey() const { CPrivKey CKey::GetPrivKey() const {
assert(fValid); assert(fValid);
CPrivKey privkey; CPrivKey privkey;
int privkeylen, ret;
#ifdef USE_SECP256K1 #ifdef USE_SECP256K1
privkey.resize(279); privkey.resize(279);
int privkeylen = 279; privkeylen = 279;
int ret = secp256k1_ecdsa_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed); ret = secp256k1_ecdsa_privkey_export(begin(), (unsigned char*)&privkey[0], &privkeylen, fCompressed);
assert(ret); assert(ret);
privkey.resize(privkeylen); privkey.resize(privkeylen);
#else #else
CECKey key; CECKey key;
key.SetSecretBytes(vch); key.SetSecretBytes(vch);
key.GetPrivKey(privkey, fCompressed); privkeylen = key.GetPrivKeySize(fCompressed);
assert(privkeylen);
privkey.resize(privkeylen);
ret = key.GetPrivKey(&privkey[0], fCompressed);
assert(ret == (int)privkey.size());
#endif #endif
return privkey; return privkey;
} }
@ -517,7 +520,7 @@ bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, bool fSkipCheck=false) {
return false; return false;
#else #else
CECKey key; CECKey key;
if (!key.SetPrivKey(privkey, fSkipCheck)) if (!key.SetPrivKey(&privkey[0], privkey.size(), fSkipCheck))
return false; return false;
key.GetSecretBytes(vch); key.GetSecretBytes(vch);
#endif #endif

Loading…
Cancel
Save