"Fossies" - the Fresh Open Source Software Archive

Member "src/Common/Volumes.h" (10 Oct 2018, 8465 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 "Volumes.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18_Source_vs_1.19_Source.

    1 /*
    2  Legal Notice: Some portions of the source code contained in this file were
    3  derived from the source code of TrueCrypt 7.1a, which is 
    4  Copyright (c) 2003-2012 TrueCrypt Developers Association and which is 
    5  governed by the TrueCrypt License 3.0, also from the source code of
    6  Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux
    7  and which is governed by the 'License Agreement for Encryption for the Masses' 
    8  Modifications and additions to the original source code (contained in this file) 
    9  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
   10  and are governed by the Apache License 2.0 the full text of which is
   11  contained in the file License.txt included in VeraCrypt binary and source
   12  code distribution packages. */
   13 
   14 #ifndef TC_HEADER_Common_Volumes
   15 #define TC_HEADER_Common_Volumes
   16 
   17 #ifdef __cplusplus
   18 extern "C" {
   19 #endif
   20 
   21 // Volume header version
   22 #define VOLUME_HEADER_VERSION                   0x0005 
   23 
   24 // Version number written to volume header during format;
   25 // specifies the minimum program version required to mount the volume
   26 #define TC_VOLUME_MIN_REQUIRED_PROGRAM_VERSION  0x010b
   27 
   28 // Version number written (encrypted) to the key data area of an encrypted system partition/drive;
   29 // specifies the minimum program version required to decrypt the system partition/drive
   30 #define TC_SYSENC_KEYSCOPE_MIN_REQ_PROG_VERSION 0x010b
   31 
   32 // Current volume format version (created by TrueCrypt 6.0+)
   33 #define TC_VOLUME_FORMAT_VERSION                2
   34 
   35 // Version number of volume format created by TrueCrypt 1.0-5.1a
   36 #define TC_VOLUME_FORMAT_VERSION_PRE_6_0        1
   37 
   38 // Volume header sizes
   39 #define TC_VOLUME_HEADER_SIZE                   (64 * 1024L)
   40 #define TC_VOLUME_HEADER_EFFECTIVE_SIZE         512
   41 #define TC_BOOT_ENCRYPTION_VOLUME_HEADER_SIZE   512
   42 #define TC_VOLUME_HEADER_SIZE_LEGACY            512
   43 
   44 #define TC_VOLUME_HEADER_GROUP_SIZE             (2 * TC_VOLUME_HEADER_SIZE)
   45 #define TC_TOTAL_VOLUME_HEADERS_SIZE            (4 * TC_VOLUME_HEADER_SIZE)
   46 
   47 // Volume offsets
   48 #define TC_VOLUME_HEADER_OFFSET                 0
   49 #define TC_HIDDEN_VOLUME_HEADER_OFFSET          TC_VOLUME_HEADER_SIZE
   50 
   51 // Sector sizes
   52 #define TC_MIN_VOLUME_SECTOR_SIZE               512
   53 #define TC_MAX_VOLUME_SECTOR_SIZE               4096
   54 #define TC_SECTOR_SIZE_FILE_HOSTED_VOLUME       512
   55 #define TC_SECTOR_SIZE_LEGACY                   512
   56 
   57 // Sector size which can be safely assumed to be supported by all BIOSes
   58 #define TC_SECTOR_SIZE_BIOS                     512
   59 
   60 #define TC_VOLUME_SMALL_SIZE_THRESHOLD          (2 * BYTES_PER_MB)      // Volume sizes below this threshold are considered small
   61 
   62 #define TC_HIDDEN_VOLUME_HOST_FS_RESERVED_END_AREA_SIZE         TC_MAX_VOLUME_SECTOR_SIZE   // FAT file system fills the last sector with zeroes (marked as free; observed when quick format was performed using the OS format tool).
   63 #define TC_HIDDEN_VOLUME_HOST_FS_RESERVED_END_AREA_SIZE_HIGH    TC_VOLUME_HEADER_GROUP_SIZE // Reserved area size used for hidden volumes larger than TC_VOLUME_SMALL_SIZE_THRESHOLD
   64 
   65 #define TC_VOLUME_DATA_OFFSET                   TC_VOLUME_HEADER_GROUP_SIZE
   66 
   67 // The offset, in bytes, of the legacy hidden volume header position from the end of the file (a positive value).
   68 #define TC_HIDDEN_VOLUME_HEADER_OFFSET_LEGACY   (TC_VOLUME_HEADER_SIZE_LEGACY + TC_SECTOR_SIZE_LEGACY * 2)
   69 
   70 #define TC_MAX_128BIT_BLOCK_VOLUME_SIZE BYTES_PER_PB            // Security bound (128-bit block XTS mode)
   71 
   72 // Filesystem size limits
   73 #define TC_MIN_FAT_FS_SIZE              (9 * TC_MAX_VOLUME_SECTOR_SIZE)
   74 #define TC_MAX_FAT_SECTOR_COUNT         0x100000000ULL
   75 #define TC_MIN_NTFS_FS_SIZE             (884 * TC_MAX_VOLUME_SECTOR_SIZE)
   76 #define TC_MAX_NTFS_FS_SIZE             (128LL * BYTES_PER_TB)  // NTFS volume can theoretically be up to 16 exabytes, but Windows XP and 2003 limit the size to that addressable with 32-bit clusters, i.e. max size is 128 TB (if 64-KB clusters are used).
   77 #define TC_MAX_FAT_CLUSTER_SIZE         (256 * BYTES_PER_KB)    // Windows XP/Vista may crash when writing to a filesystem using clusters larger than 256 KB
   78 #define TC_MIN_EXFAT_FS_SIZE            (42 * TC_MAX_VOLUME_SECTOR_SIZE)
   79 #define TC_MAX_EXFAT_FS_SIZE            (128LL * BYTES_PER_PB)
   80 #define TC_MIN_REFS_FS_SIZE             (641 * BYTES_PER_MB) // minimal allowed volume size with 4KiB clusters. Determined empirically.
   81 #define TC_MAX_REFS_FS_SIZE             (0xFFFFFFFFFFFFFFFFULL) // real limit is 1 Yobibyte but it's too large for 64-bit integer
   82 
   83 // Volume size limits
   84 #define TC_MIN_VOLUME_SIZE              (TC_TOTAL_VOLUME_HEADERS_SIZE + TC_MIN_FAT_FS_SIZE)
   85 #define TC_MIN_VOLUME_SIZE_LEGACY       (37 * TC_SECTOR_SIZE_LEGACY)
   86 #define TC_MAX_VOLUME_SIZE_GENERAL      0x7fffFFFFffffFFFFLL    // Signed 64-bit integer file offset values
   87 #define TC_MAX_VOLUME_SIZE              TC_MAX_128BIT_BLOCK_VOLUME_SIZE
   88 
   89 #define TC_MIN_HIDDEN_VOLUME_SIZE       (TC_MIN_FAT_FS_SIZE + TC_HIDDEN_VOLUME_HOST_FS_RESERVED_END_AREA_SIZE)
   90 
   91 #define TC_MIN_HIDDEN_VOLUME_HOST_SIZE  (TC_MIN_VOLUME_SIZE + TC_MIN_HIDDEN_VOLUME_SIZE + 2 * TC_MAX_VOLUME_SECTOR_SIZE)
   92 #define TC_MAX_HIDDEN_VOLUME_HOST_SIZE  (TC_MAX_NTFS_FS_SIZE - TC_TOTAL_VOLUME_HEADERS_SIZE)
   93 
   94 #ifndef TC_NO_COMPILER_INT64
   95 #   if TC_MAX_VOLUME_SIZE > TC_MAX_VOLUME_SIZE_GENERAL
   96 #       error TC_MAX_VOLUME_SIZE > TC_MAX_VOLUME_SIZE_GENERAL
   97 #   endif
   98 #endif
   99 
  100 #define HEADER_ENCRYPTED_DATA_SIZE          (TC_VOLUME_HEADER_EFFECTIVE_SIZE - HEADER_ENCRYPTED_DATA_OFFSET)
  101 
  102 // Volume header field offsets
  103 #define HEADER_SALT_OFFSET                  0
  104 #define HEADER_ENCRYPTED_DATA_OFFSET        PKCS5_SALT_SIZE
  105 #define HEADER_MASTER_KEYDATA_OFFSET        256
  106     
  107 #define TC_HEADER_OFFSET_MAGIC                  64
  108 #define TC_HEADER_OFFSET_VERSION                68
  109 #define TC_HEADER_OFFSET_REQUIRED_VERSION       70
  110 #define TC_HEADER_OFFSET_KEY_AREA_CRC           72
  111 #define TC_HEADER_OFFSET_VOLUME_CREATION_TIME   76
  112 #define TC_HEADER_OFFSET_MODIFICATION_TIME      84
  113 #define TC_HEADER_OFFSET_HIDDEN_VOLUME_SIZE     92
  114 #define TC_HEADER_OFFSET_VOLUME_SIZE            100
  115 #define TC_HEADER_OFFSET_ENCRYPTED_AREA_START   108
  116 #define TC_HEADER_OFFSET_ENCRYPTED_AREA_LENGTH  116
  117 #define TC_HEADER_OFFSET_FLAGS                  124
  118 #define TC_HEADER_OFFSET_SECTOR_SIZE            128
  119 #define TC_HEADER_OFFSET_HEADER_CRC             252
  120 
  121 // Volume header flags
  122 #define TC_HEADER_FLAG_ENCRYPTED_SYSTEM         0x1
  123 #define TC_HEADER_FLAG_NONSYS_INPLACE_ENC       0x2     // The volume has been created (or is being encrypted/decrypted) using non-system in-place encryption
  124 
  125 
  126 #ifndef TC_HEADER_Volume_VolumeHeader
  127 
  128 #include "Password.h"
  129 
  130 extern BOOL ReadVolumeHeaderRecoveryMode;
  131 
  132 uint16 GetHeaderField16 (byte *header, int offset);
  133 uint32 GetHeaderField32 (byte *header, int offset);
  134 UINT64_STRUCT GetHeaderField64 (byte *header, int offset);
  135 #if defined(TC_WINDOWS_BOOT)
  136 int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int pim, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo);
  137 #elif defined(_UEFI)
  138 int ReadVolumeHeader(BOOL bBoot, char *encryptedHeader, Password *password, int pkcs5_prf, int pim, BOOL truecryptMode, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo);
  139 int CreateVolumeHeaderInMemory(BOOL bBoot, char *encryptedHeader, int ea, int mode, Password *password, int pkcs5_prf, int pim, char *masterKeydata, PCRYPTO_INFO *retInfo, unsigned __int64 volumeSize, unsigned __int64 hiddenVolumeSize, unsigned __int64 encryptedAreaStart, unsigned __int64 encryptedAreaLength, uint16 requiredProgramVersion, uint32 headerFlags, uint32 sectorSize, BOOL bWipeMode);
  140 BOOL RandgetBytes(unsigned char *buf, int len, BOOL forceSlowPoll);
  141 #else
  142 int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int pkcs5_prf, int pim, BOOL truecryptMode, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo);
  143 #if defined(_WIN32) && !defined(_UEFI)
  144 void ComputeBootloaderFingerprint (byte *bootLoaderBuf, unsigned int bootLoaderSize, byte* fingerprint);
  145 #endif
  146 #endif
  147 
  148 #if !defined (DEVICE_DRIVER) && !defined (TC_WINDOWS_BOOT) && !defined(_UEFI)
  149 int CreateVolumeHeaderInMemory (HWND hwndDlg, BOOL bBoot, char *encryptedHeader, int ea, int mode, Password *password, int pkcs5_prf, int pim, char *masterKeydata, PCRYPTO_INFO *retInfo, unsigned __int64 volumeSize, unsigned __int64 hiddenVolumeSize, unsigned __int64 encryptedAreaStart, unsigned __int64 encryptedAreaLength, uint16 requiredProgramVersion, uint32 headerFlags, uint32 sectorSize, BOOL bWipeMode);
  150 BOOL ReadEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header, DWORD *bytesRead);
  151 BOOL WriteEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header);
  152 int WriteRandomDataToReservedHeaderAreas (HWND hwndDlg, HANDLE dev, CRYPTO_INFO *cryptoInfo, uint64 dataAreaSize, BOOL bPrimaryOnly, BOOL bBackupOnly);
  153 #endif
  154 
  155 #endif // !TC_HEADER_Volume_VolumeHeader
  156 
  157 #ifdef __cplusplus
  158 }
  159 #endif
  160 
  161 #endif // TC_HEADER_Common_Volumes