| Index: base/crypto/encryptor_win.cc
|
| ===================================================================
|
| --- base/crypto/encryptor_win.cc (revision 43354)
|
| +++ base/crypto/encryptor_win.cc (working copy)
|
| @@ -4,10 +4,28 @@
|
|
|
| #include "base/crypto/encryptor.h"
|
|
|
| +#include <vector>
|
| +
|
| namespace base {
|
|
|
| -// TODO(albertb): Implement on Windows.
|
| +namespace {
|
|
|
| +// On success, returns the block size (in bytes) for the algorithm that |key|
|
| +// is for. On failure, returns 0.
|
| +DWORD GetCipherBlockSize(HCRYPTKEY key) {
|
| + DWORD block_size_in_bits = 0;
|
| + DWORD param_size = sizeof(block_size_in_bits);
|
| + BOOL ok = CryptGetKeyParam(key, KP_BLOCKLEN,
|
| + reinterpret_cast<BYTE*>(&block_size_in_bits),
|
| + ¶m_size, 0);
|
| + if (!ok)
|
| + return 0;
|
| +
|
| + return block_size_in_bits / 8;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| Encryptor::Encryptor() {
|
| }
|
|
|
| @@ -15,15 +33,68 @@
|
| }
|
|
|
| bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) {
|
| - return false;
|
| + DCHECK(key);
|
| + DCHECK_EQ(CBC, mode) << "Unsupported mode of operation";
|
| +
|
| + BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive());
|
| + if (!ok)
|
| + return false;
|
| +
|
| + block_size_ = GetCipherBlockSize(capi_key_.get());
|
| + if (block_size_ == 0)
|
| + return false;
|
| +
|
| + if (iv.size() != block_size_)
|
| + return false;
|
| +
|
| + ok = CryptSetKeyParam(capi_key_.get(), KP_IV,
|
| + const_cast<BYTE*>(
|
| + reinterpret_cast<const BYTE*>(iv.data())),
|
| + 0);
|
| + if (!ok)
|
| + return false;
|
| +
|
| + DWORD padding_type = PKCS5_PADDING;
|
| + ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING,
|
| + reinterpret_cast<BYTE*>(&padding_type), 0);
|
| + if (!ok)
|
| + return false;
|
| +
|
| + return true;
|
| }
|
|
|
| bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) {
|
| - return false;
|
| + DWORD cipher_len = plaintext.size();
|
| + DWORD total_len = cipher_len + block_size_;
|
| +
|
| + // CrypoAPI encrypts/decrypts in place
|
| + std::vector<BYTE> tmp(total_len);
|
| + memcpy(&tmp[0], plaintext.data(), cipher_len);
|
| +
|
| + BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0],
|
| + &cipher_len, total_len);
|
| + if (!ok)
|
| + return false;
|
| +
|
| + ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), cipher_len);
|
| + return true;
|
| }
|
|
|
| bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) {
|
| - return false;
|
| + DWORD plaintext_len = ciphertext.size();
|
| +
|
| + std::vector<BYTE> tmp(plaintext_len);
|
| + memcpy(&tmp[0], ciphertext.data(), plaintext_len);
|
| +
|
| + BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0],
|
| + &plaintext_len);
|
| + if (!ok)
|
| + return false;
|
| +
|
| + DCHECK_GT(tmp.size(), plaintext_len);
|
| +
|
| + plaintext->assign(reinterpret_cast<char*>(&tmp[0]), plaintext_len);
|
| + return true;
|
| }
|
|
|
| } // namespace base
|
|
|