mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-16 06:02:18 +02:00
common MLKEMKeys for all types
This commit is contained in:
parent
4c5d0116f8
commit
828cd9d07b
3 changed files with 50 additions and 53 deletions
|
@ -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
|
||||||
|
|
|
@ -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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,11 +22,46 @@ namespace i2p
|
||||||
{
|
{
|
||||||
namespace crypto
|
namespace crypto
|
||||||
{
|
{
|
||||||
|
enum MLKEMTypes
|
||||||
|
{
|
||||||
|
eMLKEM512 = 0,
|
||||||
|
eMLKEM768,
|
||||||
|
eMLKEM1024
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr size_t MLKEM512_KEY_LENGTH = 800;
|
||||||
|
constexpr size_t MLKEM512_CIPHER_TEXT_LENGTH = 768;
|
||||||
|
constexpr size_t MLKEM768_KEY_LENGTH = 1184;
|
||||||
|
constexpr size_t MLKEM768_CIPHER_TEXT_LENGTH = 1088;
|
||||||
|
constexpr size_t MLKEM1024_KEY_LENGTH = 1568;
|
||||||
|
constexpr size_t MLKEM1024_CIPHER_TEXT_LENGTH = 1568;
|
||||||
|
|
||||||
|
constexpr std::array<std::tuple<std::string_view, size_t, size_t>, 3> MLKEMS =
|
||||||
|
{
|
||||||
|
std::make_tuple ("ML-KEM-512", MLKEM512_KEY_LENGTH, MLKEM512_CIPHER_TEXT_LENGTH),
|
||||||
|
std::make_tuple ("ML-KEM-768", MLKEM768_KEY_LENGTH, MLKEM768_CIPHER_TEXT_LENGTH),
|
||||||
|
std::make_tuple ("ML-KEM-1024", MLKEM1024_KEY_LENGTH, MLKEM1024_CIPHER_TEXT_LENGTH)
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr size_t GetMLKEMPublicKeyLen (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 0;
|
||||||
|
return std::get<1>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr size_t GetMLKEMCipherTextLen (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 0;
|
||||||
|
return std::get<2>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]);
|
||||||
|
}
|
||||||
|
|
||||||
class MLKEMKeys
|
class MLKEMKeys
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
MLKEMKeys (std::string_view name, size_t keyLen, size_t ctLen);
|
MLKEMKeys (MLKEMTypes type);
|
||||||
~MLKEMKeys ();
|
~MLKEMKeys ();
|
||||||
|
|
||||||
void GenerateKeys ();
|
void GenerateKeys ();
|
||||||
|
@ -41,54 +77,7 @@ namespace crypto
|
||||||
EVP_PKEY * m_Pkey;
|
EVP_PKEY * m_Pkey;
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr size_t MLKEM512_KEY_LENGTH = 800;
|
std::unique_ptr<MLKEMKeys> CreateMLKEMKeys (i2p::data::CryptoKeyType type);
|
||||||
constexpr size_t MLKEM512_CIPHER_TEXT_LENGTH = 768;
|
|
||||||
constexpr size_t MLKEM768_KEY_LENGTH = 1184;
|
|
||||||
constexpr size_t MLKEM768_CIPHER_TEXT_LENGTH = 1088;
|
|
||||||
constexpr size_t MLKEM1024_KEY_LENGTH = 1568;
|
|
||||||
constexpr size_t MLKEM1024_CIPHER_TEXT_LENGTH = 1568;
|
|
||||||
|
|
||||||
constexpr std::array<std::tuple<std::string_view, size_t, size_t>, 3> MLKEMS =
|
|
||||||
{
|
|
||||||
std::make_tuple ("ML-KEM-512", MLKEM512_KEY_LENGTH, MLKEM512_CIPHER_TEXT_LENGTH),
|
|
||||||
std::make_tuple ("ML-KEM-768", MLKEM768_KEY_LENGTH, MLKEM768_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)
|
|
||||||
{
|
|
||||||
if (type <= i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD ||
|
|
||||||
type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD > (int)MLKEMS.size ()) return 0;
|
|
||||||
return std::get<1>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr size_t GetMLKEMCipherTextLen (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 0;
|
|
||||||
return std::get<2>(MLKEMS[type - i2p::data::CRYPTO_KEY_TYPE_ECIES_X25519_AEAD - 1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue