"Fossies" - the Fresh Open Source Software Archive

Member "src/Volume/EncryptionAlgorithm.cpp" (10 Oct 2018, 9925 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 "EncryptionAlgorithm.cpp" 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 #include "EncryptionAlgorithm.h"
   14 #include "EncryptionModeXTS.h"
   15 
   16 namespace VeraCrypt
   17 {
   18     EncryptionAlgorithm::EncryptionAlgorithm () : Deprecated (false)
   19     {
   20     }
   21 
   22     EncryptionAlgorithm::~EncryptionAlgorithm ()
   23     {
   24     }
   25 
   26     void EncryptionAlgorithm::Decrypt (byte *data, uint64 length) const
   27     {
   28         if_debug (ValidateState ());
   29         Mode->Decrypt (data, length);
   30     }
   31 
   32     void EncryptionAlgorithm::Decrypt (const BufferPtr &data) const
   33     {
   34         Decrypt (data, data.Size());
   35     }
   36 
   37     void EncryptionAlgorithm::DecryptSectors (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const
   38     {
   39         if_debug (ValidateState());
   40         Mode->DecryptSectors (data, sectorIndex, sectorCount, sectorSize);
   41     }
   42 
   43     void EncryptionAlgorithm::Encrypt (byte *data, uint64 length) const
   44     {
   45         if_debug (ValidateState());
   46         Mode->Encrypt (data, length);
   47     }
   48 
   49     void EncryptionAlgorithm::Encrypt (const BufferPtr &data) const
   50     {
   51         Encrypt (data, data.Size());
   52     }
   53 
   54     void EncryptionAlgorithm::EncryptSectors (byte *data, uint64 sectorIndex, uint64 sectorCount, size_t sectorSize) const
   55     {
   56         if_debug (ValidateState ());
   57         Mode->EncryptSectors (data, sectorIndex, sectorCount, sectorSize);
   58     }
   59 
   60     EncryptionAlgorithmList EncryptionAlgorithm::GetAvailableAlgorithms ()
   61     {
   62         EncryptionAlgorithmList l;
   63 
   64         l.push_back (shared_ptr <EncryptionAlgorithm> (new AES ()));
   65         l.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ()));
   66         l.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ()));
   67         l.push_back (shared_ptr <EncryptionAlgorithm> (new Camellia ()));
   68         l.push_back (shared_ptr <EncryptionAlgorithm> (new GOST89 ()));
   69         l.push_back (shared_ptr <EncryptionAlgorithm> (new Kuznyechik ()));
   70         l.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ()));
   71         l.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ()));
   72         l.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaKuznyechik ()));
   73         l.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaSerpent ()));
   74         l.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikAES ()));
   75         l.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikSerpentCamellia ()));
   76         l.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikTwofish ()));
   77         l.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ()));
   78         l.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ()));
   79         l.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ()));
   80 
   81         return l;
   82     }
   83 
   84     size_t EncryptionAlgorithm::GetLargestKeySize (const EncryptionAlgorithmList &algorithms)
   85     {
   86         size_t largestKeySize = 0;
   87 
   88         foreach_ref (const EncryptionAlgorithm &ea, algorithms)
   89         {
   90             if (ea.GetKeySize() > largestKeySize)
   91                 largestKeySize = ea.GetKeySize();
   92         }
   93 
   94         return largestKeySize;
   95     }
   96 
   97     size_t EncryptionAlgorithm::GetKeySize () const
   98     {
   99         if (Ciphers.size() < 1)
  100             throw NotInitialized (SRC_POS);
  101 
  102         size_t keySize = 0;
  103 
  104         foreach_ref (const Cipher &c, Ciphers)
  105             keySize += c.GetKeySize();
  106 
  107         return keySize;
  108     }
  109 
  110     size_t EncryptionAlgorithm::GetMaxBlockSize () const
  111     {
  112         size_t blockSize = 0;
  113 
  114         foreach_ref (const Cipher &c, Ciphers)
  115             if (c.GetBlockSize() > blockSize)
  116                 blockSize = c.GetBlockSize();
  117 
  118         return blockSize;
  119     }
  120 
  121     size_t EncryptionAlgorithm::GetMinBlockSize () const
  122     {
  123         size_t blockSize = 0;
  124 
  125         foreach_ref (const Cipher &c, Ciphers)
  126             if (blockSize == 0 || c.GetBlockSize() < blockSize)
  127                 blockSize = c.GetBlockSize();
  128 
  129         return blockSize;
  130     }
  131 
  132     shared_ptr <EncryptionMode> EncryptionAlgorithm::GetMode () const
  133     {
  134         if (Mode.get() == nullptr)
  135             throw NotInitialized (SRC_POS);
  136 
  137         return Mode;
  138     }
  139 
  140     wstring EncryptionAlgorithm::GetName (bool forGuiDisplay) const
  141     {
  142         if (Ciphers.size() < 1)
  143             throw NotInitialized (SRC_POS);
  144 
  145         wstring name;
  146 
  147         int depth = 0;
  148         foreach_reverse_ref (const Cipher &c, Ciphers)
  149         {
  150             if (name.empty())
  151                 name = c.GetName();
  152             else
  153             {
  154                 depth++;
  155                 if (forGuiDisplay)
  156                     name += wstring (L"(");
  157                 else
  158                     name += wstring (L"-");
  159                 name += c.GetName();
  160             }
  161         }
  162 
  163         if (forGuiDisplay && depth)
  164         {
  165             for (int i = 0; i < depth; i++)
  166                 name += wstring(L")");
  167         }
  168 
  169         return name;
  170     }
  171 
  172     bool EncryptionAlgorithm::IsModeSupported (const EncryptionMode &mode) const
  173     {
  174         bool supported = false;
  175 
  176         foreach_ref (const EncryptionMode &em, SupportedModes)
  177         {
  178             if (typeid (mode) == typeid (em))
  179             {
  180                 supported = true;
  181                 break;
  182             }
  183         }
  184 
  185         return supported;
  186     }
  187 
  188 
  189     bool EncryptionAlgorithm::IsModeSupported (const shared_ptr <EncryptionMode> mode) const
  190     {
  191         return IsModeSupported (*mode);
  192     }
  193 
  194     void EncryptionAlgorithm::SetMode (shared_ptr <EncryptionMode> mode)
  195     {
  196         if (!IsModeSupported (*mode))
  197             throw ParameterIncorrect (SRC_POS);
  198 
  199         mode->SetCiphers (Ciphers);
  200         Mode = mode;
  201     }
  202 
  203     void EncryptionAlgorithm::SetKey (const ConstBufferPtr &key)
  204     {
  205         if (Ciphers.size() < 1)
  206             throw NotInitialized (SRC_POS);
  207 
  208         if (GetKeySize() != key.Size())
  209             throw ParameterIncorrect (SRC_POS);
  210 
  211         size_t keyOffset = 0;
  212         foreach_ref (Cipher &c, Ciphers)
  213         {
  214             c.SetKey (key.GetRange (keyOffset, c.GetKeySize()));
  215             keyOffset += c.GetKeySize();
  216         }
  217     }
  218 
  219     void EncryptionAlgorithm::ValidateState () const
  220     {
  221         if (Ciphers.size() < 1 || Mode.get() == nullptr)
  222             throw NotInitialized (SRC_POS);
  223     }
  224 
  225     // AES
  226     AES::AES ()
  227     {
  228         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES()));
  229 
  230         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  231     }
  232 
  233     // AES-Twofish
  234     AESTwofish::AESTwofish ()
  235     {
  236         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
  237         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ()));
  238 
  239         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  240     }
  241 
  242     // AES-Twofish-Serpent
  243     AESTwofishSerpent::AESTwofishSerpent ()
  244     {
  245         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  246         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
  247         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ()));
  248 
  249         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  250     }
  251 
  252     // Serpent
  253     Serpent::Serpent ()
  254     {
  255         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent()));
  256 
  257         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  258     }
  259 
  260     // Serpent-AES
  261     SerpentAES::SerpentAES ()
  262     {
  263         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ()));
  264         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  265 
  266         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  267     }
  268 
  269     // Twofish
  270     Twofish::Twofish ()
  271     {
  272         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish()));
  273 
  274         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  275     }
  276 
  277     // Twofish-Serpent
  278     TwofishSerpent::TwofishSerpent ()
  279     {
  280         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  281         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
  282 
  283         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  284     }
  285 
  286     // Serpent-Twofish-AES
  287     SerpentTwofishAES::SerpentTwofishAES ()
  288     {
  289         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ()));
  290         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
  291         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  292 
  293         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  294     }
  295     
  296     // Camellia
  297     Camellia::Camellia ()
  298     {
  299         Ciphers.push_back (shared_ptr <Cipher> (new CipherCamellia()));
  300 
  301         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  302     }
  303 
  304     
  305     // GOST89
  306     GOST89::GOST89 ()
  307     {
  308         Deprecated = true;
  309 
  310         Ciphers.push_back (shared_ptr <Cipher> (new CipherGost89()));
  311 
  312         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  313     }
  314 
  315     // Kuznyechik
  316     Kuznyechik::Kuznyechik ()
  317     {
  318         Ciphers.push_back (shared_ptr <Cipher> (new CipherKuznyechik()));
  319 
  320         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  321     }
  322     
  323     // Kuznyechik-Twofish
  324     KuznyechikTwofish::KuznyechikTwofish ()
  325     {
  326         Ciphers.push_back (shared_ptr <Cipher> (new CipherTwofish ()));
  327         Ciphers.push_back (shared_ptr <Cipher> (new CipherKuznyechik ()));
  328 
  329         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  330     }
  331     
  332     // Kuznyechik-AES
  333     KuznyechikAES::KuznyechikAES ()
  334     {
  335         Ciphers.push_back (shared_ptr <Cipher> (new CipherAES ()));
  336         Ciphers.push_back (shared_ptr <Cipher> (new CipherKuznyechik ()));
  337 
  338         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  339     }
  340     
  341     // Kuznyechik-Serpent-Camellia
  342     KuznyechikSerpentCamellia::KuznyechikSerpentCamellia ()
  343     {
  344         Ciphers.push_back (shared_ptr <Cipher> (new CipherCamellia ()));
  345         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  346         Ciphers.push_back (shared_ptr <Cipher> (new CipherKuznyechik ()));
  347 
  348         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  349     }
  350     
  351     // Camellia-Kuznyechik
  352     CamelliaKuznyechik::CamelliaKuznyechik ()
  353     {
  354         Ciphers.push_back (shared_ptr <Cipher> (new CipherKuznyechik ()));
  355         Ciphers.push_back (shared_ptr <Cipher> (new CipherCamellia ()));
  356 
  357         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  358     }
  359     
  360     // Camellia-Serpent
  361     CamelliaSerpent::CamelliaSerpent ()
  362     {
  363         Ciphers.push_back (shared_ptr <Cipher> (new CipherSerpent ()));
  364         Ciphers.push_back (shared_ptr <Cipher> (new CipherCamellia ()));
  365 
  366         SupportedModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  367     }
  368 }