将公钥坐标与 Crypto++ 的 ECDH 类一起使用
Using public key coordinate with Crypto++'s ECDH class
我正在使用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> ¶ms = 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 行周围。例程写入一个未压缩的点,但阻止x
和y
公共元素的最大大小,该大小应该是字段大小或子组顺序。
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)
中提供 x
和 y
的值。它们只是加密++整数。
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维基页面,这样其他人就不必去翻找它。
- 如何将enable-if与模板参数和参数包一起使用
- 如何将PERF_AMPLE_READ与mmap一起使用
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将C++中的库和头与MinGW一起使用
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 为什么我不能将 rand() 与数组的大小一起使用?
- 要与"if constexpr"一起使用的编译时消息(在预处理器之后)
- 不能将复制初始化与隐式转换的多个步骤一起使用
- 将fold表达式与std::一起用于两个元组
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 将 std::allocate_shared 与多态资源分配器一起使用
- 为什么常量词在重载运算符中不与 ostream 对象一起使用<<?
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- 将 exprtk 与自定义类的对象一起使用
- 将 std::set 与基于键的比较器一起使用
- 将 C++ 类与 Rcpp 一起使用,从 C 或 R 修改它
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 如何将AERT_Allocate与 std:vector 一起使用
- 如何使我的 sizeof sum 结构与空参数包一起工作
- 将公钥坐标与 Crypto++ 的 ECDH 类一起使用