11#include <openssl/types.h>
12#include <openssl/ecdsa.h>
13#include <openssl/obj_mac.h>
14#include <openssl/evp.h>
15#include <openssl/err.h>
17#include <QCryptographicHash>
23#include <openssl/pem.h>
24#include <openssl/core_names.h>
35 EVP_PKEY *pkey =
nullptr;
36 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(
nullptr,
"EC",
nullptr);
42 EVP_PKEY_keygen_init(pctx);
44 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
45 const_cast<char*
>(getCStr(_curve)),
47 params[1] = OSSL_PARAM_construct_end();
48 EVP_PKEY_CTX_set_params(pctx, params);
50 EVP_PKEY_generate(pctx, &pkey);
51 EVP_PKEY_CTX_free(pctx);
65 return QSsl::KeyAlgorithm::Ec;
69 const QByteArray &key)
const {
74 auto ecPrivateKey = PEM_read_bio_PrivateKey(pkey,
nullptr,
nullptr,
nullptr);
78 qCritical() <<
"Error reading private key";
83 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
84 if (mdctx ==
nullptr) {
89 if (EVP_DigestSignInit(mdctx,
nullptr, EVP_sha256(),
nullptr, ecPrivateKey) != 1) {
92 EVP_MD_CTX_free(mdctx);
96 auto hash = QCryptographicHash::hash(inputData,
97 QCryptographicHash::Sha256);
100 if (EVP_DigestSignUpdate(mdctx, hash.data(), hash.size()) != 1) {
103 EVP_MD_CTX_free(mdctx);
107 size_t signatureLength = EVP_PKEY_size(ecPrivateKey);
108 signature.resize(signatureLength);
111 if (EVP_DigestSignFinal(mdctx,
reinterpret_cast<unsigned char*
>(signature.data()), &signatureLength) != 1) {
114 EVP_MD_CTX_free(mdctx);
118 signature.resize(signatureLength);
120 EVP_MD_CTX_free(mdctx);
125 const QByteArray &signature,
126 const QByteArray &key)
const {
129 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
130 if (mdctx ==
nullptr) {
135 auto rsaPublickKey = PEM_read_bio_PUBKEY(pkey,
nullptr,
nullptr,
nullptr);
139 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, rsaPublickKey) != 1) {
142 EVP_MD_CTX_free(mdctx);
146 auto hash = QCryptographicHash::hash(inputData,
147 QCryptographicHash::Sha256);
150 if (EVP_DigestVerifyUpdate(mdctx, hash.data(), hash.size()) != 1) {
153 EVP_MD_CTX_free(mdctx);
158 int verificationResult = EVP_DigestVerifyFinal(mdctx,
159 reinterpret_cast<const unsigned char*
>(signature.data()),
162 EVP_MD_CTX_free(mdctx);
164 return verificationResult == 1;
184const char *ECDSASSL::getCStr(EllipticCurveStandart value)
const {
186 case P_256:
return "P-256";
187 case P_384:
return "P-384";
188 case P_521:
return "P-521";
189 case X448:
return "X448";
190 case X25519:
return "X25519";
192 default:
return nullptr;
ECDSASSL(EllipticCurveStandart curveStandart=EllipticCurveStandart::P_256)
QByteArray decrypt(const QByteArray &message, const QByteArray &key) override
decrypt This method has empty implementation.
QByteArray encrypt(const QByteArray &message, const QByteArray &key) override
encrypt This method has empty implementation.
EllipticCurveStandart curve() const
curve This method returns the current curve method. Using only for generating new pair of keys.
QByteArray signMessage(const QByteArray &inputData, const QByteArray &key) const override
signMessage This method should be sign the message using the key.
Features supportedFeatures() const override
supportedFeatures This method should return supported featurs of the current encryption algorithm
QSsl::KeyAlgorithm keyAlgorithm() const override
keyAlgorithm This method should be return Qt Key algorithm (needed for generate cetrificates....
void setCurve(EllipticCurveStandart newCurve)
setCurve This method sets new curve standart value.
void * makeRawKeys() const override
makeKeys This method generate the public and private keys of the ECDSA.
bool checkSign(const QByteArray &inputData, const QByteArray &signature, const QByteArray &key) const override
checkSign This method should be check signature of the message using the key.
static BIO * byteArrayToBio(const QByteArray &byteArray)
byteArrayToBio This method creates the BIO struct from the Qt QByteArray object.
static void printlastOpenSSlError()
printlastOpenSSlError This method prints the latest ssl error message.
Features
The Features enum this is list of the supported description features.
@ Signing
Signin and check sign of the data.