VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Platform/Serializer.h
blob: c2dac3242985187f5b2ee39b131c6fc38376932d (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
/*
 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.
*/

#ifndef TC_HEADER_Platform_Serializer
#define TC_HEADER_Platform_Serializer

#include "PlatformBase.h"
#include "Buffer.h"
#include "SharedPtr.h"
#include "Stream.h"

namespace VeraCrypt
{
	class Serializer
	{
	public:
		Serializer (shared_ptr <Stream> stream) : DataStream (stream) { }
		virtual ~Serializer () { }

		void Deserialize (const string &name, bool &data);
		void Deserialize (const string &name, byte &data);
		void Deserialize (const string &name, int32 &data);
		void Deserialize (const string &name, int64 &data);
		void Deserialize (const string &name, uint32 &data);
		void Deserialize (const string &name, uint64 &data);
		void Deserialize (const string &name, string &data);
		void Deserialize (const string &name, wstring &data);
		void Deserialize (const string &name, const BufferPtr &data);
		bool DeserializeBool (const string &name);
		int32 DeserializeInt32 (const string &name);
		int64 DeserializeInt64 (const string &name);
		uint32 DeserializeUInt32 (const string &name);
		uint64 DeserializeUInt64 (const string &name);
		string DeserializeString (const string &name);
		list <string> DeserializeStringList (const string &name);
		wstring DeserializeWString (const string &name);
		list <wstring> DeserializeWStringList (const string &name);
		void Serialize (const string &name, bool data);
		void Serialize (const string &name, byte data);
		void Serialize (const string &name, const char *data);
		void Serialize (const string &name, int32 data);
		void Serialize (const string &name, int64 data);
		void Serialize (const string &name, uint32 data);
		void Serialize (const string &name, uint64 data);
		void Serialize (const string &name, const string &data);
		void Serialize (const string &name, const wstring &data);
		void Serialize (const string &name, const wchar_t *data);
		void Serialize (const string &name, const list <string> &stringList);
		void Serialize (const string &name, const list <wstring> &stringList);
		void Serialize (const string &name, const ConstBufferPtr &data);

	protected:
		template <typename T> T Deserialize ();
		string DeserializeString ();
		wstring DeserializeWString ();
		template <typename T> void Serialize (T data);
		void SerializeString (const string &data);
		void SerializeWString (const wstring &data);
		void ValidateName (const string &name);

		shared_ptr <Stream> DataStream;

	private:
		Serializer (const Serializer &);
		Serializer &operator= (const Serializer &);
	};
}

#endif // TC_HEADER_Platform_Serializer
AND_GenerateBlock@8> = <MASM_RDRAND_GenerateBlock> ALIAS <@MASM_RDSEED_GenerateBlock@8> = <MASM_RDSEED_GenerateBlock> ENDIF ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IFDEF _M_X86 ;; Set via the command line .CODE ALIGN 8 OPTION PROLOGUE:NONE OPTION EPILOGUE:NONE ;; No need for Load_Arguments due to fastcall ;; ECX (in): arg1, byte* buffer ;; EDX (in): arg2, size_t bsize MASM_RDRAND_GenerateBlock PROC ;; arg1:DWORD, arg2:DWORD MWSIZE EQU 04h ;; machine word size buffer EQU ecx bsize EQU edx ;; Top of While loop GenerateBlock_Top: ;; Check remaining size cmp bsize, 0 je GenerateBlock_Return Call_RDRAND_EAX: ;; RDRAND is not available prior to VS2012. Just emit ;; the byte codes using DB. This is `rdrand eax`. DB 0Fh, 0C7h, 0F0h ;; If CF=1, the number returned by RDRAND is valid. ;; If CF=0, a random number was not available. ;; Retry immediately jnc Call_RDRAND_EAX RDRAND_succeeded: cmp bsize, MWSIZE jb Partial_Machine_Word Full_Machine_Word: mov DWORD PTR [buffer], eax add buffer, MWSIZE ;; No need for Intel Core 2 slow workarounds, like sub bsize, MWSIZE ;; `lea buffer,[buffer+MWSIZE]` for faster adds ;; Continue jmp GenerateBlock_Top ;; 1,2,3 bytes remain Partial_Machine_Word: ;; Test bit 1 to see if size is at least 2 test bsize, 2 jz Bit_1_Not_Set mov WORD PTR [buffer], ax shr eax, 16 add buffer, 2 Bit_1_Not_Set: ;; Test bit 0 to see if size is at least 1 test bsize, 1 jz Bit_0_Not_Set mov BYTE PTR [buffer], al Bit_0_Not_Set: ;; We've hit all the bits GenerateBlock_Return: ;; Clear artifacts xor eax, eax ret MASM_RDRAND_GenerateBlock ENDP ENDIF ;; _M_X86 OPTION PROLOGUE:PrologueDef OPTION EPILOGUE:EpilogueDef ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IFDEF _M_X64 ;; Set via the command line .CODE ALIGN 16 OPTION PROLOGUE:NONE OPTION EPILOGUE:NONE ;; No need for Load_Arguments due to fastcall ;; RCX (in): arg1, byte* buffer ;; RDX (in): arg2, size_t bsize MASM_RDRAND_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD MWSIZE EQU 08h ;; machine word size buffer EQU rcx bsize EQU rdx ;; Top of While loop GenerateBlock_Top: ;; Check remaining size cmp bsize, 0 je GenerateBlock_Return Call_RDRAND_RAX: ;; RDRAND is not available prior to VS2012. Just emit ;; the byte codes using DB. This is `rdrand rax`. DB 048h, 0Fh, 0C7h, 0F0h ;; If CF=1, the number returned by RDRAND is valid. ;; If CF=0, a random number was not available. ;; Retry immediately jnc Call_RDRAND_RAX RDRAND_succeeded: cmp bsize, MWSIZE jb Partial_Machine_Word Full_Machine_Word: mov QWORD PTR [buffer], rax add buffer, MWSIZE sub bsize, MWSIZE ;; Continue jmp GenerateBlock_Top ;; 1,2,3,4,5,6,7 bytes remain Partial_Machine_Word: ;; Test bit 2 to see if size is at least 4 test bsize, 4 jz Bit_2_Not_Set mov DWORD PTR [buffer], eax shr rax, 32 add buffer, 4 Bit_2_Not_Set: ;; Test bit 1 to see if size is at least 2 test bsize, 2 jz Bit_1_Not_Set mov WORD PTR [buffer], ax shr eax, 16 add buffer, 2 Bit_1_Not_Set: ;; Test bit 0 to see if size is at least 1 test bsize, 1 jz Bit_0_Not_Set mov BYTE PTR [buffer], al Bit_0_Not_Set: ;; We've hit all the bits GenerateBlock_Return: ;; Clear artifacts xor rax, rax ret MASM_RDRAND_GenerateBlock ENDP ENDIF ;; _M_X64 OPTION PROLOGUE:PrologueDef OPTION EPILOGUE:EpilogueDef ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IFDEF _M_X86 ;; Set via the command line .CODE ALIGN 8 OPTION PROLOGUE:NONE OPTION EPILOGUE:NONE ;; No need for Load_Arguments due to fastcall ;; ECX (in): arg1, byte* buffer ;; EDX (in): arg2, size_t bsize MASM_RDSEED_GenerateBlock PROC ;; arg1:DWORD, arg2:DWORD MWSIZE EQU 04h ;; machine word size buffer EQU ecx bsize EQU edx ;; Top of While loop GenerateBlock_Top: ;; Check remaining size cmp bsize, 0 je GenerateBlock_Return Call_RDSEED_EAX: ;; RDSEED is not available prior to VS2012. Just emit ;; the byte codes using DB. This is `rdseed eax`. DB 0Fh, 0C7h, 0F8h ;; If CF=1, the number returned by RDSEED is valid. ;; If CF=0, a random number was not available. ;; Retry immediately jnc Call_RDSEED_EAX RDSEED_succeeded: cmp bsize, MWSIZE jb Partial_Machine_Word Full_Machine_Word: mov DWORD PTR [buffer], eax add buffer, MWSIZE ;; No need for Intel Core 2 slow workarounds, like sub bsize, MWSIZE ;; `lea buffer,[buffer+MWSIZE]` for faster adds ;; Continue jmp GenerateBlock_Top ;; 1,2,3 bytes remain Partial_Machine_Word: ;; Test bit 1 to see if size is at least 2 test bsize, 2 jz Bit_1_Not_Set mov WORD PTR [buffer], ax shr eax, 16 add buffer, 2 Bit_1_Not_Set: ;; Test bit 0 to see if size is at least 1 test bsize, 1 jz Bit_0_Not_Set mov BYTE PTR [buffer], al Bit_0_Not_Set: ;; We've hit all the bits GenerateBlock_Return: ;; Clear artifacts xor eax, eax ret MASM_RDSEED_GenerateBlock ENDP ENDIF ;; _M_X86 OPTION PROLOGUE:PrologueDef OPTION EPILOGUE:EpilogueDef ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; IFDEF _M_X64 ;; Set via the command line .CODE ALIGN 16 OPTION PROLOGUE:NONE OPTION EPILOGUE:NONE ;; No need for Load_Arguments due to fastcall ;; RCX (in): arg1, byte* buffer ;; RDX (in): arg2, size_t bsize MASM_RDSEED_GenerateBlock PROC ;; arg1:QWORD, arg2:QWORD MWSIZE EQU 08h ;; machine word size buffer EQU rcx bsize EQU rdx ;; Top of While loop GenerateBlock_Top: ;; Check remaining size cmp bsize, 0 je GenerateBlock_Return Call_RDSEED_RAX: ;; RDSEED is not available prior to VS2012. Just emit ;; the byte codes using DB. This is `rdseed rax`. DB 048h, 0Fh, 0C7h, 0F8h ;; If CF=1, the number returned by RDSEED is valid. ;; If CF=0, a random number was not available. ;; Retry immediately jnc Call_RDSEED_RAX RDSEED_succeeded: cmp bsize, MWSIZE jb Partial_Machine_Word Full_Machine_Word: mov QWORD PTR [buffer], rax add buffer, MWSIZE sub bsize, MWSIZE ;; Continue jmp GenerateBlock_Top ;; 1,2,3,4,5,6,7 bytes remain Partial_Machine_Word: ;; Test bit 2 to see if size is at least 4 test bsize, 4 jz Bit_2_Not_Set mov DWORD PTR [buffer], eax shr rax, 32 add buffer, 4 Bit_2_Not_Set: ;; Test bit 1 to see if size is at least 2 test bsize, 2 jz Bit_1_Not_Set mov WORD PTR [buffer], ax shr eax, 16 add buffer, 2 Bit_1_Not_Set: ;; Test bit 0 to see if size is at least 1 test bsize, 1 jz Bit_0_Not_Set mov BYTE PTR [buffer], al Bit_0_Not_Set: ;; We've hit all the bits GenerateBlock_Return: ;; Clear artifacts xor rax, rax ret MASM_RDSEED_GenerateBlock ENDP ENDIF ;; _M_X64 OPTION PROLOGUE:PrologueDef OPTION EPILOGUE:EpilogueDef ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; END