I do not find any function to load the public key. I've tried to use BERDecode on DH Domain
http://www.cryptopp.com/docs/ref/class_d_h___domain.html but without success. I've tried too declaring a CryptoPP::ECIES< CryptoPP::ECP >::PublicKey and using .Load, but nothing. I've managed to pass from SecByteBlock to Byte but now i would need to load a public key in .der format on this.
Here's is the code I have at the moment.
int main( int, char** ) {
OID CURVE = secp256k1();
AutoSeededX917RNG<AES> rng;
/* A */
ECDH < ECP >::Domain dhA( CURVE );
byte privA[dhA.PrivateKeyLength()];
byte pubA[dhA.PublicKeyLength()];
dhA.GenerateKeyPair(rng, privA, pubA);
byte sharedA[dhA.AgreedValueLength()];
/* B */
ECDH < ECP >::Domain dhB( CURVE );
byte privB[dhB.PrivateKeyLength()];
byte pubB[dhB.PublicKeyLength()];
dhB.GenerateKeyPair(rng, privB, pubB);
byte sharedB[dhB.AgreedValueLength()];
/* AGREEMENT */
dhA.Agree (sharedA, privA, pubB);
dhB.Agree (sharedB, privB, pubA);
if ((dhA.AgreedValueLength() != dhB.AgreedValueLength()) || (memcmp (sharedB, sharedA, dhA.AgreedValueLength()) != 0)) {
cout << "Something wrong" << endl;
}
else {
cout << "Agreed to shared secret" << endl;
}
return 0;
}
If I use:
CryptoPP::ECIES< CryptoPP::ECP >::PublicKey pubA;
CryptoPP::ECIES< CryptoPP::ECP >::PrivateKey privA;
string namepub = "ec-pubkey.der";
string namepriv = "ec-privkey.der";
pubA.Load(FileSource( namepub.c_str(), true, NULL, true /*binary*/ ).Ref());
privA.Load(FileSource( namepriv.c_str(), true, NULL, true /*binary*/ ).Ref());
Instead of:
byte privA[dhA.PrivateKeyLength()];
byte pubA[dhA.PublicKeyLength()];
dhA.GenerateKeyPair(rng, privA, pubA);
byte sharedA[dhA.AgreedValueLength()];
The code compiles but when I execute it:
terminate called after throwing an instance of 'CryptoPP::BERDecodeErr'
what(): BER decode error