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