test_openssl.sh (2437B)
1 #!/bin/sh 2 # Anchors the sfinx hash wrappers to the openssl CLI, no Python. 3 # Skips cleanly when openssl or od is absent. 4 set -eu 5 HERE=$(cd "$(dirname "$0")" && pwd) 6 ROOT=$(cd "${HERE}/.." && pwd) 7 . "${HERE}/_tap.sh" 8 9 CC=${CC:-cc} 10 11 tap_begin "openssl hash anchor" 12 13 if ! command -v openssl >/dev/null 2>&1 || ! command -v od >/dev/null 2>&1; then 14 # No helper to call, so write the lone skip assertion directly 15 printf 'TAP version 13\n' 16 printf '# openssl hash anchor\n' 17 printf 'ok 1 - openssl and od available # SKIP openssl or od not installed\n' 18 printf '1..1\n' 19 exit 0 20 fi 21 tap 1 "openssl and od available" "" 22 23 TMP=$(mktemp -d) 24 trap 'rm -rf "${TMP}"' EXIT 25 26 HASH_FIRST="${ROOT}/lib/finwo/keccak-fast/src/keccak-fast.c" 27 HASH="${HASH_FIRST} ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar.c ${ROOT}/lib/finwo/keccak-fast/src/backend/scalar_bmi.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx2.c ${ROOT}/lib/finwo/keccak-fast/src/backend/avx512.c" 28 if [ ! -f "${HASH_FIRST}" ]; then 29 (cd "${ROOT}" && dep install) >/dev/null 2>&1 || true 30 fi 31 32 if "${CC}" -Wall -Wextra -O2 -I"${ROOT}" -I"${ROOT}/src" -I"${ROOT}/lib/.dep/include" \ 33 "${HERE}/ref/hash.c" ${HASH} -o "${TMP}/hash" >"${TMP}/build.log" 2>&1; then 34 tap 1 "hash helper builds" "" 35 else 36 tap 0 "hash helper builds" "$(cat "${TMP}/build.log")" 37 fi 38 39 # Inputs: empty, a short string, and a 200-byte pattern that crosses the SHAKE rate 40 : >"${TMP}/empty" 41 printf 'abc' >"${TMP}/abc" 42 i=0 43 : >"${TMP}/pattern" 44 while [ $i -lt 200 ]; do 45 printf "\\$(printf '%03o' $((i % 256)))" >>"${TMP}/pattern" 46 i=$((i + 1)) 47 done 48 49 for f in empty abc pattern; do 50 for h in 224 256 384 512; do 51 got=$("${TMP}/hash" $h <"${TMP}/$f") 52 want=$(openssl dgst -sha3-$h -binary <"${TMP}/$f" | od -An -v -tx1 | tr -d ' \n') 53 tap "$([ "$got" = "$want" ] && echo 1 || echo 0)" "sha3-$h $f" "got $got want $want" 54 done 55 done 56 57 # -xoflen is mandatory for SHAKE on openssl 3, probe before the group 58 if printf '' | openssl dgst -shake256 -xoflen 16 -binary >/dev/null 2>&1; then 59 for f in empty abc pattern; do 60 for n in 16 32 64 200; do 61 got=$("${TMP}/hash" shake $n <"${TMP}/$f") 62 want=$(openssl dgst -shake256 -xoflen $n -binary <"${TMP}/$f" | od -An -v -tx1 | tr -d ' \n') 63 tap "$([ "$got" = "$want" ] && echo 1 || echo 0)" "shake256 $n $f" "got $got want $want" 64 done 65 done 66 else 67 tap 0 "openssl dgst supports -shake256 -xoflen" "not supported by this openssl" 68 fi 69 70 tap_plan