Skip to content

Instantly share code, notes, and snippets.

@Epicyon
Created January 2, 2013 20:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epicyon/4437587 to your computer and use it in GitHub Desktop.
Save Epicyon/4437587 to your computer and use it in GitHub Desktop.
Build shared NaCL (Crypto) on OSX 10.8.2
# Building NaCL shared library on OSX 10.8.2
# Pulled mostly from the NaCL shared library page: http://wiki.ucis.nl/NaCl/Shared
# Download and extract the NaCl sourcecode:
wget -O- http://hyperelliptic.org/nacl/nacl-20110221.tar.bz2 | bunzip2 | tar -xf -
cd nacl-20110221
# Remove some implementations that do not want to be relocatable (it will fall back to another implementation)
rm -r crypto_onetimeauth/poly1305/amd64
# Patch the compiler commands to create relocatable code
# (OS X) be sure to use gnu sed ( brew install gnu-sed ). Apple sed complained incessantly.
gsed -i "s/$/ -fPIC/" okcompilers/c
# Patch the code to accept application input for the key generation functions
# wget -O- http://oss.ucis.nl/nacl/shared/shared.c | patch -p1 # not a patch file, used later to build the actual shared lib
wget -O- http://oss.ucis.nl/nacl/shared/keypair_random_input.patch | patch -p1
wget -O- http://oss.ucis.nl/nacl/shared/reference_only.patch | patch -p1 # do
# Build the NaCl library, this takes some time...
./do
# Build and locate some utilities used by the nacl build process to find the CPU architecture (and output directory) and an useable compiler
gcc okcompilers/abiname.c -o abiname
ABINAME="$(./abiname "" | cut -b 2-)"
BUILDDIR="build/$(hostname | sed 's/\..*//' | tr -cd '[a-z][A-Z][0-9]')"
NaCLLIB="${BUILDDIR}/lib/${ABINAME}/libnacl.a"
OKCOMPILER="$($BUILDDIR/bin/okc-${ABINAME} | head -n 1)"
# Compile some frontend code into a shared library (download, preprocess with NaCl header files, rewrite some lines, compile and link with the static nacl library)
# Header files below required to compile shared.c
wget http://oss.ucis.nl/nacl/shared/crypto_box.h
wget http://oss.ucis.nl/nacl/shared/crypto_box_curve25519xsalsa20poly1305.h
wget http://oss.ucis.nl/nacl/shared/crypto_scalarmult_curve25519.h
wget http://oss.ucis.nl/nacl/shared/crypto_sign_edwards25519sha512batch.h
# wget -O- http://oss.ucis.nl/nacl/shared/shared.c | ${OKCOMPILER} -arch x86_64 -x c -E - -I "${BUILDDIR}/include/${ABINAME}" | sed "s/export_//" | ${OKCOMPILER} -fPIC -nostdlib -shared -Wl,-L build/mini/lib/amd64 -lnacl -o "libnacl.so" -x c - -x none "${NaCLLIB}"
# Having issues with the one-liner build statement. Split it up and expanded the shell variables. Fix later.
wget http://oss.ucis.nl/nacl/shared/shared.c
gcc -m64 -O3 -fomit-frame-pointer -funroll-loops -fPIC -arch x86_64 -x c -E shared.c > shared.preprocessed
cat shared.preprocessed | sed "s/export_//" > shared_after_sed.c
gcc -O3 -fomit-frame-pointer -funroll-loops -arch x86_64 -fPIC -nostdlib -shared shared_sed.c -o libnacl.dylib -Wl,-L./build/$(hostname -s)/lib/amd64/ -lnacl -arch x86_64
# Install library to /usr/local/lib
install -m 644 libnacl.so /usr/local/lib/
# Install header files to /usr/include/nacl
mkdir /usr/local/include/nacl
(
cd /usr/local/include/nacl
wget http://oss.ucis.nl/nacl/shared/crypto_box.h http://oss.ucis.nl/nacl/shared/crypto_box_curve25519xsalsa20poly1305.h http://oss.ucis.nl/nacl/shared/crypto_scalarmult_curve25519.h http://oss.ucis.nl/nacl/shared/crypto_sign_edwards25519sha512batch.h
)
## Add the below to the downloaded .h files above. I'm sure it only belongs in one, fix.
#define crypto_box_curve25519xsalsa20poly1305_ref_PUBLICKEYBYTES 32
#define crypto_box_curve25519xsalsa20poly1305_ref_SECRETKEYBYTES 32
#define crypto_box_curve25519xsalsa20poly1305_ref_BEFORENMBYTES 32
#define crypto_box_curve25519xsalsa20poly1305_ref_NONCEBYTES 24
#define crypto_box_curve25519xsalsa20poly1305_ref_ZEROBYTES 32
#define crypto_box_curve25519xsalsa20poly1305_ref_BOXZEROBYTES 16
# Issue: Using FFI with the shared library imports the function as its originally exported name.
# Since the library does some magic with the header files depending on which platform us being used, crypto_box() is in essence an alias to the real function.
# (Probably wrong nomenclature here, not a programmer.) Fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment