xref: /aosp_15_r20/external/tink/cc/prf/prf_key_templates.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 #include "tink/prf/prf_key_templates.h"
17 
18 #include <memory>
19 
20 #include "absl/memory/memory.h"
21 #include "tink/prf/aes_cmac_prf_key_manager.h"
22 #include "tink/prf/hkdf_prf_key_manager.h"
23 #include "tink/prf/hmac_prf_key_manager.h"
24 #include "proto/aes_cmac_prf.pb.h"
25 #include "proto/hkdf_prf.pb.h"
26 #include "proto/hmac_prf.pb.h"
27 
28 namespace crypto {
29 namespace tink {
30 
31 namespace {
32 
33 using google::crypto::tink::AesCmacPrfKeyFormat;
34 using google::crypto::tink::HkdfPrfKeyFormat;
35 using google::crypto::tink::HmacPrfKeyFormat;
36 
NewHkdfSha256Template()37 std::unique_ptr<google::crypto::tink::KeyTemplate> NewHkdfSha256Template() {
38   auto key_template = absl::make_unique<google::crypto::tink::KeyTemplate>();
39   auto hkdf_prf_key_manager = absl::make_unique<HkdfPrfKeyManager>();
40   key_template->set_type_url(hkdf_prf_key_manager->get_key_type());
41   key_template->set_output_prefix_type(
42       google::crypto::tink::OutputPrefixType::RAW);
43   HkdfPrfKeyFormat key_format;
44   key_format.set_key_size(32);
45   key_format.set_version(hkdf_prf_key_manager->get_version());
46   key_format.mutable_params()->set_hash(google::crypto::tink::HashType::SHA256);
47   key_format.SerializeToString(key_template->mutable_value());
48   return key_template;
49 }
50 
NewHmacTemplate(google::crypto::tink::HashType hash_type,uint32_t key_size)51 std::unique_ptr<google::crypto::tink::KeyTemplate> NewHmacTemplate(
52     google::crypto::tink::HashType hash_type, uint32_t key_size) {
53   auto key_template = absl::make_unique<google::crypto::tink::KeyTemplate>();
54   auto hmac_prf_key_manager = absl::make_unique<HmacPrfKeyManager>();
55   key_template->set_type_url(hmac_prf_key_manager->get_key_type());
56   key_template->set_output_prefix_type(
57       google::crypto::tink::OutputPrefixType::RAW);
58   HmacPrfKeyFormat key_format;
59   key_format.set_key_size(key_size);
60   key_format.set_version(hmac_prf_key_manager->get_version());
61   key_format.mutable_params()->set_hash(hash_type);
62   key_format.SerializeToString(key_template->mutable_value());
63   return key_template;
64 }
65 
NewAesCmacTemplate()66 std::unique_ptr<google::crypto::tink::KeyTemplate> NewAesCmacTemplate() {
67   auto key_template = absl::make_unique<google::crypto::tink::KeyTemplate>();
68   auto aes_cmac_prf_key_manager = absl::make_unique<AesCmacPrfKeyManager>();
69   key_template->set_type_url(aes_cmac_prf_key_manager->get_key_type());
70   key_template->set_output_prefix_type(
71       google::crypto::tink::OutputPrefixType::RAW);
72   AesCmacPrfKeyFormat key_format;
73   key_format.set_version(aes_cmac_prf_key_manager->get_version());
74   key_format.set_key_size(32);
75   key_format.SerializeToString(key_template->mutable_value());
76   return key_template;
77 }
78 
79 }  // namespace
80 
HkdfSha256()81 const google::crypto::tink::KeyTemplate& PrfKeyTemplates::HkdfSha256() {
82   static const google::crypto::tink::KeyTemplate* key_template =
83       NewHkdfSha256Template().release();
84   return *key_template;
85 }
86 
HmacSha256()87 const google::crypto::tink::KeyTemplate& PrfKeyTemplates::HmacSha256() {
88   static const google::crypto::tink::KeyTemplate* key_template =
89       NewHmacTemplate(google::crypto::tink::HashType::SHA256, 32).release();
90   return *key_template;
91 }
92 
HmacSha512()93 const google::crypto::tink::KeyTemplate& PrfKeyTemplates::HmacSha512() {
94   static const google::crypto::tink::KeyTemplate* key_template =
95       NewHmacTemplate(google::crypto::tink::HashType::SHA512, 64).release();
96   return *key_template;
97 }
98 
AesCmac()99 const google::crypto::tink::KeyTemplate& PrfKeyTemplates::AesCmac() {
100   static const google::crypto::tink::KeyTemplate* key_template =
101       NewAesCmacTemplate().release();
102   return *key_template;
103 }
104 
105 }  // namespace tink
106 }  // namespace crypto
107