xref: /aosp_15_r20/external/tink/cc/aead/failing_aead.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/aead/failing_aead.h"
17 
18 #include <memory>
19 #include <string>
20 #include <utility>
21 
22 #include "absl/strings/string_view.h"
23 
24 namespace crypto {
25 namespace tink {
26 namespace {
27 
28 // An AEAD that always returns a kInternal status on API calls.
29 class AlwaysFailAead : public Aead {
30  public:
AlwaysFailAead(std::string message)31   explicit AlwaysFailAead(std::string message)
32       : message_(std::move(message)) {}
33 
Encrypt(absl::string_view plaintext,absl::string_view associated_data) const34   util::StatusOr<std::string> Encrypt(
35       absl::string_view plaintext,
36       absl::string_view associated_data) const override {
37     return util::Status(
38         absl::StatusCode::kInternal,
39         absl::StrCat(
40             "AlwaysFailAead will always fail on encrypt (msg=", message_, ")"));
41   }
42 
Decrypt(absl::string_view ciphertext,absl::string_view associated_data) const43   util::StatusOr<std::string> Decrypt(
44       absl::string_view ciphertext,
45       absl::string_view associated_data) const override {
46     return util::Status(
47         absl::StatusCode::kInternal,
48         absl::StrCat(
49             "AlwaysFailAead will always fail on decrypt (msg=", message_, ")"));
50   }
51 
52  private:
53   const std::string message_;
54 };
55 
56 }  // namespace
57 
CreateAlwaysFailingAead(absl::string_view message)58 std::unique_ptr<Aead> CreateAlwaysFailingAead(absl::string_view message) {
59   return absl::make_unique<AlwaysFailAead>(std::string(message));
60 }
61 
62 }  // namespace tink
63 }  // namespace crypto
64