// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /////////////////////////////////////////////////////////////////////////////// #include "tink/prf/failing_prfset.h" #include #include #include #include namespace crypto { namespace tink { namespace { // A Prf that always returns a kInternal status on API calls. class AlwaysFailPrf : public Prf { public: explicit AlwaysFailPrf(std::string message) : message_(std::move(message)) {} util::StatusOr Compute(absl::string_view /*input*/, size_t /*output_length*/) const override { return util::Status( absl::StatusCode::kInternal, absl::StrCat( "AlwaysFailPrf will always fail on Compute (msg=", message_, ")")); } private: const std::string message_; }; // A PrfSet that always returns a kInternal status on API calls. class AlwaysFailPrfSet : public PrfSet { public: explicit AlwaysFailPrfSet(std::string message) : message_(std::move(message)), always_fail_prf_0_(absl::make_unique(message_)), always_fail_prf_1_(absl::make_unique(message_)), always_fail_prf_2_(absl::make_unique(message_)), prfs_({{0, always_fail_prf_0_.get()}, {1, always_fail_prf_1_.get()}, {2, always_fail_prf_2_.get()}}) {} uint32_t GetPrimaryId() const override { return 0; } // A map of the PRFs represented by the keys in this keyset. // The map is guaranteed to contain getPrimaryId() as a key. const std::map& GetPrfs() const override { return prfs_; }; util::StatusOr ComputePrimary(absl::string_view /*input*/, size_t /*output_length*/) const { return util::Status( absl::StatusCode::kInternal, absl::StrCat( "AlwaysFailPrfSet will always fail on ComputePrimary (msg=", message_, ")")); } private: const std::string message_; std::unique_ptr always_fail_prf_0_; std::unique_ptr always_fail_prf_1_; std::unique_ptr always_fail_prf_2_; std::map prfs_; }; } // namespace std::unique_ptr CreateAlwaysFailingPrf(std::string message) { return absl::make_unique(std::move(message)); } std::unique_ptr CreateAlwaysFailingPrfSet(std::string message) { return absl::make_unique(std::move(message)); } } // namespace tink } // namespace crypto