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

Side by Side Diff: crypto/encryptor_mac.cc

Issue 8511050: Unify the error checking of crypto::Encryptor and add WARN_UNUSED_RESULT to prevent misuse. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 1 month 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, 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 if (!key || mode != CBC)
27 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; 27 return false;
28
28 CSSM_DATA raw_key = key->cssm_data(); 29 CSSM_DATA raw_key = key->cssm_data();
29 if (raw_key.Length != kCCKeySizeAES128 && 30 if (raw_key.Length != kCCKeySizeAES128 &&
30 raw_key.Length != kCCKeySizeAES192 && 31 raw_key.Length != kCCKeySizeAES192 &&
31 raw_key.Length != kCCKeySizeAES256) 32 raw_key.Length != kCCKeySizeAES256)
32 return false; 33 return false;
33 if (iv.size() != kCCBlockSizeAES128) 34 if (iv.size() != kCCBlockSizeAES128)
34 return false; 35 return false;
35 36
36 key_ = key; 37 key_ = key;
37 mode_ = mode; 38 mode_ = mode;
38 iv.CopyToString(&iv_); 39 iv.CopyToString(&iv_);
39 return true; 40 return true;
40 } 41 }
41 42
42 bool Encryptor::Crypt(int /*CCOperation*/ op, 43 bool Encryptor::Crypt(int /*CCOperation*/ op,
43 const base::StringPiece& input, 44 const base::StringPiece& input,
44 std::string* output) { 45 std::string* output) {
45 DCHECK(key_); 46 output->clear();
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."
50 54
55 std::string result;
51 size_t output_size = input.size() + iv_.size(); 56 size_t output_size = input.size() + iv_.size();
57 if (output_size == 0 || output_size + 1 < input.size())
wtc 2011/11/15 02:33:58 I don't think we need to check output_size + 1 < i
58 return false;
52 CCCryptorStatus err = CCCrypt(op, 59 CCCryptorStatus err = CCCrypt(op,
53 kCCAlgorithmAES128, 60 kCCAlgorithmAES128,
54 kCCOptionPKCS7Padding, 61 kCCOptionPKCS7Padding,
55 raw_key.Data, raw_key.Length, 62 raw_key.Data, raw_key.Length,
56 iv_.data(), 63 iv_.data(),
57 input.data(), input.size(), 64 input.data(), input.size(),
58 WriteInto(output, output_size+1), 65 WriteInto(&result, output_size+1),
59 output_size, 66 output_size,
60 &output_size); 67 &output_size);
61 if (err) { 68 if (err) {
62 output->resize(0);
63 LOG(ERROR) << "CCCrypt returned " << err; 69 LOG(ERROR) << "CCCrypt returned " << err;
64 return false; 70 return false;
65 } 71 }
66 output->resize(output_size); 72 result.resize(output_size);
73 output->swap(result);
67 return true; 74 return true;
68 } 75 }
69 76
70 bool Encryptor::Encrypt(const base::StringPiece& plaintext, 77 bool Encryptor::Encrypt(const base::StringPiece& plaintext,
71 std::string* ciphertext) { 78 std::string* ciphertext) {
79 if (plaintext.empty() && mode_ != CBC)
80 return false;
72 return Crypt(kCCEncrypt, plaintext, ciphertext); 81 return Crypt(kCCEncrypt, plaintext, ciphertext);
73 } 82 }
74 83
75 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, 84 bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
76 std::string* plaintext) { 85 std::string* plaintext) {
86 if (ciphertext.empty())
wtc 2011/11/15 02:33:58 Why don't you also check mode_ != CBC here as you
Ryan Sleevi 2011/12/14 06:07:53 Empty ciphertext should always be treated as an er
87 return false;
77 return Crypt(kCCDecrypt, ciphertext, plaintext); 88 return Crypt(kCCDecrypt, ciphertext, plaintext);
78 } 89 }
79 90
80 } // namespace crypto 91 } // namespace crypto
OLDNEW
« no previous file with comments | « crypto/encryptor.cc ('k') | crypto/encryptor_nss.cc » ('j') | crypto/encryptor_nss.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698