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> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 69 |
70 if (key_size_in_bytes == 0) | 70 if (key_size_in_bytes == 0) |
71 return NULL; | 71 return NULL; |
72 | 72 |
73 OpenSSLErrStackTracer err_tracer(FROM_HERE); | 73 OpenSSLErrStackTracer err_tracer(FROM_HERE); |
74 std::unique_ptr<SymmetricKey> key(new SymmetricKey); | 74 std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
75 uint8_t* key_data = reinterpret_cast<uint8_t*>( | 75 uint8_t* key_data = reinterpret_cast<uint8_t*>( |
76 base::WriteInto(&key->key_, key_size_in_bytes + 1)); | 76 base::WriteInto(&key->key_, key_size_in_bytes + 1)); |
77 int rv = PKCS5_PBKDF2_HMAC_SHA1( | 77 int rv = PKCS5_PBKDF2_HMAC_SHA1( |
78 password.data(), password.length(), | 78 password.data(), password.length(), |
79 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), iterations, | 79 reinterpret_cast<const uint8_t*>(salt.data()), salt.length(), |
80 static_cast<int>(key_size_in_bytes), key_data); | 80 static_cast<unsigned>(iterations), |
| 81 key_size_in_bytes, key_data); |
81 return rv == 1 ? key.release() : NULL; | 82 return rv == 1 ? key.release() : NULL; |
82 } | 83 } |
83 | 84 |
84 // static | 85 // static |
85 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, | 86 SymmetricKey* SymmetricKey::Import(Algorithm algorithm, |
86 const std::string& raw_key) { | 87 const std::string& raw_key) { |
87 if (algorithm == AES) { | 88 if (algorithm == AES) { |
88 // Whitelist supported key sizes to avoid accidentaly relying on | 89 // Whitelist supported key sizes to avoid accidentaly relying on |
89 // algorithms available in NSS but not BoringSSL and vice | 90 // algorithms available in NSS but not BoringSSL and vice |
90 // versa. Note that BoringSSL does not support AES-192. | 91 // versa. Note that BoringSSL does not support AES-192. |
91 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) | 92 if (raw_key.size() != 128/8 && raw_key.size() != 256/8) |
92 return NULL; | 93 return NULL; |
93 } | 94 } |
94 | 95 |
95 std::unique_ptr<SymmetricKey> key(new SymmetricKey); | 96 std::unique_ptr<SymmetricKey> key(new SymmetricKey); |
96 key->key_ = raw_key; | 97 key->key_ = raw_key; |
97 return key.release(); | 98 return key.release(); |
98 } | 99 } |
99 | 100 |
100 bool SymmetricKey::GetRawKey(std::string* raw_key) { | 101 bool SymmetricKey::GetRawKey(std::string* raw_key) { |
101 *raw_key = key_; | 102 *raw_key = key_; |
102 return true; | 103 return true; |
103 } | 104 } |
104 | 105 |
105 } // namespace crypto | 106 } // namespace crypto |
OLD | NEW |