diff options
author | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-14 17:37:14 +0000 |
---|---|---|
committer | rvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-14 17:37:14 +0000 |
commit | 4b559b4ddffc0b7f688019bcb80658f05e063af7 (patch) | |
tree | 0be21d8914de707f5125d2cb66733cbcf088606c /crypto/encryptor_mac.cc | |
parent | 056dd45d610de34312344445d7b078a31f4a1e20 (diff) | |
download | chromium_src-4b559b4ddffc0b7f688019bcb80658f05e063af7.zip chromium_src-4b559b4ddffc0b7f688019bcb80658f05e063af7.tar.gz chromium_src-4b559b4ddffc0b7f688019bcb80658f05e063af7.tar.bz2 |
Move crypto files out of base, to a top level directory.
src/crypto is now an independent project that contains our
cryptographic primitives (except md5 and sha1).
This removes the base dependency from nss, openssl and sqlite.
BUG=76996
TEST=none
Review URL: http://codereview.chromium.org/6805019
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81611 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'crypto/encryptor_mac.cc')
-rw-r--r-- | crypto/encryptor_mac.cc | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/crypto/encryptor_mac.cc b/crypto/encryptor_mac.cc new file mode 100644 index 0000000..ff6e019 --- /dev/null +++ b/crypto/encryptor_mac.cc @@ -0,0 +1,76 @@ +// 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 { + +Encryptor::Encryptor() + : key_(NULL), + mode_(CBC) { +} + +Encryptor::~Encryptor() { +} + +bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { + DCHECK(key); + 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) { + DCHECK(key_); + 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, + kCCAlgorithmAES128, + kCCOptionPKCS7Padding, + raw_key.Data, raw_key.Length, + iv_.data(), + input.data(), input.size(), + WriteInto(output, output_size+1), + output_size, + &output_size); + if (err) { + output->resize(0); + LOG(ERROR) << "CCCrypt returned " << err; + return false; + } + output->resize(output_size); + 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 |