c++ - 将公钥与 Crypto++ 的 ECDH 类配合使用

标签 c++ crypto++ elliptic-curve diffie-hellman

我正在使用 crypto++ 进行 ECDH key 协议(protocol)

ECDH.Agree(key, privateKey, outherpublicKey);

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

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

提前致谢

最佳答案

Given that for public key I have only X and Y coordinates. How to generate publicKey from this values?
ECDH.Agree(key,privateKey, getPublicKey(X,Y))

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

这就是我们真正想做的,但行不通。问题是 ECDH::Domain 只是域参数。公共(public)点和私有(private)指数位于顶部。

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 协议(protocol)生成的 key 是临时的或短暂的。它们不应该被持久化;相反,它们应该使用一次并丢弃。因此,Crypto++ 并不容易持久化它们(例如,通过提供 DEREncode)。

分析

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

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)。要了解那里发生了什么,我们需要查看 eccrypto.h在第 70 行附近,注意 reversibletrue:

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 行。例程写入一个未压缩的点,但在公共(public)元素的最大大小上阻塞 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 的值。它们只是 Crypto++ 整数。

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++ 的 Elliptic Curve Diffie-Hellman 中wiki 页面,这样其他人就不必去翻找它了。

关于c++ - 将公钥与 Crypto++ 的 ECDH 类配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33198362/

相关文章:

c++ - 概念和模板约束之间有什么区别?

c++ - 未定义对 CryptoPP::AlignedAllocate(unsigned int) 的引用

c++ - 在 Crypto++ 中使用 RSA 加密对称 AES key

c# - aes256 结果在 C# (Windows) 和 C++ (Ubuntu) 实现中不同

c++ - C++中指向数组与字符串的指针的大小

c++ - 使用模板参数指定策略

c++ - 运算符重载问题

python - Python中有限域上的椭圆曲线点加法

encryption - 我怎样才能最好地检查这些椭圆曲线参数是否有效?

java - 客户端 ECC SSL 证书包含 "unknown named curve"