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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 106 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
107 reinterpret_cast<const uint8*>(key.data()), | 107 reinterpret_cast<const uint8*>(key.data()), |
108 reinterpret_cast<const uint8*>(iv_.data()), | 108 reinterpret_cast<const uint8*>(iv_.data()), |
109 do_encrypt)) | 109 do_encrypt)) |
110 return false; | 110 return false; |
111 | 111 |
112 // 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. |
113 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); |
114 CHECK_GT(output_size, 0u); | 114 CHECK_GT(output_size, 0u); |
115 CHECK_GT(output_size + 1, input.size()); | 115 CHECK_GT(output_size + 1, input.size()); |
116 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, | 116 uint8* out_ptr = |
117 output_size + 1)); | 117 reinterpret_cast<uint8*>(base::WriteInto(&result, output_size + 1)); |
118 int out_len; | 118 int out_len; |
119 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, | 119 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, |
120 reinterpret_cast<const uint8*>(input.data()), | 120 reinterpret_cast<const uint8*>(input.data()), |
121 input.length())) | 121 input.length())) |
122 return false; | 122 return false; |
123 | 123 |
124 // Write out the final block plus padding (if any) to the end of the data | 124 // Write out the final block plus padding (if any) to the end of the data |
125 // just written. | 125 // just written. |
126 int tail_len; | 126 int tail_len; |
127 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) | 127 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) |
(...skipping 19 matching lines...) Expand all Loading... |
147 if (AES_set_encrypt_key(reinterpret_cast<const uint8*>(key_->key().data()), | 147 if (AES_set_encrypt_key(reinterpret_cast<const uint8*>(key_->key().data()), |
148 key_->key().size() * 8, &aes_key) != 0) { | 148 key_->key().size() * 8, &aes_key) != 0) { |
149 return false; | 149 return false; |
150 } | 150 } |
151 | 151 |
152 const size_t out_size = input.size(); | 152 const size_t out_size = input.size(); |
153 CHECK_GT(out_size, 0u); | 153 CHECK_GT(out_size, 0u); |
154 CHECK_GT(out_size + 1, input.size()); | 154 CHECK_GT(out_size + 1, input.size()); |
155 | 155 |
156 std::string result; | 156 std::string result; |
157 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, out_size + 1)); | 157 uint8* out_ptr = |
| 158 reinterpret_cast<uint8*>(base::WriteInto(&result, out_size + 1)); |
158 | 159 |
159 uint8_t ivec[AES_BLOCK_SIZE] = { 0 }; | 160 uint8_t ivec[AES_BLOCK_SIZE] = { 0 }; |
160 uint8_t ecount_buf[AES_BLOCK_SIZE] = { 0 }; | 161 uint8_t ecount_buf[AES_BLOCK_SIZE] = { 0 }; |
161 unsigned int block_offset = 0; | 162 unsigned int block_offset = 0; |
162 | 163 |
163 counter_->Write(ivec); | 164 counter_->Write(ivec); |
164 | 165 |
165 AES_ctr128_encrypt(reinterpret_cast<const uint8*>(input.data()), out_ptr, | 166 AES_ctr128_encrypt(reinterpret_cast<const uint8*>(input.data()), out_ptr, |
166 input.size(), &aes_key, ivec, ecount_buf, &block_offset); | 167 input.size(), &aes_key, ivec, ecount_buf, &block_offset); |
167 | 168 |
168 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. | 169 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. |
169 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), | 170 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), |
170 AES_BLOCK_SIZE)); | 171 AES_BLOCK_SIZE)); |
171 | 172 |
172 output->swap(result); | 173 output->swap(result); |
173 return true; | 174 return true; |
174 } | 175 } |
175 | 176 |
176 } // namespace crypto | 177 } // namespace crypto |
OLD | NEW |