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 #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 |
| 53 union { |
| 54 uint8 buf[16]; |
| 55 uint64 force_align[2]; |
| 56 } counter_; |
| 57 }; |
| 58 |
29 Encryptor(); | 59 Encryptor(); |
30 virtual ~Encryptor(); | 60 virtual ~Encryptor(); |
31 | 61 |
32 // Initializes the encryptor using |key| and |iv|. Returns false if either the | 62 // Initializes the encryptor using |key| and |iv|. Returns false if either the |
33 // key or the initialization vector cannot be used. | 63 // key or the initialization vector cannot be used. |
| 64 // |
| 65 // When |mode| is CTR then |iv| should be empty. |
34 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); | 66 bool Init(SymmetricKey* key, Mode mode, const std::string& iv); |
35 | 67 |
36 // Encrypts |plaintext| into |ciphertext|. | 68 // Encrypts |plaintext| into |ciphertext|. |
37 bool Encrypt(const std::string& plaintext, std::string* ciphertext); | 69 bool Encrypt(const std::string& plaintext, std::string* ciphertext); |
38 | 70 |
39 // Decrypts |ciphertext| into |plaintext|. | 71 // Decrypts |ciphertext| into |plaintext|. |
40 bool Decrypt(const std::string& ciphertext, std::string* plaintext); | 72 bool Decrypt(const std::string& ciphertext, std::string* plaintext); |
41 | 73 |
| 74 // Update the counter value when in CTR mode. Currently only 128-bits |
| 75 // counter value is supported. |
| 76 // |
| 77 // Return true only if update was successful. |
| 78 bool UpdateCounter(const std::string& counter); |
| 79 |
42 // TODO(albertb): Support streaming encryption. | 80 // TODO(albertb): Support streaming encryption. |
43 | 81 |
44 private: | 82 private: |
| 83 // Generate a mask using |counter_| to be used for encryption in CTR mode. |
| 84 // Resulting mask will be written to |mask| with |mask_len| bytes. |
| 85 // |
| 86 // Make sure there's enough space in mask when calling this method. |
| 87 // Reserve at least |plaintext_len| + 16 bytes for |mask|. |
| 88 // |
| 89 // The generated mask will always have at least |plaintext_len| bytes and |
| 90 // will be a multiple of the counter length. |
| 91 // |
| 92 // This method is used only in CTR mode. |
| 93 void GenerateCounterMask(size_t plaintext_len, |
| 94 uint8* mask, |
| 95 size_t* mask_len); |
| 96 |
| 97 // Mask the |plaintext| message using |mask|. The output will be written to |
| 98 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes. |
| 99 void MaskMessage(const void* plaintext, |
| 100 size_t plaintext_len, |
| 101 const void* mask, |
| 102 void* ciphertext) const; |
| 103 |
45 SymmetricKey* key_; | 104 SymmetricKey* key_; |
46 Mode mode_; | 105 Mode mode_; |
| 106 scoped_ptr<Counter> counter_; |
47 | 107 |
48 #if defined(USE_OPENSSL) | 108 #if defined(USE_OPENSSL) |
49 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. | 109 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. |
50 const std::string& input, | 110 const std::string& input, |
51 std::string* output); | 111 std::string* output); |
52 std::string iv_; | 112 std::string iv_; |
53 #elif defined(USE_NSS) | 113 #elif defined(USE_NSS) |
| 114 bool Crypt(PK11Context* context, |
| 115 const std::string& input, |
| 116 std::string* output); |
| 117 bool CryptCTR(PK11Context* context, |
| 118 const std::string& input, |
| 119 std::string* output); |
54 ScopedPK11Slot slot_; | 120 ScopedPK11Slot slot_; |
55 ScopedSECItem param_; | 121 ScopedSECItem param_; |
56 #elif defined(OS_MACOSX) | 122 #elif defined(OS_MACOSX) |
57 bool Crypt(int /*CCOperation*/ op, | 123 bool Crypt(int /*CCOperation*/ op, |
58 const std::string& input, | 124 const std::string& input, |
59 std::string* output); | 125 std::string* output); |
60 | 126 |
61 std::string iv_; | 127 std::string iv_; |
62 #elif defined(OS_WIN) | 128 #elif defined(OS_WIN) |
63 ScopedHCRYPTKEY capi_key_; | 129 ScopedHCRYPTKEY capi_key_; |
64 DWORD block_size_; | 130 DWORD block_size_; |
65 #endif | 131 #endif |
66 }; | 132 }; |
67 | 133 |
68 } // namespace crypto | 134 } // namespace crypto |
69 | 135 |
70 #endif // CRYPTO_ENCRYPTOR_H_ | 136 #endif // CRYPTO_ENCRYPTOR_H_ |
OLD | NEW |