blob: 0775e1aa93ac46bb89b0b3da293cf847637fb56d [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"
15#include "base/optional.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"
[email protected]692033a2010-04-09 18:40:5019
[email protected]4b559b4d2011-04-14 17:37:1420namespace crypto {
[email protected]39422e32010-03-25 19:13:0021
[email protected]692033a2010-04-09 18:40:5022class SymmetricKey;
23
David Benjamin3efdcb72020-06-16 22:33:0924// 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]d613a9902011-08-05 20:59:1128class CRYPTO_EXPORT Encryptor {
[email protected]39422e32010-03-25 19:13:0029 public:
30 enum Mode {
[email protected]2377cdee2011-06-24 20:46:0631 CBC,
32 CTR,
[email protected]39422e32010-03-25 19:13:0033 };
[email protected]2377cdee2011-06-24 20:46:0634
[email protected]1b47ce22010-03-31 16:18:3035 Encryptor();
svaldez22de42fe2016-04-21 19:42:2236 ~Encryptor();
[email protected]39422e32010-03-25 19:13:0037
[email protected]1b47ce22010-03-31 16:18:3038 // Initializes the encryptor using |key| and |iv|. Returns false if either the
39 // key or the initialization vector cannot be used.
[email protected]2377cdee2011-06-24 20:46:0640 //
[email protected]fdce4782011-11-29 20:06:1841 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
42 // empty.
David Benjamincda45eb2017-11-06 18:16:5243 bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv);
David Benjamin3efdcb72020-06-16 22:33:0944 bool Init(const SymmetricKey* key, Mode mode, base::span<const uint8_t> iv);
[email protected]39422e32010-03-25 19:13:0045
[email protected]fdce4782011-11-29 20:06:1846 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
47 // the mode is CBC.
David Benjamincda45eb2017-11-06 18:16:5248 bool Encrypt(base::StringPiece plaintext, std::string* ciphertext);
David Benjamin3efdcb72020-06-16 22:33:0949 bool Encrypt(base::span<const uint8_t> plaintext,
50 std::vector<uint8_t>* ciphertext);
[email protected]39422e32010-03-25 19:13:0051
[email protected]fdce4782011-11-29 20:06:1852 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty.
[email protected]ef0677462012-04-25 00:27:4353 //
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 Benjamincda45eb2017-11-06 18:16:5261 bool Decrypt(base::StringPiece ciphertext, std::string* plaintext);
David Benjamin3efdcb72020-06-16 22:33:0962 bool Decrypt(base::span<const uint8_t> ciphertext,
63 std::vector<uint8_t>* plaintext);
[email protected]39422e32010-03-25 19:13:0064
[email protected]2377cdee2011-06-24 20:46:0665 // 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 Benjamincda45eb2017-11-06 18:16:5269 bool SetCounter(base::StringPiece counter);
David Benjamin3efdcb72020-06-16 22:33:0970 bool SetCounter(base::span<const uint8_t> counter);
[email protected]2377cdee2011-06-24 20:46:0671
[email protected]39422e32010-03-25 19:13:0072 // TODO(albertb): Support streaming encryption.
73
74 private:
Chris Mumfordea3b6c192017-06-09 18:33:1375 const SymmetricKey* key_;
[email protected]39422e32010-03-25 19:13:0076 Mode mode_;
[email protected]39422e32010-03-25 19:13:0077
David Benjamin3efdcb72020-06-16 22:33:0978 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 Benjamin47feaaff2020-06-16 22:54:4694
95 // In CBC mode, the IV passed to Init(). In CTR mode, the counter value passed
96 // to SetCounter().
David Benjamin3efdcb72020-06-16 22:33:0997 std::vector<uint8_t> iv_;
[email protected]39422e32010-03-25 19:13:0098};
99
[email protected]4b559b4d2011-04-14 17:37:14100} // namespace crypto
[email protected]39422e32010-03-25 19:13:00101
[email protected]4b559b4d2011-04-14 17:37:14102#endif // CRYPTO_ENCRYPTOR_H_