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 #ifndef TINK_INTERNAL_KEYSET_WRAPPER_H_ 17 #define TINK_INTERNAL_KEYSET_WRAPPER_H_ 18 19 #include <memory> 20 #include <string> 21 22 #include "absl/container/flat_hash_map.h" 23 #include "tink/util/statusor.h" 24 #include "proto/tink.pb.h" 25 26 namespace crypto { 27 namespace tink { 28 namespace internal { 29 30 // A Keyset wrapper wraps a Tink Keyset into a set of primitives. This is a 31 // Tink internal object, which is created from a PrimitiveWrapper. 32 // 33 // The KeysetWrapper is used because the only moment during compilation in which 34 // the registry knows the input primitive type of a PrimitiveWrapper<P, Q> is 35 // when RegisterPrimitiveWrapper(transforming_wrapper) is called. (There, the 36 // compiler infers the template arguments. This means that all the work which 37 // handles Q needs to be done in that compilation unit, and when creating the 38 // primitive we cannot refer to Q. 39 // 40 // Hence, when registering the object, we first use type erasure to forget about 41 // Q and create a subclass (KeysetWrapperImpl<P,Q>) of this object. 42 template <typename Primitive> 43 class KeysetWrapper { 44 public: 45 virtual ~KeysetWrapper() = default; 46 47 // Wraps a given `keyset` with annotations `annotations`. 48 virtual crypto::tink::util::StatusOr<std::unique_ptr<Primitive>> Wrap( 49 const google::crypto::tink::Keyset& keyset, 50 const absl::flat_hash_map<std::string, std::string>& annotations) 51 const = 0; 52 }; 53 54 } // namespace internal 55 } // namespace tink 56 } // namespace crypto 57 58 #endif // TINK_INTERNAL_KEYSET_WRAPPER_H_ 59