1 // Copyright 2017 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/ecdsa_verify_key_manager.h"
18
19 #include <memory>
20 #include <utility>
21
22 #include "absl/status/status.h"
23 #include "absl/strings/string_view.h"
24 #include "tink/internal/ec_util.h"
25 #include "tink/public_key_verify.h"
26 #include "tink/subtle/ecdsa_verify_boringssl.h"
27 #include "tink/util/enums.h"
28 #include "tink/util/errors.h"
29 #include "tink/util/protobuf_helper.h"
30 #include "tink/util/status.h"
31 #include "tink/util/statusor.h"
32 #include "tink/util/validation.h"
33 #include "proto/ecdsa.pb.h"
34
35 namespace crypto {
36 namespace tink {
37
38 using crypto::tink::util::Enums;
39 using crypto::tink::util::Status;
40 using crypto::tink::util::StatusOr;
41 using google::crypto::tink::EcdsaParams;
42 using google::crypto::tink::EcdsaPublicKey;
43 using google::crypto::tink::EcdsaSignatureEncoding;
44 using google::crypto::tink::EllipticCurveType;
45 using google::crypto::tink::HashType;
46
47 StatusOr<std::unique_ptr<PublicKeyVerify>>
Create(const EcdsaPublicKey & ecdsa_public_key) const48 EcdsaVerifyKeyManager::PublicKeyVerifyFactory::Create(
49 const EcdsaPublicKey& ecdsa_public_key) const {
50 internal::EcKey ec_key;
51 ec_key.curve = Enums::ProtoToSubtle(ecdsa_public_key.params().curve());
52 ec_key.pub_x = ecdsa_public_key.x();
53 ec_key.pub_y = ecdsa_public_key.y();
54 auto result = subtle::EcdsaVerifyBoringSsl::New(
55 ec_key, Enums::ProtoToSubtle(ecdsa_public_key.params().hash_type()),
56 Enums::ProtoToSubtle(ecdsa_public_key.params().encoding()));
57 if (!result.ok()) return result.status();
58 return {std::move(result.value())};
59 }
60
ValidateParams(const EcdsaParams & params) const61 Status EcdsaVerifyKeyManager::ValidateParams(const EcdsaParams& params) const {
62 switch (params.encoding()) {
63 case EcdsaSignatureEncoding::DER: // fall through
64 case EcdsaSignatureEncoding::IEEE_P1363:
65 break;
66 default:
67 return ToStatusF(absl::StatusCode::kInvalidArgument,
68 "Unsupported signature encoding: %d", params.encoding());
69 }
70 switch (params.curve()) {
71 case EllipticCurveType::NIST_P256:
72 // Using SHA512 for curve P256 is fine. However, only the 256
73 // leftmost bits of the hash is used in signature computation.
74 // Therefore, we don't allow it here to prevent security illusion.
75 if (params.hash_type() != HashType::SHA256) {
76 return Status(absl::StatusCode::kInvalidArgument,
77 "Only SHA256 is supported for NIST P256.");
78 }
79 break;
80 case EllipticCurveType::NIST_P384:
81 // Allow using SHA384 and SHA512 with NIST-P384.
82 if ((params.hash_type() != HashType::SHA384) &&
83 (params.hash_type() != HashType::SHA512)) {
84 return Status(absl::StatusCode::kInvalidArgument,
85 "Only SHA384 and SHA512 are supported for this curve.");
86 }
87 break;
88 case EllipticCurveType::NIST_P521:
89 if (params.hash_type() != HashType::SHA512) {
90 return Status(absl::StatusCode::kInvalidArgument,
91 "Only SHA512 is supported for this curve.");
92 }
93 break;
94 default:
95 return Status(absl::StatusCode::kInvalidArgument,
96 "Unsupported elliptic curve");
97 }
98 return util::OkStatus();
99 }
100
ValidateKey(const EcdsaPublicKey & key) const101 Status EcdsaVerifyKeyManager::ValidateKey(const EcdsaPublicKey& key) const {
102 Status status = ValidateVersion(key.version(), get_version());
103 if (!status.ok()) return status;
104 return ValidateParams(key.params());
105 }
106
107 } // namespace tink
108 } // namespace crypto
109