Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: crypto/encryptor_win.cc

Issue 7272022: Eliminate an extra allocation in encryptor_win when en/decrypting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Feedback Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 if (!ok) 75 if (!ok)
77 return false; 76 return false;
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_;
85 CHECK_GT(total_len, data_len);
86 86
87 // CryptoAPI encrypts/decrypts in place. 87 // CryptoAPI encrypts/decrypts in place.
88 std::vector<BYTE> tmp(total_len); 88 char* ciphertext_data = WriteInto(ciphertext, total_len + 1);
89 memcpy(&tmp[0], plaintext.data(), data_len); 89 plaintext.copy(ciphertext_data, plaintext.size(), 0);
wtc 2011/07/07 01:03:12 Note: This comment also applies to the ciphertext.
90 90
91 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], 91 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0,
92 &data_len, total_len); 92 reinterpret_cast<BYTE*>(ciphertext_data), &data_len,
93 if (!ok) 93 total_len);
94 if (!ok) {
95 ciphertext->clear();
94 return false; 96 return false;
97 }
95 98
96 ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); 99 ciphertext->resize(data_len);
97 return true; 100 return true;
98 } 101 }
99 102
100 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, 103 bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
101 std::string* plaintext) { 104 std::string* plaintext) {
102 DWORD data_len = ciphertext.size(); 105 DWORD data_len = ciphertext.size();
103 if (data_len == 0) 106 if (data_len == 0)
104 return false; 107 return false;
105 108
106 std::vector<BYTE> tmp(data_len); 109 // CryptoAPI encrypts/decrypts in place.
107 memcpy(&tmp[0], ciphertext.data(), data_len); 110 char* plaintext_data = WriteInto(plaintext, data_len + 1);
111 ciphertext.copy(plaintext_data, ciphertext.size(), 0);
108 112
109 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); 113 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0,
110 if (!ok) 114 reinterpret_cast<BYTE*>(plaintext_data), &data_len);
115 if (!ok) {
116 plaintext->clear();
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
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698