5
5
#include " crypter.h"
6
6
7
7
#include " crypto/aes.h"
8
+ #include " crypto/sha512.h"
8
9
#include " script/script.h"
9
10
#include " script/standard.h"
10
11
#include " util.h"
11
12
12
13
#include < string>
13
14
#include < vector>
14
15
#include < boost/foreach.hpp>
15
- #include < openssl/aes.h>
16
- #include < openssl/evp.h>
16
+
17
+ int CCrypter::BytesToKeySHA512AES (const std::vector<unsigned char >& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const
18
+ {
19
+ // This mimics the behavior of openssl's EVP_BytesToKey with an aes256cbc
20
+ // cipher and sha512 message digest. Because sha512's output size (64b) is
21
+ // greater than the aes256 block size (16b) + aes256 key size (32b),
22
+ // there's no need to process more than once (D_0).
23
+
24
+ if (!count || !key || !iv)
25
+ return 0 ;
26
+
27
+ unsigned char buf[CSHA512::OUTPUT_SIZE];
28
+ CSHA512 di;
29
+
30
+ di.Write ((const unsigned char *)strKeyData.c_str (), strKeyData.size ());
31
+ if (chSalt.size ())
32
+ di.Write (&chSalt[0 ], chSalt.size ());
33
+ di.Finalize (buf);
34
+
35
+ for (int i = 0 ; i != count - 1 ; i++)
36
+ di.Reset ().Write (buf, sizeof (buf)).Finalize (buf);
37
+
38
+ memcpy (key, buf, WALLET_CRYPTO_KEY_SIZE);
39
+ memcpy (iv, buf + WALLET_CRYPTO_KEY_SIZE, WALLET_CRYPTO_IV_SIZE);
40
+ memory_cleanse (buf, sizeof (buf));
41
+ return WALLET_CRYPTO_KEY_SIZE;
42
+ }
17
43
18
44
bool CCrypter::SetKeyFromPassphrase (const SecureString& strKeyData, const std::vector<unsigned char >& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
19
45
{
@@ -22,8 +48,7 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
22
48
23
49
int i = 0 ;
24
50
if (nDerivationMethod == 0 )
25
- i = EVP_BytesToKey (EVP_aes_256_cbc (), EVP_sha512 (), &chSalt[0 ],
26
- (unsigned char *)&strKeyData[0 ], strKeyData.size (), nRounds, chKey, chIV);
51
+ i = BytesToKeySHA512AES (chSalt, strKeyData, nRounds, chKey, chIV);
27
52
28
53
if (i != (int )WALLET_CRYPTO_KEY_SIZE)
29
54
{
0 commit comments