Chromium Code Reviews| Index: crypto/encryptor_mac.cc |
| diff --git a/crypto/encryptor_mac.cc b/crypto/encryptor_mac.cc |
| index 6be373a5d92028052b1de94d5f1d85739b54ffcf..15c6e39a92bb27b8d4ddcae7123e3cd730ddc21c 100644 |
| --- a/crypto/encryptor_mac.cc |
| +++ b/crypto/encryptor_mac.cc |
| @@ -23,8 +23,10 @@ Encryptor::~Encryptor() { |
| bool Encryptor::Init(SymmetricKey* key, |
| Mode mode, |
| const base::StringPiece& iv) { |
| - DCHECK(key); |
| - DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; |
| + DCHECK_EQ(CBC, mode); |
| + if (!key) |
| + return false; |
| + |
| CSSM_DATA raw_key = key->cssm_data(); |
| if (raw_key.Length != kCCKeySizeAES128 && |
| raw_key.Length != kCCKeySizeAES192 && |
| @@ -42,42 +44,53 @@ bool Encryptor::Init(SymmetricKey* key, |
| bool Encryptor::Crypt(int /*CCOperation*/ op, |
| const base::StringPiece& input, |
| std::string* output) { |
| - DCHECK(key_); |
| + std::string result; |
| + output->swap(result); |
|
wtc
2011/12/15 02:03:58
IMPORTANT: this swap is not necessary. Our conven
Peter Kasting
2011/12/15 02:12:39
FWIW I intentionally eliminated both swap()s in my
wtc
2011/12/15 02:16:44
You are right. You stated the convention better t
Ryan Sleevi
2011/12/15 02:18:16
This was based on past misuses of this API, where
wtc
2011/12/15 22:04:14
Thank you for the explanation. It is hard to figu
|
| + if (!key_) |
| + return false; |
| + |
| CSSM_DATA raw_key = key_->cssm_data(); |
| // CommonCryptor.h: "A general rule for the size of the output buffer which |
| // must be provided by the caller is that for block ciphers, the output |
| // length is never larger than the input length plus the block size." |
| - |
| - size_t output_size = input.size() + iv_.size(); |
| - CHECK_GT(output_size, 0u); |
| - CHECK_GT(output_size + 1, input.size()); |
| + size_t result_size = input.size() + iv_.size(); |
| + if (result_size == 0 || result_size < input.size() || |
| + result_size + 1 < input.size()) { |
|
wtc
2011/12/15 02:03:58
IMPORTANT: The need to test both result_size < inp
Ryan Sleevi
2011/12/15 02:18:16
It is intentional - I will add a comment to clarif
Peter Kasting
2011/12/15 02:20:09
Uff da.
If you're going to add a comment anyway,
|
| + return false; |
| + } |
| CCCryptorStatus err = CCCrypt(op, |
| kCCAlgorithmAES128, |
| kCCOptionPKCS7Padding, |
| raw_key.Data, raw_key.Length, |
| iv_.data(), |
| input.data(), input.size(), |
| - WriteInto(output, output_size + 1), |
| - output_size, |
| - &output_size); |
| + WriteInto(&result, result_size + 1), |
| + result_size, |
| + &result_size); |
| if (err) { |
| - output->clear(); |
| LOG(ERROR) << "CCCrypt returned " << err; |
| return false; |
| } |
| - output->resize(output_size); |
| + result.resize(result_size); |
| + output->swap(result); |
| return true; |
| } |
| bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| std::string* ciphertext) { |
| - CHECK(!plaintext.empty() || (mode_ == CBC)); |
| + if (plaintext.empty() && mode_ != CBC) { |
| + ciphertext->clear(); |
|
wtc
2011/12/15 02:03:58
This ciphertext->clear() call and the one on line
|
| + return false; |
| + } |
| return Crypt(kCCEncrypt, plaintext, ciphertext); |
| } |
| bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| std::string* plaintext) { |
| - CHECK(!ciphertext.empty()); |
| + if (ciphertext.empty()) { |
| + plaintext->clear(); |
| + return false; |
| + } |
| return Crypt(kCCDecrypt, ciphertext, plaintext); |
| } |