C File Compile (GCC) issue Openssl library

Hello all,

I am trying to compile some C code given to me so I can add to it. The code relies on the openssl library and more specifically the DES header file (des.h). I am compiling within Ubuntu Linux (V9) using the GCC compiler. I have included the code (des.c), the makefile, the des.h file from the openssl library, and terminal session.

Here is the makefile:
-------------------------------------------------------------Makefile-------------------------------
all: des

des: des.o
      gcc des.o -o des

des.o: des.c
      gcc des.c

clean:
      rm -rf *o des
----------------------------------------------End Of Makefile---------------------------------



---------------------------Here is the terminal session:-------------------------------------

LH@ubuntu:~/Desktop$ make
gcc des.c
/tmp/cccOgmOc.o: In function `main':
des.c:(.text+0xbc): undefined reference to `DES_set_key_checked'
des.c:(.text+0x144): undefined reference to `Des_encrypt1'
des.c:(.text+0x199): undefined reference to `Des_encrypt1'
collect2: ld returned 1 exit status
make: *** [des.o] Error 1
---------Second Try without the makefile---------
LH@ubuntu:~/Desktop$ gcc -o hw3 des.c
/tmp/ccGtTbWb.o: In function `main':
des.c:(.text+0xb6): undefined reference to `DES_set_key_checked'
des.c:(.text+0x12b): undefined reference to `DES_encrypt1'
des.c:(.text+0x16d): undefined reference to `DES_encrypt1'
collect2: ld returned 1 exit status

------------------------End of Terminal Session--------------------------------------


I have tried with and without the makefile I created and still....Nothing. I must be missing something but I am not sure what it is. I have updated OpenSSL on my OS as well. I have checked the Des_encrypt1 function in the header file within the openssl library, here is what that function's code looks like:

---------------------- function code within des.h of the openssl library-----------------------
/*       This is the DES encryption function that gets called by just about
      every other DES routine in the library.  You should not use this
      function except to implement 'modes' of DES.  I say this because the
      functions that call this routine do the conversion from 'char *' to
      long, and this needs to be done to make sure 'non-aligned' memory
      access do not occur.  The characters are loaded 'little endian'.
      Data is a pointer to 2 unsigned long's and ks is the
      DES_key_schedule to use.  enc, is non zero specifies encryption,
      zero if decryption. */
void DES_encrypt1(DES_LONG *data,DES_key_schedule *ks, int enc);





 If anyone could point out my error(s) it would be very appreciated. I will keep trying until then. Thanks in advance!




#include <stdio.h>
#include <stdlib.h>
#include <openssl/des.h>

#define ENC 1
#define DEC 0


DES_LONG ctol(unsigned char *c) {
        DES_LONG l;
        l =((DES_LONG)(*((c)++)));
        l = l | (((DES_LONG)(*((c)++)))<<8L);
        l = l | (((DES_LONG)(*((c)++)))<<16L);
        l = l | (((DES_LONG)(*((c)++)))<<24L);
        return l;
};



ltoc(DES_LONG l, unsigned char *c) {
        *((c)++)=(unsigned char)(l&0xff);
        *((c)++)=(unsigned char)(((l)>> 8L)&0xff);
        *((c)++)=(unsigned char)(((l)>>16L)&0xff);
        *((c)++)=(unsigned char)(((l)>>24L)&0xff);
}

int main()
{
	int k;
	long	in[2];
	static unsigned char cbc_key[8] = {0x01,0x23,0x45,0x67,0x89,0xab,0x01,0xef};
	des_key_schedule key;

	if ((k = des_set_key_checked(&cbc_key,key)) != 0)
		printf("\nkey error\n");

	in[0] = 3212314;
	in[1] = 4231233;

	printf("DES Clear Text: %ld %ld\n",in[0],in[1]);
	Des_encrypt1(in,key,ENC);

	printf("DES Encryption: %ld %ld\n",in[0],in[1]);

	Des_encrypt1(in,key,DEC);

	printf("DES Decryption: %ld %ld\n",in[0],in[1]);
}

Open in new window

cybershock13045Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pgnatyukCommented:
You need to link this program with a library file. Modify the make file.
0
fridomCEO/ProgrammerCommented:
You have to install the openssl-dev libraries.

Assuming that you have,  then you have to use the proper Writing it's not
Des_encrypt1 but DES_encrypt1

and according to the docs you should not use that function directly but probably something along
Des_(cbc,ncbc,xdbc..)_encrypt

And you have to include the proper libraries.
so  you need some -lssl on the command lines

So maybe gcc -g des.c -lssl


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cybershock13045Author Commented:
Not much experience with makefiles. To link the program with the library file I would just do:

all: des

LIBRARIES: openssl/des.h

des: des.o
      gcc des.o -o des

des.o: des.c
      gcc des.c

clean:
      rm -rf *o des


That's what i've found online for performing this operation and it doesn't seem right, could you tell me what the syntax for library linking in this is? Or link (html) me to a good tutorial on it?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

cybershock13045Author Commented:
Odd, we (myself and Fridom) posted at the same time. I must have put up the older version, the one I have I had the capatalization issue solved, but now I have this when I input that command into the terminal:

@ubuntu:~/Desktop$ gcc -g des.c -lssl
des.c: In function ‘main’:
des.c:41: error: incompatible type for argument 2 of ‘DES_encrypt1’
/usr/include/openssl/des.h:162: note: expected ‘struct DES_key_schedule *’ but argument is of type ‘DES_key_schedule’
des.c:45: error: incompatible type for argument 2 of ‘DES_encrypt1’
/usr/include/openssl/des.h:162: note: expected ‘struct DES_key_schedule *’ but argument is of type ‘DES_key_schedule’


I have tried/changed multiple things to get this to work with the struct pointer --> struct problem....all were unsuccessful. Posting up new code (little changed, mainly the capitalization of DES in the function call)

#include <stdio.h>
#include <stdlib.h>
#include <openssl/des.h>
#include <openssl/e_os2.h>

#define ENC 1
#define DEC 0


DES_LONG ctol(unsigned char *c) {
        DES_LONG l;
        l =((DES_LONG)(*((c)++)));
        l = l | (((DES_LONG)(*((c)++)))<<8L);
        l = l | (((DES_LONG)(*((c)++)))<<16L);
        l = l | (((DES_LONG)(*((c)++)))<<24L);
        return l;
};


unsigned char ltoc(DES_LONG l, unsigned char *c) {
        *((c)++)=(unsigned char)(l&0xff);
        *((c)++)=(unsigned char)(((l)>> 8L)&0xff);
        *((c)++)=(unsigned char)(((l)>>16L)&0xff);
        *((c)++)=(unsigned char)(((l)>>24L)&0xff);
}

int main()
{
	int k;
	long	in[2];
	static unsigned char cbc_key[8] = {0x01,0x23,0x45,0x67,0x89,0xab,0x01,0xef};
	des_key_schedule key;

	if ((k = des_set_key_checked(&cbc_key,key)) != 0)
		printf("\nkey error\n");

	in[0] = 3212314;
	in[1] = 4231233;

	printf("DES Clear Text: %ld %ld\n",in[0],in[1]);
	DES_encrypt1(in,key,ENC);

	printf("DES Encryption: %ld %ld\n",in[0],in[1]);

	DES_encrypt1(in,key,DEC);

	printf("DES Decryption: %ld %ld\n",in[0],in[1]);
}

Open in new window

0
cybershock13045Author Commented:
Got it, Thanks both of you :)

Heres the new (and improved!) makefile:
------------------------------
des: des.o
      gcc des.o -o des -lssl

des.o: des.c
      gcc -c -o des.o des.c -lssl

clean:
      rm des des.o
------------------------------
0
fridomCEO/ProgrammerCommented:
Your comment about linking against the library is wrong.. I posted the correct code.

the name is stil wrong you are using des but it is DES_key_schedule and it should be a pointer
So you have to write
DES_encrypt1(in,  &key, ENC);

And again you have not read what I wrote  you should not use this function directly because of alignment issues. To  avoid them use the "public" anounced functions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.