"Fossies" - the Fresh Open Source Software Archive

Member "src/Volume/VolumeLayout.cpp" (10 Oct 2018, 10708 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 "VolumeLayout.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 "Volume/EncryptionMode.h"
   14 #include "Volume/EncryptionModeXTS.h"
   15 #include "VolumeLayout.h"
   16 #include "Boot/Windows/BootCommon.h"
   17 
   18 namespace VeraCrypt
   19 {
   20     VolumeLayout::VolumeLayout ()
   21     {
   22     }
   23 
   24     VolumeLayout::~VolumeLayout ()
   25     {
   26     }
   27 
   28     VolumeLayoutList VolumeLayout::GetAvailableLayouts (VolumeType::Enum type)
   29     {
   30         VolumeLayoutList layouts;
   31 
   32         layouts.push_back (shared_ptr <VolumeLayout> (new VolumeLayoutV2Normal ()));
   33         layouts.push_back (shared_ptr <VolumeLayout> (new VolumeLayoutV1Normal ()));
   34         layouts.push_back (shared_ptr <VolumeLayout> (new VolumeLayoutV2Hidden ()));
   35         layouts.push_back (shared_ptr <VolumeLayout> (new VolumeLayoutSystemEncryption ()));
   36 
   37         if (type != VolumeType::Unknown)
   38         {
   39             VolumeLayoutList l;
   40 
   41             foreach (shared_ptr <VolumeLayout> vl, layouts)
   42             {
   43                 if (vl->GetType() == type)
   44                     l.push_back (vl);
   45             }
   46 
   47             layouts = l;
   48         }
   49 
   50         return layouts;
   51     }
   52 
   53     shared_ptr <VolumeHeader> VolumeLayout::GetHeader ()
   54     {
   55         if (Header.get() == nullptr)
   56             Header.reset (new VolumeHeader (GetHeaderSize()));
   57 
   58         return Header;
   59     }
   60 
   61 
   62     VolumeLayoutV1Normal::VolumeLayoutV1Normal ()
   63     {
   64         Type = VolumeType::Normal;
   65         HeaderOffset = TC_VOLUME_HEADER_OFFSET;
   66         HeaderSize = TC_VOLUME_HEADER_SIZE_LEGACY;
   67 
   68         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AES ()));
   69         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ()));
   70         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ()));
   71         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Camellia ()));
   72         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ()));
   73         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ()));
   74         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ()));
   75         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ()));
   76         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ()));
   77 
   78         SupportedEncryptionModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
   79     }
   80 
   81     uint64 VolumeLayoutV1Normal::GetDataOffset (uint64 volumeHostSize) const
   82     {
   83         return HeaderSize;
   84     }
   85 
   86     uint64 VolumeLayoutV1Normal::GetDataSize (uint64 volumeHostSize) const
   87     {
   88         return volumeHostSize - GetHeaderSize();
   89     }
   90 
   91 
   92     VolumeLayoutV2Normal::VolumeLayoutV2Normal ()
   93     {
   94         Type = VolumeType::Normal;
   95         HeaderOffset = TC_VOLUME_HEADER_OFFSET;
   96         HeaderSize = TC_VOLUME_HEADER_SIZE;
   97         BackupHeaderOffset = -TC_VOLUME_HEADER_GROUP_SIZE;
   98 
   99         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AES ()));
  100         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ()));
  101         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ()));
  102         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Camellia ()));
  103         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new GOST89 ()));
  104         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Kuznyechik ()));
  105         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ()));
  106         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ()));
  107         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaKuznyechik ()));
  108         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaSerpent ()));
  109         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikAES ()));
  110         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikSerpentCamellia ()));
  111         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikTwofish ()));
  112         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ()));
  113         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ()));
  114         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ()));
  115 
  116         SupportedEncryptionModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  117     }
  118 
  119     uint64 VolumeLayoutV2Normal::GetDataOffset (uint64 volumeHostSize) const
  120     {
  121         return Header->GetEncryptedAreaStart();
  122     }
  123 
  124     uint64 VolumeLayoutV2Normal::GetDataSize (uint64 volumeHostSize) const
  125     {
  126         return Header->GetVolumeDataSize();
  127     }
  128 
  129     uint64 VolumeLayoutV2Normal::GetMaxDataSize (uint64 volumeSize) const
  130     {
  131         if (volumeSize < TC_TOTAL_VOLUME_HEADERS_SIZE)
  132             return 0;
  133 
  134         return volumeSize - TC_TOTAL_VOLUME_HEADERS_SIZE;
  135     }
  136 
  137 
  138     VolumeLayoutV2Hidden::VolumeLayoutV2Hidden ()
  139     {
  140         Type = VolumeType::Hidden;
  141         HeaderOffset = TC_HIDDEN_VOLUME_HEADER_OFFSET;
  142         HeaderSize = TC_VOLUME_HEADER_SIZE;
  143         BackupHeaderOffset = -TC_HIDDEN_VOLUME_HEADER_OFFSET;
  144 
  145         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AES ()));
  146         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ()));
  147         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ()));
  148         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Camellia ()));
  149         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new GOST89 ()));
  150         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Kuznyechik ()));
  151         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ()));
  152         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ()));
  153         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaKuznyechik ()));
  154         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaSerpent ()));
  155         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikAES ()));
  156         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikSerpentCamellia ()));
  157         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikTwofish ()));
  158         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ()));
  159         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ()));
  160         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ()));
  161 
  162         SupportedEncryptionModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  163     }
  164 
  165     uint64 VolumeLayoutV2Hidden::GetDataOffset (uint64 volumeHostSize) const
  166     {
  167         return Header->GetEncryptedAreaStart();
  168     }
  169 
  170     uint64 VolumeLayoutV2Hidden::GetDataSize (uint64 volumeHostSize) const
  171     {
  172         return Header->GetVolumeDataSize();
  173     }
  174 
  175     uint64 VolumeLayoutV2Hidden::GetMaxDataSize (uint64 volumeSize) const
  176     {
  177         // Reserve free space at the end of the host filesystem
  178         uint64 reservedSize;
  179 
  180         if (volumeSize < TC_VOLUME_SMALL_SIZE_THRESHOLD)
  181             reservedSize = TC_HIDDEN_VOLUME_HOST_FS_RESERVED_END_AREA_SIZE;
  182         else
  183             reservedSize = TC_HIDDEN_VOLUME_HOST_FS_RESERVED_END_AREA_SIZE_HIGH; // Ensure size of a hidden volume larger than TC_VOLUME_SMALL_SIZE_THRESHOLD is a multiple of the maximum supported sector size
  184 
  185         if (volumeSize < reservedSize)
  186             return 0;
  187 
  188         return volumeSize - reservedSize;
  189     }
  190 
  191 
  192     VolumeLayoutSystemEncryption::VolumeLayoutSystemEncryption ()
  193     {
  194         Type = VolumeType::Normal;
  195         HeaderOffset = TC_BOOT_VOLUME_HEADER_SECTOR_OFFSET;
  196         HeaderSize = TC_BOOT_ENCRYPTION_VOLUME_HEADER_SIZE;
  197 
  198         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AES ()));
  199         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Serpent ()));
  200         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Twofish ()));
  201         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Camellia ()));
  202         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new GOST89 ()));
  203         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new Kuznyechik ()));
  204         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofish ()));
  205         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new AESTwofishSerpent ()));
  206         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaKuznyechik ()));
  207         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new CamelliaSerpent ()));
  208         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikAES ()));
  209         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikSerpentCamellia ()));
  210         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new KuznyechikTwofish ()));
  211         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentAES ()));
  212         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new SerpentTwofishAES ()));
  213         SupportedEncryptionAlgorithms.push_back (shared_ptr <EncryptionAlgorithm> (new TwofishSerpent ()));
  214         
  215         SupportedEncryptionModes.push_back (shared_ptr <EncryptionMode> (new EncryptionModeXTS ()));
  216     }
  217 
  218     uint64 VolumeLayoutSystemEncryption::GetDataOffset (uint64 volumeHostSize) const
  219     {
  220         return 0;
  221     }
  222 
  223     uint64 VolumeLayoutSystemEncryption::GetDataSize (uint64 volumeHostSize) const
  224     {
  225         return volumeHostSize;
  226     }
  227 
  228     Pkcs5KdfList VolumeLayoutSystemEncryption::GetSupportedKeyDerivationFunctions (bool truecryptMode) const
  229     {
  230         Pkcs5KdfList l;
  231         if (!truecryptMode)
  232             l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha256_Boot ()));
  233         l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacRipemd160_1000 (truecryptMode)));
  234         if (!truecryptMode)
  235         {
  236             l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacSha512 (false)));
  237             l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacWhirlpool (false)));
  238             l.push_back (shared_ptr <Pkcs5Kdf> (new Pkcs5HmacStreebog ()));
  239         }
  240         return l;
  241     }
  242 }