Avi Drissman | 201a9a83 | 2022-09-13 19:39:25 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 5 | #include "crypto/ec_signature_creator_impl.h" |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 9 | |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 10 | #include "crypto/ec_private_key.h" |
| 11 | #include "crypto/openssl_util.h" |
tfarina | 29a3a174 | 2016-10-28 18:47:33 | [diff] [blame] | 12 | #include "third_party/boringssl/src/include/openssl/bn.h" |
| 13 | #include "third_party/boringssl/src/include/openssl/ec.h" |
| 14 | #include "third_party/boringssl/src/include/openssl/ecdsa.h" |
| 15 | #include "third_party/boringssl/src/include/openssl/evp.h" |
| 16 | #include "third_party/boringssl/src/include/openssl/sha.h" |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 17 | |
| 18 | namespace crypto { |
| 19 | |
David Benjamin | 0a02b73e8 | 2024-06-28 18:15:02 | [diff] [blame] | 20 | ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) : key_(key) {} |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 21 | |
Chris Watkins | a850a30 | 2017-11-30 03:53:49 | [diff] [blame] | 22 | ECSignatureCreatorImpl::~ECSignatureCreatorImpl() = default; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 23 | |
Hubert Chao | 7248d583 | 2021-07-21 16:33:26 | [diff] [blame] | 24 | bool ECSignatureCreatorImpl::Sign(base::span<const uint8_t> data, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 25 | std::vector<uint8_t>* signature) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 26 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 27 | bssl::ScopedEVP_MD_CTX ctx; |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 28 | size_t sig_len = 0; |
| 29 | if (!ctx.get() || |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 30 | !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr, |
| 31 | key_->key()) || |
Hubert Chao | 7248d583 | 2021-07-21 16:33:26 | [diff] [blame] | 32 | !EVP_DigestSignUpdate(ctx.get(), data.data(), data.size()) || |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 33 | !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 34 | return false; |
| 35 | } |
| 36 | |
| 37 | signature->resize(sig_len); |
| 38 | if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) |
| 39 | return false; |
| 40 | |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 41 | // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter |
| 42 | // returns a maximum allocation size, while the call without a nullptr |
| 43 | // returns the real one, which may be smaller. |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 44 | signature->resize(sig_len); |
| 45 | return true; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 46 | } |
| 47 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 48 | bool ECSignatureCreatorImpl::DecodeSignature( |
| 49 | const std::vector<uint8_t>& der_sig, |
| 50 | std::vector<uint8_t>* out_raw_sig) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 51 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 52 | // Create ECDSA_SIG object from DER-encoded data. |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 53 | bssl::UniquePtr<ECDSA_SIG> ecdsa_sig( |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 54 | ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 55 | if (!ecdsa_sig.get()) |
| 56 | return false; |
| 57 | |
| 58 | // The result is made of two 32-byte vectors. |
| 59 | const size_t kMaxBytesPerBN = 32; |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 60 | std::vector<uint8_t> result(2 * kMaxBytesPerBN); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 61 | |
eroman | 0f4b2770 | 2014-11-10 23:43:38 | [diff] [blame] | 62 | if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
| 63 | !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
| 64 | ecdsa_sig->s)) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 65 | return false; |
| 66 | } |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 67 | out_raw_sig->swap(result); |
| 68 | return true; |
[email protected] | 7c3090a0 | 2012-09-19 15:11:33 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 71 | } // namespace crypto |