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> | 7 #include <openssl/aes.h> |
8 #include <openssl/evp.h> | 8 #include <openssl/evp.h> |
9 | 9 |
10 #include "base/crypto/symmetric_key.h" | 10 #include "base/crypto/symmetric_key.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 ClearOpenSSLERRStack(); | 37 ClearOpenSSLERRStack(); |
38 } | 38 } |
39 EVP_CIPHER_CTX* get() { return &ctx_; } | 39 EVP_CIPHER_CTX* get() { return &ctx_; } |
40 | 40 |
41 private: | 41 private: |
42 EVP_CIPHER_CTX ctx_; | 42 EVP_CIPHER_CTX ctx_; |
43 }; | 43 }; |
44 | 44 |
45 } // namespace | 45 } // namespace |
46 | 46 |
47 Encryptor::Encryptor() { | 47 Encryptor::Encryptor() |
| 48 : key_(NULL) { |
48 } | 49 } |
49 | 50 |
50 Encryptor::~Encryptor() { | 51 Encryptor::~Encryptor() { |
51 } | 52 } |
52 | 53 |
53 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { | 54 bool Encryptor::Init(SymmetricKey* key, Mode mode, const std::string& iv) { |
54 DCHECK(key); | 55 DCHECK(key); |
55 DCHECK_EQ(CBC, mode); | 56 DCHECK_EQ(CBC, mode); |
56 | 57 |
| 58 EnsureOpenSSLInit(); |
57 if (iv.size() != AES_BLOCK_SIZE) | 59 if (iv.size() != AES_BLOCK_SIZE) |
58 return false; | 60 return false; |
59 | 61 |
60 if (GetCipherForKey(key) == NULL) | 62 if (GetCipherForKey(key) == NULL) |
61 return false; | 63 return false; |
62 | 64 |
63 key_ = key; | 65 key_ = key; |
64 mode_ = mode; | 66 mode_ = mode; |
65 iv_ = iv; | 67 iv_ = iv; |
66 return true; | 68 return true; |
67 } | 69 } |
68 | 70 |
69 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { | 71 bool Encryptor::Encrypt(const std::string& plaintext, std::string* ciphertext) { |
70 return Crypt(true, plaintext, ciphertext); | 72 return Crypt(true, plaintext, ciphertext); |
71 } | 73 } |
72 | 74 |
73 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { | 75 bool Encryptor::Decrypt(const std::string& ciphertext, std::string* plaintext) { |
74 return Crypt(false, ciphertext, plaintext); | 76 return Crypt(false, ciphertext, plaintext); |
75 } | 77 } |
76 | 78 |
77 bool Encryptor::Crypt(bool do_encrypt, | 79 bool Encryptor::Crypt(bool do_encrypt, |
78 const std::string& input, | 80 const std::string& input, |
79 std::string* output) { | 81 std::string* output) { |
| 82 DCHECK(key_); // Must call Init() before En/De-crypt. |
80 // Work on the result in a local variable, and then only transfer it to | 83 // 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. | 84 // |output| on success to ensure no partial data is returned. |
82 std::string result; | 85 std::string result; |
83 output->swap(result); | 86 output->swap(result); |
84 | 87 |
85 const EVP_CIPHER* cipher = GetCipherForKey(key_); | 88 const EVP_CIPHER* cipher = GetCipherForKey(key_); |
86 DCHECK(cipher); // Already handled in Init(); | 89 DCHECK(cipher); // Already handled in Init(); |
87 | 90 |
88 const std::string& key = key_->key(); | 91 const std::string& key = key_->key(); |
89 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); | 92 DCHECK_EQ(EVP_CIPHER_iv_length(cipher), static_cast<int>(iv_.length())); |
(...skipping 24 matching lines...) Expand all Loading... |
114 | 117 |
115 out_len += tail_len; | 118 out_len += tail_len; |
116 DCHECK_LE(out_len, static_cast<int>(output_size)); | 119 DCHECK_LE(out_len, static_cast<int>(output_size)); |
117 result.resize(out_len); | 120 result.resize(out_len); |
118 | 121 |
119 output->swap(result); | 122 output->swap(result); |
120 return true; | 123 return true; |
121 } | 124 } |
122 | 125 |
123 } // namespace base | 126 } // namespace base |
OLD | NEW |