xref: /aosp_15_r20/external/tink/cc/aead/aes_gcm_siv_key_manager.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2019 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 #ifndef TINK_AEAD_AES_GCM_SIV_KEY_MANAGER_H_
17 #define TINK_AEAD_AES_GCM_SIV_KEY_MANAGER_H_
18 
19 #include <stdint.h>
20 
21 #include <memory>
22 #include <string>
23 
24 #include "absl/memory/memory.h"
25 #include "absl/strings/str_cat.h"
26 #include "tink/aead.h"
27 #include "tink/core/key_type_manager.h"
28 #include "tink/core/template_util.h"
29 #include "tink/subtle/aes_gcm_siv_boringssl.h"
30 #include "tink/subtle/random.h"
31 #include "tink/util/constants.h"
32 #include "tink/util/secret_data.h"
33 #include "tink/util/status.h"
34 #include "tink/util/statusor.h"
35 #include "tink/util/validation.h"
36 #include "proto/aes_gcm_siv.pb.h"
37 #include "proto/tink.pb.h"
38 
39 namespace crypto {
40 namespace tink {
41 
42 class AesGcmSivKeyManager
43     : public KeyTypeManager<google::crypto::tink::AesGcmSivKey,
44                             google::crypto::tink::AesGcmSivKeyFormat,
45                             List<Aead>> {
46  public:
47   class AeadFactory : public PrimitiveFactory<Aead> {
Create(const google::crypto::tink::AesGcmSivKey & key)48     crypto::tink::util::StatusOr<std::unique_ptr<Aead>> Create(
49         const google::crypto::tink::AesGcmSivKey& key) const override {
50       return subtle::AesGcmSivBoringSsl::New(
51           util::SecretDataFromStringView(key.key_value()));
52     }
53   };
54 
AesGcmSivKeyManager()55   AesGcmSivKeyManager() : KeyTypeManager(absl::make_unique<AeadFactory>()) {}
56 
get_version()57   uint32_t get_version() const override { return 0; }
58 
key_material_type()59   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
60       const override {
61     return google::crypto::tink::KeyData::SYMMETRIC;
62   }
63 
get_key_type()64   const std::string& get_key_type() const override { return key_type_; }
65 
ValidateKey(const google::crypto::tink::AesGcmSivKey & key)66   crypto::tink::util::Status ValidateKey(
67       const google::crypto::tink::AesGcmSivKey& key) const override {
68     crypto::tink::util::Status status =
69         ValidateVersion(key.version(), get_version());
70     if (!status.ok()) return status;
71     return ValidateAesKeySize(key.key_value().size());
72   }
73 
ValidateKeyFormat(const google::crypto::tink::AesGcmSivKeyFormat & format)74   crypto::tink::util::Status ValidateKeyFormat(
75       const google::crypto::tink::AesGcmSivKeyFormat& format) const override {
76     return ValidateAesKeySize(format.key_size());
77   }
78 
CreateKey(const google::crypto::tink::AesGcmSivKeyFormat & format)79   crypto::tink::util::StatusOr<google::crypto::tink::AesGcmSivKey> CreateKey(
80       const google::crypto::tink::AesGcmSivKeyFormat& format)
81       const override {
82     google::crypto::tink::AesGcmSivKey key;
83     key.set_version(get_version());
84     key.set_key_value(subtle::Random::GetRandomBytes(format.key_size()));
85     return key;
86   }
87 
88  private:
89   const std::string key_type_ = absl::StrCat(
90       kTypeGoogleapisCom, google::crypto::tink::AesGcmSivKey().GetTypeName());
91 };
92 
93 }  // namespace tink
94 }  // namespace crypto
95 
96 #endif  // TINK_AEAD_AES_GCM_SIV_KEY_MANAGER_H_
97