blob: d8250f6bdf48f65ab10f9fb64d400444d1863a9d [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]692033a2010-04-09 18:40:5011#include "build/build_config.h"
12
13#if defined(USE_NSS)
[email protected]4b559b4d2011-04-14 17:37:1414#include "crypto/scoped_nss_types.h"
[email protected]692033a2010-04-09 18:40:5015#elif defined(OS_WIN)
[email protected]4b559b4d2011-04-14 17:37:1416#include "crypto/scoped_capi_types.h"
[email protected]692033a2010-04-09 18:40:5017#endif
[email protected]39422e32010-03-25 19:13:0018
[email protected]4b559b4d2011-04-14 17:37:1419namespace crypto {
[email protected]39422e32010-03-25 19:13:0020
[email protected]692033a2010-04-09 18:40:5021class SymmetricKey;
22
[email protected]4b559b4d2011-04-14 17:37:1423class Encryptor {
[email protected]39422e32010-03-25 19:13:0024 public:
25 enum Mode {
26 CBC
27 };
[email protected]1b47ce22010-03-31 16:18:3028 Encryptor();
29 virtual ~Encryptor();
[email protected]39422e32010-03-25 19:13:0030
[email protected]1b47ce22010-03-31 16:18:3031 // Initializes the encryptor using |key| and |iv|. Returns false if either the
32 // key or the initialization vector cannot be used.
[email protected]39422e32010-03-25 19:13:0033 bool Init(SymmetricKey* key, Mode mode, const std::string& iv);
34
35 // Encrypts |plaintext| into |ciphertext|.
36 bool Encrypt(const std::string& plaintext, std::string* ciphertext);
37
38 // Decrypts |ciphertext| into |plaintext|.
39 bool Decrypt(const std::string& ciphertext, std::string* plaintext);
40
41 // TODO(albertb): Support streaming encryption.
42
43 private:
[email protected]1b47ce22010-03-31 16:18:3044 SymmetricKey* key_;
[email protected]39422e32010-03-25 19:13:0045 Mode mode_;
[email protected]39422e32010-03-25 19:13:0046
[email protected]25007102010-11-12 16:29:0647#if defined(USE_OPENSSL)
48 bool Crypt(bool encrypt, // Pass true to encrypt, false to decrypt.
49 const std::string& input,
50 std::string* output);
51 std::string iv_;
52#elif defined(USE_NSS)
[email protected]39422e32010-03-25 19:13:0053 ScopedPK11Slot slot_;
54 ScopedSECItem param_;
[email protected]108118232010-03-29 18:22:2455#elif defined(OS_MACOSX)
56 bool Crypt(int /*CCOperation*/ op,
57 const std::string& input,
58 std::string* output);
59
60 std::string iv_;
[email protected]692033a2010-04-09 18:40:5061#elif defined(OS_WIN)
62 ScopedHCRYPTKEY capi_key_;
63 DWORD block_size_;
[email protected]39422e32010-03-25 19:13:0064#endif
65};
66
[email protected]4b559b4d2011-04-14 17:37:1467} // namespace crypto
[email protected]39422e32010-03-25 19:13:0068
[email protected]4b559b4d2011-04-14 17:37:1469#endif // CRYPTO_ENCRYPTOR_H_