ghash_unittest.cc 1.93 KB
Newer Older
1 2 3 4
// 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/.

5
#include "testvectors/gcm-vectors.h"
6
#include "gtest/gtest.h"
7
#include "util.h"
8 9 10 11 12

#include "gcm.h"

namespace nss_test {

13
class GHashTest : public ::testing::TestWithParam<AesGcmKatValue> {
14
 protected:
15
  void TestGHash(const AesGcmKatValue val, bool sw) {
16 17 18 19 20
    // Read test data.
    std::vector<uint8_t> hash_key = hex_string_to_bytes(val.hash_key);
    ASSERT_EQ(16UL, hash_key.size());
    std::vector<uint8_t> additional_data =
        hex_string_to_bytes(val.additional_data);
21 22 23
    std::vector<uint8_t> result = hex_string_to_bytes(val.result);
    std::vector<uint8_t> cipher_text(result.begin(), result.end() - 16);
    std::vector<uint8_t> expected = hex_string_to_bytes(val.ghash);
24 25 26 27 28 29 30 31 32
    ASSERT_EQ(16UL, expected.size());

    // Prepare context.
    gcmHashContext ghashCtx;
    ASSERT_EQ(SECSuccess, gcmHash_InitContext(&ghashCtx, hash_key.data(), sw));

    // Hash additional_data, cipher_text.
    gcmHash_Reset(&ghashCtx,
                  const_cast<const unsigned char *>(additional_data.data()),
33
                  additional_data.size());
34 35
    gcmHash_Update(&ghashCtx,
                   const_cast<const unsigned char *>(cipher_text.data()),
36
                   cipher_text.size());
37 38 39 40

    // Finalise (hash in the length).
    uint8_t result_bytes[16];
    unsigned int out_len;
41
    ASSERT_EQ(SECSuccess, gcmHash_Final(&ghashCtx, result_bytes, &out_len, 16));
42 43 44 45 46 47 48 49 50 51 52
    ASSERT_EQ(16U, out_len);
    EXPECT_EQ(expected, std::vector<uint8_t>(result_bytes, result_bytes + 16));
  }
};

#ifdef NSS_X86_OR_X64
TEST_P(GHashTest, KAT_X86_HW) { TestGHash(GetParam(), false); }
#endif
TEST_P(GHashTest, KAT_Sftw) { TestGHash(GetParam(), true); }

INSTANTIATE_TEST_CASE_P(NISTTestVector, GHashTest,
53
                        ::testing::ValuesIn(kGcmKatValues));
54 55

}  // nss_test