blob: c94a730914d216be887883509b92e6a5c20c2a5d [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:
[email protected]7226b33c2011-08-18 08:44:2233 explicit Counter(const 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.
[email protected]44a016a82011-07-08 02:53:0961 bool Init(SymmetricKey* key, Mode mode, const 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.
[email protected]44a016a82011-07-08 02:53:0965 bool Encrypt(const 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.
[email protected]44a016a82011-07-08 02:53:0976 bool Decrypt(const 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.
[email protected]44a016a82011-07-08 02:53:0982 bool SetCounter(const 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:
[email protected]1b47ce22010-03-31 16:18:3087 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.
[email protected]44a016a82011-07-08 02:53:0992 const base::StringPiece& input,
[email protected]25007102010-11-12 16:29:0693 std::string* output);
[email protected]a3f742692013-06-13 19:48:0194 bool CryptCTR(bool do_encrypt,
95 const base::StringPiece& input,
96 std::string* output);
[email protected]25007102010-11-12 16:29:0697 std::string 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_