2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2023-08-31 19:52:51 +03:00
|
|
|
* Copyright (c) 2013-2023, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2018-02-16 19:01:04 +03:00
|
|
|
#include "CPU.h"
|
|
|
|
#include "Log.h"
|
2018-02-19 18:15:39 +03:00
|
|
|
|
|
|
|
#ifndef bit_AES
|
2023-08-31 19:52:51 +03:00
|
|
|
#define bit_AES (1 << 25)
|
2018-02-19 18:15:39 +03:00
|
|
|
#endif
|
2023-09-06 23:26:46 +03:00
|
|
|
|
2023-09-18 20:11:45 +03:00
|
|
|
#if defined(__GNUC__) && __GNUC__ < 6 && IS_X86
|
2023-09-06 23:26:46 +03:00
|
|
|
#include <cpuid.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#include <intrin.h>
|
2018-02-19 18:15:39 +03:00
|
|
|
#endif
|
|
|
|
|
2018-02-16 19:01:04 +03:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace cpu
|
|
|
|
{
|
|
|
|
bool aesni = false;
|
|
|
|
|
2023-08-31 19:52:51 +03:00
|
|
|
inline bool cpu_support_aes()
|
2018-02-16 19:01:04 +03:00
|
|
|
{
|
2023-09-17 21:33:42 +03:00
|
|
|
#if IS_X86
|
2023-09-08 00:20:52 +03:00
|
|
|
#if defined(__clang__)
|
|
|
|
# if (__clang_major__ >= 6)
|
2023-09-06 23:26:46 +03:00
|
|
|
__builtin_cpu_init();
|
2023-09-08 00:20:52 +03:00
|
|
|
# endif
|
2023-09-06 23:26:46 +03:00
|
|
|
return __builtin_cpu_supports("aes");
|
2023-09-18 21:12:26 +03:00
|
|
|
#elif (defined(__GNUC__) && __GNUC__ >= 6)
|
2023-08-31 19:52:51 +03:00
|
|
|
__builtin_cpu_init();
|
|
|
|
return __builtin_cpu_supports("aes");
|
2023-09-18 21:12:26 +03:00
|
|
|
#elif (defined(__GNUC__) && __GNUC__ < 6)
|
2023-09-08 00:20:52 +03:00
|
|
|
int cpu_info[4];
|
|
|
|
bool flag = false;
|
|
|
|
__cpuid(0, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
|
|
|
|
if (cpu_info[0] >= 0x00000001) {
|
|
|
|
__cpuid(0x00000001, cpu_info[0], cpu_info[1], cpu_info[2], cpu_info[3]);
|
|
|
|
flag = ((cpu_info[2] & bit_AES) != 0);
|
|
|
|
}
|
|
|
|
return flag;
|
|
|
|
#elif defined(_MSC_VER)
|
2023-09-06 23:26:46 +03:00
|
|
|
int cpu_info[4];
|
|
|
|
__cpuid(cpu_info, 1);
|
|
|
|
return ((cpu_info[2] & bit_AES) != 0);
|
2020-11-16 03:38:52 +03:00
|
|
|
#endif
|
2023-09-08 00:20:52 +03:00
|
|
|
#endif
|
|
|
|
return false;
|
2023-08-31 19:52:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Detect(bool AesSwitch, bool force)
|
|
|
|
{
|
|
|
|
if ((cpu_support_aes() && AesSwitch) || (AesSwitch && force)) {
|
|
|
|
aesni = true;
|
2018-02-16 19:01:04 +03:00
|
|
|
}
|
2018-06-27 12:09:46 +03:00
|
|
|
|
2020-11-15 01:31:20 +03:00
|
|
|
LogPrint(eLogInfo, "AESNI ", (aesni ? "enabled" : "disabled"));
|
2018-02-16 19:01:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|