blob: e9c39b7c585f32cda7c875013ad9a72913993dd7 [file] [log] [blame]
[email protected]e4c18472012-01-25 00:56:431// 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]6b2e61f2012-02-28 08:06:545#include "crypto/ec_signature_creator_impl.h"
[email protected]e4c18472012-01-25 00:56:436
[email protected]012c2872013-10-25 17:26:087#include <openssl/bn.h>
8#include <openssl/ec.h>
9#include <openssl/ecdsa.h>
10#include <openssl/evp.h>
11#include <openssl/sha.h>
avidd373b8b2015-12-21 21:34:4312#include <stddef.h>
13#include <stdint.h>
[email protected]012c2872013-10-25 17:26:0814
[email protected]e4c18472012-01-25 00:56:4315#include "base/logging.h"
[email protected]012c2872013-10-25 17:26:0816#include "crypto/ec_private_key.h"
17#include "crypto/openssl_util.h"
[email protected]cd9b75b2014-07-10 04:39:3818#include "crypto/scoped_openssl_types.h"
[email protected]e4c18472012-01-25 00:56:4319
20namespace crypto {
21
[email protected]6b2e61f2012-02-28 08:06:5422ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key)
mlamourief55346a2015-10-21 01:23:0923 : key_(key) {
[email protected]012c2872013-10-25 17:26:0824 EnsureOpenSSLInit();
[email protected]e4c18472012-01-25 00:56:4325}
26
[email protected]6b2e61f2012-02-28 08:06:5427ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
[email protected]e4c18472012-01-25 00:56:4328
avidd373b8b2015-12-21 21:34:4329bool ECSignatureCreatorImpl::Sign(const uint8_t* data,
[email protected]6b2e61f2012-02-28 08:06:5430 int data_len,
avidd373b8b2015-12-21 21:34:4331 std::vector<uint8_t>* signature) {
[email protected]012c2872013-10-25 17:26:0832 OpenSSLErrStackTracer err_tracer(FROM_HERE);
[email protected]cd9b75b2014-07-10 04:39:3833 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create());
[email protected]012c2872013-10-25 17:26:0834 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]e4c18472012-01-25 00:56:4351}
52
avidd373b8b2015-12-21 21:34:4353bool ECSignatureCreatorImpl::DecodeSignature(
54 const std::vector<uint8_t>& der_sig,
55 std::vector<uint8_t>* out_raw_sig) {
[email protected]012c2872013-10-25 17:26:0856 OpenSSLErrStackTracer err_tracer(FROM_HERE);
57 // Create ECDSA_SIG object from DER-encoded data.
58 const unsigned char* der_data = &der_sig.front();
[email protected]cd9b75b2014-07-10 04:39:3859 ScopedECDSA_SIG ecdsa_sig(
[email protected]012c2872013-10-25 17:26:0860 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size())));
61 if (!ecdsa_sig.get())
62 return false;
63
64 // The result is made of two 32-byte vectors.
65 const size_t kMaxBytesPerBN = 32;
avidd373b8b2015-12-21 21:34:4366 std::vector<uint8_t> result(2 * kMaxBytesPerBN);
[email protected]012c2872013-10-25 17:26:0867
eroman0f4b27702014-11-10 23:43:3868 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) ||
69 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN,
70 ecdsa_sig->s)) {
[email protected]012c2872013-10-25 17:26:0871 return false;
72 }
[email protected]012c2872013-10-25 17:26:0873 out_raw_sig->swap(result);
74 return true;
[email protected]7c3090a02012-09-19 15:11:3375}
76
[email protected]e4c18472012-01-25 00:56:4377} // namespace crypto