Implement CSecureDataStream for streaming CKeyingMaterial

This commit is contained in:
Jack Grigg
2016-09-28 13:44:31 +13:00
parent 3bbf2c1422
commit 6bffc46a87
3 changed files with 69 additions and 33 deletions

View File

@@ -148,9 +148,7 @@ static bool DecryptSpendingKey(const CKeyingMaterial& vMasterKey,
if (vchSecret.size() != libzcash::SerializedSpendingKeySize)
return false;
// TODO does this undo the benefits of using CKeyingMaterial?
std::vector<unsigned char> serialized(vchSecret.begin(), vchSecret.end());
CDataStream ss(serialized, SER_NETWORK, PROTOCOL_VERSION);
CSecureDataStream ss(vchSecret, SER_NETWORK, PROTOCOL_VERSION);
ss >> sk;
return sk.address() == address;
}
@@ -313,7 +311,7 @@ bool CCryptoKeyStore::AddSpendingKey(const libzcash::SpendingKey &sk)
return false;
std::vector<unsigned char> vchCryptedSecret;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end());
auto address = sk.address();
@@ -378,7 +376,7 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
BOOST_FOREACH(SpendingKeyMap::value_type& mSpendingKey, mapSpendingKeys)
{
const libzcash::SpendingKey &sk = mSpendingKey.second;
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
CSecureDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << sk;
CKeyingMaterial vchSecret(ss.begin(), ss.end());
libzcash::PaymentAddress address = sk.address();

View File

@@ -7,6 +7,7 @@
#include "keystore.h"
#include "serialize.h"
#include "streams.h"
#include "support/allocators/secure.h"
#include "zcash/Address.hpp"
@@ -67,6 +68,18 @@ public:
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CKeyingMaterial;
class CSecureDataStream : public CBaseDataStream<CKeyingMaterial>
{
public:
explicit CSecureDataStream(int nTypeIn, int nVersionIn) : CBaseDataStream(nTypeIn, nVersionIn) { }
CSecureDataStream(const_iterator pbegin, const_iterator pend, int nTypeIn, int nVersionIn) :
CBaseDataStream(pbegin, pend, nTypeIn, nVersionIn) { }
CSecureDataStream(const vector_type& vchIn, int nTypeIn, int nVersionIn) :
CBaseDataStream(vchIn, nTypeIn, nVersionIn) { }
};
/** Encryption/decryption context with key information */
class CCrypter
{