Skip to content

Commit

Permalink
Stop using OpenSSL's sha hashing in bip38 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuzzbawls committed May 11, 2021
1 parent d531bf2 commit 5f30c2b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 34 deletions.
12 changes: 4 additions & 8 deletions src/bip38.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ void ComputePassfactor(std::string ownersalt, uint256 prefactor, uint256& passfa
{
//concat prefactor and ownersalt
uint512 temp = uint512S(ReverseEndianString(HexStr(prefactor) + ownersalt));
Hash(temp.begin(), 40, passfactor.begin()); //40 bytes is the length of prefactor + salt
Hash(passfactor.begin(), 32, passfactor.begin());
Hash(temp.begin(), temp.end(), passfactor.begin(), passfactor.end());
}

bool ComputePasspoint(uint256 passfactor, CPubKey& passpoint)
Expand Down Expand Up @@ -88,15 +87,12 @@ void ComputeSeedBPass(CPubKey passpoint, std::string strAddressHash, std::string
void ComputeFactorB(uint256 seedB, uint256& factorB)
{
//factorB - a double sha256 hash of seedb
Hash(seedB.begin(), 24, factorB.begin()); //seedB is only 24 bytes
Hash(factorB.begin(), 32, factorB.begin());
Hash(seedB.begin(), seedB.end(), factorB.begin(), factorB.end());
}

std::string AddressToBip38Hash(std::string address)
std::string AddressToBip38Hash(const std::string& address)
{
uint256 addrCheck;
Hash((void*)address.c_str(), address.size(), addrCheck.begin());
Hash(addrCheck.begin(), 32, addrCheck.begin());
uint256 addrCheck = Hash(address.begin(), address.end());

return HexStr(addrCheck).substr(0, 8);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bip38.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ void ComputeFactorB(uint256 seedB, uint256& factorB);
std::string BIP38_Encrypt(std::string strAddress, std::string strPassphrase, uint256 privKey, bool fCompressed);
bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey, uint256& privKey, bool& fCompressed);

std::string AddressToBip38Hash(std::string address);
std::string AddressToBip38Hash(const std::string& address);

#endif // BIP38_H
25 changes: 0 additions & 25 deletions src/hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
#include "crypto/sha512.h"

#include <iomanip>
#include <openssl/sha.h>
#include <sstream>
#include <vector>

Expand Down Expand Up @@ -151,30 +150,6 @@ class CHash160
}
};

/** Compute the 256-bit hash of a std::string */
inline std::string Hash(std::string input)
{
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, input.c_str(), input.size());
SHA256_Final(hash, &sha256);
std::stringstream ss;
for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)hash[i];
}
return ss.str();
}

/** Compute the 256-bit hash of a void pointer */
inline void Hash(void* in, unsigned int len, unsigned char* out)
{
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, in, len);
SHA256_Final(out, &sha256);
}

/** Compute the 512-bit hash of an object. */
template <typename T1>
inline uint512 Hash512(const T1 pbegin, const T1 pend)
Expand Down
1 change: 1 addition & 0 deletions test/functional/rpc_bip38.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def run_test(self):

self.log.info('decrypt bip38 key %s' % (bip38key))
assert_equal(self.nodes[1].bip38decrypt(bip38key, password)['Address'], address)
assert_equal(self.nodes[1].dumpprivkey(address), privkey)

if __name__ == '__main__':
Bip38Test().main()

0 comments on commit 5f30c2b

Please sign in to comment.