OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <stddef.h> | 9 #include <stddef.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 bool Encryptor::SetCounter(const base::StringPiece& counter) { | 128 bool Encryptor::SetCounter(const base::StringPiece& counter) { |
129 if (mode_ != CTR) | 129 if (mode_ != CTR) |
130 return false; | 130 return false; |
131 if (counter.length() != 16u) | 131 if (counter.length() != 16u) |
132 return false; | 132 return false; |
133 | 133 |
134 counter_.reset(new Counter(counter)); | 134 counter_.reset(new Counter(counter)); |
135 return true; | 135 return true; |
136 } | 136 } |
137 | 137 |
138 bool Encryptor::GenerateCounterMask(size_t plaintext_len, | |
139 uint8_t* mask, | |
140 size_t* mask_len) { | |
141 DCHECK_EQ(CTR, mode_); | |
142 CHECK(mask); | |
143 CHECK(mask_len); | |
144 | |
145 const size_t kBlockLength = counter_->GetLengthInBytes(); | |
146 size_t blocks = (plaintext_len + kBlockLength - 1) / kBlockLength; | |
147 CHECK(blocks); | |
148 | |
149 *mask_len = blocks * kBlockLength; | |
150 | |
151 for (size_t i = 0; i < blocks; ++i) { | |
152 counter_->Write(mask); | |
153 mask += kBlockLength; | |
154 | |
155 bool ret = counter_->Increment(); | |
156 if (!ret) | |
157 return false; | |
158 } | |
159 return true; | |
160 } | |
161 | |
162 void Encryptor::MaskMessage(const void* plaintext, | |
163 size_t plaintext_len, | |
164 const void* mask, | |
165 void* ciphertext) const { | |
166 DCHECK_EQ(CTR, mode_); | |
167 const uint8_t* plaintext_ptr = reinterpret_cast<const uint8_t*>(plaintext); | |
168 const uint8_t* mask_ptr = reinterpret_cast<const uint8_t*>(mask); | |
169 uint8_t* ciphertext_ptr = reinterpret_cast<uint8_t*>(ciphertext); | |
170 | |
171 for (size_t i = 0; i < plaintext_len; ++i) | |
172 ciphertext_ptr[i] = plaintext_ptr[i] ^ mask_ptr[i]; | |
173 } | |
174 | |
175 bool Encryptor::Crypt(bool do_encrypt, | 138 bool Encryptor::Crypt(bool do_encrypt, |
176 const base::StringPiece& input, | 139 const base::StringPiece& input, |
177 std::string* output) { | 140 std::string* output) { |
178 DCHECK(key_); // Must call Init() before En/De-crypt. | 141 DCHECK(key_); // Must call Init() before En/De-crypt. |
179 // Work on the result in a local variable, and then only transfer it to | 142 // Work on the result in a local variable, and then only transfer it to |
180 // |output| on success to ensure no partial data is returned. | 143 // |output| on success to ensure no partial data is returned. |
181 std::string result; | 144 std::string result; |
182 output->clear(); | 145 output->clear(); |
183 | 146 |
184 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 147 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 | 217 |
255 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. | 218 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. |
256 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), | 219 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), |
257 AES_BLOCK_SIZE)); | 220 AES_BLOCK_SIZE)); |
258 | 221 |
259 output->swap(result); | 222 output->swap(result); |
260 return true; | 223 return true; |
261 } | 224 } |
262 | 225 |
263 } // namespace crypto | 226 } // namespace crypto |
OLD | NEW |