4
4
5
5
#include " crypter.h"
6
6
7
+ #include " crypto/aes.h"
7
8
#include " script/script.h"
8
9
#include " script/standard.h"
9
10
#include " util.h"
@@ -53,24 +54,15 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned
53
54
return false ;
54
55
55
56
// 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);
60
59
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);
72
65
73
- vchCiphertext.resize (nCLen + nFLen);
74
66
return true ;
75
67
}
76
68
@@ -81,23 +73,14 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM
81
73
82
74
// plaintext will always be equal to or lesser than length of ciphertext
83
75
int nLen = vchCiphertext.size ();
84
- int nPLen = nLen, nFLen = 0 ;
85
-
86
- vchPlaintext = CKeyingMaterial (nPLen);
87
76
88
- EVP_CIPHER_CTX ctx ;
77
+ vchPlaintext. resize (nLen) ;
89
78
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);
101
84
return true ;
102
85
}
103
86
0 commit comments