[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 | |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 7 | #include <openssl/bn.h> |
| 8 | #include <openssl/ec.h> |
| 9 | #include <openssl/ecdsa.h> |
| 10 | #include <openssl/evp.h> |
| 11 | #include <openssl/sha.h> |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 14 | |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 15 | #include "base/logging.h" |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 16 | #include "crypto/ec_private_key.h" |
| 17 | #include "crypto/openssl_util.h" |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 18 | #include "crypto/scoped_openssl_types.h" |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 19 | |
| 20 | namespace crypto { |
| 21 | |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 22 | ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
mlamouri | ef55346a | 2015-10-21 01:23:09 | [diff] [blame] | 23 | : key_(key) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 24 | EnsureOpenSSLInit(); |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 25 | } |
| 26 | |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 27 | ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 28 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 29 | bool ECSignatureCreatorImpl::Sign(const uint8_t* data, |
[email protected] | 6b2e61f | 2012-02-28 08:06:54 | [diff] [blame] | 30 | int data_len, |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 31 | std::vector<uint8_t>* signature) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 32 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 33 | ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 34 | size_t sig_len = 0; |
| 35 | if (!ctx.get() || |
| 36 | !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) || |
| 37 | !EVP_DigestSignUpdate(ctx.get(), data, data_len) || |
| 38 | !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | signature->resize(sig_len); |
| 43 | if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) |
| 44 | return false; |
| 45 | |
| 46 | // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter returns |
| 47 | // a maximum allocation size, while the call without a NULL returns the real |
| 48 | // one, which may be smaller. |
| 49 | signature->resize(sig_len); |
| 50 | return true; |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 51 | } |
| 52 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 53 | bool ECSignatureCreatorImpl::DecodeSignature( |
| 54 | const std::vector<uint8_t>& der_sig, |
| 55 | std::vector<uint8_t>* out_raw_sig) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 56 | OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 57 | // Create ECDSA_SIG object from DER-encoded data. |
[email protected] | cd9b75b | 2014-07-10 04:39:38 | [diff] [blame] | 58 | ScopedECDSA_SIG ecdsa_sig( |
davidben | 7dad2a3 | 2016-03-01 23:47:47 | [diff] [blame^] | 59 | ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 60 | if (!ecdsa_sig.get()) |
| 61 | return false; |
| 62 | |
| 63 | // The result is made of two 32-byte vectors. |
| 64 | const size_t kMaxBytesPerBN = 32; |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 65 | std::vector<uint8_t> result(2 * kMaxBytesPerBN); |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 66 | |
eroman | 0f4b2770 | 2014-11-10 23:43:38 | [diff] [blame] | 67 | if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
| 68 | !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
| 69 | ecdsa_sig->s)) { |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 70 | return false; |
| 71 | } |
[email protected] | 012c287 | 2013-10-25 17:26:08 | [diff] [blame] | 72 | out_raw_sig->swap(result); |
| 73 | return true; |
[email protected] | 7c3090a0 | 2012-09-19 15:11:33 | [diff] [blame] | 74 | } |
| 75 | |
[email protected] | e4c1847 | 2012-01-25 00:56:43 | [diff] [blame] | 76 | } // namespace crypto |