1 // Copyright 2020 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
17 #include "tink/subtle/prf/streaming_prf_wrapper.h"
18
19 #include <memory>
20 #include <utility>
21
22 #include "absl/status/status.h"
23 #include "absl/strings/str_cat.h"
24 #include "tink/subtle/prf/streaming_prf.h"
25
26 namespace crypto {
27 namespace tink {
28
29 class StreamingPrfSetWrapper : public StreamingPrf {
30 public:
StreamingPrfSetWrapper(std::unique_ptr<PrimitiveSet<StreamingPrf>> streaming_prf_set)31 explicit StreamingPrfSetWrapper(
32 std::unique_ptr<PrimitiveSet<StreamingPrf>> streaming_prf_set)
33 : streaming_prf_set_(std::move(streaming_prf_set)) {}
34
ComputePrf(absl::string_view input) const35 std::unique_ptr<InputStream> ComputePrf(
36 absl::string_view input) const override {
37 return streaming_prf_set_->get_primary()->get_primitive().ComputePrf(input);
38 }
39
40 ~StreamingPrfSetWrapper() override = default;
41
42 private:
43 std::unique_ptr<PrimitiveSet<StreamingPrf>> streaming_prf_set_;
44 };
45
46 ::crypto::tink::util::StatusOr<std::unique_ptr<StreamingPrf>>
Wrap(std::unique_ptr<PrimitiveSet<StreamingPrf>> streaming_prf_set) const47 StreamingPrfWrapper::Wrap(std::unique_ptr<PrimitiveSet<StreamingPrf>>
48 streaming_prf_set) const {
49 if (!streaming_prf_set) {
50 return crypto::tink::util::Status(
51 absl::StatusCode::kInvalidArgument,
52 "Passed in streaming_prf_set must be non-NULL");
53 }
54
55 auto entries = streaming_prf_set->get_all();
56
57 if (entries.size() != 1) {
58 return crypto::tink::util::Status(
59 absl::StatusCode::kInvalidArgument,
60 absl::StrCat("StreamingPrfWrapper can only create StreamingPrf objects "
61 "from keysets "
62 "with exactly one key, but the but the given set has ",
63 entries.size(), " keys."));
64 }
65 auto* entry = entries[0];
66 if (entry->get_status() != google::crypto::tink::KeyStatusType::ENABLED) {
67 return crypto::tink::util::Status(
68 absl::StatusCode::kInvalidArgument,
69 absl::StrCat("StreamingPrfWrapper can only create StreamingPrf objects "
70 "from keysets "
71 "with an enabled keys, but the key is ",
72 entry->get_status()));
73 }
74 if (entry->get_output_prefix_type() !=
75 google::crypto::tink::OutputPrefixType::RAW) {
76 return crypto::tink::util::Status(
77 absl::StatusCode::kInvalidArgument,
78 absl::StrCat("StreamingPrfWrapper can only create StreamingPrf objects "
79 "from keysets with a single enabled key with "
80 "OutputPrefixType::RAW, "
81 "but output_prefix_type is ",
82 entry->get_output_prefix_type()));
83 }
84 return {
85 absl::make_unique<StreamingPrfSetWrapper>(std::move(streaming_prf_set))};
86 }
87
88 } // namespace tink
89 } // namespace crypto
90