我有一个程序可以初始化 ECDSA 密钥对并存储它们。该程序有效,但现在,我在使用 Crypto++ 库初始化 ECDSA 私钥时出现分段错误。
这就是我正在做的事情:
#include <iostream>
#include <cryptopp/eccrypto.h>
#include <cryptopp/osrng.h>
#include <cryptopp/oids.h>
#include <cryptopp/files.h>
using namespace std;
using namespace CryptoPP;
int main(int argc, char **argv)
{
if(argc < 2)
{
cout << "Usage: " << argv[0] << " key_name" << endl;
return 1;
}
ECDSA<ECP, SHA256>::PrivateKey privateKey;
AutoSeededRandomPool prng;
privateKey.Initialize(prng, ASN1::secp256r1());
bool result = privateKey.Validate(prng, 3);
if(!result)
return 2;
ECDSA<ECP, SHA256>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);
result = publicKey.Validate(prng, 3);
if(!result)
return 3;
FileSink fsppk(string(string(argv[1]) + ".ppk").c_str(), true /*binary*/);
privateKey.Save(fsppk);
fsppk.Flush(true);
FileSink fspub(string(string(argv[1]) + ".pub").c_str(), true /*binary*/);
publicKey.Save(fspub);
fspub.Flush(true);
return 0;
}
我使用 crypto++-8.2.0.3,并且 clang 11.0.0 和 g++ 10.2.0 出现分段错误。
QtCreator 告诉我故障出在 smartptr.h 中该函数的删除中:
template <class T> value_ptr<T>& value_ptr<T>::operator=(const value_ptr<T>& rhs)
{
T *old_p = this->m_p;
this->m_p = rhs.m_p ? new T(*rhs.m_p) : NULLPTR;
delete old_p;
return *this;
}
[编辑]堆栈跟踪:
#0 0x00007ffff7cbd813 in CryptoPP::value_ptr<CryptoPP::ECP>::operator= (this=0x7fffffffdd88, rhs=...) at smartptr.h:95
#1 0x00007ffff7cf483e in CryptoPP::EcPrecomputation<CryptoPP::ECP>::operator= (this=0x7fffffffdd78) at ecp.h:126
#2 CryptoPP::DL_GroupParametersImpl<CryptoPP::EcPrecomputation<CryptoPP::ECP>, CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::ECPPoint>, CryptoPP::DL_GroupParameters<CryptoPP::ECPPoint> >::operator= (this=0x7fffffffdd68) at pubkey.h:1013
#3 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::operator= (this=0x7fffffffdd68) at eccrypto.h:39
#4 0x00007ffff7d07272 in CryptoPP::GetValueHelperClass<CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>, CryptoPP::DL_GroupParameters<CryptoPP::ECPPoint> >::Assignable (this=0x7fffffffd260)
at algparam.h:197
#5 CryptoPP::GetValueHelperClass<CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>, CryptoPP::DL_GroupParameters<CryptoPP::ECPPoint> >::Assignable (this=0x7fffffffd260) at algparam.h:189
#6 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::GetVoidValue (pValue=0x7fffffffdd68, valueType=..., name=<optimized out>, this=<optimized out>) at eccrypto.cpp:510
#7 CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::GetVoidValue (this=<optimized out>, name=<optimized out>, valueType=..., pValue=0x7fffffffdd68) at eccrypto.cpp:497
#8 0x00007ffff7cdce12 in CryptoPP::NameValuePairs::GetValue<CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> > (value=..., name=<optimized out>, this=0x7fffffffd950) at cryptlib.h:379
#9 CryptoPP::NameValuePairs::GetThisObject<CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> > (object=..., this=0x7fffffffd950) at cryptlib.h:359
#10 CryptoPP::DL_PrivateKeyImpl<CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> >::GenerateRandom (this=this@entry=0x7fffffffdd00, rng=..., params=...) at pubkey.h:1278
#11 0x00007ffff7d216ac in CryptoPP::DL_PrivateKey_WithSignaturePairwiseConsistencyTest<CryptoPP::DL_PrivateKey_EC<CryptoPP::ECP>, CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA256> >::GenerateRandom (this=0x7fffffffdd00, rng=..., params=...) at pubkey.h:1321
#12 0x000055555555c5d6 in main (argc=2, argv=0x7fffffffe018) at ../../../pki/main.cpp:21
并且,在 QtCreator 中我有以下消息:
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
can't find linker symbol for virtual table for `CryptoPP::ECP' value
found `CryptoPP::DL_FixedBasePrecomputationImpl<CryptoPP::EC2NPoint>::~DL_FixedBasePrecomputationImpl()' instead
我找到了解决方案:我的
/usr/lib/
中有两个版本的 crypto++。
libcryptopp.so.8 指向 8.3.0 版本,通过指向 8.2.0 它再次工作。
此答案作为 edit 发布到问题 Crypto++ ECDSA