Fix format

This commit is contained in:
Michele Rodolfi 2020-12-13 00:56:18 +01:00
parent 3d89f30a94
commit 01ff00f3b7
20 changed files with 855 additions and 817 deletions

View File

@ -1,6 +1,6 @@
root = true
[*]
end_of_line = lf
indent_style = tab
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

View File

@ -6,16 +6,16 @@ set(CMAKE_CXX_STANDARD 20)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
message(FATAL_ERROR "CMake step for googletest failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
RESULT_VARIABLE result
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/googletest-download )
if(result)
message(FATAL_ERROR "Build step for googletest failed: ${result}")
message(FATAL_ERROR "Build step for googletest failed: ${result}")
endif()
# Prevent overriding the parent project's compiler/linker
@ -32,7 +32,7 @@ add_subdirectory(${CMAKE_CURRENT_BINARY_DIR}/googletest-src
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if (CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include")
include_directories("${gtest_SOURCE_DIR}/include")
endif()

View File

@ -4,12 +4,12 @@ project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

View File

@ -1,9 +1,9 @@
set(SOURCES
main.cpp
horcrux.cpp
crypto.cpp
io.cpp
utils.cpp
main.cpp
horcrux.cpp
crypto.cpp
io.cpp
utils.cpp
)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCES})
@ -16,5 +16,5 @@ target_link_libraries(${CMAKE_PROJECT_NAME} OpenSSL::Crypto)
target_link_libraries(${CMAKE_PROJECT_NAME}_lib OpenSSL::Crypto)
install(TARGETS ${CMAKE_PROJECT_NAME}
DESTINATION /usr/local/bin
DESTINATION /usr/local/bin
)

View File

@ -1,3 +1,5 @@
#include "crypto.h"
#include <openssl/evp.h>
#include <string>
#include <vector>
#include <exception>
@ -7,188 +9,203 @@
#include <iostream>
#include <algorithm>
#include <openssl/evp.h>
#include "crypto.h"
#include "utils.h"
namespace horcrux {
class EvpCipherCtx {
EVP_CIPHER_CTX *ptr;
EVP_CIPHER_CTX *ptr;
public:
EvpCipherCtx(){
ptr = EVP_CIPHER_CTX_new();
}
EvpCipherCtx() {
ptr = EVP_CIPHER_CTX_new();
}
~EvpCipherCtx(){
EVP_CIPHER_CTX_free(ptr);
}
~EvpCipherCtx() {
EVP_CIPHER_CTX_free(ptr);
}
EVP_CIPHER_CTX* get(){
return ptr;
}
EVP_CIPHER_CTX* get() {
return ptr;
}
};
EvpCipherCtx AES256_CBC::init(Mode mode,
const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv){
if (key.size() != kKeySize){
throw std::runtime_error("Wrong key size");
}
if (iv.size() != kIvSize){
throw std::runtime_error("Wrong IV size");
}
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv) {
if (key.size() != kKeySize) {
throw std::runtime_error("Wrong key size");
}
if (iv.size() != kIvSize) {
throw std::runtime_error("Wrong IV size");
}
EvpCipherCtx ctx;
if (mode == Mode::kEncrypt){
if (EVP_EncryptInit(ctx.get(), EVP_aes_256_cbc(),
(key.data()), (iv.data())) == 0){
throw std::runtime_error("EVP_EncryptInit");
}
} else if (mode == Mode::kDecrypt){
if (EVP_DecryptInit(ctx.get(), EVP_aes_256_cbc(),
(key.data()), (iv.data())) == 0){
throw std::runtime_error("EVP_DecryptInit");
}
} else throw std::invalid_argument("Invalid Cipher mode");
return ctx;
EvpCipherCtx ctx;
if (mode == Mode::kEncrypt) {
if (EVP_EncryptInit(ctx.get(), EVP_aes_256_cbc(),
key.data(), iv.data()) == 0) {
throw std::runtime_error("EVP_EncryptInit");
}
} else if (mode == Mode::kDecrypt) {
if (EVP_DecryptInit(ctx.get(), EVP_aes_256_cbc(),
key.data(), iv.data()) == 0) {
throw std::runtime_error("EVP_DecryptInit");
}
} else {
throw std::invalid_argument("Invalid Cipher mode");
}
return ctx;
}
size_t AES256_CBC::process_chunk(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in, bool resize_out)
{
auto chunk_size = end - begin;
int len;
if(resize_in){
// Make sure ouput is large enough to add encrypted data + padding
output.resize(output_offset + chunk_size + kIvSize);
}
if (mode == Mode::kEncrypt) {
if (1 != EVP_EncryptUpdate(ctx.get(),
output.data() + output_offset, &len, &*begin, chunk_size)){
throw std::runtime_error("EVP_EncryptUpdate");
}
} else {
if (1 != EVP_DecryptUpdate(ctx.get(),
output.data() + output_offset, &len, &*begin, chunk_size)){
throw std::runtime_error("EVP_DecryptUpdate");
}
}
if (resize_out)
output.resize(output_offset + len);
return len;
size_t AES256_CBC::process_chunk(
Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in, bool resize_out) {
auto chunk_size = end - begin;
int len;
if (resize_in) {
// Make sure ouput is large enough to add encrypted data + padding
output.resize(output_offset + chunk_size + kIvSize);
}
if (mode == Mode::kEncrypt) {
if (1 != EVP_EncryptUpdate(ctx.get(),
output.data() + output_offset, &len,
&*begin, chunk_size)) {
throw std::runtime_error("EVP_EncryptUpdate");
}
} else {
if (1 != EVP_DecryptUpdate(ctx.get(),
output.data() + output_offset, &len,
&*begin, chunk_size)) {
throw std::runtime_error("EVP_DecryptUpdate");
}
}
if (resize_out)
output.resize(output_offset + len);
return len;
}
size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in, bool resize_out)
{
int len;
if (resize_in) {
// Make sure output is large enough to add the last block
output.resize(output_offset + kIvSize);
}
if (mode == Mode::kEncrypt) {
if (1 != EVP_EncryptFinal_ex(ctx.get(), output.data() + output_offset, &len)){
throw std::runtime_error("EVP_EncryptFinal");
}
} else {
if (1 != EVP_DecryptFinal_ex(ctx.get(), (output.data()) + output_offset, &len)){
throw std::runtime_error("EVP_DecryptFinal");
}
}
if (resize_out)
output.resize(output_offset + len);
return len;
std::vector<unsigned char>& output,
size_t output_offset,
bool resize_in, bool resize_out) {
int len;
if (resize_in) {
// Make sure output is large enough to add the last block
output.resize(output_offset + kIvSize);
}
if (mode == Mode::kEncrypt) {
if (1 != EVP_EncryptFinal_ex(ctx.get(), output.data() + output_offset,
&len)) {
throw std::runtime_error("EVP_EncryptFinal");
}
} else {
if (1 != EVP_DecryptFinal_ex(ctx.get(), (output.data()) + output_offset,
&len)) {
throw std::runtime_error("EVP_DecryptFinal");
}
}
if (resize_out)
output.resize(output_offset + len);
return len;
}
size_t AES256_CBC::process_all(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in, bool resize_out)
{
int len = process_chunk(mode, ctx, begin, end,
output, output_offset, resize_in, false);
len += process_final(mode, ctx,
output, output_offset + len, false, resize_out);
return len;
size_t AES256_CBC::process_all(
Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in, bool resize_out) {
int len = process_chunk(mode, ctx, begin, end, output, output_offset,
resize_in, false);
len += process_final(mode, ctx, output, output_offset + len, false,
resize_out);
return len;
}
std::vector<unsigned char> AES256_CBC::process(const std::vector<unsigned char>& inputdata) {
if (mode == Mode::kEncrypt)
return encrypt(inputdata);
else
return decrypt(inputdata);
std::vector<unsigned char> AES256_CBC::process(
const std::vector<unsigned char>& inputdata) {
if (mode == Mode::kEncrypt)
return encrypt(inputdata);
else
return decrypt(inputdata);
}
std::vector<unsigned char> AES256_CBC::encrypt(const std::vector<unsigned char>& plaintext) {
return encrypt(encryption_key, horcrux::generate_random(kIvSize), plaintext);
}
std::vector<unsigned char> AES256_CBC::decrypt(const std::vector<unsigned char>& ciphertext) {
return decrypt(encryption_key, ciphertext);
}
std::vector<unsigned char> AES256_CBC::encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input)
{
auto ctx = init(Mode::kEncrypt, key, iv);
// Make sure ouput is large enough to contain IV + encrypted data + padding
std::vector<unsigned char> output(input.size() + (2 * kIvSize));
std::copy(iv.begin(), iv.end(), output.begin());
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, kIvSize);
return output;
}
int AES256_CBC::encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input, std::vector<unsigned char>& output)
{
auto ctx = init(Mode::kEncrypt, key, iv);
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, 0);
return 0;
}
int AES256_CBC::encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
std::istream input, const size_t input_len,
std::ostream output, size_t& output_len){
auto inbuf = std::vector<unsigned char>(input_len);
auto outbuf = std::vector<unsigned char>(input_len + 16);
input.read(reinterpret_cast<char*>(inbuf.data()), input_len);
encrypt(key, iv, inbuf, outbuf);
output.write(reinterpret_cast<char*>(outbuf.data()), outbuf.size());
return 0;
}
int AES256_CBC::decrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input, std::vector<unsigned char>& output)
{
auto ctx = init(Mode::kDecrypt, key, iv);
process_all(Mode::kDecrypt, ctx, input.begin(), input.end(), output, 0);
return 0;
}
std::vector<unsigned char> AES256_CBC::decrypt(const std::vector<unsigned char>& key,
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());
std::vector<unsigned char> AES256_CBC::encrypt(
const std::vector<unsigned char>& plaintext) {
return encrypt(encryption_key, horcrux::generate_random(kIvSize),
plaintext);
}
std::vector<unsigned char> AES256_CBC::decrypt(
const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
std::vector<unsigned char>::const_iterator begin, std::vector<unsigned char>::const_iterator end)
{
auto ctx = init(Mode::kDecrypt, key, iv);
std::vector<unsigned char> output;
process_all(Mode::kDecrypt, ctx, begin, end, output, 0);
return output;
const std::vector<unsigned char>& ciphertext) {
return decrypt(encryption_key, ciphertext);
}
}; //namespace
std::vector<unsigned char> AES256_CBC::encrypt(
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input) {
auto ctx = init(Mode::kEncrypt, key, iv);
// Make sure ouput is large enough to contain IV + encrypted data + padding
std::vector<unsigned char> output(input.size() + (2 * kIvSize));
std::copy(iv.begin(), iv.end(), output.begin());
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output,
kIvSize);
return output;
}
int AES256_CBC::encrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input,
std::vector<unsigned char>& output) {
auto ctx = init(Mode::kEncrypt, key, iv);
process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, 0);
return 0;
}
int AES256_CBC::encrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
std::istream input, const size_t input_len,
std::ostream output, size_t& output_len) {
auto inbuf = std::vector<unsigned char>(input_len);
auto outbuf = std::vector<unsigned char>(input_len + 16);
input.read(reinterpret_cast<char*>(inbuf.data()), input_len);
encrypt(key, iv, inbuf, outbuf);
output.write(reinterpret_cast<char*>(outbuf.data()), outbuf.size());
return 0;
}
int AES256_CBC::decrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input,
std::vector<unsigned char>& output) {
auto ctx = init(Mode::kDecrypt, key, iv);
process_all(Mode::kDecrypt, ctx, input.begin(), input.end(), output, 0);
return 0;
}
std::vector<unsigned char> AES256_CBC::decrypt(
const std::vector<unsigned char>& key,
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());
}
std::vector<unsigned char> AES256_CBC::decrypt(
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end) {
auto ctx = init(Mode::kDecrypt, key, iv);
std::vector<unsigned char> output;
process_all(Mode::kDecrypt, ctx, begin, end, output, 0);
return output;
}
}; // namespace horcrux

View File

@ -8,93 +8,114 @@
namespace horcrux {
enum class Mode {kEncrypt, kDecrypt};
enum class Mode { kEncrypt, kDecrypt };
class Cipher {
protected:
Cipher() = delete;
Cipher(Mode m) : mode(m) { }
Cipher(Mode m, const std::vector<unsigned char>& key) :
mode(m), encryption_key(key) {}
Cipher() = delete;
explicit Cipher(Mode m) : mode(m) { }
Cipher(Mode m, const std::vector<unsigned char>& key) :
mode(m), encryption_key(key) {}
Cipher(Mode m, std::vector<unsigned char>&& key) :
mode(m), encryption_key(key) {}
Cipher(Mode m, std::vector<unsigned char>&& key) :
mode(m), encryption_key(key) {}
std::vector<unsigned char> encryption_key;
std::vector<unsigned char> encryption_key;
public:
const Mode mode;
const std::vector<unsigned char>& get_encryption_key() {return encryption_key;}
const Mode mode;
const std::vector<unsigned char>& get_encryption_key() {
return encryption_key;
}
virtual std::vector<unsigned char> process(const std::vector<unsigned char>& inputdata) = 0;
virtual std::vector<unsigned char> encrypt(const std::vector<unsigned char>& plaintext) = 0;
virtual std::vector<unsigned char> decrypt(const std::vector<unsigned char>& ciphertext) = 0;
virtual std::vector<unsigned char> process(
const std::vector<unsigned char>& inputdata) = 0;
virtual std::vector<unsigned char> encrypt(
const std::vector<unsigned char>& plaintext) = 0;
virtual std::vector<unsigned char> decrypt(
const std::vector<unsigned char>& ciphertext) = 0;
};
class EvpCipherCtx;
class AES256_CBC : public Cipher {
const size_t kKeySize = 32;
const size_t kIvSize = 16;
const size_t kKeySize = 32;
const size_t kIvSize = 16;
public:
AES256_CBC() : Cipher(Mode::kEncrypt) {
encryption_key = horcrux::generate_random(kKeySize);
}
AES256_CBC(const std::vector<unsigned char>& key) : Cipher(Mode::kDecrypt, key)
{
if (key.size() != kKeySize)
throw std::invalid_argument("Invalid key size");
}
AES256_CBC(std::vector<unsigned char>&& key) : Cipher(Mode::kDecrypt, key)
{
if (key.size() != kKeySize)
throw std::invalid_argument("Invalid key size");
}
AES256_CBC() : Cipher(Mode::kEncrypt) {
encryption_key = horcrux::generate_random(kKeySize);
}
explicit AES256_CBC(const std::vector<unsigned char>& key)
: Cipher(Mode::kDecrypt, key) {
if (key.size() != kKeySize)
throw std::invalid_argument("Invalid key size");
}
explicit AES256_CBC(std::vector<unsigned char>&& key)
: Cipher(Mode::kDecrypt, key) {
if (key.size() != kKeySize)
throw std::invalid_argument("Invalid key size");
}
std::vector<unsigned char> process(const std::vector<unsigned char>& inputdata) override;
std::vector<unsigned char> encrypt(const std::vector<unsigned char>& plaintext) override;
std::vector<unsigned char> decrypt(const std::vector<unsigned char>& ciphertext) override;
std::vector<unsigned char> process(
const std::vector<unsigned char>& inputdata) override;
std::vector<unsigned char> encrypt(
const std::vector<unsigned char>& plaintext) override;
std::vector<unsigned char> decrypt(
const std::vector<unsigned char>& ciphertext) override;
std::vector<unsigned char> encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input);
std::vector<unsigned char> encrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input);
int encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input, std::vector<unsigned char>& output);
int encrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input,
std::vector<unsigned char>& output);
int encrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
std::istream input, const size_t input_len,
std::ostream output, size_t& output_len);
int encrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
std::istream input, const size_t input_len,
std::ostream output, size_t& output_len);
int decrypt(const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input, std::vector<unsigned char>& output);
int decrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
const std::vector<unsigned char>& input,
std::vector<unsigned char>& output);
std::vector<unsigned char> decrypt(const std::vector<unsigned char>& key,
const std::vector<unsigned char>& ciphertext);
std::vector<unsigned char> decrypt(
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& ciphertext);
std::vector<unsigned char> decrypt(
const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv,
std::vector<unsigned char>::const_iterator begin, std::vector<unsigned char>::const_iterator end);
std::vector<unsigned char> decrypt(
const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end);
private:
EvpCipherCtx init(Mode mode,
const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv);
EvpCipherCtx init(Mode mode, const std::vector<unsigned char>& key,
const std::vector<unsigned char>& iv);
size_t process_chunk(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in = true, bool resize_out = true);
size_t process_chunk(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output,
size_t output_offset,
bool resize_in = true, bool resize_out = true);
size_t process_final(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in = true, bool resize_out = true);
size_t process_final(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>& output,
size_t output_offset,
bool resize_in = true, bool resize_out = true);
size_t process_all(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in = true, bool resize_out = true);
size_t process_all(Mode mode, EvpCipherCtx& ctx,
std::vector<unsigned char>::const_iterator begin,
std::vector<unsigned char>::const_iterator end,
std::vector<unsigned char>& output, size_t output_offset,
bool resize_in = true, bool resize_out = true);
};
}; //namespace
#endif //HORCRUX_SRC_CRYPTO_H
}; // namespace horcrux
#endif // HORCRUX_SRC_CRYPTO_H

View File

@ -1,82 +1,82 @@
#include "horcrux.h"
#include <iostream>
#include <exception>
#include "horcrux.h"
#include "utils.h"
namespace horcrux {
Horcrux::Horcrux(HorcruxOptions&& opt) : options(opt){ init(); }
Horcrux::Horcrux(const HorcruxOptions& opt) : options(opt){ init(); }
void Horcrux::init(){
if (options.count <= 0){
throw std::invalid_argument("Invalid horcrux count");
}
switch (options.mode){
case Mode::kEncrypt:
cipher = std::make_unique<AES256_CBC>();
input = std::make_unique<FsPlainInput>(options.input[0]);
output = std::make_unique<FsCryptoOutput>(options.output, options.count,
std::filesystem::path(options.input[0]).filename().string());
options.base64_key = to_base64(cipher->get_encryption_key());
break;
case Mode::kDecrypt:
if (options.count != options.input.size())
throw std::invalid_argument("Invalid horcrux count");
cipher = std::make_unique<AES256_CBC>(from_base64(options.base64_key));
input = std::make_unique<FsCryptoInput>(options.input);
output = std::make_unique<FsPlainOutput>(options.output);
break;
default:
throw std::invalid_argument("Invalid horcrux mode");
}
Horcrux::Horcrux(HorcruxOptions&& opt) : options(opt) { init(); }
Horcrux::Horcrux(const HorcruxOptions& opt) : options(opt) { init(); }
void Horcrux::init() {
if (options.count <= 0) {
throw std::invalid_argument("Invalid horcrux count");
}
switch (options.mode) {
case Mode::kEncrypt:
cipher = std::make_unique<AES256_CBC>();
input = std::make_unique<FsPlainInput>(options.input[0]);
output = std::make_unique<FsCryptoOutput>(options.output, options.count,
std::filesystem::path(options.input[0]).filename().string());
options.base64_key = to_base64(cipher->get_encryption_key());
break;
case Mode::kDecrypt:
if (options.count != options.input.size()) {
throw std::invalid_argument("Invalid horcrux count");
}
cipher = std::make_unique<AES256_CBC>(from_base64(options.base64_key));
input = std::make_unique<FsCryptoInput>(options.input);
output = std::make_unique<FsPlainOutput>(options.output);
break;
default:
throw std::invalid_argument("Invalid horcrux mode");
}
}
void Horcrux::execute(){
if (options.mode == Mode::kEncrypt)
output->write(cipher->encrypt(input->read()));
else
output->write(cipher->decrypt(input->read()));
void Horcrux::execute() {
if (options.mode == Mode::kEncrypt)
output->write(cipher->encrypt(input->read()));
else
output->write(cipher->decrypt(input->read()));
}
static void print_usage(const std::string& program){
std::cout << "Usage:" << std::endl <<
"\t\t" << program << " create -n <horcrux count> <input path> <output path>" << std::endl <<
"\t\t" << program << " load -k <decryption key> <input files> <output_path>" << std::endl;
static void print_usage(const std::string& program) {
std::cout << "Usage:" << std::endl <<
"\t\t" << program << " create -n <horcrux count> <input path> <output path>" << std::endl <<
"\t\t" << program << " load -k <decryption key> <input files> <output_path>" << std::endl;
}
static void parse_error(const std::string& program, const std::string& error){
print_usage(program);
throw std::invalid_argument(error);
static void parse_error(const std::string& program, const std::string& error) {
print_usage(program);
throw std::invalid_argument(error);
}
HorcruxOptions parse_arguments(std::vector<std::string>&& arguments){
if (arguments.size() < 6){
parse_error(std::string(arguments[0]), "Wrong argument number");
}
HorcruxOptions opt;
if (arguments[1] == "create") {
if (arguments.size() != 6){
parse_error(std::string(arguments[0]), "Wrong argument number");
}
opt.mode = Mode::kEncrypt;
if (arguments[2] != "-n")
parse_error(arguments[0], "create: unknown option " + arguments[2]);
opt.count = std::stoi(arguments[3]);
opt.input.emplace_back(arguments[4]);
opt.output = arguments[5];
} else if (arguments[1] == "load"){
opt.mode = Mode::kDecrypt;
if (arguments[2] != "-k")
parse_error(arguments[0], "load: unknown option " + arguments[2]);
opt.base64_key = arguments[3];
opt.input.insert(opt.input.begin(), arguments.begin() + 4, arguments.end() - 1);
opt.count = opt.input.size();
opt.output = arguments.back();
} else {
parse_error(arguments[0], "unknown mode: " + arguments[1]);
}
return opt;
HorcruxOptions parse_arguments(std::vector<std::string>&& arguments) {
if (arguments.size() < 6) {
parse_error(std::string(arguments[0]), "Wrong argument number");
}
HorcruxOptions opt;
if (arguments[1] == "create") {
if (arguments.size() != 6) {
parse_error(std::string(arguments[0]), "Wrong argument number");
}
opt.mode = Mode::kEncrypt;
if (arguments[2] != "-n")
parse_error(arguments[0], "create: unknown option " + arguments[2]);
opt.count = std::stoi(arguments[3]);
opt.input.emplace_back(arguments[4]);
opt.output = arguments[5];
} else if (arguments[1] == "load") {
opt.mode = Mode::kDecrypt;
if (arguments[2] != "-k")
parse_error(arguments[0], "load: unknown option " + arguments[2]);
opt.base64_key = arguments[3];
opt.input.insert(opt.input.begin(), arguments.begin() + 4,
arguments.end() - 1);
opt.count = opt.input.size();
opt.output = arguments.back();
} else {
parse_error(arguments[0], "unknown mode: " + arguments[1]);
}
return opt;
}
}; //namespace
}; // namespace horcrux

View File

@ -10,32 +10,30 @@
namespace horcrux {
struct HorcruxOptions {
Mode mode;
int count;
std::vector<std::string> input;
std::string output;
std::string base64_key;
Mode mode;
int count;
std::vector<std::string> input;
std::string output;
std::string base64_key;
};
class Horcrux {
HorcruxOptions options;
std::unique_ptr<Cipher> cipher;
std::unique_ptr<Input> input;
std::unique_ptr<Output> output;
HorcruxOptions options;
std::unique_ptr<Cipher> cipher;
std::unique_ptr<Input> input;
std::unique_ptr<Output> output;
void init();
void init();
public:
Horcrux(const HorcruxOptions& opt);
Horcrux(HorcruxOptions&& opt);
void execute();
const Mode& mode() { return options.mode; }
const std::string& key() { return options.base64_key; }
explicit Horcrux(const HorcruxOptions& opt);
explicit Horcrux(HorcruxOptions&& opt);
void execute();
const Mode& mode() { return options.mode; }
const std::string& key() { return options.base64_key; }
};
HorcruxOptions parse_arguments(std::vector<std::string>&& arguments);
}; //namespace
#endif //HORCRUX_HORCRUX_H
}; // namespace horcrux
#endif // HORCRUX_HORCRUX_H

View File

@ -1,98 +1,97 @@
#include <iostream>
#include "io.h"
#include <iostream>
#include "utils.h"
namespace horcrux {
namespace fs = std::filesystem;
FsPlainInput::FsPlainInput(const std::string& path) : file_path(fs::absolute(path))
{
if (fs::status(path).type() != fs::file_type::regular){
throw std::invalid_argument("Input is not a regular file");
}
file_size = fs::file_size(file_path);
if (file_size == 0){
throw std::invalid_argument("Input is empty, nothing to encrypt");
}
file_stream = std::ifstream(file_path, std::ios::binary);
FsPlainInput::FsPlainInput(const std::string& path)
: file_path(fs::absolute(path)) {
if (fs::status(path).type() != fs::file_type::regular) {
throw std::invalid_argument("Input is not a regular file");
}
file_size = fs::file_size(file_path);
if (file_size == 0) {
throw std::invalid_argument("Input is empty, nothing to encrypt");
}
file_stream = std::ifstream(file_path, std::ios::binary);
}
std::vector<unsigned char> FsPlainInput::read(){
std::vector<unsigned char> result(file_size);
file_stream.read(reinterpret_cast<char*>(result.data()), file_size);
return result;
std::vector<unsigned char> FsPlainInput::read() {
std::vector<unsigned char> result(file_size);
file_stream.read(reinterpret_cast<char*>(result.data()), file_size);
return result;
}
FsCryptoInput::FsCryptoInput(const std::vector<std::string>& filenames)
{
if (filenames.empty()){
throw std::invalid_argument("No files to decrypt");
}
for (auto name : filenames){
auto path = absolute(fs::absolute({name}));
if (fs::status(path).type() != fs::file_type::regular){
throw std::invalid_argument("Input is not a regular file");
}
file_paths.emplace_back(path, fs::file_size(path));
total_size += file_paths.back().second;
}
if (total_size == 0){
throw std::invalid_argument("No data to decrypt");
}
FsCryptoInput::FsCryptoInput(const std::vector<std::string>& filenames) {
if (filenames.empty()){
throw std::invalid_argument("No files to decrypt");
}
for (auto name : filenames) {
auto path = absolute(fs::absolute({name}));
if (fs::status(path).type() != fs::file_type::regular) {
throw std::invalid_argument("Input is not a regular file");
}
file_paths.emplace_back(path, fs::file_size(path));
total_size += file_paths.back().second;
}
if (total_size == 0) {
throw std::invalid_argument("No data to decrypt");
}
}
std::vector<unsigned char> FsCryptoInput::read(){
std::vector<unsigned char> result(total_size);
size_t data_read{0};
for (auto& f : file_paths){
std::ifstream ifstream(f.first);
ifstream.read(reinterpret_cast<char*>(result.data()) + data_read, f.second);
data_read += f.second;
}
return result;
std::vector<unsigned char> FsCryptoInput::read() {
std::vector<unsigned char> result(total_size);
size_t data_read{0};
for (auto& f : file_paths) {
std::ifstream ifstream(f.first);
ifstream.read(reinterpret_cast<char*>(result.data()) + data_read, f.second);
data_read += f.second;
}
return result;
}
FsPlainOutput::FsPlainOutput(const std::string& filename)
: file_path(fs::absolute(filename))
{
if (fs::exists(file_path) && fs::status(file_path).type() != fs::file_type::regular){
throw std::invalid_argument("Output file is not a regular file");
}
: file_path(fs::absolute(filename)) {
if (fs::exists(file_path)
&& fs::status(file_path).type() != fs::file_type::regular) {
throw std::invalid_argument("Output file is not a regular file");
}
}
size_t FsPlainOutput::write(const std::vector<unsigned char>& to_write){
std::ofstream out(file_path, std::ios::binary);
out.write(reinterpret_cast<const char*>(to_write.data()), to_write.size());
return to_write.size();
size_t FsPlainOutput::write(const std::vector<unsigned char>& to_write) {
std::ofstream out(file_path, std::ios::binary);
out.write(reinterpret_cast<const char*>(to_write.data()), to_write.size());
return to_write.size();
}
FsCryptoOutput::FsCryptoOutput(const std::string& folder, const int horcrux_num,
const std::string& filename, const size_t horcrux_size)
: folder_path(fs::absolute(folder)), base_name(filename),
num(horcrux_num), size(horcrux_size)
{
if (fs::status(folder_path).type() != fs::file_type::directory){
throw std::invalid_argument("Output is not a directory");
}
const std::string& filename, const size_t horcrux_size)
: folder_path(fs::absolute(folder)), base_name(filename),
num(horcrux_num), size(horcrux_size) {
if (fs::status(folder_path).type() != fs::file_type::directory) {
throw std::invalid_argument("Output is not a directory");
}
if (horcrux_num <= 0){
throw std::invalid_argument("Invalid horcrux num");
}
if (horcrux_num <= 0) {
throw std::invalid_argument("Invalid horcrux num");
}
}
size_t FsCryptoOutput::write(const std::vector<unsigned char>& to_write){
size = to_write.size() / num;
std::ofstream f;
created_files.clear();
size_t data_written{0};
for (int i = 0; i < num; ++i){
std::string name = base_name + '.' + std::to_string(i) + ".enc";
created_files.emplace_back(folder_path / name);
f = std::ofstream(created_files.back(),
std::ios::binary | std::ios::trunc);
auto chunk_size = i + 1 != num ? size : size + to_write.size() % size;
f.write(reinterpret_cast<const char*>(to_write.data()) + data_written, chunk_size);
data_written += chunk_size;
}
return data_written;
size_t FsCryptoOutput::write(const std::vector<unsigned char>& to_write) {
size = to_write.size() / num;
std::ofstream f;
created_files.clear();
size_t data_written{0};
for (int i = 0; i < num; ++i) {
std::string name = base_name + '.' + std::to_string(i) + ".enc";
created_files.emplace_back(folder_path / name);
f = std::ofstream(created_files.back(),
std::ios::binary | std::ios::trunc);
auto chunk_size = i + 1 != num ? size : size + to_write.size() % size;
f.write(reinterpret_cast<const char*>(to_write.data()) + data_written,
chunk_size);
data_written += chunk_size;
}
return data_written;
}
};

View File

@ -4,16 +4,17 @@
#include <filesystem>
#include <fstream>
#include <vector>
#include <utility>
namespace horcrux {
class Input {
public:
virtual std::vector<unsigned char> read() = 0;
virtual std::vector<unsigned char> read() = 0;
};
class Output {
public:
virtual size_t write(const std::vector<unsigned char>& data) = 0;
virtual size_t write(const std::vector<unsigned char>& data) = 0;
};
class PlainInput : public Input {};
class CryptoInput : public Input {};
@ -21,48 +22,49 @@ class PlainOutput : public Output {};
class CryptoOutput : public Output {};
class FsPlainInput : public Input {
std::filesystem::path file_path;
size_t file_size;
std::ifstream file_stream;
FsPlainInput() = delete;
std::filesystem::path file_path;
size_t file_size;
std::ifstream file_stream;
FsPlainInput() = delete;
public:
FsPlainInput(const std::string& path);
std::vector<unsigned char> read() override;
explicit FsPlainInput(const std::string& path);
std::vector<unsigned char> read() override;
};
class FsCryptoInput : public Input{
std::vector<std::pair<std::filesystem::path, size_t>> file_paths;
size_t total_size{0};
FsCryptoInput() = delete;
std::vector<std::pair<std::filesystem::path, size_t>> file_paths;
size_t total_size{0};
FsCryptoInput() = delete;
public:
FsCryptoInput(const std::vector<std::string>& filenames);
std::vector<unsigned char> read() override;
explicit FsCryptoInput(const std::vector<std::string>& filenames);
std::vector<unsigned char> read() override;
};
class FsPlainOutput : public Output {
std::filesystem::path file_path;
FsPlainOutput() = delete;
std::filesystem::path file_path;
FsPlainOutput() = delete;
public:
FsPlainOutput(const std::string& filename);
size_t write(const std::vector<unsigned char>& to_write) override;
explicit FsPlainOutput(const std::string& filename);
size_t write(const std::vector<unsigned char>& to_write) override;
};
class FsCryptoOutput : public Output {
std::filesystem::path folder_path;
std::string base_name;
int num;
size_t size;
FsCryptoOutput() = delete;
std::filesystem::path folder_path;
std::string base_name;
int num;
size_t size;
FsCryptoOutput() = delete;
public:
std::vector<std::filesystem::path> created_files;
std::vector<std::filesystem::path> created_files;
FsCryptoOutput(const std::string& folder, const int horcrux_num,
const std::string& filename = "horcrux", const size_t horcrux_size = 0);
size_t write(const std::vector<unsigned char>& to_write) override;
FsCryptoOutput(const std::string& folder, const int horcrux_num,
const std::string& filename = "horcrux",
const size_t horcrux_size = 0);
size_t write(const std::vector<unsigned char>& to_write) override;
};
};
#endif //HORCRUX_SRC_IO_H
}; // namespace horcrux
#endif // HORCRUX_SRC_IO_H

View File

@ -3,28 +3,28 @@
#include "horcrux.h"
int main(const int argc, const char *argv[]) {
int ret = -1;
std::vector<std::string> arguments;
for (int i = 0; i < argc; ++i){
arguments.emplace_back(argv[i]);
}
int ret = -1;
std::vector<std::string> arguments;
for (int i = 0; i < argc; ++i) {
arguments.emplace_back(argv[i]);
}
try {
horcrux::Horcrux h(horcrux::parse_arguments(std::move(arguments)));
h.execute();
if (h.mode() == horcrux::Mode::kEncrypt){
//print encryption key to stdout
std::cout << h.key() << std::endl;
}
ret = 0;
} catch (std::invalid_argument& ex){
std::cerr << "Error: Invalid arguments. " << ex.what() << std::endl;
} catch (std::runtime_error& ex){
std::cerr << "Runtime error: " << ex.what() << std::endl;
} catch (std::exception& ex){
std::cerr << "Unknown error: " << ex.what() << std::endl;
}
try {
horcrux::Horcrux h(horcrux::parse_arguments(std::move(arguments)));
h.execute();
if (h.mode() == horcrux::Mode::kEncrypt) {
// print encryption key to stdout
std::cout << h.key() << std::endl;
}
ret = 0;
} catch (std::invalid_argument& ex) {
std::cerr << "Error: Invalid arguments. " << ex.what() << std::endl;
} catch (std::runtime_error& ex) {
std::cerr << "Runtime error: " << ex.what() << std::endl;
} catch (std::exception& ex) {
std::cerr << "Unknown error: " << ex.what() << std::endl;
}
return ret;
return ret;
}

View File

@ -1,41 +1,39 @@
#include "utils.h"
#include <openssl/rand.h>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <openssl/rand.h>
#include "utils.h"
std::string horcrux::to_base64(const std::vector<unsigned char>& binary)
{
using namespace boost::archive::iterators;
using It = base64_from_binary<transform_width<std::vector<unsigned char>::const_iterator, 6, 8>>;
auto base64 = std::string(It(binary.begin()), It(binary.end()));
// Add padding.
return base64.append((3 - binary.size() % 3) % 3, '=');
std::string horcrux::to_base64(const std::vector<unsigned char>& binary) {
using namespace boost::archive::iterators;
using It = base64_from_binary<transform_width<
std::vector<unsigned char>::const_iterator, 6, 8>>;
auto base64 = std::string(It(binary.begin()), It(binary.end()));
// Add padding.
return base64.append((3 - binary.size() % 3) % 3, '=');
}
std::vector<unsigned char> horcrux::from_base64(const std::string& base64)
{
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
auto binary = std::vector<unsigned char>(It(base64.begin()), It(base64.end()));
// Remove padding.
auto length = base64.size();
if(binary.size() > 2 && base64[length - 1] == '=' && base64[length - 2] == '=')
{
binary.erase(binary.end() - 2, binary.end());
}
else if(binary.size() > 1 && base64[length - 1] == '=')
{
binary.erase(binary.end() - 1, binary.end());
}
return binary;
std::vector<unsigned char> horcrux::from_base64(const std::string& base64) {
using namespace boost::archive::iterators;
using It = transform_width<binary_from_base64<std::string::const_iterator>,
8, 6>;
auto binary = std::vector<unsigned char>(It(base64.begin()),
It(base64.end()));
// Remove padding.
auto length = base64.size();
if (binary.size() > 2 && base64[length - 1] == '='
&& base64[length - 2] == '=') {
binary.erase(binary.end() - 2, binary.end());
} else if (binary.size() > 1 && base64[length - 1] == '=') {
binary.erase(binary.end() - 1, binary.end());
}
return binary;
}
std::vector<unsigned char> horcrux::generate_random(const size_t size){
std::vector<unsigned char> res(size);
if(!RAND_bytes(res.data(), size)){
throw std::exception();
}
return res;
std::vector<unsigned char> horcrux::generate_random(const size_t size) {
std::vector<unsigned char> res(size);
if (!RAND_bytes(res.data(), size)) {
throw std::exception();
}
return res;
}

View File

@ -11,4 +11,4 @@ std::vector<unsigned char> from_base64(const std::string& base64);
std::vector<unsigned char> generate_random(const size_t buf_len);
}
#endif //HORCRUX_SRC_UTILS_H
#endif // HORCRUX_SRC_UTILS_H

View File

@ -1,11 +1,11 @@
set(BINARY ${CMAKE_PROJECT_NAME}_test)
add_executable(${BINARY}
main.cpp
crypto-test.cpp
io-test.cpp
utils-test.cpp
integration-test.cpp
main.cpp
crypto-test.cpp
io-test.cpp
utils-test.cpp
integration-test.cpp
)
add_test(NAME test COMMAND ${BINARY})
@ -14,5 +14,5 @@ target_link_libraries(${BINARY} PUBLIC ${CMAKE_PROJECT_NAME}_lib gtest)
#Copy files needed for unit tests
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/test_work_dir
DESTINATION ${CMAKE_BINARY_DIR})
DESTINATION ${CMAKE_BINARY_DIR})
add_definitions(-DTEST_WORK_DIR="${CMAKE_BINARY_DIR}/test_work_dir")

View File

@ -6,94 +6,94 @@
using namespace horcrux;
TEST(CryptoTest, encrypt1){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = plaintext.size() + 16;
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = plaintext.size() + 16;
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
a.encrypt(key, iv, plaintext, output);
AES256_CBC a;
a.encrypt(key, iv, plaintext, output);
EXPECT_EQ(test1_enc, output);
EXPECT_EQ(test1_enc, output);
}
TEST(CryptoTest, encrypt2){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
auto output = a.encrypt(key, iv, plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
AES256_CBC a;
auto output = a.encrypt(key, iv, plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
EXPECT_EQ(temp.size(), output.size());
EXPECT_EQ(temp, output);
EXPECT_EQ(temp.size(), output.size());
EXPECT_EQ(temp, output);
}
TEST(CryptoTest, encrypt3){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
//size_t output_len = plaintext.size() + 16;
//std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a(key);
auto output = a.encrypt(plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
AES256_CBC a(key);
auto output = a.encrypt(plaintext);
auto temp = iv;
temp.insert(temp.end(), test1_enc.begin(), test1_enc.end());
EXPECT_EQ(temp.size(), output.size());
EXPECT_EQ(temp.size(), output.size());
}
TEST(CryptoTest, decrypt1){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
std::vector<unsigned char> output(output_len);
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
AES256_CBC a;
a.decrypt(key, iv, test1_enc, output);
AES256_CBC a;
a.decrypt(key, iv, test1_enc, output);
EXPECT_EQ(plaintext, output);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, decrypt2){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
AES256_CBC a;
auto output = a.decrypt(key, input);
AES256_CBC a;
auto output = a.decrypt(key, input);
EXPECT_EQ(plaintext, output);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, decrypt3){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
size_t output_len = test1_enc.size();
const std::vector<unsigned char> key(test1_key.begin(), test1_key.end());
const std::vector<unsigned char> iv(test1_iv.begin(), test1_iv.end());
// constructs encrypted input (iv + encrypted_data)
auto input = std::vector<unsigned char>(iv);
input.insert(input.end(),test1_enc.begin(), test1_enc.end());
AES256_CBC a(key);
auto output = a.decrypt(input);
AES256_CBC a(key);
auto output = a.decrypt(input);
EXPECT_EQ(plaintext, output);
EXPECT_EQ(plaintext, output);
}
TEST(CryptoTest, endToEnd){
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
AES256_CBC a;
auto crypto = a.encrypt(plaintext);
EXPECT_GT(crypto.size(), plaintext.size());
auto decrypted = a.decrypt(crypto);
EXPECT_EQ(plaintext, decrypted);
const std::vector<unsigned char> plaintext(test1_str.begin(), test1_str.end());
AES256_CBC a;
auto crypto = a.encrypt(plaintext);
EXPECT_GT(crypto.size(), plaintext.size());
auto decrypted = a.decrypt(crypto);
EXPECT_EQ(plaintext, decrypted);
}

View File

@ -5,124 +5,124 @@
using namespace horcrux;
TEST(IntegrationTest, EndToEndEncryptDecrypt){
auto buf = generic_read_file(image);
auto buf = generic_read_file(image);
//Prepare encrypt operation
auto in = FsPlainInput(image);
auto cipher = AES256_CBC();
auto out = FsCryptoOutput(folder, 3);
//Prepare encrypt operation
auto in = FsPlainInput(image);
auto cipher = AES256_CBC();
auto out = FsCryptoOutput(folder, 3);
// Perform encrypt operation
out.write(cipher.encrypt(in.read()));
// Perform encrypt operation
out.write(cipher.encrypt(in.read()));
//Prepare decrypt operation
auto in2 = FsCryptoInput(get_created_filenames(out));
//Perform decrypt operation
auto buf2 = cipher.decrypt(in2.read());
//Prepare decrypt operation
auto in2 = FsCryptoInput(get_created_filenames(out));
//Perform decrypt operation
auto buf2 = cipher.decrypt(in2.read());
//check outcome
EXPECT_EQ(buf, buf2);
//check outcome
EXPECT_EQ(buf, buf2);
auto out2 = FsPlainOutput(noexist);
out2.write(cipher.decrypt(in2.read()));
auto out2 = FsPlainOutput(noexist);
out2.write(cipher.decrypt(in2.read()));
auto buf3 = generic_read_file(noexist);
EXPECT_EQ(buf, buf3);
auto buf3 = generic_read_file(noexist);
EXPECT_EQ(buf, buf3);
delete_created_files(out);
std::filesystem::remove(noexist);
delete_created_files(out);
std::filesystem::remove(noexist);
}
TEST(IntegrationTest, HorcruxOptions){
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = -1,
.input = {noexist},
.output = {image}
};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {folder};
options.count = 0;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_NO_THROW(Horcrux {options});
options.mode = Mode::kDecrypt;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image, text};
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {noexist};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.base64_key = to_base64(generate_random(32));
EXPECT_NO_THROW(Horcrux {options});
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = -1,
.input = {noexist},
.output = {image}
};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {folder};
options.count = 0;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_NO_THROW(Horcrux {options});
options.mode = Mode::kDecrypt;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.input = {image, text};
options.count = 1;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.output = {noexist};
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.count = 2;
EXPECT_THROW(Horcrux {options}, std::invalid_argument);
options.base64_key = to_base64(generate_random(32));
EXPECT_NO_THROW(Horcrux {options});
}
TEST(IntegrationTest, HorcruxEncryptiDecrypt) {
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = 3,
.input = {image},
.output = {folder}
};
Horcrux enc{options};
HorcruxOptions options {
.mode = Mode::kEncrypt,
.count = 3,
.input = {image},
.output = {folder}
};
Horcrux enc{options};
enc.execute();
auto key = enc.key();
enc.execute();
auto key = enc.key();
HorcruxOptions options2 {
.mode = Mode::kDecrypt,
.count = 3,
.input = {get_encrypted_files(folder, image)},
.output = {noexist},
.base64_key = key
};
HorcruxOptions options2 {
.mode = Mode::kDecrypt,
.count = 3,
.input = {get_encrypted_files(folder, image)},
.output = {noexist},
.base64_key = key
};
Horcrux dec{options2};
dec.execute();
Horcrux dec{options2};
dec.execute();
//Compare input and ouput files
EXPECT_EQ(generic_read_file(image), generic_read_file(noexist));
std::filesystem::remove(noexist);
std::for_each(options2.input.begin(), options2.input.end(), [](auto& p){
std::filesystem::remove(p);});
//Compare input and ouput files
EXPECT_EQ(generic_read_file(image), generic_read_file(noexist));
std::filesystem::remove(noexist);
std::for_each(options2.input.begin(), options2.input.end(), [](auto& p){
std::filesystem::remove(p);});
}
TEST(IntegrationTest, arguments){
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "pippo", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file", "garbage"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-h", "4", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "boh", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file"}));
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-r", "lshgflushlgkjusghl", "input", "input", "output"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "output"}));
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "input", "output"}));
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "pippo", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file", "garbage"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-h", "4", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "create", "-n", "boh", "input_file", "output_file"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "create", "-n", "4", "input_file", "output_file"}));
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-n", "4", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input_file"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-r", "lshgflushlgkjusghl", "input", "input", "output"}),
std::invalid_argument);
EXPECT_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input"}),
std::invalid_argument);
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "output"}));
EXPECT_NO_THROW(
parse_arguments({"horcrux", "load", "-k", "lshgflushlgkjusghl", "input", "input", "output"}));
}

View File

@ -3,174 +3,174 @@
#include "test.h"
TEST(IoTests, FsPlainInput) {
EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument);
EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument);
EXPECT_THROW(horcrux::FsPlainInput input{empty}, std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsPlainInput input{text});
EXPECT_NO_THROW(horcrux::FsPlainInput input{image});
EXPECT_THROW(horcrux::FsPlainInput input{noexist}, std::invalid_argument);
EXPECT_THROW(horcrux::FsPlainInput input{folder}, std::invalid_argument);
EXPECT_THROW(horcrux::FsPlainInput input{empty}, std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsPlainInput input{text});
EXPECT_NO_THROW(horcrux::FsPlainInput input{image});
}
TEST(IoTests, FsPlainInputReadText) {
auto input = horcrux::FsPlainInput{text};
auto buf = generic_read_file(text);
EXPECT_EQ(buf, input.read());
auto input = horcrux::FsPlainInput{text};
auto buf = generic_read_file(text);
EXPECT_EQ(buf, input.read());
}
TEST(IoTests, FsPlainInputReadImg) {
auto input = horcrux::FsPlainInput{image};
auto buf = generic_read_file(image);
EXPECT_EQ(buf, input.read());
auto input = horcrux::FsPlainInput{image};
auto buf = generic_read_file(image);
EXPECT_EQ(buf, input.read());
}
TEST(IoTests, FsCryptoOutput) {
EXPECT_THROW(horcrux::FsCryptoOutput output(noexist, 1), std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsCryptoOutput output(folder, 1));
EXPECT_THROW(horcrux::FsCryptoOutput output(folder, 0), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(empty, 1), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(text, 1), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(image, 1), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(noexist, 1), std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsCryptoOutput output(folder, 1));
EXPECT_THROW(horcrux::FsCryptoOutput output(folder, 0), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(empty, 1), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(text, 1), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoOutput output(image, 1), std::invalid_argument);
}
TEST(IoTests, FsCryptoOutputWriteImage){
auto ifstream = std::ifstream{image, std::ios::binary};
auto buf = generic_read_file(image);
auto ifstream = std::ifstream{image, std::ios::binary};
auto buf = generic_read_file(image);
horcrux::FsCryptoOutput out(folder, 10);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
horcrux::FsCryptoOutput out(folder, 10);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
EXPECT_EQ(10, out.created_files.size());
delete_created_files(out);
EXPECT_EQ(10, out.created_files.size());
delete_created_files(out);
}
TEST(IoTests, FsCryptoOutputWriteText){
auto buf = generic_read_file(text);
auto buf = generic_read_file(text);
horcrux::FsCryptoOutput out(folder, 2);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
horcrux::FsCryptoOutput out(folder, 2);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
EXPECT_EQ(2, out.created_files.size());
delete_created_files(out);
EXPECT_EQ(2, out.created_files.size());
delete_created_files(out);
}
TEST(IoTest, FsCryptoInput){
EXPECT_THROW(horcrux::FsCryptoInput output({noexist, text, image}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({folder, text, image}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output(std::vector<std::string>{}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({empty}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({empty, empty}), std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsCryptoInput output({empty, empty, text}));
EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text}));
EXPECT_THROW(horcrux::FsCryptoInput output({noexist, text, image}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({folder, text, image}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output(std::vector<std::string>{}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({empty}), std::invalid_argument);
EXPECT_THROW(horcrux::FsCryptoInput output({empty, empty}), std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsCryptoInput output({empty, empty, text}));
EXPECT_NO_THROW(horcrux::FsCryptoInput output({image, empty, text}));
}
TEST(IoTest, FsCryptoInputImage){
auto buf = generic_read_file(image);
//split image using FsCryptoOutput
auto out = horcrux::FsCryptoOutput(folder, 7);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
auto buf = generic_read_file(image);
//split image using FsCryptoOutput
auto out = horcrux::FsCryptoOutput(folder, 7);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
auto files = get_created_filenames(out);
auto in = horcrux::FsCryptoInput(files);
auto read = in.read();
EXPECT_EQ(read, buf);
auto files = get_created_filenames(out);
auto in = horcrux::FsCryptoInput(files);
auto read = in.read();
EXPECT_EQ(read, buf);
files.push_back(text);
in = horcrux::FsCryptoInput(files);
read = in.read();
EXPECT_NE(read, buf);
files.push_back(text);
in = horcrux::FsCryptoInput(files);
read = in.read();
EXPECT_NE(read, buf);
delete_created_files(out);
delete_created_files(out);
}
TEST(IoTest, FsPlainOuput){
EXPECT_NO_THROW(horcrux::FsPlainOutput output{noexist});
EXPECT_THROW(horcrux::FsPlainOutput output{folder}, std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsPlainOutput output{empty});
EXPECT_NO_THROW(horcrux::FsPlainOutput output{text});
EXPECT_NO_THROW(horcrux::FsPlainOutput output{image});
EXPECT_NO_THROW(horcrux::FsPlainOutput output{noexist});
EXPECT_THROW(horcrux::FsPlainOutput output{folder}, std::invalid_argument);
EXPECT_NO_THROW(horcrux::FsPlainOutput output{empty});
EXPECT_NO_THROW(horcrux::FsPlainOutput output{text});
EXPECT_NO_THROW(horcrux::FsPlainOutput output{image});
}
TEST(IoTest, FsPlainOutputWrite){
auto buf = generic_read_file(image);
auto out = horcrux::FsPlainOutput(noexist);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
auto buf = generic_read_file(image);
auto out = horcrux::FsPlainOutput(noexist);
auto written = out.write(buf);
EXPECT_EQ(written, buf.size());
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
}
TEST(IoTest, PlainToPlain){
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsPlainOutput(noexist);
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsPlainOutput(noexist);
//Execute Plain To Plain
out.write(in.read());
//Execute Plain To Plain
out.write(in.read());
//Check outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
//Check outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
std::filesystem::remove(noexist);
}
TEST(IoTest, PlainToCrypto){
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsCryptoOutput(folder, 5);
auto buf = generic_read_file(image);
auto in = horcrux::FsPlainInput(image);
auto out = horcrux::FsCryptoOutput(folder, 5);
//Execute Plain to Crypto
out.write(in.read());
//Execute Plain to Crypto
out.write(in.read());
//use FsCryptoInput to re-join the files and check the outcome
auto files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
delete_created_files(out);
//use FsCryptoInput to re-join the files and check the outcome
auto files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
delete_created_files(out);
}
TEST(IoTest, CryptoToCrypto){
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsCryptoOutput(folder, 5, "test");
//Execute Crypto to Crypto
out.write(in.read());
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsCryptoOutput(folder, 5, "test");
//Execute Crypto to Crypto
out.write(in.read());
//use FsCryptoInput to re-join the files and check the outcome
files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
//use FsCryptoInput to re-join the files and check the outcome
files = get_created_filenames(out);
auto in_test = horcrux::FsCryptoInput(files);
auto buf2 = in_test.read();
EXPECT_EQ(buf, buf2);
delete_created_files(out_test);
delete_created_files(out);
delete_created_files(out_test);
delete_created_files(out);
}
TEST(IoTest, CryptoToPlain){
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
auto buf = generic_read_file(image);
// First use FsCryptoOutput to split the files
auto out_test = horcrux::FsCryptoOutput(folder, 8);
out_test.write(buf);
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsPlainOutput(noexist);
//Execute Crypto to Plain
out.write(in.read());
//use FsCryptoInput to re-join the files
auto files = get_created_filenames(out_test);
auto in = horcrux::FsCryptoInput(files);
auto out = horcrux::FsPlainOutput(noexist);
//Execute Crypto to Plain
out.write(in.read());
//check the outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
delete_created_files(out_test);
std::filesystem::remove(noexist);
//check the outcome
auto buf2 = generic_read_file(noexist);
EXPECT_EQ(buf, buf2);
delete_created_files(out_test);
std::filesystem::remove(noexist);
}

View File

@ -1,40 +1,43 @@
#include "gtest/gtest.h"
#include "test.h"
#include "gtest/gtest.h"
/* Utility functions */
std::vector<unsigned char> generic_read_file(const std::string& filename){
auto ifstream = std::ifstream{filename, std::ios::binary};
auto path = std::filesystem::path{filename};
auto size = std::filesystem::file_size(path);
std::vector<unsigned char> buf(size);
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
return buf;
std::vector<unsigned char> generic_read_file(const std::string& filename) {
auto ifstream = std::ifstream{filename, std::ios::binary};
auto path = std::filesystem::path{filename};
auto size = std::filesystem::file_size(path);
std::vector<unsigned char> buf(size);
ifstream.read(reinterpret_cast<char*>(buf.data()), size);
return buf;
}
std::vector<std::string> get_created_filenames(const horcrux::FsCryptoOutput& out){
std::vector<std::string> result(out.created_files.size());
transform(out.created_files.begin(), out.created_files.end(), result.begin(),
[](auto path){return path.string();});
return result;
std::vector<std::string> get_created_filenames(
const horcrux::FsCryptoOutput& out) {
std::vector<std::string> result(out.created_files.size());
transform(out.created_files.begin(), out.created_files.end(),
result.begin(), [](auto path) { return path.string(); } );
return result;
}
void delete_created_files(const horcrux::FsCryptoOutput& out){
std::for_each(out.created_files.begin(), out.created_files.end(),
[](auto f){std::filesystem::remove(f);});
void delete_created_files(const horcrux::FsCryptoOutput& out) {
std::for_each(out.created_files.begin(), out.created_files.end(),
[](auto f) { std::filesystem::remove(f); } );
}
std::vector<std::string> get_encrypted_files(const std::string& folder, const std::string& basename) {
std::vector<std::string> result;
for (auto& p: std::filesystem::directory_iterator(folder)){
if (p.path().string().starts_with(basename) &&
p.path().string().ends_with(".enc"))
result.push_back(p.path().string());
}
std::sort(result.begin(), result.end());
return result;
std::vector<std::string> get_encrypted_files(const std::string& folder,
const std::string& basename) {
std::vector<std::string> result;
for (auto& p: std::filesystem::directory_iterator(folder)) {
if (p.path().string().starts_with(basename)
&& p.path().string().ends_with(".enc")) {
result.push_back(p.path().string());
}
}
std::sort(result.begin(), result.end());
return result;
}
int main(int argc, char *argv[]) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -28,8 +28,8 @@ std::vector<std::string> get_encrypted_files(const std::string& folder, const st
/* test command:
echo "ditemi perche' se la mucca fa mu il merlo non fa me" |
scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF |
xxd -i
scripts/aes256.sh -k 0123456789ABCDEF0123456789ABCDEF -i 0123456789ABCDEF |
xxd -i
*/
const std::string test1_str = "ditemi perche' se la mucca fa mu il merlo non fa me";
const std::string test1_key = "0123456789ABCDEF0123456789ABCDEF";

View File

@ -16,31 +16,31 @@ const std::string test4_str = "mi e' entrata una bruschetta nell'occhio";
const std::string test4_base64 = "bWkgZScgZW50cmF0YSB1bmEgYnJ1c2NoZXR0YSBuZWxsJ29jY2hpbw==";
TEST(utilTests, to_base64Test){
EXPECT_EQ(test1_base64, horcrux::to_base64(std::vector<unsigned char>(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(test3_base64, horcrux::to_base64(std::vector<unsigned char>(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(test1_base64, horcrux::to_base64(std::vector<unsigned char>(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(test3_base64, horcrux::to_base64(std::vector<unsigned char>(test3_str.begin(), test3_str.end())));
EXPECT_EQ(test4_base64, horcrux::to_base64(std::vector<unsigned char>(test4_str.begin(), test4_str.end())));
}
TEST(utilTests, from_base64Test){
auto result = horcrux::from_base64(test1_base64);
EXPECT_EQ(test1_str, std::string(result.begin(), result.end()));
auto result = horcrux::from_base64(test1_base64);
EXPECT_EQ(test1_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test2_base64);
EXPECT_EQ(test2_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test2_base64);
EXPECT_EQ(test2_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test3_base64);
EXPECT_EQ(test3_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test3_base64);
EXPECT_EQ(test3_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test4_base64);
EXPECT_EQ(test4_str, std::string(result.begin(), result.end()));
result = horcrux::from_base64(test4_base64);
EXPECT_EQ(test4_str, std::string(result.begin(), result.end()));
}
TEST(utilTests, generate_random){
/* It works, but it's not deterministic */
//EXPECT_NE(horcrux::generate_random(32), horcrux::generate_random(32));
//EXPECT_NE(horcrux::generate_random(16), horcrux::generate_random(16));
//EXPECT_NE(horcrux::generate_random(8), horcrux::generate_random(8));
//EXPECT_NE(horcrux::generate_random(1), horcrux::generate_random(1));
EXPECT_EQ(horcrux::generate_random(0), horcrux::generate_random(0));
/* It works, but it's not deterministic */
//EXPECT_NE(horcrux::generate_random(32), horcrux::generate_random(32));
//EXPECT_NE(horcrux::generate_random(16), horcrux::generate_random(16));
//EXPECT_NE(horcrux::generate_random(8), horcrux::generate_random(8));
//EXPECT_NE(horcrux::generate_random(1), horcrux::generate_random(1));
EXPECT_EQ(horcrux::generate_random(0), horcrux::generate_random(0));
}