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

Side by Side Diff: crypto/encryptor.h

Issue 7056026: Implement AES-CTR for NSS. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git try Created 9 years, 6 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
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"
12 #include "base/scoped_ptr.h"
11 #include "build/build_config.h" 13 #include "build/build_config.h"
12 #include "crypto/crypto_api.h" 14 #include "crypto/crypto_api.h"
13 15
14 #if defined(USE_NSS) 16 #if defined(USE_NSS)
15 #include "crypto/scoped_nss_types.h" 17 #include "crypto/scoped_nss_types.h"
16 #elif defined(OS_WIN) 18 #elif defined(OS_WIN)
17 #include "crypto/scoped_capi_types.h" 19 #include "crypto/scoped_capi_types.h"
18 #endif 20 #endif
19 21
20 namespace crypto { 22 namespace crypto {
21 23
22 class SymmetricKey; 24 class SymmetricKey;
23 25
24 class CRYPTO_API Encryptor { 26 class CRYPTO_API Encryptor {
25 public: 27 public:
26 enum Mode { 28 enum Mode {
27 CBC 29 CBC,
30 CTR,
28 }; 31 };
32
33 // 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 class Counter {
36 public:
37 Counter(const std::string& counter);
38 ~Counter();
39
40 // Increment the counter value.
41 void Increment();
42
43 // Write the content of the counter to |buf|. |buf| should have enough
44 // space for |GetLengthInBytes()|.
45 void Write(void* buf);
46
47 // Return the length of this counter.
48 size_t GetLengthInBytes() const;
49
50 private:
51 size_t counter_bits_;
52 uint8 counter_buf_[16];
wtc 2011/06/22 22:12:28 IMPORTANT: in the crypto/encryptor.cc you cast thi
Alpha Left Google 2011/06/22 22:22:58 Done.
53 };
54
29 Encryptor(); 55 Encryptor();
30 virtual ~Encryptor(); 56 virtual ~Encryptor();
31 57
32 // Initializes the encryptor using |key| and |iv|. Returns false if either the 58 // Initializes the encryptor using |key| and |iv|. Returns false if either the
33 // key or the initialization vector cannot be used. 59 // key or the initialization vector cannot be used.
60 //
61 // When |mode| is CTR then |iv| should be empty.
34 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); 62 bool Init(SymmetricKey* key, Mode mode, const std::string& iv);
35 63
36 // Encrypts |plaintext| into |ciphertext|. 64 // Encrypts |plaintext| into |ciphertext|.
37 bool Encrypt(const std::string& plaintext, std::string* ciphertext); 65 bool Encrypt(const std::string& plaintext, std::string* ciphertext);
38 66
39 // Decrypts |ciphertext| into |plaintext|. 67 // Decrypts |ciphertext| into |plaintext|.
40 bool Decrypt(const std::string& ciphertext, std::string* plaintext); 68 bool Decrypt(const std::string& ciphertext, std::string* plaintext);
41 69
70 // Update the counter value when in CTR mode. Currently only 128-bits
71 // counter value is supported.
72 //
73 // Return true only if update was successful.
74 bool UpdateCounter(const std::string& counter);
75
42 // TODO(albertb): Support streaming encryption. 76 // TODO(albertb): Support streaming encryption.
43 77
44 private: 78 private:
79 // Generate a mask using |counter_| to be used for encryption in CTR mode.
80 // Resulting mask will be written to |mask| with |mask_len| bytes.
81 //
82 // Make sure there's enough space in mask when calling this method.
83 // Reserve at least |plaintext_len| + 16 bytes for |mask|.
84 //
85 // The generated mask will always have at least |plaintext_len| bytes and
86 // will be a multiple of the counter length.
87 //
88 // This method is used only in CTR mode.
89 void GenerateCounterMask(size_t plaintext_len,
90 uint8* mask,
91 size_t* mask_len);
92
93 // Mask the |plaintext| message using |mask|. The output will be written to
94 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes.
95 void MaskMessage(const void* plaintext,
96 size_t plaintext_len,
97 const void* mask,
98 void* ciphertext) const;
99
45 SymmetricKey* key_; 100 SymmetricKey* key_;
46 Mode mode_; 101 Mode mode_;
102 scoped_ptr<Counter> counter_;
47 103
48 #if defined(USE_OPENSSL) 104 #if defined(USE_OPENSSL)
49 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. 105 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
50 const std::string& input, 106 const std::string& input,
51 std::string* output); 107 std::string* output);
52 std::string iv_; 108 std::string iv_;
53 #elif defined(USE_NSS) 109 #elif defined(USE_NSS)
110 bool Crypt(PK11Context* context,
111 const std::string& input,
112 std::string* output);
113 bool CryptCTR(PK11Context* context,
114 const std::string& input,
115 std::string* output);
54 ScopedPK11Slot slot_; 116 ScopedPK11Slot slot_;
55 ScopedSECItem param_; 117 ScopedSECItem param_;
56 #elif defined(OS_MACOSX) 118 #elif defined(OS_MACOSX)
57 bool Crypt(int /*CCOperation*/ op, 119 bool Crypt(int /*CCOperation*/ op,
58 const std::string& input, 120 const std::string& input,
59 std::string* output); 121 std::string* output);
60 122
61 std::string iv_; 123 std::string iv_;
62 #elif defined(OS_WIN) 124 #elif defined(OS_WIN)
63 ScopedHCRYPTKEY capi_key_; 125 ScopedHCRYPTKEY capi_key_;
64 DWORD block_size_; 126 DWORD block_size_;
65 #endif 127 #endif
66 }; 128 };
67 129
68 } // namespace crypto 130 } // namespace crypto
69 131
70 #endif // CRYPTO_ENCRYPTOR_H_ 132 #endif // CRYPTO_ENCRYPTOR_H_
OLDNEW
« no previous file with comments | « crypto/crypto.gyp ('k') | crypto/encryptor.cc » ('j') | crypto/encryptor.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698