aes.h raw

   1  // Copyright (c) 2015-present The Bitcoin Core developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  //
   5  // C++ wrapper around ctaes, a constant-time AES implementation
   6  
   7  #ifndef BITCOIN_CRYPTO_AES_H
   8  #define BITCOIN_CRYPTO_AES_H
   9  
  10  #include <support/allocators/secure.h>
  11  extern "C" {
  12  #include <crypto/ctaes/ctaes.h>
  13  }
  14  
  15  static const int AES_BLOCKSIZE = 16;
  16  static const int AES256_KEYSIZE = 32;
  17  
  18  /** An encryption class for AES-256. */
  19  class AES256Encrypt
  20  {
  21  private:
  22      secure_allocator<AES256_ctx> allocator;
  23      AES256_ctx *ctx;
  24  
  25  public:
  26      explicit AES256Encrypt(const unsigned char key[32]);
  27      ~AES256Encrypt();
  28      void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const;
  29  };
  30  
  31  /** A decryption class for AES-256. */
  32  class AES256Decrypt
  33  {
  34  private:
  35      secure_allocator<AES256_ctx> allocator;
  36      AES256_ctx *ctx;
  37  
  38  public:
  39      explicit AES256Decrypt(const unsigned char key[32]);
  40      ~AES256Decrypt();
  41      void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const;
  42  };
  43  
  44  class AES256CBCEncrypt
  45  {
  46  public:
  47      AES256CBCEncrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  48      ~AES256CBCEncrypt();
  49      int Encrypt(const unsigned char* data, int size, unsigned char* out) const;
  50  
  51  private:
  52      secure_allocator<unsigned char> allocator;
  53      const AES256Encrypt enc;
  54      const bool pad;
  55      unsigned char *iv;
  56  };
  57  
  58  class AES256CBCDecrypt
  59  {
  60  public:
  61      AES256CBCDecrypt(const unsigned char key[AES256_KEYSIZE], const unsigned char ivIn[AES_BLOCKSIZE], bool padIn);
  62      ~AES256CBCDecrypt();
  63      int Decrypt(const unsigned char* data, int size, unsigned char* out) const;
  64  
  65  private:
  66      secure_allocator<unsigned char> allocator;
  67      const AES256Decrypt dec;
  68      const bool pad;
  69      unsigned char *iv;
  70  };
  71  
  72  #endif // BITCOIN_CRYPTO_AES_H
  73