使用 Crypto++ 加载 ECDSA 私钥

Load ECDSA private key with Crypto++

我正在尝试使用 Crypto++ 加载以字节数组形式给出的 EC 密钥。这是关键:

-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIPQLO9zyl40X3lh1wbSR6S88aCsUvJr9R5n2pA3DbD9+oAoGCCqGSM49
AwEHoUQDQgAEs+nDydkW5F07yZPb/c05TSjzRJXCvD8Ni76ppfWJFOEOdM/WuHU6
zBMcdIzoY+LuqdZ8LgVlMBsnx8NwNvvFAA==
-----END EC PRIVATE KEY-----

这是与字节数组相同的键(假设我没有搞乱转换):

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,
};

最后,我像这样加载密钥:

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);

但是,调用 Load 会导致“BER 解码错误”异常。我做错了什么?

您的私钥为SEC1格式,但仅支持PKCS#8格式(参见here and here),因此必须转换密钥,例如使用 OpenSSL:

openssl pkcs8 -topk8 -nocrypt -in <path to input-sec1-pem> -out <path to output-pkcs8-pem>

这导致(PEM 编码):

-----BEGIN PRIVATE KEY-----
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg9As73PKXjRfeWHXB
tJHpLzxoKxS8mv1HmfakDcNsP36hRANCAASz6cPJ2RbkXTvJk9v9zTlNKPNElcK8
Pw2Lvqml9YkU4Q50z9a4dTrMExx0jOhj4u6p1nwuBWUwGyfHw3A2+8UA
-----END PRIVATE KEY-----

或作为字节数组(DER 编码):

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
};

在此格式中,可以使用发布的代码导入密钥。


测试:
在下面的代码中,导入私钥,对消息进行签名,public密钥从导入的私钥中导出,然后成功验证消息:

#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