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

Side by Side Diff: crypto/encryptor_win.cc

Issue 8511050: Unify the error checking of crypto::Encryptor and add WARN_UNUSED_RESULT to prevent misuse. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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
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 <string.h> 7 #include <string.h>
8 8
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "crypto/symmetric_key.h" 10 #include "crypto/symmetric_key.h"
(...skipping 23 matching lines...) Expand all
34 mode_(CBC), 34 mode_(CBC),
35 block_size_(0) { 35 block_size_(0) {
36 } 36 }
37 37
38 Encryptor::~Encryptor() { 38 Encryptor::~Encryptor() {
39 } 39 }
40 40
41 bool Encryptor::Init(SymmetricKey* key, 41 bool Encryptor::Init(SymmetricKey* key,
42 Mode mode, 42 Mode mode,
43 const base::StringPiece& iv) { 43 const base::StringPiece& iv) {
44 DCHECK(key); 44 if (!key || mode != CBC)
45 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; 45 return false;
46 46
47 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining 47 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining
48 // mode) are properties of a key, so we have to create a copy of the key for 48 // mode) are properties of a key, so we have to create a copy of the key for
49 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. 49 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page.
50 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); 50 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive());
51 if (!ok) 51 if (!ok)
52 return false; 52 return false;
53 53
54 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider, 54 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider,
55 // but we set it anyway to be safe. 55 // but we set it anyway to be safe.
(...skipping 19 matching lines...) Expand all
75 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, 75 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING,
76 reinterpret_cast<BYTE*>(&padding_method), 0); 76 reinterpret_cast<BYTE*>(&padding_method), 0);
77 if (!ok) 77 if (!ok)
78 return false; 78 return false;
79 79
80 return true; 80 return true;
81 } 81 }
82 82
83 bool Encryptor::Encrypt(const base::StringPiece& plaintext, 83 bool Encryptor::Encrypt(const base::StringPiece& plaintext,
84 std::string* ciphertext) { 84 std::string* ciphertext) {
85 ciphertext->clear();
86 if (plaintext.empty() && mode_ != CBC)
87 return false;
88
85 DWORD data_len = plaintext.size(); 89 DWORD data_len = plaintext.size();
86 DWORD total_len = data_len + block_size_; 90 DWORD total_len = data_len + block_size_;
87 CHECK_GT(total_len, data_len); 91 if (total_len == 0 || total_len + 1 < data_len)
92 return false;
88 93
89 // CryptoAPI encrypts/decrypts in place. 94 // CryptoAPI encrypts/decrypts in place.
90 char* ciphertext_data = WriteInto(ciphertext, total_len + 1); 95 std::string result;
91 memcpy(ciphertext_data, plaintext.data(), data_len); 96 char* result_data = WriteInto(&result, total_len + 1);
97 memcpy(result_data, plaintext.data(), data_len);
92 98
93 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, 99 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0,
94 reinterpret_cast<BYTE*>(ciphertext_data), &data_len, 100 reinterpret_cast<BYTE*>(result_data), &data_len,
95 total_len); 101 total_len);
96 if (!ok) { 102 if (!ok)
97 ciphertext->clear();
98 return false; 103 return false;
99 }
100 104
101 ciphertext->resize(data_len); 105 result.resize(data_len);
106 ciphertext->swap(result);
102 return true; 107 return true;
103 } 108 }
104 109
105 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, 110 bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
106 std::string* plaintext) { 111 std::string* plaintext) {
112 plaintext->clear();
107 DWORD data_len = ciphertext.size(); 113 DWORD data_len = ciphertext.size();
108 if (data_len == 0 || (data_len + 1) < data_len) 114 if (data_len == 0 || data_len + 1 < data_len)
109 return false; 115 return false;
110 116
111 // CryptoAPI encrypts/decrypts in place. 117 // CryptoAPI encrypts/decrypts in place.
112 char* plaintext_data = WriteInto(plaintext, data_len + 1); 118 std::string result;
113 memcpy(plaintext_data, ciphertext.data(), data_len); 119 char* result_data = WriteInto(&result, data_len + 1);
120 memcpy(result_data, ciphertext.data(), data_len);
114 121
115 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, 122 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0,
116 reinterpret_cast<BYTE*>(plaintext_data), &data_len); 123 reinterpret_cast<BYTE*>(result_data), &data_len);
117 if (!ok) { 124 if (!ok)
118 plaintext->clear();
119 return false; 125 return false;
120 }
121 126
122 plaintext->resize(data_len); 127 result.resize(data_len);
128 plaintext->swap(result);
123 return true; 129 return true;
124 } 130 }
125 131
126 } // namespace crypto 132 } // namespace crypto
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698