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> | |
8 #include <openssl/ec.h> | |
9 #include <openssl/ecdsa.h> | |
10 #include <openssl/evp.h> | |
11 #include <openssl/sha.h> | |
12 | |
7 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "crypto/ec_private_key.h" | |
15 #include "crypto/openssl_util.h" | |
8 | 16 |
9 namespace crypto { | 17 namespace crypto { |
10 | 18 |
11 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) | 19 ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key) |
12 : key_(key) { | 20 : key_(key), signature_len_(0) { |
Ryan Sleevi
2013/10/15 19:49:01
STYLE: per the style guide, I believe this should
digit1
2013/10/15 19:54:17
Thanks, but that's already the output of "git cl f
| |
13 NOTIMPLEMENTED(); | 21 EnsureOpenSSLInit(); |
14 } | 22 } |
15 | 23 |
16 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} | 24 ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {} |
17 | 25 |
18 bool ECSignatureCreatorImpl::Sign(const uint8* data, | 26 bool ECSignatureCreatorImpl::Sign(const uint8* data, |
19 int data_len, | 27 int data_len, |
20 std::vector<uint8>* signature) { | 28 std::vector<uint8>* signature) { |
21 NOTIMPLEMENTED(); | 29 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
22 return false; | 30 ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> ctx(EVP_MD_CTX_create()); |
31 size_t sig_len = 0; | |
32 if (!ctx.get() || | |
33 !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) || | |
34 !EVP_DigestSignUpdate(ctx.get(), data, data_len) || | |
35 !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len)) | |
36 return false; | |
wtc
2013/10/15 23:25:29
Nit: we usually add curly braces if the conditiona
digit1
2013/10/17 14:22:07
I've added them. Apparently the formatter doesn't
| |
37 | |
38 signature->resize(sig_len); | |
39 if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len)) | |
40 return false; | |
wtc
2013/10/15 23:25:29
Nit: it may be a good idea to call signature->resi
digit1
2013/10/17 14:22:07
I didn't notice this in the function's documentati
| |
41 | |
42 return true; | |
23 } | 43 } |
24 | 44 |
25 bool ECSignatureCreatorImpl::DecodeSignature(const std::vector<uint8>& der_sig, | 45 bool ECSignatureCreatorImpl::DecodeSignature(const std::vector<uint8>& der_sig, |
26 std::vector<uint8>* out_raw_sig) { | 46 std::vector<uint8>* out_raw_sig) { |
27 NOTIMPLEMENTED(); | 47 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
28 return false; | 48 // Create ECDSA_SIG object from DER-encoded data. |
49 const unsigned char* der_data = | |
50 reinterpret_cast<const unsigned char*>(der_sig.front()); | |
wtc
2013/10/15 23:25:29
This reinterpret_cast is not necessary because uin
digit1
2013/10/17 14:22:07
Done.
| |
51 ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free> ecdsa_sig( | |
52 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size()))); | |
53 if (!ecdsa_sig.get()) | |
54 return false; | |
55 | |
56 // The result is made of two 256-bit vectors. | |
57 const size_t kMaxBitsPerBN = 256; | |
58 const size_t kMaxBytesPerBN = (kMaxBitsPerBN + 7) / 8; | |
wtc
2013/10/15 23:25:29
Nit: It is OK to just define kMaxBytesPerBN as 32
digit1
2013/10/17 14:22:07
Done.
| |
59 std::vector<uint8> result; | |
60 result.resize(2 * kMaxBytesPerBN); | |
61 memset(&result[0], 0, result.size()); | |
62 | |
63 BIGNUM* r = ecdsa_sig.get()->r; | |
64 BIGNUM* s = ecdsa_sig.get()->s; | |
65 int r_bytes = BN_num_bytes(r); | |
66 int s_bytes = BN_num_bytes(s); | |
67 // NOTE: Can't really check for equality here since sometimes the value | |
68 // returned by BN_num_bytes() will be slightly smaller than kMaxBytesPerBN. | |
69 if (r_bytes > static_cast<int>(kMaxBytesPerBN) || | |
70 s_bytes > static_cast<int>(kMaxBytesPerBN)) { | |
71 DLOG(ERROR) << "Invalid key sizes r(" << r_bytes << ") s(" << s_bytes | |
72 << ")"; | |
73 return false; | |
74 } | |
75 BN_bn2bin(ecdsa_sig.get()->r, &result[kMaxBytesPerBN - r_bytes]); | |
76 BN_bn2bin(ecdsa_sig.get()->s, &result[2 * kMaxBytesPerBN - s_bytes]); | |
77 out_raw_sig->swap(result); | |
78 return true; | |
29 } | 79 } |
30 | 80 |
31 } // namespace crypto | 81 } // namespace crypto |
OLD | NEW |