xref: /aosp_15_r20/external/tink/cc/streamingaead/aes_ctr_hmac_streaming_key_manager.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 #ifndef TINK_STREAMINGAEAD_AES_CTR_HMAC_STREAMING_KEY_MANAGER_H_
17 #define TINK_STREAMINGAEAD_AES_CTR_HMAC_STREAMING_KEY_MANAGER_H_
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_cat.h"
25 #include "tink/core/key_type_manager.h"
26 #include "tink/streaming_aead.h"
27 #include "tink/subtle/aes_ctr_hmac_streaming.h"
28 #include "tink/util/constants.h"
29 #include "tink/util/enums.h"
30 #include "tink/util/errors.h"
31 #include "tink/util/protobuf_helper.h"
32 #include "tink/util/secret_data.h"
33 #include "tink/util/status.h"
34 #include "tink/util/statusor.h"
35 #include "proto/aes_ctr_hmac_streaming.pb.h"
36 #include "proto/hmac.pb.h"
37 #include "proto/tink.pb.h"
38 
39 namespace crypto {
40 namespace tink {
41 
42 class AesCtrHmacStreamingKeyManager
43     : public KeyTypeManager<google::crypto::tink::AesCtrHmacStreamingKey,
44                             google::crypto::tink::AesCtrHmacStreamingKeyFormat,
45                             List<StreamingAead>> {
46  public:
47   class StreamingAeadFactory
48       : public PrimitiveFactory<StreamingAead> {
Create(const google::crypto::tink::AesCtrHmacStreamingKey & key)49     crypto::tink::util::StatusOr<std::unique_ptr<StreamingAead>> Create(
50         const google::crypto::tink::AesCtrHmacStreamingKey& key)
51         const override {
52           subtle::AesCtrHmacStreaming::Params params;
53           params.ikm = util::SecretDataFromStringView(key.key_value());
54           params.hkdf_algo = crypto::tink::util::Enums::ProtoToSubtle(
55               key.params().hkdf_hash_type());
56           params.key_size = key.params().derived_key_size();
57           params.ciphertext_segment_size =
58               key.params().ciphertext_segment_size();
59           params.ciphertext_offset = 0;
60           params.tag_algo = crypto::tink::util::Enums::ProtoToSubtle(
61               key.params().hmac_params().hash());
62           params.tag_size = key.params().hmac_params().tag_size();
63           auto streaming_result =
64               crypto::tink::subtle::AesCtrHmacStreaming::New(params);
65           if (!streaming_result.ok()) return streaming_result.status();
66           return {std::move(streaming_result.value())};
67         }
68   };
69 
AesCtrHmacStreamingKeyManager()70   AesCtrHmacStreamingKeyManager()
71       : KeyTypeManager(
72             absl::make_unique<AesCtrHmacStreamingKeyManager::
73                                   StreamingAeadFactory>()) {}
74 
75   // Returns the version of this key manager.
get_version()76   uint32_t get_version() const override { return 0; }
77 
key_material_type()78   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
79       const override {
80     return google::crypto::tink::KeyData::SYMMETRIC;
81   }
82 
get_key_type()83   const std::string& get_key_type() const override { return key_type_; }
84 
85   crypto::tink::util::Status ValidateKey(
86       const google::crypto::tink::AesCtrHmacStreamingKey& key) const override;
87 
88   crypto::tink::util::Status ValidateKeyFormat(
89       const google::crypto::tink::AesCtrHmacStreamingKeyFormat& key_format)
90       const override;
91 
92   crypto::tink::util::StatusOr<google::crypto::tink::AesCtrHmacStreamingKey>
93   CreateKey(const google::crypto::tink::AesCtrHmacStreamingKeyFormat&
94                 key_format) const override;
95 
96   crypto::tink::util::StatusOr<google::crypto::tink::AesCtrHmacStreamingKey>
97   DeriveKey(
98       const google::crypto::tink::AesCtrHmacStreamingKeyFormat& key_format,
99       InputStream* input_stream) const override;
100 
101   ~AesCtrHmacStreamingKeyManager() override = default;
102 
103  private:
104   const std::string key_type_ = absl::StrCat(
105       kTypeGoogleapisCom,
106       google::crypto::tink::AesCtrHmacStreamingKey().GetTypeName());
107 };
108 
109 }  // namespace tink
110 }  // namespace crypto
111 
112 #endif  // TINK_STREAMINGAEAD_AES_CTR_HMAC_STREAMING_KEY_MANAGER_H_
113