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 if (!key_) |
| 48 return false; |
| 49 |
46 CSSM_DATA raw_key = key_->cssm_data(); | 50 CSSM_DATA raw_key = key_->cssm_data(); |
47 // CommonCryptor.h: "A general rule for the size of the output buffer which | 51 // 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 | 52 // 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." | 53 // length is never larger than the input length plus the block size." |
| 54 size_t output_size = input.size() + iv_.size(); |
| 55 if (output_size == 0 || output_size + 1 < input.size()) |
| 56 return false; |
50 | 57 |
51 size_t output_size = input.size() + iv_.size(); | |
52 CHECK_GT(output_size, 0u); | |
53 CHECK_GT(output_size + 1, input.size()); | |
54 CCCryptorStatus err = CCCrypt(op, | 58 CCCryptorStatus err = CCCrypt(op, |
55 kCCAlgorithmAES128, | 59 kCCAlgorithmAES128, |
56 kCCOptionPKCS7Padding, | 60 kCCOptionPKCS7Padding, |
57 raw_key.Data, raw_key.Length, | 61 raw_key.Data, raw_key.Length, |
58 iv_.data(), | 62 iv_.data(), |
59 input.data(), input.size(), | 63 input.data(), input.size(), |
60 WriteInto(output, output_size + 1), | 64 WriteInto(output, output_size + 1), |
61 output_size, | 65 output_size, |
62 &output_size); | 66 &output_size); |
63 if (err) { | 67 if (err) { |
64 output->clear(); | |
65 LOG(ERROR) << "CCCrypt returned " << err; | 68 LOG(ERROR) << "CCCrypt returned " << err; |
66 return false; | 69 return false; |
67 } | 70 } |
68 output->resize(output_size); | 71 output->resize(output_size); |
69 return true; | 72 return true; |
70 } | 73 } |
71 | 74 |
72 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 75 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
73 std::string* ciphertext) { | 76 std::string* ciphertext) { |
74 CHECK(!plaintext.empty() || (mode_ == CBC)); | 77 if (plaintext.empty() && mode_ != CBC) |
| 78 return false; |
75 return Crypt(kCCEncrypt, plaintext, ciphertext); | 79 return Crypt(kCCEncrypt, plaintext, ciphertext); |
76 } | 80 } |
77 | 81 |
78 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 82 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
79 std::string* plaintext) { | 83 std::string* plaintext) { |
80 CHECK(!ciphertext.empty()); | 84 if (ciphertext.empty()) |
| 85 return false; |
81 return Crypt(kCCDecrypt, ciphertext, plaintext); | 86 return Crypt(kCCDecrypt, ciphertext, plaintext); |
82 } | 87 } |
83 | 88 |
84 } // namespace crypto | 89 } // namespace crypto |
OLD | NEW |