Add scrypt+sha512 key derivation method.
[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 "scrypt.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     int i = 0;
22     if (nDerivationMethod == 0)
23     {
24         i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
25                           (unsigned char *)&strKeyData[0], strKeyData.size(), nRounds, chKey, chIV);
26     }
27
28     if (nDerivationMethod == 1)
29     {
30         // Passphrase conversion
31         uint256 scryptHash = scrypt_salted_multiround_hash((const void*)strKeyData.c_str(), strKeyData.size(), &chSalt[0], 8, nRounds);
32
33         i = EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha512(), &chSalt[0],
34                           (unsigned char *)&scryptHash, sizeof scryptHash, nRounds, chKey, chIV);
35         memset(&scryptHash, 0, sizeof scryptHash);
36     }
37
38
39     if (i != (int)WALLET_CRYPTO_KEY_SIZE)
40     {
41         memset(&chKey, 0, sizeof chKey);
42         memset(&chIV, 0, sizeof chIV);
43         return false;
44     }
45
46     fKeySet = true;
47     return true;
48 }
49
50 bool CCrypter::SetKey(const CKeyingMaterial& chNewKey, const std::vector<unsigned char>& chNewIV)
51 {
52     if (chNewKey.size() != WALLET_CRYPTO_KEY_SIZE || chNewIV.size() != WALLET_CRYPTO_KEY_SIZE)
53         return false;
54
55     memcpy(&chKey[0], &chNewKey[0], sizeof chKey);
56     memcpy(&chIV[0], &chNewIV[0], sizeof chIV);
57
58     fKeySet = true;
59     return true;
60 }
61
62 bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned char> &vchCiphertext)
63 {
64     if (!fKeySet)
65         return false;
66
67     // max ciphertext len for a n bytes of plaintext is
68     // n + AES_BLOCK_SIZE - 1 bytes
69     int nLen = vchPlaintext.size();
70     int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
71     vchCiphertext = std::vector<unsigned char> (nCLen);
72
73     EVP_CIPHER_CTX ctx;
74
75     bool fOk = true;
76
77     EVP_CIPHER_CTX_init(&ctx);
78     if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
79     if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen);
80     if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0])+nCLen, &nFLen);
81     EVP_CIPHER_CTX_cleanup(&ctx);
82
83     if (!fOk) return false;
84
85     vchCiphertext.resize(nCLen + nFLen);
86     return true;
87 }
88
89 bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingMaterial& vchPlaintext)
90 {
91     if (!fKeySet)
92         return false;
93
94     // plaintext will always be equal to or lesser than length of ciphertext
95     int nLen = vchCiphertext.size();
96     int nPLen = nLen, nFLen = 0;
97
98     vchPlaintext = CKeyingMaterial(nPLen);
99
100     EVP_CIPHER_CTX ctx;
101
102     bool fOk = true;
103
104     EVP_CIPHER_CTX_init(&ctx);
105     if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV);
106     if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen);
107     if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0])+nPLen, &nFLen);
108     EVP_CIPHER_CTX_cleanup(&ctx);
109
110     if (!fOk) return false;
111
112     vchPlaintext.resize(nPLen + nFLen);
113     return true;
114 }
115
116
117 bool EncryptSecret(CKeyingMaterial& vMasterKey, const CSecret &vchPlaintext, const uint256& nIV, std::vector<unsigned char> &vchCiphertext)
118 {
119     CCrypter cKeyCrypter;
120     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
121     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
122     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
123         return false;
124     return cKeyCrypter.Encrypt((CKeyingMaterial)vchPlaintext, vchCiphertext);
125 }
126
127 bool DecryptSecret(const CKeyingMaterial& vMasterKey, const std::vector<unsigned char>& vchCiphertext, const uint256& nIV, CSecret& vchPlaintext)
128 {
129     CCrypter cKeyCrypter;
130     std::vector<unsigned char> chIV(WALLET_CRYPTO_KEY_SIZE);
131     memcpy(&chIV[0], &nIV, WALLET_CRYPTO_KEY_SIZE);
132     if(!cKeyCrypter.SetKey(vMasterKey, chIV))
133         return false;
134     return cKeyCrypter.Decrypt(vchCiphertext, *((CKeyingMaterial*)&vchPlaintext));
135 }