Improve readability
This commit is contained in:
parent
016d66c722
commit
4b7cb59e30
@ -33,9 +33,8 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
EvpCipherCtx AES256_CBC::init(Mode mode,
|
EvpCipherCtx AES256_CBC::init(Mode mode, const raw_data& key,
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& iv) {
|
||||||
const std::vector<unsigned char>& iv) {
|
|
||||||
if (key.size() != kKeySize) {
|
if (key.size() != kKeySize) {
|
||||||
throw std::runtime_error("Wrong key size");
|
throw std::runtime_error("Wrong key size");
|
||||||
}
|
}
|
||||||
@ -60,12 +59,11 @@ EvpCipherCtx AES256_CBC::init(Mode mode,
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t AES256_CBC::process_chunk(
|
size_t AES256_CBC::process_chunk(Mode mode, EvpCipherCtx& ctx,
|
||||||
Mode mode, EvpCipherCtx& ctx,
|
raw_data::const_iterator begin,
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
raw_data::const_iterator end,
|
||||||
std::vector<unsigned char>::const_iterator end,
|
raw_data& output, size_t output_offset,
|
||||||
std::vector<unsigned char>& output, size_t output_offset,
|
bool resize_in, bool resize_out) {
|
||||||
bool resize_in, bool resize_out) {
|
|
||||||
auto chunk_size = end - begin;
|
auto chunk_size = end - begin;
|
||||||
int len;
|
int len;
|
||||||
if (resize_in) {
|
if (resize_in) {
|
||||||
@ -91,8 +89,7 @@ size_t AES256_CBC::process_chunk(
|
|||||||
}
|
}
|
||||||
|
|
||||||
size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx,
|
size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx,
|
||||||
std::vector<unsigned char>& output,
|
raw_data& output, size_t output_offset,
|
||||||
size_t output_offset,
|
|
||||||
bool resize_in, bool resize_out) {
|
bool resize_in, bool resize_out) {
|
||||||
int len;
|
int len;
|
||||||
if (resize_in) {
|
if (resize_in) {
|
||||||
@ -115,12 +112,11 @@ size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx,
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t AES256_CBC::process_all(
|
size_t AES256_CBC::process_all(Mode mode, EvpCipherCtx& ctx,
|
||||||
Mode mode, EvpCipherCtx& ctx,
|
raw_data::const_iterator begin,
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
raw_data::const_iterator end,
|
||||||
std::vector<unsigned char>::const_iterator end,
|
raw_data& output, size_t output_offset,
|
||||||
std::vector<unsigned char>& output, size_t output_offset,
|
bool resize_in, bool resize_out) {
|
||||||
bool resize_in, bool resize_out) {
|
|
||||||
int len = process_chunk(mode, ctx, begin, end, output, output_offset,
|
int len = process_chunk(mode, ctx, begin, end, output, output_offset,
|
||||||
resize_in, false);
|
resize_in, false);
|
||||||
len += process_final(mode, ctx, output, output_offset + len, false,
|
len += process_final(mode, ctx, output, output_offset + len, false,
|
||||||
@ -128,32 +124,27 @@ size_t AES256_CBC::process_all(
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> AES256_CBC::process(
|
raw_data AES256_CBC::process(const raw_data& inputdata) {
|
||||||
const std::vector<unsigned char>& inputdata) {
|
|
||||||
if (mode == Mode::kEncrypt)
|
if (mode == Mode::kEncrypt)
|
||||||
return encrypt(inputdata);
|
return encrypt(inputdata);
|
||||||
else
|
else
|
||||||
return decrypt(inputdata);
|
return decrypt(inputdata);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> AES256_CBC::encrypt(
|
raw_data AES256_CBC::encrypt(const raw_data& plaintext) {
|
||||||
const std::vector<unsigned char>& plaintext) {
|
|
||||||
return encrypt(encryption_key, horcrux::generate_random(kIvSize),
|
return encrypt(encryption_key, horcrux::generate_random(kIvSize),
|
||||||
plaintext);
|
plaintext);
|
||||||
}
|
}
|
||||||
std::vector<unsigned char> AES256_CBC::decrypt(
|
raw_data AES256_CBC::decrypt(const raw_data& ciphertext) {
|
||||||
const std::vector<unsigned char>& ciphertext) {
|
|
||||||
return decrypt(encryption_key, ciphertext);
|
return decrypt(encryption_key, ciphertext);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> AES256_CBC::encrypt(
|
raw_data AES256_CBC::encrypt(const raw_data& key, const raw_data& iv,
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& input) {
|
||||||
const std::vector<unsigned char>& iv,
|
|
||||||
const std::vector<unsigned char>& input) {
|
|
||||||
auto ctx = init(Mode::kEncrypt, key, iv);
|
auto ctx = init(Mode::kEncrypt, key, iv);
|
||||||
|
|
||||||
// Make sure ouput is large enough to contain IV + encrypted data + padding
|
// Make sure ouput is large enough to contain IV + encrypted data + padding
|
||||||
std::vector<unsigned char> output(input.size() + (2 * kIvSize));
|
raw_data output(input.size() + (2 * kIvSize));
|
||||||
std::copy(iv.begin(), iv.end(), output.begin());
|
std::copy(iv.begin(), iv.end(), output.begin());
|
||||||
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output,
|
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output,
|
||||||
kIvSize);
|
kIvSize);
|
||||||
@ -162,12 +153,11 @@ std::vector<unsigned char> AES256_CBC::encrypt(
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
int AES256_CBC::encrypt(const std::vector<unsigned char>& key,
|
int AES256_CBC::encrypt(const raw_data& key, const raw_data& iv,
|
||||||
const std::vector<unsigned char>& iv,
|
|
||||||
std::istream input, const size_t input_len,
|
std::istream input, const size_t input_len,
|
||||||
std::ostream output, size_t& output_len) {
|
std::ostream output, size_t& output_len) {
|
||||||
auto inbuf = std::vector<unsigned char>(input_len);
|
auto inbuf = raw_data(input_len);
|
||||||
auto outbuf = std::vector<unsigned char>(input_len + 16);
|
auto outbuf = raw_data(input_len + 16);
|
||||||
input.read(reinterpret_cast<char*>(inbuf.data()), input_len);
|
input.read(reinterpret_cast<char*>(inbuf.data()), input_len);
|
||||||
|
|
||||||
encrypt(key, iv, inbuf, outbuf);
|
encrypt(key, iv, inbuf, outbuf);
|
||||||
@ -178,28 +168,21 @@ int AES256_CBC::encrypt(const std::vector<unsigned char>& key,
|
|||||||
}
|
}
|
||||||
|
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> AES256_CBC::decrypt(
|
raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& ciphertext) {
|
||||||
const std::vector<unsigned char>& key,
|
raw_data iv(ciphertext.begin(), ciphertext.begin() + kIvSize);
|
||||||
const std::vector<unsigned char>& ciphertext) {
|
|
||||||
std::vector<unsigned char> iv(ciphertext.begin(),
|
|
||||||
ciphertext.begin() + kIvSize);
|
|
||||||
return decrypt(key, iv, ciphertext.begin() + kIvSize, ciphertext.end());
|
return decrypt(key, iv, ciphertext.begin() + kIvSize, ciphertext.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> AES256_CBC::decrypt(
|
raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& iv,
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& ciphertext) {
|
||||||
const std::vector<unsigned char>& iv,
|
|
||||||
const std::vector<unsigned char>& ciphertext) {
|
|
||||||
return decrypt(key, iv, ciphertext.begin(), ciphertext.end());
|
return decrypt(key, iv, ciphertext.begin(), ciphertext.end());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> AES256_CBC::decrypt(
|
raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& iv,
|
||||||
const std::vector<unsigned char>& key,
|
raw_data::const_iterator begin,
|
||||||
const std::vector<unsigned char>& iv,
|
raw_data::const_iterator end) {
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
|
||||||
std::vector<unsigned char>::const_iterator end) {
|
|
||||||
auto ctx = init(Mode::kDecrypt, key, iv);
|
auto ctx = init(Mode::kDecrypt, key, iv);
|
||||||
std::vector<unsigned char> output;
|
raw_data output;
|
||||||
process_all(Mode::kDecrypt, ctx, begin, end, output, 0);
|
process_all(Mode::kDecrypt, ctx, begin, end, output, 0);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
84
src/crypto.h
84
src/crypto.h
@ -25,7 +25,7 @@ public:
|
|||||||
/** @brief create Cipher
|
/** @brief create Cipher
|
||||||
* @param m Cipher mode (encrypt/decrypt)
|
* @param m Cipher mode (encrypt/decrypt)
|
||||||
* @param key use provided key for encryption/decryption */
|
* @param key use provided key for encryption/decryption */
|
||||||
Cipher(Mode m, const std::vector<unsigned char>& key) :
|
Cipher(Mode m, const raw_data& key) :
|
||||||
mode(m), encryption_key(key) {}
|
mode(m), encryption_key(key) {}
|
||||||
|
|
||||||
/** @brief default destructor */
|
/** @brief default destructor */
|
||||||
@ -33,7 +33,7 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
/** @brief key used for encryption / decryption */
|
/** @brief key used for encryption / decryption */
|
||||||
std::vector<unsigned char> encryption_key;
|
raw_data encryption_key;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** @brief Cipher mode (encrypt or decrypt) */
|
/** @brief Cipher mode (encrypt or decrypt) */
|
||||||
@ -42,7 +42,7 @@ public:
|
|||||||
/** @brief get Cipher encryption key
|
/** @brief get Cipher encryption key
|
||||||
* @return the encryption key
|
* @return the encryption key
|
||||||
*/
|
*/
|
||||||
const std::vector<unsigned char>& get_encryption_key() {
|
const raw_data& get_encryption_key() {
|
||||||
return encryption_key;
|
return encryption_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,22 +50,22 @@ public:
|
|||||||
* @param inputdata buffer containing data to process
|
* @param inputdata buffer containing data to process
|
||||||
* @return processed data
|
* @return processed data
|
||||||
*/
|
*/
|
||||||
virtual std::vector<unsigned char> process(
|
virtual raw_data process(
|
||||||
const std::vector<unsigned char>& inputdata) = 0;
|
const raw_data& inputdata) = 0;
|
||||||
|
|
||||||
/** @brief encrypt the content of a buffer using Cipher key
|
/** @brief encrypt the content of a buffer using Cipher key
|
||||||
* @param plaintext the input buffer
|
* @param plaintext the input buffer
|
||||||
* @return encrypted data
|
* @return encrypted data
|
||||||
*/
|
*/
|
||||||
virtual std::vector<unsigned char> encrypt(
|
virtual raw_data encrypt(
|
||||||
const std::vector<unsigned char>& plaintext) = 0;
|
const raw_data& plaintext) = 0;
|
||||||
|
|
||||||
/** @brief decrypt the content of a buffer using Cipher key
|
/** @brief decrypt the content of a buffer using Cipher key
|
||||||
* @param decrypt the input buffer
|
* @param decrypt the input buffer
|
||||||
* @return decrypted data
|
* @return decrypted data
|
||||||
*/
|
*/
|
||||||
virtual std::vector<unsigned char> decrypt(
|
virtual raw_data decrypt(
|
||||||
const std::vector<unsigned char>& ciphertext) = 0;
|
const raw_data& ciphertext) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// forward declaration
|
// forward declaration
|
||||||
@ -98,7 +98,7 @@ public:
|
|||||||
/** @brief Create cipher in decrypt mode using provided key
|
/** @brief Create cipher in decrypt mode using provided key
|
||||||
* @param key 256 bit key
|
* @param key 256 bit key
|
||||||
*/
|
*/
|
||||||
explicit AES256_CBC(const std::vector<unsigned char>& key)
|
explicit AES256_CBC(const raw_data& key)
|
||||||
: Cipher(Mode::kDecrypt, key) {
|
: Cipher(Mode::kDecrypt, key) {
|
||||||
if (key.size() != kKeySize)
|
if (key.size() != kKeySize)
|
||||||
throw std::invalid_argument("Invalid key size");
|
throw std::invalid_argument("Invalid key size");
|
||||||
@ -110,22 +110,22 @@ public:
|
|||||||
* @param inputdata buffer containing data to process
|
* @param inputdata buffer containing data to process
|
||||||
* @return processed data
|
* @return processed data
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> process(
|
raw_data process(
|
||||||
const std::vector<unsigned char>& inputdata) override;
|
const raw_data& inputdata) override;
|
||||||
|
|
||||||
/** @brief encrypt the content of a buffer using Cipher key
|
/** @brief encrypt the content of a buffer using Cipher key
|
||||||
* @param plaintext the input buffer
|
* @param plaintext the input buffer
|
||||||
* @return encrypted data
|
* @return encrypted data
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> encrypt(
|
raw_data encrypt(
|
||||||
const std::vector<unsigned char>& plaintext) override;
|
const raw_data& plaintext) override;
|
||||||
|
|
||||||
/** @brief decrypt the content of a buffer using Cipher key
|
/** @brief decrypt the content of a buffer using Cipher key
|
||||||
* @param decrypt the input buffer
|
* @param decrypt the input buffer
|
||||||
* @return decrypted data
|
* @return decrypted data
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> decrypt(
|
raw_data decrypt(
|
||||||
const std::vector<unsigned char>& ciphertext) override;
|
const raw_data& ciphertext) override;
|
||||||
|
|
||||||
/** @brief Read the whole input and return a buffer with the encrypted data
|
/** @brief Read the whole input and return a buffer with the encrypted data
|
||||||
* @param key the encryption key
|
* @param key the encryption key
|
||||||
@ -134,13 +134,13 @@ public:
|
|||||||
* @param input the input buffer
|
* @param input the input buffer
|
||||||
* @return the encrypted data buffer
|
* @return the encrypted data buffer
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> encrypt(const std::vector<unsigned char>& key,
|
raw_data encrypt(const raw_data& key,
|
||||||
const std::vector<unsigned char>& iv,
|
const raw_data& iv,
|
||||||
const std::vector<unsigned char>& input);
|
const raw_data& input);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
int encrypt(const std::vector<unsigned char>& key,
|
int encrypt(const raw_data& key,
|
||||||
const std::vector<unsigned char>& iv,
|
const raw_data& iv,
|
||||||
std::istream input, const size_t input_len,
|
std::istream input, const size_t input_len,
|
||||||
std::ostream output, size_t& output_len);
|
std::ostream output, size_t& output_len);
|
||||||
|
|
||||||
@ -151,9 +151,9 @@ public:
|
|||||||
* @param ciphertext the input buffer
|
* @param ciphertext the input buffer
|
||||||
* @return the encrypted data buffer
|
* @return the encrypted data buffer
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> decrypt(
|
raw_data decrypt(
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& key,
|
||||||
const std::vector<unsigned char>& ciphertext);
|
const raw_data& ciphertext);
|
||||||
|
|
||||||
/** @brief Read the whole input and return a buffer with the decrypted data.
|
/** @brief Read the whole input and return a buffer with the decrypted data.
|
||||||
* ciphertext does not contain the iv.
|
* ciphertext does not contain the iv.
|
||||||
@ -162,10 +162,10 @@ public:
|
|||||||
* @param ciphertext the input buffer
|
* @param ciphertext the input buffer
|
||||||
* @return the encrypted data buffer
|
* @return the encrypted data buffer
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> decrypt(
|
raw_data decrypt(
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& key,
|
||||||
const std::vector<unsigned char>& iv,
|
const raw_data& iv,
|
||||||
const std::vector<unsigned char>& ciphertext);
|
const raw_data& ciphertext);
|
||||||
|
|
||||||
/** @brief Read some input from begin to end and return a buffer with the
|
/** @brief Read some input from begin to end and return a buffer with the
|
||||||
* decrypted data. Output data is finalized.
|
* decrypted data. Output data is finalized.
|
||||||
@ -175,11 +175,11 @@ public:
|
|||||||
* @param end end of the input buffer
|
* @param end end of the input buffer
|
||||||
* @return the encrypted data buffer
|
* @return the encrypted data buffer
|
||||||
*/
|
*/
|
||||||
std::vector<unsigned char> decrypt(
|
raw_data decrypt(
|
||||||
const std::vector<unsigned char>& key,
|
const raw_data& key,
|
||||||
const std::vector<unsigned char>& iv,
|
const raw_data& iv,
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
raw_data::const_iterator begin,
|
||||||
std::vector<unsigned char>::const_iterator end);
|
raw_data::const_iterator end);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/** @brief Create a Cipher context object needed for enc/dec operations
|
/** @brief Create a Cipher context object needed for enc/dec operations
|
||||||
@ -188,8 +188,8 @@ private:
|
|||||||
* @param the initialization vector
|
* @param the initialization vector
|
||||||
* @return the Cipher context object
|
* @return the Cipher context object
|
||||||
*/
|
*/
|
||||||
EvpCipherCtx init(Mode mode, const std::vector<unsigned char>& key,
|
EvpCipherCtx init(Mode mode, const raw_data& key,
|
||||||
const std::vector<unsigned char>& iv);
|
const raw_data& iv);
|
||||||
|
|
||||||
/** @brief process a chunk of input
|
/** @brief process a chunk of input
|
||||||
* @param mode encrypt or decrypt
|
* @param mode encrypt or decrypt
|
||||||
@ -204,9 +204,9 @@ private:
|
|||||||
* @param resize_out shrink the output buffer at the end
|
* @param resize_out shrink the output buffer at the end
|
||||||
*/
|
*/
|
||||||
size_t process_chunk(Mode mode, EvpCipherCtx& ctx,
|
size_t process_chunk(Mode mode, EvpCipherCtx& ctx,
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
raw_data::const_iterator begin,
|
||||||
std::vector<unsigned char>::const_iterator end,
|
raw_data::const_iterator end,
|
||||||
std::vector<unsigned char>& output,
|
raw_data& output,
|
||||||
size_t output_offset,
|
size_t output_offset,
|
||||||
bool resize_in = true, bool resize_out = true);
|
bool resize_in = true, bool resize_out = true);
|
||||||
|
|
||||||
@ -222,7 +222,7 @@ private:
|
|||||||
* @param resize_out shrink the output buffer at the end
|
* @param resize_out shrink the output buffer at the end
|
||||||
*/
|
*/
|
||||||
size_t process_final(Mode mode, EvpCipherCtx& ctx,
|
size_t process_final(Mode mode, EvpCipherCtx& ctx,
|
||||||
std::vector<unsigned char>& output,
|
raw_data& output,
|
||||||
size_t output_offset,
|
size_t output_offset,
|
||||||
bool resize_in = true, bool resize_out = true);
|
bool resize_in = true, bool resize_out = true);
|
||||||
|
|
||||||
@ -239,9 +239,9 @@ private:
|
|||||||
* @param resize_out shrink the output buffer at the end
|
* @param resize_out shrink the output buffer at the end
|
||||||
*/
|
*/
|
||||||
size_t process_all(Mode mode, EvpCipherCtx& ctx,
|
size_t process_all(Mode mode, EvpCipherCtx& ctx,
|
||||||
std::vector<unsigned char>::const_iterator begin,
|
raw_data::const_iterator begin,
|
||||||
std::vector<unsigned char>::const_iterator end,
|
raw_data::const_iterator end,
|
||||||
std::vector<unsigned char>& output, size_t output_offset,
|
raw_data& output, size_t output_offset,
|
||||||
bool resize_in = true, bool resize_out = true);
|
bool resize_in = true, bool resize_out = true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
12
src/io.cpp
12
src/io.cpp
@ -17,8 +17,8 @@ FsPlainInput::FsPlainInput(const std::string& path)
|
|||||||
file_stream = std::ifstream(file_path, std::ios::binary);
|
file_stream = std::ifstream(file_path, std::ios::binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> FsPlainInput::read() {
|
raw_data FsPlainInput::read() {
|
||||||
std::vector<unsigned char> result(file_size);
|
raw_data result(file_size);
|
||||||
file_stream.read(reinterpret_cast<char*>(result.data()), file_size);
|
file_stream.read(reinterpret_cast<char*>(result.data()), file_size);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -40,8 +40,8 @@ FsCryptoInput::FsCryptoInput(const std::vector<std::string>& filenames) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> FsCryptoInput::read() {
|
raw_data FsCryptoInput::read() {
|
||||||
std::vector<unsigned char> result(total_size);
|
raw_data result(total_size);
|
||||||
size_t data_read{0};
|
size_t data_read{0};
|
||||||
for (auto& f : file_paths) {
|
for (auto& f : file_paths) {
|
||||||
std::ifstream ifstream(f.first);
|
std::ifstream ifstream(f.first);
|
||||||
@ -58,7 +58,7 @@ FsPlainOutput::FsPlainOutput(const std::string& filename)
|
|||||||
throw std::invalid_argument("Output file is not a regular file");
|
throw std::invalid_argument("Output file is not a regular file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
size_t FsPlainOutput::write(const std::vector<unsigned char>& to_write) {
|
size_t FsPlainOutput::write(const raw_data& to_write) {
|
||||||
std::ofstream out(file_path, std::ios::binary);
|
std::ofstream out(file_path, std::ios::binary);
|
||||||
out.write(reinterpret_cast<const char*>(to_write.data()), to_write.size());
|
out.write(reinterpret_cast<const char*>(to_write.data()), to_write.size());
|
||||||
return to_write.size();
|
return to_write.size();
|
||||||
@ -77,7 +77,7 @@ FsCryptoOutput::FsCryptoOutput(const std::string& folder, const int horcrux_num,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t FsCryptoOutput::write(const std::vector<unsigned char>& to_write) {
|
size_t FsCryptoOutput::write(const raw_data& to_write) {
|
||||||
size = to_write.size() / num;
|
size = to_write.size() / num;
|
||||||
std::ofstream f;
|
std::ofstream f;
|
||||||
created_files.clear();
|
created_files.clear();
|
||||||
|
13
src/io.h
13
src/io.h
@ -5,6 +5,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
namespace horcrux {
|
namespace horcrux {
|
||||||
|
|
||||||
@ -15,7 +16,7 @@ public:
|
|||||||
virtual ~Input() = default;
|
virtual ~Input() = default;
|
||||||
/** @brief Read from the input
|
/** @brief Read from the input
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
virtual std::vector<unsigned char> read() = 0;
|
virtual raw_data read() = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Base Class for Output
|
/** @brief Base Class for Output
|
||||||
@ -26,7 +27,7 @@ public:
|
|||||||
/** @brief Write to the output
|
/** @brief Write to the output
|
||||||
* @param data to write
|
* @param data to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
virtual size_t write(const std::vector<unsigned char>& data) = 0;
|
virtual size_t write(const raw_data& data) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Input that reads data from a regular file */
|
/** @brief Input that reads data from a regular file */
|
||||||
@ -49,7 +50,7 @@ public:
|
|||||||
|
|
||||||
/** @brief Read from the input
|
/** @brief Read from the input
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
std::vector<unsigned char> read() override;
|
raw_data read() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Input that reads scattered data from multiple files */
|
/** @brief Input that reads scattered data from multiple files */
|
||||||
@ -71,7 +72,7 @@ public:
|
|||||||
/** @brief Read from the input
|
/** @brief Read from the input
|
||||||
* @details basically concatenate the input files.
|
* @details basically concatenate the input files.
|
||||||
* @return new buffer containing all read data */
|
* @return new buffer containing all read data */
|
||||||
std::vector<unsigned char> read() override;
|
raw_data read() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Output that writes to a single regular file */
|
/** @brief Output that writes to a single regular file */
|
||||||
@ -91,7 +92,7 @@ public:
|
|||||||
/** @brief Write to the output
|
/** @brief Write to the output
|
||||||
* @param data to write
|
* @param data to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
size_t write(const std::vector<unsigned char>& to_write) override;
|
size_t write(const raw_data& to_write) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @brief Output that splits the data in equal size chunks and writes them to
|
/** @brief Output that splits the data in equal size chunks and writes them to
|
||||||
@ -128,7 +129,7 @@ public:
|
|||||||
/** @brief Write to the output
|
/** @brief Write to the output
|
||||||
* @param data to write
|
* @param data to write
|
||||||
* @return written data */
|
* @return written data */
|
||||||
size_t write(const std::vector<unsigned char>& to_write) override;
|
size_t write(const raw_data& to_write) override;
|
||||||
};
|
};
|
||||||
}; // namespace horcrux
|
}; // namespace horcrux
|
||||||
#endif // HORCRUX_SRC_IO_H
|
#endif // HORCRUX_SRC_IO_H
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
#include <boost/archive/iterators/base64_from_binary.hpp>
|
#include <boost/archive/iterators/base64_from_binary.hpp>
|
||||||
#include <boost/archive/iterators/transform_width.hpp>
|
#include <boost/archive/iterators/transform_width.hpp>
|
||||||
|
|
||||||
std::string horcrux::to_base64(const std::vector<unsigned char>& binary) {
|
std::string horcrux::to_base64(const raw_data& binary) {
|
||||||
using namespace boost::archive::iterators;
|
using namespace boost::archive::iterators;
|
||||||
using It = base64_from_binary<transform_width<
|
using It = base64_from_binary<transform_width<
|
||||||
std::vector<unsigned char>::const_iterator, 6, 8>>;
|
std::vector<unsigned char>::const_iterator, 6, 8>>;
|
||||||
@ -13,7 +13,7 @@ std::string horcrux::to_base64(const std::vector<unsigned char>& binary) {
|
|||||||
return base64.append((3 - binary.size() % 3) % 3, '=');
|
return base64.append((3 - binary.size() % 3) % 3, '=');
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> horcrux::from_base64(const std::string& base64) {
|
horcrux::raw_data horcrux::from_base64(const std::string& base64) {
|
||||||
using namespace boost::archive::iterators;
|
using namespace boost::archive::iterators;
|
||||||
using It = transform_width<binary_from_base64<std::string::const_iterator>,
|
using It = transform_width<binary_from_base64<std::string::const_iterator>,
|
||||||
8, 6>;
|
8, 6>;
|
||||||
@ -30,8 +30,8 @@ std::vector<unsigned char> horcrux::from_base64(const std::string& base64) {
|
|||||||
return binary;
|
return binary;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<unsigned char> horcrux::generate_random(const size_t size) {
|
horcrux::raw_data horcrux::generate_random(const size_t size) {
|
||||||
std::vector<unsigned char> res(size);
|
horcrux::raw_data res(size);
|
||||||
if (!RAND_bytes(res.data(), size)) {
|
if (!RAND_bytes(res.data(), size)) {
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,11 @@
|
|||||||
|
|
||||||
namespace horcrux {
|
namespace horcrux {
|
||||||
|
|
||||||
std::string to_base64(const std::vector<unsigned char>& binary);
|
typedef std::vector<unsigned char> raw_data;
|
||||||
std::vector<unsigned char> from_base64(const std::string& base64);
|
|
||||||
std::vector<unsigned char> generate_random(const size_t size);
|
std::string to_base64(const raw_data& binary);
|
||||||
|
raw_data from_base64(const std::string& base64);
|
||||||
|
raw_data generate_random(const size_t size);
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif // HORCRUX_SRC_UTILS_H
|
#endif // HORCRUX_SRC_UTILS_H
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
using namespace horcrux;
|
using namespace horcrux;
|
||||||
TEST(CryptoTest, encrypt1){
|
TEST(CryptoTest, encrypt1){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
|
|
||||||
AES256_CBC a;
|
AES256_CBC a;
|
||||||
auto output = a.encrypt(key, iv, plaintext);
|
auto output = a.encrypt(key, iv, plaintext);
|
||||||
@ -19,11 +19,11 @@ TEST(CryptoTest, encrypt1){
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(CryptoTest, encrypt2){
|
TEST(CryptoTest, encrypt2){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
//size_t output_len = plaintext.size() + 16;
|
//size_t output_len = plaintext.size() + 16;
|
||||||
//std::vector<unsigned char> output(output_len);
|
//raw_data output(output_len);
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
|
|
||||||
AES256_CBC a;
|
AES256_CBC a;
|
||||||
auto output = a.encrypt(key, iv, plaintext);
|
auto output = a.encrypt(key, iv, plaintext);
|
||||||
@ -35,11 +35,11 @@ TEST(CryptoTest, encrypt2){
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(CryptoTest, encrypt3){
|
TEST(CryptoTest, encrypt3){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
//size_t output_len = plaintext.size() + 16;
|
//size_t output_len = plaintext.size() + 16;
|
||||||
//std::vector<unsigned char> output(output_len);
|
//raw_data output(output_len);
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
|
|
||||||
AES256_CBC a(key);
|
AES256_CBC a(key);
|
||||||
auto output = a.encrypt(plaintext);
|
auto output = a.encrypt(plaintext);
|
||||||
@ -50,9 +50,9 @@ TEST(CryptoTest, encrypt3){
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(CryptoTest, decrypt1){
|
TEST(CryptoTest, decrypt1){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
|
|
||||||
AES256_CBC a;
|
AES256_CBC a;
|
||||||
auto output = a.decrypt(key, iv, test1_enc);
|
auto output = a.decrypt(key, iv, test1_enc);
|
||||||
@ -60,12 +60,12 @@ TEST(CryptoTest, decrypt1){
|
|||||||
EXPECT_EQ(plaintext, output);
|
EXPECT_EQ(plaintext, output);
|
||||||
}
|
}
|
||||||
TEST(CryptoTest, decrypt2){
|
TEST(CryptoTest, decrypt2){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
size_t output_len = test1_enc.size();
|
size_t output_len = test1_enc.size();
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
// constructs encrypted input (iv + encrypted_data)
|
// constructs encrypted input (iv + encrypted_data)
|
||||||
auto input = std::vector<unsigned char>(iv);
|
auto input = raw_data(iv);
|
||||||
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
|
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
|
||||||
|
|
||||||
AES256_CBC a;
|
AES256_CBC a;
|
||||||
@ -74,12 +74,12 @@ TEST(CryptoTest, decrypt2){
|
|||||||
EXPECT_EQ(plaintext, output);
|
EXPECT_EQ(plaintext, output);
|
||||||
}
|
}
|
||||||
TEST(CryptoTest, decrypt3){
|
TEST(CryptoTest, decrypt3){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
size_t output_len = test1_enc.size();
|
size_t output_len = test1_enc.size();
|
||||||
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
|
const raw_data key(test1_key.begin(), test1_key.end());
|
||||||
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
|
const raw_data iv(test1_iv.begin(), test1_iv.end());
|
||||||
// constructs encrypted input (iv + encrypted_data)
|
// constructs encrypted input (iv + encrypted_data)
|
||||||
auto input = std::vector<unsigned char>(iv);
|
auto input = raw_data(iv);
|
||||||
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
|
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
|
||||||
|
|
||||||
AES256_CBC a(key);
|
AES256_CBC a(key);
|
||||||
@ -88,7 +88,7 @@ TEST(CryptoTest, decrypt3){
|
|||||||
EXPECT_EQ(plaintext, output);
|
EXPECT_EQ(plaintext, output);
|
||||||
}
|
}
|
||||||
TEST(CryptoTest, endToEnd){
|
TEST(CryptoTest, endToEnd){
|
||||||
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
|
const raw_data plaintext(test1_str.begin(), test1_str.end());
|
||||||
AES256_CBC a;
|
AES256_CBC a;
|
||||||
auto crypto = a.encrypt(plaintext);
|
auto crypto = a.encrypt(plaintext);
|
||||||
EXPECT_GT(crypto.size(), plaintext.size());
|
EXPECT_GT(crypto.size(), plaintext.size());
|
||||||
|
@ -2,11 +2,11 @@
|
|||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
/* Utility functions */
|
/* Utility functions */
|
||||||
std::vector<unsigned char> generic_read_file(const std::string& filename) {
|
horcrux::raw_data generic_read_file(const std::string& filename) {
|
||||||
auto ifstream = std::ifstream{filename, std::ios::binary};
|
auto ifstream = std::ifstream{filename, std::ios::binary};
|
||||||
auto path = std::filesystem::path{filename};
|
auto path = std::filesystem::path{filename};
|
||||||
auto size = std::filesystem::file_size(path);
|
auto size = std::filesystem::file_size(path);
|
||||||
std::vector<unsigned char> buf(size);
|
horcrux::raw_data buf(size);
|
||||||
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
|
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
|
||||||
return buf;
|
return buf;
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include "io.h"
|
#include "io.h"
|
||||||
#include "crypto.h"
|
#include "crypto.h"
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
/* IO Test utils */
|
/* IO Test utils */
|
||||||
#ifndef TEST_WORK_DIR
|
#ifndef TEST_WORK_DIR
|
||||||
@ -16,7 +17,7 @@ const std::string empty{TEST_WORK_DIR "/empty"};
|
|||||||
const std::string text{TEST_WORK_DIR "/test.txt"};
|
const std::string text{TEST_WORK_DIR "/test.txt"};
|
||||||
const std::string image{TEST_WORK_DIR "/mangoni.jpg"};
|
const std::string image{TEST_WORK_DIR "/mangoni.jpg"};
|
||||||
|
|
||||||
std::vector<unsigned char> generic_read_file(const std::string& filename);
|
horcrux::raw_data generic_read_file(const std::string& filename);
|
||||||
|
|
||||||
std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out);
|
std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out);
|
||||||
|
|
||||||
@ -34,7 +35,7 @@ echo "ditemi perche' se la mucca fa mu il merlo non fa me" |
|
|||||||
const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me";
|
const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me";
|
||||||
const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF";
|
const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF";
|
||||||
const std::string test1_iv = "0123456789ABCDEF";
|
const std::string test1_iv = "0123456789ABCDEF";
|
||||||
const std::vector<unsigned char> test1_enc {
|
const horcrux::raw_data test1_enc {
|
||||||
0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f,
|
0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f,
|
||||||
0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d,
|
0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d,
|
||||||
0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81,
|
0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81,
|
||||||
|
@ -16,10 +16,10 @@ const std::string test4_str = "mi e' entrata una bruschetta nell'occhio";
|
|||||||
const std::string test4_base64 = "bWkgZScgZW50cmF0YSB1bmEgYnJ1c2NoZXR0YSBuZWxsJ29jY2hpbw==";
|
const std::string test4_base64 = "bWkgZScgZW50cmF0YSB1bmEgYnJ1c2NoZXR0YSBuZWxsJ29jY2hpbw==";
|
||||||
|
|
||||||
TEST(utilTests, to_base64Test){
|
TEST(utilTests, to_base64Test){
|
||||||
EXPECT_EQ(test1_base64, horcrux::to_base64(std::vector<unsigned char>(test1_str.begin(), test1_str.end())));
|
EXPECT_EQ(test1_base64, horcrux::to_base64(horcrux::raw_data(test1_str.begin(), test1_str.end())));
|
||||||
EXPECT_EQ(test2_base64, horcrux::to_base64(std::vector<unsigned char>(test2_str.begin(), test2_str.end())));
|
EXPECT_EQ(test2_base64, horcrux::to_base64(horcrux::raw_data(test2_str.begin(), test2_str.end())));
|
||||||
EXPECT_EQ(test3_base64, horcrux::to_base64(std::vector<unsigned char>(test3_str.begin(), test3_str.end())));
|
EXPECT_EQ(test3_base64, horcrux::to_base64(horcrux::raw_data(test3_str.begin(), test3_str.end())));
|
||||||
EXPECT_EQ(test4_base64, horcrux::to_base64(std::vector<unsigned char>(test4_str.begin(), test4_str.end())));
|
EXPECT_EQ(test4_base64, horcrux::to_base64(horcrux::raw_data(test4_str.begin(), test4_str.end())));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(utilTests, from_base64Test){
|
TEST(utilTests, from_base64Test){
|
||||||
|
Loading…
Reference in New Issue
Block a user