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_PRF_HMAC_PRF_KEY_MANAGER_H_ 17 #define TINK_PRF_HMAC_PRF_KEY_MANAGER_H_ 18 19 #include <algorithm> 20 #include <map> 21 #include <memory> 22 #include <string> 23 #include <vector> 24 25 #include "absl/memory/memory.h" 26 #include "absl/strings/string_view.h" 27 #include "tink/core/key_type_manager.h" 28 #include "tink/internal/fips_utils.h" 29 #include "tink/key_manager.h" 30 #include "tink/subtle/common_enums.h" 31 #include "tink/subtle/prf/prf_set_util.h" 32 #include "tink/subtle/random.h" 33 #include "tink/subtle/stateful_hmac_boringssl.h" 34 #include "tink/util/constants.h" 35 #include "tink/util/enums.h" 36 #include "tink/util/errors.h" 37 #include "tink/util/protobuf_helper.h" 38 #include "tink/util/secret_data.h" 39 #include "tink/util/status.h" 40 #include "tink/util/statusor.h" 41 #include "tink/util/validation.h" 42 #include "proto/hmac_prf.pb.h" 43 #include "proto/tink.pb.h" 44 45 namespace crypto { 46 namespace tink { 47 48 class HmacPrfKeyManager 49 : public KeyTypeManager<google::crypto::tink::HmacPrfKey, 50 google::crypto::tink::HmacPrfKeyFormat, List<Prf>> { 51 public: 52 class PrfFactory : public PrimitiveFactory<Prf> { Create(const google::crypto::tink::HmacPrfKey & key)53 crypto::tink::util::StatusOr<std::unique_ptr<Prf>> Create( 54 const google::crypto::tink::HmacPrfKey& key) const override { 55 return subtle::CreatePrfFromStatefulMacFactory( 56 absl::make_unique<subtle::StatefulHmacBoringSslFactory>( 57 util::Enums::ProtoToSubtle(key.params().hash()), 58 MaxOutputLength(util::Enums::ProtoToSubtle(key.params().hash())), 59 util::SecretDataFromStringView(key.key_value()))); 60 } 61 }; 62 HmacPrfKeyManager()63 HmacPrfKeyManager() 64 : KeyTypeManager(absl::make_unique<HmacPrfKeyManager::PrfFactory>()) {} 65 get_version()66 uint32_t get_version() const override { return 0; } 67 key_material_type()68 google::crypto::tink::KeyData::KeyMaterialType key_material_type() 69 const override { 70 return google::crypto::tink::KeyData::SYMMETRIC; 71 } 72 MaxOutputLength(subtle::HashType hash_type)73 static uint64_t MaxOutputLength(subtle::HashType hash_type) { 74 static std::map<subtle::HashType, uint64_t>* max_output_length = 75 new std::map<subtle::HashType, uint64_t>( 76 {{subtle::HashType::SHA1, 20}, 77 {subtle::HashType::SHA256, 32}, 78 {subtle::HashType::SHA512, 64}}); 79 auto length_it = max_output_length->find(hash_type); 80 if (length_it == max_output_length->end()) { 81 return 0; 82 } 83 return length_it->second; 84 } 85 get_key_type()86 const std::string& get_key_type() const override { return key_type_; } 87 88 crypto::tink::util::Status ValidateKey( 89 const google::crypto::tink::HmacPrfKey& key) const override; 90 91 crypto::tink::util::Status ValidateKeyFormat( 92 const google::crypto::tink::HmacPrfKeyFormat& key_format) const override; 93 94 crypto::tink::util::StatusOr<google::crypto::tink::HmacPrfKey> CreateKey( 95 const google::crypto::tink::HmacPrfKeyFormat& key_format) const override; 96 97 util::StatusOr<google::crypto::tink::HmacPrfKey> DeriveKey( 98 const google::crypto::tink::HmacPrfKeyFormat& hmac_prf_key_format, 99 InputStream* input_stream) const override; 100 FipsStatus()101 internal::FipsCompatibility FipsStatus() const override { 102 return internal::FipsCompatibility::kRequiresBoringCrypto; 103 } 104 105 private: 106 util::Status ValidateParams( 107 const google::crypto::tink::HmacPrfParams& params) const; 108 109 const std::string key_type_ = absl::StrCat( 110 kTypeGoogleapisCom, google::crypto::tink::HmacPrfKey().GetTypeName()); 111 }; 112 113 } // namespace tink 114 } // namespace crypto 115 116 #endif // TINK_PRF_HMAC_PRF_KEY_MANAGER_H_ 117