blob: d84a3875d894486508f4b6241205e99efade3454 [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
[email protected]daf079a2013-04-17 21:42:4014#include "base/strings/string_piece.h"
[email protected]692033a2010-04-09 18:40:5015#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1116#include "crypto/crypto_export.h"
[email protected]692033a2010-04-09 18:40:5017
[email protected]4b559b4d2011-04-14 17:37:1418namespace crypto {
[email protected]39422e32010-03-25 19:13:0019
[email protected]692033a2010-04-09 18:40:5020class SymmetricKey;
21
[email protected]d613a9902011-08-05 20:59:1122class CRYPTO_EXPORT Encryptor {
[email protected]39422e32010-03-25 19:13:0023 public:
24 enum Mode {
[email protected]2377cdee2011-06-24 20:46:0625 CBC,
26 CTR,
[email protected]39422e32010-03-25 19:13:0027 };
[email protected]2377cdee2011-06-24 20:46:0628
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]45a445212012-06-15 08:11:5231 class CRYPTO_EXPORT Counter {
[email protected]2377cdee2011-06-24 20:46:0632 public:
David Benjamincda45eb2017-11-06 18:16:5233 explicit Counter(base::StringPiece counter);
[email protected]2377cdee2011-06-24 20:46:0634 ~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 {
avidd373b8b2015-12-21 21:34:4348 uint32_t components32[4];
49 uint64_t components64[2];
[email protected]2377cdee2011-06-24 20:46:0650 } counter_;
51 };
52
[email protected]1b47ce22010-03-31 16:18:3053 Encryptor();
svaldez22de42fe2016-04-21 19:42:2254 ~Encryptor();
[email protected]39422e32010-03-25 19:13:0055
[email protected]1b47ce22010-03-31 16:18:3056 // Initializes the encryptor using |key| and |iv|. Returns false if either the
57 // key or the initialization vector cannot be used.
[email protected]2377cdee2011-06-24 20:46:0658 //
[email protected]fdce4782011-11-29 20:06:1859 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
60 // empty.
David Benjamincda45eb2017-11-06 18:16:5261 bool Init(const SymmetricKey* key, Mode mode, base::StringPiece iv);
[email protected]39422e32010-03-25 19:13:0062
[email protected]fdce4782011-11-29 20:06:1863 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
64 // the mode is CBC.
David Benjamincda45eb2017-11-06 18:16:5265 bool Encrypt(base::StringPiece plaintext, std::string* ciphertext);
[email protected]39422e32010-03-25 19:13:0066
[email protected]fdce4782011-11-29 20:06:1867 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty.
[email protected]ef0677462012-04-25 00:27:4368 //
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 Benjamincda45eb2017-11-06 18:16:5276 bool Decrypt(base::StringPiece ciphertext, std::string* plaintext);
[email protected]39422e32010-03-25 19:13:0077
[email protected]2377cdee2011-06-24 20:46:0678 // 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 Benjamincda45eb2017-11-06 18:16:5282 bool SetCounter(base::StringPiece counter);
[email protected]2377cdee2011-06-24 20:46:0683
[email protected]39422e32010-03-25 19:13:0084 // TODO(albertb): Support streaming encryption.
85
86 private:
Chris Mumfordea3b6c192017-06-09 18:33:1387 const SymmetricKey* key_;
[email protected]39422e32010-03-25 19:13:0088 Mode mode_;
thakisd1a18472016-04-08 22:30:4189 std::unique_ptr<Counter> counter_;
[email protected]39422e32010-03-25 19:13:0090
[email protected]a3f742692013-06-13 19:48:0191 bool Crypt(bool do_encrypt, // Pass true to encrypt, false to decrypt.
David Benjamincda45eb2017-11-06 18:16:5292 base::StringPiece input,
[email protected]25007102010-11-12 16:29:0693 std::string* output);
David Benjamincda45eb2017-11-06 18:16:5294 bool CryptCTR(bool do_encrypt, base::StringPiece input, std::string* output);
[email protected]25007102010-11-12 16:29:0695 std::string iv_;
[email protected]39422e32010-03-25 19:13:0096};
97
[email protected]4b559b4d2011-04-14 17:37:1498} // namespace crypto
[email protected]39422e32010-03-25 19:13:0099
[email protected]4b559b4d2011-04-14 17:37:14100#endif // CRYPTO_ENCRYPTOR_H_