blob: f9c1dc2c0c02f9e4c4e56323300a596153af5ea7 [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
avidd373b8b2015-12-21 21:34:437#include <stddef.h>
8#include <stdint.h>
[email protected]012c2872013-10-25 17:26:089
[email protected]e4c18472012-01-25 00:56:4310#include "base/logging.h"
[email protected]012c2872013-10-25 17:26:0811#include "crypto/ec_private_key.h"
12#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3313#include "third_party/boringssl/src/include/openssl/bn.h"
14#include "third_party/boringssl/src/include/openssl/ec.h"
15#include "third_party/boringssl/src/include/openssl/ecdsa.h"
16#include "third_party/boringssl/src/include/openssl/evp.h"
17#include "third_party/boringssl/src/include/openssl/sha.h"
[email protected]e4c18472012-01-25 00:56:4318
19namespace crypto {
20
[email protected]6b2e61f2012-02-28 08:06:5421ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key)
mlamourief55346a2015-10-21 01:23:0922 : key_(key) {
[email protected]012c2872013-10-25 17:26:0823 EnsureOpenSSLInit();
[email protected]e4c18472012-01-25 00:56:4324}
25
[email protected]6b2e61f2012-02-28 08:06:5426ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
[email protected]e4c18472012-01-25 00:56:4327
avidd373b8b2015-12-21 21:34:4328bool ECSignatureCreatorImpl::Sign(const uint8_t* data,
[email protected]6b2e61f2012-02-28 08:06:5429 int data_len,
avidd373b8b2015-12-21 21:34:4330 std::vector<uint8_t>* signature) {
[email protected]012c2872013-10-25 17:26:0831 OpenSSLErrStackTracer err_tracer(FROM_HERE);
davidben74f67442016-10-01 01:45:2232 bssl::ScopedEVP_MD_CTX ctx;
[email protected]012c2872013-10-25 17:26:0833 size_t sig_len = 0;
34 if (!ctx.get() ||
rsleeviffe5a132016-06-28 01:51:5235 !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr,
36 key_->key()) ||
[email protected]012c2872013-10-25 17:26:0837 !EVP_DigestSignUpdate(ctx.get(), data, data_len) ||
rsleeviffe5a132016-06-28 01:51:5238 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) {
[email protected]012c2872013-10-25 17:26:0839 return false;
40 }
41
42 signature->resize(sig_len);
43 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len))
44 return false;
45
rsleeviffe5a132016-06-28 01:51:5246 // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter
47 // returns a maximum allocation size, while the call without a nullptr
48 // returns the real one, which may be smaller.
[email protected]012c2872013-10-25 17:26:0849 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.
davidben74f67442016-10-01 01:45:2258 bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(
davidben7dad2a32016-03-01 23:47:4759 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size()));
[email protected]012c2872013-10-25 17:26:0860 if (!ecdsa_sig.get())
61 return false;
62
63 // The result is made of two 32-byte vectors.
64 const size_t kMaxBytesPerBN = 32;
avidd373b8b2015-12-21 21:34:4365 std::vector<uint8_t> result(2 * kMaxBytesPerBN);
[email protected]012c2872013-10-25 17:26:0866
eroman0f4b27702014-11-10 23:43:3867 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) ||
68 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN,
69 ecdsa_sig->s)) {
[email protected]012c2872013-10-25 17:26:0870 return false;
71 }
[email protected]012c2872013-10-25 17:26:0872 out_raw_sig->swap(result);
73 return true;
[email protected]7c3090a02012-09-19 15:11:3374}
75
[email protected]e4c18472012-01-25 00:56:4376} // namespace crypto