Link to home
Start Free TrialLog in
Avatar of siayubi
siayubi

asked on

linking problem using openssl/des.h

I am using Des to encrypt my data but when i want to compile my code. it is giving linkage problem can anyone solve my problem ..............

code is here
****************************************************************************************************
#include <stdio.h>
#include <openssl/des.h>

/* Use libdes to encrypt one input under one key, then show result. */
typedef struct _des_ctx
  {
    unsigned long int encrypt_subkeys[32];
    unsigned long int  decrypt_subkeys[32];
  }
des_ctx[1];

int  main(int argc, char **argv){

  int i;
  const_des_cblock key[]={0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef};
  const_des_cblock plaintext[]={0xab, 0xcd, 0xef, 0xab, 0xcd, 0xef, 0xab, 0xcd};
  des_cblock ciphertext[8];
  des_cblock recoverd[8];
  des_ctx context;
  //*     * Set up the DES encryption context *
  des_set_key((void *)context, (void *)key);
  //     * Encrypt the plaintext *
  des_ecb_encrypt((void *)context, (void *)plaintext, (void *)ciphertext,0);
  for(i = 0; i < 8; i++)
    printf("%2.2x", recoverd[i]);
  printf("\n");      
  //     * To recover the orginal plaintext from ciphertext use: *
  des_ecb_encrypt((void *)context, (void *)ciphertext, (void *)recoverd, 1);
  for(i = 0; i < 8; i++)
    printf("%2.2x", recoverd[i]);
  printf("\n");      
}
*******************************************************************************************************
and linkage error is as follow

*******************************************************************************************************
[siayubi@localhost work]$
/tmp/cclvFxUi.o: In function `main':
/tmp/cclvFxUi.o(.text+0x62): undefined reference to `des_set_key'
/tmp/cclvFxUi.o(.text+0x7b): undefined reference to `des_ecb_encrypt'
/tmp/cclvFxUi.o(.text+0xdd): undefined reference to `des_ecb_encrypt'
*******************************************************************************************************
Avatar of kishorebv
kishorebv

u should probably link the  object files against the proper library files (i am sorry i dont which ones exactly)
[joe@www ssl]$ vi one.c [joe@www ssl]$ gcc one.c -lssl [joe@www ssl]$ ls a.out  one.c  one.c~ [joe@www ssl]$ ./a.out bffff580bffff588bffff590bffff598bffff5a0bffff5a8bffff5b0bffff5b8 bffff580bffff588bffff590bffff598bffff5a0bffff5a8bffff5b0bffff5b8 Segmentation fault [joe@www ssl]$ man ldd [joe@www ssl]$ ldd a.out         libssl.so.2 => /lib/libssl.so.2 (0x40031000)         libc.so.6 => /lib/i686/libc.so.6 (0x42000000)         libcrypto.so.2 => /lib/libcrypto.so.2 (0x40062000)         libdl.so.2 => /lib/libdl.so.2 (0x40136000)         /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000) [joe@www ssl]$
[joe@www ssl]$ vi one.c
[joe@www ssl]$ gcc one.c -lssl
[joe@www ssl]$ ls
a.out  one.c  one.c~
[joe@www ssl]$ ./a.out
bffff580bffff588bffff590bffff598bffff5a0bffff5a8bffff5b0bffff5b8
bffff580bffff588bffff590bffff598bffff5a0bffff5a8bffff5b0bffff5b8
Segmentation fault
[joe@www ssl]$ man ldd
[joe@www ssl]$ ldd a.out
        libssl.so.2 => /lib/libssl.so.2 (0x40031000)
        libc.so.6 => /lib/i686/libc.so.6 (0x42000000)
        libcrypto.so.2 => /lib/libcrypto.so.2 (0x40062000)
        libdl.so.2 => /lib/libdl.so.2 (0x40136000)
        /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)
[joe@www ssl]$
[joe@www lib]$ nm libcrypto.so | grep des_set_key
00031690 T des_set_key
000316e0 T des_set_key_checked
00031740 T des_set_key_unchecked
[joe@www lib]$
Avatar of siayubi

ASKER

what is the solution of this Segmentation fault
ASKER CERTIFIED SOLUTION
Avatar of joesp
joesp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The command to untar the openssl-0.9.7b.tar.gz file is 'tar zxvf openssl-0.9.7b.tar.gz'

then type 'cd openssl-0.9.7b'
then type './configure'
then type 'make'
then type 'make install'

I am wondering what your programming environment is (visual c++, cygwin).  Also, if you are using windows or what else.

Also, it's not a segmentation fault.  It's a linking error.  To get a segmentation fault, you have to first succeed in building the final program.  This isn't the case.  You can't build the final program because it can't find function definitions it needs to make sense out of your 'extern' function calls.
Avatar of siayubi

ASKER

Dear  joesp

I have reinstall OpenSSL but i am still facing the segmentation fault/linkage problem, actually i have write my code in windows platform and then port it on linux. Is there any solution of this problem,
I am very thankful for your assestance.
Avatar of siayubi

ASKER

You can correct my code if it has problem
Sir mr. siayubi,  I think you have all the tools youneed at your disposal so it may be your methnods are missing a few steps.  It does happen, that inadvertently we forget to follow one step in teh installation procedure.  I believe that is the case here.  I have compiled your code and it compiles okay.  If you need further help, I can help with your linux installation.  Soembody will help with the windows problems.  If the reinstall on linux did not work, I suggest you may need to check your compiler switches.  I think openssl goes in /usr/local/ssl by default, not in the root directories.  Therefore, you will need to specify -L/usr/local/ssl/lib -I/usr/local/ssl/include one.c and try again to compile.  The compiler flags tell where the headers and libraries are located.  Also, after you install the library (agin, on linux)  run 'ldconfig' as root.  This will update the system libraries.  Also, add /usr/local/ssl to the bottom of a file called /etc/ld.so.conf and then try to compile with the correct flags.  good luck.   joe