1 // Copyright 2021 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 #ifndef TINK_HYBRID_INTERNAL_HPKE_PRIVATE_KEY_MANAGER_H_ 18 #define TINK_HYBRID_INTERNAL_HPKE_PRIVATE_KEY_MANAGER_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "tink/core/key_type_manager.h" 24 #include "tink/core/private_key_type_manager.h" 25 #include "tink/hybrid/internal/hpke_decrypt.h" 26 #include "tink/hybrid_decrypt.h" 27 #include "tink/key_manager.h" 28 #include "tink/util/constants.h" 29 #include "tink/util/status.h" 30 #include "tink/util/statusor.h" 31 #include "proto/hpke.pb.h" 32 #include "proto/tink.pb.h" 33 34 namespace crypto { 35 namespace tink { 36 namespace internal { 37 38 class HpkePrivateKeyManager 39 : public PrivateKeyTypeManager<google::crypto::tink::HpkePrivateKey, 40 google::crypto::tink::HpkeKeyFormat, 41 google::crypto::tink::HpkePublicKey, 42 List<HybridDecrypt>> { 43 public: 44 class HybridDecryptFactory : public PrimitiveFactory<HybridDecrypt> { Create(const google::crypto::tink::HpkePrivateKey & private_key)45 crypto::tink::util::StatusOr<std::unique_ptr<HybridDecrypt>> Create( 46 const google::crypto::tink::HpkePrivateKey& private_key) 47 const override { 48 return HpkeDecrypt::New(private_key); 49 } 50 }; 51 HpkePrivateKeyManager()52 HpkePrivateKeyManager() 53 : PrivateKeyTypeManager(absl::make_unique<HybridDecryptFactory>()) {} 54 get_version()55 uint32_t get_version() const override { return 0; } 56 key_material_type()57 google::crypto::tink::KeyData::KeyMaterialType key_material_type() 58 const override { 59 return google::crypto::tink::KeyData::ASYMMETRIC_PRIVATE; 60 } 61 get_key_type()62 const std::string& get_key_type() const override { return key_type_; } 63 64 crypto::tink::util::Status ValidateKey( 65 const google::crypto::tink::HpkePrivateKey& key) const override; 66 67 crypto::tink::util::Status ValidateKeyFormat( 68 const google::crypto::tink::HpkeKeyFormat& key_format) const override; 69 70 crypto::tink::util::StatusOr<google::crypto::tink::HpkePrivateKey> CreateKey( 71 const google::crypto::tink::HpkeKeyFormat& key_format) const override; 72 73 crypto::tink::util::StatusOr<google::crypto::tink::HpkePublicKey> 74 GetPublicKey( 75 const google::crypto::tink::HpkePrivateKey& private_key) const override; 76 77 private: 78 const std::string key_type_ = absl::StrCat( 79 kTypeGoogleapisCom, google::crypto::tink::HpkePrivateKey().GetTypeName()); 80 }; 81 82 } // namespace internal 83 } // namespace tink 84 } // namespace crypto 85 86 #endif // TINK_HYBRID_INTERNAL_HPKE_PRIVATE_KEY_MANAGER_H_ 87