Index: crypto/encryptor_mac.cc |
diff --git a/crypto/encryptor_mac.cc b/crypto/encryptor_mac.cc |
index a08d09ef3779ff7677203634048d80709d1f0a02..ab786445320e21d7762acc0785a17914baa8ce90 100644 |
--- a/crypto/encryptor_mac.cc |
+++ b/crypto/encryptor_mac.cc |
@@ -23,8 +23,9 @@ Encryptor::~Encryptor() { |
bool Encryptor::Init(SymmetricKey* key, |
Mode mode, |
const base::StringPiece& iv) { |
- DCHECK(key); |
- DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; |
+ if (!key || mode != CBC) |
+ return false; |
+ |
CSSM_DATA raw_key = key->cssm_data(); |
if (raw_key.Length != kCCKeySizeAES128 && |
raw_key.Length != kCCKeySizeAES192 && |
@@ -42,38 +43,48 @@ bool Encryptor::Init(SymmetricKey* key, |
bool Encryptor::Crypt(int /*CCOperation*/ op, |
const base::StringPiece& input, |
std::string* output) { |
- DCHECK(key_); |
+ output->clear(); |
+ 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." |
+ std::string result; |
size_t output_size = input.size() + iv_.size(); |
+ if (output_size == 0 || output_size + 1 < input.size()) |
wtc
2011/11/15 02:33:58
I don't think we need to check output_size + 1 < i
|
+ 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), |
+ WriteInto(&result, output_size+1), |
output_size, |
&output_size); |
if (err) { |
- output->resize(0); |
LOG(ERROR) << "CCCrypt returned " << err; |
return false; |
} |
- output->resize(output_size); |
+ result.resize(output_size); |
+ output->swap(result); |
return true; |
} |
bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
std::string* ciphertext) { |
+ if (plaintext.empty() && mode_ != CBC) |
+ return false; |
return Crypt(kCCEncrypt, plaintext, ciphertext); |
} |
bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
std::string* plaintext) { |
+ if (ciphertext.empty()) |
wtc
2011/11/15 02:33:58
Why don't you also check mode_ != CBC here as you
Ryan Sleevi
2011/12/14 06:07:53
Empty ciphertext should always be treated as an er
|
+ return false; |
return Crypt(kCCDecrypt, ciphertext, plaintext); |
} |