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; CSecret vchSecret; if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; CKey key; key.SetPubKey(vchPubKey); key.SetSecret(vchSecret); if (key.GetPubKey() == vchPubKey) break; return false; } vMasterKey = vMasterKeyIn; } NotifyStatusChanged(this); return true; }
Blob RippleAddress::accountPrivateDecrypt (const RippleAddress& naPublicFrom, Blob const& vucCipherText) const { CKey ckPrivate; CKey ckPublic; Blob vucPlainText; if (!ckPublic.SetPubKey (naPublicFrom.getAccountPublic ())) { // Bad public key. WriteLog (lsWARNING, RippleAddress) << "accountPrivateDecrypt: Bad public key."; } else if (!ckPrivate.SetPrivateKeyU (getAccountPrivate ())) { // Bad private key. WriteLog (lsWARNING, RippleAddress) << "accountPrivateDecrypt: Bad private key."; } else { try { vucPlainText = ckPrivate.decryptECIES (ckPublic, vucCipherText); } catch (...) { nothing (); } } return vucPlainText; }
bool CPubKey::IsFullyValid() const { if (!IsValid()) return false; CKey key; if (!key.SetPubKey(*this)) return false; return true; }
/* Check the script signature to confirm it came from a valid prime node key * nTime is the time of the transaction/stake. * entry is the resulting database entry if the script signature is valid. */ bool CPrimeNodeDB::IsPrimeNodeKey(CScript scriptPubKeyType, unsigned int nTime, CPrimeNodeDBEntry &entry) { vector<CPrimeNodeDBEntry> primeNodeDBEntries; Dbc* pcursor = GetCursor(); if (!pcursor) throw runtime_error("IsPrimeNodeKey() : cannot create DB cursor"); unsigned int fFlags = DB_SET_RANGE; for (;;) { CDataStream ssKey(SER_DISK, CLIENT_VERSION); if (fFlags == DB_SET_RANGE) ssKey << make_pair(string("primenode"), string("")); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue, fFlags); fFlags = DB_NEXT; if (ret == DB_NOTFOUND) break; else if (ret != 0) { pcursor->close(); throw runtime_error("IsPrimeNodeKey() : error scanning DB"); } // Unserialize string strType; ssKey >> strType; if (strType != "primenode") break; CPrimeNodeDBEntry dbentry; ssKey >> dbentry.key; ssValue >> dbentry; primeNodeDBEntries.push_back(dbentry); } pcursor->close(); BOOST_FOREACH(CPrimeNodeDBEntry dbentry, primeNodeDBEntries) { vector<unsigned char> vchPubKey = ParseHex(dbentry.key); CKey key; key.SetPubKey(vchPubKey); CScript scriptTime; scriptTime << nTime; uint256 hashScriptTime = Hash(scriptTime.begin(), scriptTime.end()); vector<unsigned char> vchSig; vchSig.insert(vchSig.end(), scriptPubKeyType.begin() + 2, scriptPubKeyType.end()); if(key.Verify(hashScriptTime, vchSig)) { entry = dbentry; return true; } }
bool CAlert::CheckSignature() const { CKey key; if (!key.SetPubKey(ParseHex(fTestNet ? pszTestKey : pszMainKey))) return error("CAlert::CheckSignature() : SetPubKey failed"); if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) return error("CAlert::CheckSignature() : verify signature failed"); // Now unserialize the data CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION); sMsg >> *(CUnsignedAlert*)this; return true; }
// ppcoin: verify signature of sync-checkpoint message bool CSyncCheckpoint::CheckSignature() { CKey key; std::string strMasterPubKey = fTestNet? CSyncCheckpoint::strTestPubKey : CSyncCheckpoint::strMainPubKey; if (!key.SetPubKey(ParseHex(strMasterPubKey))) return error("CSyncCheckpoint::CheckSignature() : SetPubKey failed"); if (!key.Verify(Hash(vchMsg.begin(), vchMsg.end()), vchSig)) return error("CSyncCheckpoint::CheckSignature() : verify signature failed"); // Now unserialize the data CDataStream sMsg(vchMsg, SER_NETWORK, PROTOCOL_VERSION); sMsg >> *(CUnsignedSyncCheckpoint*)this; return true; }
bool CWalletBackup::LoadBuffer(CDataStream& ssBuffer) { try { while(!ssBuffer.empty()) { string strType; ssBuffer >> strType; if (strType == "key") { vector<unsigned char> vchPubKey; CPrivKey pkey; ssBuffer >> vchPubKey >> pkey; CKey key; key.SetPubKey(vchPubKey); key.SetPrivKey(pkey); if (key.GetPubKey() != vchPubKey || !key.IsValid() || !AddKey(key)) { return false; } } else if (strType == "wkey") { vector<unsigned char> vchPubKey; CWalletKey wkey; ssBuffer >> vchPubKey >> wkey; CKey key; key.SetPubKey(vchPubKey); key.SetPrivKey(wkey.vchPrivKey); if (key.GetPubKey() != vchPubKey || !key.IsValid() || !AddKey(key)) { return false; } }
bool RippleAddress::verifyNodePublic(const uint256& hash, const std::vector<unsigned char>& vchSig) const { CKey pubkey = CKey(); bool bVerified; if (!pubkey.SetPubKey(getNodePublic())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify(hash, vchSig); } return bVerified; }
bool RippleAddress::verifyNodePublic (uint256 const& hash, Blob const& vchSig) const { CKey pubkey = CKey (); bool bVerified; if (!pubkey.SetPubKey (getNodePublic ())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify (hash, vchSig); } return bVerified; }
bool RippleAddress::accountPublicVerify(const uint256& uHash, const std::vector<unsigned char>& vucSig) const { CKey ckPublic; bool bVerified; if (!ckPublic.SetPubKey(getAccountPublic())) { // Bad private key. cLog(lsWARNING) << "accountPublicVerify: Bad private key."; bVerified = false; } else { bVerified = ckPublic.Verify(uHash, vucSig); } return bVerified; }
bool CheckSig(vector<unsigned char> vchSig, vector<unsigned char> vchPubKey, CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType) { CKey key; if (!key.SetPubKey(vchPubKey)) return false; // Hash type is one byte tacked on to the end of the signature if (vchSig.empty()) return false; if (nHashType == 0) nHashType = vchSig.back(); else if (nHashType != vchSig.back()) return false; vchSig.pop_back(); return key.Verify(SignatureHash(scriptCode, txTo, nIn, nHashType), vchSig); }
bool RippleAddress::accountPublicVerify (uint256 const& uHash, Blob const& vucSig) const { CKey ckPublic; bool bVerified; if (!ckPublic.SetPubKey (getAccountPublic ())) { // Bad private key. WriteLog (lsWARNING, RippleAddress) << "accountPublicVerify: Bad private key."; bVerified = false; } else { bVerified = ckPublic.Verify (uHash, vucSig); } return bVerified; }
bool RippleAddress::verifyNodePublic (uint256 const& hash, Blob const& vchSig, ECDSA fullyCanonical) const { CKey pubkey = CKey (); bool bVerified; bVerified = isCanonicalECDSASig (vchSig, fullyCanonical); if (bVerified && !pubkey.SetPubKey (getNodePublic ())) { // Failed to set public key. bVerified = false; } else { bVerified = pubkey.Verify (hash, vchSig); } return bVerified; }
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; CSecret vchSecret; if (!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret)) return false; if (vchSecret.size() != 32) return false; keyOut.SetPubKey(vchPubKey); keyOut.SetSecret(vchSecret); return true; } } return false; }
int CWalletDB::LoadWallet(CWallet* pwallet) { pwallet->vchDefaultKey = CPubKey(); int nFileVersion = 0; vector<uint256> vWalletUpgrade; bool fIsEncrypted = false; //// todo: shouldn't we catch exceptions and try to recover and continue? { LOCK(pwallet->cs_wallet); int nMinVersion = 0; if (Read((string)"minversion", nMinVersion)) { if (nMinVersion > CLIENT_VERSION) return DB_TOO_NEW; pwallet->LoadMinVersion(nMinVersion); } // Get cursor Dbc* pcursor = GetCursor(); if (!pcursor) { printf("Error getting wallet database cursor\n"); return DB_CORRUPT; } loop { // Read next record CDataStream ssKey(SER_DISK, CLIENT_VERSION); CDataStream ssValue(SER_DISK, CLIENT_VERSION); int ret = ReadAtCursor(pcursor, ssKey, ssValue); if (ret == DB_NOTFOUND) break; else if (ret != 0) { printf("Error reading next record from wallet database\n"); return DB_CORRUPT; } // Unserialize // Taking advantage of the fact that pair serialization // is just the two items serialized one after the other string strType; ssKey >> strType; if (strType == "name") { string strAddress; ssKey >> strAddress; ssValue >> pwallet->mapAddressBook[CBitcoinAddress(strAddress).Get()]; } else if (strType == "key" || strType == "wkey") { vector<unsigned char> vchPubKey; ssKey >> vchPubKey; CKey key; if (strType == "key") { CPrivKey pkey; ssValue >> pkey; key.SetPubKey(vchPubKey); key.SetPrivKey(pkey); if (key.GetPubKey() != vchPubKey) { printf("Error reading wallet database: CPrivKey pubkey inconsistency\n"); return DB_CORRUPT; } if (!key.IsValid()) { printf("Error reading wallet database: invalid CPrivKey\n"); return DB_CORRUPT; } }
int main(int argc, char* argv[]) { if (argc != 7){ printf("Hashchecker needs to know the cryptographic details of your wallet!\nUsage: hashchecker pw_to_permute iterations salt crypted_key public_key crypted_secret\n"); return 1; } CCrypter crypter; CKeyingMaterial vMasterKey; // Try any password as input SecureString attempt = argv[1]; const unsigned int nDeriveIterations = atoi(argv[2]);//29731; const vector<unsigned char> chSalt = Convert(argv[3]);//"b29a2e128e8e0a2f");//argv[1]; const vector<unsigned char> vchCryptedKey = Convert(argv[4]);//"982a07407ccb8d70514e7b7ccae4b53d68318ec41fd2bf99bf9dbcafd2f150a92c6eb8f9ea743b782fc5b85403421c1d");//argv[2]; const vector<unsigned char> vchPubKey = Convert(argv[5]);//"03fefd771544971f3ab95b041bbce02cc799a335d0d12c3bcd46c7c61a4e3ba897"); const vector<unsigned char> vchCryptedSecret = Convert(argv[6]);//"17169083a74b07ff3497027af7423b9aec1593c90f15a57f52c368593947c85e37b03430840ad48ef409e97ba5a4cdeb"); double count = Factorial(attempt.size()); bool found = false; for (int i = 0; i <= count; i++) { if (i > 0) {//test the word as typed in on first iteration permutation(i-1, attempt); } const SecureString strWalletPassphrase = attempt; cout << i << "-" << strWalletPassphrase <<"\n"; if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, chSalt, nDeriveIterations, 0)) { cout << i << " " << strWalletPassphrase <<"\n"; continue; } if (!crypter.Decrypt(vchCryptedKey, vMasterKey)) { cout << i << " " << strWalletPassphrase <<"\n"; continue; } CSecret vchSecret; if(!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) { cout << "** didn't decrypt **" <<"\n"; continue; } if (vchSecret.size() != 32) { cout << "** wrong size secret **" <<"\n"; continue; } CKey key; key.SetPubKey(vchPubKey); key.SetSecret(vchSecret); if (key.GetPubKey() == vchPubKey) { cout<<"Found one: "<<strWalletPassphrase<<"\n"; found = true; break; } // else // cout << "** didn't get the pubkey back **\n"; } if (found) cout << "Found it! Congratulations\n"; return 0; }
void SignVerifyMessageDialog::on_verifyMessageButton_VM_clicked() { CBitcoinAddress addr(ui->addressIn_VM->text().toStdString()); if (!addr.IsValid()) { ui->addressIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The entered address is invalid.") + QString(" ") + tr("Please check the address and try again.")); return; } CKeyID keyID; if (!addr.GetKeyID(keyID)) { ui->addressIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The entered address does not refer to a key.") + QString(" ") + tr("Please check the address and try again.")); return; } bool fInvalid = false; std::vector<unsigned char> vchSig = DecodeBase64(ui->signatureIn_VM->text().toStdString().c_str(), &fInvalid); if (fInvalid) { ui->signatureIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The signature could not be decoded.") + QString(" ") + tr("Please check the signature and try again.")); return; } CDataStream ss(SER_GETHASH, 0); ss << strMessageMagic; ss << ui->messageIn_VM->document()->toPlainText().toStdString(); // get the public key from UI fInvalid = false; std::vector<unsigned char> vchPubKey = DecodeBase64(ui->pubkeyIn_VM->text().toStdString().c_str(), &fInvalid); if (fInvalid) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key could not be decoded.") + QString(" ") + tr("Please check it and try again.")); return; } CPubKey pubkey(vchPubKey); if (!pubkey.IsValid()) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key is not valid.") + QString(" ") + tr("Please check it and try again.")); return; } CKey key; if (!key.SetPubKey(pubkey)) { ui->pubkeyIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The public key cannot be added.") + QString(" ") + tr("Please check it and try again.")); return; } if (!key.Verify(HashKeccak(ss.begin(), ss.end()), vchSig)) { ui->signatureIn_VM->setValid(false); ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(tr("The signature did not match the message digest.") + QString(" ") + tr("Please check the signature and try again.")); return; } // TODO // add the public key //key.SetPubKey(); if (!(CBitcoinAddress(key.GetPubKey().GetID()) == addr)) { ui->statusLabel_VM->setStyleSheet("QLabel { color: red; }"); ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verification failed.") + QString("</nobr>")); return; } ui->statusLabel_VM->setStyleSheet("QLabel { color: green; }"); ui->statusLabel_VM->setText(QString("<nobr>") + tr("Message verified.") + QString("</nobr>")); }