VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/FileCommon.cpp
blob: e9b77540135d88ba02297ac05cab8af9f44d253a (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
/*
 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-2016 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 "File.h"
#ifdef TC_UNIX
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#endif

namespace VeraCrypt
{
	File::~File ()
	{
		try
		{
			if (FileIsOpen)
				Close();
		}
		catch (...) { }
	}

	void File::Copy (const FilePath &sourcePath, const FilePath &destinationPath, bool preserveTimestamps)
	{
		File source;
		source.Open (sourcePath);

		File destination;
		destination.Open (destinationPath, CreateWrite);

		SecureBuffer buffer (OptimalReadSize);
		uint64 len;

		while ((len = source.Read (buffer)) > 0)
		{
			destination.Write (buffer, static_cast <size_t> (len));
		}

		if (preserveTimestamps)
		{
			destination.Flush();
#ifndef TC_WINDOWS
			struct stat statData;
			throw_sys_sub_if (stat (string (sourcePath).c_str(), &statData) == -1, wstring (sourcePath));

			struct utimbuf u;
			u.actime = statData.st_atime;
			u.modtime = statData.st_mtime;
			throw_sys_sub_if (utime (string (destinationPath).c_str(), &u) == -1, wstring (destinationPath));
#endif
		}
	}
	
	FilePath File::GetPath () const
	{
		if_debug (ValidateState());
		return Path;
	}

	void File::ReadCompleteBuffer (const BufferPtr &buffer) const
	{
		size_t dataLeft = buffer.Size();
		size_t offset = 0;

		while (dataLeft > 0)
		{
			size_t dataRead = static_cast <size_t> (Read (buffer.GetRange (offset, dataLeft)));
			if (dataRead == 0)
				throw InsufficientData (SRC_POS);

			dataLeft -= dataRead;
			offset += dataRead;
		}
	}
	
	void File::ValidateState () const
	{
		if (!FileIsOpen)
			throw NotInitialized (SRC_POS);
	}
}
an class="k">return 2000; } virtual wstring GetName () const { return L"HMAC-RIPEMD-160"; } private: Pkcs5HmacRipemd160 (const Pkcs5HmacRipemd160 &); Pkcs5HmacRipemd160 &operator= (const Pkcs5HmacRipemd160 &); }; class Pkcs5HmacRipemd160_1000 : public Pkcs5Kdf { public: Pkcs5HmacRipemd160_1000 () { } virtual ~Pkcs5HmacRipemd160_1000 () { } virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const; virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Ripemd160); } virtual int GetIterationCount () const { return 1000; } virtual wstring GetName () const { return L"HMAC-RIPEMD-160"; } private: Pkcs5HmacRipemd160_1000 (const Pkcs5HmacRipemd160_1000 &); Pkcs5HmacRipemd160_1000 &operator= (const Pkcs5HmacRipemd160_1000 &); }; class Pkcs5HmacSha1 : public Pkcs5Kdf { public: Pkcs5HmacSha1 () { } virtual ~Pkcs5HmacSha1 () { } virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const; virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Sha1); } virtual int GetIterationCount () const { return 2000; } virtual wstring GetName () const { return L"HMAC-SHA-1"; } private: Pkcs5HmacSha1 (const Pkcs5HmacSha1 &); Pkcs5HmacSha1 &operator= (const Pkcs5HmacSha1 &); }; class Pkcs5HmacSha512 : public Pkcs5Kdf { public: Pkcs5HmacSha512 () { } virtual ~Pkcs5HmacSha512 () { } virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const; virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Sha512); } virtual int GetIterationCount () const { return 1000; } virtual wstring GetName () const { return L"HMAC-SHA-512"; } private: Pkcs5HmacSha512 (const Pkcs5HmacSha512 &); Pkcs5HmacSha512 &operator= (const Pkcs5HmacSha512 &); }; class Pkcs5HmacWhirlpool : public Pkcs5Kdf { public: Pkcs5HmacWhirlpool () { } virtual ~Pkcs5HmacWhirlpool () { } virtual void DeriveKey (const BufferPtr &key, const VolumePassword &password, const ConstBufferPtr &salt, int iterationCount) const; virtual shared_ptr <Hash> GetHash () const { return shared_ptr <Hash> (new Whirlpool); } virtual int GetIterationCount () const { return 1000; } virtual wstring GetName () const { return L"HMAC-Whirlpool"; } private: Pkcs5HmacWhirlpool (const Pkcs5HmacWhirlpool &); Pkcs5HmacWhirlpool &operator= (const Pkcs5HmacWhirlpool &); }; } #endif // TC_HEADER_Encryption_Pkcs5