GF New KeyAgreement

03/12/2024 13:29 Cy0rk#1
Hello guys,
I found out they changed from RFC 5114 to RFC 8998 for the KeyAgreement.

Code:
uint32_t KeyAgreement::prepare(void* buffer, uint32_t* length)
{
	CryptoPP::AutoSeededX917RNG<CryptoPP::Rijndael> rng;

	auto values = CryptoPP::ASN1::secp256r1().GetValues();

	m_domain.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp256r1());
	if (!m_domain.GetGroupParameters().ValidateGroup(rng, 3))
	{
		return 0;
	}

	m_privateKey.New(m_domain.PrivateKeyLength());
	m_publicKey.New(m_domain.PublicKeyLength());

	m_domain.GenerateKeyPair(rng, m_privateKey, m_publicKey);
	size_t dataLength = m_publicKey.size();
	if (*length < dataLength)
	{
		return 0;
	}

	*length = dataLength;
	memcpy(buffer, m_publicKey.data(), dataLength);

	return m_domain.AgreedValueLength();
}

bool KeyAgreement::agree(uint32_t agreeLength, const void* buffer, uint32_t length)
{
	if (agreeLength != m_domain.AgreedValueLength() || length != m_domain.PublicKeyLength())
	{
		return false;
	}

	m_sharedKey.New(agreeLength);
	CryptoPP::SecByteBlock pubKey((const uint8_t*)buffer, length);
	if (!m_domain.Agree(m_sharedKey, m_privateKey, pubKey))
	{
		return false;
	}

	CryptoPP::Integer sharedKey(m_sharedKey.data(), m_sharedKey.size());
	if (sharedKey == 0)
	{
		return false;
	}

	return true;
}