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

Unified Diff: crypto/ec_signature_creator_openssl.cc

Issue 26911006: crypto: Implement ECSignatureCreatorImpl for OpenSSL (Closed) Base URL: https://codereview.chromium.org/27195002/
Patch Set: Rebase + nits. Created 7 years, 2 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | crypto/ec_signature_creator_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: crypto/ec_signature_creator_openssl.cc
diff --git a/crypto/ec_signature_creator_openssl.cc b/crypto/ec_signature_creator_openssl.cc
index 8854f5ed464de9c8fa1f658e1ca31b646cbbdea7..70df13577297d40532d562576bb31bd549baa762 100644
--- a/crypto/ec_signature_creator_openssl.cc
+++ b/crypto/ec_signature_creator_openssl.cc
@@ -4,13 +4,21 @@
#include "crypto/ec_signature_creator_impl.h"
+#include <openssl/bn.h>
+#include <openssl/ec.h>
+#include <openssl/ecdsa.h>
+#include <openssl/evp.h>
+#include <openssl/sha.h>
+
#include "base/logging.h"
+#include "crypto/ec_private_key.h"
+#include "crypto/openssl_util.h"
namespace crypto {
ECSignatureCreatorImpl::ECSignatureCreatorImpl(ECPrivateKey* key)
- : key_(key) {
- NOTIMPLEMENTED();
+ : 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
+ EnsureOpenSSLInit();
}
ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
@@ -18,14 +26,56 @@ ECSignatureCreatorImpl::~ECSignatureCreatorImpl() {}
bool ECSignatureCreatorImpl::Sign(const uint8* data,
int data_len,
std::vector<uint8>* signature) {
- NOTIMPLEMENTED();
- return false;
+ OpenSSLErrStackTracer err_tracer(FROM_HERE);
+ ScopedOpenSSL<EVP_MD_CTX, EVP_MD_CTX_destroy> ctx(EVP_MD_CTX_create());
+ size_t sig_len = 0;
+ if (!ctx.get() ||
+ !EVP_DigestSignInit(ctx.get(), NULL, EVP_sha256(), NULL, key_->key()) ||
+ !EVP_DigestSignUpdate(ctx.get(), data, data_len) ||
+ !EVP_DigestSignFinal(ctx.get(), NULL, &sig_len))
+ 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
+
+ signature->resize(sig_len);
+ if (!EVP_DigestSignFinal(ctx.get(), &signature->front(), &sig_len))
+ 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
+
+ return true;
}
bool ECSignatureCreatorImpl::DecodeSignature(const std::vector<uint8>& der_sig,
std::vector<uint8>* out_raw_sig) {
- NOTIMPLEMENTED();
- return false;
+ OpenSSLErrStackTracer err_tracer(FROM_HERE);
+ // Create ECDSA_SIG object from DER-encoded data.
+ const unsigned char* der_data =
+ 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.
+ ScopedOpenSSL<ECDSA_SIG, ECDSA_SIG_free> ecdsa_sig(
+ d2i_ECDSA_SIG(NULL, &der_data, static_cast<long>(der_sig.size())));
+ if (!ecdsa_sig.get())
+ return false;
+
+ // The result is made of two 256-bit vectors.
+ const size_t kMaxBitsPerBN = 256;
+ 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.
+ std::vector<uint8> result;
+ result.resize(2 * kMaxBytesPerBN);
+ memset(&result[0], 0, result.size());
+
+ BIGNUM* r = ecdsa_sig.get()->r;
+ BIGNUM* s = ecdsa_sig.get()->s;
+ int r_bytes = BN_num_bytes(r);
+ int s_bytes = BN_num_bytes(s);
+ // NOTE: Can't really check for equality here since sometimes the value
+ // returned by BN_num_bytes() will be slightly smaller than kMaxBytesPerBN.
+ if (r_bytes > static_cast<int>(kMaxBytesPerBN) ||
+ s_bytes > static_cast<int>(kMaxBytesPerBN)) {
+ DLOG(ERROR) << "Invalid key sizes r(" << r_bytes << ") s(" << s_bytes
+ << ")";
+ return false;
+ }
+ BN_bn2bin(ecdsa_sig.get()->r, &result[kMaxBytesPerBN - r_bytes]);
+ BN_bn2bin(ecdsa_sig.get()->s, &result[2 * kMaxBytesPerBN - s_bytes]);
+ out_raw_sig->swap(result);
+ return true;
}
} // namespace crypto
« no previous file with comments | « no previous file | crypto/ec_signature_creator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698