| 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 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be | 65 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be |
| 65 // empty. | 66 // empty. |
| 66 bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv); | 67 bool Init(SymmetricKey* key, |
| 68 Mode mode, |
| 69 const base::StringPiece& iv) WARN_UNUSED_RESULT; |
| 67 | 70 |
| 68 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if | 71 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if |
| 69 // the mode is CBC. | 72 // the mode is CBC. |
| 70 bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext); | 73 bool Encrypt(const base::StringPiece& plaintext, |
| 74 std::string* ciphertext) WARN_UNUSED_RESULT; |
| 71 | 75 |
| 72 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty. | 76 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty. |
| 73 bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext); | 77 bool Decrypt(const base::StringPiece& ciphertext, |
| 78 std::string* plaintext) WARN_UNUSED_RESULT; |
| 74 | 79 |
| 75 // Sets the counter value when in CTR mode. Currently only 128-bits | 80 // Sets the counter value when in CTR mode. Currently only 128-bits |
| 76 // counter value is supported. | 81 // counter value is supported. |
| 77 // | 82 // |
| 78 // Returns true only if update was successful. | 83 // Returns true only if update was successful. |
| 79 bool SetCounter(const base::StringPiece& counter); | 84 bool SetCounter(const base::StringPiece& counter) WARN_UNUSED_RESULT; |
| 80 | 85 |
| 81 // TODO(albertb): Support streaming encryption. | 86 // TODO(albertb): Support streaming encryption. |
| 82 | 87 |
| 83 private: | 88 private: |
| 84 // Generates a mask using |counter_| to be used for encryption in CTR mode. | 89 // Generates a mask using |counter_| to be used for encryption in CTR mode. |
| 85 // Resulting mask will be written to |mask| with |mask_len| bytes. | 90 // Resulting mask will be written to |mask| with |mask_len| bytes. |
| 86 // | 91 // |
| 87 // Make sure there's enough space in mask when calling this method. | 92 // Make sure there's enough space in mask when calling this method. |
| 88 // Reserve at least |plaintext_len| + 16 bytes for |mask|. | 93 // Reserve at least |plaintext_len| + 16 bytes for |mask|. |
| 89 // | 94 // |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 std::string iv_; | 135 std::string iv_; |
| 131 #elif defined(OS_WIN) | 136 #elif defined(OS_WIN) |
| 132 ScopedHCRYPTKEY capi_key_; | 137 ScopedHCRYPTKEY capi_key_; |
| 133 DWORD block_size_; | 138 DWORD block_size_; |
| 134 #endif | 139 #endif |
| 135 }; | 140 }; |
| 136 | 141 |
| 137 } // namespace crypto | 142 } // namespace crypto |
| 138 | 143 |
| 139 #endif // CRYPTO_ENCRYPTOR_H_ | 144 #endif // CRYPTO_ENCRYPTOR_H_ |
| OLD | NEW |