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/keyderivation/key_derivation_config.h"
18
19 #include <memory>
20 #include <string>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "tink/aead/aead_config.h"
25 #include "tink/aead/aead_key_templates.h"
26 #include "tink/aead/aes_gcm_key_manager.h"
27 #include "tink/keyderivation/key_derivation_key_templates.h"
28 #include "tink/keyderivation/keyset_deriver.h"
29 #include "tink/prf/prf_key_templates.h"
30 #include "tink/registry.h"
31 #include "tink/util/statusor.h"
32 #include "tink/util/test_matchers.h"
33
34 namespace crypto {
35 namespace tink {
36
37 namespace {
38
39 using ::crypto::tink::test::IsOk;
40 using ::testing::Not;
41
TEST(KeyDerivationConfigTest,Register)42 TEST(KeyDerivationConfigTest, Register) {
43 Registry::Reset();
44
45 EXPECT_THAT(KeyDerivationKeyTemplates::CreatePrfBasedKeyTemplate(
46 PrfKeyTemplates::HkdfSha256(), AeadKeyTemplates::Aes256Gcm()),
47 Not(IsOk()));
48
49 ASSERT_THAT(KeyDerivationConfig::Register(), IsOk());
50 ASSERT_THAT(AeadConfig::Register(), IsOk());
51 ASSERT_THAT(Registry::RegisterKeyTypeManager(
52 absl::make_unique<AesGcmKeyManager>(), true),
53 IsOk());
54
55 util::StatusOr<::google::crypto::tink::KeyTemplate> templ =
56 KeyDerivationKeyTemplates::CreatePrfBasedKeyTemplate(
57 PrfKeyTemplates::HkdfSha256(), AeadKeyTemplates::Aes256Gcm());
58 ASSERT_THAT(templ, IsOk());
59 util::StatusOr<std::unique_ptr<KeysetHandle>> handle =
60 KeysetHandle::GenerateNew(*templ);
61 ASSERT_THAT(handle, IsOk());
62 util::StatusOr<std::unique_ptr<KeysetDeriver>> deriver =
63 (*handle)->GetPrimitive<KeysetDeriver>();
64 ASSERT_THAT(deriver, IsOk());
65 util::StatusOr<std::unique_ptr<KeysetHandle>> derived_handle =
66 (*deriver)->DeriveKeyset("salty");
67 ASSERT_THAT(derived_handle, IsOk());
68
69 util::StatusOr<std::unique_ptr<Aead>> aead =
70 (*derived_handle)->GetPrimitive<Aead>();
71 ASSERT_THAT(aead, IsOk());
72 std::string plaintext = "plaintext";
73 std::string ad = "ad";
74 util::StatusOr<std::string> ciphertext = (*aead)->Encrypt(plaintext, ad);
75 ASSERT_THAT(ciphertext, IsOk());
76 util::StatusOr<std::string> got = (*aead)->Decrypt(*ciphertext, ad);
77 ASSERT_THAT(got, IsOk());
78 EXPECT_EQ(plaintext, *got);
79 }
80
81 } // namespace
82 } // namespace tink
83 } // namespace crypto
84