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

Side by Side Diff: crypto/encryptor.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: Fix mac and linux Created 9 years 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 "base/logging.h" 7 #include "base/logging.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 9
10 // Include headers to provide bswap for all platforms. 10 // Include headers to provide bswap for all platforms.
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 87
88 bool Encryptor::GenerateCounterMask(size_t plaintext_len, 88 bool Encryptor::GenerateCounterMask(size_t plaintext_len,
89 uint8* mask, 89 uint8* mask,
90 size_t* mask_len) { 90 size_t* mask_len) {
91 DCHECK_EQ(CTR, mode_); 91 DCHECK_EQ(CTR, mode_);
92 CHECK(mask); 92 CHECK(mask);
93 CHECK(mask_len); 93 CHECK(mask_len);
94 94
95 const size_t kBlockLength = counter_->GetLengthInBytes(); 95 const size_t kBlockLength = counter_->GetLengthInBytes();
96 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; 96 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength;
97 CHECK(blocks); 97 if (blocks == 0)
98 return false;
wtc 2011/12/15 02:03:58 Should we also mark the GenerateCounterMask method
Ryan Sleevi 2011/12/15 02:18:16 Yes. It matches the previous CHECK, but empty plai
wtc 2011/12/15 22:04:14 Thanks for the explanation. This is fine.
98 99
99 *mask_len = blocks * kBlockLength; 100 *mask_len = blocks * kBlockLength;
100 101
101 for (size_t i = 0; i < blocks; ++i) { 102 for (size_t i = 0; i < blocks; ++i) {
102 counter_->Write(mask); 103 counter_->Write(mask);
103 mask += kBlockLength; 104 mask += kBlockLength;
104 105
105 bool ret = counter_->Increment(); 106 bool ret = counter_->Increment();
106 if (!ret) 107 if (!ret)
107 return false; 108 return false;
108 } 109 }
109 return true; 110 return true;
110 } 111 }
111 112
112 void Encryptor::MaskMessage(const void* plaintext, 113 void Encryptor::MaskMessage(const void* plaintext,
113 size_t plaintext_len, 114 size_t plaintext_len,
114 const void* mask, 115 const void* mask,
115 void* ciphertext) const { 116 void* ciphertext) const {
116 DCHECK_EQ(CTR, mode_); 117 DCHECK_EQ(CTR, mode_);
117 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext); 118 const uint8* plaintext_ptr = reinterpret_cast<const uint8*>(plaintext);
118 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask); 119 const uint8* mask_ptr = reinterpret_cast<const uint8*>(mask);
119 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext); 120 uint8* ciphertext_ptr = reinterpret_cast<uint8*>(ciphertext);
120 121
121 for (size_t i = 0; i < plaintext_len; ++i) 122 for (size_t i = 0; i < plaintext_len; ++i)
122 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; 123 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i];
123 } 124 }
124 125
125 } // namespace crypto 126 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.h ('k') | crypto/encryptor_mac.cc » ('j') | crypto/encryptor_mac.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698