I have a 32bytes SHA256 digest data and need to sign it with ECDSA. The private key is in a .pem file. I've already made it in console using the command: openssl.exe dgst -sha256 -sign ecc.pem -out %sigfile% %data_file%. Now i need to do it in c++ and the results I have been getting isn't the same as in console.
What are wrong in the code below, considering the command I uses in console?
bool sign(unsigned char *digest) {
BIO *b_pem;
EVP_PKEY* pkey2 = nullptr;
const char * fileName = "ecc.pem";
pkey2 = EVP_PKEY_new();
b_pem = BIO_new_file(ecFileName, "rb");
PEM_read_bio_PrivateKey(b_pem, &pkey, NULL, NULL))
unsigned char sign[200] = {0};
EVP_MD_CTX *mctx;
const EVP_MD *md = EVP_get_digestbyname("SHA256");
size_t signlen = 0;
mctx = EVP_MD_CTX_new();
if (EVP_DigestSignInit(mctx, NULL, md, NULL, pkey)<=0) {
return false;
}
if (EVP_DigestSignUpdate(mctx, digest, 32) <= 0) {
return false;
}
if (EVP_DigestSignFinal(mctx, sign, &signlen) <=0) {
return false;
}
EVP_MD_CTX_free(mctx);
return true;
}