VeraCrypt
aboutsummaryrefslogtreecommitdiff
path: root/src/Main/System.h
blob: 9d7ebc915e852841215771acce7f30de7bb8d5d8 (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
/*
 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_Main_System
#define TC_HEADER_Main_System

#ifndef TC_WINDOWS

#include "SystemPrecompiled.h"

#else

#ifndef WINVER
#define WINVER 0x0501
#endif

#ifndef TC_LOCAL_WIN32_WINNT_OVERRIDE
#	ifndef _WIN32_WINNT
#		define _WIN32_WINNT 0x0501
#	endif
#endif

#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0410
#endif

#ifndef _WIN32_IE
#define _WIN32_IE 0x0600
#endif

#define WIN32_LEAN_AND_MEAN

#ifndef UNICODE
#define UNICODE
#endif

#ifndef _UNICODE
#define _UNICODE
#endif _UNICODE

#include <wx/wxprec.h>
#include <wx/dde.h>
#include <wx/dnd.h>
#include <wx/filename.h>
#include <wx/hyperlink.h>
#include <wx/imaglist.h>
#include <wx/listctrl.h>
#include <wx/mstream.h>
#include <wx/power.h>
#include <wx/snglinst.h>
#include <wx/taskbar.h>
#include <wx/txtstrm.h>
#include <wx/valgen.h>
#include <wx/wfstream.h>
#include <shellapi.h>

#include <iostream>
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>

#endif

#endif // TC_HEADER_Main_System
an> { /* fill rs_buf with the keystream */ if (pCtx->m_rs_have) memset(pCtx->m_rs_buf + sizeof(pCtx->m_rs_buf) - pCtx->m_rs_have, 0, pCtx->m_rs_have); ChaCha256Encrypt(&pCtx->m_chachaCtx, pCtx->m_rs_buf, sizeof (pCtx->m_rs_buf), pCtx->m_rs_buf); /* mix in optional user provided data */ if (pCtx->m_getRandSeedCallback && useCallBack) { unsigned char dat[CHACHA20RNG_KEYSZ + CHACHA20RNG_IVSZ]; size_t i; pCtx->m_getRandSeedCallback (dat, sizeof (dat)); for (i = 0; i < (CHACHA20RNG_KEYSZ + CHACHA20RNG_IVSZ); i++) pCtx->m_rs_buf[i] ^= dat[i]; burn (dat, sizeof(dat)); } /* immediately reinit for backtracking resistance */ ChaCha256Init (&pCtx->m_chachaCtx, pCtx->m_rs_buf, pCtx->m_rs_buf + CHACHA20RNG_KEYSZ, 20); memset(pCtx->m_rs_buf, 0, CHACHA20RNG_KEYSZ + CHACHA20RNG_IVSZ); pCtx->m_rs_have = sizeof (pCtx->m_rs_buf) - CHACHA20RNG_KEYSZ - CHACHA20RNG_IVSZ; } static VC_INLINE void ChaCha20RngStir(ChaCha20RngCtx* pCtx) { ChaCha20RngReKey (pCtx, 1); /* invalidate rs_buf */ pCtx->m_rs_have = 0; memset(pCtx->m_rs_buf, 0, CHACHA20RNG_RSBUFSZ); pCtx->m_rs_count = 1600000; } static VC_INLINE void ChaCha20RngStirIfNeeded(ChaCha20RngCtx* pCtx, size_t len) { if (pCtx->m_rs_count <= len) { ChaCha20RngStir(pCtx); } else pCtx->m_rs_count -= len; } void ChaCha20RngInit (ChaCha20RngCtx* pCtx, const unsigned char* key, GetRandSeedFn rngSeedCallback, size_t InitialBytesToSkip) { ChaCha256Init (&pCtx->m_chachaCtx, key, key + 32, 20); pCtx->m_getRandSeedCallback = rngSeedCallback; /* fill rs_buf with the keystream */ pCtx->m_rs_have = 0; memset (pCtx->m_rs_buf, 0, sizeof (pCtx->m_rs_buf)); pCtx->m_rs_count = 1600000; ChaCha20RngReKey(pCtx, 0); if (InitialBytesToSkip) ChaCha20RngGetBytes (pCtx, NULL, InitialBytesToSkip); } void ChaCha20RngGetBytes (ChaCha20RngCtx* pCtx, unsigned char* buffer, size_t bufferLen) { unsigned char *buf = (unsigned char*) buffer; unsigned char* keystream; size_t m; ChaCha20RngStirIfNeeded(pCtx, bufferLen); while (bufferLen > 0) { if (pCtx->m_rs_have > 0) { m = VC_MIN(bufferLen, pCtx->m_rs_have); keystream = pCtx->m_rs_buf + sizeof(pCtx->m_rs_buf) - pCtx->m_rs_have; if (buf) { memcpy(buf, keystream, m); buf += m; } memset(keystream, 0, m); bufferLen -= m; pCtx->m_rs_have -= m; } if (pCtx->m_rs_have == 0) ChaCha20RngReKey (pCtx, 0); } }