| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // | |
| 5 // NOTE: this file is Winodws specific. | |
| 6 | 4 |
| 7 #include "sync/util/data_encryption_win.h" | 5 #include "sync/util/data_encryption_win.h" |
| 8 | 6 |
| 9 #include <windows.h> | 7 #include <windows.h> |
| 10 #include <wincrypt.h> | 8 #include <wincrypt.h> |
| 11 | 9 |
| 12 #include <cstddef> | 10 #include <cstddef> |
| 13 #include <string> | |
| 14 #include <vector> | |
| 15 | 11 |
| 16 #include "base/logging.h" | 12 #include "base/logging.h" |
| 17 | 13 |
| 18 #pragma comment(lib, "crypt32.lib") | 14 #pragma comment(lib, "crypt32.lib") |
| 19 | 15 |
| 20 // TODO(akalin): Merge this with similar code in | 16 // TODO(akalin): Merge this with similar code in |
| 21 // chrome/browser/password_manager/encryptor_win.cc. Preferably, all | 17 // chrome/browser/password_manager/encryptor_win.cc. Preferably, all |
| 22 // this stuff would live in crypto/. | 18 // this stuff would live in crypto/. |
| 23 | 19 |
| 24 using std::string; | 20 namespace syncer { |
| 25 using std::vector; | |
| 26 | 21 |
| 27 vector<uint8> EncryptData(const string& data) { | 22 std::vector<uint8> EncryptData(const std::string& data) { |
| 28 DATA_BLOB unencrypted_data = { 0 }; | 23 DATA_BLOB unencrypted_data = { 0 }; |
| 29 unencrypted_data.pbData = (BYTE*)(data.data()); | 24 unencrypted_data.pbData = (BYTE*)(data.data()); |
| 30 unencrypted_data.cbData = data.size(); | 25 unencrypted_data.cbData = data.size(); |
| 31 DATA_BLOB encrypted_data = { 0 }; | 26 DATA_BLOB encrypted_data = { 0 }; |
| 32 | 27 |
| 33 if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0, | 28 if (!CryptProtectData(&unencrypted_data, L"", NULL, NULL, NULL, 0, |
| 34 &encrypted_data)) | 29 &encrypted_data)) |
| 35 LOG(ERROR) << "Encryption fails: " << data; | 30 LOG(ERROR) << "Encryption fails: " << data; |
| 36 | 31 |
| 37 vector<uint8> result(encrypted_data.pbData, | 32 std::vector<uint8> result(encrypted_data.pbData, |
| 38 encrypted_data.pbData + encrypted_data.cbData); | 33 encrypted_data.pbData + encrypted_data.cbData); |
| 39 LocalFree(encrypted_data.pbData); | 34 LocalFree(encrypted_data.pbData); |
| 40 return result; | 35 return result; |
| 41 } | 36 } |
| 42 | 37 |
| 43 bool DecryptData(const vector<uint8>& in_data, string* out_data) { | 38 bool DecryptData(const std::vector<uint8>& in_data, std::string* out_data) { |
| 44 DATA_BLOB encrypted_data, decrypted_data; | 39 DATA_BLOB encrypted_data, decrypted_data; |
| 45 encrypted_data.pbData = | 40 encrypted_data.pbData = |
| 46 (in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0])); | 41 (in_data.empty() ? NULL : const_cast<BYTE*>(&in_data[0])); |
| 47 encrypted_data.cbData = in_data.size(); | 42 encrypted_data.cbData = in_data.size(); |
| 48 LPWSTR descrip = L""; | 43 LPWSTR descrip = L""; |
| 49 | 44 |
| 50 if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0, | 45 if (!CryptUnprotectData(&encrypted_data, &descrip, NULL, NULL, NULL, 0, |
| 51 &decrypted_data)) { | 46 &decrypted_data)) { |
| 52 LOG(ERROR) << "Decryption fails: "; | 47 LOG(ERROR) << "Decryption fails: "; |
| 53 return false; | 48 return false; |
| 54 } else { | 49 } else { |
| 55 out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData), | 50 out_data->assign(reinterpret_cast<const char*>(decrypted_data.pbData), |
| 56 decrypted_data.cbData); | 51 decrypted_data.cbData); |
| 57 LocalFree(decrypted_data.pbData); | 52 LocalFree(decrypted_data.pbData); |
| 58 return true; | 53 return true; |
| 59 } | 54 } |
| 60 } | 55 } |
| 56 |
| 57 } // namespace syncer |
| OLD | NEW |