VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Buffer.cpp
blob: 7c61dc483fef464928aea92e7446c944b489bde4 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 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 "Buffer.h"
#include "Exception.h"

namespace VeraCrypt
{
	Buffer::Buffer () : DataPtr (nullptr), DataSize (0), DataAlignment (0)
	{
	}

	Buffer::Buffer (size_t size, size_t alignment) : DataPtr (nullptr), DataSize (0), DataAlignment (0)
	{
		Allocate (size, alignment);
	}

	Buffer::~Buffer ()
	{
		if (DataPtr != nullptr)
			Free ();
	}

	void Buffer::Allocate (size_t size, size_t alignment)
	{
		if (size < 1)
			throw ParameterIncorrect (SRC_POS);

		if (DataPtr != nullptr)
		{
			if ((DataSize == size) && (DataAlignment == alignment))
				return;
			Free();
		}

		try
		{
			DataPtr = static_cast<byte *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size));
			DataSize = size;
			DataAlignment = alignment;
		}
		catch (...)
		{
			DataPtr = nullptr;
			DataSize = 0;
			DataAlignment = 0;
			throw;
		}
	}

	void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment)
	{
		if (!IsAllocated () || ((bufferPtr.Size()) && (DataAlignment != alignment)))
		{
			if (IsAllocated ())
				Free ();

			if (bufferPtr.Size())
				Allocate (bufferPtr.Size(), alignment);
		}
		else if (bufferPtr.Size() > DataSize)
			throw ParameterTooLarge (SRC_POS);

		if (bufferPtr.Size())
			Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
	}

	void Buffer::Erase ()
	{
		if (DataSize > 0)
			Memory::Erase (DataPtr, DataSize);
	}

	void Buffer::Free ()
	{
		if (DataPtr == nullptr)
			throw NotInitialized (SRC_POS);

		if (DataAlignment > 0)
			Memory::FreeAligned (DataPtr);
		else
			Memory::Free (DataPtr);
		DataPtr = nullptr;
		DataSize = 0;
		DataAlignment = 0;
	}

	BufferPtr Buffer::GetRange (size_t offset, size_t size) const
	{
		if (offset + size > DataSize)
			throw ParameterIncorrect (SRC_POS);

		return BufferPtr (DataPtr + offset, size);
	}

	void Buffer::Zero ()
	{
		if (DataSize > 0)
			Memory::Zero (DataPtr, DataSize);
	}

	SecureBuffer::SecureBuffer (size_t size, size_t alignment)
	{
		Allocate (size, alignment);
	}

	SecureBuffer::~SecureBuffer ()
	{
		if (DataPtr != nullptr && DataSize != 0)
			Free ();
	}

	void SecureBuffer::Allocate (size_t size, size_t alignment)
	{
		Buffer::Allocate (size, alignment);
	}

	void SecureBuffer::Free ()
	{
		if (DataPtr == nullptr)
			throw NotInitialized (SRC_POS);

		Erase ();
		Buffer::Free ();
	}

	void BufferPtr::CopyFrom (const ConstBufferPtr &bufferPtr) const
	{
		if (bufferPtr.Size() > DataSize)
			throw ParameterTooLarge (SRC_POS);

		Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
	}

	BufferPtr BufferPtr::GetRange (size_t offset, size_t size) const
	{
		if (offset + size > DataSize)
			throw ParameterIncorrect (SRC_POS);

		return BufferPtr (DataPtr + offset, size);
	}

	ConstBufferPtr ConstBufferPtr::GetRange (size_t offset, size_t size) const
	{
		if (offset + size > DataSize)
			throw ParameterIncorrect (SRC_POS);

		return ConstBufferPtr (DataPtr + offset, size);
	}
}
n class="p">><li>Right-click the mounted volume in the drive list in the main VeraCrypt window and select &lsquo;<em>Add to Favorites</em>&rsquo;. </li><li>The Favorites Organizer window should appear now. In this window, enable the option &lsquo;<em>Mount selected volume when its host device gets connected</em>&rsquo; and click <em>OK</em>. </li></ol> <p>Then, when you insert e.g. a USB flash drive on which a VeraCrypt volume is located into the USB port, you will be asked for the volume password (and/or keyfiles) (unless it is cached) and if it is correct, the volume will be mounted.<br> <br> Note: VeraCrypt will not prompt you for a password if you have enabled caching of the pre-boot authentication password (<em>Settings </em>&gt; &lsquo;<em>System Encryption</em>&rsquo;) and the volume uses the same password as the system partition/drive.</p> <p>A special label can be assigned to each favorite volume. This label is not the same as the filesystem label and it is shown within the VeraCrypt user interface instead of the volume path. To assign such a label, follow these steps:</p> <ol> <li>Select <em>Favorites </em>&gt; &lsquo;<em>Organize Favorite Volumes</em>&rsquo;. </li><li>The Favorite Volumes Organizer window should appear now. In this window, select the volume whose label you want to edit. </li><li>Enter the label in the &lsquo;<em>Label of selected favorite volume</em>&rsquo; input field and click OK. </li></ol> <p>Note that the Favorite Volumes Organizer window (<em>Favorites </em>&gt; &lsquo;<em>Organize Favorite Volumes</em>&rsquo;) allows you to <strong>set various other options for each favorite volume</strong>. For example, any of them can be mounted as read-only or as removable medium. To set any of these options, follow these steps:</p> <ol> <li>Select <em>Favorites </em>&gt; &lsquo;<em>Organize Favorite Volumes</em>&rsquo;. </li><li>The Favorite Volumes Organizer window should appear now. In this window, select the volume whose options you want to set. </li><li>Set the options and click OK. </li></ol> <p>The order in which system favorite volumes are displayed in the Favorites Organizer window (<em>Favorites </em>&gt; &lsquo;<em>Organize Favorite Volumes</em>&rsquo;) is <strong>the order in which the volumes are mounted </strong>when you select <em>Favorites </em>&gt; <em>Mount Favorite Volumes </em> or when you press the &lsquo;<em>Mount Favorite Volumes</em>&rsquo; hotkey (<em>Settings </em>&gt; <em>Hot Keys</em>). You can use the <em>Move Up </em>and <em>Move Down </em> buttons to change the order of the volumes.<br> <br> Note that a favorite volume can also be a <strong>partition that is within the key scope of system encryption mounted without pre-boot authentication </strong>(for example, a partition located on the encrypted system drive of another operating system that is not running). When you mount such a volume and add it to favorites, you will no longer have to select <em>System </em>&gt; <em>Mount Without Pre-Boot Authentication </em>or to enable the mount option &lsquo;<em>Mount partition using system encryption without pre- boot authentication</em>&rsquo;. You can simply mount the favorite volume (as explained above) without setting any options, as the mode in which the volume is mounted is saved in the configuration file containing the list of your favorite volumes.</p> <p>Warning: When the drive letter assigned to a favorite volume (saved in the configuration file) is not free, the volume is not mounted and no error message is displayed.<br> <br> <strong>To remove a volume form the list of favorite volumes</strong>, select <em> Favorites </em>&gt; <em>Organize Favorite Volumes</em>, select the volume, click <em> Remove</em>, and click OK.</p> <p>&nbsp;</p> <p><a href="System%20Favorite%20Volumes.html" style="text-align:left; color:#0080c0; text-decoration:none; font-weight:bold.html">Next Section &gt;&gt;</a></p> </div> </div> </body></html>