blob: d7da650870d07d5fcce94a48e8d23acc23096e2a [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]012c2872013-10-25 17:26:0810#include "crypto/ec_private_key.h"
11#include "crypto/openssl_util.h"
tfarina29a3a1742016-10-28 18:47:3312#include "third_party/boringssl/src/include/openssl/bn.h"
13#include "third_party/boringssl/src/include/openssl/ec.h"
14#include "third_party/boringssl/src/include/openssl/ecdsa.h"
15#include "third_party/boringssl/src/include/openssl/evp.h"
16#include "third_party/boringssl/src/include/openssl/sha.h"
[email protected]e4c18472012-01-25 00:56:4317
18namespace crypto {
19
[email protected]6b2e61f2012-02-28 08:06:5420ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key)
mlamourief55346a2015-10-21 01:23:0921 : key_(key) {
[email protected]012c2872013-10-25 17:26:0822 EnsureOpenSSLInit();
[email protected]e4c18472012-01-25 00:56:4323}
24
Chris Watkinsa850a302017-11-30 03:53:4925ECSignatureCreatorImpl::~ECSignatureCreatorImpl() = default;
[email protected]e4c18472012-01-25 00:56:4326
avidd373b8b2015-12-21 21:34:4327bool ECSignatureCreatorImpl::Sign(const uint8_t* data,
[email protected]6b2e61f2012-02-28 08:06:5428 int data_len,
avidd373b8b2015-12-21 21:34:4329 std::vector<uint8_t>* signature) {
[email protected]012c2872013-10-25 17:26:0830 OpenSSLErrStackTracer err_tracer(FROM_HERE);
davidben74f67442016-10-01 01:45:2231 bssl::ScopedEVP_MD_CTX ctx;
[email protected]012c2872013-10-25 17:26:0832 size_t sig_len = 0;
33 if (!ctx.get() ||
rsleeviffe5a132016-06-28 01:51:5234 !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr,
35 key_->key()) ||
[email protected]012c2872013-10-25 17:26:0836 !EVP_DigestSignUpdate(ctx.get(), data, data_len) ||
rsleeviffe5a132016-06-28 01:51:5237 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) {
[email protected]012c2872013-10-25 17:26:0838 return false;
39 }
40
41 signature->resize(sig_len);
42 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len))
43 return false;
44
rsleeviffe5a132016-06-28 01:51:5245 // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter
46 // returns a maximum allocation size, while the call without a nullptr
47 // returns the real one, which may be smaller.
[email protected]012c2872013-10-25 17:26:0848 signature->resize(sig_len);
49 return true;
[email protected]e4c18472012-01-25 00:56:4350}
51
avidd373b8b2015-12-21 21:34:4352bool ECSignatureCreatorImpl::DecodeSignature(
53 const std::vector<uint8_t>& der_sig,
54 std::vector<uint8_t>* out_raw_sig) {
[email protected]012c2872013-10-25 17:26:0855 OpenSSLErrStackTracer err_tracer(FROM_HERE);
56 // Create ECDSA_SIG object from DER-encoded data.
davidben74f67442016-10-01 01:45:2257 bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(
davidben7dad2a32016-03-01 23:47:4758 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size()));
[email protected]012c2872013-10-25 17:26:0859 if (!ecdsa_sig.get())
60 return false;
61
62 // The result is made of two 32-byte vectors.
63 const size_t kMaxBytesPerBN = 32;
avidd373b8b2015-12-21 21:34:4364 std::vector<uint8_t> result(2 * kMaxBytesPerBN);
[email protected]012c2872013-10-25 17:26:0865
eroman0f4b27702014-11-10 23:43:3866 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) ||
67 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN,
68 ecdsa_sig->s)) {
[email protected]012c2872013-10-25 17:26:0869 return false;
70 }
[email protected]012c2872013-10-25 17:26:0871 out_raw_sig->swap(result);
72 return true;
[email protected]7c3090a02012-09-19 15:11:3373}
74
[email protected]e4c18472012-01-25 00:56:4375} // namespace crypto