1# Copyright (c) 2019, Google Inc. 2# 3# Permission to use, copy, modify, and/or distribute this software for any 4# purpose with or without fee is hereby granted, provided that the above 5# copyright notice and this permission notice appear in all copies. 6# 7# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 10# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION 12# OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 13# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 14 15# This script exists to exercise breaking each of the FIPS tests on an Android 16# device. Since, on Android, BoringCrypto exists in both 32- and 64-bit 17# versions, the first argument must be either "32" or "64" to select which is 18# being tested. The Android source tree must have been setup (with "lunch") for 19# a matching build configuration before using this script to build the 20# binaries. (Although it'll fail non-silently if there's a mismatch.) 21# 22# Since each test needs the FIPS module to be compiled differently, and that 23# can take a long time, this script is run twice: once with "build" as the 24# second argument to run the builds, and then with "run" as the second argument 25# to run each test. 26# 27# Run it with /bin/bash, not /bin/sh, otherwise "read" may fail. 28# 29# In order to reconfigure the build for each test, it needs to set a define. It 30# does so by rewriting a template in external/boringssl/Android.bp and you must 31# add the template value before doing the builds. To do so, insert 32# -DBORINGSSL_FIPS_BREAK_XXX=1 in the cflags list for the module, probably by 33# putting it in the "boringssl_flags" stanza. 34 35set -x 36set -e 37 38if [ ! -f external/boringssl/Android.bp ]; then 39 echo "Must be run from the top-level of an Android source tree." 40 exit 1 41fi 42 43. build/envsetup.sh 44 45TESTS="NONE ECDSA_PWCT CRNG RSA_PWCT AES_CBC AES_GCM DES SHA_1 SHA_256 SHA_512 RSA_SIG DRBG ECDSA_SIG Z_COMPUTATION TLS_KDF FFC_DH" 46 47if [ "x$1" = "x32" ]; then 48 lib="lib" 49 bits="32" 50elif [ "x$1" = "x64" ] ; then 51 lib="lib64" 52 bits="64" 53else 54 echo "First argument must be 32 or 64" 55 exit 1 56fi 57 58if [ "x$2" = "xbuild" ]; then 59 if ! grep -q DBORINGSSL_FIPS_BREAK_XXX=1 external/boringssl/Android.bp; then 60 echo "Missing DBORINGSSL_FIPS_BREAK_XXX in external/boringssl/Android.bp. Edit the file and insert -DBORINGSSL_FIPS_BREAK_XXX=1 in the cflags for the FIPS module" 61 exit 1 62 fi 63 64 printf "\\x1b[1mBuilding modules\\x1b[0m\n" 65 for test in $TESTS; do 66 printf "\\x1b[1mBuilding for ${test}\\x1b[0m\n" 67 cp external/boringssl/Android.bp external/boringssl/Android.bp.orig 68 sed -i -e "s/DBORINGSSL_FIPS_BREAK_XXX/DBORINGSSL_FIPS_BREAK_${test}/" external/boringssl/Android.bp 69 m test_fips 70 dir=test-${bits}-${test} 71 rm -Rf $dir 72 mkdir $dir 73 cp ${ANDROID_PRODUCT_OUT}/system/${lib}/libcrypto.so $dir 74 cp ${ANDROID_PRODUCT_OUT}/system/bin/test_fips $dir 75 if [ $bits = "32" ] ; then 76 if ! file ${dir}/test_fips | grep -q "32-bit" ; then 77 echo "32-bit build requested but binaries don't appear to be 32-bit:" 78 file ${dir}/test_fips 79 exit 1 80 fi 81 else 82 if ! file ${dir}/test_fips | grep -q "64-bit" ; then 83 echo "64-bit build requested but binaries don't appear to be 64-bit:" 84 file ${dir}/test_fips 85 exit 1 86 fi 87 fi 88 cp external/boringssl/Android.bp.orig external/boringssl/Android.bp 89 done 90elif [ "x$2" = "xrun" ]; then 91 printf "\\x1b[1mTesting\\x1b[0m\n" 92 for test in $TESTS; do 93 dir=test-${bits}-${test} 94 if [ ! '(' -d ${dir} -a -f ${dir}/test_fips -a -f ${dir}/libcrypto.so ')' ] ; then 95 echo "Build directory ${dir} is missing or is missing files" 96 exit 1 97 fi 98 adb push ${dir}/* /data/local/tmp 99 printf "\\x1b[1mTesting ${test}\\x1b[0m\n" 100 adb shell -n -t -x LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/test_fips 101 read 102 done 103 104 printf "\\x1b[1mTesting integrity}\\x1b[0m\n" 105 src=test-${bits}-NONE 106 dir=test-${bits}-INT 107 rm -Rf $dir 108 mkdir $dir 109 go run external/boringssl/src/util/fipstools/break-hash.go ${src}/libcrypto.so ${dir}/libcrypto.so 110 cp ${src}/test_fips $dir 111 adb push ${dir}/* /data/local/tmp 112 adb shell -n -t -x LD_LIBRARY_PATH=/data/local/tmp /data/local/tmp/test_fips 113 read 114else 115 echo "Second argument must be build or run" 116 exit 1 117fi 118