PQ: fix logic

This commit is contained in:
user
2026-06-08 04:16:33 +00:00
parent 1cc4854bb6
commit cd176796b3
3 changed files with 63 additions and 13 deletions
+59 -11
View File
@@ -19,11 +19,12 @@
#if LIBRESSL_PQ
#warning like you use libressl
#include<openssl/mlkem.h>
#warning like you use libressl
#else
#warning like you use openssl
#endif
#define DEF_RANK MLKEM768_RANK
namespace i2p
{
namespace crypto
@@ -33,13 +34,15 @@ namespace crypto
m_CTLen (std::get<2>(MLKEMS[type])), m_Pkey (nullptr)
{
}
MLKEMKEYS::FreeKeys(void)
void MLKEMKeys::FreeKeys(void)
{
#ifndef is_libre
#ifndef LIBRESSL_PQ
if (m_Pkey) EVP_PKEY_free (m_Pkey);
#else
if (m_Pkey) MLKEM_private_key_free (m_Pkey);
if (m_PublicKey) { MLKEM_public_key_free(m_PublicKey); m_PublicKey = nullptr; }
if (m_Pkey) MLKEM_private_key_free (m_Pkey);
#endif
if (m_Pkey) m_Pkey = nullptr;
}
MLKEMKeys::~MLKEMKeys ()
{
@@ -49,10 +52,10 @@ namespace crypto
void MLKEMKeys::GenerateKeys ()
{
FreeKeys();
#ifndef is_libre
#ifndef LIBRESSL_PQ
m_Pkey = EVP_PKEY_Q_keygen(NULL, NULL, m_Name.c_str ());
#else
m_Pkey = MLKEM_private_key_new(MLKEM768_RANK);
m_Pkey = MLKEM_private_key_new(DEF_RANK);
#endif
}
@@ -61,17 +64,47 @@ namespace crypto
if (m_Pkey)
{
size_t len = m_KeyLen;
#ifndef LIBRESSL_PQ
EVP_PKEY_get_octet_string_param (m_Pkey, OSSL_PKEY_PARAM_PUB_KEY, pub, m_KeyLen, &len);
#else
/*
* MLKEM_generate_key(MLKEM_private_key *private_key,
uint8_t **out_encoded_public_key, size_t *out_encoded_public_key_len,
uint8_t **out_optional_seed, size_t *out_optional_seed_len);
https://github.com/libressl/openbsd/blob/8662e35dbd36d8450a6d4c7188a65c580e4b339f/src/lib/libcrypto/mlkem/mlkem.h#L125C5-L128C1
maybe int MLKEM_public_from_private(const MLKEM_private_key *private_key,
MLKEM_public_key *public_key);?
*/
{
// auto result = MLKEM_generate_key(m_Pkey, &pub, &len, nullptr, nullptr);
// if(result != 0) {
// LogPrint (eLogError, "MLKEM [libressl]: can't generate public key");
if (!m_Pkey) return;
auto pub_key = MLKEM_public_key_new(DEF_RANK);
if (MLKEM_public_from_private(m_Pkey, pub_key) == 0)
{
//int MLKEM_marshal_public_key(const MLKEM_public_key *public_key, uint8_t **out, size_t *out_len);
MLKEM_marshal_public_key(pub_key, &pub, &len);
}
else
{
LogPrint(eLogError, "MLKEM [libressl]: can't extract public key from private");
}
MLKEM_public_key_free(pub_key);
}
#endif
}
}
void MLKEMKeys::SetPublicKey (const uint8_t * pub)
{
if (m_Pkey)
{
EVP_PKEY_free (m_Pkey);
m_Pkey = nullptr;
}
#ifndef LIBRESSL_PQ
if(!m_Pkey) return LogPrint(eLogError, "We are don't have private key for set public key");
FreeKeys();
OSSL_PARAM params[] =
{
OSSL_PARAM_octet_string (OSSL_PKEY_PARAM_PUB_KEY, (uint8_t *)pub, m_KeyLen),
@@ -86,11 +119,23 @@ namespace crypto
}
else
LogPrint (eLogError, "MLKEM can't create PKEY context");
#else
if (m_PublicKey) MLKEM_public_key_free(m_PublicKey);
m_PublicKey = MLKEM_public_key_new(DEF_RANK);
if (!MLKEM_parse_public_key(m_PublicKey, pub, m_KeyLen))
{
LogPrint(eLogError, "MLKEM: failed to parse public key");
MLKEM_public_key_free(m_PublicKey);
m_PublicKey = nullptr;
}
//LogPrint(eLogError, "MLKEM SetPublicKey [libressl] NOT IMPLEMENTED YET");
#endif
}
void MLKEMKeys::Encaps (uint8_t * ciphertext, uint8_t * shared)
{
if (!m_Pkey) return;
#ifndef LIBRESSL_PQ
auto ctx = EVP_PKEY_CTX_new_from_pkey (NULL, m_Pkey, NULL);
if (ctx)
{
@@ -101,6 +146,9 @@ namespace crypto
}
else
LogPrint (eLogError, "MLKEM can't create PKEY context");
#else
// auto res = MLKEM_encap(
#endif
}
void MLKEMKeys::Decaps (const uint8_t * ciphertext, uint8_t * shared)