I'm trying to load an EC key given as a byte array using Crypto++. Here is the key:
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIPQLO9zyl40X3lh1wbSR6S88aCsUvJr9R5n2pA3DbD9+oAoGCCqGSM49
AwEHoUQDQgAEs+nDydkW5F07yZPb/c05TSjzRJXCvD8Ni76ppfWJFOEOdM/WuHU6
zBMcdIzoY+LuqdZ8LgVlMBsnx8NwNvvFAA==
-----END EC PRIVATE KEY-----
And here is the same key as a byte array (assuming I didn't mess up the conversion):
uint8_t server_priv_key_[] = {
0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0xf4, 0x0b, 0x3b, 0xdc, 0xf2,
0x97, 0x8d, 0x17, 0xde, 0x58, 0x75, 0xc1, 0xb4, 0x91, 0xe9, 0x2f, 0x3c,
0x68, 0x2b, 0x14, 0xbc, 0x9a, 0xfd, 0x47, 0x99, 0xf6, 0xa4, 0x0d, 0xc3,
0x6c, 0x3f, 0x7e, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xb3, 0xe9, 0xc3,
0xc9, 0xd9, 0x16, 0xe4, 0x5d, 0x3b, 0xc9, 0x93, 0xdb, 0xfd, 0xcd, 0x39,
0x4d, 0x28, 0xf3, 0x44, 0x95, 0xc2, 0xbc, 0x3f, 0x0d, 0x8b, 0xbe, 0xa9,
0xa5, 0xf5, 0x89, 0x14, 0xe1, 0x0e, 0x74, 0xcf, 0xd6, 0xb8, 0x75, 0x3a,
0xcc, 0x13, 0x1c, 0x74, 0x8c, 0xe8, 0x63, 0xe2, 0xee, 0xa9, 0xd6, 0x7c,
0x2e, 0x05, 0x65, 0x30, 0x1b, 0x27, 0xc7, 0xc3, 0x70, 0x36, 0xfb, 0xc5,
0x00,
};
Finally, I'm loading the key like this:
ArraySource server_priv_key_source { server_priv_key_, sizeof(server_priv_key_), true };
ECDSA<ECP, SHA256>::PrivateKey server_priv_key;
server_priv_key.Load(server_priv_key_source);
However, the call to Load
causes a "BER decode error" exception. What am I doing wrong?
Your private key has the SEC1 format, but only the PKCS#8 format is supported (see here and here), so the key has to be converted, e.g. with OpenSSL:
openssl pkcs8 -topk8 -nocrypt -in <path to input-sec1-pem> -out <path to output-pkcs8-pem>
This results in (PEM encoded):
-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg9As73PKXjRfeWHXB
tJHpLzxoKxS8mv1HmfakDcNsP36hRANCAASz6cPJ2RbkXTvJk9v9zTlNKPNElcK8
Pw2Lvqml9YkU4Q50z9a4dTrMExx0jOhj4u6p1nwuBWUwGyfHw3A2+8UA
-----END PRIVATE KEY-----
or as byte array (DER encoded):
uint8_t server_priv_key_[] = {
0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
0x03, 0x01, 0x07, 0x04, 0x6d, 0x30, 0x6b, 0x02, 0x01, 0x01, 0x04, 0x20,
0xf4, 0x0b, 0x3b, 0xdc, 0xf2, 0x97, 0x8d, 0x17, 0xde, 0x58, 0x75, 0xc1,
0xb4, 0x91, 0xe9, 0x2f, 0x3c, 0x68, 0x2b, 0x14, 0xbc, 0x9a, 0xfd, 0x47,
0x99, 0xf6, 0xa4, 0x0d, 0xc3, 0x6c, 0x3f, 0x7e, 0xa1, 0x44, 0x03, 0x42,
0x00, 0x04, 0xb3, 0xe9, 0xc3, 0xc9, 0xd9, 0x16, 0xe4, 0x5d, 0x3b, 0xc9,
0x93, 0xdb, 0xfd, 0xcd, 0x39, 0x4d, 0x28, 0xf3, 0x44, 0x95, 0xc2, 0xbc,
0x3f, 0x0d, 0x8b, 0xbe, 0xa9, 0xa5, 0xf5, 0x89, 0x14, 0xe1, 0x0e, 0x74,
0xcf, 0xd6, 0xb8, 0x75, 0x3a, 0xcc, 0x13, 0x1c, 0x74, 0x8c, 0xe8, 0x63,
0xe2, 0xee, 0xa9, 0xd6, 0x7c, 0x2e, 0x05, 0x65, 0x30, 0x1b, 0x27, 0xc7,
0xc3, 0x70, 0x36, 0xfb, 0xc5, 0x00
};
In this format the key can be imported with the posted code.
Test:
In the following code, the private key is imported, a message is signed, the public key is derived from the imported private key, and the message is then successfully verified:
#include <osrng.h>
#include <eccrypto.h>
using namespace CryptoPP;
using namespace std;
...
uint8_t server_priv_key_[] = ... // the DER encoded PKCS#8 key above
// Import private key
ArraySource server_priv_key_source{ server_priv_key_, sizeof(server_priv_key_), true };
ECDSA<ECP, SHA256>::PrivateKey server_priv_key;
server_priv_key.Load(server_priv_key_source);
// Derive public key
ECDSA<ECP, SHA256>::PublicKey publicKey;
server_priv_key.MakePublicKey(publicKey);
// Sign
AutoSeededRandomPool prng;
ECDSA<ECP, SHA256>::Signer signer(server_priv_key);
size_t signatureLen = signer.MaxSignatureLength();
string signature(signatureLen, 0x00);
string msg = "The quick brown fox jumps over the lazy dog";
signatureLen = signer.SignMessage(prng, (const byte*)&msg[0], msg.size(), (byte*)&signature[0]);
signature.resize(signatureLen);
// Verify
ECDSA<ECP, SHA256>::Verifier verifier(publicKey);
bool result = verifier.VerifyMessage((const byte*)&msg[0], msg.size(), (const byte*)&signature[0], signature.size());
printf("%s", result ? "verified" : "failed"); // verified