-
+ 24B00BF75C58AC2A7A14C14125F7638776AFBC8AB88F7E0026D2FC5B715AAB1FDCCBA91CE43DC4A37D37E7C68341672C235531D6C8A7FEC90F63E95C0F634CE1
bitcoin/src/crypter.cpp
(0 . 0)(1 . 163)
3860 // /****************************\
3861 // * EXPERIMENTAL BRANCH. *
3862 // * FOR LABORATORY USE ONLY. *
3863 // ********************************
3864 // ************
3865 // **************
3866 // ****************
3867 // **** **** ****
3868 // *** *** ***
3869 // *** *** ***
3870 // *** * * **
3871 // ******** ********
3872 // ******* ******
3873 // *** **
3874 // * ******* **
3875 // ** * * * * *
3876 // ** * * ***
3877 // **** * * * * ****
3878 // **** *** * * ** ***
3879 // **** ********* ******
3880 // ******* ***** *******
3881 // ********* ****** **
3882 // ** ****** ******
3883 // ** ******* **
3884 // ** ******* ***
3885 // **** ******** ************
3886 // ************ ************
3887 // ******** *******
3888 // ****** ****
3889 // *** ***
3890 // ********************************
3891 // Copyright (c) 2011 The Bitcoin Developers
3892 // Distributed under the MIT/X11 software license, see the accompanying
3893 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
3894
3895 #include <openssl/aes.h>
3896 #include <openssl/evp.h>
3897 #include <vector>
3898 #include <string>
3899 #include "headers.h"
3900 #ifdef WIN32
3901 #include <windows.h>
3902 #endif
3903
3904 #include "crypter.h"
3905 #include "main.h"
3906 #include "util.h"
3907
3908 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
3909 {
3910 if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
3911 return false;
3912
3913 // Try to keep the keydata out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
3914 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
3915 // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
3916 mlock(&chKey[0], sizeof chKey);
3917 mlock(&chIV[0], sizeof chIV);
3918
3919 int i = 0;
3920 if (nDerivationMethod == 0)
3921 i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
3922 (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
3923
3924 if (i != WALLET_CRYPTO_KEY_SIZE)
3925 {
3926 memset(&chKey, 0, sizeof chKey);
3927 memset(&chIV, 0, sizeof chIV);
3928 return false;
3929 }
3930
3931 fKeySet = true;
3932 return true;
3933 }
3934
3935 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
3936 {
3937 if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
3938 return false;
3939
3940 // Try to keep the keydata out of swap
3941 // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
3942 // Note as well that at no point in this program is any attempt made to prevent stealing of keys by reading the memory of the running process.
3943 mlock(&chKey[0], sizeof chKey);
3944 mlock(&chIV[0], sizeof chIV);
3945
3946 memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
3947 memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
3948
3949 fKeySet = true;
3950 return true;
3951 }
3952
3953 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
3954 {
3955 if (!fKeySet)
3956 return false;
3957
3958 // max ciphertext len for a n bytes of plaintext is
3959 // n + AES_BLOCK_SIZE - 1 bytes
3960 int nLen = vchPlaintext.size();
3961 int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
3962 vchCiphertext = std::vector<unsigned char> (nCLen);
3963
3964 EVP_CIPHER_CTX ctx;
3965
3966 EVP_CIPHER_CTX_init(&ctx);
3967 EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
3968
3969 EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
3970 EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
3971
3972 EVP_CIPHER_CTX_cleanup(&ctx);
3973
3974 vchCiphertext.resize(nCLen + nFLen);
3975 return true;
3976 }
3977
3978 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
3979 {
3980 if (!fKeySet)
3981 return false;
3982
3983 // plaintext will always be equal to or lesser than length of ciphertext
3984 int nLen = vchCiphertext.size();
3985 int nPLen = nLen, nFLen = 0;
3986
3987 vchPlaintext = CKeyingMaterial(nPLen);
3988
3989 EVP_CIPHER_CTX ctx;
3990
3991 EVP_CIPHER_CTX_init(&ctx);
3992 EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
3993
3994 EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
3995 EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
3996
3997 EVP_CIPHER_CTX_cleanup(&ctx);
3998
3999 vchPlaintext.resize(nPLen + nFLen);
4000 return true;
4001 }
4002
4003
4004 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
4005 {
4006 CCrypter cKeyCrypter;
4007 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
4008 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
4009 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
4010 return false;
4011 return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
4012 }
4013
4014 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
4015 {
4016 CCrypter cKeyCrypter;
4017 std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
4018 memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
4019 if(!cKeyCrypter.SetKey(vMasterKey, chIV))
4020 return false;
4021 return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
4022 }