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/prf/failing_prfset.h"
17
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 namespace crypto {
24 namespace tink {
25 namespace {
26
27 // A Prf that always returns a kInternal status on API calls.
28 class AlwaysFailPrf : public Prf {
29 public:
AlwaysFailPrf(std::string message)30 explicit AlwaysFailPrf(std::string message) : message_(std::move(message)) {}
31
Compute(absl::string_view,size_t) const32 util::StatusOr<std::string> Compute(absl::string_view /*input*/,
33 size_t /*output_length*/) const override {
34 return util::Status(
35 absl::StatusCode::kInternal,
36 absl::StrCat(
37 "AlwaysFailPrf will always fail on Compute (msg=", message_, ")"));
38 }
39
40 private:
41 const std::string message_;
42 };
43
44 // A PrfSet that always returns a kInternal status on API calls.
45 class AlwaysFailPrfSet : public PrfSet {
46 public:
AlwaysFailPrfSet(std::string message)47 explicit AlwaysFailPrfSet(std::string message)
48 : message_(std::move(message)),
49 always_fail_prf_0_(absl::make_unique<AlwaysFailPrf>(message_)),
50 always_fail_prf_1_(absl::make_unique<AlwaysFailPrf>(message_)),
51 always_fail_prf_2_(absl::make_unique<AlwaysFailPrf>(message_)),
52 prfs_({{0, always_fail_prf_0_.get()},
53 {1, always_fail_prf_1_.get()},
54 {2, always_fail_prf_2_.get()}}) {}
55
GetPrimaryId() const56 uint32_t GetPrimaryId() const override { return 0; }
57
58 // A map of the PRFs represented by the keys in this keyset.
59 // The map is guaranteed to contain getPrimaryId() as a key.
GetPrfs() const60 const std::map<uint32_t, Prf*>& GetPrfs() const override { return prfs_; };
61
ComputePrimary(absl::string_view,size_t) const62 util::StatusOr<std::string> ComputePrimary(absl::string_view /*input*/,
63 size_t /*output_length*/) const {
64 return util::Status(
65 absl::StatusCode::kInternal,
66 absl::StrCat(
67 "AlwaysFailPrfSet will always fail on ComputePrimary (msg=",
68 message_, ")"));
69 }
70
71 private:
72 const std::string message_;
73 std::unique_ptr<AlwaysFailPrf> always_fail_prf_0_;
74 std::unique_ptr<AlwaysFailPrf> always_fail_prf_1_;
75 std::unique_ptr<AlwaysFailPrf> always_fail_prf_2_;
76 std::map<uint32_t, Prf*> prfs_;
77 };
78 } // namespace
79
CreateAlwaysFailingPrf(std::string message)80 std::unique_ptr<Prf> CreateAlwaysFailingPrf(std::string message) {
81 return absl::make_unique<AlwaysFailPrf>(std::move(message));
82 }
83
CreateAlwaysFailingPrfSet(std::string message)84 std::unique_ptr<PrfSet> CreateAlwaysFailingPrfSet(std::string message) {
85 return absl::make_unique<AlwaysFailPrfSet>(std::move(message));
86 }
87
88 } // namespace tink
89 } // namespace crypto
90