| 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 "crypto/encryptor.h" | 5 #include "crypto/encryptor.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/string_util.h" |
| 9 #include "crypto/symmetric_key.h" | 10 #include "crypto/symmetric_key.h" |
| 10 | 11 |
| 11 namespace crypto { | 12 namespace crypto { |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 // On success, returns the block size (in bytes) for the algorithm that |key| | 16 // On success, returns the block size (in bytes) for the algorithm that |key| |
| 16 // is for. On failure, returns 0. | 17 // is for. On failure, returns 0. |
| 17 DWORD GetCipherBlockSize(HCRYPTKEY key) { | 18 DWORD GetCipherBlockSize(HCRYPTKEY key) { |
| 18 DWORD block_size_in_bits = 0; | 19 DWORD block_size_in_bits = 0; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 if (!ok) | 77 if (!ok) |
| 77 return false; | 78 return false; |
| 78 | 79 |
| 79 return true; | 80 return true; |
| 80 } | 81 } |
| 81 | 82 |
| 82 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 83 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 83 std::string* ciphertext) { | 84 std::string* ciphertext) { |
| 84 DWORD data_len = plaintext.size(); | 85 DWORD data_len = plaintext.size(); |
| 85 DWORD total_len = data_len + block_size_; | 86 DWORD total_len = data_len + block_size_; |
| 87 CHECK_GT(total_len, data_len); |
| 86 | 88 |
| 87 // CryptoAPI encrypts/decrypts in place. | 89 // CryptoAPI encrypts/decrypts in place. |
| 88 std::vector<BYTE> tmp(total_len); | 90 char* ciphertext_data = WriteInto(ciphertext, total_len + 1); |
| 89 memcpy(&tmp[0], plaintext.data(), data_len); | 91 memcpy(ciphertext_data, plaintext.data(), data_len); |
| 90 | 92 |
| 91 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], | 93 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, |
| 92 &data_len, total_len); | 94 reinterpret_cast<BYTE*>(ciphertext_data), &data_len, |
| 93 if (!ok) | 95 total_len); |
| 96 if (!ok) { |
| 97 ciphertext->clear(); |
| 94 return false; | 98 return false; |
| 99 } |
| 95 | 100 |
| 96 ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); | 101 ciphertext->resize(data_len); |
| 97 return true; | 102 return true; |
| 98 } | 103 } |
| 99 | 104 |
| 100 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 105 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 101 std::string* plaintext) { | 106 std::string* plaintext) { |
| 102 DWORD data_len = ciphertext.size(); | 107 DWORD data_len = ciphertext.size(); |
| 103 if (data_len == 0) | 108 if (data_len == 0 || (data_len + 1) < data_len) |
| 104 return false; | 109 return false; |
| 105 | 110 |
| 106 std::vector<BYTE> tmp(data_len); | 111 // CryptoAPI encrypts/decrypts in place. |
| 107 memcpy(&tmp[0], ciphertext.data(), data_len); | 112 char* plaintext_data = WriteInto(plaintext, data_len + 1); |
| 113 memcpy(plaintext_data, ciphertext.data(), data_len); |
| 108 | 114 |
| 109 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); | 115 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, |
| 110 if (!ok) | 116 reinterpret_cast<BYTE*>(plaintext_data), &data_len); |
| 117 if (!ok) { |
| 118 plaintext->clear(); |
| 111 return false; | 119 return false; |
| 120 } |
| 112 | 121 |
| 113 DCHECK_GT(tmp.size(), data_len); | 122 plaintext->resize(data_len); |
| 114 | |
| 115 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); | |
| 116 return true; | 123 return true; |
| 117 } | 124 } |
| 118 | 125 |
| 119 } // namespace crypto | 126 } // namespace crypto |
| OLD | NEW |