xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/signature/falcon_sign_key_manager.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 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_EXPERIMENTAL_PQCRYPTO_SIGNATURE_FALCON_SIGN_KEY_MANAGER_H_
18 #define TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_FALCON_SIGN_KEY_MANAGER_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_cat.h"
25 #include "tink/core/private_key_type_manager.h"
26 #include "tink/public_key_sign.h"
27 #include "tink/util/constants.h"
28 #include "tink/util/errors.h"
29 #include "tink/util/input_stream_util.h"
30 #include "tink/util/protobuf_helper.h"
31 #include "tink/util/status.h"
32 #include "tink/util/statusor.h"
33 #include "proto/experimental/pqcrypto/falcon.pb.h"
34 
35 namespace crypto {
36 namespace tink {
37 
38 class FalconSignKeyManager
39     : public PrivateKeyTypeManager<google::crypto::tink::FalconPrivateKey,
40                                    google::crypto::tink::FalconKeyFormat,
41                                    google::crypto::tink::FalconPublicKey,
42                                    List<PublicKeySign>> {
43  public:
44   class PublicKeySignFactory : public PrimitiveFactory<PublicKeySign> {
45     crypto::tink::util::StatusOr<std::unique_ptr<PublicKeySign>> Create(
46         const google::crypto::tink::FalconPrivateKey& private_key)
47         const override;
48   };
49 
FalconSignKeyManager()50   FalconSignKeyManager()
51       : PrivateKeyTypeManager(absl::make_unique<PublicKeySignFactory>()) {}
52 
get_version()53   uint32_t get_version() const override { return 0; }
54 
key_material_type()55   google::crypto::tink::KeyData::KeyMaterialType key_material_type()
56       const override {
57     return google::crypto::tink::KeyData::ASYMMETRIC_PRIVATE;
58   }
59 
get_key_type()60   const std::string& get_key_type() const override { return key_type_; }
61 
62   crypto::tink::util::Status ValidateKey(
63       const google::crypto::tink::FalconPrivateKey& key) const override;
64 
65   crypto::tink::util::Status ValidateKeyFormat(
66       const google::crypto::tink::FalconKeyFormat& key_format)
67       const override;
68 
69   crypto::tink::util::StatusOr<google::crypto::tink::FalconPrivateKey>
70   CreateKey(const google::crypto::tink::FalconKeyFormat& key_format)
71       const override;
72 
73   crypto::tink::util::StatusOr<google::crypto::tink::FalconPublicKey>
GetPublicKey(const google::crypto::tink::FalconPrivateKey & private_key)74   GetPublicKey(const google::crypto::tink::FalconPrivateKey& private_key)
75       const override {
76     return private_key.public_key();
77   }
78 
79  private:
80   const std::string key_type_ =
81       absl::StrCat(kTypeGoogleapisCom,
82                    google::crypto::tink::FalconPrivateKey().GetTypeName());
83 };
84 
85 }  // namespace tink
86 }  // namespace crypto
87 
88 #endif  // TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_FALCON_SIGN_KEY_MANAGER_H_
89