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_decrypt_factory.h"
18
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 #include "gtest/gtest.h"
24 #include "absl/memory/memory.h"
25 #include "tink/crypto_format.h"
26 #include "tink/hybrid/ecies_aead_hkdf_public_key_manager.h"
27 #include "tink/hybrid/hybrid_config.h"
28 #include "tink/hybrid_decrypt.h"
29 #include "tink/hybrid_encrypt.h"
30 #include "tink/keyset_handle.h"
31 #include "tink/util/status.h"
32 #include "tink/util/test_keyset_handle.h"
33 #include "tink/util/test_util.h"
34 #include "proto/ecies_aead_hkdf.pb.h"
35 #include "proto/tink.pb.h"
36
37 using crypto::tink::test::AddRawKey;
38 using crypto::tink::test::AddTinkKey;
39 using google::crypto::tink::EciesAeadHkdfPrivateKey;
40 using google::crypto::tink::EcPointFormat;
41 using google::crypto::tink::EllipticCurveType;
42 using google::crypto::tink::HashType;
43 using google::crypto::tink::KeyData;
44 using google::crypto::tink::Keyset;
45 using google::crypto::tink::KeyStatusType;
46
47 namespace crypto {
48 namespace tink {
49 namespace {
50
51 class HybridDecryptFactoryTest : public ::testing::Test {
52 };
53
GetNewEciesPrivateKey()54 EciesAeadHkdfPrivateKey GetNewEciesPrivateKey() {
55 return test::GetEciesAesGcmHkdfTestKey(
56 EllipticCurveType::NIST_P256, EcPointFormat::UNCOMPRESSED,
57 HashType::SHA256, 32);
58 }
59
TEST_F(HybridDecryptFactoryTest,testBasic)60 TEST_F(HybridDecryptFactoryTest, testBasic) {
61 Keyset keyset;
62 auto hybrid_decrypt_result = HybridDecryptFactory::GetPrimitive(
63 *TestKeysetHandle::GetKeysetHandle(keyset));
64 EXPECT_FALSE(hybrid_decrypt_result.ok());
65 EXPECT_EQ(absl::StatusCode::kInvalidArgument,
66 hybrid_decrypt_result.status().code());
67 EXPECT_PRED_FORMAT2(testing::IsSubstring, "at least one key",
68 std::string(hybrid_decrypt_result.status().message()));
69 }
70
TEST_F(HybridDecryptFactoryTest,testPrimitive)71 TEST_F(HybridDecryptFactoryTest, testPrimitive) {
72 // Prepare a Keyset.
73 Keyset keyset;
74 std::string key_type =
75 "type.googleapis.com/google.crypto.tink.EciesAeadHkdfPrivateKey";
76
77 uint32_t key_id_1 = 1234543;
78 auto ecies_key_1 = GetNewEciesPrivateKey();
79 AddTinkKey(key_type, key_id_1, ecies_key_1, KeyStatusType::ENABLED,
80 KeyData::ASYMMETRIC_PRIVATE, &keyset);
81
82 uint32_t key_id_2 = 726329;
83 auto ecies_key_2 = GetNewEciesPrivateKey();
84 AddRawKey(key_type, key_id_2, ecies_key_2, KeyStatusType::ENABLED,
85 KeyData::ASYMMETRIC_PRIVATE, &keyset);
86
87 uint32_t key_id_3 = 7213743;
88 auto ecies_key_3 = GetNewEciesPrivateKey();
89 AddTinkKey(key_type, key_id_3, ecies_key_3, KeyStatusType::ENABLED,
90 KeyData::ASYMMETRIC_PRIVATE, &keyset);
91
92 keyset.set_primary_key_id(key_id_3);
93
94 // Initialize the registry.
95 ASSERT_TRUE(HybridConfig::Register().ok());
96
97 // Prepare HybridEncrypt-instances.
98 auto ecies_key_manager = absl::make_unique<EciesAeadHkdfPublicKeyManager>();
99 std::unique_ptr<HybridEncrypt> ecies_1 = std::move(
100 ecies_key_manager->GetPrimitive<HybridEncrypt>(ecies_key_1.public_key())
101 .value());
102 std::unique_ptr<HybridEncrypt> ecies_2 = std::move(
103 ecies_key_manager->GetPrimitive<HybridEncrypt>(ecies_key_2.public_key())
104 .value());
105
106 // Create a KeysetHandle and use it with the factory.
107 auto hybrid_decrypt_result = HybridDecryptFactory::GetPrimitive(
108 *TestKeysetHandle::GetKeysetHandle(keyset));
109 EXPECT_TRUE(hybrid_decrypt_result.ok()) << hybrid_decrypt_result.status();
110 auto hybrid_decrypt = std::move(hybrid_decrypt_result.value());
111
112 // Test the resulting HybridDecrypt-instance.
113 std::string plaintext = "some plaintext";
114 std::string context_info = "some context info";
115 auto ciphertext_1 =
116 CryptoFormat::GetOutputPrefix(KeyInfoFromKey(keyset.key(0))).value() +
117 ecies_1->Encrypt(plaintext, context_info).value();
118 auto ciphertext_2 =
119 CryptoFormat::GetOutputPrefix(KeyInfoFromKey(keyset.key(1))).value() +
120 ecies_2->Encrypt(plaintext, context_info).value();
121
122 { // Regular decryption with key_1.
123 auto decrypt_result = hybrid_decrypt->Decrypt(ciphertext_1, context_info);
124 EXPECT_TRUE(decrypt_result.ok()) << decrypt_result.status();
125 EXPECT_EQ(plaintext, decrypt_result.value());
126 }
127
128 { // Regular decryption with key_2.
129 auto decrypt_result = hybrid_decrypt->Decrypt(ciphertext_2, context_info);
130 EXPECT_TRUE(decrypt_result.ok()) << decrypt_result.status();
131 EXPECT_EQ(plaintext, decrypt_result.value());
132 }
133
134 { // Wrong context_info.
135 auto decrypt_result = hybrid_decrypt->Decrypt(ciphertext_1, "bad context");
136 EXPECT_FALSE(decrypt_result.ok());
137 EXPECT_EQ(absl::StatusCode::kInvalidArgument,
138 decrypt_result.status().code());
139 EXPECT_PRED_FORMAT2(testing::IsSubstring, "decryption failed",
140 std::string(decrypt_result.status().message()));
141 }
142 }
143
144 } // namespace
145 } // namespace tink
146 } // namespace crypto
147