VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Memory.cpp
blob: c4afed64650cdec331cd515cbf3ba94bcf1516a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 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.
*/

#include "Memory.h"
#include "Exception.h"
#include <stdlib.h>

namespace VeraCrypt
{
	void *Memory::Allocate (std::size_t size)
	{
		if (size < 1)
			throw ParameterIncorrect (SRC_POS);

		void *bufPtr = malloc (size);
		if (!bufPtr)
			throw bad_alloc();

		return bufPtr;
	}
	
	void *Memory::AllocateAligned (std::size_t size, std::size_t alignment)
	{
		if (size < 1)
			throw ParameterIncorrect (SRC_POS);
#ifdef TC_WINDOWS
		void *bufPtr = _aligned_malloc (size, alignment);
#else
		void *bufPtr = NULL;
		if (0 != posix_memalign (&bufPtr, alignment, size))
			bufPtr = NULL;
#endif
		if (!bufPtr)
			throw bad_alloc();

		return bufPtr;
	}

	int Memory::Compare (const void *memory1, size_t size1, const void *memory2, size_t size2)
	{
		if (size1 > size2)
			return 1;
		else if (size1 < size2)
			return -1;

		return memcmp (memory1, memory2, size1);
	}

	void Memory::Copy (void *memoryDestination, const void *memorySource, size_t size)
	{
		assert (memoryDestination != nullptr && memorySource != nullptr);
		memcpy (memoryDestination, memorySource, size);
	}

	void Memory::Zero (void *memory, size_t size)
	{
		memset (memory, 0, size);
	}

	void Memory::Free (void *memory)
	{
		assert (memory != nullptr);
		free (memory);
	}
	
	void Memory::FreeAligned (void *memory)
	{
		assert (memory != nullptr);
#ifdef TC_WINDOWS
		_aligned_free (memory);
#else
		free (memory);
#endif
	}
}
span> # if TC_MAX_VOLUME_SIZE > TC_MAX_VOLUME_SIZE_GENERAL # error TC_MAX_VOLUME_SIZE > TC_MAX_VOLUME_SIZE_GENERAL # endif #endif #define HEADER_ENCRYPTED_DATA_SIZE (TC_VOLUME_HEADER_EFFECTIVE_SIZE - HEADER_ENCRYPTED_DATA_OFFSET) // Volume header field offsets #define HEADER_SALT_OFFSET 0 #define HEADER_ENCRYPTED_DATA_OFFSET PKCS5_SALT_SIZE #define HEADER_MASTER_KEYDATA_OFFSET 256 #define TC_HEADER_OFFSET_MAGIC 64 #define TC_HEADER_OFFSET_VERSION 68 #define TC_HEADER_OFFSET_REQUIRED_VERSION 70 #define TC_HEADER_OFFSET_KEY_AREA_CRC 72 #define TC_HEADER_OFFSET_VOLUME_CREATION_TIME 76 #define TC_HEADER_OFFSET_MODIFICATION_TIME 84 #define TC_HEADER_OFFSET_HIDDEN_VOLUME_SIZE 92 #define TC_HEADER_OFFSET_VOLUME_SIZE 100 #define TC_HEADER_OFFSET_ENCRYPTED_AREA_START 108 #define TC_HEADER_OFFSET_ENCRYPTED_AREA_LENGTH 116 #define TC_HEADER_OFFSET_FLAGS 124 #define TC_HEADER_OFFSET_SECTOR_SIZE 128 #define TC_HEADER_OFFSET_HEADER_CRC 252 // Volume header flags #define TC_HEADER_FLAG_ENCRYPTED_SYSTEM 0x1 #define TC_HEADER_FLAG_NONSYS_INPLACE_ENC 0x2 // The volume has been created (or is being encrypted/decrypted) using non-system in-place encryption #ifndef TC_HEADER_Volume_VolumeHeader #include "Password.h" extern BOOL ReadVolumeHeaderRecoveryMode; uint16 GetHeaderField16 (byte *header, int offset); uint32 GetHeaderField32 (byte *header, int offset); UINT64_STRUCT GetHeaderField64 (byte *header, int offset); #if defined(TC_WINDOWS_BOOT) int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int pim, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo); #elif defined(_UEFI) int ReadVolumeHeader(BOOL bBoot, char *encryptedHeader, Password *password, int pkcs5_prf, int pim, BOOL truecryptMode, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo); 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); BOOL RandgetBytes(unsigned char *buf, int len, BOOL forceSlowPoll); #else int ReadVolumeHeader (BOOL bBoot, char *encryptedHeader, Password *password, int pkcs5_prf, int pim, BOOL truecryptMode, PCRYPTO_INFO *retInfo, CRYPTO_INFO *retHeaderCryptoInfo); #if defined(_WIN32) && !defined(_UEFI) void ComputeBootloaderFingerprint (byte *bootLoaderBuf, unsigned int bootLoaderSize, byte* fingerprint); #endif #endif #if !defined (DEVICE_DRIVER) && !defined (TC_WINDOWS_BOOT) && !defined(_UEFI) 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); BOOL ReadEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header, DWORD *bytesRead); BOOL WriteEffectiveVolumeHeader (BOOL device, HANDLE fileHandle, byte *header); int WriteRandomDataToReservedHeaderAreas (HWND hwndDlg, HANDLE dev, CRYPTO_INFO *cryptoInfo, uint64 dataAreaSize, BOOL bPrimaryOnly, BOOL bBackupOnly); #endif #endif // !TC_HEADER_Volume_VolumeHeader #ifdef __cplusplus } #endif #endif // TC_HEADER_Common_Volumes