|
|
@ -4,6 +4,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include "crypter.h" |
|
|
|
#include "crypter.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "crypto/aes.h" |
|
|
|
#include "script/script.h" |
|
|
|
#include "script/script.h" |
|
|
|
#include "script/standard.h" |
|
|
|
#include "script/standard.h" |
|
|
|
#include "util.h" |
|
|
|
#include "util.h" |
|
|
@ -53,24 +54,15 @@ bool CCrypter::Encrypt(const CKeyingMaterial& vchPlaintext, std::vector<unsigned |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
// max ciphertext len for a n bytes of plaintext is
|
|
|
|
// max ciphertext len for a n bytes of plaintext is
|
|
|
|
// n + AES_BLOCK_SIZE - 1 bytes
|
|
|
|
// n + AES_BLOCKSIZE bytes
|
|
|
|
int nLen = vchPlaintext.size(); |
|
|
|
vchCiphertext.resize(vchPlaintext.size() + AES_BLOCKSIZE); |
|
|
|
int nCLen = nLen + AES_BLOCK_SIZE, nFLen = 0; |
|
|
|
|
|
|
|
vchCiphertext = std::vector<unsigned char> (nCLen); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX ctx; |
|
|
|
AES256CBCEncrypt enc(chKey, chIV, true); |
|
|
|
|
|
|
|
size_t nLen = enc.Encrypt(&vchPlaintext[0], vchPlaintext.size(), &vchCiphertext[0]); |
|
|
|
bool fOk = true; |
|
|
|
if(nLen < vchPlaintext.size()) |
|
|
|
|
|
|
|
return false; |
|
|
|
EVP_CIPHER_CTX_init(&ctx); |
|
|
|
vchCiphertext.resize(nLen); |
|
|
|
if (fOk) fOk = EVP_EncryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; |
|
|
|
|
|
|
|
if (fOk) fOk = EVP_EncryptUpdate(&ctx, &vchCiphertext[0], &nCLen, &vchPlaintext[0], nLen) != 0; |
|
|
|
|
|
|
|
if (fOk) fOk = EVP_EncryptFinal_ex(&ctx, (&vchCiphertext[0]) + nCLen, &nFLen) != 0; |
|
|
|
|
|
|
|
EVP_CIPHER_CTX_cleanup(&ctx); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!fOk) return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vchCiphertext.resize(nCLen + nFLen); |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -81,23 +73,14 @@ bool CCrypter::Decrypt(const std::vector<unsigned char>& vchCiphertext, CKeyingM |
|
|
|
|
|
|
|
|
|
|
|
// plaintext will always be equal to or lesser than length of ciphertext
|
|
|
|
// plaintext will always be equal to or lesser than length of ciphertext
|
|
|
|
int nLen = vchCiphertext.size(); |
|
|
|
int nLen = vchCiphertext.size(); |
|
|
|
int nPLen = nLen, nFLen = 0; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vchPlaintext = CKeyingMaterial(nPLen); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX ctx; |
|
|
|
vchPlaintext.resize(nLen); |
|
|
|
|
|
|
|
|
|
|
|
bool fOk = true; |
|
|
|
AES256CBCDecrypt dec(chKey, chIV, true); |
|
|
|
|
|
|
|
nLen = dec.Decrypt(&vchCiphertext[0], vchCiphertext.size(), &vchPlaintext[0]); |
|
|
|
EVP_CIPHER_CTX_init(&ctx); |
|
|
|
if(nLen == 0) |
|
|
|
if (fOk) fOk = EVP_DecryptInit_ex(&ctx, EVP_aes_256_cbc(), NULL, chKey, chIV) != 0; |
|
|
|
return false; |
|
|
|
if (fOk) fOk = EVP_DecryptUpdate(&ctx, &vchPlaintext[0], &nPLen, &vchCiphertext[0], nLen) != 0; |
|
|
|
vchPlaintext.resize(nLen); |
|
|
|
if (fOk) fOk = EVP_DecryptFinal_ex(&ctx, (&vchPlaintext[0]) + nPLen, &nFLen) != 0; |
|
|
|
|
|
|
|
EVP_CIPHER_CTX_cleanup(&ctx); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!fOk) return false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
vchPlaintext.resize(nPLen + nFLen); |
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|