bool CHDSeed::Unlock(const CKeyingMaterial& vMasterKeyIn) { assert(sizeof(m_UUID) == WALLET_CRYPTO_IV_SIZE); CKeyingMaterial vchMnemonic; if (!DecryptSecret(vMasterKeyIn, encryptedMnemonic, std::vector<unsigned char>(m_UUID.begin(), m_UUID.end()), vchMnemonic)) return false; unencryptedMnemonic = SecureString(vchMnemonic.begin(), vchMnemonic.end()); CKeyingMaterial vchMasterKeyPrivEncoded; if (!DecryptSecret(vMasterKeyIn, masterKeyPrivEncrypted, masterKeyPub.pubkey.GetHash(), vchMasterKeyPrivEncoded)) return false; masterKeyPriv.Decode(vchMasterKeyPrivEncoded.data()); CKeyingMaterial vchPurposeKeyPrivEncoded; if (!DecryptSecret(vMasterKeyIn, purposeKeyPrivEncrypted, purposeKeyPub.pubkey.GetHash(), vchPurposeKeyPrivEncoded)) return false; purposeKeyPriv.Decode(vchPurposeKeyPrivEncoded.data()); CKeyingMaterial vchCoinTypeKeyPrivEncoded; if (!DecryptSecret(vMasterKeyIn, cointypeKeyPrivEncrypted, cointypeKeyPub.pubkey.GetHash(), vchCoinTypeKeyPrivEncoded)) return false; cointypeKeyPriv.Decode(vchCoinTypeKeyPrivEncoded.data()); vMasterKey = vMasterKeyIn; return true; }
bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) { { LOCK(cs_KeyStore); if (!SetCrypted()) return false; CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); for (; mi != mapCryptedKeys.end(); ++mi) { const CPubKey &vchPubKey = (*mi).second.first; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; CKeyingMaterial vchSecret; if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; CKey key; key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); if (key.GetPubKey() == vchPubKey) break; return false; } vMasterKey = vMasterKeyIn; } NotifyStatusChanged(this); return true; }
// LogTrace("spark", "the vEncrypted:%s\n", VectorToString(vEncrypted)); return true; } bool CLotData::DecryptData(const CLottoFileKey &key, const vector<unsigned char>& vInput, vector<unsigned char>& vDecrypted) { CLottoFileKey nkey = key; vector<uint256> keydata = nkey.getKey(); if (keydata.size() == 1) { int ii = 0; vector<unsigned char> tmpdata = vInput; // LogTrace("spark", "read out from file:%s\n", VectorToString(tmpdata)); while (ii >= 0) { CKeyingMaterial tmpkey(keydata[ii].begin(), keydata[ii].end()); CKeyingMaterial tmpout; // LogTrace("spark", "the tmp:%s\n", VectorToString(vector<unsigned char>(tmpkey.begin(), tmpkey.end()))); if (!sDecryptData(tmpkey, tmpdata, uint256(100), tmpout)) { return false; } tmpdata.assign(tmpout.begin(), tmpout.end()); // LogTrace("spark", "the tmpout:%s\n", VectorToString(tmpdata)); ii--; } vDecrypted = tmpdata; } else {
static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key) { CKeyingMaterial vchSecret; if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); return key.VerifyPubKey(vchPubKey); }
bool WalletUtilityDB::DecryptKey(const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key) { CKeyingMaterial vchSecret; if(!DecryptSecret(vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); return true; }
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext, const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>()) { std::vector<unsigned char> vchCiphertext; crypt.Encrypt(vchPlaintext, vchCiphertext); if (!vchCiphertextCorrect.empty()) BOOST_CHECK(vchCiphertext == vchCiphertextCorrect); const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); TestDecrypt(crypt, vchCiphertext, vchPlaintext2); }
bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn) { { LOCK(cs_KeyStore); if (!SetCrypted()) return false; bool keyPass = false; bool keyFail = false; CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin(); for (; mi != mapCryptedKeys.end(); ++mi) { const CPubKey &vchPubKey = (*mi).second.first; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; CKeyingMaterial vchSecret; if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) { keyFail = true; break; } if (vchSecret.size() != 32) { keyFail = true; break; } CKey key; key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); if (key.GetPubKey() != vchPubKey) { keyFail = true; break; } keyPass = true; if (fDecryptionThoroughlyChecked) break; } if (keyPass && keyFail) { LogPrintf("The wallet is probably corrupted: Some keys decrypt but not all."); assert(false); } if (keyFail || !keyPass) return false; vMasterKey = vMasterKeyIn; fDecryptionThoroughlyChecked = true; } NotifyStatusChanged(this); return true; }
/* The old namecoind encrypted not the 32-byte secret, but the full 279-byte serialised keys. Thus, we need to handle both formats. This is done by the following utility routine: It decrypts a secret and initialises a CKey object from it. */ static bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key) { CKeyingMaterial vchSecret; if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; //LogPrintf("%s : decrypted %u-byte key\n", __func__, vchSecret.size()); if (vchSecret.size() == 32) { key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); return true; } return key.SetPrivKey(vchSecret, vchPubKey.IsCompressed()); }
static void TestEncryptSingle(const CCrypter& crypt, const CKeyingMaterial& vchPlaintext, const std::vector<unsigned char>& vchCiphertextCorrect = std::vector<unsigned char>()) { std::vector<unsigned char> vchCiphertext1; std::vector<unsigned char> vchCiphertext2; int result1 = crypt.Encrypt(vchPlaintext, vchCiphertext1); int result2 = OldEncrypt(vchPlaintext, vchCiphertext2, crypt.vchKey.data(), crypt.vchIV.data()); BOOST_CHECK(result1 == result2); BOOST_CHECK(vchCiphertext1 == vchCiphertext2); if (!vchCiphertextCorrect.empty()) BOOST_CHECK(vchCiphertext2 == vchCiphertextCorrect); const std::vector<unsigned char> vchPlaintext2(vchPlaintext.begin(), vchPlaintext.end()); if(vchCiphertext1 == vchCiphertext2) TestDecrypt(crypt, vchCiphertext1, vchPlaintext2); }
bool CCryptoKeyStore::GetKey(const CKeyID &address, CKey& keyOut) const { { LOCK(cs_KeyStore); if (!IsCrypted()) return CBasicKeyStore::GetKey(address, keyOut); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address); if (mi != mapCryptedKeys.end()) { const CPubKey &vchPubKey = (*mi).second.first; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second; CKeyingMaterial vchSecret; if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; keyOut.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed()); return true; } } return false; }