Skip to content

Commit 976f9ec

Browse files
theunisipa
authored andcommitted
crypter: add a BytesToKey clone to replace the use of openssl
BytesToKeySHA512AES should be functionally identical to EVP_BytesToKey, but drops the dependency on openssl.
1 parent 9049cde commit 976f9ec

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/wallet/crypter.cpp

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,41 @@
55
#include "crypter.h"
66

77
#include "crypto/aes.h"
8+
#include "crypto/sha512.h"
89
#include "script/script.h"
910
#include "script/standard.h"
1011
#include "util.h"
1112

1213
#include <string>
1314
#include <vector>
1415
#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+
}
1743

1844
bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
1945
{
@@ -22,8 +48,7 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
2248

2349
int i = 0;
2450
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);
2752

2853
if (i != (int)WALLET_CRYPTO_KEY_SIZE)
2954
{

src/wallet/crypter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ class CCrypter
7575
unsigned char chIV[WALLET_CRYPTO_IV_SIZE];
7676
bool fKeySet;
7777

78+
int BytesToKeySHA512AES(const std::vector<unsigned char>& chSalt, const SecureString& strKeyData, int count, unsigned char *key,unsigned char *iv) const;
79+
7880
public:
7981
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod);
8082
bool Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext) const;

0 commit comments

Comments
 (0)