ghash_unittest.cc (1987B)
1 // This Source Code Form is subject to the terms of the Mozilla Public 2 // License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 // You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 #include "testvectors/gcm-vectors.h" 6 #include "gtest/gtest.h" 7 #include "util.h" 8 9 #include "gcm.h" 10 11 namespace nss_test { 12 13 class GHashTest : public ::testing::TestWithParam<AesGcmKatValue> { 14 protected: 15 void TestGHash(const AesGcmKatValue val, bool sw) { 16 // Read test data. 17 std::vector<uint8_t> hash_key = hex_string_to_bytes(val.hash_key); 18 ASSERT_EQ(16UL, hash_key.size()); 19 std::vector<uint8_t> additional_data = 20 hex_string_to_bytes(val.additional_data); 21 std::vector<uint8_t> result = hex_string_to_bytes(val.result); 22 std::vector<uint8_t> cipher_text(result.begin(), result.end() - 16); 23 std::vector<uint8_t> expected = hex_string_to_bytes(val.ghash); 24 ASSERT_EQ(16UL, expected.size()); 25 26 // Prepare context. 27 gcmHashContext ghashCtx; 28 ASSERT_EQ(SECSuccess, gcmHash_InitContext(&ghashCtx, hash_key.data(), sw)); 29 30 // Hash additional_data, cipher_text. 31 gcmHash_Reset(&ghashCtx, 32 const_cast<const unsigned char *>(additional_data.data()), 33 additional_data.size()); 34 gcmHash_Update(&ghashCtx, 35 const_cast<const unsigned char *>(cipher_text.data()), 36 cipher_text.size()); 37 38 // Finalise (hash in the length). 39 uint8_t result_bytes[16]; 40 unsigned int out_len; 41 ASSERT_EQ(SECSuccess, gcmHash_Final(&ghashCtx, result_bytes, &out_len, 16)); 42 ASSERT_EQ(16U, out_len); 43 EXPECT_EQ(expected, std::vector<uint8_t>(result_bytes, result_bytes + 16)); 44 } 45 }; 46 47 #ifdef NSS_X86_OR_X64 48 TEST_P(GHashTest, KAT_X86_HW) { TestGHash(GetParam(), false); } 49 #endif 50 TEST_P(GHashTest, KAT_Sftw) { TestGHash(GetParam(), true); } 51 52 INSTANTIATE_TEST_SUITE_P(NISTTestVector, GHashTest, 53 ::testing::ValuesIn(kGcmKatValues)); 54 55 } // namespace nss_test