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
- #ifndef _CRYPTOTEST_H_
- #define _CRYPTOTEST_H_
- typedef enum {
- GENERAL = 0,
- ECB,
- CBC,
- CFB,
- OFB,
- TRIPLE_ECB,
- TRIPLE_CBC
- }CRYPTO_MODE;
- //string DES_Encrypt(const string cleartext, const string key, CRYPTO_MODE mode);
- //string DES_Decrypt(const string ciphertext, const string key, CRYPTO_MODE mode);
- char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen);
- char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen);
- #endif //_CRYPTOTEST_H_
- #include “cryptotest.h”
- #include <string.h>
- #include <stdio.h>
- int main()
- {
- char cleartext[] = “中国北京12345$abcde%ABCDE@!!!”;
- char *ciphertext;
- char key[] = “beijingchina1234567890ABCDEFGH!!!”;
- ciphertext = RC4_Encrypt(cleartext, key, strlen(cleartext), strlen(key));
- char * decrypt = RC4_Decrypt(ciphertext, key, strlen(cleartext), strlen(key));
- printf(”cleartext:%s\n”, cleartext);
- printf(”genarate ciphertext:%s\n”, ciphertext);
- printf(”src ciphertext:%s\n”, ciphertext);
- printf(”genarate ciphertext:%s\n”, decrypt);
- if (strcmp(cleartext, decrypt) == 0)
- printf(”RC4 crypto ok!!!\n”);
- else
- printf(”RC4 crypto error!!!\n”);
- return 0;
- }
- #include <stdlib.h>
- #include <openssl/rc4.h>
- #include <string.h>
- #include “cryptotest.h”
- char * RC4_Encrypt(const char *cleartext, const char * key, int cleartextlen, int keylen)
- {
- RC4_KEY rc4key;
- char* tmp = malloc(cleartextlen + 1);
- memset(tmp, 0, cleartextlen + 1);
- RC4_set_key(&rc4key, keylen, (const unsigned char*)key);
- RC4(&rc4key, cleartextlen, (const unsigned char*)cleartext, tmp);
- return tmp;
- }
- char * RC4_Decrypt(const char * ciphertext, const char * key, int cleartextlen, int keylen)
- {
- RC4_KEY rc4key;
- unsigned char* tmp = malloc(cleartextlen + 1);
- memset(tmp, 0, cleartextlen + 1);
- RC4_set_key(&rc4key, keylen, (const unsigned char*)key);
- RC4(&rc4key, cleartextlen, (const unsigned char*)ciphertext, tmp);
- return tmp;
- }
- #####################################################################
- ## 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, (INCLUDE_PATH), -I$(dir))
- ## get all library path
- LDFLAGS += (foreach lib, (LIBRARY_PATH), -L$(lib))
- ## get all librarys
- LDFLAGS += (foreach lib, (LIBS), -l$(lib))
- all: clean build
- build:
- (CC) -c (CFLAGS) $(SRCS)
- (CC) (CFLAGS) -o (TARGET) (OBJS) $(LDFLAGS)
- (RM) (OBJS)
- clean:
- (RM) (OBJS) $(TARGET)
- 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文件
- #####################################################################
- ## file : test makefile for build current dir .c ##
- ## author : jernymy ##
- ## date-time : 05/06/2010 ##
- #####################################################################
- CC = arm-linux-gcc
- CPP = g++
- RM = rm -rf
- ## debug flag
- DBG_ENABLE = 0
- ## source file path
- SRC_PATH := .
- ## target exec file name
- TARGET := openssltest-arm
- ## 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/arm/openssl/include/
- ## used include librarys file path
- LIBRARY_PATH := /home/linux/arm/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, (INCLUDE_PATH), -I$(dir))
- ## get all library path
- LDFLAGS += (foreach lib, (LIBRARY_PATH), -L$(lib))
- ## get all librarys
- LDFLAGS += (foreach lib, (LIBS), -l$(lib))
- all: clean build
- build:
- (CC) -c (CFLAGS) $(SRCS)
- (CC) (CFLAGS) -o (TARGET) (OBJS) $(LDFLAGS)
- (RM) (OBJS)
- clean:
- (RM) (OBJS) $(TARGET)
将可执行文件拷贝到开发板中
linux@ubuntu:~/work/opensslDemo/rc4test$ cp openssltest-arm /nfsroot/rootfs/root/
在开发板中执行openssltest-arm文件,效果和在电脑上的效果一样
- root@EasyARM-iMX28x ~# ./openssltest-arm
- cleartext:涓浗鍖椾含12345$abcde%ABCDE@锛侊紒锛
- genarate ciphertext:ZuXv幂徛栝
- src ciphertext:ZuXv幂徛栝
- genarate ciphertext:涓浗鍖椾含12345$abcde%ABCDE@锛侊紒锛
- 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,然后拷贝到开发板中,运行,运行速度确实快了许多,说明静态库真的比动态库效率高许多。