| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 } // namespace | 45 } // namespace |
| 46 | 46 |
| 47 Encryptor::Encryptor() | 47 Encryptor::Encryptor() |
| 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, Mode mode, const std::string& iv) { | 55 bool Encryptor::Init(SymmetricKey* key, |
| 56 Mode mode, |
| 57 const base::StringPiece& iv) { |
| 56 DCHECK(key); | 58 DCHECK(key); |
| 57 DCHECK_EQ(CBC, mode); | 59 DCHECK_EQ(CBC, mode); |
| 58 | 60 |
| 59 EnsureOpenSSLInit(); | 61 EnsureOpenSSLInit(); |
| 60 if (iv.size() != AES_BLOCK_SIZE) | 62 if (iv.size() != AES_BLOCK_SIZE) |
| 61 return false; | 63 return false; |
| 62 | 64 |
| 63 if (GetCipherForKey(key) == NULL) | 65 if (GetCipherForKey(key) == NULL) |
| 64 return false; | 66 return false; |
| 65 | 67 |
| 66 key_ = key; | 68 key_ = key; |
| 67 mode_ = mode; | 69 mode_ = mode; |
| 68 iv_ = iv; | 70 iv_ = iv; |
| 69 return true; | 71 return true; |
| 70 } | 72 } |
| 71 | 73 |
| 72 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 74 bool Encryptor::Encrypt(const base::StringPiece& plaintext, |
| 75 std::string* ciphertext) { |
| 73 return Crypt(true, plaintext, ciphertext); | 76 return Crypt(true, plaintext, ciphertext); |
| 74 } | 77 } |
| 75 | 78 |
| 76 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 79 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
| 80 std::string* plaintext) { |
| 77 return Crypt(false, ciphertext, plaintext); | 81 return Crypt(false, ciphertext, plaintext); |
| 78 } | 82 } |
| 79 | 83 |
| 80 bool Encryptor::Crypt(bool do_encrypt, | 84 bool Encryptor::Crypt(bool do_encrypt, |
| 81 const std::string& input, | 85 const base::StringPiece& input, |
| 82 std::string* output) { | 86 std::string* output) { |
| 83 DCHECK(key_); // Must call Init() before En/De-crypt. | 87 DCHECK(key_); // Must call Init() before En/De-crypt. |
| 84 // Work on the result in a local variable, and then only transfer it to | 88 // Work on the result in a local variable, and then only transfer it to |
| 85 // |output| on success to ensure no partial data is returned. | 89 // |output| on success to ensure no partial data is returned. |
| 86 std::string result; | 90 std::string result; |
| 87 output->swap(result); | 91 output->swap(result); |
| 88 | 92 |
| 89 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 93 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
| 90 DCHECK(cipher); // Already handled in Init(); | 94 DCHECK(cipher); // Already handled in Init(); |
| 91 | 95 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 118 | 122 |
| 119 out_len += tail_len; | 123 out_len += tail_len; |
| 120 DCHECK_LE(out_len, static_cast<int>(output_size)); | 124 DCHECK_LE(out_len, static_cast<int>(output_size)); |
| 121 result.resize(out_len); | 125 result.resize(out_len); |
| 122 | 126 |
| 123 output->swap(result); | 127 output->swap(result); |
| 124 return true; | 128 return true; |
| 125 } | 129 } |
| 126 | 130 |
| 127 } // namespace crypto | 131 } // namespace crypto |
| OLD | NEW |