Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | |
| 13 #if defined(USE_NSS) | 14 #if defined(USE_NSS) |
| 14 #include "crypto/scoped_nss_types.h" | 15 #include "crypto/scoped_nss_types.h" |
| 15 #elif defined(OS_WIN) | 16 #elif defined(OS_WIN) |
| 16 #include "crypto/scoped_capi_types.h" | 17 #include "crypto/scoped_capi_types.h" |
| 17 #endif | 18 #endif |
| 18 | 19 |
| 19 namespace crypto { | 20 namespace crypto { |
| 20 | 21 |
| 21 class SymmetricKey; | 22 class SymmetricKey; |
| 22 | 23 |
| 23 class Encryptor { | 24 class Encryptor { |
| 24 public: | 25 public: |
| 25 enum Mode { | 26 enum Mode { |
| 26 CBC | 27 CBC, |
| 28 CTR, | |
| 27 }; | 29 }; |
| 30 | |
| 31 class Counter { | |
|
wtc
2011/06/14 18:11:06
Please document this class. At least mention it i
Alpha Left Google
2011/06/14 22:22:43
This is implemented to support AES-CTR-128 mode on
| |
| 32 public: | |
| 33 Counter(const std::string& counter); | |
| 34 ~Counter(); | |
| 35 | |
| 36 // Increment the counter value. | |
| 37 void Increment(); | |
| 38 | |
| 39 // Write the content of the counter to |buf|. |buf| should have enough | |
| 40 // space for |GetLengthInBytes()|. | |
| 41 void Write(void* buf); | |
| 42 | |
| 43 // Return the length of this counter. | |
| 44 const int GetLengthInBytes() const; | |
|
wtc
2011/06/14 18:11:06
GetLengthInBytes() should return size_t. The retu
Alpha Left Google
2011/06/14 22:22:43
Done.
| |
| 45 | |
| 46 private: | |
| 47 uint64 high_num_; | |
| 48 uint64 low_num_; | |
| 49 }; | |
| 50 | |
| 28 Encryptor(); | 51 Encryptor(); |
| 29 virtual ~Encryptor(); | 52 virtual ~Encryptor(); |
| 30 | 53 |
| 31 // Initializes the encryptor using |key| and |iv|. Returns false if either the | 54 // Initializes the encryptor using |key| and |iv|. Returns false if either the |
| 32 // key or the initialization vector cannot be used. | 55 // key or the initialization vector cannot be used. |
| 56 // | |
| 57 // When |mode| is CTR then |iv| should be empty. | |
| 33 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); | 58 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); |
| 34 | 59 |
| 35 // Encrypts |plaintext| into |ciphertext|. | 60 // Encrypts |plaintext| into |ciphertext|. |
| 36 bool Encrypt(const std::string& plaintext, std::string* ciphertext); | 61 bool Encrypt(const std::string& plaintext, std::string* ciphertext); |
| 37 | 62 |
| 38 // Decrypts |ciphertext| into |plaintext|. | 63 // Decrypts |ciphertext| into |plaintext|. |
| 39 bool Decrypt(const std::string& ciphertext, std::string* plaintext); | 64 bool Decrypt(const std::string& ciphertext, std::string* plaintext); |
| 40 | 65 |
| 66 // Update the counter value when in CTR mode. Currently only 128-bits | |
| 67 // counter value is supported. | |
|
wtc
2011/06/14 18:11:06
The limitation of 128-bits counter values should b
Alpha Left Google
2011/06/14 22:22:43
Done.
| |
| 68 // | |
| 69 // Return true only if update was successful. | |
| 70 bool UpdateCounter(const std::string& counter); | |
| 71 | |
| 41 // TODO(albertb): Support streaming encryption. | 72 // TODO(albertb): Support streaming encryption. |
| 42 | 73 |
| 43 private: | 74 private: |
| 75 // Generate a mask using |counter_| to be used for encryption in CTR mode. | |
| 76 // Resulting mask will be written to |mask| with |mask_len| bytes. | |
| 77 // | |
| 78 // The generated mask will always have at least |plaintext_len| bytes and | |
| 79 // will be a multiple of the counter length. | |
| 80 // | |
| 81 // This method is used only in CTR mode. | |
| 82 void GenerateCounterMask(size_t plaintext_len, | |
| 83 scoped_array<uint8>* mask, | |
| 84 size_t* mask_len); | |
| 85 | |
| 86 // Mask the |plaintext| message using |mask|. The output will be written to | |
| 87 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes. | |
| 88 void MaskMessage(const void* plaintext, | |
| 89 size_t plaintext_len, | |
| 90 const void* mask, | |
| 91 void* ciphertext) const; | |
| 92 | |
| 44 SymmetricKey* key_; | 93 SymmetricKey* key_; |
| 45 Mode mode_; | 94 Mode mode_; |
| 95 scoped_ptr<Counter> counter_; | |
| 46 | 96 |
| 47 #if defined(USE_OPENSSL) | 97 #if defined(USE_OPENSSL) |
| 48 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. | 98 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. |
| 49 const std::string& input, | 99 const std::string& input, |
| 50 std::string* output); | 100 std::string* output); |
| 51 std::string iv_; | 101 std::string iv_; |
| 52 #elif defined(USE_NSS) | 102 #elif defined(USE_NSS) |
| 103 bool Crypt(PK11Context* context, | |
| 104 const std::string& input, | |
| 105 std::string* output); | |
| 106 bool CryptCTR(PK11Context* context, | |
| 107 const std::string& input, | |
| 108 std::string* output); | |
| 53 ScopedPK11Slot slot_; | 109 ScopedPK11Slot slot_; |
| 54 ScopedSECItem param_; | 110 ScopedSECItem param_; |
| 55 #elif defined(OS_MACOSX) | 111 #elif defined(OS_MACOSX) |
| 56 bool Crypt(int /*CCOperation*/ op, | 112 bool Crypt(int /*CCOperation*/ op, |
| 57 const std::string& input, | 113 const std::string& input, |
| 58 std::string* output); | 114 std::string* output); |
| 59 | 115 |
| 60 std::string iv_; | 116 std::string iv_; |
| 61 #elif defined(OS_WIN) | 117 #elif defined(OS_WIN) |
| 62 ScopedHCRYPTKEY capi_key_; | 118 ScopedHCRYPTKEY capi_key_; |
| 63 DWORD block_size_; | 119 DWORD block_size_; |
| 64 #endif | 120 #endif |
| 65 }; | 121 }; |
| 66 | 122 |
| 67 } // namespace crypto | 123 } // namespace crypto |
| 68 | 124 |
| 69 #endif // CRYPTO_ENCRYPTOR_H_ | 125 #endif // CRYPTO_ENCRYPTOR_H_ |
| OLD | NEW |