Browse Source

Merge pull request #1968 from PurpleI2P/cpu

[cpu] use short defines instead of long checks
pull/1970/head 2.49.0
orignal 1 year ago committed by GitHub
parent
commit
dd32f1ed6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  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 @@ @@ -13,7 +13,7 @@
#define bit_AES (1 << 25)
#endif
#if (defined(__GNUC__) && __GNUC__ < 5 && (defined(__x86_64__) || defined(__i386__)))
#if defined(__GNUC__) && __GNUC__ < 5 && IS_X86
#include <cpuid.h>
#endif
@ -29,7 +29,7 @@ namespace cpu @@ -29,7 +29,7 @@ namespace cpu
inline bool cpu_support_aes()
{
#if (defined(_M_AMD64) || defined(__x86_64__)) || (defined(_M_IX86) || defined(__i386__))
#if IS_X86
#if defined(__clang__)
# if (__clang_major__ >= 6)
__builtin_cpu_init();

18
libi2pd/CPU.h

@ -9,6 +9,24 @@ @@ -9,6 +9,24 @@
#ifndef 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 cpu

15
libi2pd/Crypto.cpp

@ -28,11 +28,6 @@ @@ -28,11 +28,6 @@
#include "I2PEndian.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
{
@ -165,7 +160,7 @@ namespace crypto @@ -165,7 +160,7 @@ namespace crypto
// DH/ElGamal
#if !defined(__x86_64__)
#if !IS_X86_64
const int ELGAMAL_SHORT_EXPONENT_NUM_BITS = 226;
const int ELGAMAL_SHORT_EXPONENT_NUM_BYTES = ELGAMAL_SHORT_EXPONENT_NUM_BITS/8+1;
#endif
@ -367,7 +362,7 @@ namespace crypto @@ -367,7 +362,7 @@ namespace crypto
BIGNUM * b1 = BN_CTX_get (ctx);
BIGNUM * b = BN_CTX_get (ctx);
// 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
#else
BN_rand (k, ELGAMAL_SHORT_EXPONENT_NUM_BITS, -1, 1); // short exponent of 226 bits
@ -434,7 +429,7 @@ namespace crypto @@ -434,7 +429,7 @@ namespace crypto
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);
#else
// lower 226 bits (28 bytes and 2 bits) only. short exponent
@ -1309,7 +1304,7 @@ namespace crypto @@ -1309,7 +1304,7 @@ namespace crypto
CRYPTO_set_locking_callback (OpensslLockingCallback);*/
if (precomputation)
{
#if (defined(_M_AMD64) || defined(__x86_64__))
#if IS_X86_64
g_ElggTable = new BIGNUM * [ELGAMAL_FULL_EXPONENT_NUM_BYTES][255];
PrecalculateElggTable (g_ElggTable, ELGAMAL_FULL_EXPONENT_NUM_BYTES);
#else
@ -1324,7 +1319,7 @@ namespace crypto @@ -1324,7 +1319,7 @@ namespace crypto
if (g_ElggTable)
{
DestroyElggTable (g_ElggTable,
#if (defined(_M_AMD64) || defined(__x86_64__))
#if IS_X86_64
ELGAMAL_FULL_EXPONENT_NUM_BYTES
#else
ELGAMAL_SHORT_EXPONENT_NUM_BYTES

8
libi2pd/Crypto.h

@ -1,5 +1,5 @@ @@ -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
*
@ -150,7 +150,7 @@ namespace crypto @@ -150,7 +150,7 @@ namespace crypto
};
#ifdef __AES__
#if SUPPORTS_AES
class ECBCryptoAESNI
{
public:
@ -167,7 +167,7 @@ namespace crypto @@ -167,7 +167,7 @@ namespace crypto
};
#endif
#ifdef __AES__
#if SUPPORTS_AES
class ECBEncryption: public ECBCryptoAESNI
#else
class ECBEncryption
@ -183,7 +183,7 @@ namespace crypto @@ -183,7 +183,7 @@ namespace crypto
AES_KEY m_Key;
};
#ifdef __AES__
#if SUPPORTS_AES
class ECBDecryption: public ECBCryptoAESNI
#else
class ECBDecryption

Loading…
Cancel
Save