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

Side by Side Diff: crypto/signature_creator_openssl.cc

Issue 560583002: Generalize crypto::SignatureCreator to allow choice of hash function, so as to support SHA256 (not … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase, fix some lint issues, and a shameful missing ")" Created 6 years, 3 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/signature_creator_nss.cc ('k') | crypto/signature_creator_unittest.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/signature_creator.h" 5 #include "crypto/signature_creator.h"
6 6
7 #include <openssl/evp.h> 7 #include <openssl/evp.h>
8 #include <openssl/rsa.h> 8 #include <openssl/rsa.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "crypto/openssl_util.h" 13 #include "crypto/openssl_util.h"
14 #include "crypto/rsa_private_key.h" 14 #include "crypto/rsa_private_key.h"
15 #include "crypto/scoped_openssl_types.h" 15 #include "crypto/scoped_openssl_types.h"
16 16
17 namespace crypto { 17 namespace crypto {
18 18
19 namespace {
20
21 const EVP_MD* ToOpenSSLDigest(SignatureCreator::HashAlgorithm hash_alg) {
22 switch (hash_alg) {
23 case SignatureCreator::SHA1:
24 return EVP_sha1();
25 case SignatureCreator::SHA256:
26 return EVP_sha256();
27 }
28 return NULL;
29 }
30
31 int ToOpenSSLDigestType(SignatureCreator::HashAlgorithm hash_alg) {
32 switch (hash_alg) {
33 case SignatureCreator::SHA1:
34 return NID_sha1;
35 case SignatureCreator::SHA256:
36 return NID_sha256;
37 }
38 return NID_undef;
39 }
40
41 } // namespace
42
19 // static 43 // static
20 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) { 44 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key,
45 HashAlgorithm hash_alg) {
21 OpenSSLErrStackTracer err_tracer(FROM_HERE); 46 OpenSSLErrStackTracer err_tracer(FROM_HERE);
22 scoped_ptr<SignatureCreator> result(new SignatureCreator); 47 scoped_ptr<SignatureCreator> result(new SignatureCreator);
23 result->key_ = key; 48 result->key_ = key;
24 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL)) 49 const EVP_MD* const digest = ToOpenSSLDigest(hash_alg);
50 DCHECK(digest);
51 if (!digest) {
52 return NULL;
53 }
54 if (!EVP_SignInit_ex(result->sign_context_, digest, NULL))
25 return NULL; 55 return NULL;
26 return result.release(); 56 return result.release();
27 } 57 }
28 58
29 // static 59 // static
30 bool SignatureCreator::Sign(RSAPrivateKey* key, 60 bool SignatureCreator::Sign(RSAPrivateKey* key,
61 HashAlgorithm hash_alg,
31 const uint8* data, 62 const uint8* data,
32 int data_len, 63 int data_len,
33 std::vector<uint8>* signature) { 64 std::vector<uint8>* signature) {
34 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key())); 65 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key()));
35 if (!rsa_key) 66 if (!rsa_key)
36 return false; 67 return false;
37 signature->resize(RSA_size(rsa_key.get())); 68 signature->resize(RSA_size(rsa_key.get()));
38 69
39 unsigned int len = 0; 70 unsigned int len = 0;
40 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature), 71 bool success = RSA_sign(ToOpenSSLDigestType(hash_alg), data, data_len,
41 &len, rsa_key.get()); 72 vector_as_array(signature), &len, rsa_key.get());
42 if (!success) { 73 if (!success) {
43 signature->clear(); 74 signature->clear();
44 return false; 75 return false;
45 } 76 }
46 signature->resize(len); 77 signature->resize(len);
47 return true; 78 return true;
48 } 79 }
49 80
50 SignatureCreator::SignatureCreator() 81 SignatureCreator::SignatureCreator()
51 : sign_context_(EVP_MD_CTX_create()) { 82 : sign_context_(EVP_MD_CTX_create()) {
(...skipping 17 matching lines...) Expand all
69 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key); 100 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key);
70 if (!rv) { 101 if (!rv) {
71 signature->clear(); 102 signature->clear();
72 return false; 103 return false;
73 } 104 }
74 signature->resize(len); 105 signature->resize(len);
75 return true; 106 return true;
76 } 107 }
77 108
78 } // namespace crypto 109 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/signature_creator_nss.cc ('k') | crypto/signature_creator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698