如何从 SynCrypto select AES 加密版本?
How to select AES Encryption version from SynCrypto?
SynCrypto 是 this pack.
中包含的用于数据加密的开源库
在这个单元 (SynCrypto.pas) 中,文件开头的注释如下:
AES128 cypher 84 MB/s uncypher 81 MB/s asm version
AES128 cypher 57 MB/s uncypher 57 MB/s pascal version
AES192 cypher 72 MB/s uncypher 70 MB/s asm version
AES192 cypher 48 MB/s uncypher 48 MB/s pascal version
AES256 cypher 62 MB/s uncypher 61 MB/s asm version
AES256 cypher 42 MB/s uncypher 42 MB/s pascal version
由此我了解到有两个版本的代码:pascal-slower 和 asm-faster。但问题是,我怎么能在这两者之间select?
编辑:第二个问题,如果你允许的话:我如何 select 密钥的位宽:128、192 和 256?
我用这段代码加解密:
outp := TAESCFB.SimpleEncrypt(inp,'privatekey',true,true);
inp := TAESCFB.SimpleEncrypt(outp,'privatekey',false,true);
...和 Delphi 2009.
简答: 最快的代码将始终为您使用,具体取决于编译器和计算机。
长答案:
一次只能编译一个代码版本,要么是pascal,要么是optimized asm。它取决于单元中 运行ning 时的条件定义,根据您的编译器版本(从 Delphi 5 到最新的 Delphi 或 FPC 修订版)和 CPU 自动设置平台(如果它有 Intel 或 ARM)。然后在 运行 时间,在 Intel 上,可以检测和使用 AES-NI 硬件操作码,或者在 VIA 上,可以检测和使用 Padlock 硬件操作码。甚至可以滚动或展开 pascal 实现。
{$define AES_ROLLED}
// if defined, use rolled version, which is faster (at least on my AMD CPU)
{$ifdef DELPHI5OROLDER}
{$define AES_PASCAL} // Delphi 5 internal asm is buggy :(
{$define SHA3_PASCAL}
{$else}
{$ifdef CPUINTEL} // AES-NI supported for x86 and x64
{$ifdef CPU64}
{$ifdef HASAESNI}
{$define USEAESNI}
{$define USEAESNI64}
{$else}
{$define AES_PASCAL} // Delphi XE2/XE3 do not have the AES-NI opcodes :(
{$endif}
{$define AESPASCAL_OR_CPU64}
{$else}
{$define USEAESNI}
{$define USEAESNI32}
{$endif}
{$else}
{$define AES_PASCAL} // AES128 unrolled pascal(Delphi7)=57MB/s rolled asm=84MB/s :)
{$define SHA3_PASCAL}
{$endif CPUINTEL}
{$endif}
总结一下,当使用 AES 时,该单元有六个不同版本的算法:pascal rolled、pascal unrolled、x86/x64 asm without AES-NI、x86/x64 asm with AES-NI , x86 带挂锁, Windows API.
简单加密:
有两个重载的 SimpleEncrypt
方法。一个固定大小为 256 位(您使用的那个),另一个带有 KeySize
参数(128、192 或 256):
class function TAESAbstract.SimpleEncrypt(const Input: RawByteString; const Key;
KeySize: integer; Encrypt, IVAtBeginning: boolean): RawByteString;
当然,你应该先hash将输入的密码文本转换成THash128或THash256密钥,然后再调用该函数。为此,我们建议使用 PBKDF2_HMAC_SHA256
(具有较高的整数)。
一些数字:
使用包括 AES-NI 在内的现代 CPU,您将获得惊人的性能。这是 Delphi/FPC 中最快的本机 AES 实现,当然(没有其他使用 AES-NI)。一些数字 运行 我的笔记本电脑,取自 TTestCryptographicRoutines.Benchmark
回归测试:
50000 AES128CFB in 229.60ms i.e. 217764/s or 463.4 MB/s
50000 AES128OFB in 183.66ms i.e. 272237/s or 579.3 MB/s
50000 AES128CFBCRC in 227.61ms i.e. 219674/s or 467.5 MB/s
50000 AES128OFBCRC in 181.18ms i.e. 275954/s or 587.2 MB/s
50000 AES256CFB in 288.57ms i.e. 173265/s or 368.7 MB/s
50000 AES256OFB in 243.96ms i.e. 204944/s or 436.1 MB/s
50000 AES256CFBCRC in 294.38ms i.e. 169844/s or 361.4 MB/s
50000 AES256OFBCRC in 244.49ms i.e. 204507/s or 435.2 MB/s
50000 SHAKE128 in 473.34ms i.e. 105631/s or 224.8 MB/s
50000 SHAKE256 in 596.74ms i.e. 83787/s or 178.3 MB/s
忘掉 Windows API,相比之下它们非常慢。 TAES...CRC
class 在压缩过程中将 128 位 crc32c 添加到输入和输出,而不会降低速度,以进行身份验证和验证内容。我还包括了 SHAKE128/SHAKE256 加密(不兼容 AES),它与一步中的 HMAC-SHA-256 + AES-256 相当,并且在没有 AES-NI 的所有 CPU 上都非常快。
SynCrypto 是 this pack.
中包含的用于数据加密的开源库在这个单元 (SynCrypto.pas) 中,文件开头的注释如下:
AES128 cypher 84 MB/s uncypher 81 MB/s asm version
AES128 cypher 57 MB/s uncypher 57 MB/s pascal version
AES192 cypher 72 MB/s uncypher 70 MB/s asm version
AES192 cypher 48 MB/s uncypher 48 MB/s pascal version
AES256 cypher 62 MB/s uncypher 61 MB/s asm version
AES256 cypher 42 MB/s uncypher 42 MB/s pascal version
由此我了解到有两个版本的代码:pascal-slower 和 asm-faster。但问题是,我怎么能在这两者之间select?
编辑:第二个问题,如果你允许的话:我如何 select 密钥的位宽:128、192 和 256?
我用这段代码加解密:
outp := TAESCFB.SimpleEncrypt(inp,'privatekey',true,true);
inp := TAESCFB.SimpleEncrypt(outp,'privatekey',false,true);
...和 Delphi 2009.
简答: 最快的代码将始终为您使用,具体取决于编译器和计算机。
长答案:
一次只能编译一个代码版本,要么是pascal,要么是optimized asm。它取决于单元中 运行ning 时的条件定义,根据您的编译器版本(从 Delphi 5 到最新的 Delphi 或 FPC 修订版)和 CPU 自动设置平台(如果它有 Intel 或 ARM)。然后在 运行 时间,在 Intel 上,可以检测和使用 AES-NI 硬件操作码,或者在 VIA 上,可以检测和使用 Padlock 硬件操作码。甚至可以滚动或展开 pascal 实现。
{$define AES_ROLLED}
// if defined, use rolled version, which is faster (at least on my AMD CPU)
{$ifdef DELPHI5OROLDER}
{$define AES_PASCAL} // Delphi 5 internal asm is buggy :(
{$define SHA3_PASCAL}
{$else}
{$ifdef CPUINTEL} // AES-NI supported for x86 and x64
{$ifdef CPU64}
{$ifdef HASAESNI}
{$define USEAESNI}
{$define USEAESNI64}
{$else}
{$define AES_PASCAL} // Delphi XE2/XE3 do not have the AES-NI opcodes :(
{$endif}
{$define AESPASCAL_OR_CPU64}
{$else}
{$define USEAESNI}
{$define USEAESNI32}
{$endif}
{$else}
{$define AES_PASCAL} // AES128 unrolled pascal(Delphi7)=57MB/s rolled asm=84MB/s :)
{$define SHA3_PASCAL}
{$endif CPUINTEL}
{$endif}
总结一下,当使用 AES 时,该单元有六个不同版本的算法:pascal rolled、pascal unrolled、x86/x64 asm without AES-NI、x86/x64 asm with AES-NI , x86 带挂锁, Windows API.
简单加密:
有两个重载的 SimpleEncrypt
方法。一个固定大小为 256 位(您使用的那个),另一个带有 KeySize
参数(128、192 或 256):
class function TAESAbstract.SimpleEncrypt(const Input: RawByteString; const Key;
KeySize: integer; Encrypt, IVAtBeginning: boolean): RawByteString;
当然,你应该先hash将输入的密码文本转换成THash128或THash256密钥,然后再调用该函数。为此,我们建议使用 PBKDF2_HMAC_SHA256
(具有较高的整数)。
一些数字:
使用包括 AES-NI 在内的现代 CPU,您将获得惊人的性能。这是 Delphi/FPC 中最快的本机 AES 实现,当然(没有其他使用 AES-NI)。一些数字 运行 我的笔记本电脑,取自 TTestCryptographicRoutines.Benchmark
回归测试:
50000 AES128CFB in 229.60ms i.e. 217764/s or 463.4 MB/s
50000 AES128OFB in 183.66ms i.e. 272237/s or 579.3 MB/s
50000 AES128CFBCRC in 227.61ms i.e. 219674/s or 467.5 MB/s
50000 AES128OFBCRC in 181.18ms i.e. 275954/s or 587.2 MB/s
50000 AES256CFB in 288.57ms i.e. 173265/s or 368.7 MB/s
50000 AES256OFB in 243.96ms i.e. 204944/s or 436.1 MB/s
50000 AES256CFBCRC in 294.38ms i.e. 169844/s or 361.4 MB/s
50000 AES256OFBCRC in 244.49ms i.e. 204507/s or 435.2 MB/s
50000 SHAKE128 in 473.34ms i.e. 105631/s or 224.8 MB/s
50000 SHAKE256 in 596.74ms i.e. 83787/s or 178.3 MB/s
忘掉 Windows API,相比之下它们非常慢。 TAES...CRC
class 在压缩过程中将 128 位 crc32c 添加到输入和输出,而不会降低速度,以进行身份验证和验证内容。我还包括了 SHAKE128/SHAKE256 加密(不兼容 AES),它与一步中的 HMAC-SHA-256 + AES-256 相当,并且在没有 AES-NI 的所有 CPU 上都非常快。