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" | 11 #include "base/basictypes.h" |
| 12 #include "base/compiler_specific.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/string_piece.h" | 14 #include "base/string_piece.h" |
14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
15 #include "crypto/crypto_export.h" | 16 #include "crypto/crypto_export.h" |
16 | 17 |
17 #if defined(USE_NSS) | 18 #if defined(USE_NSS) |
18 #include "crypto/scoped_nss_types.h" | 19 #include "crypto/scoped_nss_types.h" |
19 #elif defined(OS_WIN) | 20 #elif defined(OS_WIN) |
20 #include "crypto/scoped_capi_types.h" | 21 #include "crypto/scoped_capi_types.h" |
21 #endif | 22 #endif |
(...skipping 10 matching lines...) Expand all Loading... |
32 }; | 33 }; |
33 | 34 |
34 // This class implements a 128-bits counter to be used in AES-CTR encryption. | 35 // This class implements a 128-bits counter to be used in AES-CTR encryption. |
35 // Only 128-bits counter is supported in this class. | 36 // Only 128-bits counter is supported in this class. |
36 class Counter { | 37 class Counter { |
37 public: | 38 public: |
38 explicit Counter(const base::StringPiece& counter); | 39 explicit Counter(const base::StringPiece& counter); |
39 ~Counter(); | 40 ~Counter(); |
40 | 41 |
41 // Increment the counter value. | 42 // Increment the counter value. |
42 bool Increment(); | 43 bool Increment() WARN_UNUSED_RESULT; |
43 | 44 |
44 // Write the content of the counter to |buf|. |buf| should have enough | 45 // Write the content of the counter to |buf|. |buf| should have enough |
45 // space for |GetLengthInBytes()|. | 46 // space for |GetLengthInBytes()|. |
46 void Write(void* buf); | 47 void Write(void* buf); |
47 | 48 |
48 // Return the length of this counter. | 49 // Return the length of this counter. |
49 size_t GetLengthInBytes() const; | 50 size_t GetLengthInBytes() const; |
50 | 51 |
51 private: | 52 private: |
52 union { | 53 union { |
53 uint32 components32[4]; | 54 uint32 components32[4]; |
54 uint64 components64[2]; | 55 uint64 components64[2]; |
55 } counter_; | 56 } counter_; |
56 }; | 57 }; |
57 | 58 |
58 Encryptor(); | 59 Encryptor(); |
59 virtual ~Encryptor(); | 60 virtual ~Encryptor(); |
60 | 61 |
61 // 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 |
62 // key or the initialization vector cannot be used. | 63 // key or the initialization vector cannot be used. |
63 // | 64 // |
64 // When |mode| is CTR then |iv| should be empty. | 65 // When |mode| is CTR then |iv| should be empty. |
65 bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv); | 66 bool Init(SymmetricKey* key, Mode mode, |
| 67 const base::StringPiece& iv) WARN_UNUSED_RESULT; |
66 | 68 |
67 // Encrypts |plaintext| into |ciphertext|. | 69 // Encrypts |plaintext| into |ciphertext|. |
68 bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext); | 70 bool Encrypt(const base::StringPiece& plaintext, |
| 71 std::string* ciphertext) WARN_UNUSED_RESULT; |
69 | 72 |
70 // Decrypts |ciphertext| into |plaintext|. | 73 // Decrypts |ciphertext| into |plaintext|. |
71 bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext); | 74 bool Decrypt(const base::StringPiece& ciphertext, |
| 75 std::string* plaintext) WARN_UNUSED_RESULT; |
72 | 76 |
73 // Sets the counter value when in CTR mode. Currently only 128-bits | 77 // Sets the counter value when in CTR mode. Currently only 128-bits |
74 // counter value is supported. | 78 // counter value is supported. |
75 // | 79 // |
76 // Returns true only if update was successful. | 80 // Returns true only if update was successful. |
77 bool SetCounter(const base::StringPiece& counter); | 81 bool SetCounter(const base::StringPiece& counter) WARN_UNUSED_RESULT; |
78 | 82 |
79 // TODO(albertb): Support streaming encryption. | 83 // TODO(albertb): Support streaming encryption. |
80 | 84 |
81 private: | 85 private: |
82 // Generates a mask using |counter_| to be used for encryption in CTR mode. | 86 // Generates a mask using |counter_| to be used for encryption in CTR mode. |
83 // Resulting mask will be written to |mask| with |mask_len| bytes. | 87 // Resulting mask will be written to |mask| with |mask_len| bytes. |
84 // | 88 // |
85 // Make sure there's enough space in mask when calling this method. | 89 // Make sure there's enough space in mask when calling this method. |
86 // Reserve at least |plaintext_len| + 16 bytes for |mask|. | 90 // Reserve at least |plaintext_len| + 16 bytes for |mask|. |
87 // | 91 // |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 std::string iv_; | 132 std::string iv_; |
129 #elif defined(OS_WIN) | 133 #elif defined(OS_WIN) |
130 ScopedHCRYPTKEY capi_key_; | 134 ScopedHCRYPTKEY capi_key_; |
131 DWORD block_size_; | 135 DWORD block_size_; |
132 #endif | 136 #endif |
133 }; | 137 }; |
134 | 138 |
135 } // namespace crypto | 139 } // namespace crypto |
136 | 140 |
137 #endif // CRYPTO_ENCRYPTOR_H_ | 141 #endif // CRYPTO_ENCRYPTOR_H_ |
OLD | NEW |