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" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
42 bool Encryptor::Crypt(int /*CCOperation*/ op, | 42 bool Encryptor::Crypt(int /*CCOperation*/ op, |
43 const base::StringPiece& input, | 43 const base::StringPiece& input, |
44 std::string* output) { | 44 std::string* output) { |
45 DCHECK(key_); | 45 DCHECK(key_); |
46 CSSM_DATA raw_key = key_->cssm_data(); | 46 CSSM_DATA raw_key = key_->cssm_data(); |
47 // CommonCryptor.h: "A general rule for the size of the output buffer which | 47 // 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 | 48 // 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." | 49 // length is never larger than the input length plus the block size." |
50 | 50 |
51 size_t output_size = input.size() + iv_.size(); | 51 size_t output_size = input.size() + iv_.size(); |
52 DCHECK_GT(output_size, 0u); | |
53 DCHECK_GT(output_size + 1, input.size()); | |
Ryan Sleevi
2011/11/01 23:08:59
Both of these can be changed to CHECK_GT
| |
52 CCCryptorStatus err = CCCrypt(op, | 54 CCCryptorStatus err = CCCrypt(op, |
53 kCCAlgorithmAES128, | 55 kCCAlgorithmAES128, |
54 kCCOptionPKCS7Padding, | 56 kCCOptionPKCS7Padding, |
55 raw_key.Data, raw_key.Length, | 57 raw_key.Data, raw_key.Length, |
56 iv_.data(), | 58 iv_.data(), |
57 input.data(), input.size(), | 59 input.data(), input.size(), |
58 WriteInto(output, output_size+1), | 60 WriteInto(output, output_size + 1), |
59 output_size, | 61 output_size, |
60 &output_size); | 62 &output_size); |
61 if (err) { | 63 if (err) { |
62 output->resize(0); | 64 output->clear(); |
63 LOG(ERROR) << "CCCrypt returned " << err; | 65 LOG(ERROR) << "CCCrypt returned " << err; |
64 return false; | 66 return false; |
65 } | 67 } |
66 output->resize(output_size); | 68 output->resize(output_size); |
67 return true; | 69 return true; |
68 } | 70 } |
69 | 71 |
70 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 72 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
71 std::string* ciphertext) { | 73 std::string* ciphertext) { |
74 DCHECK(!plaintext.empty() || (mode_ == CBC)); | |
72 return Crypt(kCCEncrypt, plaintext, ciphertext); | 75 return Crypt(kCCEncrypt, plaintext, ciphertext); |
73 } | 76 } |
74 | 77 |
75 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 78 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
76 std::string* plaintext) { | 79 std::string* plaintext) { |
80 DCHECK(!ciphertext.empty()); | |
77 return Crypt(kCCDecrypt, ciphertext, plaintext); | 81 return Crypt(kCCDecrypt, ciphertext, plaintext); |
78 } | 82 } |
79 | 83 |
80 } // namespace crypto | 84 } // namespace crypto |
OLD | NEW |