OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" | 5 #include "base/crypto/encryptor.h" |
6 | 6 |
| 7 #include <vector> |
| 8 |
7 namespace base { | 9 namespace base { |
8 | 10 |
9 // TODO(albertb): Implement on Windows. | 11 namespace { |
| 12 |
| 13 // On success, returns the block size (in bytes) for the algorithm that |key| |
| 14 // is for. On failure, returns 0. |
| 15 DWORD GetCipherBlockSize(HCRYPTKEY key) { |
| 16 DWORD block_size_in_bits = 0; |
| 17 DWORD param_size = sizeof(block_size_in_bits); |
| 18 BOOL ok = CryptGetKeyParam(key, KP_BLOCKLEN, |
| 19 reinterpret_cast<BYTE*>(&block_size_in_bits), |
| 20 ¶m_size, 0); |
| 21 if (!ok) |
| 22 return 0; |
| 23 |
| 24 return block_size_in_bits / 8; |
| 25 } |
| 26 |
| 27 } // namespace |
10 | 28 |
11 Encryptor::Encryptor() { | 29 Encryptor::Encryptor() { |
12 } | 30 } |
13 | 31 |
14 Encryptor::~Encryptor() { | 32 Encryptor::~Encryptor() { |
15 } | 33 } |
16 | 34 |
17 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 35 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
18 return false; | 36 DCHECK(key); |
| 37 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; |
| 38 |
| 39 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); |
| 40 if (!ok) |
| 41 return false; |
| 42 |
| 43 block_size_ = GetCipherBlockSize(capi_key_.get()); |
| 44 if (block_size_ == 0) |
| 45 return false; |
| 46 |
| 47 if (iv.size() != block_size_) |
| 48 return false; |
| 49 |
| 50 ok = CryptSetKeyParam(capi_key_.get(), KP_IV, |
| 51 const_cast<BYTE*>( |
| 52 reinterpret_cast<const BYTE*>(iv.data())), |
| 53 0); |
| 54 if (!ok) |
| 55 return false; |
| 56 |
| 57 DWORD padding_type = PKCS5_PADDING; |
| 58 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, |
| 59 reinterpret_cast<BYTE*>(&padding_type), 0); |
| 60 if (!ok) |
| 61 return false; |
| 62 |
| 63 return true; |
19 } | 64 } |
20 | 65 |
21 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 66 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { |
22 return false; | 67 DWORD cipher_len = plaintext.size(); |
| 68 DWORD total_len = cipher_len + block_size_; |
| 69 |
| 70 // CrypoAPI encrypts/decrypts in place |
| 71 std::vector<BYTE> tmp(total_len); |
| 72 memcpy(&tmp[0], plaintext.data(), cipher_len); |
| 73 |
| 74 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], |
| 75 &cipher_len, total_len); |
| 76 if (!ok) |
| 77 return false; |
| 78 |
| 79 ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), cipher_len); |
| 80 return true; |
23 } | 81 } |
24 | 82 |
25 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 83 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { |
26 return false; | 84 DWORD plaintext_len = ciphertext.size(); |
| 85 |
| 86 std::vector<BYTE> tmp(plaintext_len); |
| 87 memcpy(&tmp[0], ciphertext.data(), plaintext_len); |
| 88 |
| 89 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], |
| 90 &plaintext_len); |
| 91 if (!ok) |
| 92 return false; |
| 93 |
| 94 DCHECK_GT(tmp.size(), plaintext_len); |
| 95 |
| 96 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), plaintext_len); |
| 97 return true; |
27 } | 98 } |
28 | 99 |
29 } // namespace base | 100 } // namespace base |
OLD | NEW |