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