common MLKEMKeys for all types

This commit is contained in:
orignal 2025-04-14 19:38:43 -04:00
parent 4c5d0116f8
commit 828cd9d07b
3 changed files with 50 additions and 53 deletions

View file

@ -290,7 +290,7 @@ namespace garlic
len -= i2p::crypto::MLKEM512_KEY_LENGTH + 16; len -= i2p::crypto::MLKEM512_KEY_LENGTH + 16;
n++; n++;
m_PQKeys = std::make_unique<i2p::crypto::MLKEM512Keys>(); m_PQKeys = i2p::crypto::CreateMLKEMKeys (i2p::data::CRYPTO_KEY_TYPE_ECIES_MLKEM512_X25519_AEAD);
m_PQKeys->SetPublicKey (encapsKey); m_PQKeys->SetPublicKey (encapsKey);
} }
} }
@ -546,7 +546,7 @@ namespace garlic
if (m_RemoteStaticKeyType == i2p::data::CRYPTO_KEY_TYPE_ECIES_MLKEM512_X25519_AEAD) if (m_RemoteStaticKeyType == i2p::data::CRYPTO_KEY_TYPE_ECIES_MLKEM512_X25519_AEAD)
{ {
i2p::crypto::InitNoiseIKStateMLKEM512 (GetNoiseState (), m_RemoteStaticKey); // bpk i2p::crypto::InitNoiseIKStateMLKEM512 (GetNoiseState (), m_RemoteStaticKey); // bpk
m_PQKeys = std::make_unique<i2p::crypto::MLKEM512Keys>(); m_PQKeys = i2p::crypto::CreateMLKEMKeys (m_RemoteStaticKeyType);
m_PQKeys->GenerateKeys (); m_PQKeys->GenerateKeys ();
} }
else else

View file

@ -18,8 +18,9 @@ namespace i2p
{ {
namespace crypto namespace crypto
{ {
MLKEMKeys::MLKEMKeys (std::string_view name, size_t keyLen, size_t ctLen): MLKEMKeys::MLKEMKeys (MLKEMTypes type):
m_Name (name), m_KeyLen (keyLen), m_CTLen (ctLen),m_Pkey (nullptr) m_Name (std::get<0>(MLKEMS[type])), m_KeyLen (std::get<1>(MLKEMS[type])),
m_CTLen (std::get<2>(MLKEMS[type])), m_Pkey (nullptr)
{ {
} }
@ -95,6 +96,13 @@ namespace crypto
else else
LogPrint (eLogError, "MLKEM512 can't create PKEY context"); LogPrint (eLogError, "MLKEM512 can't create PKEY context");
} }
std::unique_ptr<MLKEMKeys> CreateMLKEMKeys (i2p::data::CryptoKeyType type)
{
if (type <= i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD ||
type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD > (int)MLKEMS.size ()) return nullptr;
return std::make_unique<MLKEMKeys>((MLKEMTypes)(type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1));
}
} }
} }

View file

@ -9,6 +9,7 @@
#ifndef POST_QUANTUM_H__ #ifndef POST_QUANTUM_H__
#define POST_QUANTUM_H__ #define POST_QUANTUM_H__
#include <memory>
#include <string_view> #include <string_view>
#include <array> #include <array>
#include <tuple> #include <tuple>
@ -21,24 +22,11 @@ namespace i2p
{ {
namespace crypto namespace crypto
{ {
class MLKEMKeys enum MLKEMTypes
{ {
public: eMLKEM512 = 0,
eMLKEM768,
MLKEMKeys (std::string_view name, size_t keyLen, size_t ctLen); eMLKEM1024
~MLKEMKeys ();
void GenerateKeys ();
void GetPublicKey (uint8_t * pub) const;
void SetPublicKey (const uint8_t * pub);
void Encaps (uint8_t * ciphertext, uint8_t * shared);
void Decaps (const uint8_t * ciphertext, uint8_t * shared);
private:
const std::string m_Name;
const size_t m_KeyLen, m_CTLen;
EVP_PKEY * m_Pkey;
}; };
constexpr size_t MLKEM512_KEY_LENGTH = 800; constexpr size_t MLKEM512_KEY_LENGTH = 800;
@ -55,27 +43,6 @@ namespace crypto
std::make_tuple ("ML-KEM-1024", MLKEM1024_KEY_LENGTH, MLKEM1024_CIPHER_TEXT_LENGTH) std::make_tuple ("ML-KEM-1024", MLKEM1024_KEY_LENGTH, MLKEM1024_CIPHER_TEXT_LENGTH)
}; };
class MLKEM512Keys: public MLKEMKeys
{
public:
MLKEM512Keys (): MLKEMKeys (std::get<0>(MLKEMS[0]), std::get<1>(MLKEMS[0]), std::get<2>(MLKEMS[0])) {}
};
class MLKEM768Keys: public MLKEMKeys
{
public:
MLKEM768Keys (): MLKEMKeys (std::get<0>(MLKEMS[1]), std::get<1>(MLKEMS[1]), std::get<2>(MLKEMS[1])) {}
};
class MLKEM1024Keys: public MLKEMKeys
{
public:
MLKEM1024Keys (): MLKEMKeys (std::get<0>(MLKEMS[2]), std::get<1>(MLKEMS[2]), std::get<2>(MLKEMS[2])) {}
};
constexpr size_t GetMLKEMPublicKeyLen (i2p::data::CryptoKeyType type) constexpr size_t GetMLKEMPublicKeyLen (i2p::data::CryptoKeyType type)
{ {
if (type <= i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD || if (type <= i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD ||
@ -89,6 +56,28 @@ namespace crypto
type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD > (int)MLKEMS.size ()) return 0; type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD > (int)MLKEMS.size ()) return 0;
return std::get<2>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]); return std::get<2>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]);
} }
class MLKEMKeys
{
public:
MLKEMKeys (MLKEMTypes type);
~MLKEMKeys ();
void GenerateKeys ();
void GetPublicKey (uint8_t * pub) const;
void SetPublicKey (const uint8_t * pub);
void Encaps (uint8_t * ciphertext, uint8_t * shared);
void Decaps (const uint8_t * ciphertext, uint8_t * shared);
private:
const std::string m_Name;
const size_t m_KeyLen, m_CTLen;
EVP_PKEY * m_Pkey;
};
std::unique_ptr<MLKEMKeys> CreateMLKEMKeys (i2p::data::CryptoKeyType type);
} }
} }