From ce27bdecdfa0958ce0d3eea0f24bb6328f878fe7 Mon Sep 17 00:00:00 2001 From: orignal Date: Sun, 7 Jun 2026 08:34:19 -0400 Subject: [PATCH] removed MLDSA code --- libi2pd/Signature.cpp | 218 ++++++++++-------------------------------- libi2pd/Signature.h | 127 ++++++++---------------- 2 files changed, 86 insertions(+), 259 deletions(-) diff --git a/libi2pd/Signature.cpp b/libi2pd/Signature.cpp index cad7d484..8c9e665a 100644 --- a/libi2pd/Signature.cpp +++ b/libi2pd/Signature.cpp @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2025, The PurpleI2P Project +* Copyright (c) 2013-2026, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -39,7 +39,7 @@ namespace crypto m_PublicKey = CreateDSA (pub); BN_free (pub); } - + bool DSAVerifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { // signature @@ -88,7 +88,7 @@ namespace crypto bn2buf (s, signature + DSA_SIGNATURE_LENGTH/2, DSA_SIGNATURE_LENGTH/2); DSA_SIG_free(sig); } - + void CreateDSARandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { EVP_PKEY * paramskey = CreateDSA(); @@ -105,9 +105,9 @@ namespace crypto EVP_PKEY_free (pkey); EVP_PKEY_free (paramskey); EVP_PKEY_CTX_free (ctx); - } -#else - + } +#else + DSAVerifier::DSAVerifier () { m_PublicKey = CreateDSA (); @@ -122,7 +122,7 @@ namespace crypto { DSA_set0_key (m_PublicKey, BN_bin2bn (signingKey, DSA_PUBLIC_KEY_LENGTH, NULL), NULL); } - + bool DSAVerifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { // calculate SHA1 digest @@ -170,7 +170,7 @@ namespace crypto bn2buf (pub_key, signingPublicKey, DSA_PUBLIC_KEY_LENGTH); DSA_free (dsa); } -#endif +#endif #if (OPENSSL_VERSION_NUMBER >= 0x030000000) // since 3.0.0 ECDSAVerifier::ECDSAVerifier (int curve, size_t keyLen, const EVP_MD * hash): @@ -182,20 +182,20 @@ namespace crypto { if (m_PublicKey) EVP_PKEY_free (m_PublicKey); - } + } void ECDSAVerifier::SetPublicKey (const uint8_t * signingKey) { if (m_PublicKey) - { + { EVP_PKEY_free (m_PublicKey); m_PublicKey = nullptr; - } + } auto plen = GetPublicKeyLen (); std::vector pub(plen + 1); pub[0] = POINT_CONVERSION_UNCOMPRESSED; memcpy (pub.data() + 1, signingKey, plen); // 0x04|x|y - OSSL_PARAM_BLD * paramBld = OSSL_PARAM_BLD_new (); + OSSL_PARAM_BLD * paramBld = OSSL_PARAM_BLD_new (); OSSL_PARAM_BLD_push_utf8_string (paramBld, OSSL_PKEY_PARAM_GROUP_NAME, OBJ_nid2ln(m_Curve), 0); OSSL_PARAM_BLD_push_octet_string (paramBld, OSSL_PKEY_PARAM_PUB_KEY, pub.data (), pub.size ()); OSSL_PARAM * params = OSSL_PARAM_BLD_to_param(paramBld); @@ -210,16 +210,16 @@ namespace crypto } else LogPrint (eLogError, "ECDSA can't create PKEY context"); - - OSSL_PARAM_free (params); + + OSSL_PARAM_free (params); OSSL_PARAM_BLD_free (paramBld); - } + } bool ECDSAVerifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { // signature ECDSA_SIG * sig = ECDSA_SIG_new(); - ECDSA_SIG_set0 (sig, BN_bin2bn (signature, GetSignatureLen ()/2, NULL), + ECDSA_SIG_set0 (sig, BN_bin2bn (signature, GetSignatureLen ()/2, NULL), BN_bin2bn (signature + GetSignatureLen ()/2, GetSignatureLen ()/2, NULL)); // to DER format std::vector sign(GetSignatureLen () + 8); @@ -232,17 +232,17 @@ namespace crypto auto ret = EVP_DigestVerify (ctx, sign.data (), l, buf, len) == 1; EVP_MD_CTX_destroy (ctx); return ret; - } + } ECDSASigner::ECDSASigner (int curve, size_t keyLen, const EVP_MD * hash, const uint8_t * signingPrivateKey): m_KeyLen (keyLen), m_Hash(hash), m_PrivateKey (nullptr) { BIGNUM * priv = BN_bin2bn (signingPrivateKey, keyLen/2, NULL); - OSSL_PARAM_BLD * paramBld = OSSL_PARAM_BLD_new (); + OSSL_PARAM_BLD * paramBld = OSSL_PARAM_BLD_new (); OSSL_PARAM_BLD_push_utf8_string (paramBld, OSSL_PKEY_PARAM_GROUP_NAME, OBJ_nid2ln(curve), 0); - OSSL_PARAM_BLD_push_BN (paramBld, OSSL_PKEY_PARAM_PRIV_KEY, priv); - OSSL_PARAM * params = OSSL_PARAM_BLD_to_param(paramBld); - + OSSL_PARAM_BLD_push_BN (paramBld, OSSL_PKEY_PARAM_PRIV_KEY, priv); + OSSL_PARAM * params = OSSL_PARAM_BLD_to_param(paramBld); + EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name (NULL, "EC", NULL); if (ctx) { @@ -254,11 +254,11 @@ namespace crypto else LogPrint (eLogError, "ECDSA can't create PKEY context"); - OSSL_PARAM_free (params); - OSSL_PARAM_BLD_free (paramBld); - BN_free (priv); + OSSL_PARAM_free (params); + OSSL_PARAM_BLD_free (paramBld); + BN_free (priv); } - + ECDSASigner::~ECDSASigner () { if (m_PrivateKey) @@ -273,7 +273,7 @@ namespace crypto EVP_DigestSignInit (ctx, NULL, m_Hash, NULL, m_PrivateKey); EVP_DigestSign (ctx, sign.data(), &l, buf, len); EVP_MD_CTX_destroy (ctx); - // decode r and s + // decode r and s const uint8_t * s1 = sign.data (); ECDSA_SIG * sig = d2i_ECDSA_SIG (NULL, &s1, l); const BIGNUM * r, * s; @@ -281,8 +281,8 @@ namespace crypto bn2buf (r, signature, m_KeyLen/2); bn2buf (s, signature + m_KeyLen/2, m_KeyLen/2); ECDSA_SIG_free(sig); - } - + } + void CreateECDSARandomKeys (int curve, size_t keyLen, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { EVP_PKEY * pkey = EVP_EC_gen (OBJ_nid2ln(curve)); @@ -299,10 +299,10 @@ namespace crypto bn2buf (y, signingPublicKey + keyLen/2, keyLen/2); BN_free (x); BN_free (y); EVP_PKEY_free (pkey); - } - -#endif - + } + +#endif + EDDSA25519Verifier::EDDSA25519Verifier (): m_Pkey (nullptr) { @@ -322,13 +322,13 @@ namespace crypto bool EDDSA25519Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { if (m_Pkey) - { + { EVP_MD_CTX * ctx = EVP_MD_CTX_create (); EVP_DigestVerifyInit (ctx, NULL, NULL, NULL, m_Pkey); auto ret = EVP_DigestVerify (ctx, signature, 64, buf, len) == 1; - EVP_MD_CTX_destroy (ctx); - return ret; - } + EVP_MD_CTX_destroy (ctx); + return ret; + } else LogPrint (eLogError, "EdDSA verification key is not set"); return false; @@ -387,11 +387,11 @@ namespace crypto void EDDSA25519Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const { - if (m_Fallback) + if (m_Fallback) return m_Fallback->Sign (buf, len, signature); else if (m_Pkey) { - + EVP_MD_CTX * ctx = EVP_MD_CTX_create (); size_t l = 64; uint8_t sig[64]; // temporary buffer for signature. openssl issue #7232 @@ -411,35 +411,35 @@ namespace crypto OSSL_PARAM_utf8_string ("instance", (char *)"Ed25519ph", 9), OSSL_PARAM_END }; - + bool EDDSA25519phVerifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { auto pkey = GetPkey (); if (pkey) - { + { uint8_t digest[64]; SHA512 (buf, len, digest); EVP_MD_CTX * ctx = EVP_MD_CTX_create (); EVP_DigestVerifyInit_ex (ctx, NULL, NULL, NULL, NULL, pkey, EDDSA25519phParams); auto ret = EVP_DigestVerify (ctx, signature, 64, digest, 64); - EVP_MD_CTX_destroy (ctx); - return ret; - } + EVP_MD_CTX_destroy (ctx); + return ret; + } else LogPrint (eLogError, "EdDSA verification key is not set"); return false; } - EDDSA25519phSigner::EDDSA25519phSigner (const uint8_t * signingPrivateKey): + EDDSA25519phSigner::EDDSA25519phSigner (const uint8_t * signingPrivateKey): EDDSA25519Signer (signingPrivateKey) - { + { } - + void EDDSA25519phSigner::Sign (const uint8_t * buf, int len, uint8_t * signature) const { auto pkey = GetPkey (); if (pkey) - { + { uint8_t digest[64]; SHA512 (buf, len, digest); EVP_MD_CTX * ctx = EVP_MD_CTX_create (); @@ -453,129 +453,7 @@ namespace crypto } else LogPrint (eLogError, "EdDSA signing key is not set"); - } -#endif - -#if OPENSSL_PQ - - MLDSA44Verifier::MLDSA44Verifier (): - m_Pkey (nullptr) - { } - - MLDSA44Verifier::~MLDSA44Verifier () - { - EVP_PKEY_free (m_Pkey); - } - - void MLDSA44Verifier::SetPublicKey (const uint8_t * signingKey) - { - if (m_Pkey) - { - EVP_PKEY_free (m_Pkey); - m_Pkey = nullptr; - } - OSSL_PARAM params[] = - { - OSSL_PARAM_octet_string (OSSL_PKEY_PARAM_PUB_KEY, (uint8_t *)signingKey, GetPublicKeyLen ()), - OSSL_PARAM_END - }; - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name (NULL, "ML-DSA-44", NULL); - if (ctx) - { - EVP_PKEY_fromdata_init (ctx); - EVP_PKEY_fromdata (ctx, &m_Pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY, params); - EVP_PKEY_CTX_free (ctx); - } - else - LogPrint (eLogError, "MLDSA44 can't create PKEY context"); - } - - bool MLDSA44Verifier::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const - { - bool ret = false; - if (m_Pkey) - { - EVP_PKEY_CTX * vctx = EVP_PKEY_CTX_new_from_pkey (NULL, m_Pkey, NULL); - if (vctx) - { - EVP_SIGNATURE * sig = EVP_SIGNATURE_fetch (NULL, "ML-DSA-44", NULL); - if (sig) - { - int encode = 1; - OSSL_PARAM params[] = - { - OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING, &encode), - OSSL_PARAM_END - }; - EVP_PKEY_verify_message_init (vctx, sig, params); - ret = EVP_PKEY_verify (vctx, signature, GetSignatureLen (), buf, len) == 1; - EVP_SIGNATURE_free (sig); - } - EVP_PKEY_CTX_free (vctx); - } - else - LogPrint (eLogError, "MLDSA44 can't obtain context from PKEY"); - } - else - LogPrint (eLogError, "MLDSA44 verification key is not set"); - return ret; - } - - MLDSA44Signer::MLDSA44Signer (const uint8_t * signingPrivateKey): - m_Pkey (nullptr) - { - OSSL_PARAM params[] = - { - OSSL_PARAM_octet_string (OSSL_PKEY_PARAM_PRIV_KEY, (uint8_t *)signingPrivateKey, MLDSA44_PRIVATE_KEY_LENGTH), - OSSL_PARAM_END - }; - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name (NULL, "ML-DSA-44", NULL); - if (ctx) - { - EVP_PKEY_fromdata_init (ctx); - EVP_PKEY_fromdata (ctx, &m_Pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY, params); - EVP_PKEY_CTX_free (ctx); - } - else - LogPrint (eLogError, "MLDSA44 can't create PKEY context"); - } - - MLDSA44Signer::~MLDSA44Signer () - { - if (m_Pkey) EVP_PKEY_free (m_Pkey); - } - - void MLDSA44Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - if (m_Pkey) - { - EVP_PKEY_CTX * sctx = EVP_PKEY_CTX_new_from_pkey (NULL, m_Pkey, NULL); - if (sctx) - { - EVP_SIGNATURE * sig = EVP_SIGNATURE_fetch (NULL, "ML-DSA-44", NULL); - if (sig) - { - int encode = 1; - OSSL_PARAM params[] = - { - OSSL_PARAM_int(OSSL_SIGNATURE_PARAM_MESSAGE_ENCODING, &encode), - OSSL_PARAM_END - }; - EVP_PKEY_sign_message_init (sctx, sig, params); - size_t siglen = MLDSA44_SIGNATURE_LENGTH; - EVP_PKEY_sign (sctx, signature, &siglen, buf, len); - EVP_SIGNATURE_free (sig); - } - EVP_PKEY_CTX_free (sctx); - } - else - LogPrint (eLogError, "MLDSA44 can't obtain context from PKEY"); - } - else - LogPrint (eLogError, "MLDSA44 signing key is not set"); - } - -#endif +#endif } } diff --git a/libi2pd/Signature.h b/libi2pd/Signature.h index 43f706bd..fe609fb8 100644 --- a/libi2pd/Signature.h +++ b/libi2pd/Signature.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2025, The PurpleI2P Project +* Copyright (c) 2013-2026, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -50,7 +50,7 @@ namespace crypto class DSAVerifier: public Verifier { public: - + DSAVerifier (); ~DSAVerifier (); @@ -59,14 +59,14 @@ namespace crypto bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const override; size_t GetPublicKeyLen () const override { return DSA_PUBLIC_KEY_LENGTH; }; size_t GetSignatureLen () const override { return DSA_SIGNATURE_LENGTH; }; - + private: #if (OPENSSL_VERSION_NUMBER >= 0x030000000) // since 3.0.0 EVP_PKEY * m_PublicKey; -#else +#else DSA * m_PublicKey; -#endif +#endif }; class DSASigner: public Signer @@ -86,7 +86,7 @@ namespace crypto EVP_PKEY * m_PrivateKey; #else DSA * m_PrivateKey; -#endif +#endif }; void CreateDSARandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey); @@ -95,7 +95,7 @@ namespace crypto constexpr size_t ECDSAP256_KEY_LENGTH = 64; constexpr size_t ECDSAP384_KEY_LENGTH = 96; constexpr size_t ECDSAP521_KEY_LENGTH = 132; - + #if (OPENSSL_VERSION_NUMBER >= 0x030000000) // since 3.0.0 class ECDSAVerifier: public Verifier { @@ -103,10 +103,10 @@ namespace crypto ECDSAVerifier (int curve, size_t keyLen, const EVP_MD * hash); ~ECDSAVerifier (); - + void SetPublicKey (const uint8_t * signingKey); bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; - + size_t GetPublicKeyLen () const { return m_KeyLen; }; size_t GetSignatureLen () const { return m_KeyLen; }; // signature length = key length @@ -116,7 +116,7 @@ namespace crypto size_t m_KeyLen; const EVP_MD * m_Hash; EVP_PKEY * m_PublicKey; - }; + }; class ECDSASigner: public Signer { @@ -126,23 +126,23 @@ namespace crypto ~ECDSASigner (); void Sign (const uint8_t * buf, int len, uint8_t * signature) const; - + private: size_t m_KeyLen; const EVP_MD * m_Hash; EVP_PKEY * m_PrivateKey; - }; - + }; + void CreateECDSARandomKeys (int curve, size_t keyLen, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); -// ECDSA_SHA256_P256 +// ECDSA_SHA256_P256 class ECDSAP256Verifier: public ECDSAVerifier { public: - + ECDSAP256Verifier (): ECDSAVerifier (NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH, EVP_sha256()) {}; - }; + }; class ECDSAP256Signer: public ECDSASigner { @@ -150,8 +150,8 @@ namespace crypto ECDSAP256Signer (const uint8_t * signingPrivateKey): ECDSASigner (NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH, EVP_sha256(), signingPrivateKey) {}; - }; - + }; + inline void CreateECDSAP256RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { CreateECDSARandomKeys (NID_X9_62_prime256v1, ECDSAP256_KEY_LENGTH, signingPrivateKey, signingPublicKey); @@ -161,9 +161,9 @@ namespace crypto class ECDSAP384Verifier: public ECDSAVerifier { public: - + ECDSAP384Verifier (): ECDSAVerifier (NID_secp384r1, ECDSAP384_KEY_LENGTH, EVP_sha384()) {}; - }; + }; class ECDSAP384Signer: public ECDSASigner { @@ -171,8 +171,8 @@ namespace crypto ECDSAP384Signer (const uint8_t * signingPrivateKey): ECDSASigner (NID_secp384r1, ECDSAP384_KEY_LENGTH, EVP_sha384(), signingPrivateKey) {}; - }; - + }; + inline void CreateECDSAP384RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { CreateECDSARandomKeys (NID_secp384r1, ECDSAP384_KEY_LENGTH, signingPrivateKey, signingPublicKey); @@ -182,9 +182,9 @@ namespace crypto class ECDSAP521Verifier: public ECDSAVerifier { public: - + ECDSAP521Verifier (): ECDSAVerifier (NID_secp521r1, ECDSAP521_KEY_LENGTH, EVP_sha512()) {}; - }; + }; class ECDSAP521Signer: public ECDSASigner { @@ -192,15 +192,15 @@ namespace crypto ECDSAP521Signer (const uint8_t * signingPrivateKey): ECDSASigner (NID_secp521r1, ECDSAP521_KEY_LENGTH, EVP_sha512(), signingPrivateKey) {}; - }; - + }; + inline void CreateECDSAP521RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { CreateECDSARandomKeys (NID_secp521r1, ECDSAP521_KEY_LENGTH, signingPrivateKey, signingPublicKey); } - + #else - + struct SHA256Hash { static void CalculateHash (const uint8_t * buf, size_t len, uint8_t * digest) @@ -351,7 +351,7 @@ namespace crypto { CreateECDSARandomKeys (NID_secp521r1, ECDSAP521_KEY_LENGTH, signingPrivateKey, signingPublicKey); } - + #endif // EdDSA @@ -369,9 +369,9 @@ namespace crypto size_t GetSignatureLen () const { return EDDSA25519_SIGNATURE_LENGTH; }; private: - + EVP_PKEY * m_Pkey; - + protected: EVP_PKEY * GetPkey () const { return m_Pkey; }; @@ -384,8 +384,8 @@ namespace crypto bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; }; -#endif - +#endif + class EDDSA25519SignerCompat: public Signer { public: @@ -416,7 +416,7 @@ namespace crypto protected: EVP_PKEY * GetPkey () const { return m_Pkey; }; - + private: EVP_PKEY * m_Pkey; @@ -429,12 +429,12 @@ namespace crypto public: EDDSA25519phSigner (const uint8_t * signingPrivateKey); - + void Sign (const uint8_t * buf, int len, uint8_t * signature) const; }; - -#endif - + +#endif + inline void CreateEDDSA25519RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { EVP_PKEY *pkey = NULL; @@ -609,57 +609,6 @@ namespace crypto RedDSA25519Signer signer (signingPrivateKey); memcpy (signingPublicKey, signer.GetPublicKey (), EDDSA25519_PUBLIC_KEY_LENGTH); } - -#if OPENSSL_PQ -#include - - // Post-Quantum - const size_t MLDSA44_PUBLIC_KEY_LENGTH = 1312; - const size_t MLDSA44_SIGNATURE_LENGTH = 2420; - const size_t MLDSA44_PRIVATE_KEY_LENGTH = 2560; - class MLDSA44Verifier: public Verifier - { - public: - - MLDSA44Verifier (); - void SetPublicKey (const uint8_t * signingKey); - ~MLDSA44Verifier (); - - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; - - size_t GetPublicKeyLen () const { return MLDSA44_PUBLIC_KEY_LENGTH; }; - size_t GetSignatureLen () const { return MLDSA44_SIGNATURE_LENGTH; }; - size_t GetPrivateKeyLen () const { return MLDSA44_PRIVATE_KEY_LENGTH; }; - - private: - - EVP_PKEY * m_Pkey; - }; - - class MLDSA44Signer: public Signer - { - public: - - MLDSA44Signer (const uint8_t * signingPrivateKey); - ~MLDSA44Signer (); - - void Sign (const uint8_t * buf, int len, uint8_t * signature) const; - - private: - - EVP_PKEY * m_Pkey; - }; - - inline void CreateMLDSA44RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) - { - EVP_PKEY * pkey = EVP_PKEY_Q_keygen (NULL, NULL, "ML-DSA-44"); - size_t len = MLDSA44_PUBLIC_KEY_LENGTH; - EVP_PKEY_get_octet_string_param (pkey, OSSL_PKEY_PARAM_PUB_KEY, signingPublicKey, MLDSA44_PUBLIC_KEY_LENGTH, &len); - len = MLDSA44_PRIVATE_KEY_LENGTH; - EVP_PKEY_get_octet_string_param (pkey, OSSL_PKEY_PARAM_PRIV_KEY, signingPrivateKey, MLDSA44_PRIVATE_KEY_LENGTH, &len); - EVP_PKEY_free (pkey); - } -#endif } }