[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 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
| 10 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 11 | #include <memory> |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 12 | #include <string> |
| 13 | |
[email protected] | daf079a | 2013-04-17 21:42:40 | [diff] [blame] | 14 | #include "base/strings/string_piece.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 15 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 16 | #include "crypto/crypto_export.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 17 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 18 | namespace crypto { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 19 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 20 | class SymmetricKey; |
| 21 | |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 22 | class CRYPTO_EXPORT Encryptor { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 23 | public: |
| 24 | enum Mode { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 25 | CBC, |
| 26 | CTR, |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 27 | }; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 28 | |
| 29 | // This class implements a 128-bits counter to be used in AES-CTR encryption. |
| 30 | // Only 128-bits counter is supported in this class. |
[email protected] | 45a44521 | 2012-06-15 08:11:52 | [diff] [blame] | 31 | class CRYPTO_EXPORT Counter { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 32 | public: |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 33 | explicit Counter(base::StringPiece counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 34 | ~Counter(); |
| 35 | |
| 36 | // Increment the counter value. |
| 37 | bool Increment(); |
| 38 | |
| 39 | // Write the content of the counter to |buf|. |buf| should have enough |
| 40 | // space for |GetLengthInBytes()|. |
| 41 | void Write(void* buf); |
| 42 | |
| 43 | // Return the length of this counter. |
| 44 | size_t GetLengthInBytes() const; |
| 45 | |
| 46 | private: |
| 47 | union { |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 48 | uint32_t components32[4]; |
| 49 | uint64_t components64[2]; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 50 | } counter_; |
| 51 | }; |
| 52 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 53 | Encryptor(); |
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 54 | ~Encryptor(); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 55 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 56 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
| 57 | // key or the initialization vector cannot be used. |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 58 | // |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 59 | // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be |
| 60 | // empty. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 61 | bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 62 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 63 | // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if |
| 64 | // the mode is CBC. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 65 | bool Encrypt(base::StringPiece plaintext, std::string* ciphertext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 66 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 67 | // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty. |
[email protected] | ef067746 | 2012-04-25 00:27:43 | [diff] [blame] | 68 | // |
| 69 | // WARNING: In CBC mode, Decrypt() returns false if it detects the padding |
| 70 | // in the decrypted plaintext is wrong. Padding errors can result from |
| 71 | // tampered ciphertext or a wrong decryption key. But successful decryption |
| 72 | // does not imply the authenticity of the data. The caller of Decrypt() |
| 73 | // must either authenticate the ciphertext before decrypting it, or take |
| 74 | // care to not report decryption failure. Otherwise it could inadvertently |
| 75 | // be used as a padding oracle to attack the cryptosystem. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 76 | bool Decrypt(base::StringPiece ciphertext, std::string* plaintext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 77 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 78 | // Sets the counter value when in CTR mode. Currently only 128-bits |
| 79 | // counter value is supported. |
| 80 | // |
| 81 | // Returns true only if update was successful. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 82 | bool SetCounter(base::StringPiece counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 83 | |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 84 | // TODO(albertb): Support streaming encryption. |
| 85 | |
| 86 | private: |
Chris Mumford | ea3b6c19 | 2017-06-09 18:33:13 | [diff] [blame] | 87 | const SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 88 | Mode mode_; |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 89 | std::unique_ptr<Counter> counter_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 90 | |
[email protected] | a3f74269 | 2013-06-13 19:48:01 | [diff] [blame] | 91 | bool Crypt(bool do_encrypt, // Pass true to encrypt, false to decrypt. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 92 | base::StringPiece input, |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 93 | std::string* output); |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame^] | 94 | bool CryptCTR(bool do_encrypt, base::StringPiece input, std::string* output); |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 95 | std::string iv_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 96 | }; |
| 97 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 98 | } // namespace crypto |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 99 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 100 | #endif // CRYPTO_ENCRYPTOR_H_ |