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> |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 53 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
54 // Create ECDSA_SIG object from DER-encoded data. | 54 // Create ECDSA_SIG object from DER-encoded data. |
55 const unsigned char* der_data = &der_sig.front(); | 55 const unsigned char* der_data = &der_sig.front(); |
56 ScopedECDSA_SIG ecdsa_sig( | 56 ScopedECDSA_SIG ecdsa_sig( |
57 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size()))); | 57 d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size()))); |
58 if (!ecdsa_sig.get()) | 58 if (!ecdsa_sig.get()) |
59 return false; | 59 return false; |
60 | 60 |
61 // The result is made of two 32-byte vectors. | 61 // The result is made of two 32-byte vectors. |
62 const size_t kMaxBytesPerBN = 32; | 62 const size_t kMaxBytesPerBN = 32; |
63 std::vector<uint8> result; | 63 std::vector<uint8> result(2 * kMaxBytesPerBN); |
64 result.resize(2 * kMaxBytesPerBN); | |
65 memset(&result[0], 0, result.size()); | |
66 | 64 |
67 BIGNUM* r = ecdsa_sig.get()->r; | 65 if (!BN_bn2bin_padded(&result[0], kMaxBytesPerBN, ecdsa_sig->r) || |
68 BIGNUM* s = ecdsa_sig.get()->s; | 66 !BN_bn2bin_padded(&result[kMaxBytesPerBN], kMaxBytesPerBN, |
69 int r_bytes = BN_num_bytes(r); | 67 ecdsa_sig->s)) { |
70 int s_bytes = BN_num_bytes(s); | |
71 // NOTE: Can't really check for equality here since sometimes the value | |
72 // returned by BN_num_bytes() will be slightly smaller than kMaxBytesPerBN. | |
73 if (r_bytes > static_cast<int>(kMaxBytesPerBN) || | |
74 s_bytes > static_cast<int>(kMaxBytesPerBN)) { | |
75 DLOG(ERROR) << "Invalid key sizes r(" << r_bytes << ") s(" << s_bytes | |
76 << ")"; | |
77 return false; | 68 return false; |
78 } | 69 } |
79 BN_bn2bin(ecdsa_sig.get()->r, &result[kMaxBytesPerBN - r_bytes]); | |
80 BN_bn2bin(ecdsa_sig.get()->s, &result[2 * kMaxBytesPerBN - s_bytes]); | |
81 out_raw_sig->swap(result); | 70 out_raw_sig->swap(result); |
82 return true; | 71 return true; |
83 } | 72 } |
84 | 73 |
85 } // namespace crypto | 74 } // namespace crypto |
OLD | NEW |