blob: cfab6f284b902291662d42172e3f451ca628d30d [file] [log] [blame]
[email protected]9493ee95c2011-03-28 23:48:441// Copyright (c) 2011 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]39422e32010-03-25 19:13:008
9#include <string>
10
[email protected]2377cdee2011-06-24 20:46:0611#include "base/basictypes.h"
12#include "base/scoped_ptr.h"
[email protected]44a016a82011-07-08 02:53:0913#include "base/string_piece.h"
[email protected]692033a2010-04-09 18:40:5014#include "build/build_config.h"
[email protected]82091cc2011-06-17 21:11:1315#include "crypto/crypto_api.h"
[email protected]692033a2010-04-09 18:40:5016
17#if defined(USE_NSS)
[email protected]4b559b4d2011-04-14 17:37:1418#include "crypto/scoped_nss_types.h"
[email protected]692033a2010-04-09 18:40:5019#elif defined(OS_WIN)
[email protected]4b559b4d2011-04-14 17:37:1420#include "crypto/scoped_capi_types.h"
[email protected]692033a2010-04-09 18:40:5021#endif
[email protected]39422e32010-03-25 19:13:0022
[email protected]4b559b4d2011-04-14 17:37:1423namespace crypto {
[email protected]39422e32010-03-25 19:13:0024
[email protected]692033a2010-04-09 18:40:5025class SymmetricKey;
26
[email protected]82091cc2011-06-17 21:11:1327class CRYPTO_API Encryptor {
[email protected]39422e32010-03-25 19:13:0028 public:
29 enum Mode {
[email protected]2377cdee2011-06-24 20:46:0630 CBC,
31 CTR,
[email protected]39422e32010-03-25 19:13:0032 };
[email protected]2377cdee2011-06-24 20:46:0633
34 // This class implements a 128-bits counter to be used in AES-CTR encryption.
35 // Only 128-bits counter is supported in this class.
36 class Counter {
37 public:
[email protected]44a016a82011-07-08 02:53:0938 Counter(const base::StringPiece& counter);
[email protected]2377cdee2011-06-24 20:46:0639 ~Counter();
40
41 // Increment the counter value.
42 bool Increment();
43
44 // Write the content of the counter to |buf|. |buf| should have enough
45 // space for |GetLengthInBytes()|.
46 void Write(void* buf);
47
48 // Return the length of this counter.
49 size_t GetLengthInBytes() const;
50
51 private:
52 union {
53 uint32 components32[4];
54 uint64 components64[2];
55 } counter_;
56 };
57
[email protected]1b47ce22010-03-31 16:18:3058 Encryptor();
59 virtual ~Encryptor();
[email protected]39422e32010-03-25 19:13:0060
[email protected]1b47ce22010-03-31 16:18:3061 // Initializes the encryptor using |key| and |iv|. Returns false if either the
62 // key or the initialization vector cannot be used.
[email protected]2377cdee2011-06-24 20:46:0663 //
64 // When |mode| is CTR then |iv| should be empty.
[email protected]44a016a82011-07-08 02:53:0965 bool Init(SymmetricKey* key, Mode mode, const base::StringPiece& iv);
[email protected]39422e32010-03-25 19:13:0066
67 // Encrypts |plaintext| into |ciphertext|.
[email protected]44a016a82011-07-08 02:53:0968 bool Encrypt(const base::StringPiece& plaintext, std::string* ciphertext);
[email protected]39422e32010-03-25 19:13:0069
70 // Decrypts |ciphertext| into |plaintext|.
[email protected]44a016a82011-07-08 02:53:0971 bool Decrypt(const base::StringPiece& ciphertext, std::string* plaintext);
[email protected]39422e32010-03-25 19:13:0072
[email protected]2377cdee2011-06-24 20:46:0673 // Sets the counter value when in CTR mode. Currently only 128-bits
74 // counter value is supported.
75 //
76 // Returns true only if update was successful.
[email protected]44a016a82011-07-08 02:53:0977 bool SetCounter(const base::StringPiece& counter);
[email protected]2377cdee2011-06-24 20:46:0678
[email protected]39422e32010-03-25 19:13:0079 // TODO(albertb): Support streaming encryption.
80
81 private:
[email protected]2377cdee2011-06-24 20:46:0682 // Generates a mask using |counter_| to be used for encryption in CTR mode.
83 // Resulting mask will be written to |mask| with |mask_len| bytes.
84 //
85 // Make sure there's enough space in mask when calling this method.
86 // Reserve at least |plaintext_len| + 16 bytes for |mask|.
87 //
88 // The generated mask will always have at least |plaintext_len| bytes and
89 // will be a multiple of the counter length.
90 //
91 // This method is used only in CTR mode.
92 //
93 // Returns false if this call failed.
94 bool GenerateCounterMask(size_t plaintext_len,
95 uint8* mask,
96 size_t* mask_len);
97
98 // Mask the |plaintext| message using |mask|. The output will be written to
99 // |ciphertext|. |ciphertext| must have at least |plaintext_len| bytes.
100 void MaskMessage(const void* plaintext,
101 size_t plaintext_len,
102 const void* mask,
103 void* ciphertext) const;
104
[email protected]1b47ce22010-03-31 16:18:30105 SymmetricKey* key_;
[email protected]39422e32010-03-25 19:13:00106 Mode mode_;
[email protected]2377cdee2011-06-24 20:46:06107 scoped_ptr<Counter> counter_;
[email protected]39422e32010-03-25 19:13:00108
[email protected]25007102010-11-12 16:29:06109#if defined(USE_OPENSSL)
110 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
[email protected]44a016a82011-07-08 02:53:09111 const base::StringPiece& input,
[email protected]25007102010-11-12 16:29:06112 std::string* output);
113 std::string iv_;
114#elif defined(USE_NSS)
[email protected]2377cdee2011-06-24 20:46:06115 bool Crypt(PK11Context* context,
[email protected]44a016a82011-07-08 02:53:09116 const base::StringPiece& input,
[email protected]2377cdee2011-06-24 20:46:06117 std::string* output);
118 bool CryptCTR(PK11Context* context,
[email protected]44a016a82011-07-08 02:53:09119 const base::StringPiece& input,
[email protected]2377cdee2011-06-24 20:46:06120 std::string* output);
[email protected]39422e32010-03-25 19:13:00121 ScopedPK11Slot slot_;
122 ScopedSECItem param_;
[email protected]108118232010-03-29 18:22:24123#elif defined(OS_MACOSX)
124 bool Crypt(int /*CCOperation*/ op,
[email protected]44a016a82011-07-08 02:53:09125 const base::StringPiece& input,
[email protected]108118232010-03-29 18:22:24126 std::string* output);
127
128 std::string iv_;
[email protected]692033a2010-04-09 18:40:50129#elif defined(OS_WIN)
130 ScopedHCRYPTKEY capi_key_;
131 DWORD block_size_;
[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_