简体   繁体   English

使用 VS2017 的 Crypto++ AES 在发布模式下崩溃

[英]Crypto++ AES crashing in release mode using VS2017

I have the following code.我有以下代码。 A very simple example of encryption and decryption of string "abcd".字符串“abcd”的加密和解密的一个非常简单的例子。 I've tried it with sample from Crypto++ ( https://www.cryptopp.com/wiki/CBC_mode ) and it produces the same exception.我已经用来自 Crypto++ ( https://www.cryptopp.com/wiki/CBC_mode ) 的示例尝试过它,它产生了相同的异常。

AutoSeededRandomPool rand;

SecByteBlock key(nullptr, AES::MAX_KEYLENGTH);
rand.GenerateBlock(key, key.size());

byte iv[AES::BLOCKSIZE];
rand.GenerateBlock(iv, AES::BLOCKSIZE);

std::string encryptedData;
CBC_Mode<AES>::Encryption cbcEncryption(key, key.size(), iv);

StringSource ss("abcd", true,
    new StreamTransformationFilter(cbcEncryption,
        new StringSink(encryptedData)
    )
);

std::string decryptedData;

CBC_Mode<AES>::Decryption cbcDecryption(key, key.size(), iv);

StringSource ss2(encryptedData, true,
    new StreamTransformationFilter(cbcDecryption,
        new StringSink(decryptedData)
    )
);

The problem is when I build in debug mode, it works fine but when I do it in release mode I get an exception from Crypto++ code ("StreamTransformationFilter: invalid PKCS #7 block padding found")问题是当我在调试模式下构建时,它工作正常,但是当我在发布模式下执行时,我从 Crypto++ 代码中得到一个异常(“StreamTransformationFilter: invalid PKCS #7 block padding found”)

例外

The problem is when I build in debug mode, it works fine but when I do it in release mode I get an exception from Crypto++ code ("StreamTransformationFilter: invalid PKCS #7 block padding found") ...问题是当我在调试模式下构建时,它工作正常,但是当我在发布模式下进行时,我从 Crypto++ 代码中得到一个异常(“StreamTransformationFilter: invalid PKCS #7 block padding found”)...

It appears to be a compiler issue related to global optimizations.这似乎是与全局优化相关的编译器问题。 Our workaround was to disable global optimizations for the source file rijndael.cpp .我们的解决方法是禁用源文件rijndael.cpp全局优化。

In rijndael.cpp you can add the following around the top of the file to avoid the issue:rijndael.cpp您可以在文件顶部添加以下内容以避免出现此问题:

#if defined(_MSC_VER) && (_MSC_VER >= 1910)
# pragma optimize("", off)
# pragma optimize("ts", on)
#endif

You can reproduce the issue with the following in rijndael.cpp :您可以在rijndael.cpp使用以下内容重现该问题:

#if defined(_MSC_VER) && (_MSC_VER >= 1910)
# pragma optimize("", off)
# pragma optimize("g", on)
#endif

Also see Commit f57df06c5e6d and pragma optimize on MSDN.另请参阅 MSDN 上的Commit f57df06c5e6dpragma optimize


If your machine has AES-NI but you want to reproduce the issue, then comment-out the code that assigns g_hasAESNI in cpu.cpp .如果你的机器有AES-NI,但要重现该问题,则注释掉的代码,受让人g_hasAESNIcpu.cpp g_hasAESNI will retain the default value of false . g_hasAESNI将保留默认值false

--- a/cpu.cpp
+++ b/cpu.cpp
@@ -242,7 +242,7 @@ void DetectX86Features()
        g_hasSSSE3 = g_hasSSE2 && ((cpuid1[2] & (1<< 9)) != 0);
        g_hasSSE41 = g_hasSSE2 && ((cpuid1[2] & (1<<19)) != 0);
        g_hasSSE42 = g_hasSSE2 && ((cpuid1[2] & (1<<20)) != 0);
-       g_hasAESNI = g_hasSSE2 && ((cpuid1[2] & (1<<25)) != 0);
+       //g_hasAESNI = g_hasSSE2 && ((cpuid1[2] & (1<<25)) != 0);
        g_hasCLMUL = g_hasSSE2 && ((cpuid1[2] & (1<< 1)) != 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM