CScriptCheck : Add initializers
[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
10 #include "crypter.h"
11
12 #ifdef WIN32
13 #include <windows.h>
14 #endif
15
16 //
17 //CMasterKey
18 //
19
20 CMasterKey::CMasterKey()
21 {
22     // 25000 rounds is just under 0.1 seconds on a 1.86 GHz Pentium M
23     // ie slightly lower than the lowest hardware we need bother supporting
24     nDeriveIterations = 25000;
25     nDerivationMethod = 0;
26     vchOtherDerivationParameters = std::vector<unsigned char>(0);
27 }
28
29 //
30 //CCrypter
31 //
32
33 bool CCrypter::SetKeyFromPassphrase(const SecureString& strKeyData, const std::vector<unsigned char>& chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
34 {
35     if (nRounds < 1 || chSalt.size() != WALLET_CRYPTO_SALT_SIZE)
36         return false;
37
38     int i = 0;
39     if (nDerivationMethod == 0)
40     {
41         i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
42                           (const unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
43     }
44
45     if (i != (int)WALLET_CRYPTO_KEY_SIZE)
46     {
47         OPENSSL_cleanse(&chKey, sizeof chKey);
48         OPENSSL_cleanse(&chIV, sizeof chIV);
49         return false;
50     }
51
52     fKeySet = true;
53     return true;
54 }
55
56 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
57 {
58     if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
59         return false;
60
61     memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
62     memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
63
64     fKeySet = true;
65     return true;
66 }
67
68 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
69 {
70     if (!fKeySet)
71         return false;
72
73     // max ciphertext len for a n bytes of plaintext is
74     // n + AES_BLOCK_SIZE - 1 bytes
75     int nLen = vchPlaintext.size();
76     int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
77     vchCiphertext = std::vector<unsigned char> (nCLen);
78
79     EVP_CIPHER_CTX ctx;
80
81     bool fOk = true;
82
83     EVP_CIPHER_CTX_init(&ctx);
84     if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
85     if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
86     if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
87     EVP_CIPHER_CTX_cleanup(&ctx);
88
89     if (!fOk) return false;
90
91     vchCiphertext.resize(nCLen + nFLen);
92     return true;
93 }
94
95 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
96 {
97     if (!fKeySet)
98         return false;
99
100     // plaintext will always be equal to or lesser than length of ciphertext
101     int nLen = vchCiphertext.size();
102     int nPLen = nLen, nFLen = 0;
103
104     vchPlaintext = CKeyingMaterial(nPLen);
105
106     EVP_CIPHER_CTX ctx;
107
108     bool fOk = true;
109
110     EVP_CIPHER_CTX_init(&ctx);
111     if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
112     if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
113     if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
114     EVP_CIPHER_CTX_cleanup(&ctx);
115
116     if (!fOk) return false;
117
118     vchPlaintext.resize(nPLen + nFLen);
119     return true;
120 }
121
122 void CCrypter::CleanKey()
123 {
124     OPENSSL_cleanse(&chKey, sizeof chKey);
125     OPENSSL_cleanse(&chIV, sizeof chIV);
126     fKeySet = false;
127 }
128
129 CCrypter::CCrypter()
130 {
131     fKeySet = false;
132
133     // Try to keep the key data out of swap (and be a bit over-careful to keep the IV that we don't even use out of swap)
134     // Note that this does nothing about suspend-to-disk (which will put all our key data on disk)
135     // 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.
136     LockedPageManager::instance.LockRange(&chKey[0], sizeof chKey);
137     LockedPageManager::instance.LockRange(&chIV[0], sizeof chIV);
138 }
139
140 CCrypter::~CCrypter()
141 {
142     CleanKey();
143
144     LockedPageManager::instance.UnlockRange(&chKey[0], sizeof chKey);
145     LockedPageManager::instance.UnlockRange(&chIV[0], sizeof chIV);
146 }
147
148 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
149 {
150     CCrypter cKeyCrypter;
151     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
152     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
153     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
154         return false;
155     return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
156 }
157
158 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
159 {
160     CCrypter cKeyCrypter;
161     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
162     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
163     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
164         return false;
165     return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
166 }