[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / crypto / ec_signature_creator_impl.cc
1 // Copyright 2012 The Chromium Authors
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 <stddef.h>
8 #include <stdint.h>
9
10 #include "crypto/ec_private_key.h"
11 #include "crypto/openssl_util.h"
12 #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"
17
18 namespace crypto {
19
20 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key)
21     : key_(key) {
22   EnsureOpenSSLInit();
23 }
24
25 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() = default;
26
27 bool ECSignatureCreatorImpl::Sign(base::span<const uint8_t> data,
28                                   std::vector<uint8_t>* signature) {
29   OpenSSLErrStackTracer err_tracer(FROM_HERE);
30   bssl::ScopedEVP_MD_CTX ctx;
31   size_t sig_len = 0;
32   if (!ctx.get() ||
33       !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr,
34                           key_->key()) ||
35       !EVP_DigestSignUpdate(ctx.get(), data.data(), data.size()) ||
36       !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) {
37     return false;
38   }
39
40   signature->resize(sig_len);
41   if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len))
42     return false;
43
44   // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter
45   // returns a maximum allocation size, while the call without a nullptr
46   // returns the real one, which may be smaller.
47   signature->resize(sig_len);
48   return true;
49 }
50
51 bool ECSignatureCreatorImpl::DecodeSignature(
52     const std::vector<uint8_t>& der_sig,
53     std::vector<uint8_t>* out_raw_sig) {
54   OpenSSLErrStackTracer err_tracer(FROM_HERE);
55   // Create ECDSA_SIG object from DER-encoded data.
56   bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(
57       ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size()));
58   if (!ecdsa_sig.get())
59     return false;
60
61   // The result is made of two 32-byte vectors.
62   const size_t kMaxBytesPerBN = 32;
63   std::vector<uint8_t> result(2 * kMaxBytesPerBN);
64
65   if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) ||
66       !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN,
67                         ecdsa_sig->s)) {
68     return false;
69   }
70   out_raw_sig->swap(result);
71   return true;
72 }
73
74 }  // namespace crypto