diff --git a/Signature.cpp b/Signature.cpp index 24f84c20..2e83e91c 100644 --- a/Signature.cpp +++ b/Signature.cpp @@ -515,9 +515,23 @@ namespace crypto ~GOSTR3410Curve () { - EC_GROUP_free (m_Group); + EC_GROUP_free (m_Group); } + EC_POINT * MulP (const BIGNUM * n) const + { + BN_CTX * ctx = BN_CTX_new (); + auto p = EC_POINT_new (m_Group); + EC_POINT_mul (m_Group, p, n, nullptr, nullptr, ctx); + BN_CTX_free (ctx); + return p; + } + + bool GetXY (const EC_POINT * p, BIGNUM * x, BIGNUM * y) const + { + return EC_POINT_get_affine_coordinates_GFp (m_Group, p, x, y, nullptr); + } + private: EC_GROUP * m_Group; @@ -579,7 +593,21 @@ namespace crypto } return g_GOSTR3410Curves[paramSet]; } - + + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey) + { + RAND_bytes (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BIGNUM * priv = BN_bin2bn (signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2, nullptr); + const auto& curve = GetGOSTR3410Curve (paramSet); + auto pub = curve->MulP (priv); + BN_free (priv); + BIGNUM * x = BN_new (), * y = BN_new (); + curve->GetXY (pub, x, y); + EC_POINT_free (pub); + bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); + bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); + BN_free (x); BN_free (y); + } } } diff --git a/Signature.h b/Signature.h index 6c0ca76e..204102e8 100644 --- a/Signature.h +++ b/Signature.h @@ -524,22 +524,10 @@ namespace crypto EVP_PKEY * m_PrivateKey; }; + void CreateGOSTR3410RandomKeys (GOSTR3410ParamSet paramSet, uint8_t * signingPrivateKey, uint8_t * signingPublicKey); inline void CreateGOSTR3410RandomKeys (uint8_t * signingPrivateKey, uint8_t * signingPublicKey) { - auto ctx = EVP_PKEY_CTX_new_id(NID_id_GostR3410_2001, nullptr); - EVP_PKEY_keygen_init (ctx); - EVP_PKEY_CTX_ctrl_str (ctx, "paramset", "A"); // TODO should be in one place - EVP_PKEY* pkey = nullptr; - EVP_PKEY_keygen (ctx, &pkey); - const EC_KEY* ecKey = (const EC_KEY*) EVP_PKEY_get0(pkey); - bn2buf (EC_KEY_get0_private_key (ecKey), signingPrivateKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BIGNUM * x = BN_new(), * y = BN_new(); - EC_POINT_get_affine_coordinates_GFp (EC_KEY_get0_group(ecKey), EC_KEY_get0_public_key (ecKey), x, y, NULL); - bn2buf (x, signingPublicKey, GOSTR3410_PUBLIC_KEY_LENGTH/2); - bn2buf (y, signingPublicKey + GOSTR3410_PUBLIC_KEY_LENGTH/2, GOSTR3410_PUBLIC_KEY_LENGTH/2); - BN_free (x); BN_free (y); - EVP_PKEY_CTX_free (ctx); - EVP_PKEY_free (pkey); + CreateGOSTR3410RandomKeys (eGOSTR3410CryptoProA, signingPrivateKey, signingPublicKey); // A by default } } }