| 12 | |
| 13 | namespace wallet { |
| 14 | int CCrypter::BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const |
| 15 | { |
| 16 | // This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc |
| 17 | // cipher and sha512 message digest. Because sha512's output size (64b) is |
| 18 | // greater than the aes256 block size (16b) + aes256 key size (32b), |
| 19 | // there's no need to process more than once (D_0). |
| 20 | |
| 21 | if(!count || !key || !iv) |
| 22 | return 0; |
| 23 | |
| 24 | unsigned char buf[CSHA512::OUTPUT_SIZE]; |
| 25 | CSHA512 di; |
| 26 | |
| 27 | di.Write((const unsigned char*)strKeyData.data(), strKeyData.size()); |
| 28 | di.Write(chSalt.data(), chSalt.size()); |
| 29 | di.Finalize(buf); |
| 30 | |
| 31 | for(int i = 0; i != count - 1; i++) |
| 32 | di.Reset().Write(buf, sizeof(buf)).Finalize(buf); |
| 33 | |
| 34 | memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE); |
| 35 | memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE); |
| 36 | memory_cleanse(buf, sizeof(buf)); |
| 37 | return WALLET_CRYPTO_KEY_SIZE; |
| 38 | } |
| 39 | |
| 40 | bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod) |
| 41 | { |