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

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: fixed 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 uint64 high_num_;
52 uint64 low_num_;
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 // The generated mask will always have at least |plaintext_len| bytes and
83 // will be a multiple of the counter length.
84 //
85 // This method is used only in CTR mode.
86 void GenerateCounterMask(size_t plaintext_len,
87 scoped_array<uint8>* mask,
88 size_t* mask_len);
89
90 // Mask the |plaintext| message using |mask|. The output will be written to
91 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes.
92 void MaskMessage(const void* plaintext,
93 size_t plaintext_len,
94 const void* mask,
95 void* ciphertext) const;
96
45 SymmetricKey* key_; 97 SymmetricKey* key_;
46 Mode mode_; 98 Mode mode_;
99 scoped_ptr<Counter> counter_;
47 100
48 #if defined(USE_OPENSSL) 101 #if defined(USE_OPENSSL)
49 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. 102 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
50 const std::string& input, 103 const std::string& input,
51 std::string* output); 104 std::string* output);
52 std::string iv_; 105 std::string iv_;
53 #elif defined(USE_NSS) 106 #elif defined(USE_NSS)
107 bool Crypt(PK11Context* context,
108 const std::string& input,
109 std::string* output);
110 bool CryptCTR(PK11Context* context,
111 const std::string& input,
112 std::string* output);
54 ScopedPK11Slot slot_; 113 ScopedPK11Slot slot_;
55 ScopedSECItem param_; 114 ScopedSECItem param_;
56 #elif defined(OS_MACOSX) 115 #elif defined(OS_MACOSX)
57 bool Crypt(int /*CCOperation*/ op, 116 bool Crypt(int /*CCOperation*/ op,
58 const std::string& input, 117 const std::string& input,
59 std::string* output); 118 std::string* output);
60 119
61 std::string iv_; 120 std::string iv_;
62 #elif defined(OS_WIN) 121 #elif defined(OS_WIN)
63 ScopedHCRYPTKEY capi_key_; 122 ScopedHCRYPTKEY capi_key_;
64 DWORD block_size_; 123 DWORD block_size_;
65 #endif 124 #endif
66 }; 125 };
67 126
68 } // namespace crypto 127 } // namespace crypto
69 128
70 #endif // CRYPTO_ENCRYPTOR_H_ 129 #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