在我的 C++ 程序中有一种方法可以检查 CPU 是否有 AES-NI
Within my c++ program is there a way to check if the CPU has AES-NI
我希望能够使用 windows 上的 C++ 代码检查 CPU 是否有可用的 AES-NI。 (MinGW GCC)
我用 visual studio 找到了用 C# 编写的解决方案。
Test for AES-NI instructions from C#
private static bool IsAESNIPresent()
{
byte[] sn = new byte[16]; // !!! Here were 8 bytes
if (!ExecuteCode(ref sn))
return false;
var ecx = BitConverter.ToUInt32(sn, 8);
return (ecx & (1 << 25)) != 0;
}
有没有一种简单的方法可以用 C++ 做同样的事情? (海湾合作委员会)
pycrypto 中有一些代码似乎适用。我把代码的重要部分用于测试:
#include <cpuid.h>
#include <stdint.h>
#include <stdio.h>
int main()
{
uint32_t eax, ebx, ecx, edx;
eax = ebx = ecx = edx = 0;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
printf("%08x %08x %08x %08x\n", eax, ebx, ecx, edx);
printf("Has AES-NI: %d\n", (ecx & bit_AES) > 0);
return 0;
}
结果似乎与/proc/cpuinfo提供的信息或intel网页提供的信息一致。
有关其他功能,请参阅 clang documentation
为了咯咯笑你可以这样做...
int have_aes_ni = (((int(*)())"1\xc0\xb0\xf\xa2\x89\xc8\xc3")()>>25)&1;
...但是您必须将 /SECTION:.data,RWE
传递给链接器,您的专业同事会对您皱眉。当安全护送您离开设施时,您可以吹嘘您的代码在 Linux 中如何工作。
在 Windows 自 Visual C++ 2005 以来,您可以执行类似
的操作
#include<intrin.h>
...
int regs[4];
__cpuid( regs, 1 );
int have_aes_ni = ( regs[2] >> 25 ) & 1;
编辑:我没发现您使用的是 mingw,其中 intrin.h
可能不可用。在那种情况下,J J. Hakala 的答案可能更好。
我希望能够使用 windows 上的 C++ 代码检查 CPU 是否有可用的 AES-NI。 (MinGW GCC)
我用 visual studio 找到了用 C# 编写的解决方案。
Test for AES-NI instructions from C#
private static bool IsAESNIPresent()
{
byte[] sn = new byte[16]; // !!! Here were 8 bytes
if (!ExecuteCode(ref sn))
return false;
var ecx = BitConverter.ToUInt32(sn, 8);
return (ecx & (1 << 25)) != 0;
}
有没有一种简单的方法可以用 C++ 做同样的事情? (海湾合作委员会)
pycrypto 中有一些代码似乎适用。我把代码的重要部分用于测试:
#include <cpuid.h>
#include <stdint.h>
#include <stdio.h>
int main()
{
uint32_t eax, ebx, ecx, edx;
eax = ebx = ecx = edx = 0;
__get_cpuid(1, &eax, &ebx, &ecx, &edx);
printf("%08x %08x %08x %08x\n", eax, ebx, ecx, edx);
printf("Has AES-NI: %d\n", (ecx & bit_AES) > 0);
return 0;
}
结果似乎与/proc/cpuinfo提供的信息或intel网页提供的信息一致。
有关其他功能,请参阅 clang documentation
为了咯咯笑你可以这样做...
int have_aes_ni = (((int(*)())"1\xc0\xb0\xf\xa2\x89\xc8\xc3")()>>25)&1;
...但是您必须将 /SECTION:.data,RWE
传递给链接器,您的专业同事会对您皱眉。当安全护送您离开设施时,您可以吹嘘您的代码在 Linux 中如何工作。
在 Windows 自 Visual C++ 2005 以来,您可以执行类似
的操作#include<intrin.h>
...
int regs[4];
__cpuid( regs, 1 );
int have_aes_ni = ( regs[2] >> 25 ) & 1;
编辑:我没发现您使用的是 mingw,其中 intrin.h
可能不可用。在那种情况下,J J. Hakala 的答案可能更好。