xref: /aosp_15_r20/external/tink/cc/mac/mac_factory_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ////////////////////////////////////////////////////////////////////////////////
16 
17 #include "tink/mac/mac_factory.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include "gtest/gtest.h"
23 #include "tink/crypto_format.h"
24 #include "tink/internal/key_info.h"
25 #include "tink/keyset_handle.h"
26 #include "tink/mac.h"
27 #include "tink/mac/hmac_key_manager.h"
28 #include "tink/mac/mac_config.h"
29 #include "tink/util/status.h"
30 #include "tink/util/test_keyset_handle.h"
31 #include "tink/util/test_util.h"
32 #include "proto/common.pb.h"
33 #include "proto/hmac.pb.h"
34 #include "proto/tink.pb.h"
35 
36 using crypto::tink::test::AddRawKey;
37 using crypto::tink::test::AddTinkKey;
38 using google::crypto::tink::HashType;
39 using google::crypto::tink::HmacKeyFormat;
40 using google::crypto::tink::KeyData;
41 using google::crypto::tink::Keyset;
42 using google::crypto::tink::KeyStatusType;
43 
44 
45 namespace crypto {
46 namespace tink {
47 namespace {
48 
49 class MacFactoryTest : public ::testing::Test {
50 };
51 
TEST_F(MacFactoryTest,testBasic)52 TEST_F(MacFactoryTest, testBasic) {
53   Keyset keyset;
54   auto mac_result =
55       MacFactory::GetPrimitive(*TestKeysetHandle::GetKeysetHandle(keyset));
56   EXPECT_FALSE(mac_result.ok());
57   EXPECT_EQ(absl::StatusCode::kInvalidArgument, mac_result.status().code());
58   EXPECT_PRED_FORMAT2(testing::IsSubstring, "at least one key",
59                       std::string(mac_result.status().message()));
60 }
61 
TEST_F(MacFactoryTest,testPrimitive)62 TEST_F(MacFactoryTest, testPrimitive) {
63   // Prepare a format for generating keys for a Keyset.
64   HmacKeyManager key_type_manager;
65   auto key_manager = internal::MakeKeyManager<Mac>(&key_type_manager);
66   const KeyFactory& key_factory = key_manager->get_key_factory();
67   std::string key_type = key_manager->get_key_type();
68 
69   HmacKeyFormat key_format;
70   key_format.set_key_size(16);
71   key_format.mutable_params()->set_tag_size(10);
72   key_format.mutable_params()->set_hash(HashType::SHA256);
73 
74   // Prepare a Keyset.
75   Keyset keyset;
76   uint32_t key_id_1 = 1234543;
77   auto new_key = std::move(key_factory.NewKey(key_format).value());
78   AddTinkKey(key_type, key_id_1, *new_key, KeyStatusType::ENABLED,
79              KeyData::SYMMETRIC, &keyset);
80 
81   uint32_t key_id_2 = 726329;
82   new_key = std::move(key_factory.NewKey(key_format).value());
83   AddRawKey(key_type, key_id_2, *new_key, KeyStatusType::ENABLED,
84             KeyData::SYMMETRIC, &keyset);
85 
86   uint32_t key_id_3 = 7213743;
87   new_key = std::move(key_factory.NewKey(key_format).value());
88   AddTinkKey(key_type, key_id_3, *new_key, KeyStatusType::ENABLED,
89              KeyData::SYMMETRIC, &keyset);
90 
91   keyset.set_primary_key_id(key_id_3);
92 
93   // Initialize the registry.
94   ASSERT_TRUE(MacConfig::Register().ok());;
95 
96   // Create a KeysetHandle and use it with the factory.
97   auto mac_result =
98       MacFactory::GetPrimitive(*TestKeysetHandle::GetKeysetHandle(keyset));
99   EXPECT_TRUE(mac_result.ok()) << mac_result.status();
100   auto mac = std::move(mac_result.value());
101 
102   // Test the resulting Mac-instance.
103   std::string data = "some_data_for_mac";
104 
105   auto compute_mac_result = mac->ComputeMac(data);
106   EXPECT_TRUE(compute_mac_result.ok()) << compute_mac_result.status();
107   std::string mac_value = compute_mac_result.value();
108   std::string prefix =
109       CryptoFormat::GetOutputPrefix(KeyInfoFromKey(keyset.key(2))).value();
110   EXPECT_PRED_FORMAT2(testing::IsSubstring, prefix, mac_value);
111 
112   util::Status status = mac->VerifyMac(mac_value, data);
113   EXPECT_TRUE(status.ok()) << status;
114 
115   status = mac->VerifyMac(mac_value, "bad data for mac");
116   EXPECT_FALSE(status.ok());
117   EXPECT_EQ(absl::StatusCode::kInvalidArgument, status.code());
118   EXPECT_PRED_FORMAT2(testing::IsSubstring, "verification failed",
119                       std::string(status.message()));
120 
121   status = mac->VerifyMac("some bad mac value", data);
122   EXPECT_FALSE(status.ok());
123   EXPECT_EQ(absl::StatusCode::kInvalidArgument, status.code());
124   EXPECT_PRED_FORMAT2(testing::IsSubstring, "verification failed",
125                       std::string(status.message()));
126 
127   // Create raw MAC value with 2nd key, and verify with Mac-instance.
128   auto raw_mac =
129       std::move(key_manager->GetPrimitive(keyset.key(1).key_data()).value());
130   std::string raw_mac_value = raw_mac->ComputeMac(data).value();
131   status = mac->VerifyMac(raw_mac_value, data);
132   EXPECT_TRUE(status.ok()) << status;
133 }
134 
135 }  // namespace
136 }  // namespace tink
137 }  // namespace crypto
138