Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/crypter.cpp
Show All 22 Lines | int CCrypter::BytesToKeySHA512AES(const std::vector<uint8_t> &chSalt, | ||||
if (!count || !key || !iv) { | if (!count || !key || !iv) { | ||||
return 0; | return 0; | ||||
} | } | ||||
uint8_t buf[CSHA512::OUTPUT_SIZE]; | uint8_t buf[CSHA512::OUTPUT_SIZE]; | ||||
CSHA512 di; | CSHA512 di; | ||||
di.Write((const uint8_t *)strKeyData.c_str(), strKeyData.size()); | di.Write((const uint8_t *)strKeyData.c_str(), strKeyData.size()); | ||||
if (chSalt.size()) { | di.Write(chSalt.data(), chSalt.size()); | ||||
di.Write(&chSalt[0], chSalt.size()); | |||||
} | |||||
di.Finalize(buf); | di.Finalize(buf); | ||||
for (int i = 0; i != count - 1; i++) { | for (int i = 0; i != count - 1; i++) { | ||||
di.Reset().Write(buf, sizeof(buf)).Finalize(buf); | di.Reset().Write(buf, sizeof(buf)).Finalize(buf); | ||||
} | } | ||||
memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE); | memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE); | ||||
memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE); | memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE); | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | if (!fKeySet) { | ||||
return false; | return false; | ||||
} | } | ||||
// max ciphertext len for a n bytes of plaintext is | // max ciphertext len for a n bytes of plaintext is | ||||
// n + AES_BLOCKSIZE bytes | // n + AES_BLOCKSIZE bytes | ||||
vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE); | vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE); | ||||
AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true); | AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true); | ||||
size_t nLen = | size_t nLen = enc.Encrypt(vchPlaintext.data(), vchPlaintext.size(), | ||||
enc.Encrypt(&vchPlaintext[0], vchPlaintext.size(), &vchCiphertext[0]); | vchCiphertext.data()); | ||||
if (nLen < vchPlaintext.size()) { | if (nLen < vchPlaintext.size()) { | ||||
return false; | return false; | ||||
} | } | ||||
vchCiphertext.resize(nLen); | vchCiphertext.resize(nLen); | ||||
return true; | return true; | ||||
} | } | ||||
bool CCrypter::Decrypt(const std::vector<uint8_t> &vchCiphertext, | bool CCrypter::Decrypt(const std::vector<uint8_t> &vchCiphertext, | ||||
CKeyingMaterial &vchPlaintext) const { | CKeyingMaterial &vchPlaintext) const { | ||||
if (!fKeySet) { | if (!fKeySet) { | ||||
return false; | return false; | ||||
} | } | ||||
// plaintext will always be equal to or lesser than length of ciphertext | // plaintext will always be equal to or lesser than length of ciphertext | ||||
int nLen = vchCiphertext.size(); | int nLen = vchCiphertext.size(); | ||||
vchPlaintext.resize(nLen); | vchPlaintext.resize(nLen); | ||||
AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true); | AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true); | ||||
nLen = | nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), | ||||
dec.Decrypt(&vchCiphertext[0], vchCiphertext.size(), &vchPlaintext[0]); | vchPlaintext.data()); | ||||
if (nLen == 0) { | if (nLen == 0) { | ||||
return false; | return false; | ||||
} | } | ||||
vchPlaintext.resize(nLen); | vchPlaintext.resize(nLen); | ||||
return true; | return true; | ||||
} | } | ||||
static bool EncryptSecret(const CKeyingMaterial &vMasterKey, | static bool EncryptSecret(const CKeyingMaterial &vMasterKey, | ||||
const CKeyingMaterial &vchPlaintext, | const CKeyingMaterial &vchPlaintext, | ||||
const uint256 &nIV, | const uint256 &nIV, | ||||
std::vector<uint8_t> &vchCiphertext) { | std::vector<uint8_t> &vchCiphertext) { | ||||
CCrypter cKeyCrypter; | CCrypter cKeyCrypter; | ||||
std::vector<uint8_t> chIV(WALLET_CRYPTO_IV_SIZE); | std::vector<uint8_t> chIV(WALLET_CRYPTO_IV_SIZE); | ||||
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE); | memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE); | ||||
if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { | if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { | ||||
return false; | return false; | ||||
} | } | ||||
return cKeyCrypter.Encrypt(*((const CKeyingMaterial *)&vchPlaintext), | return cKeyCrypter.Encrypt(*((const CKeyingMaterial *)&vchPlaintext), | ||||
vchCiphertext); | vchCiphertext); | ||||
} | } | ||||
static bool DecryptSecret(const CKeyingMaterial &vMasterKey, | static bool DecryptSecret(const CKeyingMaterial &vMasterKey, | ||||
const std::vector<uint8_t> &vchCiphertext, | const std::vector<uint8_t> &vchCiphertext, | ||||
const uint256 &nIV, CKeyingMaterial &vchPlaintext) { | const uint256 &nIV, CKeyingMaterial &vchPlaintext) { | ||||
CCrypter cKeyCrypter; | CCrypter cKeyCrypter; | ||||
std::vector<uint8_t> chIV(WALLET_CRYPTO_IV_SIZE); | std::vector<uint8_t> chIV(WALLET_CRYPTO_IV_SIZE); | ||||
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE); | memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE); | ||||
if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { | if (!cKeyCrypter.SetKey(vMasterKey, chIV)) { | ||||
return false; | return false; | ||||
} | } | ||||
return cKeyCrypter.Decrypt(vchCiphertext, | return cKeyCrypter.Decrypt(vchCiphertext, | ||||
*((CKeyingMaterial *)&vchPlaintext)); | *((CKeyingMaterial *)&vchPlaintext)); | ||||
} | } | ||||
static bool DecryptKey(const CKeyingMaterial &vMasterKey, | static bool DecryptKey(const CKeyingMaterial &vMasterKey, | ||||
▲ Show 20 Lines • Show All 198 Lines • Show Last 20 Lines |