blob: ff6e019be44fe2b4ba65640d612befcb504dcff3 [file] [log] [blame]
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "crypto/encryptor.h"
#include <CommonCrypto/CommonCryptor.h>
#include "base/logging.h"
#include "base/string_util.h"
#include "crypto/symmetric_key.h"
namespace crypto {
: key_(NULL),
mode_(CBC) {
Encryptor::~Encryptor() {
bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
CSSM_DATA raw_key = key->cssm_data();
if (raw_key.Length != kCCKeySizeAES128 &&
raw_key.Length != kCCKeySizeAES192 &&
raw_key.Length != kCCKeySizeAES256)
return false;
if (iv.size() != kCCBlockSizeAES128)
return false;
key_ = key;
mode_ = mode;
iv_ = iv;
return true;
bool Encryptor::Crypt(int /*CCOperation*/ op,
const std::string& input,
std::string* output) {
CSSM_DATA raw_key = key_->cssm_data();
// CommonCryptor.h: "A general rule for the size of the output buffer which
// must be provided by the caller is that for block ciphers, the output
// length is never larger than the input length plus the block size."
size_t output_size = input.size() + iv_.size();
CCCryptorStatus err = CCCrypt(op,
raw_key.Data, raw_key.Length,,, input.size(),
WriteInto(output, output_size+1),
if (err) {
LOG(ERROR) << "CCCrypt returned " << err;
return false;
return true;
bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
return Crypt(kCCEncrypt, plaintext, ciphertext);
bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
return Crypt(kCCDecrypt, ciphertext, plaintext);
} // namespace crypto