Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: crypto/encryptor_mac.cc

Issue 7230037: Use base::StringPiece for input parameters in Encryptor, rather than std::string (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Feedback Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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,
24 Mode mode,
25 const base::StringPiece& iv) {
24 DCHECK(key); 26 DCHECK(key);
25 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; 27 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
26 CSSM_DATA raw_key = key->cssm_data(); 28 CSSM_DATA raw_key = key->cssm_data();
27 if (raw_key.Length != kCCKeySizeAES128 && 29 if (raw_key.Length != kCCKeySizeAES128 &&
28 raw_key.Length != kCCKeySizeAES192 && 30 raw_key.Length != kCCKeySizeAES192 &&
29 raw_key.Length != kCCKeySizeAES256) 31 raw_key.Length != kCCKeySizeAES256)
30 return false; 32 return false;
31 if (iv.size() != kCCBlockSizeAES128) 33 if (iv.size() != kCCBlockSizeAES128)
32 return false; 34 return false;
33 35
34 key_ = key; 36 key_ = key;
35 mode_ = mode; 37 mode_ = mode;
36 iv_ = iv; 38 iv.CopyToString(&iv_);
37 return true; 39 return true;
38 } 40 }
39 41
40 bool Encryptor::Crypt(int /*CCOperation*/ op, 42 bool Encryptor::Crypt(int /*CCOperation*/ op,
41 const std::string& input, 43 const base::StringPiece& input,
42 std::string* output) { 44 std::string* output) {
43 DCHECK(key_); 45 DCHECK(key_);
44 CSSM_DATA raw_key = key_->cssm_data(); 46 CSSM_DATA raw_key = key_->cssm_data();
45 // 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
46 // 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
47 // 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."
48 50
49 size_t output_size = input.size() + iv_.size(); 51 size_t output_size = input.size() + iv_.size();
50 CCCryptorStatus err = CCCrypt(op, 52 CCCryptorStatus err = CCCrypt(op,
51 kCCAlgorithmAES128, 53 kCCAlgorithmAES128,
52 kCCOptionPKCS7Padding, 54 kCCOptionPKCS7Padding,
53 raw_key.Data, raw_key.Length, 55 raw_key.Data, raw_key.Length,
54 iv_.data(), 56 iv_.data(),
55 input.data(), input.size(), 57 input.data(), input.size(),
56 WriteInto(output, output_size+1), 58 WriteInto(output, output_size+1),
57 output_size, 59 output_size,
58 &output_size); 60 &output_size);
59 if (err) { 61 if (err) {
60 output->resize(0); 62 output->resize(0);
61 LOG(ERROR) << "CCCrypt returned " << err; 63 LOG(ERROR) << "CCCrypt returned " << err;
62 return false; 64 return false;
63 } 65 }
64 output->resize(output_size); 66 output->resize(output_size);
65 return true; 67 return true;
66 } 68 }
67 69
68 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { 70 bool Encryptor::Encrypt(const base::StringPiece& plaintext,
71 std::string* ciphertext) {
69 return Crypt(kCCEncrypt, plaintext, ciphertext); 72 return Crypt(kCCEncrypt, plaintext, ciphertext);
70 } 73 }
71 74
72 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { 75 bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
76 std::string* plaintext) {
73 return Crypt(kCCDecrypt, ciphertext, plaintext); 77 return Crypt(kCCDecrypt, ciphertext, plaintext);
74 } 78 }
75 79
76 } // namespace crypto 80 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.cc ('k') | crypto/encryptor_nss.cc » ('j') | crypto/encryptor_openssl.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698