[email protected] | ef067746 | 2012-04-25 00:27:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_ENCRYPTOR_H_ |
| 6 | #define CRYPTO_ENCRYPTOR_H_ |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 10 | #include "base/basictypes.h" |
[email protected] | 7226b33c | 2011-08-18 08:44:22 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | daf079a | 2013-04-17 21:42:40 | [diff] [blame] | 12 | #include "base/strings/string_piece.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 13 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 14 | #include "crypto/crypto_export.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 15 | |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 16 | #if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 17 | #include "crypto/scoped_nss_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 18 | #endif |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 19 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 20 | namespace crypto { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 21 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 22 | class SymmetricKey; |
| 23 | |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 24 | class CRYPTO_EXPORT Encryptor { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 25 | public: |
| 26 | enum Mode { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 27 | CBC, |
| 28 | CTR, |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 29 | }; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 30 | |
| 31 | // This class implements a 128-bits counter to be used in AES-CTR encryption. |
| 32 | // Only 128-bits counter is supported in this class. |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 33 | class CRYPTO_EXPORT Counter { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 34 | public: |
[email protected] | 7226b33c | 2011-08-18 08:44:22 | [diff] [blame] | 35 | explicit Counter(const base::StringPiece& counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 36 | ~Counter(); |
| 37 | |
| 38 | // Increment the counter value. |
| 39 | bool Increment(); |
| 40 | |
| 41 | // Write the content of the counter to |buf|. |buf| should have enough |
| 42 | // space for |GetLengthInBytes()|. |
| 43 | void Write(void* buf); |
| 44 | |
| 45 | // Return the length of this counter. |
| 46 | size_t GetLengthInBytes() const; |
| 47 | |
| 48 | private: |
| 49 | union { |
| 50 | uint32 components32[4]; |
| 51 | uint64 components64[2]; |
| 52 | } counter_; |
| 53 | }; |
| 54 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 55 | Encryptor(); |
| 56 | virtual ~Encryptor(); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 57 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 58 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
| 59 | // key or the initialization vector cannot be used. |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 60 | // |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 61 | // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be |
| 62 | // empty. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 63 | bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 64 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 65 | // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if |
| 66 | // the mode is CBC. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 67 | bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 68 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 69 | // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty. |
[email protected] | ef067746 | 2012-04-25 00:27:43 | [diff] [blame] | 70 | // |
| 71 | // WARNING: In CBC mode, Decrypt() returns false if it detects the padding |
| 72 | // in the decrypted plaintext is wrong. Padding errors can result from |
| 73 | // tampered ciphertext or a wrong decryption key. But successful decryption |
| 74 | // does not imply the authenticity of the data. The caller of Decrypt() |
| 75 | // must either authenticate the ciphertext before decrypting it, or take |
| 76 | // care to not report decryption failure. Otherwise it could inadvertently |
| 77 | // be used as a padding oracle to attack the cryptosystem. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 78 | bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 79 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 80 | // Sets the counter value when in CTR mode. Currently only 128-bits |
| 81 | // counter value is supported. |
| 82 | // |
| 83 | // Returns true only if update was successful. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 84 | bool SetCounter(const base::StringPiece& counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 85 | |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 86 | // TODO(albertb): Support streaming encryption. |
| 87 | |
| 88 | private: |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 89 | // Generates a mask using |counter_| to be used for encryption in CTR mode. |
| 90 | // Resulting mask will be written to |mask| with |mask_len| bytes. |
| 91 | // |
| 92 | // Make sure there's enough space in mask when calling this method. |
| 93 | // Reserve at least |plaintext_len| + 16 bytes for |mask|. |
| 94 | // |
| 95 | // The generated mask will always have at least |plaintext_len| bytes and |
| 96 | // will be a multiple of the counter length. |
| 97 | // |
| 98 | // This method is used only in CTR mode. |
| 99 | // |
| 100 | // Returns false if this call failed. |
| 101 | bool GenerateCounterMask(size_t plaintext_len, |
| 102 | uint8* mask, |
| 103 | size_t* mask_len); |
| 104 | |
| 105 | // Mask the |plaintext| message using |mask|. The output will be written to |
| 106 | // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes. |
| 107 | void MaskMessage(const void* plaintext, |
| 108 | size_t plaintext_len, |
| 109 | const void* mask, |
| 110 | void* ciphertext) const; |
| 111 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 112 | SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 113 | Mode mode_; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 114 | scoped_ptr<Counter> counter_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 115 | |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 116 | #if defined(USE_OPENSSL) |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 117 | bool Crypt(bool do_encrypt, // Pass true to encrypt, false to decrypt. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 118 | const base::StringPiece& input, |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 119 | std::string* output); |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 120 | bool CryptCTR(bool do_encrypt, |
| 121 | const base::StringPiece& input, |
| 122 | std::string* output); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 123 | std::string iv_; |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 124 | #elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 125 | bool Crypt(PK11Context* context, |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 126 | const base::StringPiece& input, |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 127 | std::string* output); |
| 128 | bool CryptCTR(PK11Context* context, |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 129 | const base::StringPiece& input, |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 130 | std::string* output); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 131 | ScopedSECItem param_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 132 | #endif |
| 133 | }; |
| 134 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 135 | } // namespace crypto |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 136 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 137 | #endif // CRYPTO_ENCRYPTOR_H_ |