| 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 <openssl/aes.h> | 7 #include <openssl/aes.h> |
| 8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 : key_(NULL), | 48 : key_(NULL), |
| 49 mode_(CBC) { | 49 mode_(CBC) { |
| 50 } | 50 } |
| 51 | 51 |
| 52 Encryptor::~Encryptor() { | 52 Encryptor::~Encryptor() { |
| 53 } | 53 } |
| 54 | 54 |
| 55 bool Encryptor::Init(SymmetricKey* key, | 55 bool Encryptor::Init(SymmetricKey* key, |
| 56 Mode mode, | 56 Mode mode, |
| 57 const base::StringPiece& iv) { | 57 const base::StringPiece& iv) { |
| 58 DCHECK(key); | |
| 59 DCHECK_EQ(CBC, mode); | 58 DCHECK_EQ(CBC, mode); |
| 59 if (!key) |
| 60 return false; |
| 60 | 61 |
| 61 EnsureOpenSSLInit(); | 62 EnsureOpenSSLInit(); |
| 62 if (iv.size() != AES_BLOCK_SIZE) | 63 if (iv.size() != AES_BLOCK_SIZE) |
| 63 return false; | 64 return false; |
| 64 | 65 |
| 65 if (GetCipherForKey(key) == NULL) | 66 if (GetCipherForKey(key) == NULL) |
| 66 return false; | 67 return false; |
| 67 | 68 |
| 68 key_ = key; | 69 key_ = key; |
| 69 mode_ = mode; | 70 mode_ = mode; |
| 70 iv.CopyToString(&iv_); | 71 iv.CopyToString(&iv_); |
| 71 return true; | 72 return true; |
| 72 } | 73 } |
| 73 | 74 |
| 74 bool Encryptor::Encrypt(const base::StringPiece& plaintext, | 75 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 75 std::string* ciphertext) { | 76 std::string* ciphertext) { |
| 76 CHECK(!plaintext.empty() || (mode_ == CBC)); | 77 if (plaintext.empty() && mode_ != CBC) { |
| 78 ciphertext->clear(); |
| 79 return false; |
| 80 } |
| 77 return Crypt(true, plaintext, ciphertext); | 81 return Crypt(true, plaintext, ciphertext); |
| 78 } | 82 } |
| 79 | 83 |
| 80 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 84 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 81 std::string* plaintext) { | 85 std::string* plaintext) { |
| 82 CHECK(!ciphertext.empty()); | 86 if (ciphertext.empty()) { |
| 87 plaintext->clear(); |
| 88 return false; |
| 89 } |
| 83 return Crypt(false, ciphertext, plaintext); | 90 return Crypt(false, ciphertext, plaintext); |
| 84 } | 91 } |
| 85 | 92 |
| 86 bool Encryptor::Crypt(bool do_encrypt, | 93 bool Encryptor::Crypt(bool do_encrypt, |
| 87 const base::StringPiece& input, | 94 const base::StringPiece& input, |
| 88 std::string* output) { | 95 std::string* output) { |
| 89 DCHECK(key_); // Must call Init() before En/De-crypt. | |
| 90 // Work on the result in a local variable, and then only transfer it to | |
| 91 // |output| on success to ensure no partial data is returned. | |
| 92 std::string result; | 96 std::string result; |
| 93 output->clear(); | 97 output->swap(result); |
| 98 if (!key_) |
| 99 return false; // Must call Init() first. |
| 94 | 100 |
| 95 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 101 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
| 96 DCHECK(cipher); // Already handled in Init(); | 102 DCHECK(cipher); // Already handled in Init(); |
| 97 | 103 |
| 98 const std::string& key = key_->key(); | 104 const std::string& key = key_->key(); |
| 99 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); | 105 if (EVP_CIPHER_iv_length(cipher) != static_cast<int>(iv_.length()) || |
| 100 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length())); | 106 EVP_CIPHER_key_length(cipher) != static_cast<int>(key.length())) { |
| 107 return false; |
| 108 } |
| 101 | 109 |
| 102 ScopedCipherCTX ctx; | 110 ScopedCipherCTX ctx; |
| 103 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 111 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
| 104 reinterpret_cast<const uint8*>(key.data()), | 112 reinterpret_cast<const uint8*>(key.data()), |
| 105 reinterpret_cast<const uint8*>(iv_.data()), | 113 reinterpret_cast<const uint8*>(iv_.data()), |
| 106 do_encrypt)) | 114 do_encrypt)) { |
| 107 return false; | 115 return false; |
| 116 } |
| 108 | 117 |
| 109 // When encrypting, add another block size of space to allow for any padding. | 118 // When encrypting, add another block size of space to allow for any padding. |
| 110 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0); | 119 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0); |
| 111 CHECK_GT(output_size, 0u); | 120 if (output_size == 0 || output_size < input.size() || |
| 112 CHECK_GT(output_size + 1, input.size()); | 121 output_size + 1 < input.size()) { |
| 122 return false; |
| 123 } |
| 124 |
| 113 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, | 125 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, |
| 114 output_size + 1)); | 126 output_size + 1)); |
| 115 int out_len; | 127 int out_len; |
| 116 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, | 128 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, |
| 117 reinterpret_cast<const uint8*>(input.data()), | 129 reinterpret_cast<const uint8*>(input.data()), |
| 118 input.length())) | 130 input.length())) { |
| 119 return false; | 131 return false; |
| 132 } |
| 120 | 133 |
| 121 // Write out the final block plus padding (if any) to the end of the data | 134 // Write out the final block plus padding (if any) to the end of the data |
| 122 // just written. | 135 // just written. |
| 123 int tail_len; | 136 int tail_len; |
| 124 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) | 137 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) |
| 125 return false; | 138 return false; |
| 126 | 139 |
| 127 out_len += tail_len; | 140 out_len += tail_len; |
| 128 DCHECK_LE(out_len, static_cast<int>(output_size)); | 141 CHECK_LE(out_len, static_cast<int>(output_size)); |
| 142 |
| 129 result.resize(out_len); | 143 result.resize(out_len); |
| 130 | |
| 131 output->swap(result); | 144 output->swap(result); |
| 132 return true; | 145 return true; |
| 133 } | 146 } |
| 134 | 147 |
| 135 } // namespace crypto | 148 } // namespace crypto |
| OLD | NEW |