openssl库linux编译

虚拟机环境

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. typedef enum {
  4. GENERAL = 0,
  5. ECB,
  6. CBC,
  7. CFB,
  8. OFB,
  9. TRIPLE_ECB,
  10. TRIPLE_CBC
  11. }CRYPTO_MODE;
  12. //string DES_Encrypt(const string cleartext, const string key, CRYPTO_MODE mode);
  13. //string DES_Decrypt(const string ciphertext, const string key, CRYPTO_MODE mode);
  14. char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen);
  15. char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen);
  16. #endif //_CRYPTOTEST_H_
openssltest.c

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

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

#####################################################################
## file        : test makefile for build current dir .c   ##
## author      : jernymy                                  ##
## date-time   : 05/06/2010                               ##
#####################################################################

CC = gcc
CPP = g++
RM = rm -rf

## debug flag
DBG_ENABLE = 0

## source file path
SRC_PATH := .

## target exec file name
TARGET := openssltest

## get all source files
SRCS += $(wildcard $(SRC_PATH)/*.c)

## all .o based on all .c
OBJS := $(SRCS:.c=.o)

## need libs, add at here
LIBS := ssl crypto

## used headers file path
INCLUDE_PATH := /home/linux/opt/openssl/include/

## used include librarys file path
LIBRARY_PATH := /home/linux/opt/openssl/lib/

## debug for debug info, when use gdb to debug
ifeq (1, ${DBG_ENABLE})
CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
endif

## get all include path
CFLAGS += $(foreach dir, ( I N C L U D E P A T H ) , − I (INCLUDE_PATH), -I (INCLUDEPATH),I(dir))

## get all library path
LDFLAGS += $(foreach lib, ( L I B R A R Y P A T H ) , − L (LIBRARY_PATH), -L (LIBRARYPATH),L(lib))

## get all librarys
LDFLAGS += $(foreach lib, ( L I B S ) , − l (LIBS), -l (LIBS),l(lib))

all: clean build

build:
$(CC) -c $(CFLAGS) $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
$(RM) $(OBJS)

clean:
$(RM) $(OBJS) $(TARGET)准备好这几个文件,然后就可以make了,会生成openssltest可执行文件,我们执行以下这个文件会有输出

linux@ubuntu:~/work/opensslDemo/rc4test$ ./openssltest
cleartext:中国北京12345$abcde%ABCDE@!!!
genarate ciphertext:Zu�)�0Xv�ݏ����
src ciphertext:Zu�)�0Xv�ݏ����
genarate ciphertext:中国北京12345$abcde%ABCDE@!!!
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. CC = arm-linux-gcc
  7. CPP = g++
  8. RM = rm -rf
  9. ## debug flag
  10. DBG_ENABLE = 0
  11. ## source file path
  12. SRC_PATH := .
  13. ## target exec file name
  14. TARGET := openssltest-arm
  15. ## get all source files
  16. SRCS += $(wildcard $(SRC_PATH) /*.c)
  17. ## all .o based on all .c
  18. OBJS := $(SRCS:.c=.o)
  19. ## need libs, add at here
  20. LIBS := ssl crypto
  21. ## used headers file path
  22. INCLUDE_PATH := /home/linux/arm/openssl/include/
  23. ## used include librarys file path
  24. LIBRARY_PATH := /home/linux/arm/openssl/lib/
  25. ## debug for debug info, when use gdb to debug
  26. ifeq (1, ${DBG_ENABLE})
  27. CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
  28. endif
  29. ## get all include path
  30. CFLAGS += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
  31. ## get all library path
  32. LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
  33. ## get all librarys
  34. LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))
  35. all: clean build
  36. build:
  37. $(CC) -c $(CFLAGS) $(SRCS)
  38. $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
  39. $(RM) $(OBJS)
  40. clean:
  41. $(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中。

linux@ubuntu:~/arm/openssl/lib$ ls
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
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值