| 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> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "crypto/encryptor.h" | 14 #include "crypto/encryptor.h" |
| 14 #include "crypto/symmetric_key.h" | 15 #include "crypto/symmetric_key.h" |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Salt for Symmetric key derivation. | 19 // Salt for Symmetric key derivation. |
| 19 const char kSalt[] = "saltysalt"; | 20 const char kSalt[] = "saltysalt"; |
| 20 | 21 |
| 21 // Key size required for 128 bit AES. | 22 // Key size required for 128 bit AES. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 if (ciphertext.empty()) { | 112 if (ciphertext.empty()) { |
| 112 *plaintext = std::string(); | 113 *plaintext = std::string(); |
| 113 return true; | 114 return true; |
| 114 } | 115 } |
| 115 | 116 |
| 116 // Check that the incoming cyphertext was indeed encrypted with the expected | 117 // Check that the incoming cyphertext was indeed encrypted with the expected |
| 117 // version. If the prefix is not found then we'll assume we're dealing with | 118 // version. If the prefix is not found then we'll assume we're dealing with |
| 118 // old data saved as clear text and we'll return it directly. | 119 // old data saved as clear text and we'll return it directly. |
| 119 // Credit card numbers are current legacy data, so false match with prefix | 120 // Credit card numbers are current legacy data, so false match with prefix |
| 120 // won't happen. | 121 // won't happen. |
| 121 if (ciphertext.find(kObfuscationPrefix) != 0) { | 122 if (!base::StartsWith(ciphertext, kObfuscationPrefix, |
| 123 base::CompareCase::SENSITIVE)) { |
| 122 *plaintext = ciphertext; | 124 *plaintext = ciphertext; |
| 123 return true; | 125 return true; |
| 124 } | 126 } |
| 125 | 127 |
| 126 // Strip off the versioning prefix before decrypting. | 128 // Strip off the versioning prefix before decrypting. |
| 127 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); | 129 std::string raw_ciphertext = ciphertext.substr(strlen(kObfuscationPrefix)); |
| 128 | 130 |
| 129 std::unique_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); | 131 std::unique_ptr<crypto::SymmetricKey> encryption_key(GetEncryptionKey()); |
| 130 if (!encryption_key.get()) | 132 if (!encryption_key.get()) |
| 131 return false; | 133 return false; |
| 132 | 134 |
| 133 std::string iv(kIVBlockSizeAES128, ' '); | 135 std::string iv(kIVBlockSizeAES128, ' '); |
| 134 crypto::Encryptor encryptor; | 136 crypto::Encryptor encryptor; |
| 135 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) | 137 if (!encryptor.Init(encryption_key.get(), crypto::Encryptor::CBC, iv)) |
| 136 return false; | 138 return false; |
| 137 | 139 |
| 138 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) | 140 if (!encryptor.Decrypt(raw_ciphertext, plaintext)) |
| 139 return false; | 141 return false; |
| 140 | 142 |
| 141 return true; | 143 return true; |
| 142 } | 144 } |
| OLD | NEW |