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 #include <stddef.h> |
| 10 #include <stdint.h> |
9 | 11 |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
12 #include "crypto/openssl_util.h" | 14 #include "crypto/openssl_util.h" |
13 #include "crypto/symmetric_key.h" | 15 #include "crypto/symmetric_key.h" |
14 | 16 |
15 namespace crypto { | 17 namespace crypto { |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 output->clear(); | 98 output->clear(); |
97 | 99 |
98 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 100 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
99 DCHECK(cipher); // Already handled in Init(); | 101 DCHECK(cipher); // Already handled in Init(); |
100 | 102 |
101 const std::string& key = key_->key(); | 103 const std::string& key = key_->key(); |
102 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), iv_.length()); | 104 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), iv_.length()); |
103 DCHECK_EQ(EVP_CIPHER_key_length(cipher), key.length()); | 105 DCHECK_EQ(EVP_CIPHER_key_length(cipher), key.length()); |
104 | 106 |
105 ScopedCipherCTX ctx; | 107 ScopedCipherCTX ctx; |
106 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 108 if (!EVP_CipherInit_ex( |
107 reinterpret_cast<const uint8*>(key.data()), | 109 ctx.get(), cipher, NULL, reinterpret_cast<const uint8_t*>(key.data()), |
108 reinterpret_cast<const uint8*>(iv_.data()), | 110 reinterpret_cast<const uint8_t*>(iv_.data()), do_encrypt)) |
109 do_encrypt)) | |
110 return false; | 111 return false; |
111 | 112 |
112 // When encrypting, add another block size of space to allow for any padding. | 113 // 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); | 114 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0); |
114 CHECK_GT(output_size, 0u); | 115 CHECK_GT(output_size, 0u); |
115 CHECK_GT(output_size + 1, input.size()); | 116 CHECK_GT(output_size + 1, input.size()); |
116 uint8* out_ptr = | 117 uint8_t* out_ptr = |
117 reinterpret_cast<uint8*>(base::WriteInto(&result, output_size + 1)); | 118 reinterpret_cast<uint8_t*>(base::WriteInto(&result, output_size + 1)); |
118 int out_len; | 119 int out_len; |
119 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, | 120 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, |
120 reinterpret_cast<const uint8*>(input.data()), | 121 reinterpret_cast<const uint8_t*>(input.data()), |
121 input.length())) | 122 input.length())) |
122 return false; | 123 return false; |
123 | 124 |
124 // Write out the final block plus padding (if any) to the end of the data | 125 // Write out the final block plus padding (if any) to the end of the data |
125 // just written. | 126 // just written. |
126 int tail_len; | 127 int tail_len; |
127 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) | 128 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) |
128 return false; | 129 return false; |
129 | 130 |
130 out_len += tail_len; | 131 out_len += tail_len; |
131 DCHECK_LE(out_len, static_cast<int>(output_size)); | 132 DCHECK_LE(out_len, static_cast<int>(output_size)); |
132 result.resize(out_len); | 133 result.resize(out_len); |
133 | 134 |
134 output->swap(result); | 135 output->swap(result); |
135 return true; | 136 return true; |
136 } | 137 } |
137 | 138 |
138 bool Encryptor::CryptCTR(bool do_encrypt, | 139 bool Encryptor::CryptCTR(bool do_encrypt, |
139 const base::StringPiece& input, | 140 const base::StringPiece& input, |
140 std::string* output) { | 141 std::string* output) { |
141 if (!counter_.get()) { | 142 if (!counter_.get()) { |
142 LOG(ERROR) << "Counter value not set in CTR mode."; | 143 LOG(ERROR) << "Counter value not set in CTR mode."; |
143 return false; | 144 return false; |
144 } | 145 } |
145 | 146 |
146 AES_KEY aes_key; | 147 AES_KEY aes_key; |
147 if (AES_set_encrypt_key(reinterpret_cast<const uint8*>(key_->key().data()), | 148 if (AES_set_encrypt_key(reinterpret_cast<const uint8_t*>(key_->key().data()), |
148 key_->key().size() * 8, &aes_key) != 0) { | 149 key_->key().size() * 8, &aes_key) != 0) { |
149 return false; | 150 return false; |
150 } | 151 } |
151 | 152 |
152 const size_t out_size = input.size(); | 153 const size_t out_size = input.size(); |
153 CHECK_GT(out_size, 0u); | 154 CHECK_GT(out_size, 0u); |
154 CHECK_GT(out_size + 1, input.size()); | 155 CHECK_GT(out_size + 1, input.size()); |
155 | 156 |
156 std::string result; | 157 std::string result; |
157 uint8* out_ptr = | 158 uint8_t* out_ptr = |
158 reinterpret_cast<uint8*>(base::WriteInto(&result, out_size + 1)); | 159 reinterpret_cast<uint8_t*>(base::WriteInto(&result, out_size + 1)); |
159 | 160 |
160 uint8_t ivec[AES_BLOCK_SIZE] = { 0 }; | 161 uint8_t ivec[AES_BLOCK_SIZE] = { 0 }; |
161 uint8_t ecount_buf[AES_BLOCK_SIZE] = { 0 }; | 162 uint8_t ecount_buf[AES_BLOCK_SIZE] = { 0 }; |
162 unsigned int block_offset = 0; | 163 unsigned int block_offset = 0; |
163 | 164 |
164 counter_->Write(ivec); | 165 counter_->Write(ivec); |
165 | 166 |
166 AES_ctr128_encrypt(reinterpret_cast<const uint8*>(input.data()), out_ptr, | 167 AES_ctr128_encrypt(reinterpret_cast<const uint8_t*>(input.data()), out_ptr, |
167 input.size(), &aes_key, ivec, ecount_buf, &block_offset); | 168 input.size(), &aes_key, ivec, ecount_buf, &block_offset); |
168 | 169 |
169 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. | 170 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. |
170 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), | 171 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), |
171 AES_BLOCK_SIZE)); | 172 AES_BLOCK_SIZE)); |
172 | 173 |
173 output->swap(result); | 174 output->swap(result); |
174 return true; | 175 return true; |
175 } | 176 } |
176 | 177 |
177 } // namespace crypto | 178 } // namespace crypto |
OLD | NEW |