/* --------------------------------------------------------------------------- Copyright (c) 1998-2007, Brian Gladman, Worcester, UK. All rights reserved. LICENSE TERMS The free distribution and use of this software is allowed (with or without changes) provided that: 1. source code distributions include the above copyright notice, this list of conditions and the following disclaimer; 2. binary distributions include the above copyright notice, this list of conditions and the following disclaimer in their documentation; 3. the name of the copyright holder is not used to endorse products built using this software without specific written permission. DISCLAIMER This software is provided 'as is' with no explicit or implied warranties in respect of its properties, including, but not limited to, correctness and/or fitness for purpose. --------------------------------------------------------------------------- Issue Date: 20/12/2007 This file contains the definitions required to use AES in C. See aesopt.h for optimisation details. */ /* Adapted for TrueCrypt */ #ifndef _AES_H #define _AES_H #include "Common/Tcdefs.h" #ifndef EXIT_SUCCESS #define EXIT_SUCCESS 0 #define EXIT_FAILURE 1 #endif #define INT_RETURN int #if defined(__cplusplus) extern "C" { #endif // #define AES_128 /* define if AES with 128 bit keys is needed */ // #define AES_192 /* define if AES with 192 bit keys is needed */ #define AES_256 /* define if AES with 256 bit keys is needed */ // #define AES_VAR /* define if a variable key size is needed */ // #define AES_MODES /* define if support is needed for modes */ /* The following must also be set in assembler files if being used */ #define AES_ENCRYPT /* if support for encryption is needed */ #define AES_DECRYPT /* if support for decryption is needed */ #define AES_ERR_CHK /* for parameter checks & error return codes */ #define AES_REV_DKS /* define to reverse decryption key schedule */ #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ #define N_COLS 4 /* the number of columns in the state */ /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ /* or 44, 52 or 60 32-bit words. */ #if defined( AES_VAR ) || defined( AES_256 ) #define KS_LENGTH 60 #elif defined( AES_192 ) #define KS_LENGTH 52 #else #define KS_LENGTH 44 #endif #if defined( AES_ERR_CHK ) #define AES_RETURN INT_RETURN #else #define AES_RETURN VOID_RETURN #endif /* the character array 'inf' in the following structures is used */ /* to hold AES context information. This AES code uses cx->inf.b[0] */ /* to hold the number of rounds multiplied by 16. The other three */ /* elements can be used by code that implements additional modes */ typedef union { uint_32t l; uint_8t b[4]; } aes_inf; typedef struct { uint_32t ks[KS_LENGTH]; aes_inf inf; } aes_encrypt_ctx; typedef struct { uint_32t ks[KS_LENGTH]; aes_inf inf; } aes_decrypt_ctx; /* This routine must be called before first use if non-static */ /* tables are being used */ AES_RETURN aes_init(void); /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ /* those in the range 128 <= key_len <= 256 are given in bits */ #if defined( AES_ENCRYPT ) #if defined(AES_128) || defined(AES_VAR) AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); #endif #if defined(AES_192) || defined(AES_VAR) AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); #endif #if defined(AES_256) || defined(AES_VAR) AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); #endif #if defined(AES_VAR) AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); #endif AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); #endif #if defined( AES_DECRYPT ) #if defined(AES_128) || defined(AES_VAR) AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); #endif #if defined(AES_192) || defined(AES_VAR) AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); #endif #if defined(AES_256) || defined(AES_VAR) AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); #endif #if defined(AES_VAR) AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); #endif AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); #endif #if defined(AES_MODES) /* Multiple calls to the following subroutines for multiple block */ /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */ /* long messages incremantally provided that the context AND the iv */ /* are preserved between all such calls. For the ECB and CBC modes */ /* each individual call within a series of incremental calls must */ /* process only full blocks (i.e. len must be a multiple of 16) but */ /* the CFB, OFB and CTR mode calls can handle multiple incremental */ /* calls of any length. Each mode is reset when a new AES key is */ /* set but ECB and CBC operations can be reset without setting a */ /* new key by setting a new IV value. To reset CFB, OFB and CTR */ /* without setting the key, aes_mode_reset() must be called and the */ /* IV must be set. NOTE: All these calls update the IV on exit so */ /* this has to be reset if a new operation with the same IV as the */ /* previous one is required (or decryption follows encryption with */ /* the same IV array). */ AES_RETURN aes_test_alignment_detection(unsigned int n); AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, const aes_encrypt_ctx cx[1]); AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, const aes_decrypt_ctx cx[1]); AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, const aes_encrypt_ctx cx[1]); AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, const aes_decrypt_ctx cx[1]); AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]); AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1]); AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1]); #define aes_ofb_encrypt aes_ofb_crypt #define aes_ofb_decrypt aes_ofb_crypt AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *iv, aes_encrypt_ctx cx[1]); typedef void cbuf_inc(unsigned char *cbuf); #define aes_ctr_encrypt aes_ctr_crypt #define aes_ctr_decrypt aes_ctr_crypt AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf, int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]); #endif #if defined(__cplusplus) } #endif #endif n118' href='#n118'>118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/*
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-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.
*/
#ifndef TC_HEADER_Boot_BootCommon
#define TC_HEADER_Boot_BootCommon
#include "Common/Password.h"
#include "BootDefs.h"
// The user will be advised to upgrade the rescue disk if upgrading from the following or any previous version
#define TC_RESCUE_DISK_UPGRADE_NOTICE_MAX_VERSION 0x0121
#define TC_BOOT_LOADER_AREA_SIZE (TC_BOOT_LOADER_AREA_SECTOR_COUNT * TC_SECTOR_SIZE_BIOS)
#define TC_BOOT_VOLUME_HEADER_SECTOR (TC_BOOT_LOADER_AREA_SECTOR_COUNT - 1)
#define TC_BOOT_VOLUME_HEADER_SECTOR_OFFSET (TC_BOOT_VOLUME_HEADER_SECTOR * TC_SECTOR_SIZE_BIOS)
#define TC_CD_BOOTSECTOR_OFFSET 0xd000
#define TC_CD_BOOT_LOADER_SECTOR 26
#define TC_ORIG_BOOT_LOADER_BACKUP_SECTOR TC_BOOT_LOADER_AREA_SECTOR_COUNT
#define TC_ORIG_BOOT_LOADER_BACKUP_SECTOR_OFFSET (TC_ORIG_BOOT_LOADER_BACKUP_SECTOR * TC_SECTOR_SIZE_BIOS)
#define TC_BOOT_LOADER_BACKUP_RESCUE_DISK_SECTOR (TC_ORIG_BOOT_LOADER_BACKUP_SECTOR + TC_BOOT_LOADER_AREA_SECTOR_COUNT)
#define TC_BOOT_LOADER_BACKUP_RESCUE_DISK_SECTOR_OFFSET (TC_BOOT_LOADER_BACKUP_RESCUE_DISK_SECTOR * TC_SECTOR_SIZE_BIOS)
#define TC_MBR_SECTOR 0
#define TC_MAX_MBR_BOOT_CODE_SIZE 440
#define TC_MAX_EXTRA_BOOT_PARTITION_SIZE (512UL * 1024UL * 1024UL)
#pragma pack (1)
typedef struct
{
byte Flags;
} BootSectorConfiguration;
// Modifying this value can introduce incompatibility with previous versions
#define TC_BOOT_LOADER_ARGS_OFFSET 0x10
typedef struct
{
// Modifying this structure can introduce incompatibility with previous versions
char Signature[8];
uint16 BootLoaderVersion;
uint16 CryptoInfoOffset;
uint16 CryptoInfoLength;
uint32 HeaderSaltCrc32;
Password BootPassword;
uint64 HiddenSystemPartitionStart;
uint64 DecoySystemPartitionStart;
uint32 Flags;
uint32 BootDriveSignature;
uint32 BootArgumentsCrc32;
} BootArguments;
// Modifying these values can introduce incompatibility with previous versions
#define TC_BOOT_ARGS_FLAG_EXTRA_BOOT_PARTITION 0x1
#pragma pack ()
// Boot arguments signature should not be defined as a static string
// Modifying these values can introduce incompatibility with previous versions
#define TC_SET_BOOT_ARGUMENTS_SIGNATURE(SG) do { SG[0] = 'T'; SG[1] = 'R'; SG[2] = 'U'; SG[3] = 'E'; SG[4] = 0x11; SG[5] = 0x23; SG[6] = 0x45; SG[7] = 0x66; } while (FALSE)
#define TC_IS_BOOT_ARGUMENTS_SIGNATURE(SG) (SG[0] == 'T' && SG[1] == 'R' && SG[2] == 'U' && SG[3] == 'E' && SG[4] == 0x11 && SG[5] == 0x23 && SG[6] == 0x45 && SG[7] == 0x66)
#if defined(_MSC_VER) && !defined(TC_WINDOWS_BOOT)
#define DCS_DISK_ENTRY_LIST_HEADER_SIGN SIGNATURE_64 ('D','C','S','D','E','L','S','T')
#ifndef CSTATIC_ASSERT
#define CSTATIC_ASSERT(b, name) typedef int StaticAssertFailed##name[b ? 1 : -1];
#endif
#define DE_IDX_CRYPTOHEADER 0
#define DE_IDX_LIST 1
#define DE_IDX_DISKID 2
#define DE_IDX_MAINGPTHDR 3
#define DE_IDX_MAINGPTENTRYS 4
#define DE_IDX_ALTGPTHDR 5
#define DE_IDX_ALTGPTENTRYS 6
#define DE_IDX_EXEC 7
#define DE_IDX_PWDCACHE 8
#define DE_IDX_RND 9
#define DE_IDX_TOTAL 10
CSTATIC_ASSERT(DE_IDX_TOTAL <= 15, DE_IDX_TOTAL_too_big);
enum DcsDiskEntryTypes {
DE_Unused = 0,
DE_Sectors,
DE_List,
DE_DISKID,
DE_ExecParams,
DE_PwdCache,
DE_Rnd
};
#pragma pack(1)
typedef struct _SECREGION_BOOT_PARAMS {
uint64 Ptr;
uint32 Size;
uint32 Crc;
} SECREGION_BOOT_PARAMS;
typedef struct {
uint32 Data1;
uint16 Data2;
uint16 Data3;
byte Data4[8];
} DCS_GUID;
// DE types
typedef struct _DCS_DISK_ENTRY_SECTORS {
uint32 Type;
uint32 Offset; // Offset in memory
uint64 Reserved;
uint64 Start; // Start on disk (byte)
uint64 Length; // length on disk (byte)
} DCS_DISK_ENTRY_SECTORS;
CSTATIC_ASSERT(sizeof(DCS_DISK_ENTRY_SECTORS) == 32, Wrong_size_DCS_DISK_ENTRY_SECTORS);
typedef struct _DCS_DISK_ENTRY_PARAMS {
uint32 Type;
uint32 Offset;
uint64 Reserved[2];
uint64 Length; // size of data
} DCS_DISK_ENTRY_PARAMS;
CSTATIC_ASSERT(sizeof(DCS_DISK_ENTRY_PARAMS) == 32, Wrong_size_DCS_DISK_ENTRY_PARAMS);
typedef struct _DCS_DISK_ENTRY_DISKID {
uint32 Type;
uint32 MbrID;
uint64 ReservedDiskId;
DCS_GUID GptID;
} DCS_DISK_ENTRY_DISKID;
CSTATIC_ASSERT(sizeof(DCS_DISK_ENTRY_DISKID) == 32, Wrong_size_DCS_DISK_ENTRY_DISKID);
#pragma warning(disable:4201)
typedef struct _DCS_DISK_ENTRY {
union {
struct {
uint32 Type;
uint32 Offset;
byte reserved[16];
uint64 Length; // size of structure at Offset
};
DCS_DISK_ENTRY_SECTORS Sectors;
DCS_DISK_ENTRY_DISKID DiskId;
DCS_DISK_ENTRY_PARAMS Prm;
};
} DCS_DISK_ENTRY;
#pragma warning(default:4201)
CSTATIC_ASSERT(sizeof(DCS_DISK_ENTRY) == 32, Wrong_size_DCS_DISK_ENTRY);
// Static compile time checks field offsets
#ifndef FIELD_OFFSET
#define FIELD_OFFSET(t, f) ((UINTN)(&((t*)0)->f))
#endif
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Type) == FIELD_OFFSET(DCS_DISK_ENTRY_SECTORS, Type), Wrong_Type_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Type) == FIELD_OFFSET(DCS_DISK_ENTRY_DISKID, Type), Wrong_Type_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Type) == FIELD_OFFSET(DCS_DISK_ENTRY_PARAMS, Type), Wrong_Type_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Length) == FIELD_OFFSET(DCS_DISK_ENTRY_SECTORS, Length), Wrong_Length_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Length) == FIELD_OFFSET(DCS_DISK_ENTRY_PARAMS, Length), Wrong_Length_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Offset) == FIELD_OFFSET(DCS_DISK_ENTRY_SECTORS, Offset), Wrong_Offset_offset);
CSTATIC_ASSERT(FIELD_OFFSET(DCS_DISK_ENTRY, Offset) == FIELD_OFFSET(DCS_DISK_ENTRY_PARAMS, Offset), Wrong_Offset_offset);
// DE type specific data
// DE List
typedef struct _DCS_DISK_ENTRY_LIST {
// EFI_TABLE_HEADER
uint64 Signature;
uint32 Revision;
uint32 HeaderSize; //< The size, in bytes, of the entire table including the EFI_TABLE_HEADER.
uint32 CRC32; //< The 32-bit CRC for the entire table. This value is computed by setting this field to 0, and computing the 32-bit CRC for HeaderSize bytes.
uint32 Reserved; //< Reserved field that must be set to 0.
//
uint32 Count;
uint32 DataSize;
//
DCS_DISK_ENTRY DE[15];
} DCS_DISK_ENTRY_LIST;
CSTATIC_ASSERT(sizeof(DCS_DISK_ENTRY_LIST) == 512, Wrong_size_DCS_DISK_ENTRY_LIST);
typedef struct _DCS_DEP_EXEC {
DCS_GUID ExecPartGuid;
uint16 ExecCmd[248];
} DCS_DEP_EXEC;
CSTATIC_ASSERT(sizeof(DCS_DEP_EXEC) == 512, Wrong_size_DCS_DEP_EXEC);
#define DCS_DEP_PWD_CACHE_SIGN SIGNATURE_64 ('P','W','D','C','A','C','H','E')
typedef struct _DCS_DEP_PWD_CACHE {
uint64 Sign;
uint32 CRC;
uint32 Count;
Password Pwd[4];
int32 Pim[4];
byte pad[512 - 8 - 4 - 4 - (sizeof(Password) + 4) * 4];
} DCS_DEP_PWD_CACHE;
CSTATIC_ASSERT(sizeof(DCS_DEP_PWD_CACHE) == 512, Wrong_size_DCS_DEP_PWD_CACHE);
#pragma pack()
#endif // #if !defined(TC_WINDOWS_BOOT)
#endif // TC_HEADER_Boot_BootCommon