将公钥坐标与 Crypto++ 的 ECDH 类一起使用

Using public key coordinate with Crypto++'s ECDH class

本文关键字:一起 ECDH 公钥 坐标 Crypto++      更新时间:2023-10-16

我正在使用crypto++进行ECDH密钥协议

ECDH.Agree(key, privateKey, outherpublicKey);

鉴于对于公钥,我只有 X 和 Y 坐标。如何从这个值生成公钥?

ECDH.Agree(key,privateKey, getPublicKey(X,Y))

提前致谢

鉴于对于公钥,我只有 X 和 Y 坐标。如何从这个值生成公钥?
ECDH.Agree(key,privateKey, getPublicKey(X,Y))

{x,y}是曲线上的一个点,但直接使用它从来都不容易。

这是我们真正想做的,但它不起作用。问题是ECDH::Domain只是域参数。公共点和私有指数分层在顶部。

OID curve = ASN1::secp256r1();
DL_GroupParameters_EC<ECP> params(curve);
Integer x("...");
Integer y("...");
ECP::Point q(x, y);
DL_PublicKey_EC<ECP> pubKey;
pubKey.Initialize(params, q);
ECDH < ECP >::Domain theirs(pubKey);

更复杂的是,ECDH协议产生的密钥是临时的或短暂的。它们不应该持久化;相反,它们应该使用一次并丢弃。因此,Crypto++并不容易持久化它们(例如,通过提供DEREncode(。

分析

要使用{x,y}坐标,我们需要弄清楚库如何使用它。临时公钥和私钥是在第 1380 行附近pubkey.h创建的。它们的代码如下:

void GeneratePrivateKey(RandomNumberGenerator &rng, byte *privateKey)
{
    Integer x(rng, Integer::One(), GetAbstractGroupParameters().GetMaxExponent());
    x.Encode(privateKey, PrivateKeyLength());
}
void GeneratePublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey)
{
    const DL_GroupParameters<T> &params = GetAbstractGroupParameters();
    Integer x(privateKey, PrivateKeyLength());
    Element y = params.ExponentiateBase(x);
    params.EncodeElement(true, y, publicKey);
}

上面感兴趣的线是 params.EncodeElement(true, y, publicKey) .要了解那里发生了什么,我们需要查看第 70 行周围的eccrypto.h,并注意reversible true

void EncodeElement(bool reversible, const Element &element, byte *encoded)
{
    if (reversible)
        GetCurve().EncodePoint(encoded, element, m_compress);
    else
        element.x.Encode(encoded, GetEncodedElementSize(false));
}

params.EncodeElement打电话给ECP::EncodePoint.要了解它的作用,我们可以检查ecp.cpp第 120 行周围。例程写入一个未压缩的点,但阻止xy公共元素的最大大小,该大小应该是字段大小或子组顺序。

void ECP::EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed)
{
    if (P.identity)
        NullStore().TransferTo(bt, EncodedPointSize(compressed));
    else if (compressed)
    {
        bt.Put(2 + P.y.GetBit(0));
        P.x.Encode(bt, GetField().MaxElementByteLength());
    }
    else
    {
        unsigned int len = GetField().MaxElementByteLength();
        bt.Put(4);      // uncompressed
        P.x.Encode(bt, len);
        P.y.Encode(bt, len);
    }
}

不要太担心BufferedTransformation.有一些方法可以将byte[]变成一个,它发生在上面显示的代码之前。如果您跟踪代码,您将看到它通过ArraySource转换:

byte myArray[PublicEphemeralKeyLength()];
ArraySource as(myArray, COUNTOF(myArray));

上面,as是一个BufferedTransformation,它包装了您传递到函数中的byte[]

最后一个悬而未决的问题是字段元素的最大大小。这似乎是模数大小减去一个,以字节为单位:

$ grep -I -A 1 MaxElementByteLength modarith.h 
    unsigned int MaxElementByteLength() const
        {return (m_modulus-1).ByteCount();}

协议

鉴于上述信息,您应该执行以下操作。您需要在 ECP::Point q(x,y) 中提供 xy 的值。它们只是加密++整数。

OID curve = ASN1::secp256r1();
DL_GroupParameters_EC<ECP> params(curve);
size_t size = params.GetEncodedElementSize(true);
vector<byte> othersPublicKey(size);
ECP::Point q(x,y);
params.EncodeElement(true, q, &othersPublicKey[0]);

然后你可以打电话:

ecdh.Agree(key, myPrivateKey, &othersPublicKey[0]);

一个注意事项:params.GetEncodedElementSize(true)应该等于PublicEphemeralKeyLength()。如果它们不相等,那么就有问题了。


如果需要修改压缩,则可以:

params.SetPointCompression(true);

我会把它添加到Crypto++的椭圆曲线Diffie-Hellman维基页面,这样其他人就不必去翻找它。