1 // Copyright (c) 2009-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
10 #include <openssl/aes.h>
11 #include <openssl/evp.h>
13 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
15 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
19 if (nDerivationMethod == 0)
20 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
21 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
23 if (i != (int)WALLET_CRYPTO_KEY_SIZE)
25 OPENSSL_cleanse(chKey, sizeof(chKey));
26 OPENSSL_cleanse(chIV, sizeof(chIV));
34 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
36 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
39 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
40 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
46 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
51 // max ciphertext len for a n bytes of plaintext is
52 // n + AES_BLOCK_SIZE - 1 bytes
53 int nLen = vchPlaintext.size();
54 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
55 vchCiphertext = std::vector<unsigned char> (nCLen);
61 EVP_CIPHER_CTX_init(&ctx);
62 if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
63 if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
64 if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
65 EVP_CIPHER_CTX_cleanup(&ctx);
67 if (!fOk) return false;
69 vchCiphertext.resize(nCLen + nFLen);
73 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
78 // plaintext will always be equal to or lesser than length of ciphertext
79 int nLen = vchCiphertext.size();
80 int nPLen = nLen, nFLen = 0;
82 vchPlaintext = CKeyingMaterial(nPLen);
88 EVP_CIPHER_CTX_init(&ctx);
89 if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
90 if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
91 if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
92 EVP_CIPHER_CTX_cleanup(&ctx);
94 if (!fOk) return false;
96 vchPlaintext.resize(nPLen + nFLen);
101 bool EncryptSecret(const CKeyingMaterial& vMasterKey, const CKeyingMaterial &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
103 CCrypter cKeyCrypter;
104 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
105 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
106 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
108 return cKeyCrypter.Encrypt(*((const CKeyingMaterial*)&vchPlaintext), vchCiphertext);
111 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CKeyingMaterial& vchPlaintext)
113 CCrypter cKeyCrypter;
114 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
115 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
116 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
118 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));