【问题标题】:Within my c++ program is there a way to check if the CPU has AES-NI在我的 c++ 程序中有一种方法可以检查 CPU 是否有 AES-NI
【发布时间】:2016-08-05 22:58:49
【问题描述】:

我希望能够通过 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++ 做同样的事情? (海合会)

【问题讨论】:

    标签: c++ windows gcc mingw aes-ni


    【解决方案1】:

    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

    【讨论】:

    • 这看起来会起作用..我会尽快接受你的回答,只要我在 mingw 中工作(我是新的)
    【解决方案2】:

    对于傻笑,你可以这样做......

    int have_aes_ni = (((int(*)())"1\xc0\xb0\1\xf\xa2\x89\xc8\xc3")()>>25)&1;
    

    ...但是您必须将/SECTION:.data,RWE 传递给链接器,您的专业同事会不喜欢您。您可以吹嘘您的代码在 Linux 中的工作方式,因为 Security 会护送您离开设施。

    在自 Visual C++ 2005 以来的 Windows 上,您可以执行类似的操作

    #include<intrin.h>
    
    ...
    
      int regs[4];
      __cpuid( regs, 1 );
      int have_aes_ni = ( regs[2] >> 25 ) & 1;
    

    编辑:我没有发现您正在使用 mingw,其中 intrin.h 可能不可用。在那种情况下,J J. Hakala 的回答可能会更好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 1970-01-01
      • 2020-10-21
      • 2019-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多