1 // Copyright 2019 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/keyderivation/key_derivation_key_templates.h" 18 19 #include <memory> 20 21 #include "tink/keyderivation/internal/prf_based_deriver_key_manager.h" 22 #include "tink/subtle/random.h" 23 24 namespace crypto { 25 namespace tink { 26 27 using ::google::crypto::tink::KeyTemplate; 28 using ::google::crypto::tink::PrfBasedDeriverKeyFormat; 29 30 util::StatusOr<KeyTemplate> CreatePrfBasedKeyTemplate(const KeyTemplate & prf_key_template,const KeyTemplate & derived_key_template)31KeyDerivationKeyTemplates::CreatePrfBasedKeyTemplate( 32 const KeyTemplate& prf_key_template, 33 const KeyTemplate& derived_key_template) { 34 KeyTemplate key_template; 35 key_template.set_type_url( 36 internal::PrfBasedDeriverKeyManager().get_key_type()); 37 key_template.set_output_prefix_type( 38 derived_key_template.output_prefix_type()); 39 40 PrfBasedDeriverKeyFormat format; 41 *format.mutable_prf_key_template() = prf_key_template; 42 *format.mutable_params()->mutable_derived_key_template() = 43 derived_key_template; 44 format.SerializeToString(key_template.mutable_value()); 45 46 // Verify `key_template` is derivable. 47 util::StatusOr<std::unique_ptr<KeysetHandle>> handle = 48 KeysetHandle::GenerateNew(key_template); 49 if (!handle.ok()) { 50 return handle.status(); 51 } 52 util::StatusOr<std::unique_ptr<KeysetDeriver>> deriver = 53 (*handle)->GetPrimitive<KeysetDeriver>(); 54 if (!deriver.ok()) { 55 return deriver.status(); 56 } 57 58 return key_template; 59 } 60 61 } // namespace tink 62 } // namespace crypto 63