0

Edit: I extracted the SHA3 code from this question(Generate SHA-3 hash in C++ using OpenSSL library) and SHA3 is in the openssl 1.0.2q(but not 1.0.2r) version.

I am trying to generate SHA3 with the openssl library.

#include <stdio.h>
#include <openssl/evp.h>
unsigned int SHALEN=32;
void sha3(unsigned char *digest, const unsigned char *message, size_t message_len)
{
    EVP_MD_CTX *mdctx;
    mdctx = EVP_MD_CTX_create();
    EVP_DigestInit_ex(mdctx, EVP_sha3_256(), NULL);
    EVP_DigestUpdate(mdctx, message, message_len);
    EVP_DigestFinal_ex(mdctx, digest, &SHALEN);
    EVP_MD_CTX_destroy(mdctx);
}
int main()
{
    unsigned char digest[32];
    unsigned char message[32];
    for(int i=0;i<32;i++)
        message[i]=0;
    sha3(digest,message,32);
}

The above code works on my personal Mac (with openssl 1.0.2q 2018Nov) but not on the server(Ubuntu 16.04, openssl version=1.1.1a). When compiled with

  g++ test.cpp -lcrypto 

It gives

/tmp/ccXTFrzC.o: In function `sha3(unsigned char*, unsigned char const*, unsigned long)':
test.cpp:(.text+0x1e): undefined reference to `EVP_sha3_256'
collect2: error: ld returned 1 exit status

I looked up the openssl manual but it is not very helpful. Is there anything I missed here? Thanks in advance.

BTW, I am advised not to downgrade openssl to solve the problem.

Ruiyu Zhu
  • 71
  • 8
  • Ubuntu 16.04 uses OpenSSL 1.0.2g, which I don't believe has sha3 support. – Shawn May 08 '19 at 19:50
  • 1
    OpenSSL 1.1.1 [doesn't even appear to have `EVP_MD_CTX_create()`](https://www.openssl.org/docs/man1.1.1/man3/EVP_MD_CTX_new.html). It looks like it's been replaced with `EVP_MD_CTX_new()`. So I really have to question your compilation environment. – Andrew Henle May 08 '19 at 19:56
  • I think you are confused. There is no support for SHA3 in OpenSSL 1.0.2. It was first added in OpenSSL 1.1.1 (heres the change log entry https://github.com/openssl/openssl/blob/5c3f1e34b559c9b4372bf48aab63b61a6cd5edbb/CHANGES#L561-L562) – Matt Caswell May 08 '19 at 20:03
  • 1
    @AndrewHenle EVP_MD_CTX_create() does exist in OpenSSL 1.1.1 but it is a macro for EVP_MD_CTX_new() – Matt Caswell May 08 '19 at 20:03
  • @MattCaswell Thanks for that bit of data. You'd think the "**HISTORY** The `EVP_MD_CTX_create()` and `EVP_MD_CTX_destroy()` functions were renamed to `EVP_MD_CTX_new()` and `EVP_MD_CTX_free()` in OpenSSL 1.1.0, respectively." in the documentation might mention that... :-/ – Andrew Henle May 08 '19 at 20:17
  • I extracted the code from this question(https://stackoverflow.com/questions/51144505/generate-sha-3-hash-in-c-using-openssl-library). – Ruiyu Zhu May 08 '19 at 20:44

0 Answers0