xref: /aosp_15_r20/external/tink/cc/hybrid/ecies_aead_hkdf_hybrid_encrypt.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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/ecies_aead_hkdf_hybrid_encrypt.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/str_cat.h"
26 #include "tink/aead.h"
27 #include "tink/util/enums.h"
28 #include "tink/util/status.h"
29 #include "proto/ecies_aead_hkdf.pb.h"
30 
31 using ::google::crypto::tink::EciesAeadHkdfPublicKey;
32 using ::google::crypto::tink::EllipticCurveType;
33 
34 namespace crypto {
35 namespace tink {
36 
37 namespace {
38 
Validate(const EciesAeadHkdfPublicKey & key)39 util::Status Validate(const EciesAeadHkdfPublicKey& key) {
40   if (key.x().empty() || !key.has_params()) {
41     return util::Status(
42         absl::StatusCode::kInvalidArgument,
43         "Invalid EciesAeadHkdfPublicKey: missing required fields.");
44   }
45 
46   if (key.params().has_kem_params() &&
47       key.params().kem_params().curve_type() == EllipticCurveType::CURVE25519) {
48     if (!key.y().empty()) {
49       return util::Status(
50           absl::StatusCode::kInvalidArgument,
51           "Invalid EciesAeadHkdfPublicKey: has unexpected field.");
52     }
53   } else if (key.y().empty()) {
54     return util::Status(
55         absl::StatusCode::kInvalidArgument,
56         "Invalid EciesAeadHkdfPublicKey: missing required fields.");
57   }
58 
59   return util::OkStatus();
60 }
61 
62 }  // namespace
63 
64 // static
New(const EciesAeadHkdfPublicKey & recipient_key)65 util::StatusOr<std::unique_ptr<HybridEncrypt>> EciesAeadHkdfHybridEncrypt::New(
66     const EciesAeadHkdfPublicKey& recipient_key) {
67   util::Status status = Validate(recipient_key);
68   if (!status.ok()) return status;
69 
70   auto kem_result = subtle::EciesHkdfSenderKemBoringSsl::New(
71       util::Enums::ProtoToSubtle(
72           recipient_key.params().kem_params().curve_type()),
73       recipient_key.x(), recipient_key.y());
74   if (!kem_result.ok()) return kem_result.status();
75 
76   auto dem_result = EciesAeadHkdfDemHelper::New(
77       recipient_key.params().dem_params().aead_dem());
78   if (!dem_result.ok()) return dem_result.status();
79 
80   return {absl::WrapUnique(new EciesAeadHkdfHybridEncrypt(
81       recipient_key, std::move(kem_result).value(),
82       std::move(dem_result).value()))};
83 }
84 
Encrypt(absl::string_view plaintext,absl::string_view context_info) const85 util::StatusOr<std::string> EciesAeadHkdfHybridEncrypt::Encrypt(
86     absl::string_view plaintext, absl::string_view context_info) const {
87   // Use KEM to get a symmetric key.
88   auto kem_key_result = sender_kem_->GenerateKey(
89       util::Enums::ProtoToSubtle(
90           recipient_key_.params().kem_params().hkdf_hash_type()),
91       recipient_key_.params().kem_params().hkdf_salt(),
92       context_info,
93       dem_helper_->dem_key_size_in_bytes(),
94       util::Enums::ProtoToSubtle(
95           recipient_key_.params().ec_point_format()));
96   if (!kem_key_result.ok()) return kem_key_result.status();
97   auto kem_key = std::move(kem_key_result.value());
98 
99   // Use the symmetric key to get an AEAD-primitive.
100   auto aead_or_daead_result =
101       dem_helper_->GetAeadOrDaead(kem_key->get_symmetric_key());
102   if (!aead_or_daead_result.ok()) return aead_or_daead_result.status();
103   auto aead_or_daead = std::move(aead_or_daead_result.value());
104 
105   // Do the actual encryption using the AEAD-primitive.
106   auto encrypt_result = aead_or_daead->Encrypt(plaintext, "");  // empty aad
107   if (!encrypt_result.ok()) return encrypt_result.status();
108 
109   // Prepend AEAD-ciphertext with a KEM component.
110   std::string ciphertext =
111       absl::StrCat(kem_key->get_kem_bytes(), encrypt_result.value());
112   return ciphertext;
113 }
114 
115 }  // namespace tink
116 }  // namespace crypto
117