1

I have installed OpenSSL . I just want to run a program using OpenSSL.

Here is my program, taken from here .

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "openssl/aes.h"

int main(int argc, char* argv[])
{
  AES_KEY aesKey_;
  unsigned char userKey_[16];
  unsigned char in_[16];
  unsigned char out_[16];
  strcpy(userKey_,"0123456789123456");
  strcpy(in_,"0123456789123456");

  fprintf(stdout,"Original message: %s", in_);
  AES_set_encrypt_key(userKey_, 128, &aesKey_);
  AES_encrypt(in_, out_, &aesKey_);

  AES_set_decrypt_key(userKey_, 128, &aesKey_);
  AES_decrypt(out_, in_,&aesKey_);
  fprintf(stdout,"Recovered Original message: %s", in_);      
  return 0;
}

While compiling the program I got the same error messages as there, but the solution provided there is not working for me.

I am still getting compile error.

$ gcc -I/home/bholanath/Sources/openssl-1.0.1e/include/ op.c -lcrypt 

/tmp/ccvHr9Jr.o: In function `main':
op.c:(.text+0x9c): undefined reference to `AES_set_encrypt_key'
op.c:(.text+0xbc): undefined reference to `AES_encrypt'
op.c:(.text+0xd7): undefined reference to `AES_set_decrypt_key'
op.c:(.text+0xf7): undefined reference to `AES_decrypt'
collect2: error: ld returned 1 exit status

$ gcc op.c -lcrypt 

/tmp/ccDEZMog.o: In function `main':
op.c:(.text+0x9c): undefined reference to `AES_set_encrypt_key'
op.c:(.text+0xbc): undefined reference to `AES_encrypt'
op.c:(.text+0xd7): undefined reference to `AES_set_decrypt_key'
op.c:(.text+0xf7): undefined reference to `AES_decrypt'
collect2: error: ld returned 1 exit status

Any help to remove compilation error and run my program will be great. I am using GCC under Fedora linux.

Community
  • 1
  • 1
bholanath
  • 1,699
  • 1
  • 22
  • 40
  • 1
    Do you have `libcrypt.so` in the libraries path? Note that typically, you should be using `-lssl` and `-lcrypto` (not `-lcrypt`). – Filipe Gonçalves Feb 07 '14 at 09:47
  • Thanks Filipe. using -lcrypto it is working. – bholanath Feb 07 '14 at 09:53
  • You should *not* use `AES_encrypt` and friends. You should be using `EVP_*` functions. See [EVP Symmetric Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) on the OpenSSL wiki. In fact, you should probably be using authenticated encryption because it provides *both* confidentiality and authenticity. See [EVP Authenticated Encryption and Decryption](https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) on the OpenSSL wiki. – jww May 15 '15 at 20:28

3 Answers3

2

The OpenSSL library names are libcrypto and libssl. Try linking them. libcrypt is part of glibc.

Also, your code is invalid.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
  • Thanks rubenvb , using -lcrypto it is working. The code is fine, not invalid. – bholanath Feb 07 '14 at 09:54
  • Your code is invalid, see the declaration of [`strcpy`](http://pubs.opengroup.org/onlinepubs/009695399/functions/strcpy.html) – rubenvb Feb 07 '14 at 10:01
  • with -Wall warning is shown. without that option , it is working fine. gcc op.c -lcrypto – bholanath Feb 07 '14 at 10:11
  • 1
    @bholanath yeah, that is there for a reason. Just add a cast `(char*)` for the strcpy or use `char` and cast it to `(unsigned char*)` for the OpenSSL calls. – rubenvb Feb 07 '14 at 10:21
  • yes with your suggestion of type casting, the warning messages are also not there. thanks. – bholanath Feb 07 '14 at 10:52
1

Your error is that you're linking with -lcrypt rather than -lcrypto, quite simply.

libcrypt is a small part of glibc that provides the standard Unix functions crypt(3) and the like, and is not related to OpenSSL at all.

Dolda2000
  • 25,216
  • 4
  • 51
  • 92
0

For your information it's an linker error because it is searching for the object files but unable to find those. while compiling you are passing wrong library name. You should pass -lcrypto instead of -lcrypt

Chinna
  • 3,930
  • 4
  • 25
  • 55