mirror of
				https://github.com/PurpleI2P/i2pd.git
				synced 2025-11-04 08:30:46 +00:00 
			
		
		
		
	use builtin __AVX__ and __AES__ macros and reduce code duplication
This commit is contained in:
		
							parent
							
								
									fc16e76af1
								
							
						
					
					
						commit
						dec848f072
					
				
					 9 changed files with 104 additions and 149 deletions
				
			
		| 
						 | 
					@ -34,7 +34,7 @@ endif
 | 
				
			||||||
# Seems like all recent Mac's have AES-NI, after firmware upgrade 2.2
 | 
					# Seems like all recent Mac's have AES-NI, after firmware upgrade 2.2
 | 
				
			||||||
# Found no good way to detect it from command line. TODO: Might be some osx sysinfo magic
 | 
					# Found no good way to detect it from command line. TODO: Might be some osx sysinfo magic
 | 
				
			||||||
ifeq ($(USE_AESNI),yes)
 | 
					ifeq ($(USE_AESNI),yes)
 | 
				
			||||||
	CXXFLAGS += -maes -DAESNI
 | 
						CXXFLAGS += -maes
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
ifeq ($(USE_AVX),1)
 | 
					ifeq ($(USE_AVX),1)
 | 
				
			||||||
	CXXFLAGS += -mavx
 | 
						CXXFLAGS += -mavx
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -64,7 +64,7 @@ ifneq ($(shell $(GREP) -c aes /proc/cpuinfo),0)
 | 
				
			||||||
	ifeq ($(machine), aarch64)
 | 
						ifeq ($(machine), aarch64)
 | 
				
			||||||
		CXXFLAGS += -DARM64AES
 | 
							CXXFLAGS += -DARM64AES
 | 
				
			||||||
	else
 | 
						else
 | 
				
			||||||
		CPU_FLAGS += -maes -DAESNI
 | 
							CPU_FLAGS += -maes
 | 
				
			||||||
	endif
 | 
						endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,7 +37,7 @@ endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# don't change following line to ifeq ($(USE_AESNI),yes) !!!
 | 
					# don't change following line to ifeq ($(USE_AESNI),yes) !!!
 | 
				
			||||||
ifeq ($(USE_AESNI),1)
 | 
					ifeq ($(USE_AESNI),1)
 | 
				
			||||||
	CPU_FLAGS += -maes -DAESNI
 | 
						CPU_FLAGS += -maes
 | 
				
			||||||
else
 | 
					else
 | 
				
			||||||
	CPU_FLAGS += -msse
 | 
						CPU_FLAGS += -msse
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,7 +21,7 @@ ifeq ($(USE_UPNP),yes)
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ifeq ($(USE_AESNI),1)
 | 
					ifeq ($(USE_AESNI),1)
 | 
				
			||||||
	CXXFLAGS += -maes -DAESNI
 | 
						CXXFLAGS += -maes
 | 
				
			||||||
else
 | 
					else
 | 
				
			||||||
	CXXFLAGS += -msse
 | 
						CXXFLAGS += -msse
 | 
				
			||||||
endif
 | 
					endif
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -39,7 +39,7 @@ include_directories(${LIBI2PD_CLIENT_SRC_DIR})
 | 
				
			||||||
set (LIBI2PD_SRC
 | 
					set (LIBI2PD_SRC
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/BloomFilter.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/BloomFilter.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Config.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Config.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/CPU.cpp"  
 | 
					  "${LIBI2PD_SRC_DIR}/CPU.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Crypto.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Crypto.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/CryptoKey.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/CryptoKey.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Garlic.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Garlic.cpp"
 | 
				
			||||||
| 
						 | 
					@ -77,10 +77,10 @@ set (LIBI2PD_SRC
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/api.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/api.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Event.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Event.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Gost.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Gost.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/ChaCha20.cpp"	
 | 
					  "${LIBI2PD_SRC_DIR}/ChaCha20.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Poly1305.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Poly1305.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/Ed25519.cpp"
 | 
					  "${LIBI2PD_SRC_DIR}/Ed25519.cpp"
 | 
				
			||||||
  "${LIBI2PD_SRC_DIR}/NTCP2.cpp"		
 | 
					  "${LIBI2PD_SRC_DIR}/NTCP2.cpp"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (WITH_WEBSOCKETS)
 | 
					if (WITH_WEBSOCKETS)
 | 
				
			||||||
| 
						 | 
					@ -234,7 +234,6 @@ endif ()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (WITH_AESNI)
 | 
					if (WITH_AESNI)
 | 
				
			||||||
  set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes" )
 | 
					  set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -maes" )
 | 
				
			||||||
  add_definitions ( -DAESNI )
 | 
					 | 
				
			||||||
endif()
 | 
					endif()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if (WITH_AVX)
 | 
					if (WITH_AVX)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,23 +21,35 @@ namespace cpu
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void Detect()
 | 
						void Detect()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#if defined(__AES__) || defined(__AVX__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if defined(__x86_64__) || defined(__i386__)
 | 
					#if defined(__x86_64__) || defined(__i386__)
 | 
				
			||||||
		int info[4];
 | 
							int info[4];
 | 
				
			||||||
		__cpuid(0, info[0], info[1], info[2], info[3]);
 | 
							__cpuid(0, info[0], info[1], info[2], info[3]);
 | 
				
			||||||
		if (info[0] >= 0x00000001) {
 | 
							if (info[0] >= 0x00000001) {
 | 
				
			||||||
			__cpuid(0x00000001, info[0], info[1], info[2], info[3]);
 | 
								__cpuid(0x00000001, info[0], info[1], info[2], info[3]);
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
			aesni = info[2] & bit_AES;  // AESNI
 | 
								aesni = info[2] & bit_AES;  // AESNI
 | 
				
			||||||
 | 
					#endif  // __AES__
 | 
				
			||||||
 | 
					#ifdef __AVX__
 | 
				
			||||||
			avx = info[2] & bit_AVX;  // AVX
 | 
								avx = info[2] & bit_AVX;  // AVX
 | 
				
			||||||
 | 
					#endif  // __AVX__
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
#endif
 | 
					#endif  // defined(__x86_64__) || defined(__i386__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(aesni)
 | 
							if(aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			LogPrint(eLogInfo, "AESNI enabled");
 | 
								LogPrint(eLogInfo, "AESNI enabled");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					#endif  // __AES__
 | 
				
			||||||
 | 
					#ifdef __AVX__
 | 
				
			||||||
		if(avx)
 | 
							if(avx)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			LogPrint(eLogInfo, "AVX enabled");
 | 
								LogPrint(eLogInfo, "AVX enabled");
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					#endif  // __AVX__
 | 
				
			||||||
 | 
					#endif  // defined(__AES__) || defined(__AVX__)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -399,7 +399,7 @@ namespace crypto
 | 
				
			||||||
			bn2buf (x, encrypted + 1, len);
 | 
								bn2buf (x, encrypted + 1, len);
 | 
				
			||||||
			bn2buf (y, encrypted + 1 + len, len);
 | 
								bn2buf (y, encrypted + 1 + len, len);
 | 
				
			||||||
			RAND_bytes (encrypted + 1 + 2*len, 256 - 2*len);
 | 
								RAND_bytes (encrypted + 1 + 2*len, 256 - 2*len);
 | 
				
			||||||
		}	
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			bn2buf (x, encrypted, len);
 | 
								bn2buf (x, encrypted, len);
 | 
				
			||||||
| 
						 | 
					@ -468,10 +468,10 @@ namespace crypto
 | 
				
			||||||
			CBCDecryption decryption;
 | 
								CBCDecryption decryption;
 | 
				
			||||||
			decryption.SetKey (shared);
 | 
								decryption.SetKey (shared);
 | 
				
			||||||
			decryption.SetIV (iv);
 | 
								decryption.SetIV (iv);
 | 
				
			||||||
			if (zeroPadding)	
 | 
								if (zeroPadding)
 | 
				
			||||||
				decryption.Decrypt (encrypted + 258, 256, m);
 | 
									decryption.Decrypt (encrypted + 258, 256, m);
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
				decryption.Decrypt (encrypted + 256, 256, m);	
 | 
									decryption.Decrypt (encrypted + 256, 256, m);
 | 
				
			||||||
			// verify and copy
 | 
								// verify and copy
 | 
				
			||||||
			uint8_t hash[32];
 | 
								uint8_t hash[32];
 | 
				
			||||||
			SHA256 (m + 33, 222, hash);
 | 
								SHA256 (m + 33, 222, hash);
 | 
				
			||||||
| 
						 | 
					@ -522,9 +522,9 @@ namespace crypto
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		uint64_t buf[256];
 | 
							uint64_t buf[256];
 | 
				
			||||||
		uint64_t hash[12]; // 96 bytes
 | 
							uint64_t hash[12]; // 96 bytes
 | 
				
			||||||
 | 
					#ifdef __AVX__
 | 
				
			||||||
		if(i2p::cpu::avx)
 | 
							if(i2p::cpu::avx)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AVX
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"vmovups %[key], %%ymm0 \n"
 | 
										"vmovups %[key], %%ymm0 \n"
 | 
				
			||||||
| 
						 | 
					@ -543,30 +543,9 @@ namespace crypto
 | 
				
			||||||
						[buf]"r"(buf), [hash]"r"(hash)
 | 
											[buf]"r"(buf), [hash]"r"(hash)
 | 
				
			||||||
					: "memory", "%xmm0"	// TODO: change to %ymm0 later
 | 
										: "memory", "%xmm0"	// TODO: change to %ymm0 later
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			// ikeypad
 | 
					 | 
				
			||||||
			buf[0] = key.GetLL ()[0] ^ IPAD;
 | 
					 | 
				
			||||||
			buf[1] = key.GetLL ()[1] ^ IPAD;
 | 
					 | 
				
			||||||
			buf[2] = key.GetLL ()[2] ^ IPAD;
 | 
					 | 
				
			||||||
			buf[3] = key.GetLL ()[3] ^ IPAD;
 | 
					 | 
				
			||||||
			buf[4] = IPAD;
 | 
					 | 
				
			||||||
			buf[5] = IPAD;
 | 
					 | 
				
			||||||
			buf[6] = IPAD;
 | 
					 | 
				
			||||||
			buf[7] = IPAD;
 | 
					 | 
				
			||||||
			// okeypad
 | 
					 | 
				
			||||||
			hash[0] = key.GetLL ()[0] ^ OPAD;
 | 
					 | 
				
			||||||
			hash[1] = key.GetLL ()[1] ^ OPAD;
 | 
					 | 
				
			||||||
			hash[2] = key.GetLL ()[2] ^ OPAD;
 | 
					 | 
				
			||||||
			hash[3] = key.GetLL ()[3] ^ OPAD;
 | 
					 | 
				
			||||||
			hash[4] = OPAD;
 | 
					 | 
				
			||||||
			hash[5] = OPAD;
 | 
					 | 
				
			||||||
			hash[6] = OPAD;
 | 
					 | 
				
			||||||
			hash[7] = OPAD;
 | 
					 | 
				
			||||||
			// fill last 16 bytes with zeros (first hash size assumed 32 bytes in I2P)
 | 
					 | 
				
			||||||
			memset (hash + 10, 0, 16);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			// ikeypad
 | 
								// ikeypad
 | 
				
			||||||
			buf[0] = key.GetLL ()[0] ^ IPAD;
 | 
								buf[0] = key.GetLL ()[0] ^ IPAD;
 | 
				
			||||||
| 
						 | 
					@ -600,12 +579,12 @@ namespace crypto
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AES
 | 
					// AES
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
        #ifdef ARM64AES
 | 
					        #ifdef ARM64AES
 | 
				
			||||||
                void init_aesenc(void){
 | 
					                void init_aesenc(void){
 | 
				
			||||||
			// TODO: Implementation
 | 
								// TODO: Implementation
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
        #endif
 | 
					        #endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	#define KeyExpansion256(round0,round1) \
 | 
						#define KeyExpansion256(round0,round1) \
 | 
				
			||||||
| 
						 | 
					@ -632,7 +611,7 @@ namespace crypto
 | 
				
			||||||
		"movaps	%%xmm3, "#round1"(%[sched]) \n"
 | 
							"movaps	%%xmm3, "#round1"(%[sched]) \n"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	void ECBCryptoAESNI::ExpandKey (const AESKey& key)
 | 
						void ECBCryptoAESNI::ExpandKey (const AESKey& key)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		__asm__
 | 
							__asm__
 | 
				
			||||||
| 
						 | 
					@ -669,11 +648,11 @@ namespace crypto
 | 
				
			||||||
			: [key]"r"((const uint8_t *)key), [sched]"r"(GetKeySchedule ()) // input
 | 
								: [key]"r"((const uint8_t *)key), [sched]"r"(GetKeySchedule ()) // input
 | 
				
			||||||
			: "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory" // clogged
 | 
								: "%xmm1", "%xmm2", "%xmm3", "%xmm4", "memory" // clogged
 | 
				
			||||||
		);
 | 
							);
 | 
				
			||||||
	}	
 | 
						}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	#define EncryptAES256(sched) \
 | 
						#define EncryptAES256(sched) \
 | 
				
			||||||
		"pxor (%["#sched"]), %%xmm0 \n" \
 | 
							"pxor (%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
		"aesenc	16(%["#sched"]), %%xmm0 \n" \
 | 
							"aesenc	16(%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
| 
						 | 
					@ -691,12 +670,12 @@ namespace crypto
 | 
				
			||||||
		"aesenc	208(%["#sched"]), %%xmm0 \n" \
 | 
							"aesenc	208(%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
		"aesenclast	224(%["#sched"]), %%xmm0 \n"
 | 
							"aesenclast	224(%["#sched"]), %%xmm0 \n"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	void ECBEncryption::Encrypt (const ChipherBlock * in, ChipherBlock * out)
 | 
						void ECBEncryption::Encrypt (const ChipherBlock * in, ChipherBlock * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[in]), %%xmm0 \n"
 | 
										"movups	(%[in]), %%xmm0 \n"
 | 
				
			||||||
| 
						 | 
					@ -704,17 +683,15 @@ namespace crypto
 | 
				
			||||||
					"movups	%%xmm0, (%[out]) \n"
 | 
										"movups	%%xmm0, (%[out]) \n"
 | 
				
			||||||
					: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
 | 
										: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			AES_encrypt (in->buf, out->buf, &m_Key);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			AES_encrypt (in->buf, out->buf, &m_Key);
 | 
								AES_encrypt (in->buf, out->buf, &m_Key);
 | 
				
			||||||
		}	
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	#define DecryptAES256(sched) \
 | 
						#define DecryptAES256(sched) \
 | 
				
			||||||
		"pxor 224(%["#sched"]), %%xmm0 \n" \
 | 
							"pxor 224(%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
		"aesdec	208(%["#sched"]), %%xmm0 \n" \
 | 
							"aesdec	208(%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
| 
						 | 
					@ -732,12 +709,12 @@ namespace crypto
 | 
				
			||||||
		"aesdec	16(%["#sched"]), %%xmm0 \n" \
 | 
							"aesdec	16(%["#sched"]), %%xmm0 \n" \
 | 
				
			||||||
		"aesdeclast (%["#sched"]), %%xmm0 \n"
 | 
							"aesdeclast (%["#sched"]), %%xmm0 \n"
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	void ECBDecryption::Decrypt (const ChipherBlock * in, ChipherBlock * out)
 | 
						void ECBDecryption::Decrypt (const ChipherBlock * in, ChipherBlock * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[in]), %%xmm0 \n"
 | 
										"movups	(%[in]), %%xmm0 \n"
 | 
				
			||||||
| 
						 | 
					@ -745,17 +722,15 @@ namespace crypto
 | 
				
			||||||
					"movups	%%xmm0, (%[out]) \n"
 | 
										"movups	%%xmm0, (%[out]) \n"
 | 
				
			||||||
					: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
 | 
										: : [sched]"r"(GetKeySchedule ()), [in]"r"(in), [out]"r"(out) : "%xmm0", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			AES_decrypt (in->buf, out->buf, &m_Key);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			AES_decrypt (in->buf, out->buf, &m_Key);
 | 
								AES_decrypt (in->buf, out->buf, &m_Key);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	#define CallAESIMC(offset) \
 | 
						#define CallAESIMC(offset) \
 | 
				
			||||||
		"movaps "#offset"(%[shed]), %%xmm0 \n"	\
 | 
							"movaps "#offset"(%[shed]), %%xmm0 \n"	\
 | 
				
			||||||
		"aesimc %%xmm0, %%xmm0 \n" \
 | 
							"aesimc %%xmm0, %%xmm0 \n" \
 | 
				
			||||||
| 
						 | 
					@ -764,25 +739,23 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void ECBEncryption::SetKey (const AESKey& key)
 | 
						void ECBEncryption::SetKey (const AESKey& key)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
								ExpandKey (key);
 | 
				
			||||||
			ExpandKey (key); 
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			AES_set_encrypt_key (key, 256, &m_Key);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			AES_set_encrypt_key (key, 256, &m_Key);
 | 
								AES_set_encrypt_key (key, 256, &m_Key);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	void ECBDecryption::SetKey (const AESKey& key)
 | 
						void ECBDecryption::SetKey (const AESKey& key)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			ExpandKey (key); // expand encryption key first
 | 
								ExpandKey (key); // expand encryption key first
 | 
				
			||||||
			// then  invert it using aesimc
 | 
								// then  invert it using aesimc
 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
| 
						 | 
					@ -802,11 +775,9 @@ namespace crypto
 | 
				
			||||||
					CallAESIMC(208)
 | 
										CallAESIMC(208)
 | 
				
			||||||
					: : [shed]"r"(GetKeySchedule ()) : "%xmm0", "memory"
 | 
										: : [shed]"r"(GetKeySchedule ()) : "%xmm0", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			AES_set_decrypt_key (key, 256, &m_Key);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			AES_set_decrypt_key (key, 256, &m_Key);
 | 
								AES_set_decrypt_key (key, 256, &m_Key);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -815,9 +786,9 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
 | 
						void CBCEncryption::Encrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[iv]), %%xmm1 \n"
 | 
										"movups	(%[iv]), %%xmm1 \n"
 | 
				
			||||||
| 
						 | 
					@ -837,16 +808,9 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
 | 
											[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
 | 
				
			||||||
					: "%xmm0", "%xmm1", "cc", "memory"
 | 
										: "%xmm0", "%xmm1", "cc", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			for (int i = 0; i < numBlocks; i++)
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				*m_LastBlock.GetChipherBlock () ^= in[i];
 | 
					 | 
				
			||||||
				m_ECBEncryption.Encrypt (m_LastBlock.GetChipherBlock (), m_LastBlock.GetChipherBlock ());
 | 
					 | 
				
			||||||
				out[i] = *m_LastBlock.GetChipherBlock ();
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			for (int i = 0; i < numBlocks; i++)
 | 
								for (int i = 0; i < numBlocks; i++)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
| 
						 | 
					@ -867,9 +831,9 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
 | 
						void CBCEncryption::Encrypt (const uint8_t * in, uint8_t * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[iv]), %%xmm1 \n"
 | 
										"movups	(%[iv]), %%xmm1 \n"
 | 
				
			||||||
| 
						 | 
					@ -883,19 +847,17 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out)
 | 
											[in]"r"(in), [out]"r"(out)
 | 
				
			||||||
					: "%xmm0", "%xmm1", "memory"
 | 
										: "%xmm0", "%xmm1", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			Encrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
			Encrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
								Encrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
 | 
						void CBCDecryption::Decrypt (int numBlocks, const ChipherBlock * in, ChipherBlock * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[iv]), %%xmm1 \n"
 | 
										"movups	(%[iv]), %%xmm1 \n"
 | 
				
			||||||
| 
						 | 
					@ -916,17 +878,9 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
 | 
											[in]"r"(in), [out]"r"(out), [num]"r"(numBlocks)
 | 
				
			||||||
					: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
 | 
										: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			for (int i = 0; i < numBlocks; i++)
 | 
					 | 
				
			||||||
			{
 | 
					 | 
				
			||||||
				ChipherBlock tmp = in[i];
 | 
					 | 
				
			||||||
				m_ECBDecryption.Decrypt (in + i, out + i);
 | 
					 | 
				
			||||||
				out[i] ^= *m_IV.GetChipherBlock ();
 | 
					 | 
				
			||||||
				*m_IV.GetChipherBlock () = tmp;
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			for (int i = 0; i < numBlocks; i++)
 | 
								for (int i = 0; i < numBlocks; i++)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
| 
						 | 
					@ -947,9 +901,9 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)
 | 
						void CBCDecryption::Decrypt (const uint8_t * in, uint8_t * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					"movups	(%[iv]), %%xmm1 \n"
 | 
										"movups	(%[iv]), %%xmm1 \n"
 | 
				
			||||||
| 
						 | 
					@ -963,19 +917,17 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out)
 | 
											[in]"r"(in), [out]"r"(out)
 | 
				
			||||||
					: "%xmm0", "%xmm1", "memory"
 | 
										: "%xmm0", "%xmm1", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			Decrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
			Decrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
								Decrypt (1, (const ChipherBlock *)in, (ChipherBlock *)out);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void TunnelEncryption::Encrypt (const uint8_t * in, uint8_t * out)
 | 
						void TunnelEncryption::Encrypt (const uint8_t * in, uint8_t * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					// encrypt IV
 | 
										// encrypt IV
 | 
				
			||||||
| 
						 | 
					@ -1001,14 +953,9 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
 | 
											[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
 | 
				
			||||||
					: "%xmm0", "%xmm1", "cc", "memory"
 | 
										: "%xmm0", "%xmm1", "cc", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			m_IVEncryption.Encrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
					 | 
				
			||||||
			m_LayerEncryption.SetIV (out);
 | 
					 | 
				
			||||||
			m_LayerEncryption.Encrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, out + 16); // data
 | 
					 | 
				
			||||||
			m_IVEncryption.Encrypt ((ChipherBlock *)out, (ChipherBlock *)out); // double iv
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			m_IVEncryption.Encrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
								m_IVEncryption.Encrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
				
			||||||
			m_LayerEncryption.SetIV (out);
 | 
								m_LayerEncryption.SetIV (out);
 | 
				
			||||||
| 
						 | 
					@ -1019,9 +966,9 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	void TunnelDecryption::Decrypt (const uint8_t * in, uint8_t * out)
 | 
						void TunnelDecryption::Decrypt (const uint8_t * in, uint8_t * out)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 | 
					#ifdef __AES__
 | 
				
			||||||
		if(i2p::cpu::aesni)
 | 
							if(i2p::cpu::aesni)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
#ifdef AESNI
 | 
					 | 
				
			||||||
			__asm__
 | 
								__asm__
 | 
				
			||||||
				(
 | 
									(
 | 
				
			||||||
					// decrypt IV
 | 
										// decrypt IV
 | 
				
			||||||
| 
						 | 
					@ -1048,14 +995,9 @@ namespace crypto
 | 
				
			||||||
						[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
 | 
											[in]"r"(in), [out]"r"(out), [num]"r"(63) // 63 blocks = 1008 bytes
 | 
				
			||||||
					: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
 | 
										: "%xmm0", "%xmm1", "%xmm2", "cc", "memory"
 | 
				
			||||||
					);
 | 
										);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
			m_IVDecryption.Decrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
					 | 
				
			||||||
			m_LayerDecryption.SetIV (out);
 | 
					 | 
				
			||||||
			m_LayerDecryption.Decrypt (in + 16, i2p::tunnel::TUNNEL_DATA_ENCRYPTED_SIZE, out + 16); // data
 | 
					 | 
				
			||||||
			m_IVDecryption.Decrypt ((ChipherBlock *)out, (ChipherBlock *)out); // double iv
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			m_IVDecryption.Decrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
								m_IVDecryption.Decrypt ((const ChipherBlock *)in, (ChipherBlock *)out); // iv
 | 
				
			||||||
			m_LayerDecryption.SetIV (out);
 | 
								m_LayerDecryption.SetIV (out);
 | 
				
			||||||
| 
						 | 
					@ -1068,7 +1010,7 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool AEADChaCha20Poly1305 (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen, const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len, bool encrypt)
 | 
						bool AEADChaCha20Poly1305 (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen, const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len, bool encrypt)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if (len < msgLen) return false;	
 | 
							if (len < msgLen) return false;
 | 
				
			||||||
		if (encrypt && len < msgLen + 16) return false;
 | 
							if (encrypt && len < msgLen + 16) return false;
 | 
				
			||||||
		bool ret = true;
 | 
							bool ret = true;
 | 
				
			||||||
#if LEGACY_OPENSSL
 | 
					#if LEGACY_OPENSSL
 | 
				
			||||||
| 
						 | 
					@ -1076,40 +1018,40 @@ namespace crypto
 | 
				
			||||||
		uint8_t polyKey[64];
 | 
							uint8_t polyKey[64];
 | 
				
			||||||
		memset(polyKey, 0, sizeof(polyKey));
 | 
							memset(polyKey, 0, sizeof(polyKey));
 | 
				
			||||||
		chacha20 (polyKey, 64, nonce, key, 0);
 | 
							chacha20 (polyKey, 64, nonce, key, 0);
 | 
				
			||||||
		// encrypt data		
 | 
							// encrypt data
 | 
				
			||||||
		memcpy (buf, msg, msgLen);
 | 
							memcpy (buf, msg, msgLen);
 | 
				
			||||||
		chacha20 (buf, msgLen, nonce, key, 1);
 | 
							chacha20 (buf, msgLen, nonce, key, 1);
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		// create Poly1305 message
 | 
							// create Poly1305 message
 | 
				
			||||||
		if (!ad) adLen = 0;	
 | 
							if (!ad) adLen = 0;
 | 
				
			||||||
		std::vector<uint8_t> polyMsg(adLen + msgLen + 3*16);
 | 
							std::vector<uint8_t> polyMsg(adLen + msgLen + 3*16);
 | 
				
			||||||
		size_t offset = 0;
 | 
							size_t offset = 0;
 | 
				
			||||||
		uint8_t padding[16]; memset (padding, 0, 16);
 | 
							uint8_t padding[16]; memset (padding, 0, 16);
 | 
				
			||||||
		if (ad)
 | 
							if (ad)
 | 
				
			||||||
		{	
 | 
							{
 | 
				
			||||||
			memcpy (polyMsg.data (), ad, adLen); offset += adLen; // additional authenticated data
 | 
								memcpy (polyMsg.data (), ad, adLen); offset += adLen; // additional authenticated data
 | 
				
			||||||
			auto rem = adLen & 0x0F; // %16
 | 
								auto rem = adLen & 0x0F; // %16
 | 
				
			||||||
			if (rem) 
 | 
								if (rem)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				// padding1
 | 
									// padding1
 | 
				
			||||||
				rem = 16 - rem;
 | 
									rem = 16 - rem;
 | 
				
			||||||
				memcpy (polyMsg.data () + offset, padding, rem); offset += rem;	
 | 
									memcpy (polyMsg.data () + offset, padding, rem); offset += rem;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		memcpy (polyMsg.data () + offset, encrypt ? buf : msg, msgLen); offset += msgLen; // encrypted data
 | 
							memcpy (polyMsg.data () + offset, encrypt ? buf : msg, msgLen); offset += msgLen; // encrypted data
 | 
				
			||||||
		auto rem = msgLen & 0x0F; // %16
 | 
							auto rem = msgLen & 0x0F; // %16
 | 
				
			||||||
		if (rem) 
 | 
							if (rem)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			// padding2
 | 
								// padding2
 | 
				
			||||||
			rem = 16 - rem;
 | 
								rem = 16 - rem;
 | 
				
			||||||
			memcpy (polyMsg.data () + offset, padding, rem); offset += rem;	
 | 
								memcpy (polyMsg.data () + offset, padding, rem); offset += rem;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		htole64buf (polyMsg.data () + offset, adLen); offset += 8;			
 | 
							htole64buf (polyMsg.data () + offset, adLen); offset += 8;
 | 
				
			||||||
		htole64buf (polyMsg.data () + offset, msgLen); offset += 8;
 | 
							htole64buf (polyMsg.data () + offset, msgLen); offset += 8;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (encrypt)
 | 
							if (encrypt)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			// calculate Poly1305 tag and write in after encrypted data		
 | 
								// calculate Poly1305 tag and write in after encrypted data
 | 
				
			||||||
			Poly1305HMAC ((uint32_t *)(buf + msgLen), (uint32_t *)polyKey, polyMsg.data (), offset);
 | 
								Poly1305HMAC ((uint32_t *)(buf + msgLen), (uint32_t *)polyKey, polyMsg.data (), offset);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
| 
						 | 
					@ -1118,9 +1060,9 @@ namespace crypto
 | 
				
			||||||
			// calculate Poly1305 tag
 | 
								// calculate Poly1305 tag
 | 
				
			||||||
			Poly1305HMAC (tag, (uint32_t *)polyKey, polyMsg.data (), offset);
 | 
								Poly1305HMAC (tag, (uint32_t *)polyKey, polyMsg.data (), offset);
 | 
				
			||||||
			if (memcmp (tag, msg + msgLen, 16)) ret = false; // compare with provided
 | 
								if (memcmp (tag, msg + msgLen, 16)) ret = false; // compare with provided
 | 
				
			||||||
		}	
 | 
							}
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
		int outlen = 0;	
 | 
							int outlen = 0;
 | 
				
			||||||
		EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
 | 
							EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new ();
 | 
				
			||||||
		if (encrypt)
 | 
							if (encrypt)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
| 
						 | 
					@ -1141,8 +1083,8 @@ namespace crypto
 | 
				
			||||||
			EVP_DecryptUpdate(ctx, NULL, &outlen, ad, adLen);
 | 
								EVP_DecryptUpdate(ctx, NULL, &outlen, ad, adLen);
 | 
				
			||||||
			ret = EVP_DecryptUpdate(ctx, buf, &outlen, msg, msgLen) > 0;
 | 
								ret = EVP_DecryptUpdate(ctx, buf, &outlen, msg, msgLen) > 0;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
		EVP_CIPHER_CTX_free (ctx);	
 | 
							EVP_CIPHER_CTX_free (ctx);
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
		return ret;
 | 
							return ret;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,9 +69,9 @@ namespace crypto
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		void operator^=(const ChipherBlock& other) // XOR
 | 
							void operator^=(const ChipherBlock& other) // XOR
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
 | 
					#ifdef __AVX__
 | 
				
			||||||
			if (i2p::cpu::avx)
 | 
								if (i2p::cpu::avx)
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
#ifdef AVX
 | 
					 | 
				
			||||||
				__asm__
 | 
									__asm__
 | 
				
			||||||
					(
 | 
										(
 | 
				
			||||||
						"vmovups (%[buf]), %%xmm0 \n"
 | 
											"vmovups (%[buf]), %%xmm0 \n"
 | 
				
			||||||
| 
						 | 
					@ -82,12 +82,9 @@ namespace crypto
 | 
				
			||||||
						: [buf]"r"(buf), [other]"r"(other.buf)
 | 
											: [buf]"r"(buf), [other]"r"(other.buf)
 | 
				
			||||||
						: "%xmm0", "%xmm1", "memory"
 | 
											: "%xmm0", "%xmm1", "memory"
 | 
				
			||||||
						);
 | 
											);
 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
				for (int i = 0; i < 16; i++)
 | 
					 | 
				
			||||||
					buf[i] ^= other.buf[i];
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				// TODO: implement it better
 | 
									// TODO: implement it better
 | 
				
			||||||
				for (int i = 0; i < 16; i++)
 | 
									for (int i = 0; i < 16; i++)
 | 
				
			||||||
| 
						 | 
					@ -123,7 +120,7 @@ namespace crypto
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	#ifdef ARM64AES
 | 
						#ifdef ARM64AES
 | 
				
			||||||
		void init_aesenc(void) __attribute__((constructor));
 | 
							void init_aesenc(void) __attribute__((constructor));
 | 
				
			||||||
	#endif
 | 
						#endif
 | 
				
			||||||
| 
						 | 
					@ -143,7 +140,7 @@ namespace crypto
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	class ECBEncryption: public ECBCryptoAESNI
 | 
						class ECBEncryption: public ECBCryptoAESNI
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
	class ECBEncryption
 | 
						class ECBEncryption
 | 
				
			||||||
| 
						 | 
					@ -152,14 +149,14 @@ namespace crypto
 | 
				
			||||||
		public:
 | 
							public:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		void SetKey (const AESKey& key);
 | 
							void SetKey (const AESKey& key);
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		void Encrypt(const ChipherBlock * in, ChipherBlock * out);
 | 
							void Encrypt(const ChipherBlock * in, ChipherBlock * out);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private:
 | 
						private:
 | 
				
			||||||
		AES_KEY m_Key;
 | 
							AES_KEY m_Key;
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef AESNI
 | 
					#ifdef __AES__
 | 
				
			||||||
	class ECBDecryption: public ECBCryptoAESNI
 | 
						class ECBDecryption: public ECBCryptoAESNI
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
	class ECBDecryption
 | 
						class ECBDecryption
 | 
				
			||||||
| 
						 | 
					@ -188,7 +185,7 @@ namespace crypto
 | 
				
			||||||
			void Encrypt (const uint8_t * in, uint8_t * out); // one block
 | 
								void Encrypt (const uint8_t * in, uint8_t * out); // one block
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			ECBEncryption & ECB() { return m_ECBEncryption; }
 | 
								ECBEncryption & ECB() { return m_ECBEncryption; }
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		private:
 | 
							private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			AESAlignedBuffer<16> m_LastBlock;
 | 
								AESAlignedBuffer<16> m_LastBlock;
 | 
				
			||||||
| 
						 | 
					@ -211,7 +208,7 @@ namespace crypto
 | 
				
			||||||
			void Decrypt (const uint8_t * in, uint8_t * out); // one block
 | 
								void Decrypt (const uint8_t * in, uint8_t * out); // one block
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			ECBDecryption & ECB() { return m_ECBDecryption; }
 | 
								ECBDecryption & ECB() { return m_ECBDecryption; }
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		private:
 | 
							private:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			AESAlignedBuffer<16> m_IV;
 | 
								AESAlignedBuffer<16> m_IV;
 | 
				
			||||||
| 
						 | 
					@ -255,8 +252,8 @@ namespace crypto
 | 
				
			||||||
	};
 | 
						};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// AEAD/ChaCha20/Poly1305
 | 
					// AEAD/ChaCha20/Poly1305
 | 
				
			||||||
	bool AEADChaCha20Poly1305 (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen, const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len, bool encrypt); // msgLen is len without tag 
 | 
						bool AEADChaCha20Poly1305 (const uint8_t * msg, size_t msgLen, const uint8_t * ad, size_t adLen, const uint8_t * key, const uint8_t * nonce, uint8_t * buf, size_t len, bool encrypt); // msgLen is len without tag
 | 
				
			||||||
   	
 | 
					
 | 
				
			||||||
// init and terminate
 | 
					// init and terminate
 | 
				
			||||||
	void InitCrypto (bool precomputation);
 | 
						void InitCrypto (bool precomputation);
 | 
				
			||||||
	void TerminateCrypto ();
 | 
						void TerminateCrypto ();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -719,24 +719,29 @@ namespace data
 | 
				
			||||||
	XORMetric operator^(const IdentHash& key1, const IdentHash& key2)
 | 
						XORMetric operator^(const IdentHash& key1, const IdentHash& key2)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		XORMetric m;
 | 
							XORMetric m;
 | 
				
			||||||
#if defined(__AVX__) // for AVX
 | 
					#ifdef __AVX__
 | 
				
			||||||
		__asm__
 | 
							if(i2p::cpu::avx)
 | 
				
			||||||
		(
 | 
							{
 | 
				
			||||||
			"vmovups %1, %%ymm0 \n"
 | 
								__asm__
 | 
				
			||||||
			"vmovups %2, %%ymm1 \n"
 | 
								(
 | 
				
			||||||
			"vxorps %%ymm0, %%ymm1, %%ymm1 \n"
 | 
									"vmovups %1, %%ymm0 \n"
 | 
				
			||||||
			"vmovups %%ymm1, %0 \n"
 | 
									"vmovups %2, %%ymm1 \n"
 | 
				
			||||||
			: "=m"(*m.metric)
 | 
									"vxorps %%ymm0, %%ymm1, %%ymm1 \n"
 | 
				
			||||||
			: "m"(*key1), "m"(*key2)
 | 
									"vmovups %%ymm1, %0 \n"
 | 
				
			||||||
			: "memory", "%xmm0", "%xmm1" // should be replaced by %ymm0/1 once supported by compiler
 | 
									: "=m"(*m.metric)
 | 
				
			||||||
		);
 | 
									: "m"(*key1), "m"(*key2)
 | 
				
			||||||
#else
 | 
									: "memory", "%xmm0", "%xmm1" // should be replaced by %ymm0/1 once supported by compiler
 | 
				
			||||||
		const uint64_t * hash1 = key1.GetLL (), * hash2 = key2.GetLL ();
 | 
								);
 | 
				
			||||||
		m.metric_ll[0] = hash1[0] ^ hash2[0];
 | 
							}
 | 
				
			||||||
		m.metric_ll[1] = hash1[1] ^ hash2[1];
 | 
							else
 | 
				
			||||||
		m.metric_ll[2] = hash1[2] ^ hash2[2];
 | 
					 | 
				
			||||||
		m.metric_ll[3] = hash1[3] ^ hash2[3];
 | 
					 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								const uint64_t * hash1 = key1.GetLL (), * hash2 = key2.GetLL ();
 | 
				
			||||||
 | 
								m.metric_ll[0] = hash1[0] ^ hash2[0];
 | 
				
			||||||
 | 
								m.metric_ll[1] = hash1[1] ^ hash2[1];
 | 
				
			||||||
 | 
								m.metric_ll[2] = hash1[2] ^ hash2[2];
 | 
				
			||||||
 | 
								m.metric_ll[3] = hash1[3] ^ hash2[3];
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		return m;
 | 
							return m;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue