openssl动态库生成以及交叉编译

虚拟机环境

ubuntu12.04

开发板

EasyARM-i.MX280A:   64m  sdram  128M  nandflash   运行官方提供的Linux-2.6.35.3内核linux

首先说一下如何在主机上进行编译,并生成动态库

在https://www.openssl.org/source/下载最新版的openssl,我下载的是 openssl-1.1.0c.tar.gz版本

拷贝到虚拟机中,找地方解压,

然后是经典三部曲,首先使用 ./config   make   make install

首先使用指令./config shared --prefix=/home/linux/opt/openssl --openssldir=/home/linux/opt/openssl/ssl

prefix 是安装目录,openssldir 是配置文件目录,shared 作用是生成动态连接库。

然后就是make

make install

全部完成之后在安装目录下会有lib文件夹,里面有我们需要的动态库和静态库文件

libcrypto.a  libcrypto.so  libcrypto.so.1.1  libssl.a  libssl.so  libssl.so.1.1 

然后我们跳转到/home/linux/opt/openssl/lib目录下,将动态库拷贝到系统库目录中/lib中

sudo cp -a libcrypto.so* libssl.so* /lib

大功告成,我们写点程序测试一下,我们写一个使用rc4加解密的程序测试一下

cryptotest.h

  1. #ifndef _CRYPTOTEST_H_  
  2. #define _CRYPTOTEST_H_  
  3.   
  4.   
  5. typedef enum {  
  6.     GENERAL = 0,  
  7.     ECB,  
  8.     CBC,  
  9.     CFB,  
  10.     OFB,  
  11.     TRIPLE_ECB,  
  12.     TRIPLE_CBC  
  13. }CRYPTO_MODE;  
  14.   
  15. //string DES_Encrypt(const string cleartext, const string key, CRYPTO_MODE mode);  
  16. //string DES_Decrypt(const string ciphertext, const string key, CRYPTO_MODE mode);  
  17.   
  18. char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen);  
  19. char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen);  
  20.   
  21. #endif //_CRYPTOTEST_H_  

openssltest.c

  1. #include "cryptotest.h"  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4.   
  5. int main()  
  6. {  
  7.     char cleartext[] = "中国北京12345$abcde%ABCDE@!!!";  
  8.     char *ciphertext;  
  9.     char key[] = "beijingchina1234567890ABCDEFGH!!!";  
  10.   
  11.     ciphertext = RC4_Encrypt(cleartext, key, strlen(cleartext), strlen(key));  
  12.     char * decrypt = RC4_Decrypt(ciphertext, key, strlen(cleartext), strlen(key));  
  13.   
  14.     printf("cleartext:%s\n", cleartext);  
  15.     printf("genarate ciphertext:%s\n", ciphertext);  
  16.     printf("src ciphertext:%s\n", ciphertext);  
  17.     printf("genarate ciphertext:%s\n", decrypt);  
  18.   
  19.     if (strcmp(cleartext, decrypt) == 0)  
  20.         printf("RC4 crypto ok!!!\n");  
  21.     else  
  22.         printf("RC4 crypto error!!!\n");  
  23.     return 0;  
  24. }  

rc4test.c

  1. #include <stdlib.h>  
  2. #include <openssl/rc4.h>  
  3. #include <string.h>  
  4. #include "cryptotest.h"  
  5.   
  6. char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen)  
  7. {  
  8.     RC4_KEY rc4key;  
  9.     char* tmp = malloc(cleartextlen + 1);  
  10.     memset(tmp, 0, cleartextlen + 1);  
  11.   
  12.     RC4_set_key(&rc4key, keylen, (const unsigned char*)key);  
  13.     RC4(&rc4key, cleartextlen, (const unsigned char*)cleartext, tmp);  
  14.   
  15.     return tmp;  
  16. }  
  17.   
  18. char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen)  
  19. {  
  20.     RC4_KEY rc4key;  
  21.     unsigned char* tmp = malloc(cleartextlen + 1);  
  22.     memset(tmp, 0, cleartextlen + 1);  
  23.   
  24.     RC4_set_key(&rc4key, keylen, (const unsigned char*)key);  
  25.     RC4(&rc4key, cleartextlen, (const unsigned char*)ciphertext, tmp);  
  26.   
  27.     return tmp;  
  28. }  

makefile

  1. #####################################################################  
  2. ## file        : test makefile for build current dir .c   ##  
  3. ## author      : jernymy                                  ##  
  4. ## date-time   : 05/06/2010                               ##  
  5. #####################################################################  
  6.   
  7. CC      = gcc  
  8. CPP     = g++  
  9. RM      = rm -rf  
  10.   
  11. ## debug flag  
  12. DBG_ENABLE   = 0  
  13.   
  14. ## source file path  
  15. SRC_PATH   := .  
  16.   
  17. ## target exec file name  
  18. TARGET     := openssltest  
  19.   
  20. ## get all source files  
  21. SRCS         += $(wildcard $(SRC_PATH)/*.c)  
  22.   
  23. ## all .o based on all .c  
  24. OBJS        := $(SRCS:.c=.o)  
  25.   
  26.   
  27. ## need libs, add at here  
  28. LIBS := ssl crypto  
  29.   
  30. ## used headers  file path  
  31. INCLUDE_PATH := /home/linux/opt/openssl/include/  
  32.   
  33. ## used include librarys file path  
  34. LIBRARY_PATH := /home/linux/opt/openssl/lib/  
  35.   
  36. ## debug for debug info, when use gdb to debug  
  37. ifeq (1, ${DBG_ENABLE})   
  38.     CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1  
  39. endif  
  40.   
  41. ## get all include path  
  42. CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))  
  43.   
  44. ## get all library path  
  45. LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))  
  46.   
  47. ## get all librarys  
  48. LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))  
  49.   
  50.   
  51. all: clean build  
  52.   
  53. build:  
  54.     $(CC) -c $(CFLAGS) $(SRCS)  
  55.     $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)  
  56.     $(RM) $(OBJS)  
  57.   
  58. clean:  
  59.     $(RM) $(OBJS) $(TARGET)  

准备好这几个文件,然后就可以make了,会生成openssltest可执行文件,我们执行以下这个文件会有输出

  1. linux@ubuntu:~/work/opensslDemo/rc4test$ ./openssltest  
  2. cleartext:中国北京12345$abcde%ABCDE@!!!  
  3. genarate ciphertext:Zu�)�0Xv�ݏ����  
  4. src ciphertext:Zu�)�0Xv�ݏ����  
  5. genarate ciphertext:中国北京12345$abcde%ABCDE@!!!  
  6. RC4 crypto ok!!!  

下面我们要准备开始交叉编译

在openssl解压目录下,使用config命令

CC=arm-linux-gcc ./config no-asm shared --prefix=/home/linux/arm/openssl --openssldir=/home/linux/arm/openssl/ssl

生成了Makefile

然后就是make和make install

之后会在安装目录下生成lib文件

跳转到lib目录下linux@ubuntu:~$ cd arm/openssl/lib/
复制动态库文件到开发板中,因为我是用的nfs文件系统,所以复制到了nfs文件系统下

linux@ubuntu:~/arm/openssl/lib$ cp -a libcrypto.so* libssl.so* /nfsroot/rootfs/lib/

然后修改刚刚的代码中的Makefile文件

  1. #####################################################################  
  2. ## file        : test makefile for build current dir .c   ##  
  3. ## author      : jernymy                                  ##  
  4. ## date-time   : 05/06/2010                               ##  
  5. #####################################################################  
  6.   
  7. CC      = arm-linux-gcc  
  8. CPP     = g++  
  9. RM      = rm -rf  
  10.   
  11. ## debug flag  
  12. DBG_ENABLE   = 0  
  13.   
  14. ## source file path  
  15. SRC_PATH   := .  
  16.   
  17. ## target exec file name  
  18. TARGET     := openssltest-arm  
  19.   
  20. ## get all source files  
  21. SRCS         += $(wildcard $(SRC_PATH)/*.c)  
  22.   
  23. ## all .o based on all .c  
  24. OBJS        := $(SRCS:.c=.o)  
  25.   
  26.   
  27. ## need libs, add at here  
  28. LIBS := ssl crypto  
  29.   
  30. ## used headers  file path  
  31. INCLUDE_PATH := /home/linux/arm/openssl/include/  
  32.   
  33. ## used include librarys file path  
  34. LIBRARY_PATH := /home/linux/arm/openssl/lib/  
  35.   
  36. ## debug for debug info, when use gdb to debug  
  37. ifeq (1, ${DBG_ENABLE})   
  38.     CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1  
  39. endif  
  40.   
  41. ## get all include path  
  42. CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))  
  43.   
  44. ## get all library path  
  45. LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))  
  46.   
  47. ## get all librarys  
  48. LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))  
  49.   
  50.   
  51. all: clean build  
  52.   
  53. build:  
  54.     $(CC) -c $(CFLAGS) $(SRCS)  
  55.     $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)  
  56.     $(RM) $(OBJS)  
  57.   
  58. clean:  
  59.     $(RM) $(OBJS) $(TARGET)  

这样用make编译出来的文件就是针对开发板的可执行文件openssltest-arm

将可执行文件拷贝到开发板中

linux@ubuntu:~/work/opensslDemo/rc4test$ cp openssltest-arm /nfsroot/rootfs/root/
在开发板中执行openssltest-arm文件,效果和在电脑上的效果一样

  1. root@EasyARM-iMX28x ~# ./openssltest-arm   
  2. cleartext:涓浗鍖椾含12345$abcde%ABCDE@锛侊紒锛  
  3. genarate ciphertext:ZuXv幂徛栝  
  4. src ciphertext:ZuXv幂徛栝  
  5. genarate ciphertext:涓浗鍖椾含12345$abcde%ABCDE@锛侊紒锛  
  6. RC4 crypto ok!!!  

开发板的速度较慢,所以执行比电脑慢许多,下面我们来使用静态库编译一下,看一下效果会不会好一些。

其实我们在编译openssl的时候动态库和静态库已经同时被编译出来了,都存放在安转目录下的lib中。

  1. linux@ubuntu:~/arm/openssl/lib$ ls  
  2. engines-1.1  libcrypto1.so  libcrypto.a  libcrypto.so.1.1  libssl1.so  libssl.a  libssl.so.1.1  pkgconfig  

其中.a结尾的文件就是动态库。其实想要使用静态库编译很简单。

在应用程序需要连接外部库的情况下,Linux默认对库的连接是使用动态库,在找不到动态库的情况下再选择静态库。
所以只要将lib文件夹中的.so文件删除,系统在编译的时候就会使用静态库编译。

仍然是make,然后拷贝到开发板中,运行,运行速度确实快了许多,说明静态库真的比动态库效率高许多。

猜你喜欢

转载自blog.csdn.net/zhangbaoqiang1/article/details/81629101