Home | History | Annotate | Download | only in crypto
      1 // Copyright (c) 2011 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 CRYPTO_ENCRYPTOR_H_
      6 #define CRYPTO_ENCRYPTOR_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "build/build_config.h"
     12 
     13 #if defined(USE_NSS)
     14 #include "crypto/scoped_nss_types.h"
     15 #elif defined(OS_WIN)
     16 #include "crypto/scoped_capi_types.h"
     17 #endif
     18 
     19 namespace crypto {
     20 
     21 class SymmetricKey;
     22 
     23 class Encryptor {
     24  public:
     25   enum Mode {
     26     CBC
     27   };
     28   Encryptor();
     29   virtual ~Encryptor();
     30 
     31   // Initializes the encryptor using |key| and |iv|. Returns false if either the
     32   // key or the initialization vector cannot be used.
     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:
     44   SymmetricKey* key_;
     45   Mode mode_;
     46 
     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)
     53   ScopedPK11Slot slot_;
     54   ScopedSECItem param_;
     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_;
     61 #elif defined(OS_WIN)
     62   ScopedHCRYPTKEY capi_key_;
     63   DWORD block_size_;
     64 #endif
     65 };
     66 
     67 }  // namespace crypto
     68 
     69 #endif  // CRYPTO_ENCRYPTOR_H_
     70