Inside Bitcoin
Opening the selected file…
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <wallet/crypter.h>7#include <common/system.h>8#include <crypto/aes.h>9#include <crypto/sha512.h>11#include <type_traits>12#include <vector>14namespace wallet {15int CCrypter::BytesToKeySHA512AES(const std::span<const unsigned char> salt, const SecureString& key_data, unsigned int count, unsigned char* key, unsigned char* iv) const16{17// This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc18// cipher and sha512 message digest. Because sha512's output size (64b) is19// greater than the aes256 block size (16b) + aes256 key size (32b),20// there's no need to process more than once (D_0).22if(!count || !key || !iv)23return 0;25unsigned char buf[CSHA512::OUTPUT_SIZE];26CSHA512 di;28di.Write(UCharCast(key_data.data()), key_data.size());29di.Write(salt.data(), salt.size());30di.Finalize(buf);32for (unsigned int i = 0; i != count - 1; ++i)33di.Reset().Write(buf, sizeof(buf)).Finalize(buf);35memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE);36memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE);37memory_cleanse(buf, sizeof(buf));38return WALLET_CRYPTO_KEY_SIZE;39}41bool CCrypter::SetKeyFromPassphrase(const SecureString& key_data, const std::span<const unsigned char> salt, const unsigned int rounds, const unsigned int derivation_method)42{43if (!rounds || salt.size() != WALLET_CRYPTO_SALT_SIZE) {44return false;45}47int i = 0;48if (derivation_method == 0) {49i = BytesToKeySHA512AES(salt, key_data, rounds, vchKey.data(), vchIV.data());50}52if (i != (int)WALLET_CRYPTO_KEY_SIZE)53{54memory_cleanse(vchKey.data(), vchKey.size());55memory_cleanse(vchIV.data(), vchIV.size());56return false;57}59fKeySet = true;60return true;61}63bool CCrypter::SetKey(const CKeyingMaterial& new_key, const std::span<const unsigned char> new_iv)64{65if (new_key.size() != WALLET_CRYPTO_KEY_SIZE || new_iv.size() != WALLET_CRYPTO_IV_SIZE) {66return false;67}69memcpy(vchKey.data(), new_key.data(), new_key.size());70memcpy(vchIV.data(), new_iv.data(), new_iv.size());72fKeySet = true;73return true;74}76bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const77{78if (!fKeySet)79return false;81// max ciphertext len for a n bytes of plaintext is82// n + AES_BLOCKSIZE bytes83vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);85AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true);86size_t nLen = enc.Encrypt(vchPlaintext.data(), vchPlaintext.size(), vchCiphertext.data());87if(nLen < vchPlaintext.size())88return false;89vchCiphertext.resize(nLen);91return true;92}94bool CCrypter::Decrypt(const std::span<const unsigned char> ciphertext, CKeyingMaterial& plaintext) const95{96if (!fKeySet)97return false;99// plaintext will always be equal to or lesser than length of ciphertext100plaintext.resize(ciphertext.size());102AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);103int len = dec.Decrypt(ciphertext.data(), ciphertext.size(), plaintext.data());104if (len == 0) {105return false;106}107plaintext.resize(len);108return true;109}111bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)112{113CCrypter cKeyCrypter;114std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);115memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);116if(!cKeyCrypter.SetKey(vMasterKey, chIV))117return false;118return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);119}121bool DecryptSecret(const CKeyingMaterial& master_key, const std::span<const unsigned char> ciphertext, const uint256& iv, CKeyingMaterial& plaintext)122{123CCrypter key_crypter;124static_assert(WALLET_CRYPTO_IV_SIZE <= std::remove_reference_t<decltype(iv)>::size());125const std::span iv_prefix{iv.data(), WALLET_CRYPTO_IV_SIZE};126if (!key_crypter.SetKey(master_key, iv_prefix)) {127return false;128}129return key_crypter.Decrypt(ciphertext, plaintext);130}132bool DecryptKey(const CKeyingMaterial& master_key, const std::span<const unsigned char> crypted_secret, const CPubKey& pub_key, CKey& key)133{134CKeyingMaterial secret;135if (!DecryptSecret(master_key, crypted_secret, pub_key.GetHash(), secret)) {136return false;137}139if (secret.size() != 32) {140return false;141}143key.Set(secret.begin(), secret.end(), pub_key.IsCompressed());144return key.VerifyPubKey(pub_key);145}146} // namespace wallet