| 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 <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "crypto/symmetric_key.h" | 10 #include "crypto/symmetric_key.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 mode_(CBC), | 34 mode_(CBC), |
| 35 block_size_(0) { | 35 block_size_(0) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 Encryptor::~Encryptor() { | 38 Encryptor::~Encryptor() { |
| 39 } | 39 } |
| 40 | 40 |
| 41 bool Encryptor::Init(SymmetricKey* key, | 41 bool Encryptor::Init(SymmetricKey* key, |
| 42 Mode mode, | 42 Mode mode, |
| 43 const base::StringPiece& iv) { | 43 const base::StringPiece& iv) { |
| 44 DCHECK(key); | 44 DCHECK_EQ(CBC, mode); |
| 45 DCHECK_EQ(CBC, mode) << "Unsupported mode of operation"; | 45 if (!key) |
| 46 return false; |
| 46 | 47 |
| 47 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining | 48 // In CryptoAPI, the IV, padding mode, and feedback register (for a chaining |
| 48 // mode) are properties of a key, so we have to create a copy of the key for | 49 // mode) are properties of a key, so we have to create a copy of the key for |
| 49 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. | 50 // the Encryptor. See the Remarks section of the CryptEncrypt MSDN page. |
| 50 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); | 51 BOOL ok = CryptDuplicateKey(key->key(), NULL, 0, capi_key_.receive()); |
| 51 if (!ok) | 52 if (!ok) |
| 52 return false; | 53 return false; |
| 53 | 54 |
| 54 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider, | 55 // CRYPT_MODE_CBC is the default for Microsoft Base Cryptographic Provider, |
| 55 // but we set it anyway to be safe. | 56 // but we set it anyway to be safe. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 75 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, | 76 ok = CryptSetKeyParam(capi_key_.get(), KP_PADDING, |
| 76 reinterpret_cast<BYTE*>(&padding_method), 0); | 77 reinterpret_cast<BYTE*>(&padding_method), 0); |
| 77 if (!ok) | 78 if (!ok) |
| 78 return false; | 79 return false; |
| 79 | 80 |
| 80 return true; | 81 return true; |
| 81 } | 82 } |
| 82 | 83 |
| 83 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 84 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 84 std::string* ciphertext) { | 85 std::string* ciphertext) { |
| 86 std::string result; |
| 87 ciphertext->swap(result); |
| 88 if (plaintext.empty() && mode_ != CBC) |
| 89 return false; |
| 90 |
| 85 DWORD data_len = plaintext.size(); | 91 DWORD data_len = plaintext.size(); |
| 86 CHECK((data_len > 0u) || (mode_ == CBC)); | |
| 87 DWORD total_len = data_len + block_size_; | 92 DWORD total_len = data_len + block_size_; |
| 88 CHECK_GT(total_len, 0u); | 93 if (total_len == 0 || total_len < data_len || total_len + 1 < data_len) |
| 89 CHECK_GT(total_len + 1, data_len); | 94 return false; |
| 90 | 95 |
| 91 // CryptoAPI encrypts/decrypts in place. | 96 // CryptoAPI encrypts/decrypts in place. |
| 92 char* ciphertext_data = WriteInto(ciphertext, total_len + 1); | 97 char* result_data = WriteInto(&result, total_len + 1); |
| 93 memcpy(ciphertext_data, plaintext.data(), data_len); | 98 memcpy(result_data, plaintext.data(), data_len); |
| 94 | 99 |
| 95 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, | 100 BOOL ok = CryptEncrypt(capi_key_.get(), NULL, TRUE, 0, |
| 96 reinterpret_cast<BYTE*>(ciphertext_data), &data_len, | 101 reinterpret_cast<BYTE*>(result_data), &data_len, |
| 97 total_len); | 102 total_len); |
| 98 if (!ok) { | 103 if (!ok) |
| 99 ciphertext->clear(); | |
| 100 return false; | 104 return false; |
| 101 } | |
| 102 | 105 |
| 103 ciphertext->resize(data_len); | 106 result.resize(data_len); |
| 107 ciphertext->swap(result); |
| 104 return true; | 108 return true; |
| 105 } | 109 } |
| 106 | 110 |
| 107 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 111 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 108 std::string* plaintext) { | 112 std::string* plaintext) { |
| 113 std::string result; |
| 114 plaintext->swap(result); |
| 109 DWORD data_len = ciphertext.size(); | 115 DWORD data_len = ciphertext.size(); |
| 110 CHECK_GT(data_len, 0u); | 116 if (data_len == 0 || data_len + 1 < data_len) |
| 111 CHECK_GT(data_len + 1, data_len); | 117 return false; |
| 112 | 118 |
| 113 // CryptoAPI encrypts/decrypts in place. | 119 // CryptoAPI encrypts/decrypts in place. |
| 114 char* plaintext_data = WriteInto(plaintext, data_len + 1); | 120 char* result_data = WriteInto(&result, data_len + 1); |
| 115 memcpy(plaintext_data, ciphertext.data(), data_len); | 121 memcpy(result_data, ciphertext.data(), data_len); |
| 116 | 122 |
| 117 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, | 123 BOOL ok = CryptDecrypt(capi_key_.get(), NULL, TRUE, 0, |
| 118 reinterpret_cast<BYTE*>(plaintext_data), &data_len); | 124 reinterpret_cast<BYTE*>(result_data), &data_len); |
| 119 if (!ok) { | 125 if (!ok) |
| 120 plaintext->clear(); | |
| 121 return false; | 126 return false; |
| 122 } | |
| 123 | 127 |
| 124 plaintext->resize(data_len); | 128 result.resize(data_len); |
| 129 plaintext->swap(result); |
| 125 return true; | 130 return true; |
| 126 } | 131 } |
| 127 | 132 |
| 128 } // namespace crypto | 133 } // namespace crypto |
| OLD | NEW |