/* Legal Notice: Some portions of the source code contained in this file were derived from the source code of TrueCrypt 7.1a, which is Copyright (c) 2003-2012 TrueCrypt Developers Association and which is governed by the TrueCrypt License 3.0, also from the source code of Encryption for the Masses 2.02a, which is Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License Agreement for Encryption for the Masses' Modifications and additions to the original source code (contained in this file) and all other portions of this file are Copyright (c) 2013-2017 IDRIX and are governed by the Apache License 2.0 the full text of which is contained in the file License.txt included in VeraCrypt binary and source code distribution packages. */ /* Update the following when adding a new cipher or EA: Crypto.h: ID #define MAX_EXPANDED_KEY #define Crypto.c: Ciphers[] EncryptionAlgorithms[] CipherInit() EncipherBlock() DecipherBlock() */ #ifndef CRYPTO_H #define CRYPTO_H #include "Tcdefs.h" #ifdef __cplusplus extern "C" { #endif // Encryption data unit size, which may differ from the sector size and must always be 512 #define ENCRYPTION_DATA_UNIT_SIZE 512 // Size of the salt (in bytes) #define PKCS5_SALT_SIZE 64 // Size of the volume header area containing concatenated master key(s) and secondary key(s) (XTS mode) #define MASTER_KEYDATA_SIZE 256 // The first PRF to try when mounting #define FIRST_PRF_ID 1 // Hash algorithms (pseudorandom functions). enum { SHA512 = FIRST_PRF_ID, WHIRLPOOL, SHA256, RIPEMD160, STREEBOG, HASH_ENUM_END_ID }; // The last PRF to try when mounting and also the number of implemented PRFs #define LAST_PRF_ID (HASH_ENUM_END_ID - 1) #define RIPEMD160_BLOCKSIZE 64 #define RIPEMD160_DIGESTSIZE 20 #define SHA256_BLOCKSIZE 64 #define SHA256_DIGESTSIZE 32 #define SHA512_BLOCKSIZE 128 #define SHA512_DIGESTSIZE 64 #define WHIRLPOOL_BLOCKSIZE 64 #define WHIRLPOOL_DIGESTSIZE 64 #define STREEBOG_BLOCKSIZE 64 #define STREEBOG_DIGESTSIZE 64 #define MAX_DIGESTSIZE WHIRLPOOL_DIGESTSIZE #define DEFAULT_HASH_ALGORITHM FIRST_PRF_ID #define DEFAULT_HASH_ALGORITHM_BOOT SHA256 // The mode of operation used for newly created volumes and first to try when mounting #define FIRST_MODE_OF_OPERATION_ID 1 // Modes of operation enum { /* If you add/remove a mode, update the following: GetMaxPkcs5OutSize(), EAInitMode() */ XTS = FIRST_MODE_OF_OPERATION_ID, MODE_ENUM_END_ID }; // The last mode of operation to try when mounting and also the number of implemented modes #define LAST_MODE_OF_OPERATION (MODE_ENUM_END_ID - 1) // Ciphertext/plaintext block size for XTS mode (in bytes) #define BYTES_PER_XTS_BLOCK 16 // Number of ciphertext/plaintext blocks per XTS data unit #define BLOCKS_PER_XTS_DATA_UNIT (ENCRYPTION_DATA_UNIT_SIZE / BYTES_PER_XTS_BLOCK) // Cipher IDs enum { NONE = 0, AES, SERPENT, TWOFISH, CAMELLIA, GOST89, KUZNYECHIK }; typedef struct { int Id; // Cipher ID #ifdef TC_WINDOWS_BOOT char *Name; // Name #else wchar_t *Name; // Name #endif int BlockSize; // Block size (bytes) int KeySize; // Key size (bytes) int KeyScheduleSize; // Scheduled key size (bytes) } Cipher; typedef struct { int Ciphers[4]; // Null terminated array of ciphers used by encryption algorithm int Modes[LAST_MODE_OF_OPERATION + 1]; // Null terminated array of modes of operation #ifndef TC_WINDOWS_BOOT BOOL MbrSysEncEnabled; #endif int FormatEnabled; } EncryptionAlgorithm; #ifndef TC_WINDOWS_BOOT typedef struct { int Id; // Hash ID wchar_t *Name; // Name BOOL Deprecated; BOOL SystemEncryption; // Available for system encryption } Hash; #endif // Maxium length of scheduled key #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES) # define AES_KS (sizeof(aes_encrypt_ctx) + sizeof(aes_decrypt_ctx)) #else # define AES_KS (sizeof(aes_context)) #endif #define SERPENT_KS (140 * 4) #ifdef TC_WINDOWS_BOOT_SINGLE_CIPHER_MODE # ifdef TC_WINDOWS_BOOT_AES # define MAX_EXPANDED_KEY AES_KS # elif defined (TC_WINDOWS_BOOT_SERPENT) # define MAX_EXPANDED_KEY SERPENT_KS # elif defined (TC_WINDOWS_BOOT_TWOFISH) # define MAX_EXPANDED_KEY TWOFISH_KS # elif defined (TC_WINDOWS_BOOT_CAMELLIA) # define MAX_EXPANDED_KEY CAMELLIA_KS # endif #else #ifdef TC_WINDOWS_BOOT #define MAX_EXPANDED_KEY VC_MAX((AES_KS + SERPENT_KS + TWOFISH_KS), CAMELLIA_KS) #else #define MAX_EXPANDED_KEY VC_MAX(VC_MAX(VC_MAX(VC_MAX((AES_KS + SERPENT_KS + TWOFISH_KS), GOST_KS), CAMELLIA_KS + KUZNYECHIK_KS + SERPENT_KS), KUZNYECHIK_KS + TWOFISH_KS), AES_KS + KUZNYECHIK_KS) #endif #endif #ifdef DEBUG # define PRAND_DISK_WIPE_PASSES 3 #else # define PRAND_DISK_WIPE_PASSES 256 #endif /* specific value for volume header wipe used only when drive is fully wiped. */ #define PRAND_HEADER_WIPE_PASSES 3 #if !defined (TC_WINDOWS_BOOT) || defined (TC_WINDOWS_BOOT_AES) # include "Aes.h" #else # include "AesSmall.h" #endif #include "Aes_hw_cpu.h" #if !defined (TC_WINDOWS_BOOT) && !defined (_UEFI) # include "SerpentFast.h" #else # include "Serpent.h" #endif #include "Twofish.h" #include "Rmd160.h" #ifndef TC_WINDOWS_BOOT # include "Sha2.h" # include "Whirlpool.h" # include "Streebog.h" # include "GostCipher.h" # include "kuznyechik.h" # include "Camellia.h" #if !defined (_UEFI) # include "chachaRng.h" # ifdef _WIN64 # include "t1ha.h" # endif #endif #else # include "CamelliaSmall.h" #endif #include "GfMul.h" #include "Password.h" #ifndef TC_WINDOWS_BOOT #include "config.h" typedef struct keyInfo_t { int noIterations; /* Number of times to iterate (PKCS-5) */ int keyLength; /* Length of the key */ uint64 dummy; /* Dummy field to ensure 16-byte alignment of this structure */ __int8 salt[PKCS5_SALT_SIZE]; /* PKCS-5 salt */ CRYPTOPP_ALIGN_DATA(16) __int8 master_keydata[MASTER_KEYDATA_SIZE]; /* Concatenated master primary and secondary key(s) (XTS mode). For LRW (deprecated/legacy), it contains the tweak key before the master key(s). For CBC (deprecated/legacy), it contains the IV seed before the master key(s). */ CRYPTOPP_ALIGN_DATA(16) __int8 userKey[MAX_PASSWORD]; /* Password (to which keyfiles may have been applied). WITHOUT +1 for the null terminator. */ } KEY_INFO, *PKEY_INFO; #endif typedef struct CRYPTO_INFO_t { int ea; /* Encryption algorithm ID */ int mode; /* Mode of operation (e.g., XTS) */ int pkcs5; /* PRF algorithm */ unsigned __int8 ks[MAX_EXPANDED_KEY]; /* Primary key schedule (if it is a cascade, it conatins multiple concatenated keys) */ unsigned __int8 ks2[MAX_EXPANDED_KEY]; /* Secondary key schedule (if cascade, multiple concatenated) for XTS mode. */ BOOL hiddenVolume; // Indicates whether the volume is mounted/mountable as hidden volume #ifndef TC_WINDOWS_BOOT uint16 HeaderVersion; #ifdef TC_WINDOWS_DRIVER unsigned __int8 master_keydata_hash[RIPEMD160_DIGESTSIZE]; #else CRYPTOPP_ALIGN_DATA(16) unsigned __int8 master_keydata[MASTER_KEYDATA_SIZE]; /* This holds the volume header area containing concatenated master key(s) and secondary key(s) (XTS mode). For LRW (deprecated/legacy), it contains the tweak key before the master key(s). For CBC (deprecated/legacy), it contains the IV seed before the master key(s). */ CRYPTOPP_ALIGN_DATA(16) unsigned __int8 k2[MASTER_KEYDATA_SIZE]; /* For XTS, this contains the secondary key (if cascade, multiple concatenated). For LRW (deprecated/legacy), it contains the tweak key. For CBC (deprecated/legacy), it contains the IV seed. */ #endif int noIterations; BOOL bTrueCryptMode; int volumePim; BOOL bProtectHiddenVolume; // Indicates whether the volume contains a hidden volume to be protected against overwriting BOOL bHiddenVolProtectionAction; // TRUE if a write operation has been denied by the driver in order to prevent the hidden volume from being overwritten (set to FALSE upon volume mount). uint64 volDataAreaOffset; // Absolut
/*
Derived from source code of TrueCrypt 7.1a, which is
Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
by the TrueCrypt License 3.0.
Modifications and additions to the original source code (contained in this file)
and all other portions of this file are Copyright (c) 2013-2015 IDRIX
and are governed by the Apache License 2.0 the full text of which is
contained in the file License.txt included in VeraCrypt binary and source
code distribution packages.
*/
#ifndef TC_HEADER_Volume_VolumeLayout
#define TC_HEADER_Volume_VolumeLayout
#include "Platform/Platform.h"
#include "Volume/EncryptionAlgorithm.h"
#include "Volume/EncryptionMode.h"
#include "Volume/Pkcs5Kdf.h"
#include "VolumeHeader.h"
namespace VeraCrypt
{
class VolumeLayout;
typedef list < shared_ptr <VolumeLayout> > VolumeLayoutList;
class VolumeLayout
{
public:
virtual ~VolumeLayout ();
static VolumeLayoutList GetAvailableLayouts (VolumeType::Enum type = VolumeType::Unknown);
virtual int GetBackupHeaderOffset () const { return BackupHeaderOffset; } // Positive value: offset from the start of host, negative: offset from the end
virtual uint64 GetDataOffset (uint64 volumeHostSize) const = 0;
virtual uint64 GetDataSize (uint64 volumeHostSize) const = 0;
virtual shared_ptr <VolumeHeader> GetHeader ();
virtual int GetHeaderOffset () const { return HeaderOffset; } // Positive value: offset from the start of host, negative: offset from the end
virtual uint32 GetHeaderSize () const { return HeaderSize; }
virtual uint64 GetMaxDataSize (uint64 volumeSize) const = 0;
virtual EncryptionAlgorithmList GetSupportedEncryptionAlgorithms () const { return SupportedEncryptionAlgorithms; }
virtual Pkcs5KdfList GetSupportedKeyDerivationFunctions (bool truecryptMode) const { return Pkcs5Kdf::GetAvailableAlgorithms(truecryptMode); }
virtual EncryptionModeList GetSupportedEncryptionModes () const { return SupportedEncryptionModes; }
virtual VolumeType::Enum GetType () const { return Type; }
virtual bool HasBackupHeader () const = 0;
virtual bool HasDriveHeader () const { return false; }
virtual void SetHeader (shared_ptr <VolumeHeader> header) { Header = header; }
protected:
VolumeLayout ();
EncryptionAlgorithmList SupportedEncryptionAlgorithms;
EncryptionModeList SupportedEncryptionModes;
int BackupHeaderOffset;
int HeaderOffset;
uint32 HeaderSize;
VolumeType::Enum Type;
shared_ptr <VolumeHeader> Header;
private:
VolumeLayout (const VolumeLayout &);
VolumeLayout &operator= (const VolumeLayout &);
};
class VolumeLayoutV1Normal : public VolumeLayout
{
public:
VolumeLayoutV1Normal ();
virtual ~VolumeLayoutV1Normal () { }
virtual int GetBackupHeaderOffset () const { throw NotApplicable (SRC_POS); }
virtual uint64 GetDataOffset (uint64 volumeHostSize) const;
virtual uint64 GetDataSize (uint64 volumeHostSize) const;
virtual uint64 GetMaxDataSize (uint64 volumeSize) const { throw NotApplicable (SRC_POS); }
virtual bool HasBackupHeader () const { return false; }
private:
VolumeLayoutV1Normal (const VolumeLayoutV1Normal &);
VolumeLayoutV1Normal &operator= (const VolumeLayoutV1Normal &);
};
class VolumeLayoutV2Normal : public VolumeLayout
{
public:
VolumeLayoutV2Normal ();
virtual ~VolumeLayoutV2Normal () { }
virtual uint64 GetDataOffset (uint64 volumeHostSize) const;
virtual uint64 GetDataSize (uint64 volumeHostSize) const;
virtual uint64 GetMaxDataSize (uint64 volumeSize) const;
virtual bool HasBackupHeader () const { return true; }
private:
VolumeLayoutV2Normal (const VolumeLayoutV2Normal &);
VolumeLayoutV2Normal &operator= (const VolumeLayoutV2Normal &);
};
class VolumeLayoutV2Hidden : public VolumeLayout
{
public:
VolumeLayoutV2Hidden ();
virtual ~VolumeLayoutV2Hidden () { }
virtual uint64 GetDataOffset (uint64 volumeHostSize) const;
virtual uint64 GetDataSize (uint64 volumeHostSize) const;
virtual uint64 GetMaxDataSize (uint64 volumeSize) const;
virtual bool HasBackupHeader () const { return true; }
private:
VolumeLayoutV2Hidden (const VolumeLayoutV2Hidden &);
VolumeLayoutV2Hidden &operator= (const VolumeLayoutV2Hidden &);
};
class VolumeLayoutSystemEncryption : public VolumeLayout
{
public:
VolumeLayoutSystemEncryption ();
virtual ~VolumeLayoutSystemEncryption () { }
virtual int GetBackupHeaderOffset () const { throw NotApplicable (SRC_POS); }
virtual uint64 GetDataOffset (uint64 volumeHostSize) const;
virtual uint64 GetDataSize (uint64 volumeHostSize) const;
virtual uint64 GetMaxDataSize (uint64 volumeSize) const { throw NotApplicable (SRC_POS); }
virtual Pkcs5KdfList GetSupportedKeyDerivationFunctions (bool truecryptMode) const;
virtual bool HasBackupHeader () const { return false; }
virtual bool HasDriveHeader () const { return true; }
private:
VolumeLayoutSystemEncryption (const VolumeLayoutSystemEncryption &);
VolumeLayoutSystemEncryption &operator= (const VolumeLayoutSystemEncryption &);
};
}
#endif // TC_HEADER_Volume_VolumeLayout