xref: /aosp_15_r20/external/tink/cc/prf/hmac_prf_key_manager_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 "tink/prf/hmac_prf_key_manager.h"
18 
19 #include <sstream>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/status/status.h"
24 #include "tink/core/key_manager_impl.h"
25 #include "tink/prf/prf_set.h"
26 #include "tink/subtle/hmac_boringssl.h"
27 #include "tink/util/istream_input_stream.h"
28 #include "tink/util/secret_data.h"
29 #include "tink/util/status.h"
30 #include "tink/util/statusor.h"
31 #include "tink/util/test_matchers.h"
32 #include "proto/hmac_prf.pb.h"
33 
34 namespace crypto {
35 namespace tink {
36 
37 using ::crypto::tink::test::IsOk;
38 using ::crypto::tink::test::StatusIs;
39 using ::crypto::tink::util::IstreamInputStream;
40 using ::crypto::tink::util::StatusOr;
41 using ::google::crypto::tink::HashType;
42 using ::google::crypto::tink::HmacPrfKey;
43 using ::google::crypto::tink::HmacPrfKeyFormat;
44 using ::testing::HasSubstr;
45 using ::testing::Not;
46 using ::testing::SizeIs;
47 using ::testing::StartsWith;
48 
49 namespace {
50 
TEST(HmacPrfKeyManagerTest,Basics)51 TEST(HmacPrfKeyManagerTest, Basics) {
52   EXPECT_EQ(HmacPrfKeyManager().get_version(), 0);
53   EXPECT_EQ(HmacPrfKeyManager().get_key_type(),
54             "type.googleapis.com/google.crypto.tink.HmacPrfKey");
55   EXPECT_EQ(HmacPrfKeyManager().key_material_type(),
56             google::crypto::tink::KeyData::SYMMETRIC);
57 }
58 
TEST(HmacPrfKeyManagerTest,ValidateEmptyKey)59 TEST(HmacPrfKeyManagerTest, ValidateEmptyKey) {
60   EXPECT_THAT(HmacPrfKeyManager().ValidateKey(HmacPrfKey()), Not(IsOk()));
61 }
62 
TEST(HmacPrfKeyManagerTest,ValidateEmptyKeyFormat)63 TEST(HmacPrfKeyManagerTest, ValidateEmptyKeyFormat) {
64   EXPECT_THAT(HmacPrfKeyManager().ValidateKeyFormat(HmacPrfKeyFormat()),
65               Not(IsOk()));
66 }
67 
TEST(HmacPrfKeyManagerTest,ValidKeyFormat)68 TEST(HmacPrfKeyManagerTest, ValidKeyFormat) {
69   HmacPrfKeyFormat key_format;
70   key_format.mutable_params()->set_hash(HashType::SHA256);
71   key_format.set_key_size(16);
72   EXPECT_THAT(HmacPrfKeyManager().ValidateKeyFormat(key_format), IsOk());
73 }
74 
TEST(HmacPrfKeyManagerTest,InvalidKeyFormatShortKey)75 TEST(HmacPrfKeyManagerTest, InvalidKeyFormatShortKey) {
76   HmacPrfKeyFormat key_format;
77   key_format.mutable_params()->set_hash(HashType::SHA512);
78 
79   key_format.set_key_size(15);
80   EXPECT_THAT(HmacPrfKeyManager().ValidateKeyFormat(key_format), Not(IsOk()));
81 }
82 
TEST(HmacPrfKeyManagerTest,CreateKey)83 TEST(HmacPrfKeyManagerTest, CreateKey) {
84   HmacPrfKeyFormat key_format;
85   key_format.set_key_size(16);
86   key_format.mutable_params()->set_hash(HashType::SHA512);
87   auto hmac_key_or = HmacPrfKeyManager().CreateKey(key_format);
88   ASSERT_THAT(hmac_key_or, IsOk());
89   EXPECT_EQ(hmac_key_or.value().version(), 0);
90   EXPECT_EQ(hmac_key_or.value().params().hash(), key_format.params().hash());
91   EXPECT_THAT(hmac_key_or.value().key_value(), SizeIs(key_format.key_size()));
92 
93   EXPECT_THAT(HmacPrfKeyManager().ValidateKey(hmac_key_or.value()), IsOk());
94 }
95 
TEST(HmacPrfKeyManagerTest,ValidKey)96 TEST(HmacPrfKeyManagerTest, ValidKey) {
97   HmacPrfKey key;
98   key.set_version(0);
99 
100   key.mutable_params()->set_hash(HashType::SHA256);
101   key.set_key_value("0123456789abcdef");
102 
103   EXPECT_THAT(HmacPrfKeyManager().ValidateKey(key), IsOk());
104 }
105 
TEST(HmacPrfKeyManagerTest,ValidateKeyShortKey)106 TEST(HmacPrfKeyManagerTest, ValidateKeyShortKey) {
107   HmacPrfKey key;
108   key.set_version(0);
109 
110   key.mutable_params()->set_hash(HashType::SHA256);
111   key.set_key_value("0123456789abcde");
112 
113   EXPECT_THAT(HmacPrfKeyManager().ValidateKey(key), Not(IsOk()));
114 }
115 
TEST(HmacPrfKeyManagerTest,DeriveKey)116 TEST(HmacPrfKeyManagerTest, DeriveKey) {
117   HmacPrfKeyFormat format;
118   format.set_key_size(23);
119   format.set_version(0);
120   format.mutable_params()->set_hash(HashType::SHA256);
121 
122   IstreamInputStream input_stream{
123       absl::make_unique<std::stringstream>("0123456789abcdefghijklmnop")};
124 
125   StatusOr<HmacPrfKey> key_or =
126       HmacPrfKeyManager().DeriveKey(format, &input_stream);
127   ASSERT_THAT(key_or, IsOk());
128   EXPECT_EQ(key_or.value().key_value(), "0123456789abcdefghijklm");
129   EXPECT_EQ(key_or.value().params().hash(), format.params().hash());
130 }
131 
TEST(HmacPrfKeyManagerTest,DeriveKeyNotEnoughRandomness)132 TEST(HmacPrfKeyManagerTest, DeriveKeyNotEnoughRandomness) {
133   HmacPrfKeyFormat format;
134   format.set_key_size(17);
135   format.set_version(0);
136   format.mutable_params()->set_hash(HashType::SHA256);
137 
138   IstreamInputStream input_stream{
139       absl::make_unique<std::stringstream>("0123456789abcdef")};
140 
141   ASSERT_THAT(HmacPrfKeyManager().DeriveKey(format, &input_stream).status(),
142               Not(IsOk()));
143 }
144 
TEST(HmacPrfKeyManagerTest,DeriveKeyWrongVersion)145 TEST(HmacPrfKeyManagerTest, DeriveKeyWrongVersion) {
146   HmacPrfKeyFormat format;
147   format.set_key_size(16);
148   format.set_version(1);
149   format.mutable_params()->set_hash(HashType::SHA256);
150 
151   IstreamInputStream input_stream{
152       absl::make_unique<std::stringstream>("0123456789abcdef")};
153 
154   ASSERT_THAT(
155       HmacPrfKeyManager().DeriveKey(format, &input_stream).status(),
156       StatusIs(absl::StatusCode::kInvalidArgument, HasSubstr("version")));
157 }
158 
TEST(HmacPrfKeyManagerTest,GetPrimitive)159 TEST(HmacPrfKeyManagerTest, GetPrimitive) {
160   HmacPrfKeyFormat key_format;
161   key_format.mutable_params()->set_hash(HashType::SHA256);
162   key_format.set_key_size(16);
163   HmacPrfKey key = HmacPrfKeyManager().CreateKey(key_format).value();
164   auto manager_mac_or = HmacPrfKeyManager().GetPrimitive<Prf>(key);
165   ASSERT_THAT(manager_mac_or, IsOk());
166   auto prf_value_or = manager_mac_or.value()->Compute("some plaintext", 16);
167   ASSERT_THAT(prf_value_or, IsOk());
168 
169   auto direct_prf_or = subtle::HmacBoringSsl::New(
170       util::Enums::ProtoToSubtle(key.params().hash()), 16,
171       util::SecretDataFromStringView(key.key_value()));
172   ASSERT_THAT(direct_prf_or, IsOk());
173   auto direct_prf_value_or =
174       direct_prf_or.value()->ComputeMac("some plaintext");
175   ASSERT_THAT(direct_prf_value_or, IsOk());
176   EXPECT_THAT(direct_prf_value_or.value(), StartsWith(prf_value_or.value()));
177 }
178 
179 }  // namespace
180 }  // namespace tink
181 }  // namespace crypto
182