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/subtle/ecdsa_verify_boringssl.h"
18
19 #include <memory>
20 #include <string>
21 #include <utility>
22
23 #include "absl/status/status.h"
24 #include "absl/strings/str_cat.h"
25 #include "openssl/bn.h"
26 #include "openssl/ec.h"
27 #include "openssl/ecdsa.h"
28 #include "openssl/evp.h"
29 #include "tink/internal/ec_util.h"
30 #include "tink/internal/err_util.h"
31 #include "tink/internal/md_util.h"
32 #include "tink/internal/ssl_unique_ptr.h"
33 #include "tink/internal/util.h"
34 #include "tink/subtle/common_enums.h"
35 #include "tink/subtle/subtle_util_boringssl.h"
36 #include "tink/util/errors.h"
37
38 namespace crypto {
39 namespace tink {
40 namespace subtle {
41
New(const SubtleUtilBoringSSL::EcKey & ec_key,HashType hash_type,EcdsaSignatureEncoding encoding)42 util::StatusOr<std::unique_ptr<EcdsaVerifyBoringSsl>> EcdsaVerifyBoringSsl::New(
43 const SubtleUtilBoringSSL::EcKey& ec_key, HashType hash_type,
44 EcdsaSignatureEncoding encoding) {
45 // Check curve.
46 auto group_result = internal::EcGroupFromCurveType(ec_key.curve);
47 if (!group_result.ok()) return group_result.status();
48 internal::SslUniquePtr<EC_GROUP> group = std::move(group_result.value());
49 internal::SslUniquePtr<EC_KEY> key(EC_KEY_new());
50 EC_KEY_set_group(key.get(), group.get());
51
52 // Check key.
53 auto ec_point_result =
54 internal::GetEcPoint(ec_key.curve, ec_key.pub_x, ec_key.pub_y);
55 if (!ec_point_result.ok()) return ec_point_result.status();
56 internal::SslUniquePtr<EC_POINT> pub_key = std::move(ec_point_result.value());
57 if (!EC_KEY_set_public_key(key.get(), pub_key.get())) {
58 return util::Status(
59 absl::StatusCode::kInvalidArgument,
60 absl::StrCat("Invalid public key: ", internal::GetSslErrors()));
61 }
62 return New(std::move(key), hash_type, encoding);
63 }
64
New(internal::SslUniquePtr<EC_KEY> ec_key,HashType hash_type,EcdsaSignatureEncoding encoding)65 util::StatusOr<std::unique_ptr<EcdsaVerifyBoringSsl>> EcdsaVerifyBoringSsl::New(
66 internal::SslUniquePtr<EC_KEY> ec_key, HashType hash_type,
67 EcdsaSignatureEncoding encoding) {
68 util::Status status =
69 internal::CheckFipsCompatibility<EcdsaVerifyBoringSsl>();
70 if (!status.ok()) {
71 return status;
72 }
73
74 // Check if the hash type is safe to use.
75 util::Status is_safe = internal::IsHashTypeSafeForSignature(hash_type);
76 if (!is_safe.ok()) {
77 return is_safe;
78 }
79 util::StatusOr<const EVP_MD*> hash = internal::EvpHashFromHashType(hash_type);
80 if (!hash.ok()) {
81 return hash.status();
82 }
83 std::unique_ptr<EcdsaVerifyBoringSsl> verify(
84 new EcdsaVerifyBoringSsl(std::move(ec_key), *hash, encoding));
85 return std::move(verify);
86 }
87
Verify(absl::string_view signature,absl::string_view data) const88 util::Status EcdsaVerifyBoringSsl::Verify(absl::string_view signature,
89 absl::string_view data) const {
90 // BoringSSL expects a non-null pointer for data,
91 // regardless of whether the size is 0.
92 data = internal::EnsureStringNonNull(data);
93
94 // Compute the digest.
95 unsigned int digest_size;
96 uint8_t digest[EVP_MAX_MD_SIZE];
97 if (1 != EVP_Digest(data.data(), data.size(), digest, &digest_size, hash_,
98 nullptr)) {
99 return util::Status(absl::StatusCode::kInternal,
100 "Could not compute digest.");
101 }
102
103 std::string derSig(signature);
104 if (encoding_ == subtle::EcdsaSignatureEncoding::IEEE_P1363) {
105 const EC_GROUP* group = EC_KEY_get0_group(key_.get());
106 auto status_or_der = internal::EcSignatureIeeeToDer(group, signature);
107
108 if (!status_or_der.ok()) {
109 return status_or_der.status();
110 }
111 derSig = status_or_der.value();
112 }
113
114 // Verify the signature.
115 if (1 != ECDSA_verify(0 /* unused */, digest, digest_size,
116 reinterpret_cast<const uint8_t*>(derSig.data()),
117 derSig.size(), key_.get())) {
118 // signature is invalid
119 return util::Status(absl::StatusCode::kInvalidArgument,
120 "Signature is not valid.");
121 }
122 // signature is valid
123 return util::OkStatus();
124 }
125
126 } // namespace subtle
127 } // namespace tink
128 } // namespace crypto
129