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

Side by Side Diff: crypto/symmetric_key_openssl.cc

Issue 8418034: Make string_util::WriteInto() DCHECK() that the supplied |length_with_null| > 1, meaning that the... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 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
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/symmetric_key.h" 5 #include "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>
11 11
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/string_util.h" 14 #include "base/string_util.h"
15 #include "crypto/openssl_util.h" 15 #include "crypto/openssl_util.h"
16 16
17 namespace crypto { 17 namespace crypto {
18 18
19 SymmetricKey::~SymmetricKey() { 19 SymmetricKey::~SymmetricKey() {
20 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key. 20 std::fill(key_.begin(), key_.end(), '\0'); // Zero out the confidential key.
21 } 21 }
22 22
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 size_t 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(key_size_in_bits, key_size_in_bytes * 8);
29 29
30 if (key_size_in_bits == 0) 30 if (key_size_in_bytes == 0)
31 return NULL; 31 return NULL;
32 32
33 OpenSSLErrStackTracer err_tracer(FROM_HERE); 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 rv = RAND_bytes(key_data, key_size_in_bytes); 38 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes));
39 return rv == 1 ? key.release() : NULL; 39 return rv == 1 ? key.release() : NULL;
40 } 40 }
41 41
42 // static 42 // static
43 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, 43 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm,
44 const std::string& password, 44 const std::string& password,
45 const std::string& salt, 45 const std::string& salt,
46 size_t iterations, 46 size_t iterations,
47 size_t key_size_in_bits) { 47 size_t key_size_in_bits) {
48 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); 48 DCHECK(algorithm == AES || algorithm == HMAC_SHA1);
49 int key_size_in_bytes = key_size_in_bits / 8; 49 size_t key_size_in_bytes = key_size_in_bits / 8;
50 DCHECK_EQ(static_cast<int>(key_size_in_bits), key_size_in_bytes * 8); 50 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8);
51
52 if (key_size_in_bytes == 0)
53 return NULL;
51 54
52 OpenSSLErrStackTracer err_tracer(FROM_HERE); 55 OpenSSLErrStackTracer err_tracer(FROM_HERE);
53 scoped_ptr<SymmetricKey> key(new SymmetricKey); 56 scoped_ptr<SymmetricKey> key(new SymmetricKey);
54 uint8* key_data = 57 uint8* key_data =
55 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); 58 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1));
56 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), 59 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(),
57 reinterpret_cast<const uint8*>(salt.data()), 60 reinterpret_cast<const uint8*>(salt.data()),
58 salt.length(), iterations, 61 salt.length(), iterations,
59 key_size_in_bytes, key_data); 62 static_cast<int>(key_size_in_bytes),
63 key_data);
60 return rv == 1 ? key.release() : NULL; 64 return rv == 1 ? key.release() : NULL;
61 } 65 }
62 66
63 // static 67 // static
64 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, 68 SymmetricKey* SymmetricKey::Import(Algorithm algorithm,
65 const std::string& raw_key) { 69 const std::string& raw_key) {
66 scoped_ptr<SymmetricKey> key(new SymmetricKey); 70 scoped_ptr<SymmetricKey> key(new SymmetricKey);
67 key->key_ = raw_key; 71 key->key_ = raw_key;
68 return key.release(); 72 return key.release();
69 } 73 }
70 74
71 bool SymmetricKey::GetRawKey(std::string* raw_key) { 75 bool SymmetricKey::GetRawKey(std::string* raw_key) {
72 *raw_key = key_; 76 *raw_key = key_;
73 return true; 77 return true;
74 } 78 }
75 79
76 } // namespace crypto 80 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698