博客專欄

        EEPW首頁 > 博客 > openssl動態庫生成以及交叉編譯

        openssl動態庫生成以及交叉編譯

        發布人:電子禪石 時間:2020-01-16 來源:工程師 發布文章
        虛擬機環境
        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

        這其中可能需要將生成的makefile 中 -m64注釋掉。

        7.png全部完成之后在安裝目錄下會有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_

        openssltest.c

        #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;
        }

        rc4test.c

        #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;
        }

        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, $(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)

        準備好這幾個文件,然后就可以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 可以使用export 命令。

        ./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)


        這樣用make編譯出來的文件就是針對開發板的可執行文件openssltest-arm
        將可執行文件拷貝到開發板中
        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,然后拷貝到開發板中,運行,運行速度確實快了許多,說明靜態庫真的比動態庫效率高許多。
        ————————————————
        版權聲明:本文為CSDN博主「andylauren」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
        原文鏈接:https://blog.csdn.net/andylauren/article/details/53456340


        *博客內容為網友個人發布,僅代表博主個人觀點,如有侵權請聯系工作人員刪除。



        關鍵詞:

        相關推薦

        技術專區

        關閉
        主站蜘蛛池模板: 建德市| 广水市| 盐城市| 彰武县| 扬州市| 鄂托克前旗| 扬中市| 天镇县| 盐城市| 九龙坡区| 朝阳区| 芦溪县| 镇平县| 乳源| 祁门县| 德庆县| 筠连县| 永登县| 西安市| 三亚市| 鹤峰县| 浦城县| 金平| 宾阳县| 清流县| 连山| 天全县| 桐庐县| 于田县| 长垣县| 阿克苏市| 泸水县| 静乐县| 石屏县| 界首市| 旺苍县| 平谷区| 锡林郭勒盟| 大丰市| 长岛县| 泰和县|