easyssl 0.51.8d8e9a5
EasySSL is base back end library for your c++ Qt projects.
ecdsassl.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2021-2025 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7
8
9#include "ecdsassl.h"
10
11#include <openssl/types.h>
12#include <openssl/ecdsa.h> // for ECDSA_do_sign, ECDSA_do_verify
13#include <openssl/obj_mac.h> // for NID_secp192k1
14#include <openssl/evp.h>
15#include <openssl/err.h>
16
17#include <QCryptographicHash>
18#include <QDataStream>
19#include <QIODevice>
20#include <QVector>
21#include <easysslutils.h>
22#include <QDebug>
23#include <openssl/pem.h>
24#include <openssl/core_names.h>
25
26namespace EasySSL {
27
28
29ECDSASSL::ECDSASSL(EllipticCurveStandart curveStandart) {
30 setCurve(curveStandart);
31}
32
33void * ECDSASSL::makeRawKeys() const {
34
35 EVP_PKEY *pkey = nullptr;
36 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr);
37 if (!pctx) {
39 return nullptr;
40 }
41
42 EVP_PKEY_keygen_init(pctx);
43 OSSL_PARAM params[2];
44 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
45 const_cast<char*>(getCStr(_curve)),
46 0);
47 params[1] = OSSL_PARAM_construct_end();
48 EVP_PKEY_CTX_set_params(pctx, params);
49
50 EVP_PKEY_generate(pctx, &pkey);
51 EVP_PKEY_CTX_free(pctx);
52
53 if (!pkey) {
55 return nullptr;
56 }
57 return pkey;
58}
59
63
64QSsl::KeyAlgorithm ECDSASSL::keyAlgorithm() const {
65 return QSsl::KeyAlgorithm::Ec;
66}
67
68QByteArray ECDSASSL::signMessage(const QByteArray &inputData,
69 const QByteArray &key) const {
70
71 QByteArray signature;
72
73 auto pkey = EasySSLUtils::byteArrayToBio(key);
74 auto ecPrivateKey = PEM_read_bio_PrivateKey(pkey, nullptr, nullptr, nullptr);
75 BIO_free(pkey);
76
77 if (!ecPrivateKey) {
78 qCritical() << "Error reading private key";
80 return {};
81 }
82
83 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
84 if (mdctx == nullptr) {
85 return {};
86 }
87
88 // Initialize the signing operation
89 if (EVP_DigestSignInit(mdctx, nullptr, EVP_sha256(), nullptr, ecPrivateKey) != 1) {
91
92 EVP_MD_CTX_free(mdctx);
93 return {};
94 }
95
96 auto hash = QCryptographicHash::hash(inputData,
97 QCryptographicHash::Sha256);
98
99 // Provide the message to be signed
100 if (EVP_DigestSignUpdate(mdctx, hash.data(), hash.size()) != 1) {
102
103 EVP_MD_CTX_free(mdctx);
104 return {};
105 }
106
107 size_t signatureLength = EVP_PKEY_size(ecPrivateKey);
108 signature.resize(signatureLength);
109
110 // Perform the final signing operation and obtain the signature
111 if (EVP_DigestSignFinal(mdctx, reinterpret_cast<unsigned char*>(signature.data()), &signatureLength) != 1) {
113
114 EVP_MD_CTX_free(mdctx);
115 return {};
116 }
117
118 signature.resize(signatureLength);
119
120 EVP_MD_CTX_free(mdctx);
121 return signature;
122}
123
124bool ECDSASSL::checkSign(const QByteArray &inputData,
125 const QByteArray &signature,
126 const QByteArray &key) const {
127
128
129 EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
130 if (mdctx == nullptr) {
131 return false;
132 }
133
134 auto pkey = EasySSLUtils::byteArrayToBio(key);
135 auto rsaPublickKey = PEM_read_bio_PUBKEY(pkey, nullptr, nullptr, nullptr);
136 BIO_free(pkey);
137
138 // Initialize the verification operation
139 if (EVP_DigestVerifyInit(mdctx, NULL, EVP_sha256(), NULL, rsaPublickKey) != 1) {
141
142 EVP_MD_CTX_free(mdctx);
143 return false;
144 }
145
146 auto hash = QCryptographicHash::hash(inputData,
147 QCryptographicHash::Sha256);
148
149 // Provide the message to be verified
150 if (EVP_DigestVerifyUpdate(mdctx, hash.data(), hash.size()) != 1) {
152
153 EVP_MD_CTX_free(mdctx);
154 return false;
155 }
156
157 // Perform the signature verification
158 int verificationResult = EVP_DigestVerifyFinal(mdctx,
159 reinterpret_cast<const unsigned char*>(signature.data()),
160 signature.length());
161
162 EVP_MD_CTX_free(mdctx);
163
164 return verificationResult == 1;
165
166}
167
168QByteArray ECDSASSL::decrypt(const QByteArray &, const QByteArray &) {
169 return {};
170}
171
172QByteArray ECDSASSL::encrypt(const QByteArray &, const QByteArray &) {
173 return {};
174}
175
176ECDSASSL::EllipticCurveStandart ECDSASSL::curve() const {
177 return _curve;
178}
179
180void ECDSASSL::setCurve(EllipticCurveStandart newCurve) {
181 _curve = newCurve;
182}
183
184const char *ECDSASSL::getCStr(EllipticCurveStandart value) const {
185 switch (value) {
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";
191
192 default: return nullptr;
193 }
194}
195
196}
ECDSASSL(EllipticCurveStandart curveStandart=EllipticCurveStandart::P_256)
Definition ecdsassl.cpp:29
QByteArray decrypt(const QByteArray &message, const QByteArray &key) override
decrypt This method has empty implementation.
Definition ecdsassl.cpp:168
QByteArray encrypt(const QByteArray &message, const QByteArray &key) override
encrypt This method has empty implementation.
Definition ecdsassl.cpp:172
EllipticCurveStandart curve() const
curve This method returns the current curve method. Using only for generating new pair of keys.
Definition ecdsassl.cpp:176
QByteArray signMessage(const QByteArray &inputData, const QByteArray &key) const override
signMessage This method should be sign the message using the key.
Definition ecdsassl.cpp:68
Features supportedFeatures() const override
supportedFeatures This method should return supported featurs of the current encryption algorithm
Definition ecdsassl.cpp:60
QSsl::KeyAlgorithm keyAlgorithm() const override
keyAlgorithm This method should be return Qt Key algorithm (needed for generate cetrificates....
Definition ecdsassl.cpp:64
void setCurve(EllipticCurveStandart newCurve)
setCurve This method sets new curve standart value.
Definition ecdsassl.cpp:180
void * makeRawKeys() const override
makeKeys This method generate the public and private keys of the ECDSA.
Definition ecdsassl.cpp:33
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.
Definition ecdsassl.cpp:124
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.
Definition icrypto.h:29
@ Signing
Signin and check sign of the data.
Definition icrypto.h:31