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

Side by Side Diff: crypto/encryptor.h

Issue 7230037: Use base::StringPiece for input parameters in Encryptor, rather than std::string (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: OpenSSL fix Created 9 years, 5 months 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 | crypto/encryptor.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) 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 #ifndef CRYPTO_ENCRYPTOR_H_ 5 #ifndef CRYPTO_ENCRYPTOR_H_
6 #define CRYPTO_ENCRYPTOR_H_ 6 #define CRYPTO_ENCRYPTOR_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/scoped_ptr.h" 12 #include "base/scoped_ptr.h"
13 #include "base/string_piece.h"
13 #include "build/build_config.h" 14 #include "build/build_config.h"
14 #include "crypto/crypto_api.h" 15 #include "crypto/crypto_api.h"
15 16
16 #if defined(USE_NSS) 17 #if defined(USE_NSS)
17 #include "crypto/scoped_nss_types.h" 18 #include "crypto/scoped_nss_types.h"
18 #elif defined(OS_WIN) 19 #elif defined(OS_WIN)
19 #include "crypto/scoped_capi_types.h" 20 #include "crypto/scoped_capi_types.h"
20 #endif 21 #endif
21 22
22 namespace crypto { 23 namespace crypto {
23 24
24 class SymmetricKey; 25 class SymmetricKey;
25 26
26 class CRYPTO_API Encryptor { 27 class CRYPTO_API Encryptor {
27 public: 28 public:
28 enum Mode { 29 enum Mode {
29 CBC, 30 CBC,
30 CTR, 31 CTR,
31 }; 32 };
32 33
33 // This class implements a 128-bits counter to be used in AES-CTR encryption. 34 // This class implements a 128-bits counter to be used in AES-CTR encryption.
34 // Only 128-bits counter is supported in this class. 35 // Only 128-bits counter is supported in this class.
35 class Counter { 36 class Counter {
36 public: 37 public:
37 Counter(const std::string& counter); 38 Counter(const base::StringPiece& counter);
38 ~Counter(); 39 ~Counter();
39 40
40 // Increment the counter value. 41 // Increment the counter value.
41 bool Increment(); 42 bool Increment();
42 43
43 // Write the content of the counter to |buf|. |buf| should have enough 44 // Write the content of the counter to |buf|. |buf| should have enough
44 // space for |GetLengthInBytes()|. 45 // space for |GetLengthInBytes()|.
45 void Write(void* buf); 46 void Write(void* buf);
46 47
47 // Return the length of this counter. 48 // Return the length of this counter.
48 size_t GetLengthInBytes() const; 49 size_t GetLengthInBytes() const;
49 50
50 private: 51 private:
51 union { 52 union {
52 uint32 components32[4]; 53 uint32 components32[4];
53 uint64 components64[2]; 54 uint64 components64[2];
54 } counter_; 55 } counter_;
55 }; 56 };
56 57
57 Encryptor(); 58 Encryptor();
58 virtual ~Encryptor(); 59 virtual ~Encryptor();
59 60
60 // Initializes the encryptor using |key| and |iv|. Returns false if either the 61 // Initializes the encryptor using |key| and |iv|. Returns false if either the
61 // key or the initialization vector cannot be used. 62 // key or the initialization vector cannot be used.
62 // 63 //
63 // When |mode| is CTR then |iv| should be empty. 64 // When |mode| is CTR then |iv| should be empty.
64 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); 65 bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv);
65 66
66 // Encrypts |plaintext| into |ciphertext|. 67 // Encrypts |plaintext| into |ciphertext|.
67 bool Encrypt(const std::string& plaintext, std::string* ciphertext); 68 bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext);
68 69
69 // Decrypts |ciphertext| into |plaintext|. 70 // Decrypts |ciphertext| into |plaintext|.
70 bool Decrypt(const std::string& ciphertext, std::string* plaintext); 71 bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext);
71 72
72 // Sets the counter value when in CTR mode. Currently only 128-bits 73 // Sets the counter value when in CTR mode. Currently only 128-bits
73 // counter value is supported. 74 // counter value is supported.
74 // 75 //
75 // Returns true only if update was successful. 76 // Returns true only if update was successful.
76 bool SetCounter(const std::string& counter); 77 bool SetCounter(const base::StringPiece& counter);
77 78
78 // TODO(albertb): Support streaming encryption. 79 // TODO(albertb): Support streaming encryption.
79 80
80 private: 81 private:
81 // Generates a mask using |counter_| to be used for encryption in CTR mode. 82 // Generates a mask using |counter_| to be used for encryption in CTR mode.
82 // Resulting mask will be written to |mask| with |mask_len| bytes. 83 // Resulting mask will be written to |mask| with |mask_len| bytes.
83 // 84 //
84 // Make sure there's enough space in mask when calling this method. 85 // Make sure there's enough space in mask when calling this method.
85 // Reserve at least |plaintext_len| + 16 bytes for |mask|. 86 // Reserve at least |plaintext_len| + 16 bytes for |mask|.
86 // 87 //
(...skipping 13 matching lines...) Expand all
100 size_t plaintext_len, 101 size_t plaintext_len,
101 const void* mask, 102 const void* mask,
102 void* ciphertext) const; 103 void* ciphertext) const;
103 104
104 SymmetricKey* key_; 105 SymmetricKey* key_;
105 Mode mode_; 106 Mode mode_;
106 scoped_ptr<Counter> counter_; 107 scoped_ptr<Counter> counter_;
107 108
108 #if defined(USE_OPENSSL) 109 #if defined(USE_OPENSSL)
109 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. 110 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
110 const std::string& input, 111 const base::StringPiece& input,
111 std::string* output); 112 std::string* output);
112 std::string iv_; 113 std::string iv_;
113 #elif defined(USE_NSS) 114 #elif defined(USE_NSS)
114 bool Crypt(PK11Context* context, 115 bool Crypt(PK11Context* context,
115 const std::string& input, 116 const base::StringPiece& input,
116 std::string* output); 117 std::string* output);
117 bool CryptCTR(PK11Context* context, 118 bool CryptCTR(PK11Context* context,
118 const std::string& input, 119 const base::StringPiece& input,
119 std::string* output); 120 std::string* output);
120 ScopedPK11Slot slot_; 121 ScopedPK11Slot slot_;
121 ScopedSECItem param_; 122 ScopedSECItem param_;
122 #elif defined(OS_MACOSX) 123 #elif defined(OS_MACOSX)
123 bool Crypt(int /*CCOperation*/ op, 124 bool Crypt(int /*CCOperation*/ op,
124 const std::string& input, 125 const base::StringPiece& input,
125 std::string* output); 126 std::string* output);
126 127
127 std::string iv_; 128 std::string iv_;
128 #elif defined(OS_WIN) 129 #elif defined(OS_WIN)
129 ScopedHCRYPTKEY capi_key_; 130 ScopedHCRYPTKEY capi_key_;
130 DWORD block_size_; 131 DWORD block_size_;
131 #endif 132 #endif
132 }; 133 };
133 134
134 } // namespace crypto 135 } // namespace crypto
135 136
136 #endif // CRYPTO_ENCRYPTOR_H_ 137 #endif // CRYPTO_ENCRYPTOR_H_
OLDNEW
« no previous file with comments | « no previous file | crypto/encryptor.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698