"Fossies" - the Fresh Open Source Software Archive

Member "VeraCrypt_1.24-Update6/src/Common/Tcdefs.h" (11 Mar 2020, 14251 Bytes) of package /linux/misc/VeraCrypt_1.24-Update6.tar.gz:


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 "Tcdefs.h" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.24-Update5_vs_1.24-Update6.

    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 TCDEFS_H
   15 #define TCDEFS_H
   16 
   17 #if defined(_UEFI)
   18 #undef  _WIN32
   19 #undef  _WIN64
   20 #undef  _DEBUG
   21 
   22 #include <Uefi.h> 
   23 #include <Library/BaseLib.h> 
   24 #include <Library/BaseMemoryLib.h>
   25 
   26 void* VeraCryptMemAlloc(IN UINTN size);
   27 void VeraCryptMemFree(IN VOID* ptr);
   28 
   29 #define BOOL int
   30 #ifndef FALSE
   31 #define FALSE 0
   32 #define TRUE 1
   33 #endif
   34 
   35 #define max(a,b)    ((a)>(b))?(a):(b)
   36 #define min(a,b)    ((a)<(b))?(a):(b)
   37 
   38 #ifdef  __cplusplus
   39 extern "C" {
   40 #endif
   41 extern unsigned __int64 __cdecl _rotl64(unsigned __int64,int);
   42 extern unsigned __int64 __cdecl _rotr64(unsigned __int64,int);
   43 extern unsigned int __cdecl _rotl(unsigned int,int);
   44 extern unsigned int __cdecl _rotr(unsigned int,int);
   45 extern unsigned char _rotr8(unsigned char value, unsigned char shift);
   46 extern unsigned short _rotr16(unsigned short value, unsigned char shift);
   47 extern unsigned char _rotl8(unsigned char value, unsigned char shift);
   48 extern unsigned short _rotl16(unsigned short value, unsigned char shift);
   49 #ifdef  __cplusplus
   50 }
   51 #endif
   52 
   53 #endif // defined(_UEFI)
   54 
   55 #define TC_APP_NAME                     "VeraCrypt"
   56 
   57 // Version displayed to user 
   58 #define VERSION_STRING                  "1.24-Update6"
   59 
   60 #ifdef VC_EFI_CUSTOM_MODE
   61 #define VERSION_STRING_SUFFIX           "-CustomEFI"
   62 #else
   63 #define VERSION_STRING_SUFFIX           ""
   64 #endif
   65 
   66 // Version number to compare against driver
   67 #define VERSION_NUM                     0x0124
   68 
   69 // Release date
   70 #define TC_STR_RELEASE_DATE         L"March 10, 2020"
   71 #define TC_RELEASE_DATE_YEAR            2020
   72 #define TC_RELEASE_DATE_MONTH            3
   73 
   74 #define BYTES_PER_KB                    1024LL
   75 #define BYTES_PER_MB                    1048576LL
   76 #define BYTES_PER_GB                    1073741824LL
   77 #define BYTES_PER_TB                    1099511627776LL
   78 #define BYTES_PER_PB                    1125899906842624LL
   79 
   80 /* GUI/driver errors */
   81 
   82 #define WIDE(x) (LPWSTR)L##x
   83 
   84 #define VC_MAX(a,b) ((a)>(b))?(a):(b)
   85 #define VC_MIN(a,b) ((a)<(b))?(a):(b)
   86 
   87 #ifdef _MSC_VER
   88 
   89 typedef __int8 int8;
   90 typedef __int16 int16;
   91 typedef __int32 int32;
   92 typedef unsigned __int8 byte;
   93 typedef unsigned __int16 uint16;
   94 typedef unsigned __int32 uint32;
   95 
   96 #ifdef TC_NO_COMPILER_INT64
   97 typedef unsigned __int32    TC_LARGEST_COMPILER_UINT;
   98 #else
   99 typedef unsigned __int64    TC_LARGEST_COMPILER_UINT;
  100 typedef __int64 int64;
  101 typedef unsigned __int64 uint64;
  102 #define LL(x) x##ui64
  103 #endif
  104 
  105 #pragma warning( disable : 4201 )  // disable: 4201 nonstandard extension used : nameless struct/union
  106 #pragma warning( disable : 4324 )  // disable: 4324 structure was padded due to __declspec(align())
  107 
  108 #else // !_MSC_VER
  109 
  110 #include <inttypes.h>
  111 #include <limits.h>
  112 #include <memory.h>
  113 
  114 typedef int8_t int8;
  115 typedef int16_t int16;
  116 typedef int32_t int32;
  117 typedef int64_t int64;
  118 typedef uint8_t byte;
  119 typedef uint16_t uint16;
  120 typedef uint32_t uint32;
  121 typedef uint64_t uint64;
  122 
  123 #define LL(x) x##ULL
  124 
  125 #if UCHAR_MAX != 0xffU
  126 #error UCHAR_MAX != 0xff
  127 #endif
  128 #define __int8 char
  129 
  130 #if USHRT_MAX != 0xffffU
  131 #error USHRT_MAX != 0xffff
  132 #endif
  133 #define __int16 short
  134 
  135 #if UINT_MAX != 0xffffffffU
  136 #error UINT_MAX != 0xffffffff
  137 #endif
  138 #define __int32 int
  139 
  140 typedef uint64 TC_LARGEST_COMPILER_UINT;
  141 
  142 #define BOOL int
  143 #ifndef FALSE
  144 #define FALSE 0
  145 #define TRUE 1
  146 #endif
  147 
  148 #endif // !_MSC_VER
  149 
  150 #define TC_INT_TYPES_DEFINED
  151 
  152 // Integer types required by Cryptolib
  153 typedef unsigned __int8 uint_8t;
  154 typedef unsigned __int16 uint_16t;
  155 typedef unsigned __int32 uint_32t;
  156 #ifndef TC_NO_COMPILER_INT64
  157 typedef uint64 uint_64t;
  158 #endif
  159 
  160 typedef union
  161 {
  162     struct
  163     {
  164         unsigned __int32 LowPart;
  165         unsigned __int32 HighPart;
  166     };
  167 #ifndef TC_NO_COMPILER_INT64
  168     uint64 Value;
  169 #endif
  170 
  171 } UINT64_STRUCT;
  172 
  173 #ifndef __has_builtin       // Optional of course
  174 #define __has_builtin(x) 0  // Compatibility with non-clang compilers
  175 #endif
  176 
  177 #if defined(_UEFI)
  178 typedef UINTN size_t;
  179 typedef uint64 uint_64t;
  180 typedef CHAR16 wchar_t;
  181 typedef int LONG;
  182 
  183 #define wcscpy StrCpy
  184 #define wcslen StrLen
  185 #define wcscmp StrCmp
  186 #define wcscat StrCat
  187 
  188 #define memcpy(dest,source,count)         CopyMem(dest,source,(UINTN)(count))
  189 #define memset(dest,ch,count)             SetMem(dest,(UINTN)(count),(UINT8)(ch))
  190 #define memchr(buf,ch,count)              ScanMem8(buf,(UINTN)(count),(UINT8)ch)
  191 #define memcmp(buf1,buf2,count)           (int)(CompareMem(buf1,buf2,(UINTN)(count)))
  192 
  193 #define MAX_STRING_SIZE  0x1000
  194 #define strcat(strDest,strSource)         AsciiStrCatS(strDest,MAX_STRING_SIZE,strSource)
  195 #define strchr(str,ch)                    ScanMem8((VOID *)(str),AsciiStrSize(str),(UINT8)ch)
  196 #define strcmp                            AsciiStrCmp
  197 #define strncmp(string1,string2,count)    (int)(AsciiStrnCmp(string1,string2,(UINTN)(count)))
  198 #define strcpy(strDest,strSource)         AsciiStrCpyS(strDest,MAX_STRING_SIZE,strSource)
  199 #define strncpy(strDest,strSource,count)  AsciiStrnCpyS(strDest,MAX_STRING_SIZE,strSource,(UINTN)count)
  200 #define strlen(str)                       (size_t)(AsciiStrnLenS(str,MAX_STRING_SIZE))
  201 #define strstr                            AsciiStrStr
  202 
  203 // #define rotr32(x,n)  (((x) >> n) | ((x) << (32 - n)))
  204 // #define rotl32(x,n)  (((x) << n) | ((x) >> (32 - n)))
  205 // #define rotr64(x,n)  (((x) >> n) | ((x) << (64 - n)))
  206 // #define rotl64(x,n)  (((x) << n) | ((x) >> (64 - n)))
  207 // #define bswap_32(x)  (rotl32((((x) & 0xFF00FF00) >> 8) | (((x) & 0x00FF00FF) << 8), 16U))
  208 // #define bswap_64(x)  rotl64(((((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0xFFFF0000FFFF0000)) >> 16) | (((((x & LL(0xFF00FF00FF00FF00)) >> 8) | ((x & LL(0x00FF00FF00FF00FF)) << 8)) & LL(0x0000FFFF0000FFFF)) << 16)), 32U)
  209 
  210 #endif
  211 
  212 #ifdef TC_WINDOWS_BOOT
  213 
  214 #   ifdef  __cplusplus
  215 extern "C"
  216 #   endif
  217 void ThrowFatalException (int line);
  218 
  219 #   define TC_THROW_FATAL_EXCEPTION ThrowFatalException (__LINE__)
  220 #elif defined (TC_WINDOWS_DRIVER)
  221 #   define TC_THROW_FATAL_EXCEPTION KeBugCheckEx (SECURITY_SYSTEM, __LINE__, 0, 0, 'VC')
  222 #elif defined(_UEFI)
  223 void ThrowFatalException(int line);
  224 #   define TC_THROW_FATAL_EXCEPTION ThrowFatalException (__LINE__)
  225 #elif (defined(__clang__) && __has_builtin(__builtin_trap)) \
  226     || (defined(__GNUC__ ) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3))) \
  227     || (__has_builtin(__builtin_trap))
  228 #   define TC_THROW_FATAL_EXCEPTION __builtin_trap()
  229 #else
  230 #   define TC_THROW_FATAL_EXCEPTION *(char *) 0 = 0
  231 #endif
  232 
  233 #ifdef __COVERITY__
  234 #undef TC_THROW_FATAL_EXCEPTION
  235 #define TC_THROW_FATAL_EXCEPTION __coverity_panic__()
  236 #endif
  237 
  238 #ifdef TC_WINDOWS_DRIVER
  239 
  240 #include <ntifs.h>
  241 #include <ntddk.h>      /* Standard header file for nt drivers */
  242 #include <ntdddisk.h>       /* Standard I/O control codes  */
  243 
  244 /* defines needed for using enhanced protection of NX pool under Windows 8 and later */
  245 #define NonPagedPoolNx  512
  246 #define MdlMappingNoExecute     0x40000000
  247 
  248 /* variables used in the implementation of enhanced protection of NX pool under Windows 8 and later */
  249 extern POOL_TYPE ExDefaultNonPagedPoolType;
  250 extern ULONG ExDefaultMdlProtection;
  251 #ifdef _WIN64
  252 extern ULONG AllocTag;
  253 #else
  254 #define AllocTag 'MMCV'
  255 #endif
  256 
  257 #define TCalloc(size) ((void *) ExAllocatePoolWithTag( ExDefaultNonPagedPoolType, size, AllocTag ))
  258 #define TCfree(memblock) ExFreePoolWithTag( memblock, AllocTag )
  259 
  260 #define DEVICE_DRIVER
  261 
  262 #ifndef BOOL
  263 typedef int BOOL;
  264 #endif
  265 
  266 #ifndef WORD
  267 typedef USHORT WORD;
  268 #endif
  269 
  270 #ifndef BOOLEAN
  271 typedef unsigned char  BOOLEAN;
  272 #endif
  273 
  274 #ifndef TRUE
  275 #define TRUE 1
  276 #endif
  277 
  278 #ifndef FALSE
  279 #define FALSE !TRUE
  280 #endif
  281 
  282 typedef NTSTATUS (NTAPI *KeSaveExtendedProcessorStateFn) (
  283     __in ULONG64 Mask,
  284     PXSTATE_SAVE XStateSave
  285     );
  286 
  287 
  288 typedef VOID (NTAPI *KeRestoreExtendedProcessorStateFn) (
  289     PXSTATE_SAVE XStateSave
  290     );
  291 
  292 typedef NTSTATUS (NTAPI *ExGetFirmwareEnvironmentVariableFn) (
  293   PUNICODE_STRING VariableName,
  294   LPGUID          VendorGuid,
  295   PVOID           Value,
  296   PULONG          ValueLength,
  297   PULONG          Attributes
  298 );
  299 
  300 typedef BOOLEAN (NTAPI *KeAreAllApcsDisabledFn) ();
  301 
  302 typedef void (NTAPI *KeSetSystemGroupAffinityThreadFn)(
  303   PGROUP_AFFINITY Affinity,
  304   PGROUP_AFFINITY PreviousAffinity
  305 );
  306 
  307 typedef USHORT (NTAPI *KeQueryActiveGroupCountFn)();
  308 
  309 typedef ULONG (NTAPI *KeQueryActiveProcessorCountExFn)(
  310   USHORT GroupNumber
  311 );
  312 
  313 extern NTSTATUS NTAPI KeSaveExtendedProcessorState (
  314     __in ULONG64 Mask,
  315     PXSTATE_SAVE XStateSave
  316     );
  317 
  318 
  319 extern VOID NTAPI KeRestoreExtendedProcessorState (
  320     PXSTATE_SAVE XStateSave
  321     );
  322 
  323 extern BOOLEAN VC_KeAreAllApcsDisabled (VOID);
  324 
  325 
  326 #else               /* !TC_WINDOWS_DRIVER */
  327 #if !defined(_UEFI)
  328 #define TCalloc malloc
  329 #define TCfree free
  330 #else
  331 #define TCalloc VeraCryptMemAlloc
  332 #define TCfree VeraCryptMemFree
  333 #endif //!defined(_UEFI)
  334 
  335 #ifdef _WIN32
  336 
  337 #ifndef TC_LOCAL_WIN32_WINNT_OVERRIDE
  338 #   undef _WIN32_WINNT
  339 #   define  _WIN32_WINNT 0x0501 /* Does not apply to the driver */
  340 #endif
  341 
  342 #include <windows.h>        /* Windows header */
  343 #include <commctrl.h>       /* The common controls */
  344 #include <process.h>        /* Process control */
  345 #include <winioctl.h>
  346 #include <stdio.h>      /* For sprintf */
  347 #include <tchar.h>
  348 
  349 #endif              /* _WIN32 */
  350 
  351 #endif              /* !TC_WINDOWS_DRIVER */
  352 
  353 #ifndef TC_TO_STRING
  354 #   define TC_TO_STRING2(n) #n
  355 #   define TC_TO_STRING(n) TC_TO_STRING2(n)
  356 #endif
  357 
  358 #ifdef DEVICE_DRIVER
  359 #   if defined (DEBUG) || defined (DEBUG_TRACE)
  360 #       if 0 // DbgPrintEx is not available on Windows 2000
  361 #           define Dump DbgPrint
  362 #       else
  363 #           define Dump(...) DbgPrintEx (DPFLTR_IHVDRIVER_ID, DPFLTR_ERROR_LEVEL, __VA_ARGS__)
  364 #       endif
  365 #       define DumpMem(...) DumpMemory (__VA_ARGS__)
  366 #   else
  367 #       define Dump(...)
  368 #       define DumpMem(...)
  369 #   endif
  370 #elif !defined (TC_WINDOWS_BOOT)
  371 #   define Dump(...)
  372 #   define DumpMem(...)
  373 #endif
  374 
  375 #if !defined (trace_msg) && !defined (TC_WINDOWS_BOOT)
  376 #   if defined(DEBUG) || defined (DEBUG_TRACE)
  377 #       ifdef DEVICE_DRIVER
  378 #           define trace_msg Dump
  379 #       elif defined (_WIN32)
  380 #           define trace_msg(...) do { char msg[2048]; StringCbPrintfA (msg, sizeof (msg), __VA_ARGS__); OutputDebugString (msg); } while (0)
  381 #       else
  382 #           define trace_msg(...)
  383 #       endif
  384 #       define trace_point trace_msg (__FUNCTION__ ":" TC_TO_STRING(__LINE__) "\n")
  385 #   else
  386 #       define trace_msg(...)
  387 #       define trace_point
  388 #   endif
  389 #endif
  390 
  391 #ifdef DEVICE_DRIVER
  392 #   define TC_EVENT KEVENT
  393 #   define TC_WAIT_EVENT(EVENT) KeWaitForSingleObject (&EVENT, Executive, KernelMode, FALSE, NULL)
  394 #elif defined (_WIN32)
  395 #   define TC_EVENT HANDLE
  396 #   define TC_WAIT_EVENT(EVENT) WaitForSingleObject (EVENT, INFINITE)
  397 #endif
  398 
  399 #if defined(_WIN32) && !defined(_UEFI)
  400 #define burn(mem,size) do { volatile char *burnm = (volatile char *)(mem); size_t burnc = size; RtlSecureZeroMemory (mem, size); while (burnc--) *burnm++ = 0; } while (0)
  401 #else
  402 #define burn(mem,size) do { volatile char *burnm = (volatile char *)(mem); int burnc = size; while (burnc--) *burnm++ = 0; } while (0)
  403 #endif
  404 
  405 #define volatile_memcpy(d,s,size) do { volatile char *destm = (volatile char *)(d); volatile char *srcm = (volatile char *)(s); size_t memc = size; while (memc--) *destm++ = *srcm++; } while (0)
  406 
  407 // The size of the memory area to wipe is in bytes amd it must be a multiple of 8.
  408 #ifndef TC_NO_COMPILER_INT64
  409 #   define FAST_ERASE64(mem,size) do { volatile uint64 *burnm = (volatile uint64 *)(mem); int burnc = size >> 3; while (burnc--) *burnm++ = 0; } while (0)
  410 #else
  411 #   define FAST_ERASE64(mem,size) do { volatile unsigned __int32 *burnm = (volatile unsigned __int32 *)(mem); int burnc = size >> 2; while (burnc--) *burnm++ = 0; } while (0)
  412 #endif
  413 
  414 #ifdef TC_WINDOWS_BOOT
  415 #   ifndef max
  416 #       define max(a,b) (((a) > (b)) ? (a) : (b))
  417 #   endif
  418 
  419 #   if defined(__cplusplus) && !defined(_UEFI)
  420 extern "C"
  421 #   endif
  422 void EraseMemory (void *memory, int size);
  423 
  424 #   undef burn
  425 #   define burn EraseMemory
  426 #endif
  427 
  428 #ifdef MAX_PATH
  429 #define TC_MAX_PATH     MAX_PATH
  430 #else
  431 #define TC_MAX_PATH     260 /* Includes the null terminator */
  432 #endif
  433 
  434 #define TC_STR_RELEASED_BY L"Released by IDRIX on " TC_STR_RELEASE_DATE
  435 
  436 #define MAX_URL_LENGTH  2084 /* Internet Explorer limit. Includes the terminating null character. */
  437 
  438 #define TC_HOMEPAGE L"https://www.idrix.fr/"
  439 #define TC_APPLINK L"https://www.veracrypt.fr"
  440 
  441 enum
  442 {
  443     /* WARNING: ADD ANY NEW CODES AT THE END (DO NOT INSERT THEM BETWEEN EXISTING). DO *NOT* DELETE ANY
  444     EXISTING CODES! Changing these values or their meanings may cause incompatibility with other versions
  445     (for example, if a new version of the TrueCrypt installer receives an error code from an installed
  446     driver whose version is lower, it will report and interpret the error incorrectly). */
  447 
  448     ERR_SUCCESS                             = 0,
  449     ERR_OS_ERROR                            = 1,
  450     ERR_OUTOFMEMORY                         = 2,
  451     ERR_PASSWORD_WRONG                      = 3,
  452     ERR_VOL_FORMAT_BAD                      = 4,
  453     ERR_DRIVE_NOT_FOUND                     = 5,
  454     ERR_FILES_OPEN                          = 6,
  455     ERR_VOL_SIZE_WRONG                      = 7,
  456     ERR_COMPRESSION_NOT_SUPPORTED           = 8,
  457     ERR_PASSWORD_CHANGE_VOL_TYPE            = 9,
  458     ERR_PASSWORD_CHANGE_VOL_VERSION         = 10,
  459     ERR_VOL_SEEKING                         = 11,
  460     ERR_VOL_WRITING                         = 12,
  461     ERR_FILES_OPEN_LOCK                     = 13,
  462     ERR_VOL_READING                         = 14,
  463     ERR_DRIVER_VERSION                      = 15,
  464     ERR_NEW_VERSION_REQUIRED                = 16,
  465     ERR_CIPHER_INIT_FAILURE                 = 17,
  466     ERR_CIPHER_INIT_WEAK_KEY                = 18,
  467     ERR_SELF_TESTS_FAILED                   = 19,
  468     ERR_SECTOR_SIZE_INCOMPATIBLE            = 20,
  469     ERR_VOL_ALREADY_MOUNTED                 = 21,
  470     ERR_NO_FREE_DRIVES                      = 22,
  471     ERR_FILE_OPEN_FAILED                    = 23,
  472     ERR_VOL_MOUNT_FAILED                    = 24,
  473     DEPRECATED_ERR_INVALID_DEVICE           = 25,
  474     ERR_ACCESS_DENIED                       = 26,
  475     ERR_MODE_INIT_FAILED                    = 27,
  476     ERR_DONT_REPORT                         = 28,
  477     ERR_ENCRYPTION_NOT_COMPLETED            = 29,
  478     ERR_PARAMETER_INCORRECT                 = 30,
  479     ERR_SYS_HIDVOL_HEAD_REENC_MODE_WRONG    = 31,
  480     ERR_NONSYS_INPLACE_ENC_INCOMPLETE       = 32,
  481     ERR_USER_ABORT                          = 33,
  482     ERR_UNSUPPORTED_TRUECRYPT_FORMAT        = 34,
  483     ERR_RAND_INIT_FAILED                    = 35,
  484     ERR_CAPI_INIT_FAILED                    = 36
  485 };
  486 
  487 #endif  // #ifndef TCDEFS_H