| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "crypto/ec_signature_creator_impl.h" | 5 #include "crypto/ec_signature_creator_impl.h" |
| 6 | 6 |
| 7 #include <openssl/bn.h> | 7 #include <openssl/bn.h> |
| 8 #include <openssl/ec.h> | 8 #include <openssl/ec.h> |
| 9 #include <openssl/ecdsa.h> | 9 #include <openssl/ecdsa.h> |
| 10 #include <openssl/evp.h> | 10 #include <openssl/evp.h> |
| 11 #include <openssl/sha.h> | 11 #include <openssl/sha.h> |
| 12 #include <stddef.h> | 12 #include <stddef.h> |
| 13 #include <stdint.h> | 13 #include <stdint.h> |
| 14 | 14 |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "crypto/ec_private_key.h" | 16 #include "crypto/ec_private_key.h" |
| 17 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
| 18 #include "crypto/scoped_openssl_types.h" | |
| 19 | 18 |
| 20 namespace crypto { | 19 namespace crypto { |
| 21 | 20 |
| 22 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) | 21 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
| 23 : key_(key) { | 22 : key_(key) { |
| 24 EnsureOpenSSLInit(); | 23 EnsureOpenSSLInit(); |
| 25 } | 24 } |
| 26 | 25 |
| 27 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} | 26 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} |
| 28 | 27 |
| 29 bool ECSignatureCreatorImpl::Sign(const uint8_t* data, | 28 bool ECSignatureCreatorImpl::Sign(const uint8_t* data, |
| 30 int data_len, | 29 int data_len, |
| 31 std::vector<uint8_t>* signature) { | 30 std::vector<uint8_t>* signature) { |
| 32 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 31 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 33 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | 32 bssl::ScopedEVP_MD_CTX ctx; |
| 34 size_t sig_len = 0; | 33 size_t sig_len = 0; |
| 35 if (!ctx.get() || | 34 if (!ctx.get() || |
| 36 !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr, | 35 !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr, |
| 37 key_->key()) || | 36 key_->key()) || |
| 38 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || | 37 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || |
| 39 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) { | 38 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) { |
| 40 return false; | 39 return false; |
| 41 } | 40 } |
| 42 | 41 |
| 43 signature->resize(sig_len); | 42 signature->resize(sig_len); |
| 44 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) | 43 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) |
| 45 return false; | 44 return false; |
| 46 | 45 |
| 47 // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter | 46 // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter |
| 48 // returns a maximum allocation size, while the call without a nullptr | 47 // returns a maximum allocation size, while the call without a nullptr |
| 49 // returns the real one, which may be smaller. | 48 // returns the real one, which may be smaller. |
| 50 signature->resize(sig_len); | 49 signature->resize(sig_len); |
| 51 return true; | 50 return true; |
| 52 } | 51 } |
| 53 | 52 |
| 54 bool ECSignatureCreatorImpl::DecodeSignature( | 53 bool ECSignatureCreatorImpl::DecodeSignature( |
| 55 const std::vector<uint8_t>& der_sig, | 54 const std::vector<uint8_t>& der_sig, |
| 56 std::vector<uint8_t>* out_raw_sig) { | 55 std::vector<uint8_t>* out_raw_sig) { |
| 57 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 56 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
| 58 // Create ECDSA_SIG object from DER-encoded data. | 57 // Create ECDSA_SIG object from DER-encoded data. |
| 59 ScopedECDSA_SIG ecdsa_sig( | 58 bssl::UniquePtr<ECDSA_SIG> ecdsa_sig( |
| 60 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); | 59 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); |
| 61 if (!ecdsa_sig.get()) | 60 if (!ecdsa_sig.get()) |
| 62 return false; | 61 return false; |
| 63 | 62 |
| 64 // The result is made of two 32-byte vectors. | 63 // The result is made of two 32-byte vectors. |
| 65 const size_t kMaxBytesPerBN = 32; | 64 const size_t kMaxBytesPerBN = 32; |
| 66 std::vector<uint8_t> result(2 * kMaxBytesPerBN); | 65 std::vector<uint8_t> result(2 * kMaxBytesPerBN); |
| 67 | 66 |
| 68 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || | 67 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
| 69 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, | 68 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
| 70 ecdsa_sig->s)) { | 69 ecdsa_sig->s)) { |
| 71 return false; | 70 return false; |
| 72 } | 71 } |
| 73 out_raw_sig->swap(result); | 72 out_raw_sig->swap(result); |
| 74 return true; | 73 return true; |
| 75 } | 74 } |
| 76 | 75 |
| 77 } // namespace crypto | 76 } // namespace crypto |
| OLD | NEW |