1 // Copyright 2020 Google LLC
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 "mac_impl.h"
18
19 #include <string>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "tink/binary_keyset_writer.h"
24 #include "tink/cleartext_keyset_handle.h"
25 #include "tink/mac/mac_config.h"
26 #include "tink/mac/mac_key_templates.h"
27 #include "proto/testing_api.grpc.pb.h"
28
29 namespace crypto {
30 namespace tink {
31 namespace {
32
33 using ::crypto::tink::BinaryKeysetWriter;
34 using ::crypto::tink::CleartextKeysetHandle;
35 using ::crypto::tink::MacKeyTemplates;
36
37 using ::testing::IsEmpty;
38 using ::tink_testing_api::ComputeMacRequest;
39 using ::tink_testing_api::ComputeMacResponse;
40 using ::tink_testing_api::CreationRequest;
41 using ::tink_testing_api::CreationResponse;
42 using ::tink_testing_api::VerifyMacRequest;
43 using ::tink_testing_api::VerifyMacResponse;
44
45 using crypto::tink::KeysetHandle;
46 using google::crypto::tink::KeyTemplate;
47
ValidKeyset()48 std::string ValidKeyset() {
49 const KeyTemplate& key_template = MacKeyTemplates::HmacSha256();
50 auto handle_result = KeysetHandle::GenerateNew(key_template);
51 EXPECT_TRUE(handle_result.ok());
52 std::stringbuf keyset;
53 auto writer_result =
54 BinaryKeysetWriter::New(absl::make_unique<std::ostream>(&keyset));
55 EXPECT_TRUE(writer_result.ok());
56
57 auto status = CleartextKeysetHandle::Write(writer_result.value().get(),
58 *handle_result.value());
59 EXPECT_TRUE(status.ok());
60 return keyset.str();
61 }
62
63 class MacImplTest : public ::testing::Test {
64 protected:
SetUpTestSuite()65 static void SetUpTestSuite() { ASSERT_TRUE(MacConfig::Register().ok()); }
66 };
67
TEST_F(MacImplTest,CreateMacSuccess)68 TEST_F(MacImplTest, CreateMacSuccess) {
69 tink_testing_api::MacImpl mac;
70 std::string keyset = ValidKeyset();
71 CreationRequest request;
72 request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
73 CreationResponse response;
74
75 EXPECT_TRUE(mac.Create(nullptr, &request, &response).ok());
76 EXPECT_THAT(response.err(), IsEmpty());
77 }
78
TEST_F(MacImplTest,CreateMacFails)79 TEST_F(MacImplTest, CreateMacFails) {
80 tink_testing_api::MacImpl mac;
81 CreationRequest request;
82 request.mutable_annotated_keyset()->set_serialized_keyset("bad keyset");
83 CreationResponse response;
84
85 EXPECT_TRUE(mac.Create(nullptr, &request, &response).ok());
86 EXPECT_THAT(response.err(), Not(IsEmpty()));
87 }
88
TEST_F(MacImplTest,ComputeVerifySuccess)89 TEST_F(MacImplTest, ComputeVerifySuccess) {
90 tink_testing_api::MacImpl mac;
91 std::string keyset = ValidKeyset();
92 ComputeMacRequest comp_request;
93 comp_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
94 comp_request.set_data("some data");
95 ComputeMacResponse comp_response;
96
97 EXPECT_TRUE(mac.ComputeMac(nullptr, &comp_request, &comp_response).ok());
98 EXPECT_THAT(comp_response.err(), IsEmpty());
99
100 VerifyMacRequest verify_request;
101 verify_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
102 verify_request.set_mac_value(comp_response.mac_value());
103 verify_request.set_data("some data");
104 VerifyMacResponse verify_response;
105
106 EXPECT_TRUE(mac.VerifyMac(nullptr, &verify_request, &verify_response).ok());
107 EXPECT_THAT(verify_response.err(), IsEmpty());
108 }
109
TEST_F(MacImplTest,ComputeBadKeysetFail)110 TEST_F(MacImplTest, ComputeBadKeysetFail) {
111 tink_testing_api::MacImpl mac;
112 ComputeMacRequest comp_request;
113 comp_request.mutable_annotated_keyset()->set_serialized_keyset("bad keyset");
114 comp_request.set_data("some data");
115 ComputeMacResponse comp_response;
116
117 EXPECT_TRUE(mac.ComputeMac(nullptr, &comp_request, &comp_response).ok());
118 EXPECT_THAT(comp_response.err(), Not(IsEmpty()));
119 }
120
TEST_F(MacImplTest,VerifyBadCiphertextFail)121 TEST_F(MacImplTest, VerifyBadCiphertextFail) {
122 tink_testing_api::MacImpl mac;
123 std::string keyset = ValidKeyset();
124 VerifyMacRequest verify_request;
125 verify_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
126 verify_request.set_mac_value("bad mac value");
127 verify_request.set_data("some data");
128 VerifyMacResponse verify_response;
129
130 EXPECT_TRUE(mac.VerifyMac(nullptr, &verify_request, &verify_response).ok());
131 EXPECT_THAT(verify_response.err(), Not(IsEmpty()));
132 }
133
134 } // namespace
135 } // namespace tink
136 } // namespace crypto
137