Sorry for the long preamble, but here goes...
I want to be able to read my LUKS encrypted USB drive on my phone and so I was searching for cryptsetup for Android. I found an abandoned Guardian Project repo which led me to an old blog post about compiling a static binary on Fedora 11. The Guardian Project repo seems to be using the ideas from the blog post but instead of using armv5tel-redhat-linux-gnueabi-gcc-c++, its using Android NDK. There's an app on the Play Store which claims to use ideas from this repo and it also provides a GUI. There's also a thread on XDA forums where a user is providing static binaries compiled using Android NDK.
These, however, are outdated or not trustable. Now that Debian has packaged a GNU cross compiler which can target armel/armhf architecture, I wanted to try building cryptsetup using it. The advantage in using Debian tools is the much smaller download/install size. The disadvantage is a larger sized binary. Using the ideas from this Github gist, I ended up with the following shell script which achieves the result:
arch=armel;triplet=arm-linux-gnueabi
#arch=armhf;triplet=arm-linux-gnueabihf #either option works
DEVMAPPERDIR=$HOME/libdevmapper
sudo dpkg --add-architecture $arch
#enable unstable sources
echo 'deb-src http://deb.debian.org/debian/ sid main'|sudo tee -a /etc/apt/sources.list
sudo apt update
#toolchain
sudo apt -y install gcc-$triplet dpkg-dev autopoint automake libtool pkg-config
#package compilation dependencies
sudo apt -y install libselinux1-dev:$arch libgcrypt20-dev:$arch uuid-dev:$arch libpopt-dev:$arch libjson-c-dev:$arch
apt-get source cryptsetup lvm2
cd lvm2-*
#patch for linking errors
sed -i 's/SELINUX_LIBS="-lsepol"/SELINUX_LIBS="-lsepol -lpcre"/' configure
./configure --enable-static-link --enable-pkgconfig --prefix=$DEVMAPPERDIR --host=$triplet ac_cv_func_malloc_0_nonnull=yes ac_cv_func_realloc_0_nonnull=yes
make install_device-mapper
cd ../cryptsetup-*
./autogen.sh && ./configure --enable-static-cryptsetup --host=$triplet PKG_CONFIG_PATH="$DEVMAPPERDIR/lib/pkgconfig" CFLAGS="-I$DEVMAPPERDIR/include/" LDFLAGS="-Wl,-rpath-link,$DEVMAPPERDIR/lib"
make install DESTDIR=$HOME/cryptsetup
The packaged version of libdevmapper-dev doesn't include a static library since it depends on udev which has dropped support for static builds. Since udev isn't needed by cryptsetup, I compiled libdevmapper from source and passed appropriate flags while compiling cryptsetup.
Now I come to the questions:
- Am I doing this the right way?
- The binary so obtained requires Linux v3.2(Android v4.2) or greater due to the requirements of the glibc packaged with Debian Stretch. Is there some simple way of targeting an earlier kernel? Debian Jessie would allow targeting v2.6.32 (Android v2.2) but there's no cross compiler.
- Is Android NDK FOSS?