blob: 8cd4cd9eff572bc9409bfdbb269235ce54eec665 [file] [log] [blame]
[email protected]ef0677462012-04-25 00:27:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]39422e32010-03-25 19:13:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4b559b4d2011-04-14 17:37:145#ifndef CRYPTO_ENCRYPTOR_H_
6#define CRYPTO_ENCRYPTOR_H_
[email protected]39422e32010-03-25 19:13:007
avidd373b8b2015-12-21 21:34:438#include <stddef.h>
9#include <stdint.h>
10
thakisd1a18472016-04-08 22:30:4111#include <memory>
[email protected]39422e32010-03-25 19:13:0012#include <string>
13
David Benjamin3efdcb72020-06-16 22:33:0914#include "base/containers/span.h"
Keishi Hattori0e45c022021-11-27 09:25:5215#include "base/memory/raw_ptr.h"
[email protected]daf079a2013-04-17 21:42:4016#include "base/strings/string_piece.h"
[email protected]692033a2010-04-09 18:40:5017#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1118#include "crypto/crypto_export.h"
Anton Bikineeva3f961db2021-05-15 17:56:1219#include "third_party/abseil-cpp/absl/types/optional.h"
[email protected]692033a2010-04-09 18:40:5020
[email protected]4b559b4d2011-04-14 17:37:1421namespace crypto {
[email protected]39422e32010-03-25 19:13:0022
[email protected]692033a2010-04-09 18:40:5023class SymmetricKey;
24
David Benjamin3efdcb72020-06-16 22:33:0925// This class implements encryption without authentication, which is usually
26// unsafe. Prefer crypto::Aead for new code. If using this class, prefer the
27// base::span and std::vector overloads over the base::StringPiece and
28// std::string overloads.
[email protected]d613a9902011-08-05 20:59:1129class CRYPTO_EXPORT Encryptor {
[email protected]39422e32010-03-25 19:13:0030 public:
31 enum Mode {
[email protected]2377cdee2011-06-24 20:46:0632 CBC,
33 CTR,
[email protected]39422e32010-03-25 19:13:0034 };
[email protected]2377cdee2011-06-24 20:46:0635
[email protected]1b47ce22010-03-31 16:18:3036 Encryptor();
svaldez22de42fe2016-04-21 19:42:2237 ~Encryptor();
[email protected]39422e32010-03-25 19:13:0038
[email protected]1b47ce22010-03-31 16:18:3039 // Initializes the encryptor using |key| and |iv|. Returns false if either the
40 // key or the initialization vector cannot be used.
[email protected]2377cdee2011-06-24 20:46:0641 //
[email protected]fdce4782011-11-29 20:06:1842 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
43 // empty.
David Benjamincda45eb2017-11-06 18:16:5244 bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv);
David Benjamin3efdcb72020-06-16 22:33:0945 bool Init(const SymmetricKey* key, Mode mode, base::span<const uint8_t> iv);
[email protected]39422e32010-03-25 19:13:0046
[email protected]fdce4782011-11-29 20:06:1847 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
48 // the mode is CBC.
David Benjamincda45eb2017-11-06 18:16:5249 bool Encrypt(base::StringPiece plaintext, std::string* ciphertext);
David Benjamin3efdcb72020-06-16 22:33:0950 bool Encrypt(base::span<const uint8_t> plaintext,
51 std::vector<uint8_t>* ciphertext);
[email protected]39422e32010-03-25 19:13:0052
[email protected]fdce4782011-11-29 20:06:1853 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty.
[email protected]ef0677462012-04-25 00:27:4354 //
55 // WARNING: In CBC mode, Decrypt() returns false if it detects the padding
56 // in the decrypted plaintext is wrong. Padding errors can result from
57 // tampered ciphertext or a wrong decryption key. But successful decryption
58 // does not imply the authenticity of the data. The caller of Decrypt()
59 // must either authenticate the ciphertext before decrypting it, or take
60 // care to not report decryption failure. Otherwise it could inadvertently
61 // be used as a padding oracle to attack the cryptosystem.
David Benjamincda45eb2017-11-06 18:16:5262 bool Decrypt(base::StringPiece ciphertext, std::string* plaintext);
David Benjamin3efdcb72020-06-16 22:33:0963 bool Decrypt(base::span<const uint8_t> ciphertext,
64 std::vector<uint8_t>* plaintext);
[email protected]39422e32010-03-25 19:13:0065
[email protected]2377cdee2011-06-24 20:46:0666 // Sets the counter value when in CTR mode. Currently only 128-bits
67 // counter value is supported.
68 //
69 // Returns true only if update was successful.
David Benjamincda45eb2017-11-06 18:16:5270 bool SetCounter(base::StringPiece counter);
David Benjamin3efdcb72020-06-16 22:33:0971 bool SetCounter(base::span<const uint8_t> counter);
[email protected]2377cdee2011-06-24 20:46:0672
[email protected]39422e32010-03-25 19:13:0073 // TODO(albertb): Support streaming encryption.
74
75 private:
Keishi Hattori0e45c022021-11-27 09:25:5276 raw_ptr<const SymmetricKey> key_;
[email protected]39422e32010-03-25 19:13:0077 Mode mode_;
[email protected]39422e32010-03-25 19:13:0078
David Benjamin3efdcb72020-06-16 22:33:0979 bool CryptString(bool do_encrypt,
80 base::StringPiece input,
81 std::string* output);
82 bool CryptBytes(bool do_encrypt,
83 base::span<const uint8_t> input,
84 std::vector<uint8_t>* output);
85
86 // On success, these helper functions return the number of bytes written to
87 // |output|.
88 size_t MaxOutput(bool do_encrypt, size_t length);
Anton Bikineeva3f961db2021-05-15 17:56:1289 absl::optional<size_t> Crypt(bool do_encrypt,
David Benjamin3efdcb72020-06-16 22:33:0990 base::span<const uint8_t> input,
91 base::span<uint8_t> output);
Anton Bikineeva3f961db2021-05-15 17:56:1292 absl::optional<size_t> CryptCTR(bool do_encrypt,
David Benjamin3efdcb72020-06-16 22:33:0993 base::span<const uint8_t> input,
94 base::span<uint8_t> output);
David Benjamin47feaaff2020-06-16 22:54:4695
96 // In CBC mode, the IV passed to Init(). In CTR mode, the counter value passed
97 // to SetCounter().
David Benjamin3efdcb72020-06-16 22:33:0998 std::vector<uint8_t> iv_;
[email protected]39422e32010-03-25 19:13:0099};
100
[email protected]4b559b4d2011-04-14 17:37:14101} // namespace crypto
[email protected]39422e32010-03-25 19:13:00102
[email protected]4b559b4d2011-04-14 17:37:14103#endif // CRYPTO_ENCRYPTOR_H_