From a6c4a358dfb3b3319c3f5f643e795f0ddc14d070 Mon Sep 17 00:00:00 2001 From: Never Date: Thu, 16 Feb 2017 15:44:45 +0100 Subject: Add ecdsa timing test --- src/extra_tests/timing/timing-tests/Makefile | 2 +- src/extra_tests/timing/timing-tests/TimingTest.h | 19 ++++++++++ src/extra_tests/timing/timing-tests/data/ecdsa.vec | 4 +++ src/extra_tests/timing/timing-tests/ecdsa.cpp | 41 ++++++++++++++++++++++ src/extra_tests/timing/timing-tests/main.cpp | 6 ++++ 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/extra_tests/timing/timing-tests/data/ecdsa.vec create mode 100644 src/extra_tests/timing/timing-tests/ecdsa.cpp diff --git a/src/extra_tests/timing/timing-tests/Makefile b/src/extra_tests/timing/timing-tests/Makefile index df77c70ec..56cc7b9eb 100644 --- a/src/extra_tests/timing/timing-tests/Makefile +++ b/src/extra_tests/timing/timing-tests/Makefile @@ -1,5 +1,5 @@ CC=g++ -BOTAN_DIR=../../../../../botan/ +BOTAN_DIR=../../../../ LDIR=$(BOTAN_DIR) IDIR=$(BOTAN_DIR)build/include DBG_FLAGS=-g -O3 diff --git a/src/extra_tests/timing/timing-tests/TimingTest.h b/src/extra_tests/timing/timing-tests/TimingTest.h index a61e21519..a0295c6fd 100644 --- a/src/extra_tests/timing/timing-tests/TimingTest.h +++ b/src/extra_tests/timing/timing-tests/TimingTest.h @@ -19,6 +19,8 @@ #include #include #include +#include +#include using namespace Botan; @@ -97,6 +99,23 @@ class Lucky13Test : public TimingTest Lucky13Test(std::vector &inputs, std::string result_file, const std::string& mac_name, size_t mac_keylen); }; + +class ECDSATest : public TimingTest + { + private: + const ECDSA_PrivateKey m_privkey; + const BigInt m_order; + Blinded_Point_Multiply m_base_point; + const BigInt m_x; + const Modular_Reducer m_mod_order; + + protected: + std::vector prepare_input(std::string input) override; + ticks measure_critical_function(std::vector input) override; + + public: + ECDSATest(std::vector &inputs, std::string result_folder, std::string ecgroup); + }; #endif /* TIMINGTEST_H */ \ No newline at end of file diff --git a/src/extra_tests/timing/timing-tests/data/ecdsa.vec b/src/extra_tests/timing/timing-tests/data/ecdsa.vec new file mode 100644 index 000000000..a940a3c20 --- /dev/null +++ b/src/extra_tests/timing/timing-tests/data/ecdsa.vec @@ -0,0 +1,4 @@ +# leading zeros +01 +# no leading zeros +FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF \ No newline at end of file diff --git a/src/extra_tests/timing/timing-tests/ecdsa.cpp b/src/extra_tests/timing/timing-tests/ecdsa.cpp new file mode 100644 index 000000000..42f281911 --- /dev/null +++ b/src/extra_tests/timing/timing-tests/ecdsa.cpp @@ -0,0 +1,41 @@ +/* + * File: ecdsa.cpp + * + * + */ + +#include "TimingTest.h" + +ECDSATest::ECDSATest(std::vector &inputs, std::string result_folder, std::string ecgroup) : + m_privkey(system_rng(), Botan::EC_Group(ecgroup)), + m_order(m_privkey.domain().get_order()), + m_base_point(m_privkey.domain().get_base_point(), m_order), + m_x(m_privkey.private_value()), + m_mod_order(m_order) + { + m_inputs = inputs; + m_result_folder = result_folder; + } + +std::vector ECDSATest::prepare_input(std::string input) + { + const std::vector input_vector = Botan::hex_decode(input); + return input_vector; + } + +ticks ECDSATest::measure_critical_function(std::vector input) + { + const BigInt k(input.data(), input.size()); + const BigInt msg(system_rng(), m_order.bits()); + + ticks start = this->get_ticks(); + + //The following ECDSA operations involve and should not leak any information about k. + const Botan::PointGFp k_times_P = m_base_point.blinded_multiply(k, system_rng()); + const BigInt r = m_mod_order.reduce(k_times_P.get_affine_x()); + const BigInt s = m_mod_order.multiply(inverse_mod(k, m_order), mul_add(m_x, r, msg)); + + ticks end = get_ticks(); + + return (end - start); + } diff --git a/src/extra_tests/timing/timing-tests/main.cpp b/src/extra_tests/timing/timing-tests/main.cpp index 7b9e2b0fc..02742da84 100644 --- a/src/extra_tests/timing/timing-tests/main.cpp +++ b/src/extra_tests/timing/timing-tests/main.cpp @@ -130,6 +130,12 @@ int main(int argc, char* argv[]) std::string result_folder_sha384 = "results/lucky13sha384"; std::unique_ptr test_sha384(new Lucky13Test(inputs, result_folder_sha384, "SHA-384", 48)); test_sha384->execute_evaluation(); + } + else if (executeEvaluationWithFile("ecdsa", file, test_arg)) + { + std::string result_folder_ecdsa = "results/ecdsa"; + std::unique_ptr test_ecdsa(new ECDSATest(inputs, result_folder_ecdsa, "secp384r1")); + test_ecdsa->execute_evaluation(); } else { -- cgit v1.2.3