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 #include <stddef.h> |
| 10 #include <stdint.h> |
9 | 11 |
10 #include <algorithm> | 12 #include <algorithm> |
11 | 13 |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
15 #include "crypto/openssl_util.h" | 17 #include "crypto/openssl_util.h" |
16 | 18 |
17 namespace crypto { | 19 namespace crypto { |
18 | 20 |
(...skipping 13 matching lines...) Expand all Loading... |
32 return NULL; | 34 return NULL; |
33 | 35 |
34 size_t key_size_in_bytes = key_size_in_bits / 8; | 36 size_t key_size_in_bytes = key_size_in_bits / 8; |
35 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 37 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
36 | 38 |
37 if (key_size_in_bytes == 0) | 39 if (key_size_in_bytes == 0) |
38 return NULL; | 40 return NULL; |
39 | 41 |
40 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 42 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
41 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 43 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
42 uint8* key_data = reinterpret_cast<uint8*>( | 44 uint8_t* key_data = reinterpret_cast<uint8_t*>( |
43 base::WriteInto(&key->key_, key_size_in_bytes + 1)); | 45 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
44 | 46 |
45 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); | 47 int rv = RAND_bytes(key_data, static_cast<int>(key_size_in_bytes)); |
46 return rv == 1 ? key.release() : NULL; | 48 return rv == 1 ? key.release() : NULL; |
47 } | 49 } |
48 | 50 |
49 // static | 51 // static |
50 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, | 52 SymmetricKey* SymmetricKey::DeriveKeyFromPassword(Algorithm algorithm, |
51 const std::string& password, | 53 const std::string& password, |
52 const std::string& salt, | 54 const std::string& salt, |
(...skipping 10 matching lines...) Expand all Loading... |
63 } | 65 } |
64 | 66 |
65 size_t key_size_in_bytes = key_size_in_bits / 8; | 67 size_t key_size_in_bytes = key_size_in_bits / 8; |
66 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); | 68 DCHECK_EQ(key_size_in_bits, key_size_in_bytes * 8); |
67 | 69 |
68 if (key_size_in_bytes == 0) | 70 if (key_size_in_bytes == 0) |
69 return NULL; | 71 return NULL; |
70 | 72 |
71 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 73 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
72 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 74 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
73 uint8* key_data = reinterpret_cast<uint8*>( | 75 uint8_t* key_data = reinterpret_cast<uint8_t*>( |
74 base::WriteInto(&key->key_, key_size_in_bytes + 1)); | 76 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
75 int rv = PKCS5_PBKDF2_HMAC_SHA1(password.data(), password.length(), | 77 int rv = PKCS5_PBKDF2_HMAC_SHA1( |
76 reinterpret_cast<const uint8*>(salt.data()), | 78 password.data(), password.length(), |
77 salt.length(), iterations, | 79 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), iterations, |
78 static_cast<int>(key_size_in_bytes), | 80 static_cast<int>(key_size_in_bytes), key_data); |
79 key_data); | |
80 return rv == 1 ? key.release() : NULL; | 81 return rv == 1 ? key.release() : NULL; |
81 } | 82 } |
82 | 83 |
83 // static | 84 // static |
84 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 85 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
85 const std::string& raw_key) { | 86 const std::string& raw_key) { |
86 if (algorithm == AES) { | 87 if (algorithm == AES) { |
87 // Whitelist supported key sizes to avoid accidentaly relying on | 88 // Whitelist supported key sizes to avoid accidentaly relying on |
88 // algorithms available in NSS but not BoringSSL and vice | 89 // algorithms available in NSS but not BoringSSL and vice |
89 // versa. Note that BoringSSL does not support AES-192. | 90 // versa. Note that BoringSSL does not support AES-192. |
90 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) | 91 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) |
91 return NULL; | 92 return NULL; |
92 } | 93 } |
93 | 94 |
94 scoped_ptr<SymmetricKey> key(new SymmetricKey); | 95 scoped_ptr<SymmetricKey> key(new SymmetricKey); |
95 key->key_ = raw_key; | 96 key->key_ = raw_key; |
96 return key.release(); | 97 return key.release(); |
97 } | 98 } |
98 | 99 |
99 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 100 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
100 *raw_key = key_; | 101 *raw_key = key_; |
101 return true; | 102 return true; |
102 } | 103 } |
103 | 104 |
104 } // namespace crypto | 105 } // namespace crypto |
OLD | NEW |