Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: base/crypto/encryptor_openssl.cc

Issue 4963002: Refactor EnsureOpenSSLInit and openssl_util into base (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments Created 10 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/crypto/symmetric_key_openssl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « no previous file | base/crypto/symmetric_key_openssl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698