OLD | NEW |
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> |
(...skipping 21 matching lines...) Expand all Loading... |
32 return NULL; | 32 return NULL; |
33 | 33 |
34 size_t key_size_in_bytes = key_size_in_bits / 8; | 34 size_t key_size_in_bytes = key_size_in_bits / 8; |
35 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 35 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
36 | 36 |
37 if (key_size_in_bytes == 0) | 37 if (key_size_in_bytes == 0) |
38 return NULL; | 38 return NULL; |
39 | 39 |
40 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 40 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
41 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 41 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
42 uint8* key_data = | 42 uint8* key_data = reinterpret_cast<uint8*>( |
43 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); | 43 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
44 | 44 |
45 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); | 45 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); |
46 return rv == 1 ? key.release() : NULL; | 46 return rv == 1 ? key.release() : NULL; |
47 } | 47 } |
48 | 48 |
49 // static | 49 // static |
50 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 50 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
51 const std::string& password, | 51 const std::string& password, |
52 const std::string& salt, | 52 const std::string& salt, |
53 size_t iterations, | 53 size_t iterations, |
54 size_t key_size_in_bits) { | 54 size_t key_size_in_bits) { |
55 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); | 55 DCHECK(algorithm == AES || algorithm == HMAC_SHA1); |
56 | 56 |
57 if (algorithm == AES) { | 57 if (algorithm == AES) { |
58 // Whitelist supported key sizes to avoid accidentaly relying on | 58 // Whitelist supported key sizes to avoid accidentaly relying on |
59 // algorithms available in NSS but not BoringSSL and vice | 59 // algorithms available in NSS but not BoringSSL and vice |
60 // versa. Note that BoringSSL does not support AES-192. | 60 // versa. Note that BoringSSL does not support AES-192. |
61 if (key_size_in_bits != 128 && key_size_in_bits != 256) | 61 if (key_size_in_bits != 128 && key_size_in_bits != 256) |
62 return NULL; | 62 return NULL; |
63 } | 63 } |
64 | 64 |
65 size_t key_size_in_bytes = key_size_in_bits / 8; | 65 size_t key_size_in_bytes = key_size_in_bits / 8; |
66 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 66 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
67 | 67 |
68 if (key_size_in_bytes == 0) | 68 if (key_size_in_bytes == 0) |
69 return NULL; | 69 return NULL; |
70 | 70 |
71 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 71 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
72 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 72 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
73 uint8* key_data = | 73 uint8* key_data = reinterpret_cast<uint8*>( |
74 reinterpret_cast<uint8*>(WriteInto(&key->key_, key_size_in_bytes + 1)); | 74 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
75 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), | 75 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), |
76 reinterpret_cast<const uint8*>(salt.data()), | 76 reinterpret_cast<const uint8*>(salt.data()), |
77 salt.length(), iterations, | 77 salt.length(), iterations, |
78 static_cast<int>(key_size_in_bytes), | 78 static_cast<int>(key_size_in_bytes), |
79 key_data); | 79 key_data); |
80 return rv == 1 ? key.release() : NULL; | 80 return rv == 1 ? key.release() : NULL; |
81 } | 81 } |
82 | 82 |
83 // static | 83 // static |
84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
(...skipping 10 matching lines...) Expand all Loading... |
95 key->key_ = raw_key; | 95 key->key_ = raw_key; |
96 return key.release(); | 96 return key.release(); |
97 } | 97 } |
98 | 98 |
99 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 99 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
100 *raw_key = key_; | 100 *raw_key = key_; |
101 return true; | 101 return true; |
102 } | 102 } |
103 | 103 |
104 } // namespace crypto | 104 } // namespace crypto |
OLD | NEW |