Line data Source code
1 : // Copyright (c) 2009-2021 The Bitcoin Core developers
2 : // Distributed under the MIT software license, see the accompanying
3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 :
5 : #include <wallet/crypter.h>
6 :
7 : #include <crypto/aes.h>
8 : #include <crypto/sha512.h>
9 : #include <util/system.h>
10 :
11 : #include <vector>
12 :
13 : namespace wallet {
14 608 : 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 608 : if(!count || !key || !iv)
22 0 : return 0;
23 :
24 : unsigned char buf[CSHA512::OUTPUT_SIZE];
25 608 : CSHA512 di;
26 :
27 608 : di.Write((const unsigned char*)strKeyData.data(), strKeyData.size());
28 608 : di.Write(chSalt.data(), chSalt.size());
29 608 : di.Finalize(buf);
30 :
31 32847404 : for(int i = 0; i != count - 1; i++)
32 32846796 : di.Reset().Write(buf, sizeof(buf)).Finalize(buf);
33 :
34 608 : memcpy(key, buf, WALLET_CRYPTO_KEY_SIZE);
35 608 : memcpy(iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE);
36 608 : memory_cleanse(buf, sizeof(buf));
37 608 : return WALLET_CRYPTO_KEY_SIZE;
38 608 : }
39 :
40 608 : bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
41 : {
42 608 : if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
43 0 : return false;
44 :
45 608 : int i = 0;
46 608 : if (nDerivationMethod == 0)
47 608 : i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data());
48 :
49 608 : if (i != (int)WALLET_CRYPTO_KEY_SIZE)
50 : {
51 0 : memory_cleanse(vchKey.data(), vchKey.size());
52 0 : memory_cleanse(vchIV.data(), vchIV.size());
53 0 : return false;
54 : }
55 :
56 608 : fKeySet = true;
57 608 : return true;
58 608 : }
59 :
60 6741 : bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
61 : {
62 6741 : if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE)
63 0 : return false;
64 :
65 6741 : memcpy(vchKey.data(), chNewKey.data(), chNewKey.size());
66 6741 : memcpy(vchIV.data(), chNewIV.data(), chNewIV.size());
67 :
68 6741 : fKeySet = true;
69 6741 : return true;
70 6741 : }
71 :
72 4291 : bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const
73 : {
74 4291 : if (!fKeySet)
75 0 : return false;
76 :
77 : // max ciphertext len for a n bytes of plaintext is
78 : // n + AES_BLOCKSIZE bytes
79 4291 : vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
80 :
81 4291 : AES256CBCEncrypt enc(vchKey.data(), vchIV.data(), true);
82 4291 : size_t nLen = enc.Encrypt(vchPlaintext.data(), vchPlaintext.size(), vchCiphertext.data());
83 4291 : if(nLen < vchPlaintext.size())
84 0 : return false;
85 4291 : vchCiphertext.resize(nLen);
86 :
87 4291 : return true;
88 4291 : }
89 :
90 9574 : bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext) const
91 : {
92 9574 : if (!fKeySet)
93 0 : return false;
94 :
95 : // plaintext will always be equal to or lesser than length of ciphertext
96 9574 : int nLen = vchCiphertext.size();
97 :
98 9574 : vchPlaintext.resize(nLen);
99 :
100 9574 : AES256CBCDecrypt dec(vchKey.data(), vchIV.data(), true);
101 9574 : nLen = dec.Decrypt(vchCiphertext.data(), vchCiphertext.size(), vchPlaintext.data());
102 9574 : if(nLen == 0)
103 138 : return false;
104 9436 : vchPlaintext.resize(nLen);
105 9436 : return true;
106 9574 : }
107 :
108 866 : bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
109 : {
110 866 : CCrypter cKeyCrypter;
111 866 : std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
112 866 : memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
113 866 : if(!cKeyCrypter.SetKey(vMasterKey, chIV))
114 0 : return false;
115 866 : return cKeyCrypter.Encrypt(vchPlaintext, vchCiphertext);
116 866 : }
117 :
118 : // General secure AES 256 CBC encryption routine
119 4 : bool EncryptAES256(const SecureString& sKey, const SecureString& sPlaintext, const std::string& sIV, std::string& sCiphertext)
120 : {
121 : // Verify key sizes
122 4 : if(sKey.size() != 32 || sIV.size() != AES_BLOCKSIZE) {
123 0 : LogPrintf("crypter EncryptAES256 - Invalid key or block size: Key: %d sIV:%d\n", sKey.size(), sIV.size());
124 0 : return false;
125 : }
126 :
127 : // max ciphertext len for a n bytes of plaintext is
128 : // n + AES_BLOCKSIZE bytes
129 4 : sCiphertext.resize(sPlaintext.size() + AES_BLOCKSIZE);
130 :
131 4 : AES256CBCEncrypt enc((const unsigned char*)sKey.data(), (const unsigned char*)sIV.data(), true);
132 4 : size_t nLen = enc.Encrypt((const unsigned char*)sPlaintext.data(), sPlaintext.size(), (unsigned char*)&sCiphertext[0]);
133 4 : if(nLen < sPlaintext.size())
134 0 : return false;
135 4 : sCiphertext.resize(nLen);
136 4 : return true;
137 4 : }
138 :
139 :
140 5875 : bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
141 : {
142 5875 : CCrypter cKeyCrypter;
143 5875 : std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
144 5875 : memcpy(chIV.data(), &nIV, WALLET_CRYPTO_IV_SIZE);
145 5875 : if(!cKeyCrypter.SetKey(vMasterKey, chIV))
146 0 : return false;
147 5875 : return cKeyCrypter.Decrypt(vchCiphertext, vchPlaintext);
148 5875 : }
149 :
150 : // General secure AES 256 CBC decryption routine
151 4 : bool DecryptAES256(const SecureString& sKey, const std::string& sCiphertext, const std::string& sIV, SecureString& sPlaintext)
152 : {
153 : // Verify key sizes
154 4 : if(sKey.size() != 32 || sIV.size() != AES_BLOCKSIZE) {
155 0 : LogPrintf("crypter DecryptAES256 - Invalid key or block size\n");
156 0 : return false;
157 : }
158 :
159 : // plaintext will always be equal to or lesser than length of ciphertext
160 4 : int nLen = sCiphertext.size();
161 :
162 4 : sPlaintext.resize(nLen);
163 :
164 4 : AES256CBCDecrypt dec((const unsigned char*)sKey.data(), (const unsigned char*)sIV.data(), true);
165 4 : nLen = dec.Decrypt((const unsigned char*)sCiphertext.data(), sCiphertext.size(), (unsigned char*)&sPlaintext[0]);
166 4 : if(nLen == 0)
167 0 : return false;
168 4 : sPlaintext.resize(nLen);
169 4 : return true;
170 4 : }
171 :
172 :
173 3011 : bool DecryptKey(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCryptedSecret, const CPubKey& vchPubKey, CKey& key)
174 : {
175 3011 : CKeyingMaterial vchSecret;
176 3011 : if(!DecryptSecret(vMasterKey, vchCryptedSecret, vchPubKey.GetHash(), vchSecret))
177 0 : return false;
178 :
179 3011 : if (vchSecret.size() != 32)
180 0 : return false;
181 :
182 3011 : key.Set(vchSecret.begin(), vchSecret.end(), vchPubKey.IsCompressed());
183 3011 : return key.VerifyPubKey(vchPubKey);
184 3011 : }
185 : } // namespace wallet
|