Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(385)

Side by Side Diff: crypto/ec_signature_creator_impl.cc

Issue 2095523002: Make //crypto factories return std::unique_ptr<>s (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: I'm blind Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « crypto/ec_signature_creator.cc ('k') | crypto/encryptor.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "crypto/ec_signature_creator_impl.h" 5 #include "crypto/ec_signature_creator_impl.h"
6 6
7 #include <openssl/bn.h> 7 #include <openssl/bn.h>
8 #include <openssl/ec.h> 8 #include <openssl/ec.h>
9 #include <openssl/ecdsa.h> 9 #include <openssl/ecdsa.h>
10 #include <openssl/evp.h> 10 #include <openssl/evp.h>
(...skipping 15 matching lines...) Expand all
26 26
27 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} 27 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
28 28
29 bool ECSignatureCreatorImpl::Sign(const uint8_t* data, 29 bool ECSignatureCreatorImpl::Sign(const uint8_t* data,
30 int data_len, 30 int data_len,
31 std::vector<uint8_t>* signature) { 31 std::vector<uint8_t>* signature) {
32 OpenSSLErrStackTracer err_tracer(FROM_HERE); 32 OpenSSLErrStackTracer err_tracer(FROM_HERE);
33 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); 33 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create());
34 size_t sig_len = 0; 34 size_t sig_len = 0;
35 if (!ctx.get() || 35 if (!ctx.get() ||
36 !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) || 36 !EVP_DigestSignInit(ctx.get(), nullptr, EVP_sha256(), nullptr,
37 key_->key()) ||
37 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || 38 !EVP_DigestSignUpdate(ctx.get(), data, data_len) ||
38 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { 39 !EVP_DigestSignFinal(ctx.get(), nullptr, &sig_len)) {
39 return false; 40 return false;
40 } 41 }
41 42
42 signature->resize(sig_len); 43 signature->resize(sig_len);
43 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) 44 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len))
44 return false; 45 return false;
45 46
46 // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter returns 47 // NOTE: A call to EVP_DigestSignFinal() with a nullptr second parameter
47 // a maximum allocation size, while the call without a NULL returns the real 48 // returns a maximum allocation size, while the call without a nullptr
48 // one, which may be smaller. 49 // returns the real one, which may be smaller.
49 signature->resize(sig_len); 50 signature->resize(sig_len);
50 return true; 51 return true;
51 } 52 }
52 53
53 bool ECSignatureCreatorImpl::DecodeSignature( 54 bool ECSignatureCreatorImpl::DecodeSignature(
54 const std::vector<uint8_t>& der_sig, 55 const std::vector<uint8_t>& der_sig,
55 std::vector<uint8_t>* out_raw_sig) { 56 std::vector<uint8_t>* out_raw_sig) {
56 OpenSSLErrStackTracer err_tracer(FROM_HERE); 57 OpenSSLErrStackTracer err_tracer(FROM_HERE);
57 // Create ECDSA_SIG object from DER-encoded data. 58 // Create ECDSA_SIG object from DER-encoded data.
58 ScopedECDSA_SIG ecdsa_sig( 59 ScopedECDSA_SIG ecdsa_sig(
59 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size())); 60 ECDSA_SIG_from_bytes(der_sig.data(), der_sig.size()));
60 if (!ecdsa_sig.get()) 61 if (!ecdsa_sig.get())
61 return false; 62 return false;
62 63
63 // The result is made of two 32-byte vectors. 64 // The result is made of two 32-byte vectors.
64 const size_t kMaxBytesPerBN = 32; 65 const size_t kMaxBytesPerBN = 32;
65 std::vector<uint8_t> result(2 * kMaxBytesPerBN); 66 std::vector<uint8_t> result(2 * kMaxBytesPerBN);
66 67
67 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || 68 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) ||
68 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, 69 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN,
69 ecdsa_sig->s)) { 70 ecdsa_sig->s)) {
70 return false; 71 return false;
71 } 72 }
72 out_raw_sig->swap(result); 73 out_raw_sig->swap(result);
73 return true; 74 return true;
74 } 75 }
75 76
76 } // namespace crypto 77 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/ec_signature_creator.cc ('k') | crypto/encryptor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698