4

I'm using the CodeBlocks IDE for testing the following know example of OpenSLL.

#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>


#include <string.h>

int main(int arc, char *argv[])
{
  /* Set up the key and iv. Do I need to say to not hard code these in a
   * real application? :-)
   */

  /* A 256 bit key */
  unsigned char *key = "01234567890123456789012345678901";

  /* A 128 bit IV */
  unsigned char *iv = "01234567890123456";

  /* Message to be encrypted */
  unsigned char *plaintext =
    "The quick brown fox jumps over the lazy dog";

  /* Buffer for ciphertext. Ensure the buffer is long enough for the
   * ciphertext which may be longer than the plaintext, dependant on the
   * algorithm and mode
   */
  unsigned char ciphertext[128];

  /* Buffer for the decrypted text */
  unsigned char decryptedtext[128];

  int decryptedtext_len, ciphertext_len;

 /* Initialise the library */
  ERR_load_crypto_strings();
  OpenSSL_add_all_algorithms();
  OPENSSL_config(NULL);

  /* Encrypt the plaintext */
  ciphertext_len = encrypt(plaintext, strlen(plaintext), key, iv,ciphertext);

  /* Do something useful with the ciphertext here */
  printf("Ciphertext is:\n");
  BIO_dump_fp(stdout, ciphertext, ciphertext_len);

  /* Decrypt the ciphertext */
  decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv,decryptedtext);

  /* Add a NULL terminator. We are expecting printable text */
  decryptedtext[decryptedtext_len] = '\0';

  /* Show the decrypted text */
  printf("Decrypted text is:\n");
  printf("%s\n", decryptedtext);

  /* Clean up */
  EVP_cleanup();
  ERR_free_strings();

  return 0;
}

I have already compile and installed the latest Openssl libraries and linked to my project.

/usr/local/ssl/lib/libcrypto.a /usr/local/ssl/lib/libssl.a /lib/x86_64-linux-gnu/libdl-2.19.so

However, when I compile my project I always receive the following errors:

||=== Build: Release in CryptoProject (compiler: GNU GCC Compiler) ===|
obj/Release/main.o||In function `main':|
main.c:(.text.startup+0x46)||undefined reference to `encrypt'|
main.c:(.text.startup+0x81)||undefined reference to `decrypt'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Am I missing some libraries to my project? Please Help!

jww
  • 97,681
  • 90
  • 411
  • 885
  • 1
    you're missing the `encrypt` and `decrypt` functions. Read better the examples http://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption – polslinux Aug 07 '14 at 09:26
  • while compiling did you linked crypto and ssl libraries? like `gcc filename.c -lcrypto -lssl` – Sathish Aug 07 '14 at 09:26
  • See [Overcome DLL Hell with Code::Blocks](http://stackoverflow.com/questions/20931941/overcome-dll-hell-with-codeblocks). It shows you where to put the OpenSSL libraries. Ignore the FIPS stuff. And ratehr than using `/usr/.../libcrypto.so`, use `/usr/.../libcrypto.a` That way, you will avoid that DLL I experienced (and Basile claimed did not occur). – jww Aug 07 '14 at 10:00
  • @polslinux You are right! Sorry guys for this post.. Thanks for the help. – Sérgio Almeida Aug 07 '14 at 10:18

3 Answers3

0

In my case, the error is solved by adding LDFLAGS '-lcrypt'. Pay attention, without 'o', NOT '-lcrypto'.

PokerFace
  • 811
  • 2
  • 9
  • 15
0

when I had this error, trying to build the D-Link WBR-1310 router firmware from supplied GPL sources, the problem was the Makefile was looking under /usr for crypt.h and libcrypt.so* instead of the toolchain included in the tarball. I had to modify the Makefile to change $(wildcard /usr/lib/libcrypt.*) to $(shell find ../.. -name libcrypt.*).

after the modification, it located the library and set LIBS accordingly.

jcomeau_ictx
  • 37,688
  • 6
  • 92
  • 107
0

encrypt and decrypt are not OpenSSL functions. Your compiler should have warned you about their implicit declarations. If it didn't, upgrade to the last stable version of gcc. Set it up to treat all warnings as errors.

Linking with libcrypt is not the right solution. This library does contain functions named encrypt and decrypt but these are not the functions you want (they are unrelated to openssl).

I don't know where you have found this exact example, but examples on the OpenSSL wiki usually contain their own implementations of encrypt and decrypt. An example more similar to yours can be found here, it also contains encrypt and decrypt implementations.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243