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

Side by Side Diff: base/crypto/symmetric_key_openssl.cc

Issue 5105003: Implements Signature Creator & Verifier for openssl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: wtc comments - change error stack tracer to use FROM_HERE Created 10 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/crypto/signature_verifier_openssl.cc ('k') | base/openssl_util.h » ('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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "base/crypto/symmetric_key.h" 5 #include "base/crypto/symmetric_key.h"
6 6
7 #include <openssl/evp.h> 7 #include <openssl/evp.h>
8 #include <openssl/rand.h> 8 #include <openssl/rand.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 12 matching lines...) Expand all
23 // static 23 // static
24 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm, 24 SymmetricKey* SymmetricKey::GenerateRandomKey(Algorithm algorithm,
25 size_t key_size_in_bits) { 25 size_t key_size_in_bits) {
26 DCHECK_EQ(AES, algorithm); 26 DCHECK_EQ(AES, algorithm);
27 int key_size_in_bytes = key_size_in_bits / 8; 27 int key_size_in_bytes = key_size_in_bits / 8;
28 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); 28 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8);
29 29
30 if (key_size_in_bits == 0) 30 if (key_size_in_bits == 0)
31 return NULL; 31 return NULL;
32 32
33 EnsureOpenSSLInit(); 33 OpenSSLErrStackTracer err_tracer(FROM_HERE);
34 scoped_ptr<SymmetricKey> key(new SymmetricKey); 34 scoped_ptr<SymmetricKey> key(new SymmetricKey);
35 uint8* key_data = 35 uint8* key_data =
36 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); 36 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
37 37
38 int res = RAND_bytes(key_data, key_size_in_bytes); 38 int rv = RAND_bytes(key_data, key_size_in_bytes);
39 if (res != 1) { 39 return rv == 1 ? key.release() : NULL;
40 DLOG(ERROR) << "RAND_bytes failed. res = " << res;
41 ClearOpenSSLERRStack();
42 return NULL;
43 }
44 return key.release();
45 } 40 }
46 41
47 // static 42 // static
48 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, 43 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
49 const std::string& password, 44 const std::string& password,
50 const std::string& salt, 45 const std::string& salt,
51 size_t iterations, 46 size_t iterations,
52 size_t key_size_in_bits) { 47 size_t key_size_in_bits) {
53 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); 48 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
54 int key_size_in_bytes = key_size_in_bits / 8; 49 int key_size_in_bytes = key_size_in_bits / 8;
55 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); 50 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8);
56 51
57 EnsureOpenSSLInit(); 52 OpenSSLErrStackTracer err_tracer(FROM_HERE);
58 scoped_ptr<SymmetricKey> key(new SymmetricKey); 53 scoped_ptr<SymmetricKey> key(new SymmetricKey);
59 uint8* key_data = 54 uint8* key_data =
60 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); 55 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
61 int res = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), 56 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
62 reinterpret_cast<const uint8*>(salt.data()), 57 reinterpret_cast<const uint8*>(salt.data()),
63 salt.length(), iterations, 58 salt.length(), iterations,
64 key_size_in_bytes, key_data); 59 key_size_in_bytes, key_data);
65 if (res != 1) { 60 return rv == 1 ? key.release() : NULL;
66 DLOG(ERROR) << "HMAC SHA1 failed. res = " << res;
67 ClearOpenSSLERRStack();
68 return NULL;
69 }
70 return key.release();
71 } 61 }
72 62
73 // static 63 // static
74 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, 64 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
75 const std::string& raw_key) { 65 const std::string& raw_key) {
76 scoped_ptr<SymmetricKey> key(new SymmetricKey); 66 scoped_ptr<SymmetricKey> key(new SymmetricKey);
77 key->key_ = raw_key; 67 key->key_ = raw_key;
78 return key.release(); 68 return key.release();
79 } 69 }
80 70
81 bool SymmetricKey::GetRawKey(std::string* raw_key) { 71 bool SymmetricKey::GetRawKey(std::string* raw_key) {
82 *raw_key = key_; 72 *raw_key = key_;
83 return true; 73 return true;
84 } 74 }
85 75
86 } // namespace base 76 } // namespace base
OLDNEW
« no previous file with comments | « base/crypto/signature_verifier_openssl.cc ('k') | base/openssl_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698