2015-04-24 18:14:25 +02:00
|
|
|
#include <cryptopp/cryptopp/modes.h>
|
|
|
|
#include "AES256_CFB.h"
|
|
|
|
|
|
|
|
using CryptoPP::CFB_Mode;
|
|
|
|
using CryptoPP::AES;
|
|
|
|
|
|
|
|
namespace blockstore {
|
|
|
|
namespace encrypted {
|
|
|
|
|
|
|
|
constexpr unsigned int AES256_CFB::IV_SIZE;
|
|
|
|
|
2015-04-24 21:08:36 +02:00
|
|
|
Data AES256_CFB::encrypt(const byte *plaintext, unsigned int plaintextSize, const EncryptionKey &encKey) {
|
2015-04-24 18:14:25 +02:00
|
|
|
FixedSizeData<IV_SIZE> iv = FixedSizeData<IV_SIZE>::CreateRandom();
|
|
|
|
auto encryption = CFB_Mode<AES>::Encryption(encKey.data(), encKey.BINARY_LENGTH, iv.data());
|
2015-04-24 21:08:36 +02:00
|
|
|
Data ciphertext(ciphertextSize(plaintextSize));
|
|
|
|
std::memcpy(ciphertext.data(), iv.data(), IV_SIZE);
|
|
|
|
encryption.ProcessData((byte*)ciphertext.data() + IV_SIZE, plaintext, plaintextSize);
|
|
|
|
return ciphertext;
|
2015-04-24 18:14:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 21:08:36 +02:00
|
|
|
boost::optional<Data> AES256_CFB::decrypt(const byte *ciphertext, unsigned int ciphertextSize, const EncryptionKey &encKey) {
|
|
|
|
const byte *ciphertextIV = ciphertext;
|
|
|
|
const byte *ciphertextData = ciphertext + IV_SIZE;
|
|
|
|
auto decryption = CFB_Mode<AES>::Decryption((byte*)encKey.data(), encKey.BINARY_LENGTH, ciphertextIV);
|
|
|
|
Data plaintext(plaintextSize(ciphertextSize));
|
|
|
|
decryption.ProcessData((byte*)plaintext.data(), ciphertextData, plaintext.size());
|
|
|
|
return std::move(plaintext);
|
2015-04-24 18:14:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|