1 // Copyright 2018 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/daead/deterministic_aead_key_templates.h"
18
19 #include <string>
20
21 #include "gtest/gtest.h"
22 #include "tink/core/key_manager_impl.h"
23 #include "tink/daead/aes_siv_key_manager.h"
24 #include "proto/aes_siv.pb.h"
25 #include "proto/common.pb.h"
26 #include "proto/tink.pb.h"
27
28 using google::crypto::tink::AesSivKeyFormat;
29 using google::crypto::tink::KeyTemplate;
30 using google::crypto::tink::OutputPrefixType;
31
32 namespace crypto {
33 namespace tink {
34 namespace {
35
TEST(DeterministicAeadKeyTemplatesTest,testAesSivKeyTemplates)36 TEST(DeterministicAeadKeyTemplatesTest, testAesSivKeyTemplates) {
37 std::string type_url = "type.googleapis.com/google.crypto.tink.AesSivKey";
38
39 { // Test Aes256Siv().
40 // Check that returned template is correct.
41 const KeyTemplate& key_template =
42 DeterministicAeadKeyTemplates::Aes256Siv();
43 EXPECT_EQ(type_url, key_template.type_url());
44 EXPECT_EQ(OutputPrefixType::TINK, key_template.output_prefix_type());
45 AesSivKeyFormat key_format;
46 EXPECT_TRUE(key_format.ParseFromString(key_template.value()));
47 EXPECT_EQ(64, key_format.key_size());
48
49 // Check that reference to the same object is returned.
50 const KeyTemplate& key_template_2 =
51 DeterministicAeadKeyTemplates::Aes256Siv();
52 EXPECT_EQ(&key_template, &key_template_2);
53
54 // Check that the template works with the key manager.
55 AesSivKeyManager key_type_manager;
56 auto key_manager =
57 internal::MakeKeyManager<DeterministicAead>(&key_type_manager);
58 EXPECT_EQ(key_manager->get_key_type(), key_template.type_url());
59 auto new_key_result =
60 key_manager->get_key_factory().NewKey(key_template.value());
61 EXPECT_TRUE(new_key_result.ok()) << new_key_result.status();
62 }
63 }
64
65 } // namespace
66 } // namespace tink
67 } // namespace crypto
68