Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: khangmartin on September 23, 2019, 12:24:52 AM



Title: Salt Explanation.
Post by: khangmartin on September 23, 2019, 12:24:52 AM
I have added several print statements throughout the source code to follow the path of encryption/decryption on the wallet. My questions are as follows:

     1. Why is the salt not the same in the bitcoin source code as the python program below? 6ec1691c413b52bb vs 0x26cde4d5880(salt)
 
Using bitcoin2john.py, I get the following string from wallet.dat.
$bitcoin
$64$
e8a8aa1de3da38001410635516891bc3d092063bd386acd867df457c8d842beb
$16$
6ec1691c413b52bb
$83782
$96$
d307ed1e99a72541b60cbcd6b9636e3f1d26b2311eb781f0c6d8b659895e5fe7b53af45edb312c3 764df8ccaac7b8a74
$66$
03fffeb3444e21170d74dcc58290e0914e59ec2b51dfac6a67465ba6dd6a7d262d

I have set several stream statements in the function to follow the path in crypter.cpp: The source code was pulled from github bitcoin source code location.

bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
{
    int roundcount = nRounds;
    int cryptokeysize = WALLET_CRYPTO_KEY_SIZE;
    int derivationmethod = nDerivationMethod;
    
    std::ofstream file3;
    file3.open ("C:\\workspace\\bitcoin\\usr\\local\\3.txt");
    file3 << "Made it here...Line3. (chsalt, strKeyData, nRounds, vchKey.data(), vchIV.data() \n";
    file3 << (int)roundcount;
    file3 << "(nrounds)\n";
    file3 << (int)cryptokeysize;
    file3 << "(keysize)\n";
    file3 << (int)derivationmethod;
    file3 << "(derivations)\n";

    if (nRounds < 1 )
        return false;

    int i = 0;
    if (nDerivationMethod == 0)
        i = BytesToKeySHA512AES(chSalt, strKeyData, nRounds, vchKey.data(), vchIV.data());

    file3 << i;
    file3 << "(nderivationmethod)\n";
    file3 << &vchKey;
    file3 << "(vchKey- memory cleanse)\n";
    file3 << vchKey.size();
    file3 << "(key size)\n";
    file3 << &vchIV;
    file3 << "(iv- memory cleanse)\n";
    file3 << vchIV.size();
    file3 << "(iv size)\n";
    file3 << &chSalt;
    file3 << "(salt)\n";
    file3 << chSalt.size();
    file3 << "(salt size)\n";
    file3 << &strKeyData;
    file3 << "(strkey)\n";
    file3 << strKeyData.size();
    file3 << "(strKeyData size)\n";

    if (i != (int)WALLET_CRYPTO_KEY_SIZE)
    {
        file3 << &vchKey;
        file3 << "(vchKey- memory cleanse2)\n";
        file3 << vchKey.size();
        file3 << "(key size2)\n";
    
        file3 << &vchIV;
        file3 << "(iv- memory cleanse2)\n";
        file3 << vchIV.size();
        file3 << "(iv size2)\n";

        memory_cleanse(vchKey.data(), vchKey.size());
        memory_cleanse(vchIV.data(), vchIV.size());
        return false;
    }
    
    file3 << (int)derivationmethod;
    file3 << "(derivation)\n";
      
    fKeySet = true;

    file3 << fKeySet;
    file3 << "(fKeySet)\n";
    file3.close();
    return true;
}

The results are as follows:
Made it here...Line3. (chsalt, strKeyData, nRounds, vchKey.data(), vchIV.data()
83782(nrounds)
32(keysize)
0(derivations)
32(nderivationmethod)
0xbd8edfa270(vchKey- memory cleanse)
32(key size)
0xbd8edfa288(iv- memory cleanse)
16(iv size)
0x26cde4d5880(salt)
8(salt size)
0xbd8edfa9c0(strkey)
16(strKeyData size)
0(derivation)
1(fKeySet)


Title: Re: Salt Explanation.
Post by: achow101 on September 23, 2019, 01:49:10 AM
1. Why is the salt not the same in the bitcoin source code as the python program below? 6ec1691c413b52bb vs 0x26cde4d5880(salt)
Because you are printing out the address of chSalt, not its contents. You are doing this everywhere that you have &. Why are you even putting & in front of things? Putting a & in front of a variable name gets a pointer to that variable, not any of its values. If you wanted to dereference a pointer to get the value it points to, you use *. But none of these are pointers, they are references which are treated exactly the same way as values. If you run it again, you will get different results because of different memory locations.



What are you trying to even accomplish?