xref: /aosp_15_r20/external/tink/cc/mac/failing_mac.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/mac/failing_mac.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 // A MAC that always returns a kInternal status on API calls.
29 class AlwaysFailMac : public Mac {
30  public:
AlwaysFailMac(std::string message)31   explicit AlwaysFailMac(std::string message) : message_(std::move(message)) {}
32 
ComputeMac(absl::string_view) const33   util::StatusOr<std::string> ComputeMac(
34       absl::string_view /*data*/) const override {
35     return util::Status(
36         absl::StatusCode::kInternal,
37         absl::StrCat("AlwaysFailMac will always fail on ComputeMac (msg=",
38                      message_, ")"));
39   }
40 
VerifyMac(absl::string_view,absl::string_view) const41   util::Status VerifyMac(absl::string_view /*mac_value*/,
42                          absl::string_view /*data*/) const override {
43     return util::Status(
44         absl::StatusCode::kInternal,
45         absl::StrCat("AlwaysFailMac will always fail on VerifyMac (msg=",
46                      message_, ")"));
47   }
48 
49  private:
50   const std::string message_;
51 };
52 
53 }  // namespace
54 
CreateAlwaysFailingMac(std::string message)55 std::unique_ptr<Mac> CreateAlwaysFailingMac(std::string message) {
56   return absl::make_unique<AlwaysFailMac>(std::move(message));
57 }
58 
59 }  // namespace tink
60 }  // namespace crypto
61