From cd860bfbf8b0427dd9633601382f087cade9ef21 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 9 Mar 2017 21:46:34 -0500 Subject: [PATCH] correct param set for GOST R 34.10 signing --- Identity.cpp | 4 ++-- Signature.cpp | 13 +++++++++++++ Signature.h | 29 +++++++---------------------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/Identity.cpp b/Identity.cpp index aecb11cc..f76bc62b 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -525,7 +525,7 @@ namespace data m_Signer.reset (new i2p::crypto::EDDSA25519Signer (m_SigningPrivateKey, m_Public->GetStandardIdentity ().certificate - i2p::crypto::EDDSA25519_PUBLIC_KEY_LENGTH)); break; case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - m_Signer.reset (new i2p::crypto::GOSTR3410Signer (m_SigningPrivateKey)); + m_Signer.reset (new i2p::crypto::GOSTR3410Signer (i2p::crypto::eGOSTR3410CryptoProA, m_SigningPrivateKey)); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)m_Public->GetSigningKeyType (), " is not supported"); @@ -563,7 +563,7 @@ namespace data i2p::crypto::CreateEDDSA25519RandomKeys (keys.m_SigningPrivateKey, signingPublicKey); break; case SIGNING_KEY_TYPE_GOSTR3410_A_GOSTR3411: - i2p::crypto::CreateGOSTR3410RandomKeys (keys.m_SigningPrivateKey, signingPublicKey); + i2p::crypto::CreateGOSTR3410RandomKeys (i2p::crypto::eGOSTR3410CryptoProA, keys.m_SigningPrivateKey, signingPublicKey); break; default: LogPrint (eLogError, "Identity: Signing key type ", (int)type, " is not supported. Create DSA-SHA1"); diff --git a/Signature.cpp b/Signature.cpp index 72826aa9..30e4fc03 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -614,6 +614,19 @@ namespace crypto return g_GOSTR3410Curves[paramSet]; } + void GOSTR3410Signer::Sign (const uint8_t * buf, int len, uint8_t * signature) const + { + uint8_t digest[32]; + GOSTR3411 (buf, len, digest); + BIGNUM * d = BN_bin2bn (digest, 32, nullptr); + BIGNUM * r = BN_new (), * s = BN_new (); + const auto& curve = GetGOSTR3410Curve (m_ParamSet); + curve->Sign (m_PrivateKey, d, r, s); + bn2buf (r, signature, GOSTR3410_SIGNATURE_LENGTH/2); + bn2buf (s, signature + GOSTR3410_SIGNATURE_LENGTH/2, GOSTR3410_SIGNATURE_LENGTH/2); + BN_free (d); BN_free (r); BN_free (s); + } + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); diff --git a/Signature.h b/Signature.h index 204102e8..c505524c 100644 --- a/Signature.h +++ b/Signature.h @@ -498,37 +498,22 @@ namespace crypto { public: - GOSTR3410Signer (const uint8_t * signingPrivateKey) + GOSTR3410Signer (GOSTR3410ParamSet paramSet, const uint8_t * signingPrivateKey): + m_ParamSet (paramSet) { - m_PrivateKey = EVP_PKEY_new (); - EC_KEY * ecKey = EC_KEY_new (); - EVP_PKEY_assign (m_PrivateKey, NID_id_GostR3410_2001, ecKey); - EVP_PKEY_copy_parameters (m_PrivateKey, GetGostPKEY ()); - EC_KEY_set_private_key (ecKey, BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, NULL)); + m_PrivateKey = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); } - ~GOSTR3410Signer () { EVP_PKEY_free (m_PrivateKey); } + ~GOSTR3410Signer () { BN_free (m_PrivateKey); } - void Sign (const uint8_t * buf, int len, uint8_t * signature) const - { - uint8_t digest[32]; - GOSTR3411 (buf, len, digest); - EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new (m_PrivateKey, nullptr); - EVP_PKEY_sign_init (ctx); - size_t l = GOSTR3410_SIGNATURE_LENGTH; - EVP_PKEY_sign (ctx, signature, &l, digest, 32); - EVP_PKEY_CTX_free (ctx); - } + void Sign (const uint8_t * buf, int len, uint8_t * signature) const; private: - EVP_PKEY * m_PrivateKey; + GOSTR3410ParamSet m_ParamSet; + BIGNUM * m_PrivateKey; }; void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); - inline void CreateGOSTR3410RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) - { - CreateGOSTR3410RandomKeys (eGOSTR3410CryptoProA, signingPrivateKey, signingPublicKey); // A by default - } } }