xref: /aosp_15_r20/external/tink/cc/prf/prf_key_templates_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 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/prf_key_templates.h"
18 
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/memory/memory.h"
22 #include "tink/prf/aes_cmac_prf_key_manager.h"
23 #include "tink/prf/hkdf_prf_key_manager.h"
24 #include "tink/prf/hmac_prf_key_manager.h"
25 #include "tink/util/test_matchers.h"
26 #include "proto/aes_cmac_prf.pb.h"
27 #include "proto/hmac_prf.pb.h"
28 
29 namespace crypto {
30 namespace tink {
31 
32 namespace {
33 
34 using ::crypto::tink::test::IsOk;
35 using ::google::crypto::tink::HkdfPrfKeyFormat;
36 using ::testing::Eq;
37 using ::testing::Ref;
38 
TEST(HkdfSha256HkdfTest,Basics)39 TEST(HkdfSha256HkdfTest, Basics) {
40   EXPECT_THAT(PrfKeyTemplates::HkdfSha256().type_url(),
41               Eq("type.googleapis.com/google.crypto.tink.HkdfPrfKey"));
42   EXPECT_THAT(PrfKeyTemplates::HkdfSha256().type_url(),
43               Eq(HkdfPrfKeyManager().get_key_type()));
44 }
45 
TEST(HkdfSha256HkdfTest,OutputPrefixType)46 TEST(HkdfSha256HkdfTest, OutputPrefixType) {
47   EXPECT_THAT(PrfKeyTemplates::HkdfSha256().output_prefix_type(),
48               Eq(google::crypto::tink::OutputPrefixType::RAW));
49 }
50 
TEST(HkdfSha256HkdfTest,MultipleCallsSameReference)51 TEST(HkdfSha256HkdfTest, MultipleCallsSameReference) {
52   EXPECT_THAT(PrfKeyTemplates::HkdfSha256(),
53               Ref(PrfKeyTemplates::HkdfSha256()));
54 }
55 
TEST(HkdfSha256HkdfTest,WorksWithKeyTypeManager)56 TEST(HkdfSha256HkdfTest, WorksWithKeyTypeManager) {
57   const google::crypto::tink::KeyTemplate& key_template =
58       PrfKeyTemplates::HkdfSha256();
59   HkdfPrfKeyFormat key_format;
60   EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
61   EXPECT_THAT(HkdfPrfKeyManager().ValidateKeyFormat(key_format), IsOk());
62 }
63 
TEST(HmacPrfTest,Basics)64 TEST(HmacPrfTest, Basics) {
65   EXPECT_THAT(PrfKeyTemplates::HmacSha256().type_url(),
66               Eq("type.googleapis.com/google.crypto.tink.HmacPrfKey"));
67   EXPECT_THAT(PrfKeyTemplates::HmacSha512().type_url(),
68               Eq("type.googleapis.com/google.crypto.tink.HmacPrfKey"));
69   auto manager = absl::make_unique<HmacPrfKeyManager>();
70   EXPECT_THAT(PrfKeyTemplates::HmacSha256().type_url(),
71               Eq(manager->get_key_type()));
72   google::crypto::tink::HmacPrfKeyFormat format;
73   ASSERT_TRUE(format.ParseFromString(PrfKeyTemplates::HmacSha256().value()));
74   EXPECT_THAT(manager->ValidateKeyFormat(format), IsOk());
75   ASSERT_TRUE(format.ParseFromString(PrfKeyTemplates::HmacSha512().value()));
76   EXPECT_THAT(manager->ValidateKeyFormat(format), IsOk());
77 }
78 
TEST(HmacPrfTest,OutputPrefixType)79 TEST(HmacPrfTest, OutputPrefixType) {
80   EXPECT_THAT(PrfKeyTemplates::HmacSha256().output_prefix_type(),
81               Eq(google::crypto::tink::OutputPrefixType::RAW));
82   EXPECT_THAT(PrfKeyTemplates::HmacSha512().output_prefix_type(),
83               Eq(google::crypto::tink::OutputPrefixType::RAW));
84 }
85 
TEST(HmacPrfTest,MultipleCallsSameReference)86 TEST(HmacPrfTest, MultipleCallsSameReference) {
87   EXPECT_THAT(PrfKeyTemplates::HmacSha256(),
88               Ref(PrfKeyTemplates::HmacSha256()));
89   EXPECT_THAT(PrfKeyTemplates::HmacSha512(),
90               Ref(PrfKeyTemplates::HmacSha512()));
91 }
92 
TEST(CmacPrfTest,Basics)93 TEST(CmacPrfTest, Basics) {
94   EXPECT_THAT(PrfKeyTemplates::AesCmac().type_url(),
95               Eq("type.googleapis.com/google.crypto.tink.AesCmacPrfKey"));
96   auto manager = absl::make_unique<AesCmacPrfKeyManager>();
97   EXPECT_THAT(PrfKeyTemplates::AesCmac().type_url(),
98               Eq(manager->get_key_type()));
99   google::crypto::tink::AesCmacPrfKeyFormat format;
100   ASSERT_TRUE(format.ParseFromString(PrfKeyTemplates::AesCmac().value()));
101   EXPECT_THAT(manager->ValidateKeyFormat(format), IsOk());
102 }
103 
TEST(CmacPrfTest,OutputPrefixType)104 TEST(CmacPrfTest, OutputPrefixType) {
105   EXPECT_THAT(PrfKeyTemplates::AesCmac().output_prefix_type(),
106               Eq(google::crypto::tink::OutputPrefixType::RAW));
107 }
108 
TEST(CmacPrfTest,MultipleCallsSameReference)109 TEST(CmacPrfTest, MultipleCallsSameReference) {
110   EXPECT_THAT(PrfKeyTemplates::AesCmac(), Ref(PrfKeyTemplates::AesCmac()));
111 }
112 
113 }  // namespace
114 }  // namespace tink
115 }  // namespace crypto
116