[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 | |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 14 | #include "base/containers/span.h" |
| 15 | #include "base/optional.h" |
[email protected] | daf079a | 2013-04-17 21:42:40 | [diff] [blame] | 16 | #include "base/strings/string_piece.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 18 | #include "crypto/crypto_export.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [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 | |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 24 | // This class implements encryption without authentication, which is usually |
| 25 | // unsafe. Prefer crypto::Aead for new code. If using this class, prefer the |
| 26 | // base::span and std::vector overloads over the base::StringPiece and |
| 27 | // std::string overloads. |
[email protected] | d613a990 | 2011-08-05 20:59:11 | [diff] [blame] | 28 | class CRYPTO_EXPORT Encryptor { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 29 | public: |
| 30 | enum Mode { |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 31 | CBC, |
| 32 | CTR, |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 33 | }; |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 34 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 35 | Encryptor(); |
svaldez | 22de42fe | 2016-04-21 19:42:22 | [diff] [blame] | 36 | ~Encryptor(); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 37 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 38 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
| 39 | // key or the initialization vector cannot be used. |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 40 | // |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 41 | // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be |
| 42 | // empty. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 43 | bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv); |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 44 | bool Init(const SymmetricKey* key, Mode mode, base::span<const uint8_t> iv); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 45 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 46 | // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if |
| 47 | // the mode is CBC. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 48 | bool Encrypt(base::StringPiece plaintext, std::string* ciphertext); |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 49 | bool Encrypt(base::span<const uint8_t> plaintext, |
| 50 | std::vector<uint8_t>* ciphertext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 51 | |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 52 | // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty. |
[email protected] | ef067746 | 2012-04-25 00:27:43 | [diff] [blame] | 53 | // |
| 54 | // WARNING: In CBC mode, Decrypt() returns false if it detects the padding |
| 55 | // in the decrypted plaintext is wrong. Padding errors can result from |
| 56 | // tampered ciphertext or a wrong decryption key. But successful decryption |
| 57 | // does not imply the authenticity of the data. The caller of Decrypt() |
| 58 | // must either authenticate the ciphertext before decrypting it, or take |
| 59 | // care to not report decryption failure. Otherwise it could inadvertently |
| 60 | // be used as a padding oracle to attack the cryptosystem. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 61 | bool Decrypt(base::StringPiece ciphertext, std::string* plaintext); |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 62 | bool Decrypt(base::span<const uint8_t> ciphertext, |
| 63 | std::vector<uint8_t>* plaintext); |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 64 | |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 65 | // Sets the counter value when in CTR mode. Currently only 128-bits |
| 66 | // counter value is supported. |
| 67 | // |
| 68 | // Returns true only if update was successful. |
David Benjamin | cda45eb | 2017-11-06 18:16:52 | [diff] [blame] | 69 | bool SetCounter(base::StringPiece counter); |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 70 | bool SetCounter(base::span<const uint8_t> counter); |
[email protected] | 2377cdee | 2011-06-24 20:46:06 | [diff] [blame] | 71 | |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 72 | // TODO(albertb): Support streaming encryption. |
| 73 | |
| 74 | private: |
Chris Mumford | ea3b6c19 | 2017-06-09 18:33:13 | [diff] [blame] | 75 | const SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 76 | Mode mode_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 77 | |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 78 | bool CryptString(bool do_encrypt, |
| 79 | base::StringPiece input, |
| 80 | std::string* output); |
| 81 | bool CryptBytes(bool do_encrypt, |
| 82 | base::span<const uint8_t> input, |
| 83 | std::vector<uint8_t>* output); |
| 84 | |
| 85 | // On success, these helper functions return the number of bytes written to |
| 86 | // |output|. |
| 87 | size_t MaxOutput(bool do_encrypt, size_t length); |
| 88 | base::Optional<size_t> Crypt(bool do_encrypt, |
| 89 | base::span<const uint8_t> input, |
| 90 | base::span<uint8_t> output); |
| 91 | base::Optional<size_t> CryptCTR(bool do_encrypt, |
| 92 | base::span<const uint8_t> input, |
| 93 | base::span<uint8_t> output); |
David Benjamin | 47feaaff | 2020-06-16 22:54:46 | [diff] [blame^] | 94 | |
| 95 | // In CBC mode, the IV passed to Init(). In CTR mode, the counter value passed |
| 96 | // to SetCounter(). |
David Benjamin | 3efdcb7 | 2020-06-16 22:33:09 | [diff] [blame] | 97 | std::vector<uint8_t> iv_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 98 | }; |
| 99 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 100 | } // namespace crypto |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 101 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 102 | #endif // CRYPTO_ENCRYPTOR_H_ |