测试从C#AES-NI指令 [英] Test for AES-NI instructions from C#

查看:351
本文介绍了测试从C#AES-NI指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有从C#.NET主机系统的CPU中以测试的存在的AES-NI的一种方式。

I want to know if there is a way to test for the presence of AES-NI in the host system's CPU from C#.NET.

让我说在前面,这个问题是不会询问如何的从.NET中使用的AES-NI。事实证明,简单地使用 AESCryptoServiceProvider 将使用AES-NI(如果可用)。这个结果是基于独立基准我做了比较 AESCryptoServiceProvider 的演出反对TrueCrypt的规定的基准,它确实支持AES-NI。结果是在具有和不具有AES-NI两台机器惊人的相似。

Let me say up front that this question is not asking about how to use AES-NI from .NET. It turns out simply using AESCryptoServiceProvider will use AES-NI if it is available. This result is based on independent benchmarks I did comparing the performances of AESCryptoServiceProvider against the benchmarks provided in TrueCrypt, which does indeed support AES-NI. The results were surprisingly similar on both machines with and without AES-NI.

我希望能够以测试它的原因是为了能够指示给用户的自己的电脑支持AES-NI。这将是重要,因为它会降低涉及这样的问题的支持事件,但我的朋友有一个酷睿i5也不过他是快了很多!如果该程序的用户界面可以指示他们的系统或不支持AES-NI的用户,它也将是可能的,以指示较慢的性能是正常的,因为这种系统不支持AES-NI。

The reason I want to be able to test for it is to be able to indicate to the user that their computer supports AES-NI. This would be relevant since it would reduce support incidents involving questions like "but my friend has a Core i5 also but his is a lot faster!" If the program's user interface could indicate to the user that their system does or does not support AES-NI, it would also be possible to indicate that "slower performance is normal since this system does not support AES-NI."

(我们可以感谢英特尔对所有不同的处理器步进的混乱!:-))

(We can thank Intel for all of the confusion with different processor steppings! :-) )

有没有办法来?检测出这些信息,也许通过WMI

Is there a way to detect this information, perhaps through WMI?

推荐答案

它似乎有在这样类似的问题:的内联汇编代码获取与伟大的答案CPU ID

It's seems that there is the similar question on SO: Inline Assembly Code to Get CPU ID with the great answer.

不过,这个答案需要一些调整,以满足您的需求。

But this answer requires some adjustments to suit your need.

首先,据我了解,AES-NI可以在64位处理器的存在只是,对不对?然后,你可以忽略上面的回答所有的32位代码

First, as I understand, AES-NI can be presence at 64-bit processors only, right? Then you could ignore all 32-bit code in the answer above.

其次,你需要ECX注册或者更确切地说,它的第25位,因此必须更改代码位:

Second, you need ECX register or rather its 25th bit, so you must change code a bit:

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;
}



最后,你需要在阵列存储ECX寄存器:

Finally, you need store ECX register in array:

byte[] code_x64 = new byte[] {
    0x53,                                     /* push rbx */
    0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, /* mov rax, 0x1 */
    0x0f, 0xa2,                               /* cpuid */
    0x41, 0x89, 0x00,                         /* mov [r8], eax */
    0x41, 0x89, 0x50, 0x04,                   /* mov [r8+0x4], ebx !!! changed */
    0x41, 0x89, 0x50, 0x08,                   /* mov [r8+0x8], ecx !!! added */
    0x41, 0x89, 0x50, 0x0C,                   /* mov [r8+0xC], edx !!! added*/
    0x5b,                                     /* pop rbx */
    0xc3,                                     /* ret */
};



据我所看到的,这一切的改变。

As far as I can see, that's all changes.

这篇关于测试从C#AES-NI指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆