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/signature/failing_signature.h"
17
18 #include <memory>
19 #include <string>
20 #include <utility>
21
22 #include "tink/public_key_sign.h"
23 #include "tink/public_key_verify.h"
24
25 namespace crypto {
26 namespace tink {
27 namespace {
28
29 // A PublicKeySign that always return a kInternal status on API calls.
30 class AlwaysFailPublicKeySign : public PublicKeySign {
31 public:
AlwaysFailPublicKeySign(std::string message)32 explicit AlwaysFailPublicKeySign(std::string message)
33 : message_(std::move(message)) {}
34
Sign(absl::string_view) const35 util::StatusOr<std::string> Sign(
36 absl::string_view /*message*/) const override {
37 return util::Status(
38 absl::StatusCode::kInternal,
39 absl::StrCat("AlwaysFailPublicKeySign will always fail on sign (msg=",
40 message_, ")"));
41 }
42
43 private:
44 const std::string message_;
45 };
46
47 // A PublicKeyVerify that always return a kInternal status on API calls.
48 class AlwaysFailPublicKeyVerify : public PublicKeyVerify {
49 public:
AlwaysFailPublicKeyVerify(std::string message)50 explicit AlwaysFailPublicKeyVerify(std::string message)
51 : message_(std::move(message)) {}
52
Verify(absl::string_view,absl::string_view) const53 util::Status Verify(absl::string_view /*signature*/,
54 absl::string_view /*message*/) const override {
55 return absl::InternalError(
56 absl::StrCat(
57 "AlwaysFailPublicKeyVerify will always fail on verify (msg=",
58 message_, ")"));
59 }
60
61 private:
62 const std::string message_;
63 };
64
65 } // namespace
66
CreateAlwaysFailingPublicKeySign(std::string message)67 std::unique_ptr<PublicKeySign> CreateAlwaysFailingPublicKeySign(
68 std::string message) {
69 return absl::make_unique<AlwaysFailPublicKeySign>(std::move(message));
70 }
71
CreateAlwaysFailingPublicKeyVerify(std::string message)72 std::unique_ptr<PublicKeyVerify> CreateAlwaysFailingPublicKeyVerify(
73 std::string message) {
74 return absl::make_unique<AlwaysFailPublicKeyVerify>(std::move(message));
75 }
76
77 } // namespace tink
78 } // namespace crypto
79