| 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/encryptor/encryptor.h" | 5 #include "components/encryptor/os_crypt.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
| 9 |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 10 | 11 |
| 11 #pragma comment(lib, "crypt32.lib") | 12 #pragma comment(lib, "crypt32.lib") |
| 12 | 13 |
| 13 bool Encryptor::EncryptString16(const base::string16& plaintext, | 14 bool OSCrypt::EncryptString16(const base::string16& plaintext, |
| 14 std::string* ciphertext) { | 15 std::string* ciphertext) { |
| 15 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); | 16 return EncryptString(base::UTF16ToUTF8(plaintext), ciphertext); |
| 16 } | 17 } |
| 17 | 18 |
| 18 bool Encryptor::DecryptString16(const std::string& ciphertext, | 19 bool OSCrypt::DecryptString16(const std::string& ciphertext, |
| 19 base::string16* plaintext) { | 20 base::string16* plaintext) { |
| 20 std::string utf8; | 21 std::string utf8; |
| 21 if (!DecryptString(ciphertext, &utf8)) | 22 if (!DecryptString(ciphertext, &utf8)) |
| 22 return false; | 23 return false; |
| 23 | 24 |
| 24 *plaintext = base::UTF8ToUTF16(utf8); | 25 *plaintext = base::UTF8ToUTF16(utf8); |
| 25 return true; | 26 return true; |
| 26 } | 27 } |
| 27 | 28 |
| 28 bool Encryptor::EncryptString(const std::string& plaintext, | 29 bool OSCrypt::EncryptString(const std::string& plaintext, |
| 29 std::string* ciphertext) { | 30 std::string* ciphertext) { |
| 30 DATA_BLOB input; | 31 DATA_BLOB input; |
| 31 input.pbData = const_cast<BYTE*>( | 32 input.pbData = const_cast<BYTE*>( |
| 32 reinterpret_cast<const BYTE*>(plaintext.data())); | 33 reinterpret_cast<const BYTE*>(plaintext.data())); |
| 33 input.cbData = static_cast<DWORD>(plaintext.length()); | 34 input.cbData = static_cast<DWORD>(plaintext.length()); |
| 34 | 35 |
| 35 DATA_BLOB output; | 36 DATA_BLOB output; |
| 36 BOOL result = CryptProtectData(&input, L"", NULL, NULL, NULL, | 37 BOOL result = CryptProtectData(&input, L"", NULL, NULL, NULL, |
| 37 0, &output); | 38 0, &output); |
| 38 if (!result) | 39 if (!result) |
| 39 return false; | 40 return false; |
| 40 | 41 |
| 41 // this does a copy | 42 // this does a copy |
| 42 ciphertext->assign(reinterpret_cast<std::string::value_type*>(output.pbData), | 43 ciphertext->assign(reinterpret_cast<std::string::value_type*>(output.pbData), |
| 43 output.cbData); | 44 output.cbData); |
| 44 | 45 |
| 45 LocalFree(output.pbData); | 46 LocalFree(output.pbData); |
| 46 return true; | 47 return true; |
| 47 } | 48 } |
| 48 | 49 |
| 49 bool Encryptor::DecryptString(const std::string& ciphertext, | 50 bool OSCrypt::DecryptString(const std::string& ciphertext, |
| 50 std::string* plaintext) { | 51 std::string* plaintext) { |
| 51 DATA_BLOB input; | 52 DATA_BLOB input; |
| 52 input.pbData = const_cast<BYTE*>( | 53 input.pbData = const_cast<BYTE*>( |
| 53 reinterpret_cast<const BYTE*>(ciphertext.data())); | 54 reinterpret_cast<const BYTE*>(ciphertext.data())); |
| 54 input.cbData = static_cast<DWORD>(ciphertext.length()); | 55 input.cbData = static_cast<DWORD>(ciphertext.length()); |
| 55 | 56 |
| 56 DATA_BLOB output; | 57 DATA_BLOB output; |
| 57 BOOL result = CryptUnprotectData(&input, NULL, NULL, NULL, NULL, | 58 BOOL result = CryptUnprotectData(&input, NULL, NULL, NULL, NULL, |
| 58 0, &output); | 59 0, &output); |
| 59 if (!result) | 60 if (!result) |
| 60 return false; | 61 return false; |
| 61 | 62 |
| 62 plaintext->assign(reinterpret_cast<char*>(output.pbData), output.cbData); | 63 plaintext->assign(reinterpret_cast<char*>(output.pbData), output.cbData); |
| 63 LocalFree(output.pbData); | 64 LocalFree(output.pbData); |
| 64 return true; | 65 return true; |
| 65 } | 66 } |
| OLD | NEW |