xref: /aosp_15_r20/external/tink/cc/keyderivation/key_derivation_key_templates.h (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 
17 #ifndef TINK_KEYDERIVATION_KEY_DERIVATION_KEY_TEMPLATES_H_
18 #define TINK_KEYDERIVATION_KEY_DERIVATION_KEY_TEMPLATES_H_
19 
20 #include "tink/util/statusor.h"
21 #include "proto/tink.pb.h"
22 
23 namespace crypto {
24 namespace tink {
25 
26 ///////////////////////////////////////////////////////////////////////////////
27 // Methods to generate KeyTemplates for key derivation.
28 class KeyDerivationKeyTemplates {
29  public:
30   // Creates a key template for key derivation that uses a PRF to derive a key
31   // that adheres to `derived_key_template`. The following must be true:
32   //   (1) `prf_key_template` is a PRF key template, i.e.
33   //         `keyset_handle->GetPrimitive<StreamingPrf>()` works.
34   //   (2) `derived_key_template` describes a key type that supports derivation.
35   //
36   // The output prefix type of the derived key will match the output prefix type
37   // of `derived_key_template`.
38   //
39   // This function verifies the newly created key template by creating a
40   // KeysetDeriver primitive from it. This requires both the `prf_key_template`
41   // and `derived_key_template` key types to be in the registry. It also
42   // attempts to derive a key, returning an error on failure.
43   static util::StatusOr<google::crypto::tink::KeyTemplate>
44   CreatePrfBasedKeyTemplate(
45       const google::crypto::tink::KeyTemplate& prf_key_template,
46       const google::crypto::tink::KeyTemplate& derived_key_template);
47 };
48 
49 }  // namespace tink
50 }  // namespace crypto
51 
52 #endif  // TINK_KEYDERIVATION_KEY_DERIVATION_KEY_TEMPLATES_H_
53