From 4c3da8d6f09323f07ea850099a8151ecd0154a13 Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Wed, 16 Nov 2016 09:18:23 +0100 Subject: [PATCH] Bug 1317924 - resurrect mpi tests, r=ttaubert Differential Revision: https://nss-review.dev.mozaws.net/D68 --HG-- rename : lib/freebl/mpi/mpi-test.c => cmd/mpitests/mpi-test.c rename : gtests/util_gtest/util_gtest.gyp => cmd/mpitests/mpitests.gyp rename : lib/freebl/mpi/test-info.c => cmd/mpitests/test-info.c rename : gtests/util_gtest/util_gtest.gyp => gtests/freebl_gtest/freebl_gtest.gyp extra : rebase_source : 163b288b5fac99b04e7de8376628a5a6b0e3f1da extra : amend_source : 5850535f6d2114601a4bed890315b130245f28c5 --- {lib/freebl/mpi => cmd/mpitests}/mpi-test.c | 0 cmd/mpitests/mpitests.gyp | 30 +++++++ {lib/freebl/mpi => cmd/mpitests}/test-info.c | 7 +- gtests/freebl_gtest/freebl_gtest.gyp | 37 +++++++++ gtests/freebl_gtest/mpi_unittest.cc | 83 ++++++++++++++++++++ lib/freebl/freebl.gyp | 3 + lib/freebl/mpi/mpi.c | 24 ++++++ lib/freebl/mpi/mpi.h | 12 ++- lib/freebl/mpi/test-arrays.txt | 1 - nss.gyp | 6 ++ 10 files changed, 195 insertions(+), 8 deletions(-) rename {lib/freebl/mpi => cmd/mpitests}/mpi-test.c (100%) create mode 100644 cmd/mpitests/mpitests.gyp rename {lib/freebl/mpi => cmd/mpitests}/test-info.c (96%) create mode 100644 gtests/freebl_gtest/freebl_gtest.gyp create mode 100644 gtests/freebl_gtest/mpi_unittest.cc diff --git a/lib/freebl/mpi/mpi-test.c b/cmd/mpitests/mpi-test.c similarity index 100% rename from lib/freebl/mpi/mpi-test.c rename to cmd/mpitests/mpi-test.c diff --git a/cmd/mpitests/mpitests.gyp b/cmd/mpitests/mpitests.gyp new file mode 100644 index 0000000000..270b63ab84 --- /dev/null +++ b/cmd/mpitests/mpitests.gyp @@ -0,0 +1,30 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +{ + 'includes': [ + '../../coreconf/config.gypi', + '../../cmd/platlibs.gypi' + ], + 'targets': [ + { + 'target_name': 'mpi_tests', + 'type': 'executable', + 'sources': [ + 'mpi-test.c', + ], + 'dependencies': [ + '<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)', + ] + } + ], + 'target_defaults': { + 'include_dirs': [ + '<(DEPTH)/lib/freebl/mpi', + '<(DEPTH)/lib/util', + ] + }, + 'variables': { + 'module': 'nss' + } +} diff --git a/lib/freebl/mpi/test-info.c b/cmd/mpitests/test-info.c similarity index 96% rename from lib/freebl/mpi/test-info.c rename to cmd/mpitests/test-info.c index 6c6c4cea22..66386ff5d2 100644 --- a/lib/freebl/mpi/test-info.c +++ b/cmd/mpitests/test-info.c @@ -8,7 +8,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* Table mapping test suite names to index numbers */ -const int g_count = 42; +const int g_count = 41; const char *g_names[] = { "list", /* print out a list of the available test suites */ "copy", /* test assignment of mp-int structures */ @@ -31,7 +31,6 @@ const char *g_names[] = { "expt-digit", /* test digit exponentiation */ "expt", /* test full exponentiation */ "expt-2", /* test power-of-two exponentiation */ - "square-root", /* test integer square root function */ "modulo-digit", /* test digit modular reduction */ "modulo", /* test full modular reduction */ "mod-add", /* test modular addition */ @@ -76,7 +75,6 @@ int test_div(void); int test_expt_d(void); int test_expt(void); int test_2expt(void); -int test_sqrt(void); int test_mod_d(void); int test_mod(void); int test_addmod(void); @@ -105,7 +103,7 @@ int (*g_tests[])(void) = { test_add, test_sub_d, test_sub, test_mul_d, test_mul, test_sqr, test_div_d, test_div_2, test_div_2d, test_div, test_expt_d, test_expt, - test_2expt, test_sqrt, test_mod_d, test_mod, + test_2expt, test_mod_d, test_mod, test_addmod, test_submod, test_mulmod, test_sqrmod, test_exptmod, test_exptmod_d, test_invmod, test_cmp_d, test_cmp_z, test_cmp, test_cmp_mag, test_parity, @@ -136,7 +134,6 @@ const char *g_descs[] = { "test digit exponentiation", "test full exponentiation", "test power-of-two exponentiation", - "test integer square root function", "test digit modular reduction", "test full modular reduction", "test modular addition", diff --git a/gtests/freebl_gtest/freebl_gtest.gyp b/gtests/freebl_gtest/freebl_gtest.gyp new file mode 100644 index 0000000000..730520fca5 --- /dev/null +++ b/gtests/freebl_gtest/freebl_gtest.gyp @@ -0,0 +1,37 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +{ + 'includes': [ + '../../coreconf/config.gypi', + '../common/gtest.gypi', + ], + 'targets': [ + { + 'target_name': 'freebl_gtest', + 'type': 'executable', + 'sources': [ + 'mpi_unittest.cc', + '<(DEPTH)/gtests/common/gtests.cc' + ], + 'dependencies': [ + '<(DEPTH)/exports.gyp:nss_exports', + '<(DEPTH)/lib/freebl/freebl.gyp:<(freebl_name)', + '<(DEPTH)/gtests/google_test/google_test.gyp:gtest', + ], + 'defines': [ + 'CT_VERIF', + ], + } + ], + 'target_defaults': { + 'include_dirs': [ + '<(DEPTH)/gtests/google_test/gtest/include', + '<(DEPTH)/gtests/common', + '<(DEPTH)/lib/freebl/mpi', + ] + }, + 'variables': { + 'module': 'nss' + } +} diff --git a/gtests/freebl_gtest/mpi_unittest.cc b/gtests/freebl_gtest/mpi_unittest.cc new file mode 100644 index 0000000000..fa05c49827 --- /dev/null +++ b/gtests/freebl_gtest/mpi_unittest.cc @@ -0,0 +1,83 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this file, +// You can obtain one at http://mozilla.org/MPL/2.0/. + +#include "secdert.h" +#include "secitem.h" +#include "secport.h" + +#include "gtest/gtest.h" + +#include +#include +#include + +#include "mpi.h" +namespace nss_test { + +class MPITest : public ::testing::Test { + protected: + void TestCmp(const std::string a_string, const std::string b_string, + int result) { + mp_int a, b, c; + MP_DIGITS(&a) = 0; + MP_DIGITS(&b) = 0; + MP_DIGITS(&c) = 0; + ASSERT_EQ(MP_OKAY, mp_init(&a)); + ASSERT_EQ(MP_OKAY, mp_init(&b)); + + mp_read_radix(&a, a_string.c_str(), 16); + mp_read_radix(&b, b_string.c_str(), 16); + EXPECT_EQ(result, mp_cmp(&a, &b)); + } +}; + +TEST_F(MPITest, MpiCmp01Test) { TestCmp("0", "1", -1); } +TEST_F(MPITest, MpiCmp10Test) { TestCmp("1", "0", 1); } +TEST_F(MPITest, MpiCmp00Test) { TestCmp("0", "0", 0); } +TEST_F(MPITest, MpiCmp11Test) { TestCmp("1", "1", 0); } + +TEST_F(MPITest, MpiCmpConstTest) { + mp_int a, b, c; + MP_DIGITS(&a) = 0; + MP_DIGITS(&b) = 0; + MP_DIGITS(&c) = 0; + ASSERT_EQ(MP_OKAY, mp_init(&a)); + ASSERT_EQ(MP_OKAY, mp_init(&b)); + ASSERT_EQ(MP_OKAY, mp_init(&c)); + + mp_read_radix(&a, const_cast("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16); + mp_read_radix(&b, const_cast("FF0FFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551"), 16); + mp_read_radix(&c, const_cast("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632550"), 16); + + mp_taint(&b); + mp_taint(&c); + + uint32_t runs = 5000000; + uint32_t time_b = 0, time_c = 0; + for (uint32_t i = 0; i < runs; ++i) { + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + int r = mp_cmp(&a, &b); + clock_gettime(CLOCK_MONOTONIC, &end); + unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec; + used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec; + time_b += used; + ASSERT_EQ(1, r); + } + printf("time b: %u\n", time_b / runs); + + for (uint32_t i = 0; i < runs; ++i) { + struct timespec start, end; + clock_gettime(CLOCK_MONOTONIC, &start); + int r = mp_cmp(&a, &c); + clock_gettime(CLOCK_MONOTONIC, &end); + unsigned long long used = end.tv_sec * 1000000000L + end.tv_nsec; + used -= (unsigned long long)start.tv_sec * 1000000000L + start.tv_nsec; + time_c += used; + ASSERT_EQ(1, r); + } + printf("time c: %u\n", time_c / runs); +} + +} // nss_test diff --git a/lib/freebl/freebl.gyp b/lib/freebl/freebl.gyp index 7e14117e06..a3a49575e6 100644 --- a/lib/freebl/freebl.gyp +++ b/lib/freebl/freebl.gyp @@ -103,6 +103,9 @@ 'dependencies': [ '<(DEPTH)/lib/util/util.gyp:nssutil3', ], + 'defines': [ + 'CT_VERIF', + ], }], [ 'target_arch=="x64"', { 'sources': [ diff --git a/lib/freebl/mpi/mpi.c b/lib/freebl/mpi/mpi.c index 96e17456fe..f6f75439c1 100644 --- a/lib/freebl/mpi/mpi.c +++ b/lib/freebl/mpi/mpi.c @@ -37,6 +37,10 @@ #include "logtab.h" #endif +#ifdef CT_VERIF +#include +#endif + /* {{{ Constant strings */ /* Constant strings returned by mp_strerror() */ @@ -82,6 +86,26 @@ mp_set_prec(mp_size prec) /* }}} */ +#ifdef CT_VERIF +void +mp_taint(mp_int *mp) +{ + size_t i; + for (i = 0; i < mp->used; ++i) { + VALGRIND_MAKE_MEM_UNDEFINED(&(mp->dp[i]), sizeof(mp_digit)); + } +} + +void +mp_untaint(mp_int *mp) +{ + size_t i; + for (i = 0; i < mp->used; ++i) { + VALGRIND_MAKE_MEM_DEFINED(&(mp->dp[i]), sizeof(mp_digit)); + } +} +#endif + /*------------------------------------------------------------------------*/ /* {{{ mp_init(mp) */ diff --git a/lib/freebl/mpi/mpi.h b/lib/freebl/mpi/mpi.h index 3c4d78ba14..64ffe75d52 100644 --- a/lib/freebl/mpi/mpi.h +++ b/lib/freebl/mpi/mpi.h @@ -12,6 +12,9 @@ #include "mpi-config.h" +#include "seccomon.h" +SEC_BEGIN_PROTOS + #if MP_DEBUG #undef MP_IOFUNC #define MP_IOFUNC 1 @@ -273,7 +276,6 @@ void freebl_cpuid(unsigned long op, unsigned long *eax, if (MP_OKAY > (res = (x))) \ goto CLEANUP -#if defined(MP_API_COMPATIBLE) #define NEG MP_NEG #define ZPOS MP_ZPOS #define DIGIT_MAX MP_DIGIT_MAX @@ -300,6 +302,12 @@ void freebl_cpuid(unsigned long op, unsigned long *eax, #else #define ARGCHK(X, Y) /* */ #endif -#endif /* defined MP_API_COMPATIBLE */ + +#ifdef CT_VERIF +void mp_taint(mp_int *mp); +void mp_untaint(mp_int *mp); +#endif + +SEC_END_PROTOS #endif /* end _H_MPI_ */ diff --git a/lib/freebl/mpi/test-arrays.txt b/lib/freebl/mpi/test-arrays.txt index 76c4e3e99c..6c8908c1a1 100644 --- a/lib/freebl/mpi/test-arrays.txt +++ b/lib/freebl/mpi/test-arrays.txt @@ -33,7 +33,6 @@ divide:test_div:test full division expt-digit:test_expt_d:test digit exponentiation expt:test_expt:test full exponentiation expt-2:test_2expt:test power-of-two exponentiation -square-root:test_sqrt:test integer square root function modulo-digit:test_mod_d:test digit modular reduction modulo:test_mod:test full modular reduction mod-add:test_addmod:test modular addition diff --git a/nss.gyp b/nss.gyp index b6bd4940b2..44551d448d 100644 --- a/nss.gyp +++ b/nss.gyp @@ -195,6 +195,12 @@ 'cmd/pkix-errcodes/pkix-errcodes.gyp:pkix-errcodes', ], }], + [ 'test_build==1', { + 'dependencies': [ + 'cmd/mpitests/mpitests.gyp:mpi_tests', + 'gtests/freebl_gtest/freebl_gtest.gyp:freebl_gtest', + ], + }], ], }, ],