OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" | 5 #include "base/crypto/encryptor.h" |
6 | 6 |
| 7 #include <CommonCrypto/CommonCryptor.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" |
| 11 |
7 namespace base { | 12 namespace base { |
8 | 13 |
9 // TODO(albertb): Implement on Mac using the Common Crypto Library: | |
10 // http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPage
s/man3/CCCryptor.3cc.html#//apple_ref/doc/man/10.5/3cc/CCCryptor?useVersion=10.5 | |
11 | |
12 Encryptor::Encryptor() { | 14 Encryptor::Encryptor() { |
13 } | 15 } |
14 | 16 |
15 Encryptor::~Encryptor() { | 17 Encryptor::~Encryptor() { |
16 } | 18 } |
17 | 19 |
18 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 20 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
19 return false; | 21 DCHECK(key); |
| 22 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; |
| 23 CSSM_DATA raw_key = key->cssm_data(); |
| 24 if (raw_key.Length != kCCKeySizeAES128 && |
| 25 raw_key.Length != kCCKeySizeAES192 && |
| 26 raw_key.Length != kCCKeySizeAES256) |
| 27 return false; |
| 28 if (iv.size() != kCCBlockSizeAES128) |
| 29 return false; |
| 30 |
| 31 key_.reset(key); |
| 32 mode_ = mode; |
| 33 iv_ = iv; |
| 34 return true; |
| 35 } |
| 36 |
| 37 bool Encryptor::Crypt(int /*CCOperation*/ op, |
| 38 const std::string& input, |
| 39 std::string* output) { |
| 40 DCHECK(key_.get()); |
| 41 CSSM_DATA raw_key = key_->cssm_data(); |
| 42 // CommonCryptor.h: "A general rule for the size of the output buffer which |
| 43 // must be provided by the caller is that for block ciphers, the output |
| 44 // length is never larger than the input length plus the block size." |
| 45 |
| 46 size_t output_size = input.size() + iv_.size(); |
| 47 CCCryptorStatus err = CCCrypt(op, |
| 48 kCCAlgorithmAES128, |
| 49 kCCOptionPKCS7Padding, |
| 50 raw_key.Data, raw_key.Length, |
| 51 iv_.data(), |
| 52 input.data(), input.size(), |
| 53 WriteInto(output, output_size), |
| 54 output_size, |
| 55 &output_size); |
| 56 if (err) { |
| 57 output->resize(0); |
| 58 LOG(ERROR) << "CCCrypt returned " << err; |
| 59 return false; |
| 60 } |
| 61 output->resize(output_size); |
| 62 return true; |
20 } | 63 } |
21 | 64 |
22 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 65 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { |
23 return false; | 66 return Crypt(kCCEncrypt, plaintext, ciphertext); |
24 } | 67 } |
25 | 68 |
26 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 69 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { |
27 return false; | 70 return Crypt(kCCDecrypt, ciphertext, plaintext); |
28 } | 71 } |
29 | 72 |
30 } // namespace base | 73 } // namespace base |
OLD | NEW |