Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1609751 - Additional tests for mp_comba r=mt
Verify that when clamping, the upper 4 bytes of an `mp_digit` is checked.

Differential Revision: https://phabricator.services.mozilla.com/D58576

--HG--
extra : moz-landing-system : lando
  • Loading branch information
Kevin Jacobs committed Feb 19, 2020
1 parent 47f4fde commit b796543
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
49 changes: 49 additions & 0 deletions gtests/freebl_gtest/mpi_unittest.cc
Expand Up @@ -13,6 +13,7 @@
#include <mach/mach.h>
#endif

#include "mplogic.h"
#include "mpi.h"
namespace nss_test {

Expand Down Expand Up @@ -224,6 +225,54 @@ 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);
}
}

// This test is slow. Disable it by default so we can run these tests on CI.
class DISABLED_MPITest : public ::testing::Test {};

Expand Down
3 changes: 3 additions & 0 deletions lib/freebl/mpi/mplogic.h
Expand Up @@ -11,6 +11,7 @@
#define _H_MPLOGIC_

#include "mpi.h"
SEC_BEGIN_PROTOS

/*
The logical operations treat an mp_int as if it were a bit vector,
Expand Down Expand Up @@ -49,4 +50,6 @@ mp_err mpl_get_bit(const mp_int *a, mp_size bitNum);
mp_err mpl_get_bits(const mp_int *a, mp_size lsbNum, mp_size numBits);
mp_size mpl_significant_bits(const mp_int *a);

SEC_END_PROTOS

#endif /* end _H_MPLOGIC_ */

0 comments on commit b796543

Please sign in to comment.