Skip to content

Commit

Permalink
Merge pull request #1968 from PurpleI2P/cpu
Browse files Browse the repository at this point in the history
[cpu] use short defines instead of long checks
  • Loading branch information
orignal authored Sep 17, 2023
2 parents 9c25a88 + 1857f14 commit dd32f1e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
4 changes: 2 additions & 2 deletions libi2pd/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions libi2pd/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 5 additions & 10 deletions libi2pd/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions libi2pd/Crypto.h
Original file line number Diff line number Diff line change
@@ -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
*
Expand Down Expand Up @@ -150,7 +150,7 @@ namespace crypto
};


#ifdef __AES__
#if SUPPORTS_AES
class ECBCryptoAESNI
{
public:
Expand All @@ -167,7 +167,7 @@ namespace crypto
};
#endif

#ifdef __AES__
#if SUPPORTS_AES
class ECBEncryption: public ECBCryptoAESNI
#else
class ECBEncryption
Expand All @@ -183,7 +183,7 @@ namespace crypto
AES_KEY m_Key;
};

#ifdef __AES__
#if SUPPORTS_AES
class ECBDecryption: public ECBCryptoAESNI
#else
class ECBDecryption
Expand Down

0 comments on commit dd32f1e

Please sign in to comment.