xref: /aosp_15_r20/external/tink/cc/signature/ed25519_verify_key_manager.cc (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 
17 #include "tink/signature/ed25519_verify_key_manager.h"
18 
19 #include <memory>
20 
21 #include "absl/status/status.h"
22 #include "absl/strings/string_view.h"
23 #include "tink/public_key_verify.h"
24 #include "tink/subtle/ed25519_verify_boringssl.h"
25 #include "tink/util/errors.h"
26 #include "tink/util/protobuf_helper.h"
27 #include "tink/util/status.h"
28 #include "tink/util/statusor.h"
29 #include "tink/util/validation.h"
30 #include "proto/ed25519.pb.h"
31 
32 namespace crypto {
33 namespace tink {
34 
35 using crypto::tink::util::Status;
36 using crypto::tink::util::StatusOr;
37 using google::crypto::tink::Ed25519PublicKey;
38 
39 StatusOr<std::unique_ptr<PublicKeyVerify>>
Create(const Ed25519PublicKey & public_key) const40 Ed25519VerifyKeyManager::PublicKeyVerifyFactory::Create(
41     const Ed25519PublicKey& public_key) const {
42   return subtle::Ed25519VerifyBoringSsl::New(public_key.key_value());
43 }
44 
ValidateKey(const Ed25519PublicKey & key) const45 Status Ed25519VerifyKeyManager::ValidateKey(const Ed25519PublicKey& key) const {
46   Status status = ValidateVersion(key.version(), get_version());
47   if (!status.ok()) return status;
48 
49   if (key.key_value().length() != 32) {
50     return Status(absl::StatusCode::kInvalidArgument,
51                   "The ED25519 public key must be 32-bytes long.");
52   }
53   return util::OkStatus();
54 }
55 
56 }  // namespace tink
57 }  // namespace crypto
58