xref: /aosp_15_r20/external/tink/cc/hybrid/ecies_aead_hkdf_dem_helper.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 Google Inc.
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_ECIES_AEAD_HKDF_DEM_HELPER_H_
18 #define TINK_HYBRID_ECIES_AEAD_HKDF_DEM_HELPER_H_
19 
20 #include <stdint.h>
21 
22 #include <memory>
23 
24 #include "tink/aead.h"
25 #include "tink/daead/subtle/aead_or_daead.h"
26 #include "tink/util/protobuf_helper.h"
27 #include "tink/util/secret_data.h"
28 #include "tink/util/statusor.h"
29 #include "proto/common.pb.h"
30 #include "proto/tink.pb.h"
31 
32 namespace crypto {
33 namespace tink {
34 
35 // A helper for DEM (data encapsulation mechanism) of ECIES-AEAD-HKDF.
36 class EciesAeadHkdfDemHelper {
37  public:
38   // Constructs a new helper for the specified DEM key template.
39   static
40   crypto::tink::util::StatusOr<std::unique_ptr<const EciesAeadHkdfDemHelper>>
41       New(const google::crypto::tink::KeyTemplate& dem_key_template);
42 
43   virtual ~EciesAeadHkdfDemHelper() = default;
44 
45   // Returns the size of the DEM-key in bytes.
dem_key_size_in_bytes()46   uint32_t dem_key_size_in_bytes() const {
47     return key_params_.key_size_in_bytes;
48   }
49 
50   // Creates and returns a new AeadOrDaead object that uses
51   // the key material given in 'symmetric_key', which must
52   // be of length dem_key_size_in_bytes().
53   virtual crypto::tink::util::StatusOr<
54       std::unique_ptr<crypto::tink::subtle::AeadOrDaead>>
55   GetAeadOrDaead(const util::SecretData& symmetric_key_value) const;
56 
57  protected:
58   enum DemKeyType {
59     AES_GCM_KEY,
60     AES_CTR_HMAC_AEAD_KEY,
61     XCHACHA20_POLY1305_KEY,
62     AES_SIV_KEY,
63   };
64 
65   struct DemKeyParams {
66     DemKeyType key_type;
67     uint32_t key_size_in_bytes;
68     uint32_t aes_ctr_key_size_in_bytes;
69     uint32_t aes_ctr_key_iv_size_in_bytes;
70     google::crypto::tink::HashType hmac_key_hash;
71     uint32_t hmac_key_tag_size_in_bytes;
72   };
73 
EciesAeadHkdfDemHelper(const google::crypto::tink::KeyTemplate & key_template,DemKeyParams key_params)74   EciesAeadHkdfDemHelper(const google::crypto::tink::KeyTemplate& key_template,
75                          DemKeyParams key_params)
76       : key_template_(key_template), key_params_(key_params) {}
77 
78   static util::StatusOr<DemKeyParams> GetKeyParams(
79       const ::google::crypto::tink::KeyTemplate& key_template);
80 
81   const google::crypto::tink::KeyTemplate key_template_;
82   const DemKeyParams key_params_;
83 };
84 
85 }  // namespace tink
86 }  // namespace crypto
87 
88 #endif  // TINK_HYBRID_ECIES_AEAD_HKDF_DEM_HELPER_H_
89