athena: Re-add ui_chromeos resources in athena_resources.pak.
[chromium-blink-merge.git] / crypto / signature_creator_openssl.cc
blob3c8f532cbebc2a31aaf26989fa9b4c622ae337fa
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
3 // found in the LICENSE file.
5 #include "crypto/signature_creator.h"
7 #include <openssl/evp.h>
8 #include <openssl/rsa.h>
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stl_util.h"
13 #include "crypto/openssl_util.h"
14 #include "crypto/rsa_private_key.h"
15 #include "crypto/scoped_openssl_types.h"
17 namespace crypto {
19 // static
20 SignatureCreator* SignatureCreator::Create(RSAPrivateKey* key) {
21 OpenSSLErrStackTracer err_tracer(FROM_HERE);
22 scoped_ptr<SignatureCreator> result(new SignatureCreator);
23 result->key_ = key;
24 if (!EVP_SignInit_ex(result->sign_context_, EVP_sha1(), NULL))
25 return NULL;
26 return result.release();
29 // static
30 bool SignatureCreator::Sign(RSAPrivateKey* key,
31 const uint8* data,
32 int data_len,
33 std::vector<uint8>* signature) {
34 ScopedRSA rsa_key(EVP_PKEY_get1_RSA(key->key()));
35 if (!rsa_key)
36 return false;
37 signature->resize(RSA_size(rsa_key.get()));
39 unsigned int len = 0;
40 bool success = RSA_sign(NID_sha1, data, data_len, vector_as_array(signature),
41 &len, rsa_key.get());
42 if (!success) {
43 signature->clear();
44 return false;
46 signature->resize(len);
47 return true;
50 SignatureCreator::SignatureCreator()
51 : sign_context_(EVP_MD_CTX_create()) {
54 SignatureCreator::~SignatureCreator() {
55 EVP_MD_CTX_destroy(sign_context_);
58 bool SignatureCreator::Update(const uint8* data_part, int data_part_len) {
59 OpenSSLErrStackTracer err_tracer(FROM_HERE);
60 return EVP_SignUpdate(sign_context_, data_part, data_part_len) == 1;
63 bool SignatureCreator::Final(std::vector<uint8>* signature) {
64 OpenSSLErrStackTracer err_tracer(FROM_HERE);
65 EVP_PKEY* key = key_->key();
66 signature->resize(EVP_PKEY_size(key));
68 unsigned int len = 0;
69 int rv = EVP_SignFinal(sign_context_, vector_as_array(signature), &len, key);
70 if (!rv) {
71 signature->clear();
72 return false;
74 signature->resize(len);
75 return true;
78 } // namespace crypto