Browse Source

[cpu] use short defines instead of long checks

Signed-off-by: r4sas <r4sas@i2pmail.org>
cpu
R4SAS 8 months ago
parent
commit
1857f14be8
Signed by: r4sas
GPG Key ID: 66F6C87B98EBCFE2
  1. 4
      libi2pd/CPU.cpp
  2. 18
      libi2pd/CPU.h
  3. 15
      libi2pd/Crypto.cpp
  4. 8
      libi2pd/Crypto.h

4
libi2pd/CPU.cpp

@ -13,7 +13,7 @@
#define bit_AES (1 << 25) #define bit_AES (1 << 25)
#endif #endif
#if (defined(__GNUC__) && __GNUC__ < 5 && (defined(__x86_64__) || defined(__i386__))) #if defined(__GNUC__) && __GNUC__ < 5 && IS_X86
#include <cpuid.h> #include <cpuid.h>
#endif #endif
@ -29,7 +29,7 @@ namespace cpu
inline bool cpu_support_aes() inline bool cpu_support_aes()
{ {
#if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_IX86) || defined(__i386__)) #if IS_X86
#if defined(__clang__) #if defined(__clang__)
# if (__clang_major__ >= 6) # if (__clang_major__ >= 6)
__builtin_cpu_init(); __builtin_cpu_init();

18
libi2pd/CPU.h

@ -9,6 +9,24 @@
#ifndef LIBI2PD_CPU_H #ifndef LIBI2PD_CPU_H
#define LIBI2PD_CPU_H #define LIBI2PD_CPU_H
#if defined(_M_AMD64) || defined(__x86_64__) || defined(_M_IX86) || defined(__i386__)
# define IS_X86 1
# if defined(_M_AMD64) || defined(__x86_64__)
# define IS_X86_64 1
# else
# define IS_X86_64 0
# endif
#else
# define IS_X86 0
# define IS_X86_64 0
#endif
#if defined(__AES__) && !defined(_MSC_VER) && IS_X86
# define SUPPORTS_AES 1
#else
# define SUPPORTS_AES 0
#endif
namespace i2p namespace i2p
{ {
namespace cpu namespace cpu

15
libi2pd/Crypto.cpp

@ -28,11 +28,6 @@
#include "I2PEndian.h" #include "I2PEndian.h"
#include "Log.h" #include "Log.h"
#if defined(__AES__) && !defined(_MSC_VER) && ((defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_IX86) || defined(__i386__)))
#define SUPPORTS_AES 1
#else
#define SUPPORTS_AES 0
#endif
namespace i2p namespace i2p
{ {
@ -165,7 +160,7 @@ namespace crypto
// DH/ElGamal // DH/ElGamal
#if !defined(__x86_64__) #if !IS_X86_64
const int ELGAMAL_SHORT_EXPONENT_NUM_BITS = 226; const int ELGAMAL_SHORT_EXPONENT_NUM_BITS = 226;
const int ELGAMAL_SHORT_EXPONENT_NUM_BYTES = ELGAMAL_SHORT_EXPONENT_NUM_BITS/8+1; const int ELGAMAL_SHORT_EXPONENT_NUM_BYTES = ELGAMAL_SHORT_EXPONENT_NUM_BITS/8+1;
#endif #endif
@ -367,7 +362,7 @@ namespace crypto
BIGNUM * b1 = BN_CTX_get (ctx); BIGNUM * b1 = BN_CTX_get (ctx);
BIGNUM * b = BN_CTX_get (ctx); BIGNUM * b = BN_CTX_get (ctx);
// select random k // select random k
#if (defined(_M_AMD64) || defined(__x86_64__)) #if IS_X86_64
BN_rand (k, ELGAMAL_FULL_EXPONENT_NUM_BITS, -1, 1); // full exponent for x64 BN_rand (k, ELGAMAL_FULL_EXPONENT_NUM_BITS, -1, 1); // full exponent for x64
#else #else
BN_rand (k, ELGAMAL_SHORT_EXPONENT_NUM_BITS, -1, 1); // short exponent of 226 bits BN_rand (k, ELGAMAL_SHORT_EXPONENT_NUM_BITS, -1, 1); // short exponent of 226 bits
@ -434,7 +429,7 @@ namespace crypto
void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub) void GenerateElGamalKeyPair (uint8_t * priv, uint8_t * pub)
{ {
#if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_IX86) || defined(__i386__)) || defined(_MSC_VER) #if IS_X86 || defined(_MSC_VER)
RAND_bytes (priv, 256); RAND_bytes (priv, 256);
#else #else
// lower 226 bits (28 bytes and 2 bits) only. short exponent // lower 226 bits (28 bytes and 2 bits) only. short exponent
@ -1309,7 +1304,7 @@ namespace crypto
CRYPTO_set_locking_callback (OpensslLockingCallback);*/ CRYPTO_set_locking_callback (OpensslLockingCallback);*/
if (precomputation) if (precomputation)
{ {
#if (defined(_M_AMD64) || defined(__x86_64__)) #if IS_X86_64
g_ElggTable = new BIGNUM * [ELGAMAL_FULL_EXPONENT_NUM_BYTES][255]; g_ElggTable = new BIGNUM * [ELGAMAL_FULL_EXPONENT_NUM_BYTES][255];
PrecalculateElggTable (g_ElggTable, ELGAMAL_FULL_EXPONENT_NUM_BYTES); PrecalculateElggTable (g_ElggTable, ELGAMAL_FULL_EXPONENT_NUM_BYTES);
#else #else
@ -1324,7 +1319,7 @@ namespace crypto
if (g_ElggTable) if (g_ElggTable)
{ {
DestroyElggTable (g_ElggTable, DestroyElggTable (g_ElggTable,
#if (defined(_M_AMD64) || defined(__x86_64__)) #if IS_X86_64
ELGAMAL_FULL_EXPONENT_NUM_BYTES ELGAMAL_FULL_EXPONENT_NUM_BYTES
#else #else
ELGAMAL_SHORT_EXPONENT_NUM_BYTES ELGAMAL_SHORT_EXPONENT_NUM_BYTES

8
libi2pd/Crypto.h

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2013-2022, The PurpleI2P Project * Copyright (c) 2013-2023, The PurpleI2P Project
* *
* This file is part of Purple i2pd project and licensed under BSD3 * This file is part of Purple i2pd project and licensed under BSD3
* *
@ -150,7 +150,7 @@ namespace crypto
}; };
#ifdef __AES__ #if SUPPORTS_AES
class ECBCryptoAESNI class ECBCryptoAESNI
{ {
public: public:
@ -167,7 +167,7 @@ namespace crypto
}; };
#endif #endif
#ifdef __AES__ #if SUPPORTS_AES
class ECBEncryption: public ECBCryptoAESNI class ECBEncryption: public ECBCryptoAESNI
#else #else
class ECBEncryption class ECBEncryption
@ -183,7 +183,7 @@ namespace crypto
AES_KEY m_Key; AES_KEY m_Key;
}; };
#ifdef __AES__ #if SUPPORTS_AES
class ECBDecryption: public ECBCryptoAESNI class ECBDecryption: public ECBCryptoAESNI
#else #else
class ECBDecryption class ECBDecryption

Loading…
Cancel
Save