DD-L / lproxy

8 stars 3 forks source link

mingw32 - cryptopp Segmentation fault: CryptoPP::Rijndael::Base::UncheckedSetKey { __m128i temp = _mm_loadu_si128((__m128i *)(void *)(userKey+keylen-16)); } #116

Open DD-L opened 8 years ago

DD-L commented 8 years ago
Program received signal SIGSEGV, Segmentation fault.
CryptoPP::Rijndael::Base::UncheckedSetKey (this=0x4b2c888,
    userKey=0x4b2c650 "u\033悡\b\t)'爯\251\341\aJ饅V颲\373\251+\t9紃\266\025鴥+\235\r\001\255\272\r\272 ", keylen=32)
    at rijndael.cpp:231
231                     __m128i temp = _mm_loadu_si128((__m128i *)(void *)(userKey+keylen-16));
(gdb) bt
#0  CryptoPP::Rijndael::Base::UncheckedSetKey (this=0x4b2c888,
    userKey=0x4b2c650 "u\033悡\b\t)'爯\251\341\aJ饅V颲\373\251+\t9紃\266\025+\235\r\001\255\272\r\272 ", keylen=32)
    at rijndael.cpp:231
#1  0x00428300 in CryptoPP::RandomPool::GenerateIntoBufferedTransformation (this=0x4b2c618, target=..., channel="", size=20)
    at randpool.cpp:40
#2  0x00420c0b in CryptoPP::RandomNumberGenerator::GenerateBlock (this=0x4b2c618,
    output=0x4b26c81 "瓠\272\r瓠\272\r瓠\272\r瓠\272\r\272\r\332\071^kK\r2U`\030\330\a\t", size=20) at cryptlib.cpp:339
#3  0x00448669 in CryptoPP::OAEP_Base::Pad (this=<optimized out>, rng=...,
    input=0x4b25ae8 "ABA369F7D2B28A9098A0A26FEB7DC9656326909417\253\253\253\253\253\253\253\253", inputLength=42,
    oaepBlock=0x4b26c81 "\272\r\272\r\272\r\272\r\272\r\332\071^kK\r2U`\030\330\a\t",
    oaepBlockLen=<optimized out>, parameters=...) at oaep.cpp:47
#4  0x0042ff55 in CryptoPP::TF_EncryptorBase::Encrypt (this=<optimized out>, rng=...,
    plaintext=0x4b25ae8 "ABA369F7D2B28A9098A0A26FEB7DC9656326909417\253\253\253\253\253\253\253\253",
    plaintextLength=42,
    ciphertext=0x4b25f20 "\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r瓠\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\272\r\253\253\253\253", <incomplete sequence \253>, parameters=...) at pubkey.cpp:164
#5  0x004d631c in CryptoPP::PK_DefaultEncryptionFilter::Put2 (this=0x4b27fe8, inString=0x0, length=0, messageEnd=-1, blocking=true)
    at cryptlib.cpp:801
#6  0x004d27df in CryptoPP::BufferedTransformation::MessageEnd (this=0x4b27fe8, propagation=-1, blocking=true)
    at ../../../contrib/cryptopp/cryptopp/cryptlib.h:1437
#7  0x004c811c in CryptoPP::SimpleProxyFilter::LastPut (this=0x4b2ca30, inString=0x0, length=0)
    at ../../../contrib/cryptopp/cryptopp/filters.h:905
#8  0x0042615e in CryptoPP::FilterWithBufferedInput::PutMaybeModifiable (this=0x4b2ca30, inString=<optimized out>, length=0,
    messageEnd=-1, blocking=true, modifiable=false) at filters.cpp:437
#9  0x004d47cd in CryptoPP::FilterWithBufferedInput::Put2 (this=0x4b2ca30, inString=0x0, length=0, messageEnd=-1, blocking=true)
    at ../../../contrib/cryptopp/cryptopp/filters.h:339
#10 0x004204f7 in CryptoPP::BufferedTransformation::ChannelPut2 (this=<optimized out>, channel="", begin=0x0, length=0,
    messageEnd=-1, blocking=true) at cryptlib.cpp:470
---Type <return> to continue, or q <return> to quit---
#11 0x00420ea2 in ChannelMessageEnd (blocking=true, propagation=<optimized out>, channel="", this=0x4b2ca30) at cryptlib.h:1908
#12 CryptoPP::BufferedTransformation::TransferMessagesTo2 (this=0x606ef64, target=..., messageCount=@0x606edb8: 0, channel="",
    blocking=true) at cryptlib.cpp:627
#13 0x0042102a in CryptoPP::BufferedTransformation::TransferAllTo2 (this=0x606ef64, target=..., channel="", blocking=true)
    at cryptlib.cpp:668
#14 0x004bd44d in CryptoPP::SourceTemplate<CryptoPP::StringStore>::PumpAll2 (this=0x606ef4c, blocking=true)
    at ../../../contrib/cryptopp/cryptopp/filters.h:1222
#15 0x004e3283 in CryptoPP::Source::PumpAll (this=0x606ef4c) at ../../../contrib/cryptopp/cryptopp/filters.h:1166
#16 0x004e3256 in CryptoPP::Source::SourceInitialize (this=0x606ef4c, pumpAll=true, parameters=...)
    at ../../../contrib/cryptopp/cryptopp/filters.h:1199
#17 0x004b8a72 in CryptoPP::StringSource::StringSource (this=0x606ef4c,
    string=0x4b2c4b4 "ABA369F7D2B28A9098A0A26FEB7DC9656326909417", length=42, pumpAll=true, attachment=0x4b2ca30)
    at ../../../contrib/cryptopp/cryptopp/filters.h:1252
#18 0x0041ddec in crypto::Rsa::encrypt (this=0x4b2c4f8, dest=std::vector of length 0, capacity 0,
    src=0x4b2c4b4 "ABA369F7D2B28A9098A0A26FEB7DC9656326909417", src_len=42) at rsa_crypto.cpp:135
#19 0x0041e5a4 in crypto::Encryptor::encrypt (this=0x606f050, dest=std::vector of length 0, capacity 0,
    src=0x4b2c4b4 "ABA369F7D2B28A9098A0A26FEB7DC9656326909417", src_len=42) at encryptor.cpp:27
#20 0x0040dbff in lproxy::local::session::pack_exchange (this=0x4b25bfc, keysize=@0x606f1ea: 1024,
    public_key="30819D300D06092A864886F70D010101050003818B00308187028181009B7CA78876C3CAA5879C1761730E7D8139A63662DC9E7752DF369BDBB28A168EF61F884709FCE3C71D9582984CB57ED6D36060E556C52433ECDFA1C766CDCF6BABCF40B3BE15D0"...) at session_local.cpp:575
#21 0x004079ad in lproxy::local::session::right_read_handler (this=0x4b25bfc, error=..., bytes_transferred=326,
    lss_reply=std::shared_ptr (count 3, weak 0) 0x4b272c4, __data_right_rest=std::shared_ptr (count 7, weak 0) 0x4b25034,
    __write_data=std::shared_ptr (count 7, weak 0) 0x4b25034) at session_local.cpp:253
#22 0x004edaf1 in boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >::call<std::shared_ptr<lproxy::local::session>, boost::system::error_code const, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > (this=0x606fad4,
---Type <return> to continue, or q <return> to quit---
    u=std::shared_ptr (count 1, weak 1) 0x4b25bfc, b1=..., b2=@0x606f9ac: 326, b3=std::shared_ptr (count 3, weak 0) 0x4b272c4,
    b4=std::shared_ptr (count 7, weak 0) 0x4b25034, b5=std::shared_ptr (count 7, weak 0) 0x4b25034)
    at ../../../contrib/boost/boost_1_57_0/boost/bind/mem_fn_template.hpp:610
#23 0x004edb92 in boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >::operator()<std::shared_ptr<lproxy::local::session> > (this=0x606fad4, u=std::shared_ptr (count 1, weak 1) 0x4b25bfc, a1=..., a2=326,
    a3=std::shared_ptr (count 3, weak 0) 0x4b272c4, a4=std::shared_ptr (count 7, weak 0) 0x4b25034,
    a5=std::shared_ptr (count 7, weak 0) 0x4b25034) at ../../../contrib/boost/boost_1_57_0/boost/bind/mem_fn_template.hpp:625
#24 0x0048bead in boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > >::operator()<boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list2<boost::system::error_code const&, unsigned int const&> > (
    this=0x606fadc, f=..., a=...) at ../../../contrib/boost/boost_1_57_0/boost/bind/bind.hpp:596
#25 0x0048cc38 in boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > >::operator()<boost::system::error_code, unsigned int> (this=0x606fad4,
    a1=..., a2=@0x606fb04: 326) at ../../../contrib/boost/boost_1_57_0/boost/bind/bind_template.hpp:102
#26 0x004a0122 in boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<---Type <return> to continue, or q <return> to quit---
1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > >, boost::system::error_code, unsigned int>::operator() (this=0x606fad4) at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/bind_handler.hpp:127
#27 0x0048fbae in boost::asio::asio_handler_invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > >, boost::system::error_code, unsigned int> > (function=...) at ../../../contrib/boost/boost_1_57_0/boost/asio/handler_invoke_hook.hpp:69
#28 0x0047be48 in boost_asio_handler_invoke_helpers::invoke<boost::asio::detail::binder2<boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > >, boost::system::error_code, unsigned int>, boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > > > (function=..., context=...)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/handler_invoke_helpers.hpp:37
#29 0x0049b5e0 in boost::asio::detail::win_iocp_socket_recv_op<boost::array<boost::asio::mutable_buffer, 5u>, boost::_bi::bind_t<void, boost::_mfi::mf5<void, lproxy::local::session, boost::system::error_code const&, unsigned int, std::shared_ptr<lproxy::local::reply---Type <return> to continue, or q <return> to quit---
>, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > >, std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::list6<boost::_bi::value<std::shared_ptr<lproxy::local::session> >, boost::arg<1>, boost::arg<2>, boost::_bi::value<std::shared_ptr<lproxy::local::reply> >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > >, boost::_bi::value<std::shared_ptr<std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> > > > > > >::do_complete (owner=0x4b25b78, base=0x4b26e58, result_ec=..., bytes_transferred=326)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/win_iocp_socket_recv_op.hpp:97
#30 0x00498ea4 in boost::asio::detail::win_iocp_operation::complete (this=0x4b26e58, owner=..., ec=..., bytes_transferred=326)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/win_iocp_operation.hpp:46
#31 0x0049a0e1 in boost::asio::detail::win_iocp_io_service::do_one (this=0x4b25b78, block=true, ec=...)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:405
#32 0x00499afc in boost::asio::detail::win_iocp_io_service::run (this=0x4b25b78, ec=...)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/detail/impl/win_iocp_io_service.ipp:164
#33 0x0048e29e in boost::asio::io_service::run (
    this=0x559558 <lproxy::lss_server<lproxy::local::session>::io_service_right()::io_service>)
    at ../../../contrib/boost/boost_1_57_0/boost/asio/impl/io_service.ipp:59
#34 0x004b0ba2 in lproxy::lss_server<lproxy::local::session>::handle_thread_right (io_service=...)
    at ../../../src/core/lss/lss_server.h:109
#35 0x0051db02 in std::_Bind_simple<void (*(std::reference_wrapper<boost::asio::io_service>))(boost::asio::io_service&)>::_M_invoke<0u>(std::_Index_tuple<0u>) (this=0x4b25a80) at D:/Qt/QtDev/Tools/mingw492_32/i686-w64-mingw32/include/c++/functional:1700
#36 0x0051db82 in std::_Bind_simple<void (*(std::reference_wrapper<boost::asio::io_service>))(boost::asio::io_service&)>::operator()() (this=0x4b25a80) at D:/Qt/QtDev/Tools/mingw492_32/i686-w64-mingw32/include/c++/functional:1688
#37 0x0052b5ee in std::thread::_Impl<std::_Bind_simple<void (*(std::reference_wrapper<boost::asio::io_service>))(boost::asio::io_service&)> >::_M_run() (this=0x4b25a74) at D:/Qt/QtDev/Tools/mingw492_32/i686-w64-mingw32/include/c++/thread:115
#38 0x6fcb8c4f in libstdc++-6!execute_native_thread_routine () from D:\Qt\QtDev\5.5\mingw492_32\bin\libstdc++-6.dll
#39 0x64944f11 in pthread_create_wrapper () from D:\Qt\QtDev\Tools\mingw492_32\opt\bin\libwinpthread-1.dll
#40 0x749771e6 in msvcrt!_beginthreadex () from C:\WINDOWS\SYSTEM32\msvcrt.dll
#41 0x749772b1 in msvcrt!_endthreadex () from C:\WINDOWS\SYSTEM32\msvcrt.dll
#42 0x747f38f4 in KERNEL32!BaseThreadInitThunk () from C:\WINDOWS\SYSTEM32\kernel32.dll
#43 0x775f5de3 in ntdll!RtlUnicodeStringToInteger () from C:\WINDOWS\SYSTEM32\ntdll.dll
#44 0x04b26560 in ?? ()
#45 0x775f5dae in ntdll!RtlUnicodeStringToInteger () from C:\WINDOWS\SYSTEM32\ntdll.dll
#46 0xffffffff in ?? ()
#47 0x7761b7ea in ntdll!RtlCaptureContext () from C:\WINDOWS\SYSTEM32\ntdll.dll
#48 0x00000000 in ?? ()
// 组装 exchange
const request session::pack_exchange(const keysize_t& keysize, 
        const data_t& public_key) {

{
    std::string k = "30819D300D06092A864886F70D010101050003818B00308187028181009EFD7A5CFBF51D88C6F98588BBBCA26A6FB4E35657094E024B6CE79582EFE6DCCCE8954D416BBD57D4DC0B81320FD97DBCBB7CAE8BFB4B3827AC99210E15EC9062C32E7337905B92866A93E689A8E9940148DB08C5964CEF0272C16CFBB8A2E403A5DB4DD142280E8C3FCD1165FCB043C256AE0A951F4E3C8153E6891DD31EED020111";

    using namespace crypto;
    std::string p = "ABA369F7D2B28A9098A0A26FEB7DC9656326909417";
    Encryptor e_test(new Rsa(RsaKey::bit1024, (const uint8_t*)&k[0], k.size()));
    std::vector<uint8_t> cip;
    e_test.encrypt(cip, (const uint8_t*)&p[0], p.size());
    std::string msg = _debug_format_data(cip, int(), ' ', std::hex);
    std::cout << msg << std::endl;
}
...

但是在 test 用例中同样的的代码就没问题:

void test() {

    std::cout << "\n\n``````````````````````````````````````\n\n\n";
    std::string k = "30819D300D06092A864886F70D010101050003818B00308187028181009EFD7A5CFBF51D88C6F98588BBBCA26A6FB4E35657094E024B6CE79582EFE6DCCCE8954D416BBD57D4DC0B81320FD97DBCBB7CAE8BFB4B3827AC99210E15EC9062C32E7337905B92866A93E689A8E9940148DB08C5964CEF0272C16CFBB8A2E403A5DB4DD142280E8C3FCD1165FCB043C256AE0A951F4E3C8153E6891DD31EED020111";
    std::string p = "ABA369F7D2B28A9098A0A26FEB7DC9656326909417";
    Encryptor e_test(new Rsa(RsaKey::bit1024, (const uint8_t*)&k[0], k.size()));
    std::vector<uint8_t> cip;
    e_test.encrypt(cip, (const uint8_t*)&p[0], p.size());
    print_hex(cip, cip.size());
    std::cout << "\n\n``````````````````````````````````````\n\n\n";
}
DD-L commented 8 years ago

进一步测试发现:

session_local.cpp:start 函数

this->resolver_right.async_resolve({server_name, server_port}, 
            boost::bind(&session::resolve_handler, shared_from_this(), _1, _2));

语句上下执行 rsa 加密都没问题,可是在绑定的回调函数 session::resolve_handler 中执行 rsa 加密, 就会出问题。

resolver_right.async_resolve() 这是 local 端第一个 boost.asio 异步回调绑定。

这种情况下,基本已经确定了是 cryptopp 库的问题,而不是我的代码封装的问题。

DD-L commented 8 years ago

cryptopp 出错代码处:

https://github.com/weidai11/cryptopp/blob/master/rijndael.cpp#L231

void Rijndael::Base::UncheckedSetKey(const byte *userKey, unsigned int keylen, const NameValuePairs &)
{
    AssertValidKeyLength(keylen);

    m_rounds = keylen/4 + 6;
    m_key.New(4*(m_rounds+1));

    word32 *rk = m_key;

#if (CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE && (!defined(_MSC_VER) || _MSC_VER >= 1600 || CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32))
    // MSVC 2008 SP1 generates bad code for _mm_extract_epi32() when compiling for X64
    if (HasAESNI())
    {
        static const word32 rcLE[] = {
            0x01, 0x02, 0x04, 0x08,
            0x10, 0x20, 0x40, 0x80,
            0x1B, 0x36, /* for 128-bit blocks, Rijndael never uses more than 10 rcon values */
        };
        const word32 *rc = rcLE;

        __m128i temp = _mm_loadu_si128((__m128i *)(void *)(userKey+keylen-16)); // <--- 程序死在这里
        memcpy(rk, userKey, keylen);

        while (true)
        {
            rk[keylen/4] = rk[0] ^ _mm_extract_epi32(_mm_aeskeygenassist_si128(temp, 0), 3) ^ *(rc++);
            rk[keylen/4+1] = rk[1] ^ rk[keylen/4];
            rk[keylen/4+2] = rk[2] ^ rk[keylen/4+1];
            rk[keylen/4+3] = rk[3] ^ rk[keylen/4+2];

            if (rk + keylen/4 + 4 == m_key.end())
                break;

            if (keylen == 24)
            {
                rk[10] = rk[ 4] ^ rk[ 9];
                rk[11] = rk[ 5] ^ rk[10];
                temp = _mm_insert_epi32(temp, rk[11], 3);
            }
            else if (keylen == 32)
            {
                temp = _mm_insert_epi32(temp, rk[11], 3);
                rk[12] = rk[ 4] ^ _mm_extract_epi32(_mm_aeskeygenassist_si128(temp, 0), 2);
                rk[13] = rk[ 5] ^ rk[12];
                rk[14] = rk[ 6] ^ rk[13];
                rk[15] = rk[ 7] ^ rk[14];
                temp = _mm_insert_epi32(temp, rk[15], 3);
            }
            else
                temp = _mm_insert_epi32(temp, rk[7], 3);

            rk += keylen/4;
        }

        if (!IsForwardTransformation())
        {
            rk = m_key;
            unsigned int i, j;

            std::swap(*(__m128i *)(void *)(rk), *(__m128i *)(void *)(rk+4*m_rounds));

            for (i = 4, j = 4*m_rounds-4; i < j; i += 4, j -= 4)
            {
                temp = _mm_aesimc_si128(*(__m128i *)(void *)(rk+i));
                *(__m128i *)(void *)(rk+i) = _mm_aesimc_si128(*(__m128i *)(void *)(rk+j));
                *(__m128i *)(void *)(rk+j) = temp;
            }

            *(__m128i *)(void *)(rk+i) = _mm_aesimc_si128(*(__m128i *)(void *)(rk+i));
        }

        return;
    }
#endif
...

看样子是处理器相关的东西

DD-L commented 8 years ago

发现了一个有用的链接:

http://stackoverflow.com/questions/22100851/failing-on-call-to-mm-loadu-si128-with-aesni-intrinsics-enabled

摘录下来就是:

We get the error when compiling on the E5-2680, and copying to the X5690.

Oh, that's interesting. Try adding -mtune=pentium4 to CXXFLAGS. The Intel Xeon E5-2680 has the AVX instruction set; while the Intel Xeon X5690 only has SSE 4.2. Crypto++ uses the the double quadword multiply (PCLMULQDQ) and AES-NI instructions from the AVX instruction set (which the other processor lacks).

If -mtune=pentium4 does not work, then you are going to have to disable via CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE. Now that I think about it, that's what you are probably going to have to do since this is a compile time feature selection, and not a runtime feature selection.


re: had to just stick with CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE......

于是在编译 cryptopp 静态库时 mingw32-make MAKE=mingw32-make CXXFLAGS="-DNDEBUG -g2 -O2 -mtune=pentium4"

发现有用