Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <CommonCrypto/CommonCryptor.h> | 7 #include <CommonCrypto/CommonCryptor.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "crypto/symmetric_key.h" | 11 #include "crypto/symmetric_key.h" |
| 12 | 12 |
| 13 namespace crypto { | 13 namespace crypto { |
| 14 | 14 |
| 15 Encryptor::Encryptor() | 15 Encryptor::Encryptor() |
| 16 : key_(NULL), | 16 : key_(NULL), |
| 17 mode_(CBC) { | 17 mode_(CBC) { |
| 18 } | 18 } |
| 19 | 19 |
| 20 Encryptor::~Encryptor() { | 20 Encryptor::~Encryptor() { |
| 21 } | 21 } |
| 22 | 22 |
| 23 bool Encryptor::Init(SymmetricKey* key, | 23 bool Encryptor::Init(SymmetricKey* key, |
| 24 Mode mode, | 24 Mode mode, |
| 25 const base::StringPiece& iv) { | 25 const base::StringPiece& iv) { |
| 26 DCHECK(key); | 26 DCHECK_EQ(CBC, mode); |
| 27 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; | 27 if (!key) |
| 28 return false; | |
| 29 | |
| 28 CSSM_DATA raw_key = key->cssm_data(); | 30 CSSM_DATA raw_key = key->cssm_data(); |
| 29 if (raw_key.Length != kCCKeySizeAES128 && | 31 if (raw_key.Length != kCCKeySizeAES128 && |
| 30 raw_key.Length != kCCKeySizeAES192 && | 32 raw_key.Length != kCCKeySizeAES192 && |
| 31 raw_key.Length != kCCKeySizeAES256) | 33 raw_key.Length != kCCKeySizeAES256) |
| 32 return false; | 34 return false; |
| 33 if (iv.size() != kCCBlockSizeAES128) | 35 if (iv.size() != kCCBlockSizeAES128) |
| 34 return false; | 36 return false; |
| 35 | 37 |
| 36 key_ = key; | 38 key_ = key; |
| 37 mode_ = mode; | 39 mode_ = mode; |
| 38 iv.CopyToString(&iv_); | 40 iv.CopyToString(&iv_); |
| 39 return true; | 41 return true; |
| 40 } | 42 } |
| 41 | 43 |
| 42 bool Encryptor::Crypt(int /*CCOperation*/ op, | 44 bool Encryptor::Crypt(int /*CCOperation*/ op, |
| 43 const base::StringPiece& input, | 45 const base::StringPiece& input, |
| 44 std::string* output) { | 46 std::string* output) { |
| 45 DCHECK(key_); | 47 std::string result; |
| 48 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
| |
| 49 if (!key_) | |
| 50 return false; | |
| 51 | |
| 46 CSSM_DATA raw_key = key_->cssm_data(); | 52 CSSM_DATA raw_key = key_->cssm_data(); |
| 47 // CommonCryptor.h: "A general rule for the size of the output buffer which | 53 // CommonCryptor.h: "A general rule for the size of the output buffer which |
| 48 // must be provided by the caller is that for block ciphers, the output | 54 // must be provided by the caller is that for block ciphers, the output |
| 49 // length is never larger than the input length plus the block size." | 55 // length is never larger than the input length plus the block size." |
| 50 | 56 size_t result_size = input.size() + iv_.size(); |
| 51 size_t output_size = input.size() + iv_.size(); | 57 if (result_size == 0 || result_size < input.size() || |
| 52 CHECK_GT(output_size, 0u); | 58 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,
| |
| 53 CHECK_GT(output_size + 1, input.size()); | 59 return false; |
| 60 } | |
| 54 CCCryptorStatus err = CCCrypt(op, | 61 CCCryptorStatus err = CCCrypt(op, |
| 55 kCCAlgorithmAES128, | 62 kCCAlgorithmAES128, |
| 56 kCCOptionPKCS7Padding, | 63 kCCOptionPKCS7Padding, |
| 57 raw_key.Data, raw_key.Length, | 64 raw_key.Data, raw_key.Length, |
| 58 iv_.data(), | 65 iv_.data(), |
| 59 input.data(), input.size(), | 66 input.data(), input.size(), |
| 60 WriteInto(output, output_size + 1), | 67 WriteInto(&result, result_size + 1), |
| 61 output_size, | 68 result_size, |
| 62 &output_size); | 69 &result_size); |
| 63 if (err) { | 70 if (err) { |
| 64 output->clear(); | |
| 65 LOG(ERROR) << "CCCrypt returned " << err; | 71 LOG(ERROR) << "CCCrypt returned " << err; |
| 66 return false; | 72 return false; |
| 67 } | 73 } |
| 68 output->resize(output_size); | 74 result.resize(result_size); |
| 75 output->swap(result); | |
| 69 return true; | 76 return true; |
| 70 } | 77 } |
| 71 | 78 |
| 72 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 79 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 73 std::string* ciphertext) { | 80 std::string* ciphertext) { |
| 74 CHECK(!plaintext.empty() || (mode_ == CBC)); | 81 if (plaintext.empty() && mode_ != CBC) { |
| 82 ciphertext->clear(); | |
|
wtc
2011/12/15 02:03:58
This ciphertext->clear() call and the one on line
| |
| 83 return false; | |
| 84 } | |
| 75 return Crypt(kCCEncrypt, plaintext, ciphertext); | 85 return Crypt(kCCEncrypt, plaintext, ciphertext); |
| 76 } | 86 } |
| 77 | 87 |
| 78 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 88 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 79 std::string* plaintext) { | 89 std::string* plaintext) { |
| 80 CHECK(!ciphertext.empty()); | 90 if (ciphertext.empty()) { |
| 91 plaintext->clear(); | |
| 92 return false; | |
| 93 } | |
| 81 return Crypt(kCCDecrypt, ciphertext, plaintext); | 94 return Crypt(kCCDecrypt, ciphertext, plaintext); |
| 82 } | 95 } |
| 83 | 96 |
| 84 } // namespace crypto | 97 } // namespace crypto |
| OLD | NEW |