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

from  http://blog.csdn.net/andylauren/article/details/53456340

虚拟机环境

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

[cpp]  view plain  copy
  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
[cpp]  view plain  copy
  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
[cpp]  view plain  copy
  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

[plain]  view plain  copy
  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可执行文件,我们执行以下这个文件会有输出

[plain]  view plain  copy
  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文件

[cpp]  view plain  copy
  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文件,效果和在电脑上的效果一样

[cpp]  view plain  copy
  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中。

[plain]  view plain  copy
  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,然后拷贝到开发板中,运行,运行速度确实快了许多,说明静态库真的比动态库效率高许多。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
交叉编译openssl-1.1.1动态库,你需要按照以下步骤进行操作: 1. 确保你的系统已经安装好交叉编译工具链。工具链通常由GNU Compiler Collection (GCC)和相关工具组成,负责将代码编译为目标架构所需的机器代码。 2. 下载openssl-1.1.1源代码包,并解压到本地目录中。 3. 进入解压后的源代码目录,打开命令行终端。 4. 设置交叉编译的环境变量。根据你的交叉编译工具链和目标架构的不同,设置对应的环境变量,例如CC、CROSS_COMPILE、AR等。 5. 执行配置命令,用来生成编译所需的Makefile。通常的配置命令为:./configure --prefix=目标安装路径。 6. 执行make命令进行编译,根据系统性能不同,编译时间可能会有所不同。 7. 执行make install命令,将编译好的动态库安装到指定目录下。 8. 检查安装是否成功。在目标安装路径下查看是否存在libcrypto.so和libssl.so文件,这些文件就是编译生成动态库。 注意事项: - 在交叉编译过程中,可能会遇到一些问题,例如缺少相关依赖库、环境变量错误等,需要根据具体情况进行解决。 - 如果你的目标架构是嵌入式设备或者特定平台,可能还需要进行额外的配置和调整才能正确交叉编译动态库。 - 在进行交叉编译时,最好参考openssl官方文档或者相关教程,以确保正确的编译结果和使用方法。 以上就是关于如何交叉编译openssl-1.1.1动态库的步骤和注意事项的回答,希望能对你有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值