Skip to content

Commit 1c391a5

Browse files
theunisipa
authored andcommitted
crypter: fix the stored initialization vector size
AES IV's are 16bytes, not 32. This was harmless but confusing. Add WALLET_CRYPTO_IV_SIZE to make its usage explicit.
1 parent daa3841 commit 1c391a5

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/wallet/crypter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::v
3737

3838
bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
3939
{
40-
if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
40+
if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_IV_SIZE)
4141
return false;
4242

4343
memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
@@ -105,8 +105,8 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
105105
static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
106106
{
107107
CCrypter cKeyCrypter;
108-
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
109-
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
108+
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
109+
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE);
110110
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
111111
return false;
112112
return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
@@ -115,8 +115,8 @@ static bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMateri
115115
static bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
116116
{
117117
CCrypter cKeyCrypter;
118-
std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
119-
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
118+
std::vector<unsigned char> chIV(WALLET_CRYPTO_IV_SIZE);
119+
memcpy(&chIV[0], &nIV, WALLET_CRYPTO_IV_SIZE);
120120
if(!cKeyCrypter.SetKey(vMasterKey, chIV))
121121
return false;
122122
return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));

src/wallet/crypter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class uint256;
1313

1414
const unsigned int WALLET_CRYPTO_KEY_SIZE = 32;
1515
const unsigned int WALLET_CRYPTO_SALT_SIZE = 8;
16+
const unsigned int WALLET_CRYPTO_IV_SIZE = 16;
1617

1718
/**
1819
* Private key encryption is done based on a CMasterKey,
@@ -71,7 +72,7 @@ class CCrypter
7172
{
7273
private:
7374
unsigned char chKey[WALLET_CRYPTO_KEY_SIZE];
74-
unsigned char chIV[WALLET_CRYPTO_KEY_SIZE];
75+
unsigned char chIV[WALLET_CRYPTO_IV_SIZE];
7576
bool fKeySet;
7677

7778
public:

0 commit comments

Comments
 (0)