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 return false; |
77 return Crypt(true, plaintext, ciphertext); | 79 return Crypt(true, plaintext, ciphertext); |
78 } | 80 } |
79 | 81 |
80 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, | 82 bool Encryptor::Decrypt(const base::StringPiece& ciphertext, |
81 std::string* plaintext) { | 83 std::string* plaintext) { |
82 CHECK(!ciphertext.empty()); | 84 if (ciphertext.empty()) |
| 85 return false; |
83 return Crypt(false, ciphertext, plaintext); | 86 return Crypt(false, ciphertext, plaintext); |
84 } | 87 } |
85 | 88 |
86 bool Encryptor::Crypt(bool do_encrypt, | 89 bool Encryptor::Crypt(bool do_encrypt, |
87 const base::StringPiece& input, | 90 const base::StringPiece& input, |
88 std::string* output) { | 91 std::string* output) { |
89 DCHECK(key_); // Must call Init() before En/De-crypt. | 92 if (!key_) |
90 // Work on the result in a local variable, and then only transfer it to | 93 return false; // Must call Init() first. |
91 // |output| on success to ensure no partial data is returned. | |
92 std::string result; | |
93 output->clear(); | |
94 | 94 |
95 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 95 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
96 DCHECK(cipher); // Already handled in Init(); | 96 DCHECK(cipher); // Already handled in Init(); |
97 | 97 |
98 const std::string& key = key_->key(); | 98 const std::string& key = key_->key(); |
99 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); | 99 if (EVP_CIPHER_iv_length(cipher) != static_cast<int>(iv_.length()) || |
100 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length())); | 100 EVP_CIPHER_key_length(cipher) != static_cast<int>(key.length())) { |
| 101 return false; |
| 102 } |
101 | 103 |
102 ScopedCipherCTX ctx; | 104 ScopedCipherCTX ctx; |
103 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 105 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
104 reinterpret_cast<const uint8*>(key.data()), | 106 reinterpret_cast<const uint8*>(key.data()), |
105 reinterpret_cast<const uint8*>(iv_.data()), | 107 reinterpret_cast<const uint8*>(iv_.data()), |
106 do_encrypt)) | 108 do_encrypt)) { |
107 return false; | 109 return false; |
| 110 } |
108 | 111 |
109 // When encrypting, add another block size of space to allow for any padding. | 112 // 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); | 113 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0); |
111 CHECK_GT(output_size, 0u); | 114 if (output_size == 0) |
112 CHECK_GT(output_size + 1, input.size()); | 115 return false; |
113 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, | 116 |
| 117 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(output, |
114 output_size + 1)); | 118 output_size + 1)); |
115 int out_len; | 119 int out_len; |
116 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, | 120 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, |
117 reinterpret_cast<const uint8*>(input.data()), | 121 reinterpret_cast<const uint8*>(input.data()), |
118 input.length())) | 122 input.length())) { |
119 return false; | 123 return false; |
| 124 } |
120 | 125 |
121 // Write out the final block plus padding (if any) to the end of the data | 126 // Write out the final block plus padding (if any) to the end of the data |
122 // just written. | 127 // just written. |
123 int tail_len; | 128 int tail_len; |
124 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) | 129 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) |
125 return false; | 130 return false; |
126 | 131 |
127 out_len += tail_len; | 132 out_len += tail_len; |
128 DCHECK_LE(out_len, static_cast<int>(output_size)); | 133 CHECK_LE(out_len, static_cast<int>(output_size)); |
129 result.resize(out_len); | |
130 | 134 |
131 output->swap(result); | 135 output->resize(out_len); |
132 return true; | 136 return true; |
133 } | 137 } |
134 | 138 |
135 } // namespace crypto | 139 } // namespace crypto |
OLD | NEW |