Skip to content

Commit 9049cde

Browse files
theunisipa
authored andcommitted
crypter: hook up the new aes cbc classes
1 parent fb96831 commit 9049cde

File tree

1 file changed

+14
-31
lines changed

1 file changed

+14
-31
lines changed

src/wallet/crypter.cpp

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "crypter.h"
66

7+
#include "crypto/aes.h"
78
#include "script/script.h"
89
#include "script/standard.h"
910
#include "util.h"
@@ -53,24 +54,15 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
5354
return false;
5455

5556
// max ciphertext len for a n bytes of plaintext is
56-
// n + AES_BLOCK_SIZE - 1 bytes
57-
int nLen = vchPlaintext.size();
58-
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0;
59-
vchCiphertext = std::vector<unsigned char> (nCLen);
57+
// n + AES_BLOCKSIZE bytes
58+
vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE);
6059

61-
EVP_CIPHER_CTX ctx;
62-
63-
bool fOk = true;
64-
65-
EVP_CIPHER_CTX_init(&ctx);
66-
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
67-
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0;
68-
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0;
69-
EVP_CIPHER_CTX_cleanup(&ctx);
70-
71-
if (!fOk) return false;
60+
AES256CBCEncrypt enc(chKey, chIV, true);
61+
size_t nLen = enc.Encrypt(&vchPlaintext[0], vchPlaintext.size(), &vchCiphertext[0]);
62+
if(nLen < vchPlaintext.size())
63+
return false;
64+
vchCiphertext.resize(nLen);
7265

73-
vchCiphertext.resize(nCLen + nFLen);
7466
return true;
7567
}
7668

@@ -81,23 +73,14 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
8173

8274
// plaintext will always be equal to or lesser than length of ciphertext
8375
int nLen = vchCiphertext.size();
84-
int nPLen = nLen, nFLen = 0;
85-
86-
vchPlaintext = CKeyingMaterial(nPLen);
8776

88-
EVP_CIPHER_CTX ctx;
77+
vchPlaintext.resize(nLen);
8978

90-
bool fOk = true;
91-
92-
EVP_CIPHER_CTX_init(&ctx);
93-
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0;
94-
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0;
95-
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0;
96-
EVP_CIPHER_CTX_cleanup(&ctx);
97-
98-
if (!fOk) return false;
99-
100-
vchPlaintext.resize(nPLen + nFLen);
79+
AES256CBCDecrypt dec(chKey, chIV, true);
80+
nLen = dec.Decrypt(&vchCiphertext[0], vchCiphertext.size(), &vchPlaintext[0]);
81+
if(nLen == 0)
82+
return false;
83+
vchPlaintext.resize(nLen);
10184
return true;
10285
}
10386

0 commit comments

Comments
 (0)