OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/os_crypt/os_crypt.h" | 5 #include "components/os_crypt/os_crypt.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
| 9 #include <memory> |
| 10 |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "crypto/encryptor.h" | 13 #include "crypto/encryptor.h" |
13 #include "crypto/symmetric_key.h" | 14 #include "crypto/symmetric_key.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 // Salt for Symmetric key derivation. | 18 // Salt for Symmetric key derivation. |
18 const char kSalt[] = "saltysalt"; | 19 const char kSalt[] = "saltysalt"; |
19 | 20 |
20 // Key size required for 128 bit AES. | 21 // Key size required for 128 bit AES. |
(...skipping 15 matching lines...) Expand all Loading... |
36 // generation error occurs. | 37 // generation error occurs. |
37 crypto::SymmetricKey* GetEncryptionKey() { | 38 crypto::SymmetricKey* GetEncryptionKey() { |
38 // We currently "obfuscate" by encrypting and decrypting with hard-coded | 39 // We currently "obfuscate" by encrypting and decrypting with hard-coded |
39 // password. We need to improve this password situation by moving a secure | 40 // password. We need to improve this password situation by moving a secure |
40 // password into a system-level key store. | 41 // password into a system-level key store. |
41 // http://crbug.com/25404 and http://crbug.com/49115 | 42 // http://crbug.com/25404 and http://crbug.com/49115 |
42 std::string password = "peanuts"; | 43 std::string password = "peanuts"; |
43 std::string salt(kSalt); | 44 std::string salt(kSalt); |
44 | 45 |
45 // Create an encryption key from our password and salt. | 46 // Create an encryption key from our password and salt. |
46 scoped_ptr<crypto::SymmetricKey> encryption_key( | 47 std::unique_ptr<crypto::SymmetricKey> encryption_key( |
47 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, | 48 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, |
48 password, | 49 password, |
49 salt, | 50 salt, |
50 kEncryptionIterations, | 51 kEncryptionIterations, |
51 kDerivedKeySizeInBits)); | 52 kDerivedKeySizeInBits)); |
52 DCHECK(encryption_key.get()); | 53 DCHECK(encryption_key.get()); |
53 | 54 |
54 return encryption_key.release(); | 55 return encryption_key.release(); |
55 } | 56 } |
56 | 57 |
(...skipping 19 matching lines...) Expand all Loading... |
76 // This currently "obfuscates" by encrypting with hard-coded password. | 77 // This currently "obfuscates" by encrypting with hard-coded password. |
77 // We need to improve this password situation by moving a secure password | 78 // We need to improve this password situation by moving a secure password |
78 // into a system-level key store. | 79 // into a system-level key store. |
79 // http://crbug.com/25404 and http://crbug.com/49115 | 80 // http://crbug.com/25404 and http://crbug.com/49115 |
80 | 81 |
81 if (plaintext.empty()) { | 82 if (plaintext.empty()) { |
82 *ciphertext = std::string(); | 83 *ciphertext = std::string(); |
83 return true; | 84 return true; |
84 } | 85 } |
85 | 86 |
86 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); | 87 std::unique_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
87 if (!encryption_key.get()) | 88 if (!encryption_key.get()) |
88 return false; | 89 return false; |
89 | 90 |
90 std::string iv(kIVBlockSizeAES128, ' '); | 91 std::string iv(kIVBlockSizeAES128, ' '); |
91 crypto::Encryptor encryptor; | 92 crypto::Encryptor encryptor; |
92 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 93 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
93 return false; | 94 return false; |
94 | 95 |
95 if (!encryptor.Encrypt(plaintext, ciphertext)) | 96 if (!encryptor.Encrypt(plaintext, ciphertext)) |
96 return false; | 97 return false; |
(...skipping 21 matching lines...) Expand all Loading... |
118 // Credit card numbers are current legacy data, so false match with prefix | 119 // Credit card numbers are current legacy data, so false match with prefix |
119 // won't happen. | 120 // won't happen. |
120 if (ciphertext.find(kObfuscationPrefix) != 0) { | 121 if (ciphertext.find(kObfuscationPrefix) != 0) { |
121 *plaintext = ciphertext; | 122 *plaintext = ciphertext; |
122 return true; | 123 return true; |
123 } | 124 } |
124 | 125 |
125 // Strip off the versioning prefix before decrypting. | 126 // Strip off the versioning prefix before decrypting. |
126 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); | 127 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); |
127 | 128 |
128 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); | 129 std::unique_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
129 if (!encryption_key.get()) | 130 if (!encryption_key.get()) |
130 return false; | 131 return false; |
131 | 132 |
132 std::string iv(kIVBlockSizeAES128, ' '); | 133 std::string iv(kIVBlockSizeAES128, ' '); |
133 crypto::Encryptor encryptor; | 134 crypto::Encryptor encryptor; |
134 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 135 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
135 return false; | 136 return false; |
136 | 137 |
137 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) | 138 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
138 return false; | 139 return false; |
139 | 140 |
140 return true; | 141 return true; |
141 } | 142 } |
OLD | NEW |