Update CMakeLists.txt - play with openssl
[novacoin.git] / src / crypter.cpp
1 // Copyright (c) 2009-2012 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.
4
5 #include "crypter.h"
6
7 #include <openssl/evp.h>
8
9
10 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
11 {
12     if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
13         return false;
14
15     int i = 0;
16     if (nDerivationMethod == 0)
17     {
18         i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
19                           (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
20     }
21
22     if (i != (int)WALLET_CRYPTO_KEY_SIZE)
23     {
24         OPENSSL_cleanse(&chKey, sizeof chKey);
25         OPENSSL_cleanse(&chIV, sizeof chIV);
26         return false;
27     }
28
29     fKeySet = true;
30     return true;
31 }
32
33 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
34 {
35     if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
36         return false;
37
38     memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
39     memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
40
41     fKeySet = true;
42     return true;
43 }
44
45 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
46 {
47     if (!fKeySet)
48         return false;
49
50     // max ciphertext len for a n bytes of plaintext is
51     // n + AES_BLOCK_SIZE - 1 bytes
52     constexpr int AES_BLOCK_SIZE = 16; // from openssl/aes.h
53     int nLen = vchPlaintext.size();
54     int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
55     vchCiphertext = std::vector<unsigned char> (nCLen);
56
57     EVP_CIPHER_CTX *ctx;
58
59     bool fOk = true;
60
61     ctx = EVP_CIPHER_CTX_new();
62     if (fOk) fOk = EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
63     if (fOk) fOk = EVP_EncryptUpdate(ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
64     if (fOk) fOk = EVP_EncryptFinal_ex(ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
65     EVP_CIPHER_CTX_free(ctx);
66
67     if (!fOk) return false;
68
69     vchCiphertext.resize(nCLen + nFLen);
70     return true;
71 }
72
73 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
74 {
75     if (!fKeySet)
76         return false;
77
78     // plaintext will always be equal to or lesser than length of ciphertext
79     int nLen = vchCiphertext.size();
80     int nPLen = nLen, nFLen = 0;
81
82     vchPlaintext = CKeyingMaterial(nPLen);
83
84     EVP_CIPHER_CTX *ctx;
85
86     bool fOk = true;
87
88     ctx = EVP_CIPHER_CTX_new();
89     if (fOk) fOk = EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
90     if (fOk) fOk = EVP_DecryptUpdate(ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
91     if (fOk) fOk = EVP_DecryptFinal_ex(ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
92     EVP_CIPHER_CTX_free(ctx);
93
94     if (!fOk) return false;
95
96     vchPlaintext.resize(nPLen + nFLen);
97     return true;
98 }
99
100
101 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
102 {
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))
107         return false;
108     return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
109 }
110
111 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
112 {
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))
117         return false;
118     return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
119 }