[email protected] | 9493ee95c | 2011-03-28 23:48:44 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 8 | |
| 9 | #include <string> |
| 10 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 11 | #include "base/basictypes.h" |
| 12 | #include "base/scoped_ptr.h" |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 13 | #include "base/string_piece.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame^] | 15 | #include "crypto/crypto_export.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 16 | |
| 17 | #if defined(USE_NSS) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 18 | #include "crypto/scoped_nss_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 19 | #elif defined(OS_WIN) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 20 | #include "crypto/scoped_capi_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 21 | #endif |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 22 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 23 | namespace crypto { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 24 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 25 | class SymmetricKey; |
| 26 | |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame^] | 27 | class CRYPTO_EXPORT Encryptor { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 28 | public: |
| 29 | enum Mode { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 30 | CBC, |
| 31 | CTR, |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 32 | }; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 33 | |
| 34 | // 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 | class Counter { |
| 37 | public: |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 38 | Counter(const base::StringPiece& counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 39 | ~Counter(); |
| 40 | |
| 41 | // Increment the counter value. |
| 42 | bool Increment(); |
| 43 | |
| 44 | // Write the content of the counter to |buf|. |buf| should have enough |
| 45 | // space for |GetLengthInBytes()|. |
| 46 | void Write(void* buf); |
| 47 | |
| 48 | // Return the length of this counter. |
| 49 | size_t GetLengthInBytes() const; |
| 50 | |
| 51 | private: |
| 52 | union { |
| 53 | uint32 components32[4]; |
| 54 | uint64 components64[2]; |
| 55 | } counter_; |
| 56 | }; |
| 57 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 58 | Encryptor(); |
| 59 | virtual ~Encryptor(); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 60 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 61 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
| 62 | // key or the initialization vector cannot be used. |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 63 | // |
| 64 | // When |mode| is CTR then |iv| should be empty. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 65 | bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 66 | |
| 67 | // Encrypts |plaintext| into |ciphertext|. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 68 | bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 69 | |
| 70 | // Decrypts |ciphertext| into |plaintext|. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 71 | bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 72 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 73 | // Sets the counter value when in CTR mode. Currently only 128-bits |
| 74 | // counter value is supported. |
| 75 | // |
| 76 | // Returns true only if update was successful. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 77 | bool SetCounter(const base::StringPiece& counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 78 | |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 79 | // TODO(albertb): Support streaming encryption. |
| 80 | |
| 81 | private: |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 82 | // 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. |
| 84 | // |
| 85 | // Make sure there's enough space in mask when calling this method. |
| 86 | // Reserve at least |plaintext_len| + 16 bytes for |mask|. |
| 87 | // |
| 88 | // The generated mask will always have at least |plaintext_len| bytes and |
| 89 | // will be a multiple of the counter length. |
| 90 | // |
| 91 | // This method is used only in CTR mode. |
| 92 | // |
| 93 | // Returns false if this call failed. |
| 94 | bool GenerateCounterMask(size_t plaintext_len, |
| 95 | uint8* mask, |
| 96 | size_t* mask_len); |
| 97 | |
| 98 | // Mask the |plaintext| message using |mask|. The output will be written to |
| 99 | // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes. |
| 100 | void MaskMessage(const void* plaintext, |
| 101 | size_t plaintext_len, |
| 102 | const void* mask, |
| 103 | void* ciphertext) const; |
| 104 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 105 | SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 106 | Mode mode_; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 107 | scoped_ptr<Counter> counter_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 108 | |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 109 | #if defined(USE_OPENSSL) |
| 110 | bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt. |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 111 | const base::StringPiece& input, |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 112 | std::string* output); |
| 113 | std::string iv_; |
| 114 | #elif defined(USE_NSS) |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 115 | bool Crypt(PK11Context* context, |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 116 | const base::StringPiece& input, |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 117 | std::string* output); |
| 118 | bool CryptCTR(PK11Context* context, |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 119 | const base::StringPiece& input, |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 120 | std::string* output); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 121 | ScopedPK11Slot slot_; |
| 122 | ScopedSECItem param_; |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 123 | #elif defined(OS_MACOSX) |
| 124 | bool Crypt(int /*CCOperation*/ op, |
[email protected] | 44a016a8 | 2011-07-08 02:53:09 | [diff] [blame] | 125 | const base::StringPiece& input, |
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 126 | std::string* output); |
| 127 | |
| 128 | std::string iv_; |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 129 | #elif defined(OS_WIN) |
| 130 | ScopedHCRYPTKEY capi_key_; |
| 131 | DWORD block_size_; |
[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_ |