diff --git a/libi2pd/PostQuantum.cpp b/libi2pd/PostQuantum.cpp index 05aa1075..266f8a70 100644 --- a/libi2pd/PostQuantum.cpp +++ b/libi2pd/PostQuantum.cpp @@ -19,11 +19,12 @@ #if LIBRESSL_PQ #warning like you use libressl #include - #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) diff --git a/libi2pd/PostQuantum.h b/libi2pd/PostQuantum.h index 9aa58f59..92dfce02 100644 --- a/libi2pd/PostQuantum.h +++ b/libi2pd/PostQuantum.h @@ -17,7 +17,9 @@ #include "Identity.h" #if OPENSSL_PQ - +#if LIBRESSL_PQ +# include +#endif namespace i2p { namespace crypto @@ -81,6 +83,7 @@ namespace crypto EVP_PKEY * m_Pkey; #else MLKEM_private_key * m_Pkey; + MLKEM_public_key * m_PublicKey; // used only for SetPublickKey maybe not neeed #endif }; diff --git a/libi2pd/Signature.cpp b/libi2pd/Signature.cpp index a710aa4b..2bd19bda 100644 --- a/libi2pd/Signature.cpp +++ b/libi2pd/Signature.cpp @@ -576,7 +576,6 @@ namespace crypto else LogPrint (eLogError, "MLDSA44 signing key is not set"); } -#endif #endif } }