"Fossies" - the Fresh Open Source Software Archive

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