I'm using Ubuntu 19.04 and (supposedly) OpenSSL 1.1.1b, however whilst using EVP_MD_CTX I am getting this issue when compiling code that should otherwise work. I toyed around with OpenSSL (trying to update manually on 18.04) however did uninstall/reinstall successfully (to my knowledge) as it now displays the version mentioned.

#include <stdio.h>
#include <openssl/evp.h>

main(int argc, char *argv[])
{
EVP_MD_CTX mdctx;
const EVP_MD *md;
char mess1[] = "Test Message\n";
char mess2[] = "Hello World\n";
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_len, i;

OpenSSL_add_all_digests();

md = EVP_get_digestbyname(argv[1]);

EVP_MD_CTX_init(&mdctx);
EVP_DigestInit_ex(&mdctx, md, NULL);
EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
EVP_MD_CTX_cleanup(&mdctx);

printf("Digest is: ");
for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
printf("\n");
}

I understand similar questions to this have been asked, however upon attempting the fixes stated as answers I've not been able to remedy mine.

The exact error is:

test2.c:6:12: error: storage size of ‘mdctx’ isn’t known
 EVP_MD_CTX mdctx;

1 Answers

1
dbush On Best Solutions

Starting with OpenSSL 1.1, the EVP_MD_CTX type is an opaque type so you can't create an instance of it.

You need to instead call EVP_MD_CTX_create() which returns a EVP_MD_CTX * you can use in calls to other functions. When you're done using it, call EVP_MD_CTX_destroy to clean up he resources.

EVP_MD_CTX *mdctx;

mdctx = EVP_MD_CTX_create();
EVP_DigestInit_ex(mdctx, md, NULL);
EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);