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