[email protected] | 9493ee95c | 2011-03-28 23:48:44 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #ifndef CRYPTO_ENCRYPTOR_H_ |
6 | #define CRYPTO_ENCRYPTOR_H_ | ||||
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 8 | |
9 | #include <string> | ||||
10 | |||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 11 | #include "build/build_config.h" |
12 | |||||
13 | #if defined(USE_NSS) | ||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 14 | #include "crypto/scoped_nss_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 15 | #elif defined(OS_WIN) |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 16 | #include "crypto/scoped_capi_types.h" |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 17 | #endif |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 18 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 19 | namespace crypto { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 20 | |
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 21 | class SymmetricKey; |
22 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 23 | class Encryptor { |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 24 | public: |
25 | enum Mode { | ||||
26 | CBC | ||||
27 | }; | ||||
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 28 | Encryptor(); |
29 | virtual ~Encryptor(); | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 30 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 31 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
32 | // key or the initialization vector cannot be used. | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 33 | 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] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 44 | SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 45 | Mode mode_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 46 | |
[email protected] | 2500710 | 2010-11-12 16:29:06 | [diff] [blame] | 47 | #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] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 53 | ScopedPK11Slot slot_; |
54 | ScopedSECItem param_; | ||||
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 55 | #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] | 692033a | 2010-04-09 18:40:50 | [diff] [blame] | 61 | #elif defined(OS_WIN) |
62 | ScopedHCRYPTKEY capi_key_; | ||||
63 | DWORD block_size_; | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 64 | #endif |
65 | }; | ||||
66 | |||||
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 67 | } // namespace crypto |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 68 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 69 | #endif // CRYPTO_ENCRYPTOR_H_ |