OLD | NEW |
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> |
11 #include <openssl/sha.h> | 11 #include <openssl/sha.h> |
| 12 #include <stddef.h> |
| 13 #include <stdint.h> |
12 | 14 |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "crypto/ec_private_key.h" | 16 #include "crypto/ec_private_key.h" |
15 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
16 #include "crypto/scoped_openssl_types.h" | 18 #include "crypto/scoped_openssl_types.h" |
17 | 19 |
18 namespace crypto { | 20 namespace crypto { |
19 | 21 |
20 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) | 22 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
21 : key_(key) { | 23 : key_(key) { |
22 EnsureOpenSSLInit(); | 24 EnsureOpenSSLInit(); |
23 } | 25 } |
24 | 26 |
25 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} | 27 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} |
26 | 28 |
27 bool ECSignatureCreatorImpl::Sign(const uint8* data, | 29 bool ECSignatureCreatorImpl::Sign(const uint8_t* data, |
28 int data_len, | 30 int data_len, |
29 std::vector<uint8>* signature) { | 31 std::vector<uint8_t>* signature) { |
30 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 32 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
31 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); | 33 ScopedEVP_MD_CTX ctx(EVP_MD_CTX_create()); |
32 size_t sig_len = 0; | 34 size_t sig_len = 0; |
33 if (!ctx.get() || | 35 if (!ctx.get() || |
34 !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) || | 36 !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) || |
35 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || | 37 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || |
36 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { | 38 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) { |
37 return false; | 39 return false; |
38 } | 40 } |
39 | 41 |
40 signature->resize(sig_len); | 42 signature->resize(sig_len); |
41 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) | 43 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) |
42 return false; | 44 return false; |
43 | 45 |
44 // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter returns | 46 // NOTE: A call to EVP_DigestSignFinal() with a NULL second parameter returns |
45 // a maximum allocation size, while the call without a NULL returns the real | 47 // a maximum allocation size, while the call without a NULL returns the real |
46 // one, which may be smaller. | 48 // one, which may be smaller. |
47 signature->resize(sig_len); | 49 signature->resize(sig_len); |
48 return true; | 50 return true; |
49 } | 51 } |
50 | 52 |
51 bool ECSignatureCreatorImpl::DecodeSignature(const std::vector<uint8>& der_sig, | 53 bool ECSignatureCreatorImpl::DecodeSignature( |
52 std::vector<uint8>* out_raw_sig) { | 54 const std::vector<uint8_t>& der_sig, |
| 55 std::vector<uint8_t>* out_raw_sig) { |
53 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 56 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
54 // Create ECDSA_SIG object from DER-encoded data. | 57 // Create ECDSA_SIG object from DER-encoded data. |
55 const unsigned char* der_data = &der_sig.front(); | 58 const unsigned char* der_data = &der_sig.front(); |
56 ScopedECDSA_SIG ecdsa_sig( | 59 ScopedECDSA_SIG ecdsa_sig( |
57 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size()))); | 60 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size()))); |
58 if (!ecdsa_sig.get()) | 61 if (!ecdsa_sig.get()) |
59 return false; | 62 return false; |
60 | 63 |
61 // The result is made of two 32-byte vectors. | 64 // The result is made of two 32-byte vectors. |
62 const size_t kMaxBytesPerBN = 32; | 65 const size_t kMaxBytesPerBN = 32; |
63 std::vector<uint8> result(2 * kMaxBytesPerBN); | 66 std::vector<uint8_t> result(2 * kMaxBytesPerBN); |
64 | 67 |
65 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || | 68 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
66 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, | 69 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
67 ecdsa_sig->s)) { | 70 ecdsa_sig->s)) { |
68 return false; | 71 return false; |
69 } | 72 } |
70 out_raw_sig->swap(result); | 73 out_raw_sig->swap(result); |
71 return true; | 74 return true; |
72 } | 75 } |
73 | 76 |
74 } // namespace crypto | 77 } // namespace crypto |
OLD | NEW |