diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..00569a2d --- /dev/null +++ b/.clang-format @@ -0,0 +1,13 @@ +--- +BasedOnStyle: LLVM +AccessModifierOffset: -4 +AllowShortFunctionsOnASingleLine: Inline +BinPackArguments: false +BinPackParameters: false +BreakConstructorInitializersBeforeComma: true +ContinuationIndentWidth: 4 +ColumnLimit: 100 +IndentWidth: 4 +NamespaceIndentation: None +PointerAlignment: Right +... diff --git a/CMakeLists.txt b/CMakeLists.txt index 96cac203..0b7b62b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,16 +80,16 @@ if (BUILD_testsuite) testsuite/hash_streams_tests.cc testsuite/stream_ciphers_streams_tests.cc testsuite/block_streams_tests.cc - testsuite/test-utils/test_streams - testsuite/test-utils/hash_test_case - testsuite/test-utils/stream_ciphers_test_case - testsuite/test-utils/block_test_case - testsuite/test-utils/common_functions - testsuite/test-utils/test_case.h) + testsuite/test_utils/test_streams + testsuite/test_utils/hash_test_case + testsuite/test_utils/stream_ciphers_test_case + testsuite/test_utils/block_test_case + testsuite/test_utils/common_functions + testsuite/test_utils/test_case.h) target_compile_definitions(testsuite PUBLIC "TEST_STREAM=1") - file(COPY testsuite/test-resources DESTINATION resources) + file(COPY testsuite/test_resources DESTINATION resources) # Standard linking to gtest stuff. target_link_libraries(testsuite gtest gtest_main) diff --git a/generator.cc b/generator.cc index 9d2fba16..96167c36 100644 --- a/generator.cc +++ b/generator.cc @@ -6,8 +6,8 @@ #include #include -#include #include +#include static std::ifstream open_config_file(const std::string path) { std::ifstream file(path); @@ -16,9 +16,9 @@ static std::ifstream open_config_file(const std::string path) { return file; } -static std::string out_name(json const& config) { - auto fname_it = config.find("file-name"); - if (fname_it != config.end()){ +static std::string out_name(json const &config) { + auto fname_it = config.find("file_name"); + if (fname_it != config.end()) { return *fname_it; } @@ -31,7 +31,7 @@ static std::string out_name(json const& config) { std::string a = config_ref.at("algorithm"); ss << a << "_r"; ss << std::setw(2) << std::setfill('0') << std::size_t(config_ref.at("round")); - ss << "_b" << config_ref.at("block-size"); + ss << "_b" << config_ref.at("block_size"); ss << ".bin"; return ss.str(); } @@ -39,24 +39,24 @@ static std::string out_name(json const& config) { generator::generator(const std::string config) : generator(open_config_file(config)) {} -generator::generator(json const& config) +generator::generator(json const &config) : _config(config) , _seed(seed::create(config.at("seed"))) - , _tv_count(config.at("tv-count")) + , _tv_count(config.at("tv_count")) , _o_file_name(out_name(config)) { seed_seq_from main_seeder(_seed); - _stream_a = make_stream(config.at("stream"), main_seeder, std::size_t(config.at("tv-size"))); + _stream_a = make_stream(config.at("stream"), main_seeder, std::size_t(config.at("tv_size"))); } void generator::generate() { auto stdout_it = _config.find("stdout"); std::unique_ptr ofstream_ptr; - std::ostream * o_file = nullptr; + std::ostream *o_file = nullptr; - if (stdout_it == _config.end() || stdout_it->get() == false){ + if (stdout_it == _config.end() || stdout_it->get() == false) { ofstream_ptr = std::make_unique(_o_file_name, std::ios::binary); o_file = ofstream_ptr.get(); } else { diff --git a/generator.h b/generator.h index d1e0f717..0090328a 100644 --- a/generator.h +++ b/generator.h @@ -1,25 +1,25 @@ #pragma once #include "stream.h" -#include -#include -#include #include #include +#include +#include +#include struct generator { generator(const std::string cofig); - generator(std::istream& config) + generator(std::istream &config) : generator(json::parse(config)) {} - generator(std::istream&& config) + generator(std::istream &&config) : generator(json::parse(config)) {} - generator(json&& config) + generator(json &&config) : generator(config) {} - generator(json const& config); + generator(json const &config); void generate(); diff --git a/main.cc b/main.cc index 14a2e503..e0098de7 100644 --- a/main.cc +++ b/main.cc @@ -1,7 +1,7 @@ #include "generator.h" -#include #include #include +#include #include void test_environment() { @@ -21,7 +21,7 @@ static cmd options{{"-h", "--help", "display help message", &config::hel {"-v", "--version", "display program version", &config::version}, {"-c", "--config", "specify the config file to load", &config::config}}; -int main(const int argc, const char** argv) try { +int main(const int argc, const char **argv) try { auto cfg = options.parse(make_view(argv, argc)); if (cfg.help) { @@ -38,7 +38,7 @@ int main(const int argc, const char** argv) try { } return 0; -} catch (std::exception& e) { +} catch (std::exception &e) { logger::error(e.what()); return 1; } diff --git a/stream.h b/stream.h index bd5b987e..76177ae5 100644 --- a/stream.h +++ b/stream.h @@ -1,9 +1,9 @@ #pragma once -#include -#include #include +#include #include +#include #include using value_type = std::uint8_t; @@ -15,13 +15,9 @@ struct stream { virtual vec_cview next() = 0; - vec_cview get_data() const { - return make_cview(_data); - } + vec_cview get_data() const { return make_cview(_data); } - void set_data(vec_cview data) { - std::copy(data.begin(), data.end(), _data.begin()); - } + void set_data(vec_cview data) { std::copy(data.begin(), data.end(), _data.begin()); } std::size_t osize() const { return _osize; } diff --git a/streams.cc b/streams.cc index f56e370b..6846afd4 100644 --- a/streams.cc +++ b/streams.cc @@ -6,7 +6,7 @@ file_stream::file_stream(const json &config, const std::size_t osize) , _istream(_path, std::ios::binary) {} vec_cview file_stream::next() { - _istream.read(reinterpret_cast(_data.data()), osize()); + _istream.read(reinterpret_cast(_data.data()), osize()); if (_istream.fail()) { perror("stream failbit (or badbit). error state:"); @@ -18,34 +18,36 @@ vec_cview file_stream::next() { return make_cview(_data); } -single_value_stream::single_value_stream(const json &config, default_seed_source& seeder, const std::size_t osize) +single_value_stream::single_value_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize) : stream(osize) { auto stream = make_stream(config, seeder, osize); vec_cview single_vector = stream->next(); std::copy(single_vector.begin(), single_vector.end(), _data.begin()); } -vec_cview single_value_stream::next() -{ +vec_cview single_value_stream::next() { return make_cview(_data); } - -repeating_stream::repeating_stream(const json& config, default_seed_source &seeder, const std::size_t osize) +repeating_stream::repeating_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize) : stream(osize) , _source(make_stream(config.at("source"), seeder, osize)) , _period(unsigned(config.value("period", 0))) - , _i(0) { } - + , _i(0) {} -repeating_stream::repeating_stream(const std::size_t osize, std::unique_ptr source, const unsigned period) +repeating_stream::repeating_stream(const std::size_t osize, + std::unique_ptr source, + const unsigned period) : stream(osize) , _source(std::move(source)) , _period(period) - , _i(0) { } + , _i(0) {} -vec_cview repeating_stream::next() -{ +vec_cview repeating_stream::next() { if (_i % _period == 0) { _data = _source->next().copy_to_vector(); } @@ -59,7 +61,7 @@ counter::counter(const std::size_t osize) } vec_cview counter::next() { - for (value_type& value : _data) { + for (value_type &value : _data) { if (value != std::numeric_limits::max()) { ++value; break; @@ -70,21 +72,18 @@ vec_cview counter::next() { } random_start_counter::random_start_counter(default_seed_source &seeder, const std::size_t osize) - : counter(osize) -{ + : counter(osize) { auto stream = std::make_unique(seeder, osize); vec_cview single_vector = stream->next(); std::copy(single_vector.begin(), single_vector.end(), _data.begin()); } -template +template xor_stream::xor_stream(const nlohmann::json &config, Seeder &&seeder, const std::size_t osize) : stream(osize) - , _source(make_stream(config.at("source"), seeder, osize * 2)) { } + , _source(make_stream(config.at("source"), seeder, osize * 2)) {} - -vec_cview xor_stream::next() -{ +vec_cview xor_stream::next() { vec_cview in = _source->next(); auto first1 = in.begin(); const auto last = in.begin() + _data.size(); @@ -98,15 +97,15 @@ vec_cview xor_stream::next() return make_cview(_data); } -template -rnd_plt_ctx_stream::rnd_plt_ctx_stream(const nlohmann::json &config, Seeder &&seeder, const std::size_t osize) +template +rnd_plt_ctx_stream::rnd_plt_ctx_stream(const nlohmann::json &config, + Seeder &&seeder, + const std::size_t osize) : stream(osize) - , _rng(std::make_unique(seeder, osize/2)) - , _source(make_stream(config, seeder, osize/2, core::optional{_rng.get()})) { } - + , _rng(std::make_unique(seeder, osize / 2)) + , _source(make_stream(config, seeder, osize / 2, core::optional{_rng.get()})) {} -vec_cview rnd_plt_ctx_stream::next() -{ +vec_cview rnd_plt_ctx_stream::next() { vec_cview ctx = _source->next(); vec_cview ptx = _rng->get_data(); @@ -116,15 +115,13 @@ vec_cview rnd_plt_ctx_stream::next() return make_cview(_data); } -template +template rho_stream::rho_stream(const nlohmann::json &config, Seeder &&seeder, const std::size_t osize) : stream(osize) , _ptx(std::make_unique(osize)) - , _source(make_stream(config, seeder, osize, core::optional{_ptx.get()})) { } + , _source(make_stream(config, seeder, osize, core::optional{_ptx.get()})) {} - -vec_cview rho_stream::next() -{ +vec_cview rho_stream::next() { _ptx->set_data(make_cview(_data)); vec_cview ctx = _source->next(); @@ -132,21 +129,20 @@ vec_cview rho_stream::next() return make_cview(_data); } - vec_cview hw_counter::next() { std::copy_n(_origin_data.begin(), osize(), _data.begin()); - for (const auto& pos : _cur_positions) { + for (const auto &pos : _cur_positions) { _data[pos / 8] ^= (1 << (pos % 8)); } - if (!combination_next()){ - if (_increase_hw){ + if (!combination_next()) { + if (_increase_hw) { _cur_hw += 1; - } else if (_randomize_overflow){ + } else if (_randomize_overflow) { randomize(); // combination space depleted && not increasing HW. } - if (_cur_hw > osize() * 8 && _increase_hw){ + if (_cur_hw > osize() * 8 && _increase_hw) { _cur_hw = 1; // reset } @@ -156,12 +152,14 @@ vec_cview hw_counter::next() { return make_cview(_data); } -column_stream::column_stream(const json& config, default_seed_source& seeder, const std::size_t osize) +column_stream::column_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize) : stream(osize) , _internal_bit_size(std::size_t(config.at("size")) * 8) , _buf(_internal_bit_size) , _position(0) - , _source(make_stream(config.at("source"), seeder, _internal_bit_size/8)) { + , _source(make_stream(config.at("source"), seeder, _internal_bit_size / 8)) { for (auto &v : _buf) v.resize(osize); } @@ -181,19 +179,21 @@ vec_cview column_stream::next() { // something like matrix transpose for (std::size_t j = 0; j < _internal_bit_size; ++j) { - _buf[j][i/8] += ((vec[j/8] & (0x1 << (j % 8))) >> j) << (i % 8); + _buf[j][i / 8] += ((vec[j / 8] & (0x1 << (j % 8))) >> j) << (i % 8); } - } } return make_cview(_buf[_position++]); // return and increment } -column_fixed_position_stream::column_fixed_position_stream(const json& config, default_seed_source& seeder, const std::size_t osize, const std::size_t position) +column_fixed_position_stream::column_fixed_position_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + const std::size_t position) : stream(osize) , _position(position) - , _source(make_stream(config.at("source"), seeder, std::size_t(config.at("size")))) { } + , _source(make_stream(config.at("source"), seeder, std::size_t(config.at("size")))) {} vec_cview column_fixed_position_stream::next() { for (auto &val : _data) @@ -205,84 +205,95 @@ vec_cview column_fixed_position_stream::next() { auto vec = _source->next().data(); std::size_t rev_i = 7 - (i % 8); - _data[i/8] += ((vec[_position/8] & (0x1 << rev_pos)) >> rev_pos) << rev_i; - + _data[i / 8] += ((vec[_position / 8] & (0x1 << rev_pos)) >> rev_pos) << rev_i; } return make_cview(_data); // return and increment } -std::unique_ptr -make_stream(const json& config, default_seed_source& seeder, const std::size_t osize, - core::optional stream) { +std::unique_ptr make_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional stream) { const std::string type = config.at("type"); if (osize == 0) throw std::runtime_error("Stream " + type + " cannot have osize 0."); - if (type == "dummy-stream") + // trivial source only streams + if (type == "dummy_stream") return std::make_unique(osize); - else if (type == "file-stream") + else if (type == "file_stream") return std::make_unique(config, osize); - else if (type == "true-stream") + else if (type == "true_stream") return std::make_unique(osize); - else if (type == "false-stream") + else if (type == "false_stream") return std::make_unique(osize); - else if (type == "mt19937-stream") + else if (type == "mt19937_stream") return std::make_unique(seeder, osize); - else if (type == "pcg32-stream" or type == "random-stream") + else if (type == "pcg32_stream" or type == "random_stream") return std::make_unique(seeder, osize); - else if (type == "single-value-stream") - return std::make_unique(config.at("source"), seeder, osize); - else if (type == "repeating-stream") - return std::make_unique(config.at("source"), seeder, osize); - else if (type == "rnd-plt-ctx-stream") + + // types of plaintext (also only sources) + else if (type == "rnd_plt_ctx_stream") return std::make_unique(config.at("source"), seeder, osize); - else if (type == "rho-stream") + else if (type == "rho_stream") return std::make_unique(config.at("source"), seeder, osize); else if (type == "counter") return std::make_unique(osize); - else if (type == "random-start-counter") + else if (type == "random_start_counter") return std::make_unique(seeder, osize); - else if (type == "xor-stream") - return std::make_unique(config, seeder, osize); else if (type == "sac") return std::make_unique(seeder, osize); - else if (type == "sac-fixed-position") { + else if (type == "sac_fixed_position") { const std::size_t pos = std::size_t(config.at("position")); return std::make_unique(seeder, osize, pos); - } - else if (type == "sac-2d-all-positions") + } else if (type == "sac_2d_all_positions") return std::make_unique(seeder, osize); - else if (type == "hw-counter") + else if (type == "hw_counter") return std::make_unique(config, seeder, osize); - else if (type == "column") - return std::make_unique(config, seeder, osize); - else if (type == "column-fixed-position") { - const std::size_t pos = std::size_t(config.at("position")); - return std::make_unique(config, seeder, osize, pos); - } - else if (type == "bernoulli-distribution") + + // sources with statistical distribution + else if (type == "bernoulli_distribution") return std::make_unique(config, seeder, osize); - else if (type == "binomial-distribution") + else if (type == "binomial_distribution") return std::make_unique(config, seeder, osize); - else if (type == "normal-distribution") + else if (type == "normal_distribution") return std::make_unique(config, seeder, osize); - else if (type == "poisson-distribution") + else if (type == "poisson_distribution") return std::make_unique(config, seeder, osize); - else if (type == "exponential-distribution") + else if (type == "exponential_distribution") return std::make_unique(config, seeder, osize); + + // modifiers -- streams that has other stream as an input (but these are used as source before + // cipher) + else if (type == "single_value_stream") + return std::make_unique(config.at("source"), seeder, osize); + else if (type == "repeating_stream") + return std::make_unique(config.at("source"), seeder, osize); + + // postprocessing modifiers -- streams that has cipher stream as an input + else if (type == "xor_stream") + return std::make_unique(config, seeder, osize); + else if (type == "column") + return std::make_unique(config, seeder, osize); + else if (type == "column_fixed_position") { + const std::size_t pos = std::size_t(config.at("position")); + return std::make_unique(config, seeder, osize, pos); + } + + // cryptoprimitives #if (BUILD_testsuite && TEST_STREAM) - else if (type == "test-stream") + else if (type == "test_stream") return std::make_unique(config); #endif #ifdef BUILD_stream_ciphers - else if (type == "estream" or type == "stream-others") + else if (type == "stream_cipher" or type == "estream") return std::make_unique(config, seeder, osize, stream); #endif #ifdef BUILD_hash - else if (type == "other_hash" || type == "sha3") + else if (type == "hash" || type == "sha3") return std::make_unique(config, seeder, osize, stream); #endif #ifdef BUILD_block @@ -292,7 +303,7 @@ make_stream(const json& config, default_seed_source& seeder, const std::size_t o throw std::runtime_error("requested stream named \"" + type + "\" does not exist"); } -void stream_to_dataset(dataset& set, std::unique_ptr& source) { +void stream_to_dataset(dataset &set, std::unique_ptr &source) { auto beg = set.rawdata(); auto end = set.rawdata() + set.rawsize(); diff --git a/streams.h b/streams.h index f85c98f8..ef629561 100644 --- a/streams.h +++ b/streams.h @@ -1,14 +1,14 @@ #pragma once #include "stream.h" -#include #include -#include #include +#include +#include #include #ifdef BUILD_testsuite -#include +#include #endif #ifdef BUILD_stream_ciphers @@ -25,35 +25,31 @@ namespace _impl { - template - struct const_stream : stream { - const_stream(const std::size_t osize) - : stream(osize) { - std::fill_n(_data.begin(), osize, value); - } +template struct const_stream : stream { + const_stream(const std::size_t osize) + : stream(osize) { + std::fill_n(_data.begin(), osize, value); + } - vec_cview next() override { - return make_cview(_data); - } - }; + vec_cview next() override { return make_cview(_data); } +}; - template - struct rng_stream : stream { - template - rng_stream(Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) {} +template struct rng_stream : stream { + template + rng_stream(Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) {} - vec_cview next() override { - std::generate_n(_data.data(), osize(), [this]() { - return std::uniform_int_distribution()(_rng); - }); - return make_cview(_data); - } + vec_cview next() override { + std::generate_n(_data.data(), osize(), [this]() { + return std::uniform_int_distribution()(_rng); + }); + return make_cview(_data); + } - private: - Generator _rng; - }; +private: + Generator _rng; +}; } // namespace _impl @@ -64,16 +60,14 @@ struct dummy_stream : stream { dummy_stream(const std::size_t osize) : stream(osize) {} - vec_cview next() override { - return make_cview(_data); - } + vec_cview next() override { return make_cview(_data); } }; /** * @brief Stream of data read from a file */ struct file_stream : stream { - file_stream(const json& config, const std::size_t osize); + file_stream(const json &config, const std::size_t osize); vec_cview next() override; @@ -86,8 +80,10 @@ struct file_stream : stream { * @brief Stream outputing a constant value for n iterations */ struct repeating_stream : stream { - repeating_stream(const json& config, default_seed_source &seeder, const std::size_t osize); - repeating_stream(const std::size_t osize, std::unique_ptr source, const unsigned period); + repeating_stream(const json &config, default_seed_source &seeder, const std::size_t osize); + repeating_stream(const std::size_t osize, + std::unique_ptr source, + const unsigned period); vec_cview next() override; @@ -101,7 +97,7 @@ struct repeating_stream : stream { * @brief Stream outputing a constant value */ struct single_value_stream : stream { - single_value_stream(const json& config, default_seed_source &seeder, const std::size_t osize); + single_value_stream(const json &config, default_seed_source &seeder, const std::size_t osize); vec_cview next() override; }; @@ -127,9 +123,10 @@ struct random_start_counter : counter { */ struct xor_stream : stream { template - xor_stream(const json& config, Seeder&& seeder, const std::size_t osize); + xor_stream(const json &config, Seeder &&seeder, const std::size_t osize); vec_cview next() override; + private: std::unique_ptr _source; }; @@ -139,9 +136,10 @@ struct xor_stream : stream { */ struct rnd_plt_ctx_stream : stream { template - rnd_plt_ctx_stream(const json& config, Seeder&& seeder, const std::size_t osize); + rnd_plt_ctx_stream(const json &config, Seeder &&seeder, const std::size_t osize); vec_cview next() override; + private: std::unique_ptr _rng; std::unique_ptr _source; @@ -156,9 +154,10 @@ struct rnd_plt_ctx_stream : stream { */ struct rho_stream : stream { template - rho_stream(const json& config, Seeder &&seeder, const std::size_t osize); + rho_stream(const json &config, Seeder &&seeder, const std::size_t osize); vec_cview next() override; + private: std::unique_ptr _ptx; std::unique_ptr _source; @@ -174,10 +173,10 @@ struct rho_stream : stream { */ struct sac_stream : stream { template - sac_stream(Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _first(true) { } + sac_stream(Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _first(true) {} vec_cview next() override { if (_first) { @@ -201,15 +200,16 @@ struct sac_stream : stream { struct sac_fixed_pos_stream : stream { template - sac_fixed_pos_stream(Seeder&& seeder, const std::size_t osize, const std::size_t flip_bit_position) - : stream(osize) - , _rng(std::forward(seeder)) - , _flip_bit_position(flip_bit_position) - , _first(true) { - if (_flip_bit_position >= osize*8) + sac_fixed_pos_stream(Seeder &&seeder, + const std::size_t osize, + const std::size_t flip_bit_position) + : stream(osize) + , _rng(std::forward(seeder)) + , _flip_bit_position(flip_bit_position) + , _first(true) { + if (_flip_bit_position >= osize * 8) throw std::runtime_error( - "Position of the flipped bit has to be in range of vector size."); - + "Position of the flipped bit has to be in range of vector size."); } vec_cview next() override { @@ -232,12 +232,11 @@ struct sac_fixed_pos_stream : stream { struct sac_2d_all_pos : stream { template - sac_2d_all_pos(Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _origin_data(osize) - , _flip_bit_position(0) - {} + sac_2d_all_pos(Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _origin_data(osize) + , _flip_bit_position(0) {} vec_cview next() override { if (_flip_bit_position == 0) { @@ -264,22 +263,22 @@ struct sac_2d_all_pos : stream { }; struct hw_counter : stream { - template - hw_counter(const json& config, Seeder &&seeder, const std::size_t osize) + template + hw_counter(const json &config, Seeder &&seeder, const std::size_t osize) : stream(osize) , _rng(std::forward(seeder)) , _origin_data(osize) , _increase_hw(config.value("increase_hw", true)) , _randomize_overflow(config.value("randomize_overflow", false)) - , _cur_hw(static_cast(config.value("hw", 1))) - { + , _cur_hw(static_cast(config.value("hw", 1))) { bool randomize_start = config.value("randomize_start", false); - if (_cur_hw == 0 || _cur_hw > osize * 8){ + if (_cur_hw == 0 || _cur_hw > osize * 8) { throw std::runtime_error("Invalid Hamming weight for the given output size"); } - if (_randomize_overflow && _increase_hw){ - throw std::runtime_error("Randomize overflow and increase counter are mutually exclusive"); + if (_randomize_overflow && _increase_hw) { + throw std::runtime_error( + "Randomize overflow and increase counter are mutually exclusive"); } if (randomize_start) { @@ -293,9 +292,8 @@ struct hw_counter : stream { vec_cview next() override; - private: - void randomize(){ + void randomize() { std::generate_n(_origin_data.data(), osize(), [this]() { return std::uniform_int_distribution()(_rng); }); @@ -312,7 +310,7 @@ struct hw_counter : stream { const auto size = static_cast(_cur_positions.size()); auto idx = size - 1; - if (_cur_positions[idx] == osize()*8 - 1) { + if (_cur_positions[idx] == osize() * 8 - 1) { do { idx -= 1; } while (idx >= 0 && _cur_positions[idx] + 1 == _cur_positions[idx + 1]); @@ -338,20 +336,23 @@ struct hw_counter : stream { }; struct column_stream : stream { - column_stream(const json& config, default_seed_source& seeder, const std::size_t osize); + column_stream(const json &config, default_seed_source &seeder, const std::size_t osize); vec_cview next() override; private: std::size_t _internal_bit_size; - std::vector> _buf; // change to array (maxe osize() constexpression), or init it to given size + std::vector> + _buf; // change to array (maxe osize() constexpression), or init it to given size std::size_t _position; std::unique_ptr _source; }; - struct column_fixed_position_stream : stream { - column_fixed_position_stream(const json& config, default_seed_source& seeder, const std::size_t osize, const std::size_t position); + column_fixed_position_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + const std::size_t position); vec_cview next() override; @@ -365,10 +366,10 @@ struct column_fixed_position_stream : stream { */ struct bernoulli_distribution_stream : stream { template - bernoulli_distribution_stream(const json& config, Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _distribution(std::bernoulli_distribution(double(config.value("p", 0.5)))) { } + bernoulli_distribution_stream(const json &config, Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _distribution(std::bernoulli_distribution(double(config.value("p", 0.5)))) {} vec_cview next() override { std::generate_n(_data.data(), osize(), [this]() { @@ -391,14 +392,14 @@ struct bernoulli_distribution_stream : stream { */ struct binomial_distribution_stream : stream { template - binomial_distribution_stream(const json& config, Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _distribution(uint8_t(config.value("max-value", std::numeric_limits::max())), - double(config.value("p", 0.5))) { } + binomial_distribution_stream(const json &config, Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _distribution(uint8_t(config.value("max_value", std::numeric_limits::max())), + double(config.value("p", 0.5))) {} vec_cview next() override { - std::generate_n(_data.data(), osize(), [this]() {return _distribution(_rng);}); + std::generate_n(_data.data(), osize(), [this]() { return _distribution(_rng); }); return make_cview(_data); } @@ -414,11 +415,10 @@ struct binomial_distribution_stream : stream { */ struct normal_distribution_stream : stream { template - normal_distribution_stream(const json& config, Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _distribution(double(config.value("mean", 0)), - double(config.value("std-dev", 1.0))) { } + normal_distribution_stream(const json &config, Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _distribution(double(config.value("mean", 0)), double(config.value("std_dev", 1.0))) {} vec_cview next() override { std::generate_n(_data.data(), osize(), [this]() { @@ -426,9 +426,10 @@ struct normal_distribution_stream : stream { double sigma_count = 4.0; double std_dev = _distribution.stddev(); do { - res = _distribution(_rng); + res = _distribution(_rng); } while (res < -sigma_count * std_dev or res > sigma_count * std_dev); - double normalized_value = res / (2 * sigma_count * std_dev) + 0.5; // normalized to [0, 1]; + double normalized_value = + res / (2 * sigma_count * std_dev) + 0.5; // normalized to [0, 1]; return uint8_t(normalized_value * std::numeric_limits::max()); }); return make_cview(_data); @@ -444,13 +445,13 @@ struct normal_distribution_stream : stream { */ struct poisson_distribution_stream : stream { template - poisson_distribution_stream(const json& config, Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _distribution(double(config.value("mean", std::numeric_limits::max() / 2))) { } + poisson_distribution_stream(const json &config, Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _distribution(double(config.value("mean", std::numeric_limits::max() / 2))) {} vec_cview next() override { - std::generate_n(_data.data(), osize(), [this]() {return _distribution(_rng);}); + std::generate_n(_data.data(), osize(), [this]() { return _distribution(_rng); }); return make_cview(_data); } @@ -464,15 +465,13 @@ struct poisson_distribution_stream : stream { */ struct exponential_distribution_stream : stream { template - exponential_distribution_stream(const json& config, Seeder&& seeder, const std::size_t osize) - : stream(osize) - , _rng(std::forward(seeder)) - , _distribution(double(config.value("lambda", 1))) { } + exponential_distribution_stream(const json &config, Seeder &&seeder, const std::size_t osize) + : stream(osize) + , _rng(std::forward(seeder)) + , _distribution(double(config.value("lambda", 1))) {} vec_cview next() override { - std::generate_n(_data.data(), osize(), [this]() { - return uint8_t(_distribution(_rng)); - }); + std::generate_n(_data.data(), osize(), [this]() { return uint8_t(_distribution(_rng)); }); return make_cview(_data); } @@ -501,6 +500,8 @@ using mt19937_stream = _impl::rng_stream; */ using pcg32_stream = _impl::rng_stream; -std::unique_ptr make_stream(const json& config, default_seed_source& seeder, std::size_t osize, +std::unique_ptr make_stream(const json &config, + default_seed_source &seeder, + std::size_t osize, core::optional stream = core::nullopt_t{}); -void stream_to_dataset(dataset &set, std::unique_ptr& source); +void stream_to_dataset(dataset &set, std::unique_ptr &source); diff --git a/streams/block/block_cipher.h b/streams/block/block_cipher.h index 19749894..2ab09f98 100644 --- a/streams/block/block_cipher.h +++ b/streams/block/block_cipher.h @@ -1,36 +1,36 @@ #pragma once -#include #include +#include namespace block { - struct block_cipher { - block_cipher(std::size_t rounds) - : _rounds(rounds) { } +struct block_cipher { + block_cipher(std::size_t rounds) + : _rounds(rounds) {} - virtual ~block_cipher() = default; + virtual ~block_cipher() = default; - /** - * Key setup. It is the user's responsibility to select the values of - * keysize and ivsize from the set of supported values specified - * above. - */ - virtual void keysetup(const std::uint8_t* key, const std::uint64_t keysize) = 0; + /** + * Key setup. It is the user's responsibility to select the values of + * keysize and ivsize from the set of supported values specified + * above. + */ + virtual void keysetup(const std::uint8_t *key, const std::uint64_t keysize) = 0; - /** - * IV setup. After having called ECRYPT_keysetup(), the user is - * allowed to call ECRYPT_ivsetup() different times in order to - * encrypt/decrypt different messages with the same key but different - * IV's. - */ - virtual void ivsetup(const std::uint8_t* iv, const std::uint64_t ivsize) = 0; + /** + * IV setup. After having called ECRYPT_keysetup(), the user is + * allowed to call ECRYPT_ivsetup() different times in order to + * encrypt/decrypt different messages with the same key but different + * IV's. + */ + virtual void ivsetup(const std::uint8_t *iv, const std::uint64_t ivsize) = 0; - virtual void encrypt(const std::uint8_t* plaintext, std::uint8_t* ciphertext) = 0; - virtual void decrypt(const std::uint8_t* ciphertext, std::uint8_t* plaintext) = 0; + virtual void encrypt(const std::uint8_t *plaintext, std::uint8_t *ciphertext) = 0; + virtual void decrypt(const std::uint8_t *ciphertext, std::uint8_t *plaintext) = 0; - protected: - std::size_t _rounds; - }; +protected: + std::size_t _rounds; +}; -} +} // namespace block diff --git a/streams/block/block_factory.cpp b/streams/block/block_factory.cpp index e130c79b..29676ae7 100644 --- a/streams/block/block_factory.cpp +++ b/streams/block/block_factory.cpp @@ -4,7 +4,7 @@ namespace block { struct block_cipher; -std::unique_ptr make_block_cipher(const std::string& name, +std::unique_ptr make_block_cipher(const std::string &name, const std::size_t round, const std::size_t block_size, const std::size_t key_size, @@ -39,4 +39,4 @@ std::unique_ptr make_block_cipher(const std::string& name, "\" is either broken or does not exists"); } -} //namespace block +} // namespace block diff --git a/streams/block/block_factory.h b/streams/block/block_factory.h index fd9e5885..4632ef6e 100644 --- a/streams/block/block_factory.h +++ b/streams/block/block_factory.h @@ -1,35 +1,38 @@ #pragma once -#include -#include #include "block_cipher.h" - #include +#include +#include -#include "ciphers/tea/tea.h" #include "ciphers/aes/aes.h" #include "ciphers/aria/aria_block.h" +#include "ciphers/blowfish/blowfish_factory.h" #include "ciphers/camellia/camellia_block.h" -#include "ciphers/simon/simon.h" -#include "ciphers/speck/speck.h" +#include "ciphers/cast/cast_block.h" #include "ciphers/des/single_des.h" #include "ciphers/des/triple_des.h" -#include "ciphers/blowfish/blowfish_factory.h" +#include "ciphers/gost/gost_block.h" +#include "ciphers/idea/idea_block.h" +#include "ciphers/kasumi/kasumi_factory.h" +#include "ciphers/kuznyechik/kuznyechik_factory.h" #include "ciphers/mars/mars.h" -#include "ciphers/serpent/serpent.h" -#include "ciphers/twofish/twofish.h" +#include "ciphers/misty1/misty1_factory.h" +#include "ciphers/noekeon/noekeon_factory.h" #include "ciphers/rc6/rc6.h" -#include "ciphers/gost/gost_block.h" #include "ciphers/seed/seed_block.h" -#include "ciphers/idea/idea_block.h" -#include "ciphers/cast/cast_block.h" -#include "streams/block/ciphers/kasumi/kasumi_factory.h" -#include "streams/block/ciphers/kuznyechik/kuznyechik_factory.h" -#include "streams/block/ciphers/misty1/misty1_factory.h" -#include "streams/block/ciphers/noekeon/noekeon_factory.h" -#include "streams/block/ciphers/shacal2/shacal2_factory.h" -#include "streams/block/ciphers/xtea/xtea_factory.h" +#include "ciphers/serpent/serpent.h" +#include "ciphers/shacal2/shacal2_factory.h" +#include "ciphers/simon/simon.h" +#include "ciphers/speck/speck.h" +#include "ciphers/tea/tea.h" +#include "ciphers/twofish/twofish.h" +#include "ciphers/xtea/xtea_factory.h" namespace block { - std::unique_ptr make_block_cipher(const std::string& name, const std::size_t round, const std::size_t block_size, const std::size_t key_size, const bool encrypt); +std::unique_ptr make_block_cipher(const std::string &name, + const std::size_t round, + const std::size_t block_size, + const std::size_t key_size, + const bool encrypt); } diff --git a/streams/block/block_stream.cc b/streams/block/block_stream.cc index d163ac95..62a593e9 100644 --- a/streams/block/block_stream.cc +++ b/streams/block/block_stream.cc @@ -1,91 +1,97 @@ #include "block_stream.h" -#include "streams.h" -#include "block_factory.h" #include "block_cipher.h" +#include "block_factory.h" +#include "streams.h" #include namespace block { - static int64_t reinit_freq(const json& config) { - try { - std::string init_freq = config.at("init-frequency"); - if (init_freq == "only-once") { - return -1; - } else { - int64_t init_freq_int = std::stoll(init_freq); - if (init_freq_int < 1) { - throw std::runtime_error("Reinitialization frequency has to be higher or equal to 1."); - } - return init_freq_int; - } - } catch (std::out_of_range& e) { - // this field is voluntary, we return presumed value "only-once" +static int64_t reinit_freq(const json &config) { + try { + std::string init_freq = config.at("init_frequency"); + if (init_freq == "only_once") { return -1; + } else { + int64_t init_freq_int = std::stoll(init_freq); + if (init_freq_int < 1) { + throw std::runtime_error( + "Reinitialization frequency has to be higher or equal to 1."); + } + return init_freq_int; } + } catch (std::out_of_range &e) { + // this field is voluntary, we return presumed value "only_once" + return -1; } +} + +block_stream::block_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream) + : stream(osize) + , _round(config.at("round")) + , _block_size(config.at("block_size")) + , _reinit_freq(reinit_freq(config)) + , _i(0) + , _source(make_stream(config.at("plaintext"), seeder, osize)) + , _iv(make_stream(config.at("iv"), seeder, _block_size)) + , _key(make_stream(config.at("key"), seeder, unsigned(config.at("key_size")))) + , _encryptor(make_block_cipher(config.at("algorithm"), + unsigned(_round), + unsigned(_block_size), + unsigned(config.at("key_size")), + true)) + , _prepared_stream_source(!plt_stream ? nullptr : *plt_stream) { + logger::info() << "stream source is block cipher: " << config.at("algorithm") << std::endl; - block_stream::block_stream(const json& config, default_seed_source& seeder, const std::size_t osize, core::optional plt_stream) - : stream(osize) - , _round(config.at("round")) - , _block_size(config.at("block-size")) - , _reinit_freq(reinit_freq(config)) - , _i(0) - , _source(make_stream(config.at("plaintext"), seeder, osize)) - , _iv(make_stream(config.at("iv"), seeder, _block_size)) - , _key(make_stream(config.at("key"), seeder, unsigned(config.at("key-size")))) - , _encryptor(make_block_cipher(config.at("algorithm"), unsigned(_round), - unsigned(_block_size), unsigned(config.at("key-size")), true)) - , _prepared_stream_source(!plt_stream ? nullptr : *plt_stream) - { - logger::info() << "stream source is block cipher: " << config.at("algorithm") << std::endl; + if (int(config.at("round")) < 0) + throw std::runtime_error("The least number of rounds is 0."); + if (_block_size < 4) + throw std::runtime_error("The block size is at least 4 bytes"); + if (osize == 0) + throw std::runtime_error("The output size has to be at least 1 byte"); + if (osize % _block_size != 0) // not necessary wrong, but we never needed this, we always did + // this by mistake. Change to warning if needed + throw std::runtime_error("Output size is not multiple of block size"); - if (int(config.at("round")) < 0) - throw std::runtime_error("The least number of rounds is 0."); - if (_block_size < 4) - throw std::runtime_error("The block size is at least 4 bytes"); - if (osize == 0) - throw std::runtime_error("The output size has to be at least 1 byte"); - if (osize % _block_size != 0) // not necessary wrong, but we never needed this, we always did this by mistake. Change to warning if needed - throw std::runtime_error("Output size is not multiple of block size"); + /* others modes than ECB are not implemented yet + vec_view iv_view = _iv->next(); + _encryptor->ivsetup(iv_view.data(), iv_view.size()); + */ + + vec_cview key_view = _key->next(); + _encryptor->keysetup(key_view.data(), std::uint32_t(key_view.size())); +} - /* others modes than ECB are not implemented yet - vec_view iv_view = _iv->next(); - _encryptor->ivsetup(iv_view.data(), iv_view.size()); - */ +block_stream::block_stream(block_stream &&) = default; +block_stream::~block_stream() = default; +vec_cview block_stream::next() { + ++_i; + if (_reinit_freq != -1 && _i % std::size_t(_reinit_freq) == 0) { vec_cview key_view = _key->next(); _encryptor->keysetup(key_view.data(), std::uint32_t(key_view.size())); } - block_stream::block_stream(block_stream&&) = default; - block_stream::~block_stream() = default; - - vec_cview block_stream::next() { - ++_i; - if (_reinit_freq != -1 && _i % std::size_t(_reinit_freq) == 0) { - vec_cview key_view = _key->next(); - _encryptor->keysetup(key_view.data(), std::uint32_t(key_view.size())); - } - - for (auto ctx_beg = _data.begin(); ctx_beg != _data.end(); ) { // ctx_beg += _source->osize() from inside - vec_cview view = get_next_ptx(); - for (auto ptx_beg = view.begin(); - ptx_beg != view.end() and ctx_beg != _data.end(); - ptx_beg += _block_size, ctx_beg += _block_size) { - _encryptor->encrypt(&(*ptx_beg), &(*ctx_beg)); - } + for (auto ctx_beg = _data.begin(); + ctx_beg != _data.end();) { // ctx_beg += _source->osize() from inside + vec_cview view = get_next_ptx(); + for (auto ptx_beg = view.begin(); ptx_beg != view.end() and ctx_beg != _data.end(); + ptx_beg += _block_size, ctx_beg += _block_size) { + _encryptor->encrypt(&(*ptx_beg), &(*ctx_beg)); } - - return make_view(_data.cbegin(), osize()); } - vec_cview block_stream::get_next_ptx() - { - if (_prepared_stream_source) { - return _prepared_stream_source->next(); - } else { - return _source->next(); - } - } + return make_view(_data.cbegin(), osize()); +} +vec_cview block_stream::get_next_ptx() { + if (_prepared_stream_source) { + return _prepared_stream_source->next(); + } else { + return _source->next(); + } } + +} // namespace block diff --git a/streams/block/block_stream.h b/streams/block/block_stream.h index bd07271a..4391f28c 100644 --- a/streams/block/block_stream.h +++ b/streams/block/block_stream.h @@ -1,38 +1,41 @@ #pragma once #include "stream.h" -#include #include -#include #include #include +#include +#include namespace block { - struct block_cipher; - struct block_stream : public stream - { - public: - block_stream(const json& config, default_seed_source &seeder, const std::size_t osize, core::optional plt_stream); - block_stream(block_stream&&); - ~block_stream() override; +struct block_cipher; + +struct block_stream : public stream { +public: + block_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream); + block_stream(block_stream &&); + ~block_stream() override; - vec_cview next() override; + vec_cview next() override; - private: - vec_cview get_next_ptx(); +private: + vec_cview get_next_ptx(); - const std::size_t _round; - const std::size_t _block_size; - const int64_t _reinit_freq; - std::size_t _i; + const std::size_t _round; + const std::size_t _block_size; + const int64_t _reinit_freq; + std::size_t _i; - std::unique_ptr _source; - std::unique_ptr _iv; - std::unique_ptr _key; + std::unique_ptr _source; + std::unique_ptr _iv; + std::unique_ptr _key; - std::unique_ptr _encryptor; - stream *_prepared_stream_source; - }; + std::unique_ptr _encryptor; + stream *_prepared_stream_source; +}; -} +} // namespace block diff --git a/streams/block/ciphers/common_fun.h b/streams/block/ciphers/common_fun.h index bf87e945..03ddd7e1 100644 --- a/streams/block/ciphers/common_fun.h +++ b/streams/block/ciphers/common_fun.h @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include #if defined(__APPLE__) || defined(_WIN32) @@ -14,35 +14,35 @@ namespace block { - static std::uint32_t u8_to_u32_copy(const uint8_t* in) { +static std::uint32_t u8_to_u32_copy(const uint8_t *in) { #if __BYTE_ORDER == __BIG_ENDIAN - return std::uint32_t((in[0] << 24) + (in[1] << 16) + (in[2] << 8) + in[3]); + return std::uint32_t((in[0] << 24) + (in[1] << 16) + (in[2] << 8) + in[3]); #elif __BYTE_ORDER == __LITTLE_ENDIAN - return std::uint32_t((in[3] << 24) + (in[2] << 16) + (in[1] << 8) + in[0]); + return std::uint32_t((in[3] << 24) + (in[2] << 16) + (in[1] << 8) + in[0]); #endif - } +} - static void u32_to_u8_copy(uint8_t* out, const uint32_t in) { +static void u32_to_u8_copy(uint8_t *out, const uint32_t in) { #if __BYTE_ORDER == __BIG_ENDIAN - out[0] = uint8_t((in >> 24) & 0xFF); - out[1] = uint8_t((in >> 16) & 0xFF); - out[2] = uint8_t((in >> 8) & 0xFF); - out[3] = uint8_t(in & 0xFF); + out[0] = uint8_t((in >> 24) & 0xFF); + out[1] = uint8_t((in >> 16) & 0xFF); + out[2] = uint8_t((in >> 8) & 0xFF); + out[3] = uint8_t(in & 0xFF); #elif __BYTE_ORDER == __LITTLE_ENDIAN - out[3] = uint8_t((in >> 24) & 0xFF); - out[2] = uint8_t((in >> 16) & 0xFF); - out[1] = uint8_t((in >> 8) & 0xFF); - out[0] = uint8_t(in & 0xFF); + out[3] = uint8_t((in >> 24) & 0xFF); + out[2] = uint8_t((in >> 16) & 0xFF); + out[1] = uint8_t((in >> 8) & 0xFF); + out[0] = uint8_t(in & 0xFF); #endif - } +} - constexpr char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7', - '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; +constexpr char hexmap[] = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; - static void bin_to_hex(const std::uint8_t *in, const std::size_t len, std::vector &out) { - for (std::size_t i = 0; i < len; ++i) { - out[2 * i] = hexmap[(in[i] & 0xF0) >> 4]; - out[2 * i + 1] = hexmap[in[i] & 0x0F]; - } +static void bin_to_hex(const std::uint8_t *in, const std::size_t len, std::vector &out) { + for (std::size_t i = 0; i < len; ++i) { + out[2 * i] = hexmap[(in[i] & 0xF0) >> 4]; + out[2 * i + 1] = hexmap[in[i] & 0x0F]; } +} } // namespace block diff --git a/streams/block/ciphers/kasumi/kasumi.cpp b/streams/block/ciphers/kasumi/kasumi.cpp index 28898525..2705a1a2 100644 --- a/streams/block/ciphers/kasumi/kasumi.cpp +++ b/streams/block/ciphers/kasumi/kasumi.cpp @@ -208,12 +208,12 @@ R = FI(R ^ K[4], K[5]) ^ L; L = FI(L ^ K[6], K[7]) ^ R; - if (j +1 == rounds) - break; - R = B2 ^= R; L = B3 ^= L; + if (j + 1 == rounds) + break; + R = FI(R ^ K[10], K[11]) ^ L; L = FI(L ^ K[12], K[13]) ^ R; R = FI(R ^ K[14], K[15]) ^ L; @@ -257,12 +257,12 @@ L ^= (rotl<1>(R) & K[8]); R ^= (rotl<1>(L) | K[9]); - if (j + 1 == rounds) - break; - R = B0 ^= R; L = B1 ^= L; + if (j + 1 == rounds) + break; + L ^= (rotl<1>(R) & K[0]); R ^= (rotl<1>(L) | K[1]); diff --git a/streams/hash/hash_factory.cc b/streams/hash/hash_factory.cc index 601f9286..43ff1403 100644 --- a/streams/hash/hash_factory.cc +++ b/streams/hash/hash_factory.cc @@ -1,17 +1,18 @@ #include "hash_factory.h" +#include "hash_interface.h" #include "others/hash_functions/hash_functions.h" #include "sha3/hash_functions/hash_functions.h" -#include "hash_interface.h" namespace hash { -void _check_rounds(const std::string& algorithm, const unsigned rounds) { +void _check_rounds(const std::string &algorithm, const unsigned rounds) { if (rounds > 0) throw std::runtime_error{"requested hash algorithm named \"" + algorithm + "\" cannot be limited in rounds"}; } -std::unique_ptr hash_factory::create(const std::string& name, const unsigned rounds) { +std::unique_ptr hash_factory::create(const std::string &name, + const unsigned rounds) { // clang-format off if (name == "Abacus") return std::make_unique(rounds); if (name == "ARIRANG") return std::make_unique(rounds); diff --git a/streams/hash/hash_factory.h b/streams/hash/hash_factory.h index f12af567..01388eb0 100644 --- a/streams/hash/hash_factory.h +++ b/streams/hash/hash_factory.h @@ -5,12 +5,13 @@ namespace hash { - struct hash_interface; +struct hash_interface; - void _check_rounds(const std::string& algorithm, const unsigned rounds); +void _check_rounds(const std::string &algorithm, const unsigned rounds); - struct hash_factory { - static std::unique_ptr create(const std::string& algorithm, const unsigned rounds); - }; +struct hash_factory { + static std::unique_ptr create(const std::string &algorithm, + const unsigned rounds); +}; } // namespace hash diff --git a/streams/hash/hash_interface.h b/streams/hash/hash_interface.h index 2ca01fd3..8857cb73 100644 --- a/streams/hash/hash_interface.h +++ b/streams/hash/hash_interface.h @@ -11,11 +11,11 @@ struct hash_interface { virtual ~hash_interface() = default; virtual int Init(int hash_bitsize) = 0; - virtual int Update(const BitSequence* data, DataLength data_bitsize) = 0; - virtual int Final(BitSequence* hash) = 0; + virtual int Update(const BitSequence *data, DataLength data_bitsize) = 0; + virtual int Final(BitSequence *hash) = 0; virtual int - Hash(int hash_bitsize, const BitSequence* data, DataLength data_bitsize, BitSequence* hash) = 0; + Hash(int hash_bitsize, const BitSequence *data, DataLength data_bitsize, BitSequence *hash) = 0; }; } // namespace hash diff --git a/streams/hash/hash_stream.cc b/streams/hash/hash_stream.cc index 60a6a3ed..d03953d2 100644 --- a/streams/hash/hash_stream.cc +++ b/streams/hash/hash_stream.cc @@ -1,13 +1,16 @@ #include "hash_stream.h" -#include "streams.h" -#include "hash_interface.h" #include "hash_factory.h" +#include "hash_interface.h" +#include "streams.h" #include namespace hash { template -void hash_data(hash_interface& hasher, const I& data, std::uint8_t* hash, const std::size_t hash_size) { +void hash_data(hash_interface &hasher, + const I &data, + std::uint8_t *hash, + const std::size_t hash_size) { using std::to_string; int status = hasher.Init(int(hash_size * 8)); @@ -23,20 +26,26 @@ void hash_data(hash_interface& hasher, const I& data, std::uint8_t* hash, const throw std::runtime_error("cannot finalize the hash (code: " + to_string(status) + ")"); } -hash_stream::hash_stream(const json& config, default_seed_source &seeder, const std::size_t osize, core::optional plt_stream) +hash_stream::hash_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream) : stream(osize) // round osize to multiple of _hash_input_size , _round(config.at("round")) - , _hash_size(std::size_t(config.at("hash-size"))) - , _source(make_stream(config.at("source"), seeder, config.value("input-size", _hash_size))) // if input size is not defined, use hash-size + , _hash_size(std::size_t(config.at("hash_size"))) + , _source(make_stream( + config.at("source"), + seeder, + config.value("input_size", _hash_size))) // if input size is not defined, use hash-size , _prepared_stream_source(!plt_stream ? nullptr : *plt_stream) , _hasher(hash_factory::create(config.at("algorithm"), unsigned(_round))) { - if (osize % _hash_size != 0) // not necessary wrong, but we never needed this, we always did this by mistake. Change to warning if needed + if (osize % _hash_size != 0) // not necessary wrong, but we never needed this, we always did + // this by mistake. Change to warning if needed throw std::runtime_error("Output size is not multiple of hash size"); logger::info() << "stream source is hash function: " << config.at("algorithm") << std::endl; - } -hash_stream::hash_stream(hash_stream&&) = default; +hash_stream::hash_stream(hash_stream &&) = default; hash_stream::~hash_stream() = default; vec_cview hash_stream::next() { @@ -50,8 +59,7 @@ vec_cview hash_stream::next() { return make_view(_data.cbegin(), osize()); } -vec_cview hash_stream::get_next_ptx() -{ +vec_cview hash_stream::get_next_ptx() { if (_prepared_stream_source) { return _prepared_stream_source->next(); } else { diff --git a/streams/hash/hash_stream.h b/streams/hash/hash_stream.h index cb6cbd6e..012b1fd9 100644 --- a/streams/hash/hash_stream.h +++ b/streams/hash/hash_stream.h @@ -1,21 +1,27 @@ #pragma once #include "stream.h" -#include #include -#include #include +#include +#include namespace hash { struct hash_interface; -template void hash_data(hash_interface& hasher, const I& data, std::uint8_t* hash, const std::size_t hash_size); - +template +void hash_data(hash_interface &hasher, + const I &data, + std::uint8_t *hash, + const std::size_t hash_size); struct hash_stream : stream { - hash_stream(const json& config, default_seed_source& seeder, const std::size_t osize, core::optional plt_stream); - hash_stream(hash_stream&&); + hash_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream); + hash_stream(hash_stream &&); ~hash_stream() override; vec_cview next() override; diff --git a/streams/hash/others/hash_functions/hash_functions.h b/streams/hash/others/hash_functions/hash_functions.h index 18da2779..bad8f8fb 100644 --- a/streams/hash/others/hash_functions/hash_functions.h +++ b/streams/hash/others/hash_functions/hash_functions.h @@ -1,7 +1,7 @@ -#include "sha1/sha1_factory.h" -#include "sha2/sha256_factory.h" -#include "md5/md5_factory.h" #include "gost/gost_factory.h" +#include "md5/md5_factory.h" #include "ripemd160/ripemd160_factory.h" +#include "sha1/sha1_factory.h" +#include "sha2/sha256_factory.h" #include "tiger/tiger_factory.h" #include "whirlpool/whirlpool_factory.h" diff --git a/streams/hash/sha3/sha3_interface.h b/streams/hash/sha3/sha3_interface.h index 6a429417..811e902d 100644 --- a/streams/hash/sha3/sha3_interface.h +++ b/streams/hash/sha3/sha3_interface.h @@ -8,7 +8,6 @@ namespace sha3 { using BitSequence = hash::BitSequence; using DataLength = hash::DataLength; -struct sha3_interface : hash::hash_interface { -}; +struct sha3_interface : hash::hash_interface {}; } // namespace sha3 diff --git a/streams/stream_ciphers/stream_cipher.cc b/streams/stream_ciphers/stream_cipher.cc index d8358cbb..a58e75af 100644 --- a/streams/stream_ciphers/stream_cipher.cc +++ b/streams/stream_ciphers/stream_cipher.cc @@ -36,7 +36,8 @@ namespace stream_ciphers { -std::unique_ptr create_stream_cipher(const std::string& name, const unsigned round) { +std::unique_ptr create_stream_cipher(const std::string &name, + const unsigned round) { // clang-format off // eSTREAM if (name == "ABC") return std::make_unique(); @@ -72,11 +73,14 @@ std::unique_ptr create_stream_cipher(const std::string& name, if (name == "RC4") return std::make_unique(round); // clang-format on - throw std::runtime_error("requested eSTREAM cipher named \"" + name + - "\" is either broken or does not exists"); + throw std::runtime_error("requested eSTREAM cipher named \"" + name + + "\" is either broken or does not exists"); } -stream_cipher::stream_cipher(const std::string& name, const unsigned round, const std::size_t iv_size, const std::size_t key_size) +stream_cipher::stream_cipher(const std::string &name, + const unsigned round, + const std::size_t iv_size, + const std::size_t key_size) : _iv(iv_size) , _key(key_size) , _encryptor(create_stream_cipher(name, round)) @@ -85,31 +89,31 @@ stream_cipher::stream_cipher(const std::string& name, const unsigned round, cons _decryptor->init(); } -stream_cipher::stream_cipher(stream_cipher&&) = default; +stream_cipher::stream_cipher(stream_cipher &&) = default; stream_cipher::~stream_cipher() = default; -void stream_cipher::setup_key_iv(std::unique_ptr& key, std::unique_ptr& iv) { - vec_cview key_data = key->next(); - _key.assign(key_data.begin(), key_data.end()); +void stream_cipher::setup_key_iv(std::unique_ptr &key, std::unique_ptr &iv) { + vec_cview key_data = key->next(); + _key.assign(key_data.begin(), key_data.end()); - _encryptor->keysetup(_key.data(), u32(8 * key->osize()), u32(8 * iv->osize())); - _decryptor->keysetup(_key.data(), u32(8 * key->osize()), u32(8 * iv->osize())); + _encryptor->keysetup(_key.data(), u32(8 * key->osize()), u32(8 * iv->osize())); + _decryptor->keysetup(_key.data(), u32(8 * key->osize()), u32(8 * iv->osize())); - vec_cview iv_data = iv->next(); - _iv.assign(iv_data.begin(), iv_data.end()); + vec_cview iv_data = iv->next(); + _iv.assign(iv_data.begin(), iv_data.end()); - _encryptor->ivsetup(_iv.data()); - _decryptor->ivsetup(_iv.data()); + _encryptor->ivsetup(_iv.data()); + _decryptor->ivsetup(_iv.data()); } -void stream_cipher::encrypt(const u8* plaintext, u8* ciphertext, std::size_t size) { - // BEWARE: only able to proccess max 2GB of plaintext - _encryptor->encrypt_bytes(plaintext, ciphertext, u32(size)); +void stream_cipher::encrypt(const u8 *plaintext, u8 *ciphertext, std::size_t size) { + // BEWARE: only able to proccess max 2GB of plaintext + _encryptor->encrypt_bytes(plaintext, ciphertext, u32(size)); } -void stream_cipher::decrypt(const u8* ciphertext, u8* plaintext, std::size_t size) { - // BEWARE: only able to proccess max 2GB of plaintext - _decryptor->decrypt_bytes(ciphertext, plaintext, u32(size)); +void stream_cipher::decrypt(const u8 *ciphertext, u8 *plaintext, std::size_t size) { + // BEWARE: only able to proccess max 2GB of plaintext + _decryptor->decrypt_bytes(ciphertext, plaintext, u32(size)); } } // namespace stream_ciphers diff --git a/streams/stream_ciphers/stream_cipher.h b/streams/stream_ciphers/stream_cipher.h index 89ef771d..67d60bcc 100644 --- a/streams/stream_ciphers/stream_cipher.h +++ b/streams/stream_ciphers/stream_cipher.h @@ -1,9 +1,9 @@ #pragma once #include +#include #include #include -#include #include #include @@ -11,25 +11,29 @@ namespace stream_ciphers { -std::unique_ptr create_stream_cipher(const std::string& name, const unsigned round); +std::unique_ptr create_stream_cipher(const std::string &name, + const unsigned round); struct stream_cipher { - stream_cipher(const std::string& name, const unsigned round, const std::size_t iv_size, const std::size_t key_size); + stream_cipher(const std::string &name, + const unsigned round, + const std::size_t iv_size, + const std::size_t key_size); - stream_cipher(stream_cipher&&); - ~stream_cipher(); + stream_cipher(stream_cipher &&); + ~stream_cipher(); - void setup_key_iv(std::unique_ptr& key, std::unique_ptr &iv); + void setup_key_iv(std::unique_ptr &key, std::unique_ptr &iv); - void encrypt(const std::uint8_t* plaintext, std::uint8_t* ciphertext, const std::size_t size); - void decrypt(const std::uint8_t* ciphertext, std::uint8_t* plaintext, const std::size_t size); + void encrypt(const std::uint8_t *plaintext, std::uint8_t *ciphertext, const std::size_t size); + void decrypt(const std::uint8_t *ciphertext, std::uint8_t *plaintext, const std::size_t size); protected: - std::vector _iv; - std::vector _key; + std::vector _iv; + std::vector _key; - std::unique_ptr _encryptor; - std::unique_ptr _decryptor; + std::unique_ptr _encryptor; + std::unique_ptr _decryptor; }; } // namespace stream_ciphers diff --git a/streams/stream_ciphers/stream_interface.h b/streams/stream_ciphers/stream_interface.h index 08e2a0c0..79538f8b 100644 --- a/streams/stream_ciphers/stream_interface.h +++ b/streams/stream_ciphers/stream_interface.h @@ -15,17 +15,16 @@ struct stream_interface { virtual void init() = 0; - virtual void keysetup(const u8* key, const u32 keysize, const u32 ivsize) = 0; - virtual void ivsetup(const u8* iv) = 0; + virtual void keysetup(const u8 *key, const u32 keysize, const u32 ivsize) = 0; + virtual void ivsetup(const u8 *iv) = 0; - virtual void encrypt_bytes(const u8* plaintext, u8* ciphertext, const u32 msglen) = 0; - virtual void decrypt_bytes(const u8* ciphertext, u8* plaintext, const u32 msglen) = 0; + virtual void encrypt_bytes(const u8 *plaintext, u8 *ciphertext, const u32 msglen) = 0; + virtual void decrypt_bytes(const u8 *ciphertext, u8 *plaintext, const u32 msglen) = 0; protected: const int _rounds; }; - struct estream_interface : stream_interface { estream_interface() : stream_interface() {} @@ -33,32 +32,27 @@ struct estream_interface : stream_interface { estream_interface(int rounds) : stream_interface(rounds) {} - void init() override { - ECRYPT_init(); - } + void init() override { ECRYPT_init(); } - void keysetup(const u8* key, u32 keysize, u32 ivsize) override { + void keysetup(const u8 *key, u32 keysize, u32 ivsize) override { ECRYPT_keysetup(key, keysize, ivsize); } - void ivsetup(const u8* iv) override { - ECRYPT_ivsetup(iv); - } + void ivsetup(const u8 *iv) override { ECRYPT_ivsetup(iv); } - void encrypt_bytes(const u8* plaintext, u8* ciphertext, u32 msglen) override { + void encrypt_bytes(const u8 *plaintext, u8 *ciphertext, u32 msglen) override { ECRYPT_encrypt_bytes(plaintext, ciphertext, msglen); } - void decrypt_bytes(const u8* ciphertext, u8* plaintext, u32 msglen) override { + void decrypt_bytes(const u8 *ciphertext, u8 *plaintext, u32 msglen) override { ECRYPT_decrypt_bytes(ciphertext, plaintext, msglen); } virtual void ECRYPT_init() = 0; - virtual void ECRYPT_keysetup(const u8* key, u32 keysize, u32 ivsize) = 0; - virtual void ECRYPT_ivsetup(const u8* iv) = 0; + virtual void ECRYPT_keysetup(const u8 *key, u32 keysize, u32 ivsize) = 0; + virtual void ECRYPT_ivsetup(const u8 *iv) = 0; - virtual void ECRYPT_encrypt_bytes(const u8* plaintext, u8* ciphertext, u32 msglen) = 0; - virtual void ECRYPT_decrypt_bytes(const u8* ciphertext, u8* plaintext, u32 msglen) = 0; + virtual void ECRYPT_encrypt_bytes(const u8 *plaintext, u8 *ciphertext, u32 msglen) = 0; + virtual void ECRYPT_decrypt_bytes(const u8 *ciphertext, u8 *plaintext, u32 msglen) = 0; }; - } // namespace stream_ciphers diff --git a/streams/stream_ciphers/stream_stream.cc b/streams/stream_ciphers/stream_stream.cc index 10daad67..5523ebe2 100644 --- a/streams/stream_ciphers/stream_stream.cc +++ b/streams/stream_ciphers/stream_stream.cc @@ -3,18 +3,26 @@ namespace stream_ciphers { -stream_stream::stream_stream(const json& config, default_seed_source& seeder, const std::size_t osize, core::optional plt_stream) +stream_stream::stream_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream) : stream(osize) - , _reinit(config.at("key").at("type") == "repeating-stream" || config.at("iv").at("type") == "repeating-stream") - , _block_size(config.at("block-size")) - , _iv_stream(make_stream(config.at("iv"), seeder, config.value("iv-size", default_iv_size))) - , _key_stream(make_stream(config.at("key"), seeder, config.value("key-size", default_key_size))) + , _reinit(config.at("key").at("type") == "repeating_stream" or + config.at("iv").at("type") == "repeating_stream") + , _block_size(config.at("block_size")) + , _iv_stream(make_stream(config.at("iv"), seeder, config.value("iv_size", default_iv_size))) + , _key_stream(make_stream(config.at("key"), seeder, config.value("key_size", default_key_size))) , _source(make_stream(config.at("plaintext"), seeder, _block_size)) , _prepared_stream_source(!plt_stream ? nullptr : *plt_stream) , _plaintext(osize) - , _algorithm(config.at("algorithm"), unsigned(config.at("round")), _iv_stream->osize(), _key_stream->osize()) { + , _algorithm(config.at("algorithm"), + unsigned(config.at("round")), + _iv_stream->osize(), + _key_stream->osize()) { - if (osize % _block_size != 0) // not necessary wrong, but we never needed this, we always did this by mistake. Change to warning if needed + if (osize % _block_size != 0) // not necessary wrong, but we never needed this, we always did + // this by mistake. Change to warning if needed throw std::runtime_error("Output size is not multiple of block size"); logger::info() << "stream source is estream cipher: " << config.at("algorithm") << std::endl; @@ -25,22 +33,21 @@ stream_stream::stream_stream(const json& config, default_seed_source& seeder, co } vec_cview stream_stream::next() { - if (_reinit) { - _algorithm.setup_key_iv(_key_stream, _iv_stream); - } - for (auto beg = _plaintext.begin(); beg != _plaintext.end(); beg += _block_size) { - vec_cview view = get_next_ptx(); + if (_reinit) { + _algorithm.setup_key_iv(_key_stream, _iv_stream); + } + for (auto beg = _plaintext.begin(); beg != _plaintext.end(); beg += _block_size) { + vec_cview view = get_next_ptx(); - std::move(view.begin(), view.end(), beg); - } + std::move(view.begin(), view.end(), beg); + } - _algorithm.encrypt(_plaintext.data(), _data.data(), _plaintext.size()); + _algorithm.encrypt(_plaintext.data(), _data.data(), _plaintext.size()); - return make_cview(_data); + return make_cview(_data); } -vec_cview stream_stream::get_next_ptx() -{ +vec_cview stream_stream::get_next_ptx() { if (_prepared_stream_source) { return _prepared_stream_source->next(); } else { diff --git a/streams/stream_ciphers/stream_stream.h b/streams/stream_ciphers/stream_stream.h index 737eb8e1..23a58a61 100644 --- a/streams/stream_ciphers/stream_stream.h +++ b/streams/stream_ciphers/stream_stream.h @@ -1,14 +1,17 @@ #pragma once -#include "stream_cipher.h" #include "stream.h" +#include "stream_cipher.h" #include #include namespace stream_ciphers { struct stream_stream : stream { - stream_stream(const json& config, default_seed_source& seeder, const std::size_t osize, core::optional plt_stream); + stream_stream(const json &config, + default_seed_source &seeder, + const std::size_t osize, + core::optional plt_stream); vec_cview next() override; diff --git a/testsuite/block_streams_tests.cc b/testsuite/block_streams_tests.cc index e680a907..41a52530 100644 --- a/testsuite/block_streams_tests.cc +++ b/testsuite/block_streams_tests.cc @@ -1,5 +1,5 @@ #include -#include +#include TEST(aes, test_vectors) { testsuite::block_test_case("AES", 10)(); @@ -112,4 +112,4 @@ TEST(shacal2, test_vectors) { TEST(xtea, test_vectors) { testsuite::block_test_case("XTEA", 32)(); -} \ No newline at end of file +} diff --git a/testsuite/estream_streams_tests.cc b/testsuite/estream_streams_tests.cc deleted file mode 100644 index e69de29b..00000000 diff --git a/testsuite/hash_streams_tests.cc b/testsuite/hash_streams_tests.cc index 2a406791..83011fec 100644 --- a/testsuite/hash_streams_tests.cc +++ b/testsuite/hash_streams_tests.cc @@ -1,128 +1,127 @@ -#include +#include "stream.h" +#include "streams.h" #include +#include +#include #include #include -#include -#include "stream.h" -#include "streams.h" -#include "testsuite/test-utils/hash_test_case.h" +#include "testsuite/test_utils/hash_test_case.h" /** Source of test vectors http://csrc.nist.gov/groups/ST/hash/sha-3/index.html */ - TEST(abacus, test_vectors) { - testsuite::sha3_test_case("Abacus", 135)(); + testsuite::hash_test_case("Abacus", 135)(); } TEST(arirang, test_vectors) { - testsuite::sha3_test_case("ARIRANG", 4)(); + testsuite::hash_test_case("ARIRANG", 4)(); } TEST(aurora, test_vectors) { - testsuite::sha3_test_case("AURORA", 17)(); + testsuite::hash_test_case("AURORA", 17)(); } TEST(blake, test_vectors) { - testsuite::sha3_test_case("BLAKE", 10)(); - testsuite::sha3_test_case("BLAKE", 14)(); + testsuite::hash_test_case("BLAKE", 10)(); + testsuite::hash_test_case("BLAKE", 14)(); } TEST(cheetah, test_vectors) { - testsuite::sha3_test_case("Cheetah", 16)(); + testsuite::hash_test_case("Cheetah", 16)(); } TEST(cube_hash, test_vectors) { - testsuite::sha3_test_case("CubeHash", 8)(); + testsuite::hash_test_case("CubeHash", 8)(); } TEST(dch, test_vectors) { - testsuite::sha3_test_case("DCH", 4)(); + testsuite::hash_test_case("DCH", 4)(); } TEST(dynamic_sha, test_vectors) { - testsuite::sha3_test_case("DynamicSHA", 16)(); + testsuite::hash_test_case("DynamicSHA", 16)(); } TEST(dynamic_sha2, test_vectors) { - testsuite::sha3_test_case("DynamicSHA2", 17)(); + testsuite::hash_test_case("DynamicSHA2", 17)(); } TEST(echo, test_vectors) { - testsuite::sha3_test_case("ECHO", 8)(); - testsuite::sha3_test_case("ECHO", 10)(); + testsuite::hash_test_case("ECHO", 8)(); + testsuite::hash_test_case("ECHO", 10)(); } TEST(grostl, test_vectors) { - testsuite::sha3_test_case("Grostl", 10)(); - testsuite::sha3_test_case("Grostl", 14)(); + testsuite::hash_test_case("Grostl", 10)(); + testsuite::hash_test_case("Grostl", 14)(); } TEST(hamsi, test_vectors) { - testsuite::sha3_test_case("Hamsi", 3)(); - testsuite::sha3_test_case("Hamsi", 6)(); + testsuite::hash_test_case("Hamsi", 3)(); + testsuite::hash_test_case("Hamsi", 6)(); } TEST(jh, test_vectors) { - testsuite::sha3_test_case("JH", 42)(); + testsuite::hash_test_case("JH", 42)(); } TEST(lesamnta, test_vectors) { - testsuite::sha3_test_case("Lesamnta", 32)(); + testsuite::hash_test_case("Lesamnta", 32)(); } TEST(luffa, test_vectors) { - testsuite::sha3_test_case("Luffa", 8)(); + testsuite::hash_test_case("Luffa", 8)(); } TEST(md6, test_vectors) { - testsuite::sha3_test_case("MD6", 96)(); - testsuite::sha3_test_case("MD6", 104)(); - testsuite::sha3_test_case("MD6", 105)(); + testsuite::hash_test_case("MD6", 96)(); + testsuite::hash_test_case("MD6", 104)(); + testsuite::hash_test_case("MD6", 105)(); } TEST(simd, test_vectors) { - testsuite::sha3_test_case("SIMD", 4)(); + testsuite::hash_test_case("SIMD", 4)(); } TEST(tangle, test_vectors) { - testsuite::sha3_test_case("Tangle", 72)(); - testsuite::sha3_test_case("Tangle", 80)(); - testsuite::sha3_test_case("Tangle", 96)(); - testsuite::sha3_test_case("Tangle", 112)(); - testsuite::sha3_test_case("Tangle", 128)(); - testsuite::sha3_test_case("Tangle", 144)(); + testsuite::hash_test_case("Tangle", 72)(); + testsuite::hash_test_case("Tangle", 80)(); + testsuite::hash_test_case("Tangle", 96)(); + testsuite::hash_test_case("Tangle", 112)(); + testsuite::hash_test_case("Tangle", 128)(); + testsuite::hash_test_case("Tangle", 144)(); } TEST(twister, test_vectors) { - testsuite::sha3_test_case("Twister", 9)(); - testsuite::sha3_test_case("Twister", 10)(); + testsuite::hash_test_case("Twister", 9)(); + testsuite::hash_test_case("Twister", 10)(); } TEST(sha1, test_vectors) { - testsuite::other_test_case("SHA1", 80)(); + testsuite::hash_test_case("SHA1", 80)(); } TEST(sha2, test_vectors) { - testsuite::other_test_case("SHA2", 64)(); + testsuite::hash_test_case("SHA2", 64)(); } TEST(md5, test_vectors) { - testsuite::other_test_case("MD5", 64)(); + testsuite::hash_test_case("MD5", 64)(); } TEST(gost, test_vectors) { - testsuite::other_test_case("Gost", 32)(); + testsuite::hash_test_case("Gost", 32)(); } TEST(ripemd160, test_vectors) { - testsuite::other_test_case("RIPEMD160", 80)(); + testsuite::hash_test_case("RIPEMD160", 80)(); } TEST(tiger, test_vectors) { - testsuite::other_test_case("Tiger", 24)(); + testsuite::hash_test_case("Tiger", 24)(); } TEST(whirlpool, test_vectors) { - testsuite::other_test_case("Whirlpool", 10)(); + testsuite::hash_test_case("Whirlpool", 10)(); } diff --git a/testsuite/stream_ciphers_streams_tests.cc b/testsuite/stream_ciphers_streams_tests.cc index 27e9d736..cbed0110 100644 --- a/testsuite/stream_ciphers_streams_tests.cc +++ b/testsuite/stream_ciphers_streams_tests.cc @@ -1,12 +1,12 @@ -#include -#include #include #include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include +#include TEST(chacha, test_vectors) { testsuite::stream_cipher_test_case("Chacha", 20)(); @@ -52,4 +52,3 @@ TEST(tsc4, test_vectors) { TEST(trivium, test_vectors) { testsuite::stream_cipher_test_case("Trivium", 9)(); } - diff --git a/testsuite/stream_tests.cc b/testsuite/stream_tests.cc index 82acd1d3..6574b970 100644 --- a/testsuite/stream_tests.cc +++ b/testsuite/stream_tests.cc @@ -2,11 +2,11 @@ // Created by mhajas on 5/4/17. // -#include "gtest/gtest.h" #include "stream.h" #include "streams.h" +#include "gtest/gtest.h" #include -#include +#include const static int testing_size = 1536; @@ -35,16 +35,15 @@ TEST(false_streams, basic_test) { } TEST(counter_stream, basic_test) { - std::unique_ptr cstream = std::make_unique(testing_size/256); + std::unique_ptr cstream = std::make_unique(testing_size / 256); - for(int j = 1; j <= 65536 + 2; j++) { // 2^16 + 2 + for (int j = 1; j <= 65536 + 2; j++) { // 2^16 + 2 auto data = cstream->next(); std::vector non_const(data.begin(), data.end()); - auto t = reinterpret_cast(non_const.data()); + auto t = reinterpret_cast(non_const.data()); ASSERT_EQ(j, *t); } - } TEST(sac_streams, basic_test) { @@ -60,8 +59,7 @@ TEST(sac_streams, basic_test) { auto first_beg = first_half.begin(); auto second_beg = second_half.begin(); - for ( ; first_beg != first_half.end(); - ++first_beg, ++second_beg) { + for (; first_beg != first_half.end(); ++first_beg, ++second_beg) { if (*first_beg ^ *second_beg) { value_type diff = *first_beg ^ *second_beg; while (diff) { @@ -72,14 +70,15 @@ TEST(sac_streams, basic_test) { } } } - ASSERT_EQ(difference, 1); // The position of change is random, hence it is not necessary to check + ASSERT_EQ(difference, + 1); // The position of change is random, hence it is not necessary to check } - } TEST(sac_streams, fixed_position) { seed_seq_from seeder(testsuite::seed1); - auto stream = std::make_unique(seeder, 16, 10); // position 10 = second byte, third bit + auto stream = std::make_unique( + seeder, 16, 10); // position 10 = second byte, third bit for (unsigned i = 0; i < 16; ++i) { int difference = 0; @@ -90,8 +89,7 @@ TEST(sac_streams, fixed_position) { auto first_beg = first_half.begin(); auto second_beg = second_half.begin(); - for (unsigned j = 0; first_beg != first_half.end(); - ++first_beg, ++second_beg, ++j) { + for (unsigned j = 0; first_beg != first_half.end(); ++first_beg, ++second_beg, ++j) { if (*first_beg ^ *second_beg) { ASSERT_EQ(j, 1); // Difference should be in second byte for given stream value_type diff = *first_beg ^ *second_beg; @@ -106,16 +104,16 @@ TEST(sac_streams, fixed_position) { } } } - ASSERT_EQ(difference, 1); // There should be exactly one different byte between first and second half + ASSERT_EQ(difference, + 1); // There should be exactly one different byte between first and second half } - } TEST(hw_counter, invalid_params) { const json json_config = { {"randomize_start", false}, {"increase_hw", false}, - {"hw", 4*8+1}, + {"hw", 4 * 8 + 1}, }; seed_seq_from seeder(testsuite::seed1); @@ -139,14 +137,14 @@ TEST(hw_counter, basic_test) { // Test difference to previous vector uint8_t buff1[size], buff2[size]; - uint8_t *buff_o=buff1, *buff_n=buff2; + uint8_t *buff_o = buff1, *buff_n = buff2; - for(int i=0; i < c_32_over_3 + 4; ++i){ + for (int i = 0; i < c_32_over_3 + 4; ++i) { uint32_t cur_hw = 0; uint32_t idx = 0; - for(auto it : stream->next()) { - for(int j=0; j<8; ++j){ + for (auto it : stream->next()) { + for (int j = 0; j < 8; ++j) { cur_hw += (uint8_t)it & (1 << j) ? 1 : 0; } buff_n[idx] = (uint8_t)it; @@ -175,12 +173,12 @@ TEST(hw_counter, period_test) { auto stream2 = std::make_unique(json_config, seeder, size); // Rewind one stream beyond the period - for(int i=0; i < c_32_over_3; ++i){ + for (int i = 0; i < c_32_over_3; ++i) { stream1->next(); } // Compare rewinded & fresh generator over period. - for(int i=0; i < c_32_over_3 + 4; ++i){ + for (int i = 0; i < c_32_over_3 + 4; ++i) { auto it1 = stream1->next(); auto it2 = stream2->next(); ASSERT_TRUE(it1 == it2); @@ -188,7 +186,7 @@ TEST(hw_counter, period_test) { // desync test stream1->next(); - for(int i=0; i < c_32_over_3 + 4; ++i){ + for (int i = 0; i < c_32_over_3 + 4; ++i) { auto it1 = stream1->next(); auto it2 = stream2->next(); ASSERT_FALSE(it1 == it2); @@ -196,39 +194,30 @@ TEST(hw_counter, period_test) { } TEST(column_streams, basic_test_with_counter) { - json json_config = { - {"size", 5}, - {"source", { - {"type", "counter"} - }} - }; + json json_config = {{"size", 5}, {"source", {{"type", "counter"}}}}; seed_seq_from seeder(testsuite::seed1); std::unique_ptr stream = std::make_unique(json_config, seeder, 5); // TODO: finish column test -// for(auto it : stream->next()) { -// std::cout << it << std::endl; -// } + // for(auto it : stream->next()) { + // std::cout << it << std::endl; + // } } TEST(rnd_plt_ctx_streams, aes_single_vector) { - const json json_config = { - {"type", "rnd-plt-ctx-stream"}, - {"source", { - {"type", "block"}, - {"init-frequency", "only-once"}, - {"algorithm", "AES"}, - {"round", 10}, - {"block-size", 16}, - {"plaintext", {{"type", "pcg32-stream"}}}, - {"key-size", 16}, - {"key", {{"type", "pcg32-stream"}}}, - {"iv", {{"type", "pcg32-stream"}}} - } - } - }; + const json json_config = {{"type", "rnd_plt_ctx_stream"}, + {"source", + {{"type", "block"}, + {"init_frequency", "only_once"}, + {"algorithm", "AES"}, + {"round", 10}, + {"block_size", 16}, + {"plaintext", {{"type", "pcg32_stream"}}}, + {"key_size", 16}, + {"key", {{"type", "pcg32_stream"}}}, + {"iv", {{"type", "pcg32_stream"}}}}}}; seed_seq_from seeder(testsuite::seed1); std::unique_ptr stream = make_stream(json_config, seeder, 32); @@ -238,13 +227,17 @@ TEST(rnd_plt_ctx_streams, aes_single_vector) { // fixed expected data AES 10 rounds, with PTX choosen by PCG32 with fixed seed testsuite::seed1 #if defined(__clang__) // if compiler is clang, data are different from gcc :( std::vector expected_data = { - 0x02, 0x6c, 0xc7, 0x6f, 0xaf, 0x91, 0x91, 0xb4, 0x82, 0x4c, 0x67, 0x8c, 0x22, 0x66, 0x39, 0x90, // ptx - 0xd5, 0x36, 0xd3, 0x30, 0xf5, 0xe5, 0xde, 0x87, 0x75, 0x54, 0x82, 0x19, 0xf2, 0xb9, 0xa6, 0xcc // ctx + 0x02, 0x6c, 0xc7, 0x6f, 0xaf, 0x91, 0x91, 0xb4, + 0x82, 0x4c, 0x67, 0x8c, 0x22, 0x66, 0x39, 0x90, // ptx + 0xd5, 0x36, 0xd3, 0x30, 0xf5, 0xe5, 0xde, 0x87, + 0x75, 0x54, 0x82, 0x19, 0xf2, 0xb9, 0xa6, 0xcc // ctx }; #elif defined(__GNUC__) || defined(__GNUG__) std::vector expected_data = { - 0xc5, 0x1e, 0xf0, 0xd7, 0x40, 0x1c, 0xa5, 0xea, 0x6f, 0x85, 0x11, 0x1e, 0x55, 0x74, 0x64, 0xcc, // ptx - 0x54, 0x30, 0x76, 0x5f, 0xbf, 0xa7, 0x0d, 0xcb, 0xf0, 0x9b, 0xb3, 0xc0, 0xbe, 0x2b, 0xba, 0xaf // ctx + 0xc5, 0x1e, 0xf0, 0xd7, 0x40, 0x1c, 0xa5, 0xea, + 0x6f, 0x85, 0x11, 0x1e, 0x55, 0x74, 0x64, 0xcc, // ptx + 0x54, 0x30, 0x76, 0x5f, 0xbf, 0xa7, 0x0d, 0xcb, + 0xf0, 0x9b, 0xb3, 0xc0, 0xbe, 0x2b, 0xba, 0xaf // ctx }; #elif defined(_MSC_VER) // MS data unknown std::vector expected_data = { @@ -256,33 +249,27 @@ TEST(rnd_plt_ctx_streams, aes_single_vector) { } TEST(rho_streams, aes_vector) { - const json json_config = { - {"type", "rho-stream"}, - {"source", { - {"type", "block"}, - {"init-frequency", "only-once"}, - {"algorithm", "AES"}, - {"round", 10}, - {"block-size", 16}, - {"plaintext", {{"type", "dummy-stream"}}}, - {"key-size", 16}, - {"key", {{"type", "false-stream"}}}, - {"iv", {{"type", "pcg32-stream"}}} - } - } - }; - - json aes_json_config = { - {"type", "block"}, - {"init-frequency", "only-once"}, - {"algorithm", "AES"}, - {"round", 10}, - {"block-size", 16}, - {"plaintext", {{"type", "test-stream"}}}, - {"key-size", 16}, - {"key", {{"type", "false-stream"}}}, - {"iv", {{"type", "pcg32-stream"}}} - }; + const json json_config = {{"type", "rho_stream"}, + {"source", + {{"type", "block"}, + {"init_frequency", "only_once"}, + {"algorithm", "AES"}, + {"round", 10}, + {"block_size", 16}, + {"plaintext", {{"type", "dummy_stream"}}}, + {"key_size", 16}, + {"key", {{"type", "false_stream"}}}, + {"iv", {{"type", "pcg32_stream"}}}}}}; + + json aes_json_config = {{"type", "block"}, + {"init_frequency", "only_once"}, + {"algorithm", "AES"}, + {"round", 10}, + {"block_size", 16}, + {"plaintext", {{"type", "test_stream"}}}, + {"key_size", 16}, + {"key", {{"type", "false_stream"}}}, + {"iv", {{"type", "pcg32_stream"}}}}; seed_seq_from seeder(testsuite::seed1); std::unique_ptr tested_stream = make_stream(json_config, seeder, 16); @@ -299,15 +286,3 @@ TEST(rho_streams, aes_vector) { std::copy(tested_view.begin(), tested_view.end(), prev.begin()); } } - - - - - - - - - - - - diff --git a/testsuite/test-utils/block_test_case.cc b/testsuite/test-utils/block_test_case.cc deleted file mode 100644 index 46cd0de0..00000000 --- a/testsuite/test-utils/block_test_case.cc +++ /dev/null @@ -1,107 +0,0 @@ -#include -#include -#include -#include -#include "block_test_case.h" - -namespace testsuite { - - const json block_test_case::base_config = { - {"type", "block"}, - {"init-frequency", "only-once"}, - {"plaintext", {{"type", "test-stream"}}}, - {"iv", {{"type", "test-stream"}}}, - {"key", {{"type", "test-stream"}}} - }; - - void block_test_case::test() const { - std::unique_ptr encryptor = block::make_block_cipher(_algorithm, _round, - _plaintext.size(), _key.size(), true); - - encryptor->keysetup(_key.data(), _key.size()); - if (_load_iv) - encryptor->ivsetup(_iv.data(), _iv.size()); - - std::vector output_ciphertext(_ciphertext.size()); - encryptor->encrypt(_plaintext.data(), output_ciphertext.data()); - - ASSERT_EQ(output_ciphertext, _ciphertext); - } - - void block_test_case::test(std::unique_ptr&& block_stream) const { - std::unique_ptr decryptor = block::make_block_cipher(_algorithm, _round, - _plaintext.size(), _key.size(), false); - vec_cview ciphertext = block_stream->next(); - - decryptor->keysetup(_key.data(), _key.size()); - if (_load_iv) - decryptor->ivsetup(_iv.data(), _iv.size()); - - std::vector plain(_plaintext.size()); - decryptor->decrypt(ciphertext.data(), plain.data()); - - ASSERT_EQ(plain, _plaintext); - } - - void block_test_case::operator()() { - _test_vectors_tested = 0; - - while (_test_vectors >> *this) { - _test_vectors_tested++; - test(); // encryption - auto stream = prepare_stream(); - test_case::test(stream); // encryption streams - test(prepare_stream()); // decryption - } - - - - std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round << "] is: " << _test_vectors_tested << std::endl; - } - - bool block_test_case::load_iv() const - { - return _load_iv; - } - - std::unique_ptr block_test_case::prepare_stream() { - std::size_t block_size = _ciphertext.size(); - _stream_config["algorithm"] = _algorithm; - _stream_config["round"] = _round; - _stream_config["key"]["outputs"] = _key; - _stream_config["plaintext"]["outputs"] = _plaintext; - _stream_config["block-size"] = block_size; - _stream_config["key-size"] = _key.size(); - if (_load_iv) { - _stream_config["mode"] = ""; // TODO: write mode here - _stream_config["iv"]["outputs"] = _iv; - _stream_config["iv-size"] = _iv.size(); - } else { - _stream_config["iv"]["outputs"] = _iv; - } - - seed_seq_from seeder(seed1); - return make_stream(_stream_config, seeder, block_size); - } - - std::istream &operator>>(std::istream &input, block_test_case &test_case) { - std::string str_plaintext; - std::string str_ciphertext; - std::string str_iv; - std::string str_key; - - input >> str_key; - input >> str_plaintext; - input >> str_ciphertext; - if (test_case.load_iv()) - input >> str_iv; - - test_case.update_test_vector(hex_string_to_binary(str_plaintext), - hex_string_to_binary(str_ciphertext), - hex_string_to_binary(str_key), - hex_string_to_binary(str_iv)); - - return input; - } - -} diff --git a/testsuite/test-utils/block_test_case.h b/testsuite/test-utils/block_test_case.h deleted file mode 100644 index 0801824c..00000000 --- a/testsuite/test-utils/block_test_case.h +++ /dev/null @@ -1,103 +0,0 @@ -#pragma once - -#include -#include "test_case.h" - -namespace testsuite { - /** test case for functions from sha3 competition **/ - class block_test_case : test_case { - - private: - /** Private key for current test vector **/ - std::vector _key; - - /** Initialization vector for current test vector **/ - std::vector _iv; - - /** whether we use IV **/ - bool _load_iv; - - /** JSON configuration for making stream using make_stream() **/ - json _stream_config; - - public: - /** - * base JSON configuration which all estream functions has in common - * _stream_config is created based on this, but with addition of - * function specific properties e.g. name of function, number of rounds - */ - const static json base_config; - - block_test_case(const std::string&& algorithm, const std::size_t round, const bool load_iv=false) - : test_case(algorithm, round, "block") - , _iv(0) // empty IV if it is not used - , _load_iv(load_iv) - , _stream_config(base_config) - {} - - - /** Setter for single test vector **/ - void update_test_vector(const std::vector &&plaintext, - const std::vector &&ciphertext, - const std::vector &&key, - const std::vector &&iv) { - _plaintext = plaintext; - _ciphertext = ciphertext; - _key = key; - if (_load_iv) - _iv = iv; - } - - /** - * Create new instance of stream for current function - * First it creates JSON configuration of stream and - * then create new stream using make_stream function - * @return Pointer to instance of stream - */ - std::unique_ptr prepare_stream() override; - - /** - * Test raw function with current test vector - * @param encryptor - */ - void test() const; - - /** - * Test stream for current function with current test vector - * including decryption of output of stream - * @param encryptor Stream for current function - * @param decryptor Pointer to raw function instance - */ - void test(std::unique_ptr&& encryptor) const; - - /** - * test case is actually a functor which can done whole testing - * It is necessary to have function name and round number and it - * will step by step load all test vectors and test each with raw - * function and stream - */ - void operator()() override; - - /** - * Reads test vector from input stream - * - * STRUCTURE OF TEST VECTORS (All present in test-resources/block/{function-name}/test-vectors-{round-number}.txt) - * {key in hex} - * {plaintext in hex} - * {ciphertext in hex} - * {initialization-vector in hex} # Only in case _load_iv is true - * {newline} - * {key in hex} - * {plaintext in hex} - * {ciphertext in hex} - * {initialization-vector in hex} # Only in case _load_iv is true - * - * @param input stream - * @param test_case test_case instance - * @return input stream - */ - friend std::istream &operator>>(std::istream &input, block_test_case &test_case); - - bool load_iv() const; - }; -} diff --git a/testsuite/test-utils/hash_test_case.cc b/testsuite/test-utils/hash_test_case.cc deleted file mode 100644 index 4032b580..00000000 --- a/testsuite/test-utils/hash_test_case.cc +++ /dev/null @@ -1,91 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include "hash_test_case.h" - -namespace testsuite { - - - const json sha3_test_case::base_config = { - {"type", "sha3"}, - {"source", {{"type", "test-stream"}}} - }; - - const json other_test_case::base_config = { - {"type", "other_hash"}, - {"source", {{"type", "test-stream"}}} - }; - - void hash_test_case::test() const { - std::unique_ptr hasher = hash::hash_factory::create(_algorithm, unsigned(_round)); - std::size_t hash_size = _ciphertext.size(); - - std::vector hash(hash_size); - - using std::to_string; - - int status = hasher->Init(int(hash_size * 8)); - if (status != 0) - throw std::runtime_error("cannot initialize hash (code: " + to_string(status) + ")"); - - status = hasher->Update(_plaintext.data(), _length); - if (status != 0) - throw std::runtime_error("cannot update the hash (code: " + to_string(status) + ")"); - - status = hasher->Final(hash.data()); - if (status != 0) - throw std::runtime_error("cannot finalize the hash (code: " + to_string(status) + ")"); - - ASSERT_EQ(hash, _ciphertext); - } - - void hash_test_case::operator()() { - _test_vectors_tested = 0; - - while (_test_vectors >> *this) { - _test_vectors_tested++; - test(); - if (length() != 0 && length() % 8 == 0) { // unfortunately our sha3 streams are built so that it fits only multiple of 8 - auto stream = prepare_stream(); - test_case::test(stream); - } - } - - std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round << "] is: " << _test_vectors_tested << std::endl; - } - - const size_t& hash_test_case::length() const { - return _length; - } - - std::unique_ptr hash_test_case::prepare_stream() { - std::size_t hash_size = _ciphertext.size(); - _stream_config["algorithm"] = _algorithm; - _stream_config["round"] = _round; - _stream_config["input-size"] = _plaintext.size(); - _stream_config["source"]["outputs"] = _plaintext; - _stream_config["hash-size"] = hash_size; - - seed_seq_from seeder(seed1); - return make_stream(_stream_config, seeder, hash_size); - } - - std::istream &operator>>(std::istream &input, hash_test_case &test_case) { - std::string str_plaintext; - std::string str_hash; - - input >> test_case._length; - input >> str_plaintext; - input >> str_hash; - - test_case.update_test_vector(hex_string_to_binary(str_plaintext), - hex_string_to_binary(str_hash)); - - return input; - } - -} diff --git a/testsuite/test-utils/hash_test_case.h b/testsuite/test-utils/hash_test_case.h deleted file mode 100644 index 6c3b9fac..00000000 --- a/testsuite/test-utils/hash_test_case.h +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -#include -#include -#include - -#include -#include "test_case.h" - -namespace testsuite { - - class hash_test_case : test_case { - - protected: - /** size of plaintext **/ - size_t _length; - - /** JSON configuration for making stream using make_stream() **/ - json _stream_config; - - public: - - const size_t& length() const; - - hash_test_case(const std::string& algorithm, const std::size_t round, const json& base_config, const std::string& competition) - : test_case(algorithm, round, competition) - , _length(0) - , _stream_config(base_config) - {} - - /** - * Create new instance of stream for current function - * First it creates JSON configuration of stream and - * then create new stream using make_stream function - * @return Pointer to instance of stream - */ - std::unique_ptr prepare_stream() override ; - - /** - * Test raw function with current test vector - * @param encryptor - */ - void test() const; - - /** - * test case is actually a functor which can done whole testing - * It is necessary to have function name and round number and it - * will step by step load all test vectors and test each with raw - * function and stream - */ - void operator()() override; - - /** Setter for single test vector **/ - void update_test_vector(const std::vector &&plaintext, - const std::vector &&hash) { - _plaintext = plaintext; - _ciphertext = hash; - } - - /** - * Reads test vector from input stream - * - * STRUCTURE OF TEST VECTORS (All present in test-resources/sha3/{function-name}/test-vectors-{round-number}.txt) - * {bitsize-of-plaintext} {plaintext in hex} - * {ciphertext in hex} - * {bitsize-of-plaintext2} {plaintext2 in hex} - * {ciphertext2 in hex} - * - * @param input stream - * @param test_case test_case instance - * @return input stream - */ - friend std::istream &operator>>(std::istream &input, hash_test_case &test_case); - - }; - - /** test case for functions from sha3 competition **/ - class sha3_test_case : public hash_test_case { - - private: - - /** - * base JSON configuration which all estream functions has in common - * _stream_config is created based on this, but with addition of - * function specific properties e.g. name of function, number of rounds - */ - const static json base_config; - - public: - - sha3_test_case(const std::string&& algorithm, const std::size_t round) - : hash_test_case(algorithm, round, base_config, "sha3") - {} - }; - - class other_test_case : public hash_test_case { - - private: - - /** - * base JSON configuration which all estream functions has in common - * _stream_config is created based on this, but with addition of - * function specific properties e.g. name of function, number of rounds - */ - const static json base_config; - - public: - - other_test_case(const std::string&& algorithm, const std::size_t round) - : hash_test_case(algorithm, round, base_config, "other_hash") - {} - - }; -} - diff --git a/testsuite/test-utils/stream_ciphers_test_case.cc b/testsuite/test-utils/stream_ciphers_test_case.cc deleted file mode 100644 index 6b303777..00000000 --- a/testsuite/test-utils/stream_ciphers_test_case.cc +++ /dev/null @@ -1,135 +0,0 @@ -#include -#include -#include -#include -#include "stream_ciphers_test_case.h" - -namespace testsuite { - const json stream_cipher_test_case::base_config = { - {"type", "estream"}, - {"plaintext", {{"type", "test-stream"}}}, - {"iv", {{"type", "test-stream"}}}, - {"key", {{"type", "test-stream"}}}, - }; - - void stream_cipher_test_case::test(std::unique_ptr &encryptor) const { - size_t size = _plaintext.size(); - std::vector cipher(size); - - encryptor->keysetup(_key.data(), unsigned(_key.size() * 8), unsigned(_iv.size() * 8)); - encryptor->ivsetup(_iv.data()); - encryptor->encrypt_bytes(_plaintext.data(), cipher.data(), unsigned(_plaintext.size())); - - std::stringstream ss; - - for (auto byte : cipher) { - ss << std::setfill('0') << std::setw(2) << std::hex << int(byte); - } - - std::string ciphertext_str = ss.str(); - compare_ciphertext(ciphertext_str); - } - - unsigned long stream_cipher_test_case::block_size() const { - return _plaintext.size(); - } - - const json &stream_cipher_test_case::stream_config() const { - return _stream_config; - } - - std::unique_ptr stream_cipher_test_case::prepare_stream() { - _stream_config["algorithm"] = _algorithm; - _stream_config["round"] = _round; - _stream_config["plaintext"]["outputs"] = _plaintext; - _stream_config["iv"]["outputs"] = _iv; - _stream_config["key"]["outputs"] = _key; - _stream_config["block-size"] = block_size(); - - seed_seq_from seeder(seed1); - - return make_stream(_stream_config, seeder, block_size()); - } - - void stream_cipher_test_case::test(std::unique_ptr&& encryptor, std::unique_ptr& decryptor) const { - vec_cview ciphertext = encryptor->next(); - - std::vector plain(ciphertext.size()); - - decryptor->keysetup(_key.data(), _key.size() * 8, _iv.size() * 8); - decryptor->ivsetup(_iv.data()); - decryptor->decrypt_bytes(ciphertext.data(), plain.data(), ciphertext.size()); - - vec_cview plain_view = vec_cview(plain.cbegin(), plain.cend()); - ASSERT_EQ(plain_view.copy_to_vector(), _plaintext); - } - - void stream_cipher_test_case::operator()() { - _cipher->init(); - - _test_vectors_tested = 0; - while(_test_vectors >> *this) { - _test_vectors_tested++; - test(_cipher); - auto stream = prepare_stream(); - test_case::test(stream); - test(prepare_stream(), _cipher); - } - - std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round << "] is: " << _test_vectors_tested << std::endl; - } - - void stream_cipher_test_case::load_ciphertext(std::istream& stream) { - _ciphertext.clear(); - std::string cipher_line; - std::getline(stream, cipher_line); - // Hexadecimal to lower case, in order to be compatible with std::hex - std::transform(cipher_line.begin(), cipher_line.end(), cipher_line.begin(), ::tolower); - - if (std::any_of(cipher_line.cbegin(), cipher_line.cend(), isspace)) { - std::stringstream cipher_stream(cipher_line); - std::size_t position; - std::string ciphertext; - - while (cipher_stream >> position) { - cipher_stream >> ciphertext; - _ciphertext.push_back(std::make_tuple(position, ciphertext)); - } - - return; - } - _ciphertext.push_back(std::make_tuple(0, cipher_line)); - } - - void stream_cipher_test_case::compare_ciphertext(const std::string& actual) const { - for (auto tuple : _ciphertext) { - ASSERT_EQ(actual.substr(std::get<0>(tuple) * 2, - std::get<1>(tuple).size()), - std::get<1>(tuple)); // *2 because sequence starts at std::get<0>(tuple) byte, but we are storing each byte on two bytes f. e. FF - } - } - - std::istream &operator>>(std::istream &input, stream_cipher_test_case &test_case) { - std::string plaintext; - std::string key; - std::string iv; - - input >> plaintext; - input >> key; - input >> iv; - - // fixes empty IV - if (iv == "empty") { - iv = ""; - } - - input.get(); // read the newline - test_case.load_ciphertext(input); - test_case.update_test_vector(hex_string_to_binary(plaintext), - hex_string_to_binary(key), - hex_string_to_binary(iv)); - - return input; - } -} - diff --git a/testsuite/test-utils/stream_ciphers_test_case.h b/testsuite/test-utils/stream_ciphers_test_case.h deleted file mode 100644 index cb21a6c1..00000000 --- a/testsuite/test-utils/stream_ciphers_test_case.h +++ /dev/null @@ -1,137 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include -#include -#include -#include "common_functions.h" -#include "test_case.h" - -namespace testsuite { - /** - * Class represents one test_case with one test vector - * class support testing of estream function directly including decryption - * and also testing of stream with - */ - class stream_cipher_test_case : test_case { - - private: - - void compare_ciphertext(const std::string &actual) const; - - /** - * cipher text in form of tuples - * for long ciphertext testvectors contains more tuples, if whole ciphertext is given - * this structure contains only one tuple <0, ciphertext> - * **/ - std::vector> _ciphertext; - - /** Private key for current test vector **/ - std::vector _key; - - /** Initialization vector for current test vector **/ - std::vector _iv; - - /** JSON configuration for making stream using make_stream() **/ - json _stream_config; - - /** Pointer to raw function instance **/ - std::unique_ptr _cipher; - public: - - /** - * base JSON configuration which all estream functions has in common - * _stream_config is created based on this, but with addition of - * function specific properties e.g. name of function, number of rounds - */ - const static json base_config; - - stream_cipher_test_case(const std::string&& algorithm, const std::size_t round) - : test_case(algorithm, round, "estream") - , _stream_config(base_config) - , _cipher(stream_ciphers::create_stream_cipher(_algorithm, unsigned(_round))) - {} - - /** Setter for single test vector **/ - void update_test_vector(const std::vector &&plaintext, - const std::vector &&key, - const std::vector &&iv) { - _plaintext = plaintext; - _key = key; - _iv = iv; - } - - /** - * Loads ciphertext from stream, stream must be in separate line and - * should be in format ciphertext, or index1 ciphertext_which_begins_in_index1 index2 ..... etc. - * @param stream to read from - */ - void load_ciphertext(std::istream& stream); - - /** Getter for current function configuration **/ - const json& stream_config() const; - - /** Getter for block size **/ - unsigned long block_size() const; - - /** - * Test raw function with current test vector - * @param encryptor - */ - void test(std::unique_ptr& encryptor) const; - - /** - * Test stream for current function with current test vector - * including decryption of output of stream - * @param encryptor Stream for current function - * @param decryptor Pointer to raw function instance - */ - void test(std::unique_ptr&& encryptor, std::unique_ptr& decryptor) const; - - /** - * Create new instance of stream for current function - * First it creates JSON configuration of stream and - * then create new stream using make_stream function - * @return Pointer to instance of stream - */ - std::unique_ptr prepare_stream() override; - - /** - * test case is actually a functor which can done whole testing - * It is necessary to have function name and round number and it - * will step by step load all test vectors and test each with raw - * function and stream - */ - void operator()() override; - - /** - * Reads test vector from input stream - * - * STRUCTURE OF TEST VECTORS (All present in test-resources/estream/{function-name}/test-vectors-{round-number}.txt) - * {plaintext in hex} - * {key in hex} - * {initialization vector} - * {ciphertext} - * {plaintext2 in hex} - * {key2 in hex} - * {initialization vector2} - * {ciphertext2} - * - * STRUCTURE OF CIPHERTEXT - * two options - * - whole plaintext - * - tuples (index where ciphertext begins) (ciphretext) - * for example "0 FF 5 EE" means that first byte is FF and sixth byte is EE - * - * @param input stream - * @param test_case test_case instance - * @return input stream - */ - friend std::istream& operator>>(std::istream& input, stream_cipher_test_case& test_case); - }; -} - diff --git a/testsuite/test-utils/test_case.h b/testsuite/test-utils/test_case.h deleted file mode 100644 index fac9fc6e..00000000 --- a/testsuite/test-utils/test_case.h +++ /dev/null @@ -1,106 +0,0 @@ -// -// Created by mhajas on 7/27/17. -// - -#ifndef EACIRC_STREAMS_TEST_CASE_H -#define EACIRC_STREAMS_TEST_CASE_H - -#include -#include -#include -#include -#include "common_functions.h" -#include "gtest/gtest.h" -#include - - -namespace testsuite { - - /** seed is not actually used but some functions needs it as argument **/ - const static seed seed1 = seed::create("1fe40505e131963c"); - - /** - * this is abstract parent of all test cases, contains all functions and variables - * which cipher suites test vectors has in common e.g. plaintext, ciphertext, rounds etc. - */ - class test_case { - - protected: - /** - * Algorithm name, which is used in cipher suites factories so that it is possible to create - * instance from it - */ - const std::string _algorithm; - - /** Number of round of function for current test vectors **/ - const std::size_t _round; - - /** This counts number of test vectors tested in operator() **/ - std::uint64_t _test_vectors_tested = 0; - - /** File stream with test vectors **/ - std::ifstream _test_vectors; - - /** Plaintext, should be already converted from pure string to bytes representation **/ - std::vector _plaintext; - - /** cipher text in form of byte vector **/ - std::vector _ciphertext; - - /** - * Auxiliary function which generates filepath to testvectors for arguments - * @param algorithm - * @param round - * @param competition - * @return filepath - */ - static std::string get_test_vectors_filename(const std::string& algorithm, const std::size_t round, const std::string& competition) { - return "resources/test-resources/" + competition + "/" + algorithm + - "/test-vectors-" + std::to_string(round) + ".txt"; - } - - public: - - test_case(const std::string& algorithm, const std::size_t round, const std::string& competition) - : _algorithm(algorithm) - , _round(round) - , _test_vectors(get_test_vectors_filename(algorithm, round, competition), std::ifstream::in) - { - if (!_test_vectors.is_open()) { - throw std::runtime_error("Not able to open file: \"" + get_test_vectors_filename(algorithm, round, competition) + "\""); - } - } - - /** - * Generates next output from stream and compare it with expected output - * @param s stream - */ - void test(std::unique_ptr& s) const { - vec_cview actual = s->next(); - ASSERT_EQ(make_cview(_ciphertext), actual); - } - - /** - * Create new instance of stream for current function - * First it creates JSON configuration of stream and - * then create new stream using make_stream function - * @return Pointer to instance of stream - */ - virtual std::unique_ptr prepare_stream() = 0; - - /** - * test case is actually a functor which can done whole testing - * It is necessary to have function name and round number and it - * will step by step load all test vectors and test each with raw - * function and stream - */ - virtual void operator()() = 0; - - virtual ~test_case() { - _test_vectors.close(); - } - }; - -} - -#endif //EACIRC_STREAMS_TEST_CASE_H diff --git a/testsuite/test-utils/test_streams.h b/testsuite/test-utils/test_streams.h deleted file mode 100644 index 5171acac..00000000 --- a/testsuite/test-utils/test_streams.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include "stream.h" -#include "common_functions.h" - -namespace testsuite { - - /** Stream for testing which return values given by JSON configuration **/ - struct test_stream : stream { - test_stream(const json& config); - - vec_cview next() override; - - private: - /** view for the data, which is provided to next() **/ - std::vector _data; - }; -} - diff --git a/testsuite/test_main.cc b/testsuite/test_main.cc index 22972c8a..3e21e6b4 100644 --- a/testsuite/test_main.cc +++ b/testsuite/test_main.cc @@ -1,7 +1,7 @@ #include "gtest/gtest.h" // Main file which triggers all tests in testsuite -int main(int argc, char** argv) { +int main(int argc, char **argv) { /* initialize google test framework */ ::testing::InitGoogleTest(&argc, argv); diff --git a/testsuite/test-resources/block/AES/test-vectors-10.txt b/testsuite/test_resources/block/AES/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/block/AES/test-vectors-10.txt rename to testsuite/test_resources/block/AES/test_vectors_10.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-1.txt b/testsuite/test_resources/block/ARIA/test_vectors_1.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-1.txt rename to testsuite/test_resources/block/ARIA/test_vectors_1.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-11.txt b/testsuite/test_resources/block/ARIA/test_vectors_11.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-11.txt rename to testsuite/test_resources/block/ARIA/test_vectors_11.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-12.txt b/testsuite/test_resources/block/ARIA/test_vectors_12.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-12.txt rename to testsuite/test_resources/block/ARIA/test_vectors_12.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-2.txt b/testsuite/test_resources/block/ARIA/test_vectors_2.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-2.txt rename to testsuite/test_resources/block/ARIA/test_vectors_2.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-6.txt b/testsuite/test_resources/block/ARIA/test_vectors_6.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-6.txt rename to testsuite/test_resources/block/ARIA/test_vectors_6.txt diff --git a/testsuite/test-resources/block/ARIA/test-vectors-7.txt b/testsuite/test_resources/block/ARIA/test_vectors_7.txt similarity index 100% rename from testsuite/test-resources/block/ARIA/test-vectors-7.txt rename to testsuite/test_resources/block/ARIA/test_vectors_7.txt diff --git a/testsuite/test-resources/block/BLOWFISH/test-vectors-16.txt b/testsuite/test_resources/block/BLOWFISH/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/BLOWFISH/test-vectors-16.txt rename to testsuite/test_resources/block/BLOWFISH/test_vectors_16.txt diff --git a/testsuite/test-resources/block/CAMELLIA/test-vectors-18.txt b/testsuite/test_resources/block/CAMELLIA/test_vectors_18.txt similarity index 100% rename from testsuite/test-resources/block/CAMELLIA/test-vectors-18.txt rename to testsuite/test_resources/block/CAMELLIA/test_vectors_18.txt diff --git a/testsuite/test-resources/block/CAMELLIA/test-vectors-24.txt b/testsuite/test_resources/block/CAMELLIA/test_vectors_24.txt similarity index 100% rename from testsuite/test-resources/block/CAMELLIA/test-vectors-24.txt rename to testsuite/test_resources/block/CAMELLIA/test_vectors_24.txt diff --git a/testsuite/test-resources/block/CAST/test-vectors-16.txt b/testsuite/test_resources/block/CAST/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/CAST/test-vectors-16.txt rename to testsuite/test_resources/block/CAST/test_vectors_16.txt diff --git a/testsuite/test-resources/block/GOST/test-vectors-32.txt b/testsuite/test_resources/block/GOST/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/GOST/test-vectors-32.txt rename to testsuite/test_resources/block/GOST/test_vectors_32.txt diff --git a/testsuite/test-resources/block/IDEA/test-vectors-8.txt b/testsuite/test_resources/block/IDEA/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/block/IDEA/test-vectors-8.txt rename to testsuite/test_resources/block/IDEA/test_vectors_8.txt diff --git a/testsuite/test-resources/block/KASUMI/test-vectors-8.txt b/testsuite/test_resources/block/KASUMI/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/block/KASUMI/test-vectors-8.txt rename to testsuite/test_resources/block/KASUMI/test_vectors_8.txt diff --git a/testsuite/test-resources/block/KUZNYECHIK/test-vectors-10.txt b/testsuite/test_resources/block/KUZNYECHIK/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/block/KUZNYECHIK/test-vectors-10.txt rename to testsuite/test_resources/block/KUZNYECHIK/test_vectors_10.txt diff --git a/testsuite/test-resources/block/MARS/test-vectors-32.txt_nonworking b/testsuite/test_resources/block/MARS/test-vectors-32.txt_nonworking similarity index 100% rename from testsuite/test-resources/block/MARS/test-vectors-32.txt_nonworking rename to testsuite/test_resources/block/MARS/test-vectors-32.txt_nonworking diff --git a/testsuite/test-resources/block/MARS/test-vectors-32.txt b/testsuite/test_resources/block/MARS/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/MARS/test-vectors-32.txt rename to testsuite/test_resources/block/MARS/test_vectors_32.txt diff --git a/testsuite/test-resources/block/MISTY1/test-vectors-4.txt b/testsuite/test_resources/block/MISTY1/test_vectors_4.txt similarity index 100% rename from testsuite/test-resources/block/MISTY1/test-vectors-4.txt rename to testsuite/test_resources/block/MISTY1/test_vectors_4.txt diff --git a/testsuite/test-resources/block/NOEKEON/test-vectors-16.txt b/testsuite/test_resources/block/NOEKEON/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/NOEKEON/test-vectors-16.txt rename to testsuite/test_resources/block/NOEKEON/test_vectors_16.txt diff --git a/testsuite/test-resources/block/RC6/test-vectors-20 (copy).txt b/testsuite/test_resources/block/RC6/test-vectors-20 (copy).txt similarity index 100% rename from testsuite/test-resources/block/RC6/test-vectors-20 (copy).txt rename to testsuite/test_resources/block/RC6/test-vectors-20 (copy).txt diff --git a/testsuite/test-resources/block/RC6/test-vectors-20.txt_wrong b/testsuite/test_resources/block/RC6/test-vectors-20.txt_wrong similarity index 100% rename from testsuite/test-resources/block/RC6/test-vectors-20.txt_wrong rename to testsuite/test_resources/block/RC6/test-vectors-20.txt_wrong diff --git a/testsuite/test-resources/block/RC6/test-vectors-20.txt b/testsuite/test_resources/block/RC6/test_vectors_20.txt similarity index 100% rename from testsuite/test-resources/block/RC6/test-vectors-20.txt rename to testsuite/test_resources/block/RC6/test_vectors_20.txt diff --git a/testsuite/test-resources/block/SEED/test-vectors-16.txt b/testsuite/test_resources/block/SEED/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/SEED/test-vectors-16.txt rename to testsuite/test_resources/block/SEED/test_vectors_16.txt diff --git a/testsuite/test-resources/block/SERPENT/test-vectors-32.txt_nessie b/testsuite/test_resources/block/SERPENT/test-vectors-32.txt_nessie similarity index 100% rename from testsuite/test-resources/block/SERPENT/test-vectors-32.txt_nessie rename to testsuite/test_resources/block/SERPENT/test-vectors-32.txt_nessie diff --git a/testsuite/test-resources/block/SERPENT/test-vectors-32.txt_wrong b/testsuite/test_resources/block/SERPENT/test-vectors-32.txt_wrong similarity index 100% rename from testsuite/test-resources/block/SERPENT/test-vectors-32.txt_wrong rename to testsuite/test_resources/block/SERPENT/test-vectors-32.txt_wrong diff --git a/testsuite/test-resources/block/SERPENT/test-vectors-32.txt b/testsuite/test_resources/block/SERPENT/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/SERPENT/test-vectors-32.txt rename to testsuite/test_resources/block/SERPENT/test_vectors_32.txt diff --git a/testsuite/test-resources/block/SHACAL2/test-vectors-64.txt b/testsuite/test_resources/block/SHACAL2/test_vectors_64.txt similarity index 100% rename from testsuite/test-resources/block/SHACAL2/test-vectors-64.txt rename to testsuite/test_resources/block/SHACAL2/test_vectors_64.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-32.txt b/testsuite/test_resources/block/SIMON/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-32.txt rename to testsuite/test_resources/block/SIMON/test_vectors_32.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-36.txt b/testsuite/test_resources/block/SIMON/test_vectors_36.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-36.txt rename to testsuite/test_resources/block/SIMON/test_vectors_36.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-42.txt b/testsuite/test_resources/block/SIMON/test_vectors_42.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-42.txt rename to testsuite/test_resources/block/SIMON/test_vectors_42.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-44.txt b/testsuite/test_resources/block/SIMON/test_vectors_44.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-44.txt rename to testsuite/test_resources/block/SIMON/test_vectors_44.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-52.txt b/testsuite/test_resources/block/SIMON/test_vectors_52.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-52.txt rename to testsuite/test_resources/block/SIMON/test_vectors_52.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-54.txt b/testsuite/test_resources/block/SIMON/test_vectors_54.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-54.txt rename to testsuite/test_resources/block/SIMON/test_vectors_54.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-68.txt b/testsuite/test_resources/block/SIMON/test_vectors_68.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-68.txt rename to testsuite/test_resources/block/SIMON/test_vectors_68.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-69.txt b/testsuite/test_resources/block/SIMON/test_vectors_69.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-69.txt rename to testsuite/test_resources/block/SIMON/test_vectors_69.txt diff --git a/testsuite/test-resources/block/SIMON/test-vectors-72.txt b/testsuite/test_resources/block/SIMON/test_vectors_72.txt similarity index 100% rename from testsuite/test-resources/block/SIMON/test-vectors-72.txt rename to testsuite/test_resources/block/SIMON/test_vectors_72.txt diff --git a/testsuite/test-resources/block/SINGLE-DES/test-vectors-16.txt b/testsuite/test_resources/block/SINGLE-DES/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/SINGLE-DES/test-vectors-16.txt rename to testsuite/test_resources/block/SINGLE-DES/test_vectors_16.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-22.txt b/testsuite/test_resources/block/SPECK/test_vectors_22.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-22.txt rename to testsuite/test_resources/block/SPECK/test_vectors_22.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-23.txt b/testsuite/test_resources/block/SPECK/test_vectors_23.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-23.txt rename to testsuite/test_resources/block/SPECK/test_vectors_23.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-26.txt b/testsuite/test_resources/block/SPECK/test_vectors_26.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-26.txt rename to testsuite/test_resources/block/SPECK/test_vectors_26.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-27.txt b/testsuite/test_resources/block/SPECK/test_vectors_27.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-27.txt rename to testsuite/test_resources/block/SPECK/test_vectors_27.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-28.txt b/testsuite/test_resources/block/SPECK/test_vectors_28.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-28.txt rename to testsuite/test_resources/block/SPECK/test_vectors_28.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-29.txt b/testsuite/test_resources/block/SPECK/test_vectors_29.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-29.txt rename to testsuite/test_resources/block/SPECK/test_vectors_29.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-32.txt b/testsuite/test_resources/block/SPECK/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-32.txt rename to testsuite/test_resources/block/SPECK/test_vectors_32.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-33.txt b/testsuite/test_resources/block/SPECK/test_vectors_33.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-33.txt rename to testsuite/test_resources/block/SPECK/test_vectors_33.txt diff --git a/testsuite/test-resources/block/SPECK/test-vectors-34.txt b/testsuite/test_resources/block/SPECK/test_vectors_34.txt similarity index 100% rename from testsuite/test-resources/block/SPECK/test-vectors-34.txt rename to testsuite/test_resources/block/SPECK/test_vectors_34.txt diff --git a/testsuite/test-resources/block/TEA/test-vectors-32.txt b/testsuite/test_resources/block/TEA/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/TEA/test-vectors-32.txt rename to testsuite/test_resources/block/TEA/test_vectors_32.txt diff --git a/testsuite/test-resources/block/TRIPLE-DES/test-vectors-16.txt b/testsuite/test_resources/block/TRIPLE-DES/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/TRIPLE-DES/test-vectors-16.txt rename to testsuite/test_resources/block/TRIPLE-DES/test_vectors_16.txt diff --git a/testsuite/test-resources/block/TWOFISH/test-vectors-16.txt b/testsuite/test_resources/block/TWOFISH/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/block/TWOFISH/test-vectors-16.txt rename to testsuite/test_resources/block/TWOFISH/test_vectors_16.txt diff --git a/testsuite/test-resources/block/XTEA/test-vectors-32.txt b/testsuite/test_resources/block/XTEA/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/block/XTEA/test-vectors-32.txt rename to testsuite/test_resources/block/XTEA/test_vectors_32.txt diff --git a/testsuite/test-resources/sha3/ARIRANG/test-vectors-4.txt b/testsuite/test_resources/hash/ARIRANG/test_vectors_4.txt similarity index 100% rename from testsuite/test-resources/sha3/ARIRANG/test-vectors-4.txt rename to testsuite/test_resources/hash/ARIRANG/test_vectors_4.txt diff --git a/testsuite/test-resources/sha3/AURORA/test-vectors-17.txt b/testsuite/test_resources/hash/AURORA/test_vectors_17.txt similarity index 100% rename from testsuite/test-resources/sha3/AURORA/test-vectors-17.txt rename to testsuite/test_resources/hash/AURORA/test_vectors_17.txt diff --git a/testsuite/test-resources/sha3/Abacus/test-vectors-135.txt b/testsuite/test_resources/hash/Abacus/test_vectors_135.txt similarity index 100% rename from testsuite/test-resources/sha3/Abacus/test-vectors-135.txt rename to testsuite/test_resources/hash/Abacus/test_vectors_135.txt diff --git a/testsuite/test-resources/sha3/BLAKE/test-vectors-10.txt b/testsuite/test_resources/hash/BLAKE/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/sha3/BLAKE/test-vectors-10.txt rename to testsuite/test_resources/hash/BLAKE/test_vectors_10.txt diff --git a/testsuite/test-resources/sha3/BLAKE/test-vectors-14.txt b/testsuite/test_resources/hash/BLAKE/test_vectors_14.txt similarity index 100% rename from testsuite/test-resources/sha3/BLAKE/test-vectors-14.txt rename to testsuite/test_resources/hash/BLAKE/test_vectors_14.txt diff --git a/testsuite/test-resources/sha3/Cheetah/test-vectors-16.txt b/testsuite/test_resources/hash/Cheetah/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/sha3/Cheetah/test-vectors-16.txt rename to testsuite/test_resources/hash/Cheetah/test_vectors_16.txt diff --git a/testsuite/test-resources/sha3/CubeHash/test-vectors-8.txt b/testsuite/test_resources/hash/CubeHash/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/sha3/CubeHash/test-vectors-8.txt rename to testsuite/test_resources/hash/CubeHash/test_vectors_8.txt diff --git a/testsuite/test-resources/sha3/DCH/test-vectors-4.txt b/testsuite/test_resources/hash/DCH/test_vectors_4.txt similarity index 100% rename from testsuite/test-resources/sha3/DCH/test-vectors-4.txt rename to testsuite/test_resources/hash/DCH/test_vectors_4.txt diff --git a/testsuite/test-resources/sha3/DynamicSHA/test-vectors-16.txt b/testsuite/test_resources/hash/DynamicSHA/test_vectors_16.txt similarity index 100% rename from testsuite/test-resources/sha3/DynamicSHA/test-vectors-16.txt rename to testsuite/test_resources/hash/DynamicSHA/test_vectors_16.txt diff --git a/testsuite/test-resources/sha3/DynamicSHA2/test-vectors-17.txt b/testsuite/test_resources/hash/DynamicSHA2/test_vectors_17.txt similarity index 100% rename from testsuite/test-resources/sha3/DynamicSHA2/test-vectors-17.txt rename to testsuite/test_resources/hash/DynamicSHA2/test_vectors_17.txt diff --git a/testsuite/test-resources/sha3/ECHO/test-vectors-10.txt b/testsuite/test_resources/hash/ECHO/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/sha3/ECHO/test-vectors-10.txt rename to testsuite/test_resources/hash/ECHO/test_vectors_10.txt diff --git a/testsuite/test-resources/sha3/ECHO/test-vectors-8.txt b/testsuite/test_resources/hash/ECHO/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/sha3/ECHO/test-vectors-8.txt rename to testsuite/test_resources/hash/ECHO/test_vectors_8.txt diff --git a/testsuite/test-resources/other_hash/Gost/test-vectors-32.txt b/testsuite/test_resources/hash/Gost/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/other_hash/Gost/test-vectors-32.txt rename to testsuite/test_resources/hash/Gost/test_vectors_32.txt diff --git a/testsuite/test-resources/sha3/Grostl/test-vectors-10.txt b/testsuite/test_resources/hash/Grostl/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/sha3/Grostl/test-vectors-10.txt rename to testsuite/test_resources/hash/Grostl/test_vectors_10.txt diff --git a/testsuite/test-resources/sha3/Grostl/test-vectors-14.txt b/testsuite/test_resources/hash/Grostl/test_vectors_14.txt similarity index 100% rename from testsuite/test-resources/sha3/Grostl/test-vectors-14.txt rename to testsuite/test_resources/hash/Grostl/test_vectors_14.txt diff --git a/testsuite/test-resources/sha3/Hamsi/test-vectors-3.txt b/testsuite/test_resources/hash/Hamsi/test_vectors_3.txt similarity index 100% rename from testsuite/test-resources/sha3/Hamsi/test-vectors-3.txt rename to testsuite/test_resources/hash/Hamsi/test_vectors_3.txt diff --git a/testsuite/test-resources/sha3/Hamsi/test-vectors-6.txt b/testsuite/test_resources/hash/Hamsi/test_vectors_6.txt similarity index 100% rename from testsuite/test-resources/sha3/Hamsi/test-vectors-6.txt rename to testsuite/test_resources/hash/Hamsi/test_vectors_6.txt diff --git a/testsuite/test-resources/sha3/JH/test-vectors-42.txt b/testsuite/test_resources/hash/JH/test_vectors_42.txt similarity index 100% rename from testsuite/test-resources/sha3/JH/test-vectors-42.txt rename to testsuite/test_resources/hash/JH/test_vectors_42.txt diff --git a/testsuite/test-resources/sha3/Lesamnta/test-vectors-32.txt b/testsuite/test_resources/hash/Lesamnta/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/sha3/Lesamnta/test-vectors-32.txt rename to testsuite/test_resources/hash/Lesamnta/test_vectors_32.txt diff --git a/testsuite/test-resources/sha3/Luffa/test-vectors-8.txt b/testsuite/test_resources/hash/Luffa/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/sha3/Luffa/test-vectors-8.txt rename to testsuite/test_resources/hash/Luffa/test_vectors_8.txt diff --git a/testsuite/test-resources/other_hash/MD5/test-vectors-64.txt b/testsuite/test_resources/hash/MD5/test_vectors_64.txt similarity index 100% rename from testsuite/test-resources/other_hash/MD5/test-vectors-64.txt rename to testsuite/test_resources/hash/MD5/test_vectors_64.txt diff --git a/testsuite/test-resources/sha3/MD6/test-vectors-104.txt b/testsuite/test_resources/hash/MD6/test_vectors_104.txt similarity index 100% rename from testsuite/test-resources/sha3/MD6/test-vectors-104.txt rename to testsuite/test_resources/hash/MD6/test_vectors_104.txt diff --git a/testsuite/test-resources/sha3/MD6/test-vectors-105.txt b/testsuite/test_resources/hash/MD6/test_vectors_105.txt similarity index 100% rename from testsuite/test-resources/sha3/MD6/test-vectors-105.txt rename to testsuite/test_resources/hash/MD6/test_vectors_105.txt diff --git a/testsuite/test-resources/sha3/MD6/test-vectors-96.txt b/testsuite/test_resources/hash/MD6/test_vectors_96.txt similarity index 100% rename from testsuite/test-resources/sha3/MD6/test-vectors-96.txt rename to testsuite/test_resources/hash/MD6/test_vectors_96.txt diff --git a/testsuite/test-resources/other_hash/RIPEMD160/test-vectors-80.txt b/testsuite/test_resources/hash/RIPEMD160/test_vectors_80.txt similarity index 100% rename from testsuite/test-resources/other_hash/RIPEMD160/test-vectors-80.txt rename to testsuite/test_resources/hash/RIPEMD160/test_vectors_80.txt diff --git a/testsuite/test-resources/other_hash/SHA1/test-vectors-80.txt b/testsuite/test_resources/hash/SHA1/test_vectors_80.txt similarity index 100% rename from testsuite/test-resources/other_hash/SHA1/test-vectors-80.txt rename to testsuite/test_resources/hash/SHA1/test_vectors_80.txt diff --git a/testsuite/test-resources/other_hash/SHA2/test-vectors-64.txt b/testsuite/test_resources/hash/SHA2/test_vectors_64.txt similarity index 100% rename from testsuite/test-resources/other_hash/SHA2/test-vectors-64.txt rename to testsuite/test_resources/hash/SHA2/test_vectors_64.txt diff --git a/testsuite/test-resources/sha3/SIMD/test-vectors-4.txt b/testsuite/test_resources/hash/SIMD/test_vectors_4.txt similarity index 100% rename from testsuite/test-resources/sha3/SIMD/test-vectors-4.txt rename to testsuite/test_resources/hash/SIMD/test_vectors_4.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-112.txt b/testsuite/test_resources/hash/Tangle/test_vectors_112.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-112.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_112.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-128.txt b/testsuite/test_resources/hash/Tangle/test_vectors_128.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-128.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_128.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-144.txt b/testsuite/test_resources/hash/Tangle/test_vectors_144.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-144.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_144.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-72.txt b/testsuite/test_resources/hash/Tangle/test_vectors_72.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-72.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_72.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-80.txt b/testsuite/test_resources/hash/Tangle/test_vectors_80.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-80.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_80.txt diff --git a/testsuite/test-resources/sha3/Tangle/test-vectors-96.txt b/testsuite/test_resources/hash/Tangle/test_vectors_96.txt similarity index 100% rename from testsuite/test-resources/sha3/Tangle/test-vectors-96.txt rename to testsuite/test_resources/hash/Tangle/test_vectors_96.txt diff --git a/testsuite/test-resources/other_hash/Tiger/test-vectors-24.txt b/testsuite/test_resources/hash/Tiger/test_vectors_24.txt similarity index 100% rename from testsuite/test-resources/other_hash/Tiger/test-vectors-24.txt rename to testsuite/test_resources/hash/Tiger/test_vectors_24.txt diff --git a/testsuite/test-resources/sha3/Twister/test-vectors-10.txt b/testsuite/test_resources/hash/Twister/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/sha3/Twister/test-vectors-10.txt rename to testsuite/test_resources/hash/Twister/test_vectors_10.txt diff --git a/testsuite/test-resources/sha3/Twister/test-vectors-9.txt b/testsuite/test_resources/hash/Twister/test_vectors_9.txt similarity index 100% rename from testsuite/test-resources/sha3/Twister/test-vectors-9.txt rename to testsuite/test_resources/hash/Twister/test_vectors_9.txt diff --git a/testsuite/test-resources/other_hash/Whirlpool/test-vectors-10.txt b/testsuite/test_resources/hash/Whirlpool/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/other_hash/Whirlpool/test-vectors-10.txt rename to testsuite/test_resources/hash/Whirlpool/test_vectors_10.txt diff --git a/testsuite/test-resources/estream/Chacha/test-vectors-12.txt b/testsuite/test_resources/stream_cipher/Chacha/test_vectors_12.txt similarity index 100% rename from testsuite/test-resources/estream/Chacha/test-vectors-12.txt rename to testsuite/test_resources/stream_cipher/Chacha/test_vectors_12.txt diff --git a/testsuite/test-resources/estream/Chacha/test-vectors-20.txt b/testsuite/test_resources/stream_cipher/Chacha/test_vectors_20.txt similarity index 100% rename from testsuite/test-resources/estream/Chacha/test-vectors-20.txt rename to testsuite/test_resources/stream_cipher/Chacha/test_vectors_20.txt diff --git a/testsuite/test-resources/estream/DECIM/test-vectors-8.txt b/testsuite/test_resources/stream_cipher/DECIM/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/estream/DECIM/test-vectors-8.txt rename to testsuite/test_resources/stream_cipher/DECIM/test_vectors_8.txt diff --git a/testsuite/test-resources/estream/Fubuki/test-vectors-4.txt b/testsuite/test_resources/stream_cipher/Fubuki/test_vectors_4.txt similarity index 100% rename from testsuite/test-resources/estream/Fubuki/test-vectors-4.txt rename to testsuite/test_resources/stream_cipher/Fubuki/test_vectors_4.txt diff --git a/testsuite/test-resources/estream/Grain/test-vectors-13.txt b/testsuite/test_resources/stream_cipher/Grain/test_vectors_13.txt similarity index 100% rename from testsuite/test-resources/estream/Grain/test-vectors-13.txt rename to testsuite/test_resources/stream_cipher/Grain/test_vectors_13.txt diff --git a/testsuite/test-resources/estream/Hermes/test-vectors-2.txt b/testsuite/test_resources/stream_cipher/Hermes/test_vectors_2.txt similarity index 100% rename from testsuite/test-resources/estream/Hermes/test-vectors-2.txt rename to testsuite/test_resources/stream_cipher/Hermes/test_vectors_2.txt diff --git a/testsuite/test-resources/estream/LEX/test-vectors-10.txt b/testsuite/test_resources/stream_cipher/LEX/test_vectors_10.txt similarity index 100% rename from testsuite/test-resources/estream/LEX/test-vectors-10.txt rename to testsuite/test_resources/stream_cipher/LEX/test_vectors_10.txt diff --git a/testsuite/test-resources/estream/MICKEY/test-vectors-8.txt b/testsuite/test_resources/stream_cipher/MICKEY/test_vectors_8.txt similarity index 100% rename from testsuite/test-resources/estream/MICKEY/test-vectors-8.txt rename to testsuite/test_resources/stream_cipher/MICKEY/test_vectors_8.txt diff --git a/testsuite/test-resources/estream/RC4/test-vectors-1.txt b/testsuite/test_resources/stream_cipher/RC4/test_vectors_1.txt similarity index 100% rename from testsuite/test-resources/estream/RC4/test-vectors-1.txt rename to testsuite/test_resources/stream_cipher/RC4/test_vectors_1.txt diff --git a/testsuite/test-resources/estream/Salsa20/test-vectors-20.txt b/testsuite/test_resources/stream_cipher/Salsa20/test_vectors_20.txt similarity index 100% rename from testsuite/test-resources/estream/Salsa20/test-vectors-20.txt rename to testsuite/test_resources/stream_cipher/Salsa20/test_vectors_20.txt diff --git a/testsuite/test-resources/estream/TSC-4/test-vectors-32.txt b/testsuite/test_resources/stream_cipher/TSC-4/test_vectors_32.txt similarity index 100% rename from testsuite/test-resources/estream/TSC-4/test-vectors-32.txt rename to testsuite/test_resources/stream_cipher/TSC-4/test_vectors_32.txt diff --git a/testsuite/test-resources/estream/Trivium/test-vectors-9.txt b/testsuite/test_resources/stream_cipher/Trivium/test_vectors_9.txt similarity index 100% rename from testsuite/test-resources/estream/Trivium/test-vectors-9.txt rename to testsuite/test_resources/stream_cipher/Trivium/test_vectors_9.txt diff --git a/testsuite/test_utils/block_test_case.cc b/testsuite/test_utils/block_test_case.cc new file mode 100644 index 00000000..df7fb5bb --- /dev/null +++ b/testsuite/test_utils/block_test_case.cc @@ -0,0 +1,103 @@ +#include "block_test_case.h" +#include +#include +#include +#include + +namespace testsuite { + +const json block_test_case::base_config = {{"type", "block"}, + {"init_frequency", "only_once"}, + {"plaintext", {{"type", "test_stream"}}}, + {"iv", {{"type", "test_stream"}}}, + {"key", {{"type", "test_stream"}}}}; + +void block_test_case::test() const { + std::unique_ptr encryptor = + block::make_block_cipher(_algorithm, _round, _plaintext.size(), _key.size(), true); + + encryptor->keysetup(_key.data(), _key.size()); + if (_load_iv) + encryptor->ivsetup(_iv.data(), _iv.size()); + + std::vector output_ciphertext(_ciphertext.size()); + encryptor->encrypt(_plaintext.data(), output_ciphertext.data()); + + ASSERT_EQ(output_ciphertext, _ciphertext); +} + +void block_test_case::test(std::unique_ptr &&block_stream) const { + std::unique_ptr decryptor = + block::make_block_cipher(_algorithm, _round, _plaintext.size(), _key.size(), false); + vec_cview ciphertext = block_stream->next(); + + decryptor->keysetup(_key.data(), _key.size()); + if (_load_iv) + decryptor->ivsetup(_iv.data(), _iv.size()); + + std::vector plain(_plaintext.size()); + decryptor->decrypt(ciphertext.data(), plain.data()); + + ASSERT_EQ(plain, _plaintext); +} + +void block_test_case::operator()() { + _test_vectors_tested = 0; + + while (_test_vectors >> *this) { + _test_vectors_tested++; + test(); // encryption + auto stream = prepare_stream(); + test_case::test(stream); // encryption streams + test(prepare_stream()); // decryption + } + + std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round + << "] is: " << _test_vectors_tested << std::endl; +} + +bool block_test_case::load_iv() const { + return _load_iv; +} + +std::unique_ptr block_test_case::prepare_stream() { + std::size_t block_size = _ciphertext.size(); + _stream_config["algorithm"] = _algorithm; + _stream_config["round"] = _round; + _stream_config["key"]["outputs"] = _key; + _stream_config["plaintext"]["outputs"] = _plaintext; + _stream_config["block_size"] = block_size; + _stream_config["key_size"] = _key.size(); + if (_load_iv) { + _stream_config["mode"] = ""; // TODO: write mode here + _stream_config["iv"]["outputs"] = _iv; + _stream_config["iv_size"] = _iv.size(); + } else { + _stream_config["iv"]["outputs"] = _iv; + } + + seed_seq_from seeder(seed1); + return make_stream(_stream_config, seeder, block_size); +} + +std::istream &operator>>(std::istream &input, block_test_case &test_case) { + std::string str_plaintext; + std::string str_ciphertext; + std::string str_iv; + std::string str_key; + + input >> str_key; + input >> str_plaintext; + input >> str_ciphertext; + if (test_case.load_iv()) + input >> str_iv; + + test_case.update_test_vector(hex_string_to_binary(str_plaintext), + hex_string_to_binary(str_ciphertext), + hex_string_to_binary(str_key), + hex_string_to_binary(str_iv)); + + return input; +} + +} // namespace testsuite diff --git a/testsuite/test_utils/block_test_case.h b/testsuite/test_utils/block_test_case.h new file mode 100644 index 00000000..3e359e3e --- /dev/null +++ b/testsuite/test_utils/block_test_case.h @@ -0,0 +1,103 @@ +#pragma once + +#include "test_case.h" +#include + +namespace testsuite { + +/** test case for functions from sha3 competition **/ +class block_test_case : test_case { + +private: + /** Private key for current test vector **/ + std::vector _key; + + /** Initialization vector for current test vector **/ + std::vector _iv; + + /** whether we use IV **/ + bool _load_iv; + + /** JSON configuration for making stream using make_stream() **/ + json _stream_config; + +public: + /** + * base JSON configuration which all estream functions has in common + * _stream_config is created based on this, but with addition of + * function specific properties e.g. name of function, number of rounds + */ + const static json base_config; + + block_test_case(const std::string &&algorithm, + const std::size_t round, + const bool load_iv = false) + : test_case(algorithm, round, "block") + , _iv(0) // empty IV if it is not used + , _load_iv(load_iv) + , _stream_config(base_config) {} + + /** Setter for single test vector **/ + void update_test_vector(const std::vector &&plaintext, + const std::vector &&ciphertext, + const std::vector &&key, + const std::vector &&iv) { + _plaintext = plaintext; + _ciphertext = ciphertext; + _key = key; + if (_load_iv) + _iv = iv; + } + + /** + * Create new instance of stream for current function + * First it creates JSON configuration of stream and + * then create new stream using make_stream function + * @return Pointer to instance of stream + */ + std::unique_ptr prepare_stream() override; + + /** + * Test raw function with current test vector + * @param encryptor + */ + void test() const; + + /** + * Test stream for current function with current test vector + * including decryption of output of stream + * @param encryptor Stream for current function + * @param decryptor Pointer to raw function instance + */ + void test(std::unique_ptr &&encryptor) const; + + /** + * test case is actually a functor which can done whole testing + * It is necessary to have function name and round number and it + * will step by step load all test vectors and test each with raw + * function and stream + */ + void operator()() override; + + /** + * Reads test vector from input stream + * + * STRUCTURE OF TEST VECTORS (All present in + * test-resources/block/{function-name}/test-vectors-{round-number}.txt) {key in hex} {plaintext + * in hex} {ciphertext in hex} {initialization-vector in hex} # Only in case _load_iv is true + * {newline} + * {key in hex} + * {plaintext in hex} + * {ciphertext in hex} + * {initialization-vector in hex} # Only in case _load_iv is true + * + * @param input stream + * @param test_case test_case instance + * @return input stream + */ + friend std::istream &operator>>(std::istream &input, block_test_case &test_case); + + bool load_iv() const; +}; + +} // namespace testsuite diff --git a/testsuite/test-utils/common_functions.cc b/testsuite/test_utils/common_functions.cc similarity index 90% rename from testsuite/test-utils/common_functions.cc rename to testsuite/test_utils/common_functions.cc index 8c2d0401..9a5aaf8b 100644 --- a/testsuite/test-utils/common_functions.cc +++ b/testsuite/test_utils/common_functions.cc @@ -10,10 +10,9 @@ value_type hex_to_bin(const char input) { if (input >= 'a' && input <= 'f') return input - 'a' + 10; throw std::invalid_argument("Invalid input string"); - } -std::vector hex_string_to_binary(const std::string& str) { +std::vector hex_string_to_binary(const std::string &str) { assert(str.length() % 2 == 0); std::vector output(str.length() / 2); diff --git a/testsuite/test-utils/common_functions.h b/testsuite/test_utils/common_functions.h similarity index 89% rename from testsuite/test-utils/common_functions.h rename to testsuite/test_utils/common_functions.h index c3549226..44a8f931 100644 --- a/testsuite/test-utils/common_functions.h +++ b/testsuite/test_utils/common_functions.h @@ -18,6 +18,6 @@ value_type hex_to_bin(const char input); * @param str String to convert * @attention Function works only for strings with even length */ -std::vector hex_string_to_binary(const std::string& str); +std::vector hex_string_to_binary(const std::string &str); } // namespace testsuite diff --git a/testsuite/test_utils/hash_test_case.cc b/testsuite/test_utils/hash_test_case.cc new file mode 100644 index 00000000..e78bdb66 --- /dev/null +++ b/testsuite/test_utils/hash_test_case.cc @@ -0,0 +1,85 @@ +#include "hash_test_case.h" +#include +#include +#include +#include +#include +#include +#include + +namespace testsuite { + +void hash_test_case::test() const { + std::unique_ptr hasher = + hash::hash_factory::create(_algorithm, unsigned(_round)); + std::size_t hash_size = _ciphertext.size(); + + std::vector hash(hash_size); + + using std::to_string; + + int status = hasher->Init(int(hash_size * 8)); + if (status != 0) + throw std::runtime_error("cannot initialize hash (code: " + to_string(status) + ")"); + + status = hasher->Update(_plaintext.data(), _length); + if (status != 0) + throw std::runtime_error("cannot update the hash (code: " + to_string(status) + ")"); + + status = hasher->Final(hash.data()); + if (status != 0) + throw std::runtime_error("cannot finalize the hash (code: " + to_string(status) + ")"); + + ASSERT_EQ(hash, _ciphertext); +} + +void hash_test_case::operator()() { + _test_vectors_tested = 0; + + while (_test_vectors >> *this) { + _test_vectors_tested++; + test(); + if (length() != 0 && + length() % 8 == + 0) { // unfortunately our sha3 streams are built so that it fits only multiple of 8 + auto stream = prepare_stream(); + test_case::test(stream); + } + } + + std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round + << "] is: " << _test_vectors_tested << std::endl; +} + +const size_t &hash_test_case::length() const { + return _length; +} + +std::unique_ptr hash_test_case::prepare_stream() { + std::size_t hash_size = _ciphertext.size(); + json config_instance = {{"type", "hash"}, {"source", {{"type", "test_stream"}}}}; + config_instance["algorithm"] = _algorithm; + config_instance["round"] = _round; + config_instance["input_size"] = _plaintext.size(); + config_instance["source"]["outputs"] = _plaintext; + config_instance["hash_size"] = hash_size; + + seed_seq_from seeder(seed1); + return make_stream(config_instance, seeder, hash_size); +} + +std::istream &operator>>(std::istream &input, hash_test_case &test_case) { + std::string str_plaintext; + std::string str_hash; + + input >> test_case._length; + input >> str_plaintext; + input >> str_hash; + + test_case.update_test_vector(hex_string_to_binary(str_plaintext), + hex_string_to_binary(str_hash)); + + return input; +} + +} // namespace testsuite diff --git a/testsuite/test_utils/hash_test_case.h b/testsuite/test_utils/hash_test_case.h new file mode 100644 index 00000000..020b0dfe --- /dev/null +++ b/testsuite/test_utils/hash_test_case.h @@ -0,0 +1,75 @@ +#pragma once + +#include "test_case.h" +#include +#include +#include + +#include + +namespace testsuite { + +class hash_test_case : test_case { +private: + /** + * base JSON configuration which all estream functions has in common + * _stream_config is created based on this, but with addition of + * function specific properties e.g. name of function, number of rounds + */ + const static json base_config; + +protected: + /** size of plaintext **/ + size_t _length; + +public: + const size_t &length() const; + + hash_test_case(const std::string &algorithm, const std::size_t round) + : test_case(algorithm, round, "hash") + , _length(0) {} + + /** + * Create new instance of stream for current function + * First it creates JSON configuration of stream and + * then create new stream using make_stream function + * @return Pointer to instance of stream + */ + std::unique_ptr prepare_stream() override; + + /** + * Test raw function with current test vector + * @param encryptor + */ + void test() const; + + /** + * test case is actually a functor which can done whole testing + * It is necessary to have function name and round number and it + * will step by step load all test vectors and test each with raw + * function and stream + */ + void operator()() override; + + /** Setter for single test vector **/ + void update_test_vector(const std::vector &&plaintext, + const std::vector &&hash) { + _plaintext = plaintext; + _ciphertext = hash; + } + + /** + * Reads test vector from input stream + * + * STRUCTURE OF TEST VECTORS (All present in + * test-resources/sha3/{function-name}/test-vectors-{round-number}.txt) {bitsize-of-plaintext} + * {plaintext in hex} {ciphertext in hex} {bitsize-of-plaintext2} {plaintext2 in hex} + * {ciphertext2 in hex} + * + * @param input stream + * @param test_case test_case instance + * @return input stream + */ + friend std::istream &operator>>(std::istream &input, hash_test_case &test_case); +}; +} // namespace testsuite diff --git a/testsuite/test_utils/stream_ciphers_test_case.cc b/testsuite/test_utils/stream_ciphers_test_case.cc new file mode 100644 index 00000000..d864bb6d --- /dev/null +++ b/testsuite/test_utils/stream_ciphers_test_case.cc @@ -0,0 +1,139 @@ +#include "stream_ciphers_test_case.h" +#include +#include +#include +#include + +namespace testsuite { + +const json stream_cipher_test_case::base_config = { + {"type", "stream_cipher"}, + {"plaintext", {{"type", "test_stream"}}}, + {"iv", {{"type", "test_stream"}}}, + {"key", {{"type", "test_stream"}}}, +}; + +void stream_cipher_test_case::test( + std::unique_ptr &encryptor) const { + size_t size = _plaintext.size(); + std::vector cipher(size); + + encryptor->keysetup(_key.data(), unsigned(_key.size() * 8), unsigned(_iv.size() * 8)); + encryptor->ivsetup(_iv.data()); + encryptor->encrypt_bytes(_plaintext.data(), cipher.data(), unsigned(_plaintext.size())); + + std::stringstream ss; + + for (auto byte : cipher) { + ss << std::setfill('0') << std::setw(2) << std::hex << int(byte); + } + + std::string ciphertext_str = ss.str(); + compare_ciphertext(ciphertext_str); +} + +unsigned long stream_cipher_test_case::block_size() const { + return _plaintext.size(); +} + +const json &stream_cipher_test_case::stream_config() const { + return _stream_config; +} + +std::unique_ptr stream_cipher_test_case::prepare_stream() { + _stream_config["algorithm"] = _algorithm; + _stream_config["round"] = _round; + _stream_config["plaintext"]["outputs"] = _plaintext; + _stream_config["iv"]["outputs"] = _iv; + _stream_config["key"]["outputs"] = _key; + _stream_config["block_size"] = block_size(); + + seed_seq_from seeder(seed1); + + return make_stream(_stream_config, seeder, block_size()); +} + +void stream_cipher_test_case::test( + std::unique_ptr &&encryptor, + std::unique_ptr &decryptor) const { + vec_cview ciphertext = encryptor->next(); + + std::vector plain(ciphertext.size()); + + decryptor->keysetup(_key.data(), _key.size() * 8, _iv.size() * 8); + decryptor->ivsetup(_iv.data()); + decryptor->decrypt_bytes(ciphertext.data(), plain.data(), ciphertext.size()); + + vec_cview plain_view = vec_cview(plain.cbegin(), plain.cend()); + ASSERT_EQ(plain_view.copy_to_vector(), _plaintext); +} + +void stream_cipher_test_case::operator()() { + _cipher->init(); + + _test_vectors_tested = 0; + while (_test_vectors >> *this) { + _test_vectors_tested++; + test(_cipher); + auto stream = prepare_stream(); + test_case::test(stream); + test(prepare_stream(), _cipher); + } + + std::cout << "Number of test vectors tested for function: \"" << _algorithm << "\"[" << _round + << "] is: " << _test_vectors_tested << std::endl; +} + +void stream_cipher_test_case::load_ciphertext(std::istream &stream) { + _ciphertext.clear(); + std::string cipher_line; + std::getline(stream, cipher_line); + // Hexadecimal to lower case, in order to be compatible with std::hex + std::transform(cipher_line.begin(), cipher_line.end(), cipher_line.begin(), ::tolower); + + if (std::any_of(cipher_line.cbegin(), cipher_line.cend(), isspace)) { + std::stringstream cipher_stream(cipher_line); + std::size_t position; + std::string ciphertext; + + while (cipher_stream >> position) { + cipher_stream >> ciphertext; + _ciphertext.push_back(std::make_tuple(position, ciphertext)); + } + + return; + } + _ciphertext.push_back(std::make_tuple(0, cipher_line)); +} + +void stream_cipher_test_case::compare_ciphertext(const std::string &actual) const { + for (auto tuple : _ciphertext) { + ASSERT_EQ(actual.substr(std::get<0>(tuple) * 2, std::get<1>(tuple).size()), + std::get<1>(tuple)); // *2 because sequence starts at std::get<0>(tuple) byte, but + // we are storing each byte on two bytes f. e. FF + } +} + +std::istream &operator>>(std::istream &input, stream_cipher_test_case &test_case) { + std::string plaintext; + std::string key; + std::string iv; + + input >> plaintext; + input >> key; + input >> iv; + + // fixes empty IV + if (iv == "empty") { + iv = ""; + } + + input.get(); // read the newline + test_case.load_ciphertext(input); + test_case.update_test_vector( + hex_string_to_binary(plaintext), hex_string_to_binary(key), hex_string_to_binary(iv)); + + return input; +} + +} // namespace testsuite diff --git a/testsuite/test_utils/stream_ciphers_test_case.h b/testsuite/test_utils/stream_ciphers_test_case.h new file mode 100644 index 00000000..6a7067bc --- /dev/null +++ b/testsuite/test_utils/stream_ciphers_test_case.h @@ -0,0 +1,136 @@ +#pragma once + +#include "common_functions.h" +#include "test_case.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace testsuite { +/** + * Class represents one test_case with one test vector + * class support testing of estream function directly including decryption + * and also testing of stream with + */ +class stream_cipher_test_case : test_case { + +private: + void compare_ciphertext(const std::string &actual) const; + + /** + * cipher text in form of tuples + * for long ciphertext testvectors contains more tuples, if whole ciphertext is given + * this structure contains only one tuple <0, ciphertext> + * **/ + std::vector> _ciphertext; + + /** Private key for current test vector **/ + std::vector _key; + + /** Initialization vector for current test vector **/ + std::vector _iv; + + /** JSON configuration for making stream using make_stream() **/ + json _stream_config; + + /** Pointer to raw function instance **/ + std::unique_ptr _cipher; + +public: + /** + * base JSON configuration which all estream functions has in common + * _stream_config is created based on this, but with addition of + * function specific properties e.g. name of function, number of rounds + */ + const static json base_config; + + stream_cipher_test_case(const std::string &&algorithm, const std::size_t round) + : test_case(algorithm, round, "stream_cipher") + , _stream_config(base_config) + , _cipher(stream_ciphers::create_stream_cipher(_algorithm, unsigned(_round))) {} + + /** Setter for single test vector **/ + void update_test_vector(const std::vector &&plaintext, + const std::vector &&key, + const std::vector &&iv) { + _plaintext = plaintext; + _key = key; + _iv = iv; + } + + /** + * Loads ciphertext from stream, stream must be in separate line and + * should be in format ciphertext, or index1 ciphertext_which_begins_in_index1 index2 ..... etc. + * @param stream to read from + */ + void load_ciphertext(std::istream &stream); + + /** Getter for current function configuration **/ + const json &stream_config() const; + + /** Getter for block size **/ + unsigned long block_size() const; + + /** + * Test raw function with current test vector + * @param encryptor + */ + void test(std::unique_ptr &encryptor) const; + + /** + * Test stream for current function with current test vector + * including decryption of output of stream + * @param encryptor Stream for current function + * @param decryptor Pointer to raw function instance + */ + void test(std::unique_ptr &&encryptor, + std::unique_ptr &decryptor) const; + + /** + * Create new instance of stream for current function + * First it creates JSON configuration of stream and + * then create new stream using make_stream function + * @return Pointer to instance of stream + */ + std::unique_ptr prepare_stream() override; + + /** + * test case is actually a functor which can done whole testing + * It is necessary to have function name and round number and it + * will step by step load all test vectors and test each with raw + * function and stream + */ + void operator()() override; + + /** + * Reads test vector from input stream + * + * STRUCTURE OF TEST VECTORS (All present in + * test-resources/estream/{function-name}/test-vectors-{round-number}.txt) {plaintext in hex} + * {key in hex} + * {initialization vector} + * {ciphertext} + * {plaintext2 in hex} + * {key2 in hex} + * {initialization vector2} + * {ciphertext2} + * + * STRUCTURE OF CIPHERTEXT + * two options + * - whole plaintext + * - tuples (index where ciphertext begins) (ciphretext) + * for example "0 FF 5 EE" means that first byte is FF and sixth byte is EE + * + * @param input stream + * @param test_case test_case instance + * @return input stream + */ + friend std::istream &operator>>(std::istream &input, stream_cipher_test_case &test_case); +}; + +} // namespace testsuite diff --git a/testsuite/test_utils/test_case.h b/testsuite/test_utils/test_case.h new file mode 100644 index 00000000..1a6145f1 --- /dev/null +++ b/testsuite/test_utils/test_case.h @@ -0,0 +1,103 @@ +// +// Created by mhajas on 7/27/17. +// + +#pragma once + +#include "common_functions.h" +#include "gtest/gtest.h" +#include +#include +#include +#include +#include + +namespace testsuite { + +/** seed is not actually used but some functions needs it as argument **/ +const static seed seed1 = seed::create("1fe40505e131963c"); + +/** + * this is abstract parent of all test cases, contains all functions and variables + * which cipher suites test vectors has in common e.g. plaintext, ciphertext, rounds etc. + */ +class test_case { + +protected: + /** + * Algorithm name, which is used in cipher suites factories so that it is possible to create + * instance from it + */ + const std::string _algorithm; + + /** Number of round of function for current test vectors **/ + const std::size_t _round; + + /** This counts number of test vectors tested in operator() **/ + std::uint64_t _test_vectors_tested = 0; + + /** File stream with test vectors **/ + std::ifstream _test_vectors; + + /** Plaintext, should be already converted from pure string to bytes representation **/ + std::vector _plaintext; + + /** cipher text in form of byte vector **/ + std::vector _ciphertext; + + /** + * Auxiliary function which generates filepath to testvectors for arguments + * @param algorithm + * @param round + * @param competition + * @return filepath + */ + static std::string get_test_vectors_filename(const std::string &algorithm, + const std::size_t round, + const std::string &competition) { + return "resources/test_resources/" + competition + "/" + algorithm + "/test_vectors_" + + std::to_string(round) + ".txt"; + } + +public: + test_case(const std::string &algorithm, const std::size_t round, const std::string &competition) + : _algorithm(algorithm) + , _round(round) + , _test_vectors(get_test_vectors_filename(algorithm, round, competition), + std::ifstream::in) { + if (!_test_vectors.is_open()) { + throw std::runtime_error("Not able to open file: \"" + + get_test_vectors_filename(algorithm, round, competition) + + "\""); + } + } + + /** + * Generates next output from stream and compare it with expected output + * @param s stream + */ + void test(std::unique_ptr &s) const { + vec_cview actual = s->next(); + ASSERT_EQ(make_cview(_ciphertext), actual); + } + + /** + * Create new instance of stream for current function + * First it creates JSON configuration of stream and + * then create new stream using make_stream function + * @return Pointer to instance of stream + */ + virtual std::unique_ptr prepare_stream() = 0; + + /** + * test case is actually a functor which can done whole testing + * It is necessary to have function name and round number and it + * will step by step load all test vectors and test each with raw + * function and stream + */ + virtual void operator()() = 0; + + virtual ~test_case() { _test_vectors.close(); } +}; + +} // namespace testsuite diff --git a/testsuite/test-utils/test_streams.cc b/testsuite/test_utils/test_streams.cc similarity index 98% rename from testsuite/test-utils/test_streams.cc rename to testsuite/test_utils/test_streams.cc index bdd93ace..b985fa60 100644 --- a/testsuite/test-utils/test_streams.cc +++ b/testsuite/test_utils/test_streams.cc @@ -1,9 +1,8 @@ #include "test_streams.h" - testsuite::test_stream::test_stream(const json &config) : stream(config.at("outputs").get>().size()) - , _data(config.at("outputs").get>()) { } + , _data(config.at("outputs").get>()) {} vec_cview testsuite::test_stream::next() { return make_cview(_data); diff --git a/testsuite/test_utils/test_streams.h b/testsuite/test_utils/test_streams.h new file mode 100644 index 00000000..c61121bc --- /dev/null +++ b/testsuite/test_utils/test_streams.h @@ -0,0 +1,19 @@ +#pragma once + +#include "common_functions.h" +#include "stream.h" + +namespace testsuite { + +/** Stream for testing which return values given by JSON configuration **/ +struct test_stream : stream { + test_stream(const json &config); + + vec_cview next() override; + +private: + /** view for the data, which is provided to next() **/ + std::vector _data; +}; + +} // namespace testsuite