|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause-Clear |
| 5 | + |
| 6 | +# Robustly find and source init_env |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 8 | +INIT_ENV="" |
| 9 | +SEARCH="$SCRIPT_DIR" |
| 10 | +while [ "$SEARCH" != "/" ]; do |
| 11 | + if [ -f "$SEARCH/init_env" ]; then |
| 12 | + INIT_ENV="$SEARCH/init_env" |
| 13 | + break |
| 14 | + fi |
| 15 | + SEARCH=$(dirname "$SEARCH") |
| 16 | +done |
| 17 | + |
| 18 | +if [ -z "$INIT_ENV" ]; then |
| 19 | + echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2 |
| 20 | + exit 1 |
| 21 | +fi |
| 22 | + |
| 23 | +# Only source if not already loaded (idempotent) |
| 24 | +if [ -z "$__INIT_ENV_LOADED" ]; then |
| 25 | + # shellcheck disable=SC1090 |
| 26 | + . "$INIT_ENV" |
| 27 | +fi |
| 28 | + |
| 29 | +# Always source functestlib.sh, using $TOOLS exported by init_env |
| 30 | +# shellcheck disable=SC1090,SC1091 |
| 31 | +. "$TOOLS/functestlib.sh" |
| 32 | + |
| 33 | +TESTNAME="qcom_hwrng" |
| 34 | +test_path=$(find_test_case_by_name "$TESTNAME") |
| 35 | +cd "$test_path" || exit 1 |
| 36 | +res_file="./$TESTNAME.res" |
| 37 | + |
| 38 | +log_info "-----------------------------------------------------------------------------------------" |
| 39 | +log_info "-------------------Starting $TESTNAME Testcase----------------------------" |
| 40 | +log_info "=== Test Initialization ===" |
| 41 | + |
| 42 | +log_info "Checking if dependency binary is available" |
| 43 | +check_dependencies rngtest dd |
| 44 | + |
| 45 | +# Set the hardware RNG source to Qualcomm's RNG |
| 46 | +RNG_PATH="/sys/class/misc/hw_random/rng_current" |
| 47 | +if [ -e "$RNG_PATH" ]; then |
| 48 | + echo qcom_hwrng > "$RNG_PATH" |
| 49 | +else |
| 50 | + log_fail "$TESTNAME : RNG path $RNG_PATH does not exist" |
| 51 | + echo "$TESTNAME FAIL" > "$res_file" |
| 52 | + exit 1 |
| 53 | +fi |
| 54 | + |
| 55 | +# Verify that qcom_hwrng was successfully set |
| 56 | +current_rng=$(cat "$RNG_PATH") |
| 57 | +if [ "$current_rng" != "qcom_hwrng" ]; then |
| 58 | + log_fail "$TESTNAME : Failed to set qcom_hwrng as the current RNG source" |
| 59 | + echo "$TESTNAME FAIL" > "$res_file" |
| 60 | + exit 1 |
| 61 | +else |
| 62 | + log_info "qcom_hwrng successfully set as the current RNG source." |
| 63 | +fi |
| 64 | + |
| 65 | +TMP_OUT="./qcom_hwrng_output.txt" |
| 66 | +ENTROPY_B=20000032 |
| 67 | +RNG_SOURCE="/dev/random" |
| 68 | + |
| 69 | +log_info "Running rngtest with $ENTROPY_B bytes of entropy from $RNG_SOURCE..." |
| 70 | + |
| 71 | +# Generate entropy and run rngtest |
| 72 | +if ! dd if="$RNG_SOURCE" bs=1 count="$ENTROPY_B" status=none 2>/dev/null | rngtest -c 1000 2>&1 | tee "$TMP_OUT"; then |
| 73 | + log_fail "$TESTNAME : rngtest pipeline execution failed" |
| 74 | + echo "$TESTNAME FAIL" > "$res_file" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +# Parse FIPS 140-2 failures |
| 79 | +failures=$(awk '/FIPS 140-2 failures:/ {print $NF}' "$TMP_OUT" | head -n1) |
| 80 | + |
| 81 | +if [ -z "$failures" ] || ! echo "$failures" | grep -Eq '^[0-9]+$'; then |
| 82 | + log_fail "rngtest did not return a valid integer for failures; got: '$failures'" |
| 83 | + echo "$TESTNAME FAIL" > "$res_file" |
| 84 | + rm -f "$TMP_OUT" |
| 85 | + exit 1 |
| 86 | +fi |
| 87 | + |
| 88 | +log_info "rngtest: FIPS 140-2 failures = $failures" |
| 89 | +# You can tune this threshold as needed (10 means <1% fail allowed) |
| 90 | +if [ "$failures" -lt 10 ]; then |
| 91 | + log_pass "$TESTNAME : Test Passed ($failures failures)" |
| 92 | + echo "$TESTNAME PASS" > "$res_file" |
| 93 | + rm -f "$TMP_OUT" |
| 94 | + exit 0 |
| 95 | +else |
| 96 | + log_fail "$TESTNAME : Test Failed ($failures failures)" |
| 97 | + echo "$TESTNAME FAIL" > "$res_file" |
| 98 | + rm -f "$TMP_OUT" |
| 99 | + exit 1 |
| 100 | +fi |
| 101 | + |
| 102 | +log_info "-------------------Completed $TESTNAME Testcase----------------------------" |
0 commit comments