[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 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 | |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 20 | ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
mlamouri | ef55346a | 2015-10-21 01:23:09 | [diff] [blame] | 21 | : key_(key) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 22 | EnsureOpenSSLInit(); |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 23 | } |
| 24 | |
Chris Watkins | a850a30 | 2017-11-30 03:53:49 | [diff] [blame] | 25 | ECSignatureCreatorImpl::~ECSignatureCreatorImpl() = default; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 26 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 27 | bool ECSignatureCreatorImpl::Sign(const uint8_t* data, |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 28 | int data_len, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 29 | std::vector<uint8_t>* signature) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 30 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 31 | bssl::ScopedEVP_MD_CTX ctx; |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 32 | size_t sig_len = 0; |
| 33 | if (!ctx.get() || |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 34 | !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr, |
| 35 | key_->key()) || |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 36 | !EVP_DigestSignUpdate(ctx.get(), data, data_len) || |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 37 | !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 38 | return false; |
| 39 | } |
| 40 | |
| 41 | signature->resize(sig_len); |
| 42 | if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) |
| 43 | return false; |
| 44 | |
rsleevi | ffe5a13 | 2016-06-28 01:51:52 | [diff] [blame] | 45 | // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter |
| 46 | // returns a maximum allocation size, while the call without a nullptr |
| 47 | // returns the real one, which may be smaller. |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 48 | signature->resize(sig_len); |
| 49 | return true; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 50 | } |
| 51 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 52 | bool ECSignatureCreatorImpl::DecodeSignature( |
| 53 | const std::vector<uint8_t>& der_sig, |
| 54 | std::vector<uint8_t>* out_raw_sig) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 55 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 56 | // Create ECDSA_SIG object from DER-encoded data. |
davidben | 74f6744 | 2016-10-01 01:45:22 | [diff] [blame] | 57 | bssl::UniquePtr<ECDSA_SIG> ecdsa_sig( |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame] | 58 | ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 59 | if (!ecdsa_sig.get()) |
| 60 | return false; |
| 61 | |
| 62 | // The result is made of two 32-byte vectors. |
| 63 | const size_t kMaxBytesPerBN = 32; |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 64 | std::vector<uint8_t> result(2 * kMaxBytesPerBN); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 65 | |
eroman | 0f4b2770 | 2014-11-10 23:43:38 | [diff] [blame] | 66 | if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
| 67 | !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
| 68 | ecdsa_sig->s)) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 69 | return false; |
| 70 | } |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 71 | out_raw_sig->swap(result); |
| 72 | return true; |
[email protected] | 7c3090a0 | 2012-09-19 15:11:33 | [diff] [blame] | 73 | } |
| 74 | |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 75 | } // namespace crypto |