Tuesday, November 17, 2020

How To Cross Compile OpenSSL for ARM

 How To Cross Compile OpenSSL for ARM

In this article we are trying to cross compile OpenSSL for ARM linux machine, such as RaspberryPi or other generic embedded linux environment.

What you need:

You need a virtual machine with Ubuntu installed. I used Ubuntu 16.04 LTS. You can use tool such as Vmware to create the virtual machine.

Steps:

  • Install git 

    Git is usefull if when you need to add additional libraries or to get latest source code.

sudo apt get install git

  • Get OpenSSL source using git.

git clone https://github.com/openssl/openssl.git
  • Install cross compiler.

    You may have different compilers based on your requirement. If you want to compile for a raspberry pi you can use.

git clone https://github.com/raspberrypi/tools.git --depth=1 pitools

This will get the latest version of the repository. If you are using compiler such as gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf get it using

wget https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabihf/gcc-linaro-7.5.0-2019.12-x86_64_arm-linux-gnueabihf.tar.xz

unzip and copy to a specific location. In this case /opt. Use -jxvf for tar.bz2 or use -xvzf for tar.gz

sudo tar -jxvf gcc-linaro-5.3.1-2018.01-x86_64_arm-linux-gnueabihf.tar.bz2 -C /opt
  •  Identify the base folder of the compiler in following manner.

/path/to/basefolder/pitools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/

The base folder will contain C compiler (gcc) C++ compiler (g++) a linker (ld) etc.

  • Go to Openssl directory using cd.

cd openssl/
  • Set the install directory as environmental variable.

export INSTALL_DIR=/home/hasi/Desktop/CCompile/opensslArm
  • Set the compiler base directory as environmental variable.

export CROSSCOMP_DIR=/opt/opt/EmbedSky/gcc-linaro-5.3.1-2016.05-x86_64_arm-linux-gnueabihf/bin
  • Check if the variables are set properly.

echo ${CROSSCOMP_DIR}
echo ${INSTALL_DIR}
  • Check the supported platforms if required. In this case I am using linux-generic32

./Configure LIST
  • Execute the configure script

./Configure linux-generic32 shared --prefix=$INSTALL_DIR --openssldir=$INSTALL_DIR/openssl --cross-compile-prefix=$CROSSCOMP_DIR/arm-linux-gnueabihf-
  • Provide the make commands

make

make depend

make install

If everything matches perfectly, you'll see Openssl compiled binary inside the INSTALL_DIR. Go inside the bin directiry of it and check it using file command.

file openssl

It will show a description that it is for ARM 32 bit

openssl: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, BuildID[sha1]=f18e19ff2b39bb250051636bf57f394854b68ef3, not stripped

Transfer the directory to the targeted machine using SCP or other method and check if the compiled binary working.

cd opensslArm
cd bin
./openssl --help

No comments:

Post a Comment