Remove headers.h
[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 <openssl/aes.h>
6 #include <openssl/evp.h>
7 #include <vector>
8 #include <string>
9 #ifdef WIN32
10 #include <windows.h>
11 #endif
12
13 #include "crypter.h"
14 #include "util.h"
15
16 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
17 {
18     if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
19         return false;
20
21     // 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)
22     // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
23     // 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.  
24     mlock(&chKey[0], sizeof chKey);
25     mlock(&chIV[0], sizeof chIV);
26
27     int i = 0;
28     if (nDerivationMethod == 0)
29         i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
30                           (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
31
32     if (i != (int)WALLET_CRYPTO_KEY_SIZE)
33     {
34         memset(&chKey, 0, sizeof chKey);
35         memset(&chIV, 0, sizeof chIV);
36         return false;
37     }
38
39     fKeySet = true;
40     return true;
41 }
42
43 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
44 {
45     if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
46         return false;
47
48     // Try to keep the keydata out of swap
49     // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
50     // 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.  
51     mlock(&chKey[0], sizeof chKey);
52     mlock(&chIV[0], sizeof chIV);
53
54     memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
55     memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
56
57     fKeySet = true;
58     return true;
59 }
60
61 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
62 {
63     if (!fKeySet)
64         return false;
65
66     // max ciphertext len for a n bytes of plaintext is
67     // n + AES_BLOCK_SIZE - 1 bytes
68     int nLen = vchPlaintext.size();
69     int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
70     vchCiphertext = std::vector<unsigned char> (nCLen);
71
72     EVP_CIPHER_CTX ctx;
73
74     bool fOk = true;
75
76     EVP_CIPHER_CTX_init(&ctx);
77     if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
78     if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
79     if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
80     EVP_CIPHER_CTX_cleanup(&ctx);
81
82     if (!fOk) return false;
83
84     vchCiphertext.resize(nCLen + nFLen);
85     return true;
86 }
87
88 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
89 {
90     if (!fKeySet)
91         return false;
92
93     // plaintext will always be equal to or lesser than length of ciphertext
94     int nLen = vchCiphertext.size();
95     int nPLen = nLen, nFLen = 0;
96
97     vchPlaintext = CKeyingMaterial(nPLen);
98
99     EVP_CIPHER_CTX ctx;
100
101     bool fOk = true;
102
103     EVP_CIPHER_CTX_init(&ctx);
104     if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
105     if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
106     if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
107     EVP_CIPHER_CTX_cleanup(&ctx);
108
109     if (!fOk) return false;
110
111     vchPlaintext.resize(nPLen + nFLen);
112     return true;
113 }
114
115
116 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
117 {
118     CCrypter cKeyCrypter;
119     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
120     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
121     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
122         return false;
123     return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
124 }
125
126 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
127 {
128     CCrypter cKeyCrypter;
129     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
130     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
131     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
132         return false;
133     return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
134 }