1ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#ifndef CRYPTO_ENCRYPTOR_H_
6ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#define CRYPTO_ENCRYPTOR_H_
73345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <string>
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "build/build_config.h"
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#if defined(USE_NSS)
14ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "crypto/scoped_nss_types.h"
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#elif defined(OS_WIN)
16ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "crypto/scoped_capi_types.h"
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
19ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsennamespace crypto {
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SymmetricKey;
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass Encryptor {
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  enum Mode {
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    CBC
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Encryptor();
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~Encryptor();
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Initializes the encryptor using |key| and |iv|. Returns false if either the
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // key or the initialization vector cannot be used.
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool Init(SymmetricKey* key, Mode mode, const std::string& iv);
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Encrypts |plaintext| into |ciphertext|.
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool Encrypt(const std::string& plaintext, std::string* ciphertext);
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Decrypts |ciphertext| into |plaintext|.
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool Decrypt(const std::string& ciphertext, std::string* plaintext);
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // TODO(albertb): Support streaming encryption.
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SymmetricKey* key_;
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Mode mode_;
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
474a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#if defined(USE_OPENSSL)
484a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  bool Crypt(bool encrypt,  // Pass true to encrypt, false to decrypt.
494a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch             const std::string& input,
504a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch             std::string* output);
514a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch  std::string iv_;
524a5e2dc747d50c653511c68ccb2cfbfb740bd5a7Ben Murdoch#elif defined(USE_NSS)
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ScopedPK11Slot slot_;
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ScopedSECItem param_;
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#elif defined(OS_MACOSX)
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool Crypt(int /*CCOperation*/ op,
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch             const std::string& input,
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch             std::string* output);
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  std::string iv_;
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#elif defined(OS_WIN)
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ScopedHCRYPTKEY capi_key_;
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DWORD block_size_;
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
67ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen}  // namespace crypto
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
69ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#endif  // CRYPTO_ENCRYPTOR_H_
70