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" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "crypto/openssl_util.h" | 12 #include "crypto/openssl_util.h" |
13 #include "crypto/symmetric_key.h" | 13 #include "crypto/symmetric_key.h" |
14 | 14 |
15 namespace crypto { | 15 namespace crypto { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 const EVP_CIPHER* GetCipherForKey(SymmetricKey* key) { | 19 const EVP_CIPHER* GetCipherForKey(SymmetricKey* key) { |
20 switch (key->key().length()) { | 20 switch (key->key().length()) { |
21 case 16: return EVP_aes_128_cbc(); | 21 case 16: return EVP_aes_128_cbc(); |
| 22 case 24: return EVP_aes_192_cbc(); |
22 case 32: return EVP_aes_256_cbc(); | 23 case 32: return EVP_aes_256_cbc(); |
23 default: return NULL; | 24 default: return NULL; |
24 } | 25 } |
25 } | 26 } |
26 | 27 |
27 // On destruction this class will cleanup the ctx, and also clear the OpenSSL | 28 // On destruction this class will cleanup the ctx, and also clear the OpenSSL |
28 // ERR stack as a convenience. | 29 // ERR stack as a convenience. |
29 class ScopedCipherCTX { | 30 class ScopedCipherCTX { |
30 public: | 31 public: |
31 explicit ScopedCipherCTX() { | 32 explicit ScopedCipherCTX() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 DCHECK(key_); // Must call Init() before En/De-crypt. | 93 DCHECK(key_); // Must call Init() before En/De-crypt. |
93 // Work on the result in a local variable, and then only transfer it to | 94 // Work on the result in a local variable, and then only transfer it to |
94 // |output| on success to ensure no partial data is returned. | 95 // |output| on success to ensure no partial data is returned. |
95 std::string result; | 96 std::string result; |
96 output->clear(); | 97 output->clear(); |
97 | 98 |
98 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 99 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
99 DCHECK(cipher); // Already handled in Init(); | 100 DCHECK(cipher); // Already handled in Init(); |
100 | 101 |
101 const std::string& key = key_->key(); | 102 const std::string& key = key_->key(); |
102 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), iv_.length()); | 103 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); |
103 DCHECK_EQ(EVP_CIPHER_key_length(cipher), key.length()); | 104 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length())); |
104 | 105 |
105 ScopedCipherCTX ctx; | 106 ScopedCipherCTX ctx; |
106 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 107 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
107 reinterpret_cast<const uint8*>(key.data()), | 108 reinterpret_cast<const uint8*>(key.data()), |
108 reinterpret_cast<const uint8*>(iv_.data()), | 109 reinterpret_cast<const uint8*>(iv_.data()), |
109 do_encrypt)) | 110 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); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |