diff --git a/src/crypto.cpp b/src/crypto.cpp index cbb06c1..d125e1e 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -33,9 +33,8 @@ public: }; -EvpCipherCtx AES256_CBC::init(Mode mode, - const std::vector& key, - const std::vector& iv) { +EvpCipherCtx AES256_CBC::init(Mode mode, const raw_data& key, + const raw_data& iv) { if (key.size() != kKeySize) { throw std::runtime_error("Wrong key size"); } @@ -60,12 +59,11 @@ EvpCipherCtx AES256_CBC::init(Mode mode, return ctx; } -size_t AES256_CBC::process_chunk( - Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in, bool resize_out) { +size_t AES256_CBC::process_chunk(Mode mode, EvpCipherCtx& ctx, + raw_data::const_iterator begin, + raw_data::const_iterator end, + raw_data& output, size_t output_offset, + bool resize_in, bool resize_out) { auto chunk_size = end - begin; int len; if (resize_in) { @@ -91,8 +89,7 @@ size_t AES256_CBC::process_chunk( } size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx, - std::vector& output, - size_t output_offset, + raw_data& output, size_t output_offset, bool resize_in, bool resize_out) { int len; if (resize_in) { @@ -115,12 +112,11 @@ size_t AES256_CBC::process_final(Mode mode, EvpCipherCtx& ctx, return len; } -size_t AES256_CBC::process_all( - Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, - bool resize_in, bool resize_out) { +size_t AES256_CBC::process_all(Mode mode, EvpCipherCtx& ctx, + raw_data::const_iterator begin, + raw_data::const_iterator end, + raw_data& 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, @@ -128,32 +124,27 @@ size_t AES256_CBC::process_all( return len; } -std::vector AES256_CBC::process( - const std::vector& inputdata) { +raw_data AES256_CBC::process(const raw_data& inputdata) { if (mode == Mode::kEncrypt) return encrypt(inputdata); else return decrypt(inputdata); } -std::vector AES256_CBC::encrypt( - const std::vector& plaintext) { +raw_data AES256_CBC::encrypt(const raw_data& plaintext) { return encrypt(encryption_key, horcrux::generate_random(kIvSize), plaintext); } -std::vector AES256_CBC::decrypt( - const std::vector& ciphertext) { +raw_data AES256_CBC::decrypt(const raw_data& ciphertext) { return decrypt(encryption_key, ciphertext); } -std::vector AES256_CBC::encrypt( - const std::vector& key, - const std::vector& iv, - const std::vector& input) { +raw_data AES256_CBC::encrypt(const raw_data& key, const raw_data& iv, + const raw_data& input) { auto ctx = init(Mode::kEncrypt, key, iv); // Make sure ouput is large enough to contain IV + encrypted data + padding - std::vector output(input.size() + (2 * kIvSize)); + raw_data output(input.size() + (2 * kIvSize)); std::copy(iv.begin(), iv.end(), output.begin()); process_all(Mode::kEncrypt, ctx, input.begin(), input.end(), output, kIvSize); @@ -162,12 +153,11 @@ std::vector AES256_CBC::encrypt( /* -int AES256_CBC::encrypt(const std::vector& key, - const std::vector& iv, +int AES256_CBC::encrypt(const raw_data& key, const raw_data& iv, std::istream input, const size_t input_len, std::ostream output, size_t& output_len) { - auto inbuf = std::vector(input_len); - auto outbuf = std::vector(input_len + 16); + auto inbuf = raw_data(input_len); + auto outbuf = raw_data(input_len + 16); input.read(reinterpret_cast(inbuf.data()), input_len); encrypt(key, iv, inbuf, outbuf); @@ -178,28 +168,21 @@ int AES256_CBC::encrypt(const std::vector& key, } */ -std::vector AES256_CBC::decrypt( - const std::vector& key, - const std::vector& ciphertext) { - std::vector iv(ciphertext.begin(), - ciphertext.begin() + kIvSize); +raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& ciphertext) { + raw_data iv(ciphertext.begin(), ciphertext.begin() + kIvSize); return decrypt(key, iv, ciphertext.begin() + kIvSize, ciphertext.end()); } -std::vector AES256_CBC::decrypt( - const std::vector& key, - const std::vector& iv, - const std::vector& ciphertext) { +raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& iv, + const raw_data& ciphertext) { return decrypt(key, iv, ciphertext.begin(), ciphertext.end()); } -std::vector AES256_CBC::decrypt( - const std::vector& key, - const std::vector& iv, - std::vector::const_iterator begin, - std::vector::const_iterator end) { +raw_data AES256_CBC::decrypt(const raw_data& key, const raw_data& iv, + raw_data::const_iterator begin, + raw_data::const_iterator end) { auto ctx = init(Mode::kDecrypt, key, iv); - std::vector output; + raw_data output; process_all(Mode::kDecrypt, ctx, begin, end, output, 0); return output; } diff --git a/src/crypto.h b/src/crypto.h index 95587c4..97a5336 100644 --- a/src/crypto.h +++ b/src/crypto.h @@ -25,7 +25,7 @@ public: /** @brief create Cipher * @param m Cipher mode (encrypt/decrypt) * @param key use provided key for encryption/decryption */ - Cipher(Mode m, const std::vector& key) : + Cipher(Mode m, const raw_data& key) : mode(m), encryption_key(key) {} /** @brief default destructor */ @@ -33,7 +33,7 @@ public: protected: /** @brief key used for encryption / decryption */ - std::vector encryption_key; + raw_data encryption_key; public: /** @brief Cipher mode (encrypt or decrypt) */ @@ -42,7 +42,7 @@ public: /** @brief get Cipher encryption key * @return the encryption key */ - const std::vector& get_encryption_key() { + const raw_data& get_encryption_key() { return encryption_key; } @@ -50,22 +50,22 @@ public: * @param inputdata buffer containing data to process * @return processed data */ - virtual std::vector process( - const std::vector& inputdata) = 0; + virtual raw_data process( + const raw_data& inputdata) = 0; /** @brief encrypt the content of a buffer using Cipher key * @param plaintext the input buffer * @return encrypted data */ - virtual std::vector encrypt( - const std::vector& plaintext) = 0; + virtual raw_data encrypt( + const raw_data& plaintext) = 0; /** @brief decrypt the content of a buffer using Cipher key * @param decrypt the input buffer * @return decrypted data */ - virtual std::vector decrypt( - const std::vector& ciphertext) = 0; + virtual raw_data decrypt( + const raw_data& ciphertext) = 0; }; // forward declaration @@ -98,7 +98,7 @@ public: /** @brief Create cipher in decrypt mode using provided key * @param key 256 bit key */ - explicit AES256_CBC(const std::vector& key) + explicit AES256_CBC(const raw_data& key) : Cipher(Mode::kDecrypt, key) { if (key.size() != kKeySize) throw std::invalid_argument("Invalid key size"); @@ -110,22 +110,22 @@ public: * @param inputdata buffer containing data to process * @return processed data */ - std::vector process( - const std::vector& inputdata) override; + raw_data process( + const raw_data& inputdata) override; /** @brief encrypt the content of a buffer using Cipher key * @param plaintext the input buffer * @return encrypted data */ - std::vector encrypt( - const std::vector& plaintext) override; + raw_data encrypt( + const raw_data& plaintext) override; /** @brief decrypt the content of a buffer using Cipher key * @param decrypt the input buffer * @return decrypted data */ - std::vector decrypt( - const std::vector& ciphertext) override; + raw_data decrypt( + const raw_data& ciphertext) override; /** @brief Read the whole input and return a buffer with the encrypted data * @param key the encryption key @@ -134,13 +134,13 @@ public: * @param input the input buffer * @return the encrypted data buffer */ - std::vector encrypt(const std::vector& key, - const std::vector& iv, - const std::vector& input); + raw_data encrypt(const raw_data& key, + const raw_data& iv, + const raw_data& input); /* - int encrypt(const std::vector& key, - const std::vector& iv, + int encrypt(const raw_data& key, + const raw_data& iv, std::istream input, const size_t input_len, std::ostream output, size_t& output_len); @@ -151,9 +151,9 @@ public: * @param ciphertext the input buffer * @return the encrypted data buffer */ - std::vector decrypt( - const std::vector& key, - const std::vector& ciphertext); + raw_data decrypt( + const raw_data& key, + const raw_data& ciphertext); /** @brief Read the whole input and return a buffer with the decrypted data. * ciphertext does not contain the iv. @@ -162,10 +162,10 @@ public: * @param ciphertext the input buffer * @return the encrypted data buffer */ - std::vector decrypt( - const std::vector& key, - const std::vector& iv, - const std::vector& ciphertext); + raw_data decrypt( + const raw_data& key, + const raw_data& iv, + const raw_data& ciphertext); /** @brief Read some input from begin to end and return a buffer with the * decrypted data. Output data is finalized. @@ -175,11 +175,11 @@ public: * @param end end of the input buffer * @return the encrypted data buffer */ - std::vector decrypt( - const std::vector& key, - const std::vector& iv, - std::vector::const_iterator begin, - std::vector::const_iterator end); + raw_data decrypt( + const raw_data& key, + const raw_data& iv, + raw_data::const_iterator begin, + raw_data::const_iterator end); private: /** @brief Create a Cipher context object needed for enc/dec operations @@ -188,8 +188,8 @@ private: * @param the initialization vector * @return the Cipher context object */ - EvpCipherCtx init(Mode mode, const std::vector& key, - const std::vector& iv); + EvpCipherCtx init(Mode mode, const raw_data& key, + const raw_data& iv); /** @brief process a chunk of input * @param mode encrypt or decrypt @@ -204,9 +204,9 @@ private: * @param resize_out shrink the output buffer at the end */ size_t process_chunk(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, + raw_data::const_iterator begin, + raw_data::const_iterator end, + raw_data& output, size_t output_offset, bool resize_in = true, bool resize_out = true); @@ -222,7 +222,7 @@ private: * @param resize_out shrink the output buffer at the end */ size_t process_final(Mode mode, EvpCipherCtx& ctx, - std::vector& output, + raw_data& output, size_t output_offset, bool resize_in = true, bool resize_out = true); @@ -239,9 +239,9 @@ private: * @param resize_out shrink the output buffer at the end */ size_t process_all(Mode mode, EvpCipherCtx& ctx, - std::vector::const_iterator begin, - std::vector::const_iterator end, - std::vector& output, size_t output_offset, + raw_data::const_iterator begin, + raw_data::const_iterator end, + raw_data& output, size_t output_offset, bool resize_in = true, bool resize_out = true); }; diff --git a/src/io.cpp b/src/io.cpp index cf2252b..7386735 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -17,8 +17,8 @@ FsPlainInput::FsPlainInput(const std::string& path) file_stream = std::ifstream(file_path, std::ios::binary); } -std::vector FsPlainInput::read() { - std::vector result(file_size); +raw_data FsPlainInput::read() { + raw_data result(file_size); file_stream.read(reinterpret_cast(result.data()), file_size); return result; } @@ -40,8 +40,8 @@ FsCryptoInput::FsCryptoInput(const std::vector& filenames) { } } -std::vector FsCryptoInput::read() { - std::vector result(total_size); +raw_data FsCryptoInput::read() { + raw_data result(total_size); size_t data_read{0}; for (auto& f : file_paths) { 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"); } } -size_t FsPlainOutput::write(const std::vector& to_write) { +size_t FsPlainOutput::write(const raw_data& to_write) { std::ofstream out(file_path, std::ios::binary); out.write(reinterpret_cast(to_write.data()), 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& to_write) { +size_t FsCryptoOutput::write(const raw_data& to_write) { size = to_write.size() / num; std::ofstream f; created_files.clear(); diff --git a/src/io.h b/src/io.h index 680bf21..5d35422 100644 --- a/src/io.h +++ b/src/io.h @@ -5,6 +5,7 @@ #include #include #include +#include "utils.h" namespace horcrux { @@ -15,7 +16,7 @@ public: virtual ~Input() = default; /** @brief Read from the input * @return new buffer containing all read data */ - virtual std::vector read() = 0; + virtual raw_data read() = 0; }; /** @brief Base Class for Output @@ -26,7 +27,7 @@ public: /** @brief Write to the output * @param data to write * @return written data */ - virtual size_t write(const std::vector& data) = 0; + virtual size_t write(const raw_data& data) = 0; }; /** @brief Input that reads data from a regular file */ @@ -49,7 +50,7 @@ public: /** @brief Read from the input * @return new buffer containing all read data */ - std::vector read() override; + raw_data read() override; }; /** @brief Input that reads scattered data from multiple files */ @@ -71,7 +72,7 @@ public: /** @brief Read from the input * @details basically concatenate the input files. * @return new buffer containing all read data */ - std::vector read() override; + raw_data read() override; }; /** @brief Output that writes to a single regular file */ @@ -91,7 +92,7 @@ public: /** @brief Write to the output * @param data to write * @return written data */ - size_t write(const std::vector& 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 @@ -128,7 +129,7 @@ public: /** @brief Write to the output * @param data to write * @return written data */ - size_t write(const std::vector& to_write) override; + size_t write(const raw_data& to_write) override; }; }; // namespace horcrux #endif // HORCRUX_SRC_IO_H diff --git a/src/utils.cpp b/src/utils.cpp index f311e50..f5661cc 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -4,7 +4,7 @@ #include #include -std::string horcrux::to_base64(const std::vector& binary) { +std::string horcrux::to_base64(const raw_data& binary) { using namespace boost::archive::iterators; using It = base64_from_binary::const_iterator, 6, 8>>; @@ -13,7 +13,7 @@ std::string horcrux::to_base64(const std::vector& binary) { return base64.append((3 - binary.size() % 3) % 3, '='); } -std::vector horcrux::from_base64(const std::string& base64) { +horcrux::raw_data horcrux::from_base64(const std::string& base64) { using namespace boost::archive::iterators; using It = transform_width, 8, 6>; @@ -30,8 +30,8 @@ std::vector horcrux::from_base64(const std::string& base64) { return binary; } -std::vector horcrux::generate_random(const size_t size) { - std::vector res(size); +horcrux::raw_data horcrux::generate_random(const size_t size) { + horcrux::raw_data res(size); if (!RAND_bytes(res.data(), size)) { throw std::exception(); } diff --git a/src/utils.h b/src/utils.h index 9552dea..fba539e 100644 --- a/src/utils.h +++ b/src/utils.h @@ -6,9 +6,11 @@ namespace horcrux { -std::string to_base64(const std::vector& binary); -std::vector from_base64(const std::string& base64); -std::vector generate_random(const size_t size); +typedef std::vector raw_data; + +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 diff --git a/test/crypto-test.cpp b/test/crypto-test.cpp index 907ffbc..476d7aa 100644 --- a/test/crypto-test.cpp +++ b/test/crypto-test.cpp @@ -6,9 +6,9 @@ using namespace horcrux; TEST(CryptoTest, encrypt1){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); AES256_CBC a; auto output = a.encrypt(key, iv, plaintext); @@ -19,11 +19,11 @@ TEST(CryptoTest, encrypt1){ } TEST(CryptoTest, encrypt2){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); //size_t output_len = plaintext.size() + 16; - //std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + //raw_data output(output_len); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); AES256_CBC a; auto output = a.encrypt(key, iv, plaintext); @@ -35,11 +35,11 @@ TEST(CryptoTest, encrypt2){ } TEST(CryptoTest, encrypt3){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); //size_t output_len = plaintext.size() + 16; - //std::vector output(output_len); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + //raw_data output(output_len); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); AES256_CBC a(key); auto output = a.encrypt(plaintext); @@ -50,9 +50,9 @@ TEST(CryptoTest, encrypt3){ } TEST(CryptoTest, decrypt1){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); AES256_CBC a; auto output = a.decrypt(key, iv, test1_enc); @@ -60,12 +60,12 @@ TEST(CryptoTest, decrypt1){ EXPECT_EQ(plaintext, output); } TEST(CryptoTest, decrypt2){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); size_t output_len = test1_enc.size(); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); // constructs encrypted input (iv + encrypted_data) - auto input = std::vector(iv); + auto input = raw_data(iv); input.insert(input.end(),test1_enc.begin(), test1_enc.end()); AES256_CBC a; @@ -74,12 +74,12 @@ TEST(CryptoTest, decrypt2){ EXPECT_EQ(plaintext, output); } TEST(CryptoTest, decrypt3){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); size_t output_len = test1_enc.size(); - const std::vector key(test1_key.begin(), test1_key.end()); - const std::vector iv(test1_iv.begin(), test1_iv.end()); + const raw_data key(test1_key.begin(), test1_key.end()); + const raw_data iv(test1_iv.begin(), test1_iv.end()); // constructs encrypted input (iv + encrypted_data) - auto input = std::vector(iv); + auto input = raw_data(iv); input.insert(input.end(),test1_enc.begin(), test1_enc.end()); AES256_CBC a(key); @@ -88,7 +88,7 @@ TEST(CryptoTest, decrypt3){ EXPECT_EQ(plaintext, output); } TEST(CryptoTest, endToEnd){ - const std::vector plaintext(test1_str.begin(), test1_str.end()); + const raw_data plaintext(test1_str.begin(), test1_str.end()); AES256_CBC a; auto crypto = a.encrypt(plaintext); EXPECT_GT(crypto.size(), plaintext.size()); diff --git a/test/main.cpp b/test/main.cpp index c5e8a03..148880c 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -2,11 +2,11 @@ #include "gtest/gtest.h" /* Utility functions */ -std::vector 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 path = std::filesystem::path{filename}; auto size = std::filesystem::file_size(path); - std::vector buf(size); + horcrux::raw_data buf(size); ifstream.read(reinterpret_cast(buf.data()), size); return buf; } diff --git a/test/test.h b/test/test.h index 2aac5b7..c16ec7e 100644 --- a/test/test.h +++ b/test/test.h @@ -4,6 +4,7 @@ #include #include "io.h" #include "crypto.h" +#include "utils.h" /* IO Test utils */ #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 image{TEST_WORK_DIR "/mangoni.jpg"}; -std::vector generic_read_file(const std::string& filename); +horcrux::raw_data generic_read_file(const std::string& filename); std::vector 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_key = "0123456789ABCDEF0123456789ABCDEF"; const std::string test1_iv = "0123456789ABCDEF"; -const std::vector test1_enc { +const horcrux::raw_data test1_enc { 0x4c, 0x17, 0x6e, 0x6d, 0xd2, 0x83, 0x51, 0x52, 0xfc, 0x5d, 0xbe, 0x0f, 0x1b, 0xcf, 0x86, 0xef, 0x73, 0x91, 0x58, 0xc4, 0xdd, 0x1b, 0x09, 0x3d, 0x77, 0xe0, 0x78, 0x5d, 0x21, 0xfe, 0x59, 0x9c, 0xb2, 0x12, 0xa6, 0x81, diff --git a/test/utils-test.cpp b/test/utils-test.cpp index 37fce1c..f371321 100644 --- a/test/utils-test.cpp +++ b/test/utils-test.cpp @@ -16,10 +16,10 @@ 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(test1_str.begin(), test1_str.end()))); - EXPECT_EQ(test2_base64, horcrux::to_base64(std::vector(test2_str.begin(), test2_str.end()))); - EXPECT_EQ(test3_base64, horcrux::to_base64(std::vector(test3_str.begin(), test3_str.end()))); - EXPECT_EQ(test4_base64, horcrux::to_base64(std::vector(test4_str.begin(), test4_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(horcrux::raw_data(test2_str.begin(), test2_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(horcrux::raw_data(test4_str.begin(), test4_str.end()))); } TEST(utilTests, from_base64Test){