使用Crypto++库仅从x压缩坐标检索ECDSA公钥

Retrieve ECDSA public key from only x-compressed coordinate using Crypto++ library

本文关键字:坐标 检索 ECDSA 公钥 压缩 Crypto++ 使用      更新时间:2023-10-16

我正在车辆通信范围上实现一个安全守护进程。

我可以收到一条带有签名和点的压缩X坐标的消息来验证该签名。椭圆曲线可以是secp256或brainpoolp256r1,算法是ECDSA。

我的问题是:如果只使用Crypto++库的压缩X坐标,我如何恢复ECC点(以及公钥)?

我关注了一些解释它的链接(以及许多其他链接)https://www.cryptopp.com/wiki/Point_Compression加密++和压缩EC密钥,但它们不适合我的问题。

我试图生成代码来解决这个问题,但它不起作用:

#include <string>
#include <iostream>
#include <cryptopp/cryptlib.h>
#include <cryptopp/ecp.h>
#include <cryptopp/eccrypto.h>
#include <cryptopp/hex.h>
#include <cryptopp/oids.h>
#include <cryptopp/osrng.h>
using namespace CryptoPP;
using std::cout;
using std::endl;
using std::string;
int main() 
{
string compactPoint = "937120662418500f3ad7c892b1db7e7c2d85ec48c74e99d64dcb7083082bb4f3";
AutoSeededRandomPool generator;
ECDSA<ECP, SHA256>::PublicKey pubKey;
OID curve       = ASN1::secp256r1();

StringSource ss (compactPoint, true, new CryptoPP::HexDecoder);
ECP::Point point;
pubKey.GetGroupParameters().GetCurve().DecodePoint (point, ss, ss.MaxRetrievable());
std::cout << "Result after decompression X: " << std::hex << point.x << std::endl;
std::cout << "Result after decompression Y: " << std::hex << point.y << std::endl;
return 0;
}

你能帮我吗?

最简单的解决方案可能是将"02""03"前置到紧凑表示。Crypto++将把它解码为压缩的公钥。

$ cat test.cxx
#include "cryptlib.h"
#include "eccrypto.h"
#include "ecp.h"
#include "hex.h"
#include "oids.h"
#include <string>
#include <iostream>
#include <iomanip>
int main(int argc, char* argv[])
{
using namespace CryptoPP;
ECDSA<ECP, SHA256>::PublicKey pubKey;
pubKey.AccessGroupParameters().Initialize(ASN1::secp256r1());
std::string compactPoint = "02" /* compressed */
"937120662418500f3ad7c892b1db7e7c"
"2d85ec48c74e99d64dcb7083082bb4f3";
StringSource ss (compactPoint, true, new HexDecoder);
ECP::Point point;
pubKey.GetGroupParameters().GetCurve().DecodePoint (point, ss, ss.MaxRetrievable());
std::cout << "Result after decompression X: " << std::hex << point.x << std::endl;
std::cout << "Result after decompression Y: " << std::hex << point.y << std::endl;
return 0;
}

然后构建并运行程序。请注意,库为坐标的y部分求解。

cryptopp$ g++ test.cxx ./libcryptopp.a -o test.exe
cryptopp$ ./test.exe
Result after decompression X: 937120662418500f3ad7c892b1db7e7c2d85ec48c74e99d64dcb7083082bb4f3h
Result after decompression Y: cfcaf74eae3ceec5993928f04970cfef343b9a6b22727fa81926bd21f256ec56h

为了省去查找的麻烦,您可以使用设置publicKey的公共元素

pubKey.SetPublicElement(point);
std::cout << "X: " << std::hex << pubKey.GetPublicElement().x << std::endl;
std::cout << "Y: " << std::hex << pubKey.GetPublicElement().y << std::endl;

使用附加代码运行会产生预期结果:

$ ./test.exe
Result after decompression X: 937120662418500f3ad7c892b1db7e7c2d85ec48c74e99d64dcb7083082bb4f3h
Result after decompression Y: cfcaf74eae3ceec5993928f04970cfef343b9a6b22727fa81926bd21f256ec56h
X: 937120662418500f3ad7c892b1db7e7c2d85ec48c74e99d64dcb7083082bb4f3h
Y: cfcaf74eae3ceec5993928f04970cfef343b9a6b22727fa81926bd21f256ec56h

如果感兴趣,以下是您用于解码ecp.cpp:点的代码

bool ECP::DecodePoint(ECP::Point &P, BufferedTransformation &bt, size_t encodedPointLen) const
{
byte type;
if (encodedPointLen < 1 || !bt.Get(type))
return false;
switch (type)
{
case 0:
P.identity = true;
return true;
case 2:
case 3:
{
if (encodedPointLen != EncodedPointSize(true))
return false;
Integer p = FieldSize();
P.identity = false;
P.x.Decode(bt, GetField().MaxElementByteLength());
P.y = ((P.x*P.x+m_a)*P.x+m_b) % p;
if (Jacobi(P.y, p) !=1)
return false;
P.y = ModularSquareRoot(P.y, p);
if ((type & 1) != P.y.GetBit(0))
P.y = p-P.y;
return true;
}
case 4:
{
if (encodedPointLen != EncodedPointSize(false))
return false;
unsigned int len = GetField().MaxElementByteLength();
P.identity = false;
P.x.Decode(bt, len);
P.y.Decode(bt, len);
return true;
}
default:
return false;
}
}

如果您想自己求解y坐标,填充point,然后直接调用SetPublicElement,我会提到它。


您也可以预先准备03而不是02。不同的是,解码返回yp-y。可变性是由于上面显示的模平方根而引入的。我们需要看到生成算法来确定应该是什么值

以下是使用03而不是02时的差异:

$ ./test.exe
X: 937120662418500f3ad7c892b1db7e7c2d85ec48c74e99d64dcb7083082bb4f3h
Y: 303508b051c3113b66c6d70fb68f3010cbc46595dd8d8057e6d942de0da913a9h

注意,03生成y坐标303508b051c3113b66c6d70fb68f3010cbc46595dd8d8057e6d942de0da913a9h,而不是02cfcaf74eae3ceec5993928f04970cfef343b9a6b22727fa81926bd21f256ec56h