| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "crypto/ec_signature_creator_impl.h" | |
| 6 | |
| 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> | |
| 12 #include <stddef.h> | |
| 13 #include <stdint.h> | |
| 14 | |
| 15 #include "base/logging.h" | |
| 16 #include "crypto/ec_private_key.h" | |
| 17 #include "crypto/openssl_util.h" | |
| 18 #include "crypto/scoped_openssl_types.h" | |
| 19 | |
| 20 namespace crypto { | |
| 21 | |
| 22 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) | |
| 23 : key_(key) { | |
| 24 EnsureOpenSSLInit(); | |
| 25 } | |
| 26 | |
| 27 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} | |
| 28 | |
| 29 bool ECSignatureCreatorImpl::Sign(const uint8_t* data, | |
| 30 int data_len, | |
| 31 std::vector<uint8_t>* signature) { | |
| 32 OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 33 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | |
| 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; | |
| 51 } | |
| 52 | |
| 53 bool ECSignatureCreatorImpl::DecodeSignature( | |
| 54 const std::vector<uint8_t>& der_sig, | |
| 55 std::vector<uint8_t>* out_raw_sig) { | |
| 56 OpenSSLErrStackTracer err_tracer(FROM_HERE); | |
| 57 // Create ECDSA_SIG object from DER-encoded data. | |
| 58 ScopedECDSA_SIG ecdsa_sig( | |
| 59 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); | |
| 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; | |
| 65 std::vector<uint8_t> result(2 * kMaxBytesPerBN); | |
| 66 | |
| 67 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || | |
| 68 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, | |
| 69 ecdsa_sig->s)) { | |
| 70 return false; | |
| 71 } | |
| 72 out_raw_sig->swap(result); | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 } // namespace crypto | |
| OLD | NEW |