blob: bcc647bbc07075e266615ce025e56033b3eac576 [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
8#include <string>
9
[email protected]2377cdee2011-06-24 20:46:0610#include "base/basictypes.h"
[email protected]7226b33c2011-08-18 08:44:2211#include "base/memory/scoped_ptr.h"
[email protected]daf079a2013-04-17 21:42:4012#include "base/strings/string_piece.h"
[email protected]692033a2010-04-09 18:40:5013#include "build/build_config.h"
[email protected]d613a9902011-08-05 20:59:1114#include "crypto/crypto_export.h"
[email protected]692033a2010-04-09 18:40:5015
[email protected]45a445212012-06-15 08:11:5216#if defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
[email protected]4b559b4d2011-04-14 17:37:1417#include "crypto/scoped_nss_types.h"
[email protected]692033a2010-04-09 18:40:5018#endif
[email protected]39422e32010-03-25 19:13:0019
[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
[email protected]d613a9902011-08-05 20:59:1124class CRYPTO_EXPORT Encryptor {
[email protected]39422e32010-03-25 19:13:0025 public:
26 enum Mode {
[email protected]2377cdee2011-06-24 20:46:0627 CBC,
28 CTR,
[email protected]39422e32010-03-25 19:13:0029 };
[email protected]2377cdee2011-06-24 20:46:0630
31 // This class implements a 128-bits counter to be used in AES-CTR encryption.
32 // Only 128-bits counter is supported in this class.
[email protected]45a445212012-06-15 08:11:5233 class CRYPTO_EXPORT Counter {
[email protected]2377cdee2011-06-24 20:46:0634 public:
[email protected]7226b33c2011-08-18 08:44:2235 explicit Counter(const base::StringPiece& counter);
[email protected]2377cdee2011-06-24 20:46:0636 ~Counter();
37
38 // Increment the counter value.
39 bool Increment();
40
41 // Write the content of the counter to |buf|. |buf| should have enough
42 // space for |GetLengthInBytes()|.
43 void Write(void* buf);
44
45 // Return the length of this counter.
46 size_t GetLengthInBytes() const;
47
48 private:
49 union {
50 uint32 components32[4];
51 uint64 components64[2];
52 } counter_;
53 };
54
[email protected]1b47ce22010-03-31 16:18:3055 Encryptor();
56 virtual ~Encryptor();
[email protected]39422e32010-03-25 19:13:0057
[email protected]1b47ce22010-03-31 16:18:3058 // Initializes the encryptor using |key| and |iv|. Returns false if either the
59 // key or the initialization vector cannot be used.
[email protected]2377cdee2011-06-24 20:46:0660 //
[email protected]fdce4782011-11-29 20:06:1861 // If |mode| is CBC, |iv| must not be empty; if it is CTR, then |iv| must be
62 // empty.
[email protected]44a016a82011-07-08 02:53:0963 bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv);
[email protected]39422e32010-03-25 19:13:0064
[email protected]fdce4782011-11-29 20:06:1865 // Encrypts |plaintext| into |ciphertext|. |plaintext| may only be empty if
66 // the mode is CBC.
[email protected]44a016a82011-07-08 02:53:0967 bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext);
[email protected]39422e32010-03-25 19:13:0068
[email protected]fdce4782011-11-29 20:06:1869 // Decrypts |ciphertext| into |plaintext|. |ciphertext| must not be empty.
[email protected]ef0677462012-04-25 00:27:4370 //
71 // WARNING: In CBC mode, Decrypt() returns false if it detects the padding
72 // in the decrypted plaintext is wrong. Padding errors can result from
73 // tampered ciphertext or a wrong decryption key. But successful decryption
74 // does not imply the authenticity of the data. The caller of Decrypt()
75 // must either authenticate the ciphertext before decrypting it, or take
76 // care to not report decryption failure. Otherwise it could inadvertently
77 // be used as a padding oracle to attack the cryptosystem.
[email protected]44a016a82011-07-08 02:53:0978 bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext);
[email protected]39422e32010-03-25 19:13:0079
[email protected]2377cdee2011-06-24 20:46:0680 // Sets the counter value when in CTR mode. Currently only 128-bits
81 // counter value is supported.
82 //
83 // Returns true only if update was successful.
[email protected]44a016a82011-07-08 02:53:0984 bool SetCounter(const base::StringPiece& counter);
[email protected]2377cdee2011-06-24 20:46:0685
[email protected]39422e32010-03-25 19:13:0086 // TODO(albertb): Support streaming encryption.
87
88 private:
[email protected]2377cdee2011-06-24 20:46:0689 // Generates a mask using |counter_| to be used for encryption in CTR mode.
90 // Resulting mask will be written to |mask| with |mask_len| bytes.
91 //
92 // Make sure there's enough space in mask when calling this method.
93 // Reserve at least |plaintext_len| + 16 bytes for |mask|.
94 //
95 // The generated mask will always have at least |plaintext_len| bytes and
96 // will be a multiple of the counter length.
97 //
98 // This method is used only in CTR mode.
99 //
100 // Returns false if this call failed.
101 bool GenerateCounterMask(size_t plaintext_len,
102 uint8* mask,
103 size_t* mask_len);
104
105 // Mask the |plaintext| message using |mask|. The output will be written to
106 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes.
107 void MaskMessage(const void* plaintext,
108 size_t plaintext_len,
109 const void* mask,
110 void* ciphertext) const;
111
[email protected]1b47ce22010-03-31 16:18:30112 SymmetricKey* key_;
[email protected]39422e32010-03-25 19:13:00113 Mode mode_;
[email protected]2377cdee2011-06-24 20:46:06114 scoped_ptr<Counter> counter_;
[email protected]39422e32010-03-25 19:13:00115
[email protected]25007102010-11-12 16:29:06116#if defined(USE_OPENSSL)
[email protected]a3f742692013-06-13 19:48:01117 bool Crypt(bool do_encrypt, // Pass true to encrypt, false to decrypt.
[email protected]44a016a82011-07-08 02:53:09118 const base::StringPiece& input,
[email protected]25007102010-11-12 16:29:06119 std::string* output);
[email protected]a3f742692013-06-13 19:48:01120 bool CryptCTR(bool do_encrypt,
121 const base::StringPiece& input,
122 std::string* output);
[email protected]25007102010-11-12 16:29:06123 std::string iv_;
[email protected]45a445212012-06-15 08:11:52124#elif defined(USE_NSS) || defined(OS_WIN) || defined(OS_MACOSX)
[email protected]2377cdee2011-06-24 20:46:06125 bool Crypt(PK11Context* context,
[email protected]44a016a82011-07-08 02:53:09126 const base::StringPiece& input,
[email protected]2377cdee2011-06-24 20:46:06127 std::string* output);
128 bool CryptCTR(PK11Context* context,
[email protected]44a016a82011-07-08 02:53:09129 const base::StringPiece& input,
[email protected]2377cdee2011-06-24 20:46:06130 std::string* output);
[email protected]39422e32010-03-25 19:13:00131 ScopedSECItem param_;
[email protected]39422e32010-03-25 19:13:00132#endif
133};
134
[email protected]4b559b4d2011-04-14 17:37:14135} // namespace crypto
[email protected]39422e32010-03-25 19:13:00136
[email protected]4b559b4d2011-04-14 17:37:14137#endif // CRYPTO_ENCRYPTOR_H_