xref: /aosp_15_r20/external/tink/cc/signature/ed25519_private_key.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2023 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_SIGNATURE_ED25519_PRIVATE_KEY_H_
18 #define TINK_SIGNATURE_ED25519_PRIVATE_KEY_H_
19 
20 #include "tink/partial_key_access_token.h"
21 #include "tink/restricted_data.h"
22 #include "tink/signature/ed25519_public_key.h"
23 #include "tink/signature/signature_private_key.h"
24 #include "tink/util/statusor.h"
25 
26 namespace crypto {
27 namespace tink {
28 
29 class Ed25519PrivateKey : public SignaturePrivateKey {
30  public:
31   // Copyable and movable.
32   Ed25519PrivateKey(const Ed25519PrivateKey& other) = default;
33   Ed25519PrivateKey& operator=(const Ed25519PrivateKey& other) = default;
34   Ed25519PrivateKey(Ed25519PrivateKey&& other) = default;
35   Ed25519PrivateKey& operator=(Ed25519PrivateKey&& other) = default;
36 
37   // Creates a new Ed25519 private key from `private_key_bytes`. Returns an
38   // error if `public_key` does not belong to the same key pair as
39   // `private_key_bytes`.
40   static util::StatusOr<Ed25519PrivateKey> Create(
41       const Ed25519PublicKey& public_key,
42       const RestrictedData& private_key_bytes, PartialKeyAccessToken token);
43 
GetPrivateKeyBytes(PartialKeyAccessToken token)44   const RestrictedData& GetPrivateKeyBytes(PartialKeyAccessToken token) const {
45     return private_key_bytes_;
46   }
47 
GetPublicKey()48   const Ed25519PublicKey& GetPublicKey() const override { return public_key_; }
49 
50   bool operator==(const Key& other) const override;
51 
52  private:
Ed25519PrivateKey(const Ed25519PublicKey & public_key,const RestrictedData & private_key_bytes)53   explicit Ed25519PrivateKey(const Ed25519PublicKey& public_key,
54                              const RestrictedData& private_key_bytes)
55       : public_key_(public_key), private_key_bytes_(private_key_bytes) {}
56 
57   Ed25519PublicKey public_key_;
58   RestrictedData private_key_bytes_;
59 };
60 
61 }  // namespace tink
62 }  // namespace crypto
63 
64 #endif  // TINK_SIGNATURE_ED25519_PRIVATE_KEY_H_
65