OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/crypto/encryptor.h" | 5 #include "base/crypto/encryptor.h" |
6 | 6 |
| 7 #include <openssl/aes.h> |
| 8 #include <openssl/evp.h> |
| 9 |
| 10 #include "base/crypto/symmetric_key.h" |
7 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/openssl_util.h" |
| 13 #include "base/string_util.h" |
8 | 14 |
9 namespace base { | 15 namespace base { |
10 | 16 |
| 17 namespace { |
| 18 |
| 19 const EVP_CIPHER* GetCipherForKey(SymmetricKey* key) { |
| 20 switch (key->key().length()) { |
| 21 case 16: return EVP_aes_128_cbc(); |
| 22 case 24: return EVP_aes_192_cbc(); |
| 23 case 32: return EVP_aes_256_cbc(); |
| 24 default: return NULL; |
| 25 } |
| 26 } |
| 27 |
| 28 // On destruction this class will cleanup the ctx, and also clear the OpenSSL |
| 29 // ERR stack as a convenience. |
| 30 class ScopedCipherCTX { |
| 31 public: |
| 32 explicit ScopedCipherCTX() { |
| 33 EVP_CIPHER_CTX_init(&ctx_); |
| 34 } |
| 35 ~ScopedCipherCTX() { |
| 36 EVP_CIPHER_CTX_cleanup(&ctx_); |
| 37 ClearOpenSSLERRStack(); |
| 38 } |
| 39 EVP_CIPHER_CTX* get() { return &ctx_; } |
| 40 |
| 41 private: |
| 42 EVP_CIPHER_CTX ctx_; |
| 43 }; |
| 44 |
| 45 } // namespace |
| 46 |
11 Encryptor::Encryptor() { | 47 Encryptor::Encryptor() { |
12 } | 48 } |
13 | 49 |
14 Encryptor::~Encryptor() { | 50 Encryptor::~Encryptor() { |
15 } | 51 } |
16 | 52 |
17 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 53 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
18 NOTIMPLEMENTED(); | 54 DCHECK(key); |
19 return false; | 55 DCHECK_EQ(CBC, mode); |
| 56 |
| 57 if (iv.size() != AES_BLOCK_SIZE) |
| 58 return false; |
| 59 |
| 60 if (GetCipherForKey(key) == NULL) |
| 61 return false; |
| 62 |
| 63 key_ = key; |
| 64 mode_ = mode; |
| 65 iv_ = iv; |
| 66 return true; |
20 } | 67 } |
21 | 68 |
22 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 69 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { |
23 NOTIMPLEMENTED(); | 70 return Crypt(true, plaintext, ciphertext); |
24 return false; | |
25 } | 71 } |
26 | 72 |
27 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 73 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { |
28 NOTIMPLEMENTED(); | 74 return Crypt(false, ciphertext, plaintext); |
29 return false; | 75 } |
| 76 |
| 77 bool Encryptor::Crypt(bool do_encrypt, |
| 78 const std::string& input, |
| 79 std::string* output) { |
| 80 // Work on the result in a local variable, and then only transfer it to |
| 81 // |output| on success to ensure no partial data is returned. |
| 82 std::string result; |
| 83 output->swap(result); |
| 84 |
| 85 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
| 86 DCHECK(cipher); // Already handled in Init(); |
| 87 |
| 88 const std::string& key = key_->key(); |
| 89 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); |
| 90 DCHECK_EQ(EVP_CIPHER_key_length(cipher), static_cast<int>(key.length())); |
| 91 |
| 92 ScopedCipherCTX ctx; |
| 93 if (!EVP_CipherInit_ex(ctx.get(), cipher, NULL, |
| 94 reinterpret_cast<const uint8*>(key.data()), |
| 95 reinterpret_cast<const uint8*>(iv_.data()), |
| 96 do_encrypt)) |
| 97 return false; |
| 98 |
| 99 // When encrypting, add another block size of space to allow for any padding. |
| 100 const size_t output_size = input.size() + (do_encrypt ? iv_.size() : 0); |
| 101 uint8* out_ptr = reinterpret_cast<uint8*>(WriteInto(&result, |
| 102 output_size + 1)); |
| 103 int out_len; |
| 104 if (!EVP_CipherUpdate(ctx.get(), out_ptr, &out_len, |
| 105 reinterpret_cast<const uint8*>(input.data()), |
| 106 input.length())) |
| 107 return false; |
| 108 |
| 109 // Write out the final block plus padding (if any) to the end of the data |
| 110 // just written. |
| 111 int tail_len; |
| 112 if (!EVP_CipherFinal_ex(ctx.get(), out_ptr + out_len, &tail_len)) |
| 113 return false; |
| 114 |
| 115 out_len += tail_len; |
| 116 DCHECK_LE(out_len, static_cast<int>(output_size)); |
| 117 result.resize(out_len); |
| 118 |
| 119 output->swap(result); |
| 120 return true; |
30 } | 121 } |
31 | 122 |
32 } // namespace base | 123 } // namespace base |
OLD | NEW |