[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef BASE_CRYPTO_ENCRYPTOR_H_ | ||||
6 | #define BASE_CRYPTO_ENCRYPTOR_H_ | ||||
7 | |||||
8 | #include <string> | ||||
9 | |||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame^] | 10 | #include "build/build_config.h" |
11 | |||||
12 | #if defined(USE_NSS) | ||||
13 | #include "base/crypto/scoped_nss_types.h" | ||||
14 | #elif defined(OS_WIN) | ||||
15 | #include "base/crypto/scoped_capi_types.h" | ||||
16 | #endif | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 17 | |
18 | namespace base { | ||||
19 | |||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame^] | 20 | class SymmetricKey; |
21 | |||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 22 | class Encryptor { |
23 | public: | ||||
24 | enum Mode { | ||||
25 | CBC | ||||
26 | }; | ||||
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 27 | Encryptor(); |
28 | virtual ~Encryptor(); | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 29 | |
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 30 | // Initializes the encryptor using |key| and |iv|. Returns false if either the |
31 | // key or the initialization vector cannot be used. | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 32 | bool Init(SymmetricKey* key, Mode mode, const std::string& iv); |
33 | |||||
34 | // Encrypts |plaintext| into |ciphertext|. | ||||
35 | bool Encrypt(const std::string& plaintext, std::string* ciphertext); | ||||
36 | |||||
37 | // Decrypts |ciphertext| into |plaintext|. | ||||
38 | bool Decrypt(const std::string& ciphertext, std::string* plaintext); | ||||
39 | |||||
40 | // TODO(albertb): Support streaming encryption. | ||||
41 | |||||
42 | private: | ||||
[email protected] | 1b47ce2 | 2010-03-31 16:18:30 | [diff] [blame] | 43 | SymmetricKey* key_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 44 | Mode mode_; |
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 45 | |
46 | #if defined(USE_NSS) | ||||
47 | ScopedPK11Slot slot_; | ||||
48 | ScopedSECItem param_; | ||||
[email protected] | 10811823 | 2010-03-29 18:22:24 | [diff] [blame] | 49 | #elif defined(OS_MACOSX) |
50 | bool Crypt(int /*CCOperation*/ op, | ||||
51 | const std::string& input, | ||||
52 | std::string* output); | ||||
53 | |||||
54 | std::string iv_; | ||||
[email protected] | 692033a | 2010-04-09 18:40:50 | [diff] [blame^] | 55 | #elif defined(OS_WIN) |
56 | ScopedHCRYPTKEY capi_key_; | ||||
57 | DWORD block_size_; | ||||
[email protected] | 39422e3 | 2010-03-25 19:13:00 | [diff] [blame] | 58 | #endif |
59 | }; | ||||
60 | |||||
61 | } // namespace base | ||||
62 | |||||
63 | #endif // BASE_CRYPTO_ENCRYPTOR_H_ |