"Fossies" - the Fresh Open Source Software Archive

Member "src/Volume/Cipher.h" (10 Oct 2018, 3998 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Cipher.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.21_Source_vs_1.22_Source.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Encryption_Ciphers
   14 #define TC_HEADER_Encryption_Ciphers
   15 
   16 #include "Platform/Platform.h"
   17 #include "Crypto/config.h"
   18 
   19 
   20 namespace VeraCrypt
   21 {
   22     class Cipher;
   23     typedef vector < shared_ptr <Cipher> > CipherList;
   24 
   25     class Cipher
   26     {
   27     public:
   28         virtual ~Cipher ();
   29 
   30         virtual void DecryptBlock (byte *data) const;
   31         virtual void DecryptBlocks (byte *data, size_t blockCount) const;
   32         static void EnableHwSupport (bool enable) { HwSupportEnabled = enable; }
   33         virtual void EncryptBlock (byte *data) const;
   34         virtual void EncryptBlocks (byte *data, size_t blockCount) const;
   35         static CipherList GetAvailableCiphers ();
   36         virtual size_t GetBlockSize () const = 0;
   37         virtual const SecureBuffer &GetKey () const { return Key; }
   38         virtual size_t GetKeySize () const = 0;
   39         virtual wstring GetName () const = 0;
   40         virtual shared_ptr <Cipher> GetNew () const = 0;
   41         virtual bool IsHwSupportAvailable () const { return false; }
   42         static bool IsHwSupportEnabled () { return HwSupportEnabled; }
   43         virtual void SetKey (const ConstBufferPtr &key);
   44 
   45         static const int MaxBlockSize = 16;
   46 
   47     protected:
   48         Cipher ();
   49 
   50         virtual void Decrypt (byte *data) const = 0;
   51         virtual void Encrypt (byte *data) const = 0;
   52         virtual size_t GetScheduledKeySize () const = 0;
   53         virtual void SetCipherKey (const byte *key) = 0;
   54 
   55         static bool HwSupportEnabled;
   56         bool Initialized;
   57         SecureBuffer Key;
   58         SecureBuffer ScheduledKey;
   59 
   60     private:
   61         Cipher (const Cipher &);
   62         Cipher &operator= (const Cipher &);
   63     };
   64 
   65     struct CipherException : public Exception
   66     {
   67     protected:
   68         CipherException () { }
   69         CipherException (const string &message) : Exception (message) { }
   70         CipherException (const string &message, const wstring &subject) : Exception (message, subject) { }
   71     };
   72 
   73 
   74 #define TC_CIPHER(NAME, BLOCK_SIZE, KEY_SIZE) \
   75     class TC_JOIN (Cipher,NAME) : public Cipher \
   76     { \
   77     public: \
   78         TC_JOIN (Cipher,NAME) () { } \
   79         virtual ~TC_JOIN (Cipher,NAME) () { } \
   80 \
   81         virtual size_t GetBlockSize () const { return BLOCK_SIZE; }; \
   82         virtual size_t GetKeySize () const { return KEY_SIZE; }; \
   83         virtual wstring GetName () const { return L###NAME; }; \
   84         virtual shared_ptr <Cipher> GetNew () const { return shared_ptr <Cipher> (new TC_JOIN (Cipher,NAME)()); } \
   85         TC_CIPHER_ADD_METHODS \
   86 \
   87     protected: \
   88         virtual void Decrypt (byte *data) const; \
   89         virtual void Encrypt (byte *data) const; \
   90         virtual size_t GetScheduledKeySize () const; \
   91         virtual void SetCipherKey (const byte *key); \
   92 \
   93     private: \
   94         TC_JOIN (Cipher,NAME) (const TC_JOIN (Cipher,NAME) &); \
   95         TC_JOIN (Cipher,NAME) &operator= (const TC_JOIN (Cipher,NAME) &); \
   96     }
   97 
   98 #define TC_CIPHER_ADD_METHODS \
   99     virtual void DecryptBlocks (byte *data, size_t blockCount) const; \
  100     virtual void EncryptBlocks (byte *data, size_t blockCount) const; \
  101     virtual bool IsHwSupportAvailable () const;
  102 
  103     TC_CIPHER (AES, 16, 32);
  104     TC_CIPHER (Serpent, 16, 32);
  105     TC_CIPHER (Twofish, 16, 32);
  106     TC_CIPHER (Camellia, 16, 32);
  107     TC_CIPHER (Kuznyechik, 16, 32);
  108 
  109 #undef TC_CIPHER_ADD_METHODS
  110 #define TC_CIPHER_ADD_METHODS
  111 
  112     TC_CIPHER (Gost89, 16, 32);
  113     TC_CIPHER (Gost89StaticSBOX, 16, 32);
  114 
  115 #undef TC_CIPHER
  116 
  117 
  118 #define TC_EXCEPTION(NAME) TC_EXCEPTION_DECL(NAME,CipherException)
  119 
  120 #undef TC_EXCEPTION_SET
  121 #define TC_EXCEPTION_SET \
  122     TC_EXCEPTION (CipherInitError); \
  123     TC_EXCEPTION (WeakKeyDetected);
  124 
  125     TC_EXCEPTION_SET;
  126 
  127 #undef TC_EXCEPTION
  128 
  129 #if (defined (TC_ARCH_X86) || defined (TC_ARCH_X64)) && !defined (__ppc__)
  130 #   define TC_AES_HW_CPU
  131 #endif
  132 
  133 }
  134 
  135 #endif // TC_HEADER_Encryption_Ciphers