| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "crypto/encryptor.h" | 5 #include "crypto/encryptor.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "crypto/symmetric_key.h" | 9 #include "crypto/symmetric_key.h" |
| 10 | 10 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 Encryptor::Encryptor() | 31 Encryptor::Encryptor() |
| 32 : key_(NULL), | 32 : key_(NULL), |
| 33 mode_(CBC), | 33 mode_(CBC), |
| 34 block_size_(0) { | 34 block_size_(0) { |
| 35 } | 35 } |
| 36 | 36 |
| 37 Encryptor::~Encryptor() { | 37 Encryptor::~Encryptor() { |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 40 bool Encryptor::Init(SymmetricKey* key, |
| 41 Mode mode, |
| 42 const base::StringPiece& iv) { |
| 41 DCHECK(key); | 43 DCHECK(key); |
| 42 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; | 44 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; |
| 43 | 45 |
| 44 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining | 46 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining |
| 45 // mode) are properties of a key, so we have to create a copy of the key for | 47 // mode) are properties of a key, so we have to create a copy of the key for |
| 46 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. | 48 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. |
| 47 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); | 49 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); |
| 48 if (!ok) | 50 if (!ok) |
| 49 return false; | 51 return false; |
| 50 | 52 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 70 | 72 |
| 71 DWORD padding_method = PKCS5_PADDING; | 73 DWORD padding_method = PKCS5_PADDING; |
| 72 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, | 74 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, |
| 73 reinterpret_cast<BYTE*>(&padding_method), 0); | 75 reinterpret_cast<BYTE*>(&padding_method), 0); |
| 74 if (!ok) | 76 if (!ok) |
| 75 return false; | 77 return false; |
| 76 | 78 |
| 77 return true; | 79 return true; |
| 78 } | 80 } |
| 79 | 81 |
| 80 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 82 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 83 std::string* ciphertext) { |
| 81 DWORD data_len = plaintext.size(); | 84 DWORD data_len = plaintext.size(); |
| 82 DWORD total_len = data_len + block_size_; | 85 DWORD total_len = data_len + block_size_; |
| 83 | 86 |
| 84 // CryptoAPI encrypts/decrypts in place. | 87 // CryptoAPI encrypts/decrypts in place. |
| 85 std::vector<BYTE> tmp(total_len); | 88 std::vector<BYTE> tmp(total_len); |
| 86 memcpy(&tmp[0], plaintext.data(), data_len); | 89 memcpy(&tmp[0], plaintext.data(), data_len); |
| 87 | 90 |
| 88 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], | 91 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], |
| 89 &data_len, total_len); | 92 &data_len, total_len); |
| 90 if (!ok) | 93 if (!ok) |
| 91 return false; | 94 return false; |
| 92 | 95 |
| 93 ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); | 96 ciphertext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
| 94 return true; | 97 return true; |
| 95 } | 98 } |
| 96 | 99 |
| 97 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 100 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 101 std::string* plaintext) { |
| 98 DWORD data_len = ciphertext.size(); | 102 DWORD data_len = ciphertext.size(); |
| 99 if (data_len == 0) | 103 if (data_len == 0) |
| 100 return false; | 104 return false; |
| 101 | 105 |
| 102 std::vector<BYTE> tmp(data_len); | 106 std::vector<BYTE> tmp(data_len); |
| 103 memcpy(&tmp[0], ciphertext.data(), data_len); | 107 memcpy(&tmp[0], ciphertext.data(), data_len); |
| 104 | 108 |
| 105 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); | 109 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, &tmp[0], &data_len); |
| 106 if (!ok) | 110 if (!ok) |
| 107 return false; | 111 return false; |
| 108 | 112 |
| 109 DCHECK_GT(tmp.size(), data_len); | 113 DCHECK_GT(tmp.size(), data_len); |
| 110 | 114 |
| 111 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); | 115 plaintext->assign(reinterpret_cast<char*>(&tmp[0]), data_len); |
| 112 return true; | 116 return true; |
| 113 } | 117 } |
| 114 | 118 |
| 115 } // namespace crypto | 119 } // namespace crypto |
| OLD | NEW |