| 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 "chrome/browser/password_manager/encryptor.h" | 5 #include "chrome/browser/password_manager/encryptor.h" |
| 6 | 6 |
| 7 #include "base/crypto/encryptor.h" | |
| 8 #include "base/crypto/symmetric_key.h" | |
| 9 #include "base/logging.h" | 7 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "crypto/encryptor.h" |
| 11 #include "crypto/symmetric_key.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Salt for Symmetric key derivation. | 15 // Salt for Symmetric key derivation. |
| 16 const char kSalt[] = "saltysalt"; | 16 const char kSalt[] = "saltysalt"; |
| 17 | 17 |
| 18 // Key size required for 128 bit AES. | 18 // Key size required for 128 bit AES. |
| 19 const size_t kDerivedKeySizeInBits = 128; | 19 const size_t kDerivedKeySizeInBits = 128; |
| 20 | 20 |
| 21 // Constant for Symmetic key derivation. | 21 // Constant for Symmetic key derivation. |
| 22 const size_t kEncryptionIterations = 1; | 22 const size_t kEncryptionIterations = 1; |
| 23 | 23 |
| 24 // Size of initialization vector for AES 128-bit. | 24 // Size of initialization vector for AES 128-bit. |
| 25 const size_t kIVBlockSizeAES128 = 16; | 25 const size_t kIVBlockSizeAES128 = 16; |
| 26 | 26 |
| 27 // Prefix for cypher text returned by obfuscation version. We prefix the | 27 // Prefix for cypher text returned by obfuscation version. We prefix the |
| 28 // cyphertext with this string so that future data migration can detect | 28 // cyphertext with this string so that future data migration can detect |
| 29 // this and migrate to full encryption without data loss. | 29 // this and migrate to full encryption without data loss. |
| 30 const char kObfuscationPrefix[] = "v10"; | 30 const char kObfuscationPrefix[] = "v10"; |
| 31 | 31 |
| 32 // Generates a newly allocated SymmetricKey object based a hard-coded password. | 32 // Generates a newly allocated SymmetricKey object based a hard-coded password. |
| 33 // Ownership of the key is passed to the caller. Returns NULL key if a key | 33 // Ownership of the key is passed to the caller. Returns NULL key if a key |
| 34 // generation error occurs. | 34 // generation error occurs. |
| 35 base::SymmetricKey* GetEncryptionKey() { | 35 crypto::SymmetricKey* GetEncryptionKey() { |
| 36 // We currently "obfuscate" by encrypting and decrypting with hard-coded | 36 // We currently "obfuscate" by encrypting and decrypting with hard-coded |
| 37 // password. We need to improve this password situation by moving a secure | 37 // password. We need to improve this password situation by moving a secure |
| 38 // password into a system-level key store. | 38 // password into a system-level key store. |
| 39 // http://crbug.com/25404 and http://crbug.com/49115 | 39 // http://crbug.com/25404 and http://crbug.com/49115 |
| 40 std::string password = "peanuts"; | 40 std::string password = "peanuts"; |
| 41 std::string salt(kSalt); | 41 std::string salt(kSalt); |
| 42 | 42 |
| 43 // Create an encryption key from our password and salt. | 43 // Create an encryption key from our password and salt. |
| 44 scoped_ptr<base::SymmetricKey> encryption_key( | 44 scoped_ptr<crypto::SymmetricKey> encryption_key( |
| 45 base::SymmetricKey::DeriveKeyFromPassword(base::SymmetricKey::AES, | 45 crypto::SymmetricKey::DeriveKeyFromPassword(crypto::SymmetricKey::AES, |
| 46 password, | 46 password, |
| 47 salt, | 47 salt, |
| 48 kEncryptionIterations, | 48 kEncryptionIterations, |
| 49 kDerivedKeySizeInBits)); | 49 kDerivedKeySizeInBits)); |
| 50 DCHECK(encryption_key.get()); | 50 DCHECK(encryption_key.get()); |
| 51 | 51 |
| 52 return encryption_key.release(); | 52 return encryption_key.release(); |
| 53 } | 53 } |
| 54 | 54 |
| 55 } // namespace | 55 } // namespace |
| 56 | 56 |
| 57 bool Encryptor::EncryptString16(const string16& plaintext, | 57 bool Encryptor::EncryptString16(const string16& plaintext, |
| 58 std::string* ciphertext) { | 58 std::string* ciphertext) { |
| 59 return EncryptString(UTF16ToUTF8(plaintext), ciphertext); | 59 return EncryptString(UTF16ToUTF8(plaintext), ciphertext); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 74 // This currently "obfuscates" by encrypting with hard-coded password. | 74 // This currently "obfuscates" by encrypting with hard-coded password. |
| 75 // We need to improve this password situation by moving a secure password | 75 // We need to improve this password situation by moving a secure password |
| 76 // into a system-level key store. | 76 // into a system-level key store. |
| 77 // http://crbug.com/25404 and http://crbug.com/49115 | 77 // http://crbug.com/25404 and http://crbug.com/49115 |
| 78 | 78 |
| 79 if (plaintext.empty()) { | 79 if (plaintext.empty()) { |
| 80 *ciphertext = std::string(); | 80 *ciphertext = std::string(); |
| 81 return true; | 81 return true; |
| 82 } | 82 } |
| 83 | 83 |
| 84 scoped_ptr<base::SymmetricKey> encryption_key(GetEncryptionKey()); | 84 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
| 85 if (!encryption_key.get()) | 85 if (!encryption_key.get()) |
| 86 return false; | 86 return false; |
| 87 | 87 |
| 88 std::string iv(kIVBlockSizeAES128, ' '); | 88 std::string iv(kIVBlockSizeAES128, ' '); |
| 89 base::Encryptor encryptor; | 89 crypto::Encryptor encryptor; |
| 90 if (!encryptor.Init(encryption_key.get(), base::Encryptor::CBC, iv)) | 90 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
| 91 return false; | 91 return false; |
| 92 | 92 |
| 93 if (!encryptor.Encrypt(plaintext, ciphertext)) | 93 if (!encryptor.Encrypt(plaintext, ciphertext)) |
| 94 return false; | 94 return false; |
| 95 | 95 |
| 96 // Prefix the cypher text with version information. | 96 // Prefix the cypher text with version information. |
| 97 ciphertext->insert(0, kObfuscationPrefix); | 97 ciphertext->insert(0, kObfuscationPrefix); |
| 98 return true; | 98 return true; |
| 99 } | 99 } |
| 100 | 100 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 116 // Credit card numbers are current legacy data, so false match with prefix | 116 // Credit card numbers are current legacy data, so false match with prefix |
| 117 // won't happen. | 117 // won't happen. |
| 118 if (ciphertext.find(kObfuscationPrefix) != 0) { | 118 if (ciphertext.find(kObfuscationPrefix) != 0) { |
| 119 *plaintext = ciphertext; | 119 *plaintext = ciphertext; |
| 120 return true; | 120 return true; |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Strip off the versioning prefix before decrypting. | 123 // Strip off the versioning prefix before decrypting. |
| 124 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); | 124 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); |
| 125 | 125 |
| 126 scoped_ptr<base::SymmetricKey> encryption_key(GetEncryptionKey()); | 126 scoped_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
| 127 if (!encryption_key.get()) | 127 if (!encryption_key.get()) |
| 128 return false; | 128 return false; |
| 129 | 129 |
| 130 std::string iv(kIVBlockSizeAES128, ' '); | 130 std::string iv(kIVBlockSizeAES128, ' '); |
| 131 base::Encryptor encryptor; | 131 crypto::Encryptor encryptor; |
| 132 if (!encryptor.Init(encryption_key.get(), base::Encryptor::CBC, iv)) | 132 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
| 133 return false; | 133 return false; |
| 134 | 134 |
| 135 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) | 135 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
| 136 return false; | 136 return false; |
| 137 | 137 |
| 138 return true; | 138 return true; |
| 139 } | 139 } |
| OLD | NEW |