diff options
author | Moonchild <moonchild@palemoon.org> | 2020-12-23 19:02:52 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-12-23 19:02:52 +0000 |
commit | 029bcfe189eae5eebbaf58ccff4e1200dd78b228 (patch) | |
tree | 1c226a334ea1a88e2d1c6f949c9320eb0c3bff59 /security/nss/gtests/freebl_gtest/mpi_unittest.cc | |
parent | 149d2ffa779826cb48a381099858e76e4624d471 (diff) | |
download | UXP-029bcfe189eae5eebbaf58ccff4e1200dd78b228.tar UXP-029bcfe189eae5eebbaf58ccff4e1200dd78b228.tar.gz UXP-029bcfe189eae5eebbaf58ccff4e1200dd78b228.tar.lz UXP-029bcfe189eae5eebbaf58ccff4e1200dd78b228.tar.xz UXP-029bcfe189eae5eebbaf58ccff4e1200dd78b228.zip |
Issue #1693 - Update NSS to 3.59.1.1
This updates to MoonchildProductions/NSS@bd49b2b88 in the repo created for our
consumption of the library.
Diffstat (limited to 'security/nss/gtests/freebl_gtest/mpi_unittest.cc')
-rw-r--r-- | security/nss/gtests/freebl_gtest/mpi_unittest.cc | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/security/nss/gtests/freebl_gtest/mpi_unittest.cc b/security/nss/gtests/freebl_gtest/mpi_unittest.cc index 56b7454dc..0503006f9 100644 --- a/security/nss/gtests/freebl_gtest/mpi_unittest.cc +++ b/security/nss/gtests/freebl_gtest/mpi_unittest.cc @@ -13,6 +13,7 @@ #include <mach/mach.h> #endif +#include "mplogic.h" #include "mpi.h" namespace nss_test { @@ -191,6 +192,39 @@ TEST_F(MPITest, MpiFixlenOctetsZero) { TestToFixedOctets(zero, sizeof(mp_digit) + 1); } +TEST_F(MPITest, MpiRadixSizeNeg) { + char* str; + mp_int a; + mp_err rv; + const char* negative_edge = + "-5400000000000000003000000002200020090919017007777777777870000090" + "00000000007500443416610000000000000000000000000000000000000000000" + "00000000000000000000000000000000000000000000000000000000075049054" + "18610000800555594485440016000031555550000000000000000220030200909" + "19017007777777700000000000000000000000000000000000000000000000000" + "00000000000500000000000000000000000000004668129841661000071000000" + "00000000000000000000000000000000000000000000000007504434166100000" + "00000000000000000000000000000000000000000000000000000000000000000" + "00000000075049054186100008005555944854400184572169555500000000000" + "0000022003020090919017007777777700000000000000000000"; + + rv = mp_init(&a); + ASSERT_EQ(MP_OKAY, rv); + rv = mp_read_variable_radix(&a, negative_edge, 10); + ASSERT_EQ(MP_OKAY, rv); + + const int radixSize = mp_radix_size(&a, 10); + ASSERT_LE(0, radixSize); + + str = (char*)malloc(radixSize); + ASSERT_NE(nullptr, str); + rv = mp_toradix(&a, str, 10); + ASSERT_EQ(MP_OKAY, rv); + ASSERT_EQ(0, strcmp(negative_edge, str)); + free(str); + mp_clear(&a); +} + TEST_F(MPITest, MpiFixlenOctetsVarlen) { std::vector<uint8_t> packed; for (size_t i = 0; i < sizeof(mp_digit) * 2; ++i) { @@ -224,6 +258,92 @@ TEST_F(MPITest, MpiFixlenOctetsTooSmall) { } } +TEST_F(MPITest, MpiSqrMulClamp) { + mp_int a, r, expect; + MP_DIGITS(&a) = 0; + MP_DIGITS(&r) = 0; + MP_DIGITS(&expect) = 0; + + // Comba32 result is 64 mp_digits. *=2 as this is an ascii representation. + std::string expect_str((64 * sizeof(mp_digit)) * 2, '0'); + + // Set second-highest bit (0x80...^2 == 0x4000...) + expect_str.replace(0, 1, "4", 1); + + // Test 32, 16, 8, and 4-1 mp_digit values. 32-4 (powers of two) use the comba + // assembly implementation, if enabled and supported. 3-1 use non-comba. + int n_digits = 32; + while (n_digits > 0) { + ASSERT_EQ(MP_OKAY, mp_init(&r)); + ASSERT_EQ(MP_OKAY, mp_init(&a)); + ASSERT_EQ(MP_OKAY, mp_init(&expect)); + ASSERT_EQ(MP_OKAY, mp_read_radix(&expect, expect_str.c_str(), 16)); + + ASSERT_EQ(MP_OKAY, mp_set_int(&a, 1)); + ASSERT_EQ(MP_OKAY, mpl_lsh(&a, &a, (n_digits * sizeof(mp_digit) * 8) - 1)); + + ASSERT_EQ(MP_OKAY, mp_sqr(&a, &r)); + EXPECT_EQ(MP_USED(&expect), MP_USED(&r)); + EXPECT_EQ(0, mp_cmp(&r, &expect)); + mp_clear(&r); + + // Take the mul path... + ASSERT_EQ(MP_OKAY, mp_init(&r)); + ASSERT_EQ(MP_OKAY, mp_mul(&a, &a, &r)); + EXPECT_EQ(MP_USED(&expect), MP_USED(&r)); + EXPECT_EQ(0, mp_cmp(&r, &expect)); + + mp_clear(&a); + mp_clear(&r); + mp_clear(&expect); + + // Once we're down to 4, check non-powers of two. + int sub = n_digits > 4 ? n_digits / 2 : 1; + n_digits -= sub; + + // "Shift right" the string (to avoid mutating |expect_str| with MPI). + expect_str.resize(expect_str.size() - 2 * 2 * sizeof(mp_digit) * sub); + } +} + +TEST_F(MPITest, MpiInvModLoop) { + mp_int a; + mp_int m; + mp_int c_actual; + mp_int c_expect; + MP_DIGITS(&a) = 0; + MP_DIGITS(&m) = 0; + MP_DIGITS(&c_actual) = 0; + MP_DIGITS(&c_expect) = 0; + ASSERT_EQ(MP_OKAY, mp_init(&a)); + ASSERT_EQ(MP_OKAY, mp_init(&m)); + ASSERT_EQ(MP_OKAY, mp_init(&c_actual)); + ASSERT_EQ(MP_OKAY, mp_init(&c_expect)); + mp_read_radix(&a, + "3e10b9f4859fb9e8150cc0d94e83ef428d655702a0b6fb1e684f4755eb6be6" + "5ac6048cdfc533f73a9bad76125801051f", + 16); + mp_read_radix(&m, + "ffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372d" + "df581a0db248b0a77aecec196accc52973", + 16); + mp_read_radix(&c_expect, + "12302214814361c15ab6c0f2131150af186099f8c22f6c9d6e77ad496b551c" + "7c8039e61098bfe2af66474420659435c6", + 16); + + int rv = mp_invmod(&a, &m, &c_actual); + ASSERT_EQ(MP_OKAY, rv); + + rv = mp_cmp(&c_actual, &c_expect); + EXPECT_EQ(0, rv); + + mp_clear(&a); + mp_clear(&m); + mp_clear(&c_actual); + mp_clear(&c_expect); +} + // This test is slow. Disable it by default so we can run these tests on CI. class DISABLED_MPITest : public ::testing::Test {}; |