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_wrapper.h"
18
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 #include "absl/status/status.h"
24 #include "tink/crypto_format.h"
25 #include "tink/deterministic_aead.h"
26 #include "tink/internal/monitoring_util.h"
27 #include "tink/internal/registry_impl.h"
28 #include "tink/internal/util.h"
29 #include "tink/monitoring/monitoring.h"
30 #include "tink/primitive_set.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33
34 namespace crypto {
35 namespace tink {
36
37 namespace {
38
39 constexpr absl::string_view kPrimitive = "daead";
40 constexpr absl::string_view kEncryptApi = "encrypt";
41 constexpr absl::string_view kDecryptApi = "decrypt";
42
Validate(PrimitiveSet<DeterministicAead> * daead_set)43 util::Status Validate(PrimitiveSet<DeterministicAead>* daead_set) {
44 if (daead_set == nullptr) {
45 return util::Status(absl::StatusCode::kInternal,
46 "daead_set must be non-NULL");
47 }
48 if (daead_set->get_primary() == nullptr) {
49 return util::Status(absl::StatusCode::kInvalidArgument,
50 "daead_set has no primary");
51 }
52 return util::OkStatus();
53 }
54
55 class DeterministicAeadSetWrapper : public DeterministicAead {
56 public:
DeterministicAeadSetWrapper(std::unique_ptr<PrimitiveSet<DeterministicAead>> daead_set,std::unique_ptr<MonitoringClient> monitoring_encryption_client=nullptr,std::unique_ptr<MonitoringClient> monitoring_decryption_client=nullptr)57 explicit DeterministicAeadSetWrapper(
58 std::unique_ptr<PrimitiveSet<DeterministicAead>> daead_set,
59 std::unique_ptr<MonitoringClient> monitoring_encryption_client = nullptr,
60 std::unique_ptr<MonitoringClient> monitoring_decryption_client = nullptr)
61 : daead_set_(std::move(daead_set)),
62 monitoring_encryption_client_(std::move(monitoring_encryption_client)),
63 monitoring_decryption_client_(std::move(monitoring_decryption_client))
64 {}
65
66 crypto::tink::util::StatusOr<std::string> EncryptDeterministically(
67 absl::string_view plaintext,
68 absl::string_view associated_data) const override;
69
70 crypto::tink::util::StatusOr<std::string> DecryptDeterministically(
71 absl::string_view ciphertext,
72 absl::string_view associated_data) const override;
73
74 ~DeterministicAeadSetWrapper() override = default;
75
76 private:
77 std::unique_ptr<PrimitiveSet<DeterministicAead>> daead_set_;
78 std::unique_ptr<MonitoringClient> monitoring_encryption_client_;
79 std::unique_ptr<MonitoringClient> monitoring_decryption_client_;
80 };
81
82 util::StatusOr<std::string>
EncryptDeterministically(absl::string_view plaintext,absl::string_view associated_data) const83 DeterministicAeadSetWrapper::EncryptDeterministically(
84 absl::string_view plaintext, absl::string_view associated_data) const {
85 // BoringSSL expects a non-null pointer for plaintext and associated_data,
86 // regardless of whether the size is 0.
87 plaintext = internal::EnsureStringNonNull(plaintext);
88 associated_data = internal::EnsureStringNonNull(associated_data);
89
90 auto encrypt_result =
91 daead_set_->get_primary()->get_primitive().EncryptDeterministically(
92 plaintext, associated_data);
93 if (!encrypt_result.ok()) {
94 if (monitoring_encryption_client_ != nullptr) {
95 monitoring_encryption_client_->LogFailure();
96 }
97 return encrypt_result.status();
98 }
99 if (monitoring_encryption_client_ != nullptr) {
100 monitoring_encryption_client_->Log(daead_set_->get_primary()->get_key_id(),
101 plaintext.size());
102 }
103 const std::string& key_id = daead_set_->get_primary()->get_identifier();
104 return key_id + encrypt_result.value();
105 }
106
107 util::StatusOr<std::string>
DecryptDeterministically(absl::string_view ciphertext,absl::string_view associated_data) const108 DeterministicAeadSetWrapper::DecryptDeterministically(
109 absl::string_view ciphertext, absl::string_view associated_data) const {
110 // BoringSSL expects a non-null pointer for plaintext and associated_data,
111 // regardless of whether the size is 0.
112 associated_data = internal::EnsureStringNonNull(associated_data);
113
114 if (ciphertext.length() > CryptoFormat::kNonRawPrefixSize) {
115 absl::string_view key_id =
116 ciphertext.substr(0, CryptoFormat::kNonRawPrefixSize);
117 auto primitives_result = daead_set_->get_primitives(key_id);
118 if (primitives_result.ok()) {
119 absl::string_view raw_ciphertext =
120 ciphertext.substr(CryptoFormat::kNonRawPrefixSize);
121 for (const auto& daead_entry : *(primitives_result.value())) {
122 DeterministicAead& daead = daead_entry->get_primitive();
123 auto decrypt_result =
124 daead.DecryptDeterministically(raw_ciphertext, associated_data);
125 if (decrypt_result.ok()) {
126 if (monitoring_decryption_client_ != nullptr) {
127 monitoring_decryption_client_->Log(daead_entry->get_key_id(),
128 raw_ciphertext.size());
129 }
130 return std::move(decrypt_result.value());
131 } else {
132 // LOG that a matching key didn't decrypt the ciphertext.
133 }
134 }
135 }
136 }
137
138 // No matching key succeeded with decryption, try all RAW keys.
139 auto raw_primitives_result = daead_set_->get_raw_primitives();
140 if (raw_primitives_result.ok()) {
141 for (const auto& daead_entry : *(raw_primitives_result.value())) {
142 DeterministicAead& daead = daead_entry->get_primitive();
143 auto decrypt_result =
144 daead.DecryptDeterministically(ciphertext, associated_data);
145 if (decrypt_result.ok()) {
146 if (monitoring_decryption_client_ != nullptr) {
147 monitoring_decryption_client_->Log(daead_entry->get_key_id(),
148 ciphertext.size());
149 }
150 return std::move(decrypt_result.value());
151 }
152 }
153 }
154 if (monitoring_decryption_client_ != nullptr) {
155 monitoring_decryption_client_->LogFailure();
156 }
157 return util::Status(absl::StatusCode::kInvalidArgument, "decryption failed");
158 }
159
160 } // anonymous namespace
161
162 util::StatusOr<std::unique_ptr<DeterministicAead>>
Wrap(std::unique_ptr<PrimitiveSet<DeterministicAead>> primitive_set) const163 DeterministicAeadWrapper::Wrap(
164 std::unique_ptr<PrimitiveSet<DeterministicAead>> primitive_set) const {
165 util::Status status = Validate(primitive_set.get());
166 if (!status.ok()) return status;
167
168 MonitoringClientFactory* const monitoring_factory =
169 internal::RegistryImpl::GlobalInstance().GetMonitoringClientFactory();
170
171 // Monitoring is not enabled. Create a wrapper without monitoring clients.
172 if (monitoring_factory == nullptr) {
173 return {absl::make_unique<DeterministicAeadSetWrapper>(
174 std::move(primitive_set))};
175 }
176
177 util::StatusOr<MonitoringKeySetInfo> keyset_info =
178 internal::MonitoringKeySetInfoFromPrimitiveSet(*primitive_set);
179 if (!keyset_info.ok()) {
180 return keyset_info.status();
181 }
182
183 util::StatusOr<std::unique_ptr<MonitoringClient>>
184 monitoring_encryption_client = monitoring_factory->New(
185 MonitoringContext(kPrimitive, kEncryptApi, *keyset_info));
186 if (!monitoring_encryption_client.ok()) {
187 return monitoring_encryption_client.status();
188 }
189
190 util::StatusOr<std::unique_ptr<MonitoringClient>>
191 monitoring_decryption_client = monitoring_factory->New(
192 MonitoringContext(kPrimitive, kDecryptApi, *keyset_info));
193 if (!monitoring_decryption_client.ok()) {
194 return monitoring_decryption_client.status();
195 }
196
197 return {absl::make_unique<DeterministicAeadSetWrapper>(
198 std::move(primitive_set), *std::move(monitoring_encryption_client),
199 *std::move(monitoring_decryption_client))};
200 }
201
202 } // namespace tink
203 } // namespace crypto
204