| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 // NOTE: this file is Winodws specific. | 5 // NOTE: this file is Winodws specific. |
| 6 | 6 |
| 7 #include "chrome/browser/sync/util/data_encryption.h" | 7 #include "chrome/browser/sync/util/data_encryption.h" |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <wincrypt.h> | 10 #include <wincrypt.h> |
| 11 | 11 |
| 12 #include <cstddef> | 12 #include <cstddef> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 using std::string; | 16 using std::string; |
| 17 using std::vector; | 17 using std::vector; |
| 18 | 18 |
| 19 vector<uint8> EncryptData(const string& data) { | 19 vector<uint8> EncryptData(const string& data) { |
| 20 DATA_BLOB unencrypted_data, encrypted_data; | 20 DATA_BLOB unencrypted_data = { 0 }; |
| 21 unencrypted_data.pbData = (BYTE*)(data.data()); | 21 unencrypted_data.pbData = (BYTE*)(data.data()); |
| 22 unencrypted_data.cbData = data.size(); | 22 unencrypted_data.cbData = data.size(); |
| 23 DATA_BLOB encrypted_data = { 0 }; |
| 23 | 24 |
| 24 if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0, | 25 if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0, |
| 25 &encrypted_data)) | 26 &encrypted_data)) |
| 26 LOG(ERROR) << "Encryption fails: " << data; | 27 LOG(ERROR) << "Encryption fails: " << data; |
| 27 | 28 |
| 28 vector<uint8> result(encrypted_data.pbData, | 29 vector<uint8> result(encrypted_data.pbData, |
| 29 encrypted_data.pbData + encrypted_data.cbData); | 30 encrypted_data.pbData + encrypted_data.cbData); |
| 30 LocalFree(encrypted_data.pbData); | 31 LocalFree(encrypted_data.pbData); |
| 31 return result; | 32 return result; |
| 32 } | 33 } |
| 33 | 34 |
| 34 bool DecryptData(const vector<uint8>& in_data, string* out_data) { | 35 bool DecryptData(const vector<uint8>& in_data, string* out_data) { |
| 35 DATA_BLOB encrypted_data, decrypted_data; | 36 DATA_BLOB encrypted_data, decrypted_data; |
| 36 encrypted_data.pbData = | 37 encrypted_data.pbData = |
| 37 (in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0])); | 38 (in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0])); |
| 38 encrypted_data.cbData = in_data.size(); | 39 encrypted_data.cbData = in_data.size(); |
| 39 LPWSTR descrip = L""; | 40 LPWSTR descrip = L""; |
| 40 | 41 |
| 41 if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0, | 42 if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0, |
| 42 &decrypted_data)) { | 43 &decrypted_data)) { |
| 43 LOG(ERROR) << "Decryption fails: "; | 44 LOG(ERROR) << "Decryption fails: "; |
| 44 return false; | 45 return false; |
| 45 } else { | 46 } else { |
| 46 out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData), | 47 out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData), |
| 47 decrypted_data.cbData); | 48 decrypted_data.cbData); |
| 48 LocalFree(decrypted_data.pbData); | 49 LocalFree(decrypted_data.pbData); |
| 49 return true; | 50 return true; |
| 50 } | 51 } |
| 51 } | 52 } |
| OLD | NEW |