Replace Callback0::Type and NewCallback() in WebSocketJobTest.
[chromium-blink-merge.git] / crypto / encryptor_mac.cc
bloba08d09ef3779ff7677203634048d80709d1f0a02
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
3 // found in the LICENSE file.
5 #include "crypto/encryptor.h"
7 #include <CommonCrypto/CommonCryptor.h>
9 #include "base/logging.h"
10 #include "base/string_util.h"
11 #include "crypto/symmetric_key.h"
13 namespace crypto {
15 Encryptor::Encryptor()
16 : key_(NULL),
17 mode_(CBC) {
20 Encryptor::~Encryptor() {
23 bool Encryptor::Init(SymmetricKey* key,
24 Mode mode,
25 const base::StringPiece& iv) {
26 DCHECK(key);
27 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
28 CSSM_DATA raw_key = key->cssm_data();
29 if (raw_key.Length != kCCKeySizeAES128 &&
30 raw_key.Length != kCCKeySizeAES192 &&
31 raw_key.Length != kCCKeySizeAES256)
32 return false;
33 if (iv.size() != kCCBlockSizeAES128)
34 return false;
36 key_ = key;
37 mode_ = mode;
38 iv.CopyToString(&iv_);
39 return true;
42 bool Encryptor::Crypt(int /*CCOperation*/ op,
43 const base::StringPiece& input,
44 std::string* output) {
45 DCHECK(key_);
46 CSSM_DATA raw_key = key_->cssm_data();
47 // 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
49 // length is never larger than the input length plus the block size."
51 size_t output_size = input.size() + iv_.size();
52 CCCryptorStatus err = CCCrypt(op,
53 kCCAlgorithmAES128,
54 kCCOptionPKCS7Padding,
55 raw_key.Data, raw_key.Length,
56 iv_.data(),
57 input.data(), input.size(),
58 WriteInto(output, output_size+1),
59 output_size,
60 &output_size);
61 if (err) {
62 output->resize(0);
63 LOG(ERROR) << "CCCrypt returned " << err;
64 return false;
66 output->resize(output_size);
67 return true;
70 bool Encryptor::Encrypt(const base::StringPiece& plaintext,
71 std::string* ciphertext) {
72 return Crypt(kCCEncrypt, plaintext, ciphertext);
75 bool Encryptor::Decrypt(const base::StringPiece& ciphertext,
76 std::string* plaintext) {
77 return Crypt(kCCDecrypt, ciphertext, plaintext);
80 } // namespace crypto