xref: /aosp_15_r20/external/tink/cc/daead/failing_daead.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 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 #include "tink/daead/failing_daead.h"
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 namespace crypto {
23 namespace tink {
24 namespace {
25 
26 // A deterministic AEAD that always return a kInternal status on API calls.
27 class AlwaysFailDeterministicAead : public DeterministicAead {
28  public:
AlwaysFailDeterministicAead(std::string message)29   explicit AlwaysFailDeterministicAead(std::string message)
30       : message_(std::move(message)) {}
31 
EncryptDeterministically(absl::string_view plaintext,absl::string_view associated_data) const32   util::StatusOr<std::string> EncryptDeterministically(
33       absl::string_view plaintext,
34       absl::string_view associated_data) const override {
35     return util::Status(
36         absl::StatusCode::kInternal,
37         absl::StrCat(
38             "AlwaysFailDeterministicAead will always fail on encrypt (msg=",
39             message_, ")"));
40   }
41 
DecryptDeterministically(absl::string_view ciphertext,absl::string_view associated_data) const42   util::StatusOr<std::string> DecryptDeterministically(
43       absl::string_view ciphertext,
44       absl::string_view associated_data) const override {
45     return util::Status(
46         absl::StatusCode::kInternal,
47         absl::StrCat(
48             "AlwaysFailDeterministicAead will always fail on decrypt (msg=",
49             message_, ")"));
50   }
51 
52  private:
53   const std::string message_;
54 };
55 
56 }  // namespace
57 
CreateAlwaysFailingDeterministicAead(std::string message)58 std::unique_ptr<DeterministicAead> CreateAlwaysFailingDeterministicAead(
59     std::string message) {
60   return absl::make_unique<AlwaysFailDeterministicAead>(std::move(message));
61 }
62 
63 }  // namespace tink
64 }  // namespace crypto
65