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, Mode mode, const std::string& iv) { | 23 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
24 DCHECK(key); | 24 DCHECK(key); |
25 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; | 25 DCHECK(CBC == mode || ECB == mode) << "Unsupported mode of operation"; |
26 CSSM_DATA raw_key = key->cssm_data(); | 26 CSSM_DATA raw_key = key->cssm_data(); |
27 if (raw_key.Length != kCCKeySizeAES128 && | 27 if (raw_key.Length != kCCKeySizeAES128 && |
28 raw_key.Length != kCCKeySizeAES192 && | 28 raw_key.Length != kCCKeySizeAES192 && |
29 raw_key.Length != kCCKeySizeAES256) | 29 raw_key.Length != kCCKeySizeAES256) |
30 return false; | 30 return false; |
31 if (iv.size() != kCCBlockSizeAES128) | 31 if (iv.size() != kCCBlockSizeAES128) |
32 return false; | 32 return false; |
33 | 33 |
34 key_ = key; | 34 key_ = key; |
35 mode_ = mode; | 35 mode_ = mode; |
36 iv_ = iv; | 36 iv_ = iv; |
37 return true; | 37 return true; |
38 } | 38 } |
39 | 39 |
40 bool Encryptor::Crypt(int /*CCOperation*/ op, | 40 bool Encryptor::Crypt(int /*CCOperation*/ op, |
41 const std::string& input, | 41 const std::string& input, |
42 std::string* output) { | 42 std::string* output) { |
43 DCHECK(key_); | 43 DCHECK(key_); |
44 CSSM_DATA raw_key = key_->cssm_data(); | 44 CSSM_DATA raw_key = key_->cssm_data(); |
45 // CommonCryptor.h: "A general rule for the size of the output buffer which | 45 // CommonCryptor.h: "A general rule for the size of the output buffer which |
46 // must be provided by the caller is that for block ciphers, the output | 46 // must be provided by the caller is that for block ciphers, the output |
47 // length is never larger than the input length plus the block size." | 47 // length is never larger than the input length plus the block size." |
48 | 48 |
49 size_t output_size = input.size() + iv_.size(); | 49 size_t output_size = input.size() + iv_.size(); |
50 CCCryptorStatus err = CCCrypt(op, | 50 CCCryptorStatus err = CCCrypt(op, |
51 kCCAlgorithmAES128, | 51 kCCAlgorithmAES128, |
52 kCCOptionPKCS7Padding, | 52 kCCOptionPKCS7Padding, |
Ryan Sleevi
2011/05/23 05:55:04
Bug: kCCOptionECBMode should also be passed in the
| |
53 raw_key.Data, raw_key.Length, | 53 raw_key.Data, raw_key.Length, |
54 iv_.data(), | 54 iv_.data(), |
55 input.data(), input.size(), | 55 input.data(), input.size(), |
56 WriteInto(output, output_size+1), | 56 WriteInto(output, output_size+1), |
57 output_size, | 57 output_size, |
58 &output_size); | 58 &output_size); |
59 if (err) { | 59 if (err) { |
60 output->resize(0); | 60 output->resize(0); |
61 LOG(ERROR) << "CCCrypt returned " << err; | 61 LOG(ERROR) << "CCCrypt returned " << err; |
62 return false; | 62 return false; |
63 } | 63 } |
64 output->resize(output_size); | 64 output->resize(output_size); |
65 return true; | 65 return true; |
66 } | 66 } |
67 | 67 |
68 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 68 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { |
69 return Crypt(kCCEncrypt, plaintext, ciphertext); | 69 return Crypt(kCCEncrypt, plaintext, ciphertext); |
70 } | 70 } |
71 | 71 |
72 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 72 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { |
73 return Crypt(kCCDecrypt, ciphertext, plaintext); | 73 return Crypt(kCCDecrypt, ciphertext, plaintext); |
74 } | 74 } |
75 | 75 |
76 } // namespace crypto | 76 } // namespace crypto |
OLD | NEW |