xref: /aosp_15_r20/external/tink/cc/signature/ed25519_public_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_PUBLIC_KEY_H_
18 #define TINK_SIGNATURE_ED25519_PUBLIC_KEY_H_
19 
20 #include <string>
21 
22 #include "absl/types/optional.h"
23 #include "tink/partial_key_access_token.h"
24 #include "tink/signature/ed25519_parameters.h"
25 #include "tink/signature/signature_public_key.h"
26 #include "tink/util/statusor.h"
27 
28 namespace crypto {
29 namespace tink {
30 
31 class Ed25519PublicKey : public SignaturePublicKey {
32  public:
33   // Copyable and movable.
34   Ed25519PublicKey(const Ed25519PublicKey& other) = default;
35   Ed25519PublicKey& operator=(const Ed25519PublicKey& other) = default;
36   Ed25519PublicKey(Ed25519PublicKey&& other) = default;
37   Ed25519PublicKey& operator=(Ed25519PublicKey&& other) = default;
38 
39   // Creates a new Ed25519 public key from `public_key_bytes`. If `parameters`
40   // specify a variant that uses a prefix, then `id_requirement` is used to
41   // compute this prefix.
42   static util::StatusOr<Ed25519PublicKey> Create(
43       const Ed25519Parameters& parameters, absl::string_view public_key_bytes,
44       absl::optional<int> id_requirement, PartialKeyAccessToken token);
45 
GetPublicKeyBytes(PartialKeyAccessToken token)46   absl::string_view GetPublicKeyBytes(PartialKeyAccessToken token) const {
47     return public_key_bytes_;
48   }
49 
GetOutputPrefix()50   absl::string_view GetOutputPrefix() const override { return output_prefix_; }
51 
GetParameters()52   const Ed25519Parameters& GetParameters() const override {
53     return parameters_;
54   }
55 
GetIdRequirement()56   absl::optional<int> GetIdRequirement() const override {
57     return id_requirement_;
58   }
59 
60   bool operator==(const Key& other) const override;
61 
62  private:
Ed25519PublicKey(const Ed25519Parameters & parameters,absl::string_view public_key_bytes,absl::optional<int> id_requirement,absl::string_view output_prefix)63   explicit Ed25519PublicKey(const Ed25519Parameters& parameters,
64                             absl::string_view public_key_bytes,
65                             absl::optional<int> id_requirement,
66                             absl::string_view output_prefix)
67       : parameters_(parameters),
68         public_key_bytes_(public_key_bytes),
69         id_requirement_(id_requirement),
70         output_prefix_(output_prefix) {}
71 
72   Ed25519Parameters parameters_;
73   std::string public_key_bytes_;
74   absl::optional<int> id_requirement_;
75   std::string output_prefix_;
76 };
77 
78 }  // namespace tink
79 }  // namespace crypto
80 
81 #endif  // TINK_SIGNATURE_ED25519_PUBLIC_KEY_H_
82