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(); | |
23 case 32: return EVP_aes_256_cbc(); | 22 case 32: return EVP_aes_256_cbc(); |
24 default: return NULL; | 23 default: return NULL; |
25 } | 24 } |
26 } | 25 } |
27 | 26 |
28 // On destruction this class will cleanup the ctx, and also clear the OpenSSL | 27 // On destruction this class will cleanup the ctx, and also clear the OpenSSL |
29 // ERR stack as a convenience. | 28 // ERR stack as a convenience. |
30 class ScopedCipherCTX { | 29 class ScopedCipherCTX { |
31 public: | 30 public: |
32 explicit ScopedCipherCTX() { | 31 explicit ScopedCipherCTX() { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 DCHECK(key_); // Must call Init() before En/De-crypt. | 92 DCHECK(key_); // Must call Init() before En/De-crypt. |
94 // Work on the result in a local variable, and then only transfer it to | 93 // Work on the result in a local variable, and then only transfer it to |
95 // |output| on success to ensure no partial data is returned. | 94 // |output| on success to ensure no partial data is returned. |
96 std::string result; | 95 std::string result; |
97 output->clear(); | 96 output->clear(); |
98 | 97 |
99 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 98 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
100 DCHECK(cipher); // Already handled in Init(); | 99 DCHECK(cipher); // Already handled in Init(); |
101 | 100 |
102 const std::string& key = key_->key(); | 101 const std::string& key = key_->key(); |
103 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); | 102 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), iv_.length()); |
104 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length())); | 103 DCHECK_EQ(EVP_CIPHER_key_length(cipher), key.length()); |
105 | 104 |
106 ScopedCipherCTX ctx; | 105 ScopedCipherCTX ctx; |
107 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, | 106 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
108 reinterpret_cast<const uint8*>(key.data()), | 107 reinterpret_cast<const uint8*>(key.data()), |
109 reinterpret_cast<const uint8*>(iv_.data()), | 108 reinterpret_cast<const uint8*>(iv_.data()), |
110 do_encrypt)) | 109 do_encrypt)) |
111 return false; | 110 return false; |
112 | 111 |
113 // 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. |
114 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); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 167 |
169 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. | 168 // AES_ctr128_encrypt() updates |ivec|. Update the |counter_| here. |
170 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), | 169 SetCounter(base::StringPiece(reinterpret_cast<const char*>(ivec), |
171 AES_BLOCK_SIZE)); | 170 AES_BLOCK_SIZE)); |
172 | 171 |
173 output->swap(result); | 172 output->swap(result); |
174 return true; | 173 return true; |
175 } | 174 } |
176 | 175 |
177 } // namespace crypto | 176 } // namespace crypto |
OLD | NEW |