1 // Copyright 2017 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/hybrid/hybrid_encrypt_factory.h"
18
19 #include <string>
20 #include <utility>
21
22 #include "gtest/gtest.h"
23 #include "tink/crypto_format.h"
24 #include "tink/hybrid/hybrid_config.h"
25 #include "tink/hybrid_encrypt.h"
26 #include "tink/keyset_handle.h"
27 #include "tink/util/status.h"
28 #include "tink/util/test_keyset_handle.h"
29 #include "tink/util/test_util.h"
30 #include "proto/ecies_aead_hkdf.pb.h"
31 #include "proto/tink.pb.h"
32
33 using crypto::tink::test::AddRawKey;
34 using crypto::tink::test::AddTinkKey;
35 using google::crypto::tink::EciesAeadHkdfPublicKey;
36 using google::crypto::tink::EcPointFormat;
37 using google::crypto::tink::EllipticCurveType;
38 using google::crypto::tink::HashType;
39 using google::crypto::tink::KeyData;
40 using google::crypto::tink::Keyset;
41 using google::crypto::tink::KeyStatusType;
42
43 namespace crypto {
44 namespace tink {
45 namespace {
46
47 class HybridEncryptFactoryTest : public ::testing::Test {
48 };
49
GetNewEciesPublicKey()50 EciesAeadHkdfPublicKey GetNewEciesPublicKey() {
51 auto ecies_key = test::GetEciesAesGcmHkdfTestKey(
52 EllipticCurveType::NIST_P256, EcPointFormat::UNCOMPRESSED,
53 HashType::SHA256, 32);
54 return ecies_key.public_key();
55 }
56
TEST_F(HybridEncryptFactoryTest,testBasic)57 TEST_F(HybridEncryptFactoryTest, testBasic) {
58 Keyset keyset;
59 auto hybrid_encrypt_result = HybridEncryptFactory::GetPrimitive(
60 *TestKeysetHandle::GetKeysetHandle(keyset));
61 EXPECT_FALSE(hybrid_encrypt_result.ok());
62 EXPECT_EQ(absl::StatusCode::kInvalidArgument,
63 hybrid_encrypt_result.status().code());
64 EXPECT_PRED_FORMAT2(testing::IsSubstring, "at least one key",
65 std::string(hybrid_encrypt_result.status().message()));
66 }
67
TEST_F(HybridEncryptFactoryTest,testPrimitive)68 TEST_F(HybridEncryptFactoryTest, testPrimitive) {
69 // Prepare a Keyset.
70 Keyset keyset;
71 std::string key_type =
72 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPublicKey";
73
74 uint32_t key_id_1 = 1234543;
75
76 AddTinkKey(key_type, key_id_1, GetNewEciesPublicKey(), KeyStatusType::ENABLED,
77 KeyData::ASYMMETRIC_PUBLIC, &keyset);
78
79 uint32_t key_id_2 = 726329;
80 AddRawKey(key_type, key_id_2, GetNewEciesPublicKey(), KeyStatusType::ENABLED,
81 KeyData::ASYMMETRIC_PUBLIC, &keyset);
82
83 uint32_t key_id_3 = 7213743;
84 AddTinkKey(key_type, key_id_3, GetNewEciesPublicKey(), KeyStatusType::ENABLED,
85 KeyData::ASYMMETRIC_PUBLIC, &keyset);
86
87 keyset.set_primary_key_id(key_id_3);
88
89 // Initialize the registry.
90 ASSERT_TRUE(HybridConfig::Register().ok());
91
92 // Create a KeysetHandle and use it with the factory.
93 auto hybrid_encrypt_result = HybridEncryptFactory::GetPrimitive(
94 *TestKeysetHandle::GetKeysetHandle(keyset));
95 EXPECT_TRUE(hybrid_encrypt_result.ok()) << hybrid_encrypt_result.status();
96 auto hybrid_encrypt = std::move(hybrid_encrypt_result.value());
97
98 // Test the resulting HybridEncrypt-instance.
99 std::string plaintext = "some plaintext";
100 std::string context_info = "some context info";
101
102 auto encrypt_result = hybrid_encrypt->Encrypt(plaintext, context_info);
103 EXPECT_TRUE(encrypt_result.ok()) << encrypt_result.status();
104 }
105
106 } // namespace
107 } // namespace tink
108 } // namespace crypto
109