/* This code is written by kerukuro for cppcrypto library (http://cppcrypto.sourceforge.net/) and released into public domain. */ /* adapted for VeraCrypt */ #include "chacha256.h" #include "cpu.h" #include "misc.h" #define rotater32(x,n) rotr32(x, n) #define rotatel32(x,n) rotl32(x, n) #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE void chacha_ECRYPT_encrypt_bytes(size_t bytes, uint32* x, const unsigned char* m, unsigned char* out, unsigned char* output, unsigned int r); #endif static VC_INLINE void xor_block_512(const unsigned char* in, const unsigned char* prev, unsigned char* out) { #if CRYPTOPP_BOOL_SSE2_INTRINSICS_AVAILABLE && !defined(_UEFI) && (!defined (TC_WINDOWS_DRIVER) || (!defined (DEBUG))) if (HasSSE2()) { __m128i b1 = _mm_loadu_si128((const __m128i*) in); __m128i p1 = _mm_loadu_si128((const __m128i*) prev); __m128i b2 = _mm_loadu_si128((const __m128i*) (in + 16)); __m128i p2 = _mm_loadu_si128((const __m128i*) (prev + 16)); _mm_storeu_si128((__m128i*) out, _mm_xor_si128(b1, p1)); _mm_storeu_si128((__m128i*) (out + 16), _mm_xor_si128(b2, p2)); b1 = _mm_loadu_si128((const __m128i*) (in + 32)); p1 = _mm_loadu_si128((const __m128i*) (prev + 32)); b2 = _mm_loadu_si128((const __m128i*) (in + 48)); p2 = _mm_loadu_si128((const __m128i*) (prev + 48)); _mm_storeu_si128((__m128i*) (out + 32), _mm_xor_si128(b1, p1)); _mm_storeu_si128((__m128i*) (out + 48), _mm_xor_si128(b2, p2)); } else #endif { int i; for (i = 0; i < 64; i++) out[i] = in[i] ^ prev[i]; } } static VC_INLINE void chacha_core(uint32* x, int r) { int i; for (i = 0; i < r; i++) { x[0] += x[4]; x[12] = rotatel32(x[12] ^ x[0], 16); x[8] += x[12]; x[4] = rotatel32(x[4] ^ x[8], 12); x[0] += x[4]; x[12] = rotatel32(x[12] ^ x[0], 8); x[8] += x[12]; x[4] = rotatel32(x[4] ^ x[8], 7); x[1] += x[5]; x[13] = rotatel32(x[13] ^ x[1], 16); x[9] += x[13]; x[5] = rotatel32(x[5] ^ x[9], 12); x[1] += x[5]; x[13] = rotatel32(x[13] ^ x[1], 8); x[9] += x[13]; x[5] = rotatel32(x[5] ^ x[9], 7); x[2] += x[6]; x[14] = rotatel32(x[14] ^ x[2], 16); x[10] += x[14]; x[6] = rotatel32(x[6] ^ x[10], 12); x[2] += x[6]; x[14] = rotatel32(x[14] ^ x[2], 8); x[10] += x[14]; x[6] = rotatel32(x[6] ^ x[10], 7); x[3] += x[7]; x[15] = rotatel32(x[15] ^ x[3], 16); x[11] += x[15]; x[7] = rotatel32(x[7] ^ x[11], 12); x[3] += x[7]; x[15] = rotatel32(x[15] ^ x[3], 8); x[11] += x[15]; x[7] = rotatel32(x[7] ^ x[11], 7); x[0] += x[5]; x[15] = rotatel32(x[15] ^ x[0], 16); x[10] += x[15]; x[5] = rotatel32(x[5] ^ x[10], 12); x[0] += x[5]; x[15] = rotatel32(x[15] ^ x[0], 8); x[10] += x[15]; x[5] = rotatel32(x[5] ^ x[10], 7); x[1] += x[6]; x[12] = rotatel32(x[12] ^ x[1], 16); x[11] += x[12]; x[6] = rotatel32(x[6] ^ x[11], 12); x[1] += x[6]; x[12] = rotatel32(x[12] ^ x[1], 8); x[11] += x[12]; x[6] = rotatel32(x[6] ^ x[11], 7); x[2] += x[7]; x[13] = rotatel32(x[13] ^ x[2], 16); x[8] += x[13]; x[7] = rotatel32(x[7] ^ x[8], 12); x[2] += x[7]; x[13] = rotatel32(x[13] ^ x[2], 8); x[8] += x[13]; x[7] = rotatel32(x[7] ^ x[8], 7); x[3] += x[4]; x[14] = rotatel32(x[14] ^ x[3], 16); x[9] += x[14]; x[4] = rotatel32(x[4] ^ x[9], 12); x[3] += x[4]; x[14] = rotatel32(x[14] ^ x[3], 8); x[9] += x[14]; x[4] = rotatel32(x[4] ^ x[9], 7); } } static VC_INLINE void chacha_hash(const uint32* in, uint32* out, int r) { uint32 x[16]; int i; memcpy(x, in, 64); chacha_core(x, r); for (i = 0; i < 16; ++i) out[i] = x[i] + in[i]; } static VC_INLINE void incrementSalsaCounter(uint32* input, uint32* block, int r) { chacha_hash(input, block, r); if (!++input[12]) ++input[13]; } static VC_INLINE void do_encrypt(const unsigned char* in, size_t len, unsigned char* out, int r, size_t* posPtr, uint32* input, uint32* block) { size_t i = 0, pos = *posPtr; if (pos) { while (pos < len && pos < 64) { out[i] = in[i] ^ ((unsigned char*)block)[pos++]; ++i; } len -= i; } if (len) pos = 0; #if CRYPTOPP_SSSE3_AVAILABLE && !defined(_UEFI) && (!defined (TC_WINDOWS_DRIVER) || (!defined (DEBUG))) if (HasSSSE3()) { size_t fullblocks = len - len % 64; if (fullblocks) { chacha_ECRYPT_encrypt_bytes(fullblocks, input, in + i, out + i, (unsigned char*)block, r); i += fullblocks; len -= fullblocks; } if (len) { chacha_ECRYPT_encrypt_bytes
/*
 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_ENCRYPTION_THREAD_POOL
#define TC_HEADER_ENCRYPTION_THREAD_POOL

#include "Tcdefs.h"
#include "Crypto.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef enum
{
	EncryptDataUnitsWork,
	DecryptDataUnitsWork,
	DeriveKeyWork,
	ReadVolumeHeaderFinalizationWork
} EncryptionThreadPoolWorkType;

#ifndef DEVICE_DRIVER
size_t GetCpuCount (WORD* pGroupCount);
#endif

void EncryptionThreadPoolBeginKeyDerivation (TC_EVENT *completionEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG *completionFlag, LONG *outstandingWorkItemCount, int pkcs5Prf, char *password, int passwordLength, char *salt, int iterationCount, char *derivedKey);
void EncryptionThreadPoolBeginReadVolumeHeaderFinalization (TC_EVENT *keyDerivationCompletedEvent, TC_EVENT *noOutstandingWorkItemEvent, LONG* outstandingWorkItemCount, void* keyInfoBuffer, int keyInfoBufferSize, void* keyDerivationWorkItems, int keyDerivationWorkItemsSize);
void EncryptionThreadPoolDoWork (EncryptionThreadPoolWorkType type, uint8 *data, const UINT64_STRUCT *startUnitNo, uint32 unitCount, PCRYPTO_INFO cryptoInfo);
BOOL EncryptionThreadPoolStart (size_t encryptionFreeCpuCount);
void EncryptionThreadPoolStop ();
size_t GetEncryptionThreadCount ();
size_t GetMaxEncryptionThreadCount ();
BOOL IsEncryptionThreadPoolRunning ();

#ifdef __cplusplus
}
#endif

#endif // TC_HEADER_ENCRYPTION_THREAD_POOL