Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Runner/suites/Kernel/Baseport/qcom_hwrng/README_qcom_hwrng.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
SPDX-License-Identifier: BSD-3-Clause-Clear

# Qualcomm Hardware Random Number Generator (QRNG) Script
# Overview

The qcom_hwrng test script validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality. This test ensures that the HWRNG kernel driver is correctly integrated and functional.

## Features

- Driver Validation: Confirms the presence and correct configuration of the qcom_hwrng kernel driver.
- Dependency Check: Verifies the availability of required tools like rngtest before execution.
- Automated Result Logging: Outputs test results to a .res file for automated result collection.
- Remote Execution Ready: Supports remote deployment and execution via scp and ssh.

## Prerequisites

Ensure the following components are present in the target:

- `rngtest` (Binary Available in /usr/bin) - this test app can be compiled from https://github.com/cernekee/rng-tools/

## Directory Structure
```
Runner/
├── suites/
│ ├── Kernel/
│ │ │ ├── Baseport/
│ │ │ │ ├── qcom_hwrng/
│ │ │ │ │ ├── run.sh
```
## Usage

1. Copy repo to Target Device: Use scp to transfer the scripts from the host to the target device. The scripts should be copied to the /var directory on the target device.

2. Verify Transfer: Ensure that the repo have been successfully copied to the /var directory on the target device.

3. Run Scripts: Navigate to the /var directory on the target device and execute the scripts as needed.

---
Quick Example
```
git clone <this-repo>
cd <this-repo>
scp -r common Runner user@target_device_ip:/<user-defined-location>
ssh user@target_device_ip
cd /<user-defined-location>/Runner && ./run-test.sh qcom_hwrng

Sample output:
sh-5.2# ./run-test.sh qcom_hwrng
[Executing test case: qcom_hwrng] 2025-11-03 10:28:02 -
[INFO] 2025-11-03 10:28:02 - -----------------------------------------------------------------------------------------
[INFO] 2025-11-03 10:28:02 - -------------------Starting qcom_hwrng Testcase----------------------------
[INFO] 2025-11-03 10:28:02 - === Test Initialization ===
[INFO] 2025-11-03 10:28:02 - Checking if dependency binary is available
[INFO] 2025-11-03 10:28:02 - qcom_hwrng successfully set as the current RNG source.
[INFO] 2025-11-03 10:28:02 - Using FIPS 140-2 failure threshold: 10
[INFO] 2025-11-03 10:28:02 - Running rngtest with 20000032 bytes of entropy from /dev/hwrng...
[INFO] 2025-11-03 10:28:35 - rngtest: FIPS 140-2 failures = 1
[PASS] 2025-11-03 10:28:35 - qcom_hwrng : Test Passed (1 failures)
[INFO] 2025-11-03 10:28:35 - -------------------Completed qcom_hwrng Testcase----------------------------
[PASS] 2025-11-03 10:28:35 - qcom_hwrng passed

[INFO] 2025-11-03 10:28:35 - ========== Test Summary ==========
PASSED:
qcom_hwrng

FAILED:
None

SKIPPED:
None
[INFO] 2025-11-03 10:28:35 - ==================================
```
4. Results will be available in the `/<user-defined-location>/Runner/suites/Kernel/Baseport/qcom_hwrng/` directory.

## Notes

- The script sets qcom_hwrng as the primary hwrng.
- It validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality.
- If any critical tool is missing, the script exits with an error message.
116 changes: 116 additions & 0 deletions Runner/suites/Kernel/Baseport/qcom_hwrng/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/bin/sh

# Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
# SPDX-License-Identifier: BSD-3-Clause-Clear

# Robustly find and source init_env
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
INIT_ENV=""
SEARCH="$SCRIPT_DIR"
while [ "$SEARCH" != "/" ]; do
if [ -f "$SEARCH/init_env" ]; then
INIT_ENV="$SEARCH/init_env"
break
fi
SEARCH=$(dirname "$SEARCH")
done
res_file="./qcom_hwrng.res"

if [ -z "$INIT_ENV" ]; then
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
echo "qcom_hwrng SKIP" > "$res_file"
exit 0
fi
if [ -z "$__INIT_ENV_LOADED" ]; then
# shellcheck disable=SC1090
. "$INIT_ENV"
fi
# Always source functestlib.sh, using $TOOLS exported by init_env
# shellcheck disable=SC1090,SC1091
. "$TOOLS/functestlib.sh"

TESTNAME="qcom_hwrng"

if [ "$(id -u)" -ne 0 ]; then
log_info "$TESTNAME : Root privileges required"
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi

test_path=$(find_test_case_by_name "$TESTNAME")
if [ -z "$test_path" ] || ! cd "$test_path"; then
log_info "$TESTNAME : Test path not found or cd failed"
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi

log_info "-----------------------------------------------------------------------------------------"
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
log_info "=== Test Initialization ==="

log_info "Checking if dependency binary is available"
if ! check_dependencies rngtest dd; then
log_info "$TESTNAME : Required dependencies not met"
echo "$TESTNAME SKIP" > "$res_file"
exit 0
fi

# Set the hardware RNG source to Qualcomm's RNG
RNG_PATH="/sys/class/misc/hw_random/rng_current"
if [ ! -e "$RNG_PATH" ]; then
log_fail "$TESTNAME : RNG path $RNG_PATH does not exist"
echo "$TESTNAME FAIL" > "$res_file"
exit 1
fi

PREV_RNG=$(cat "$RNG_PATH")
echo qcom_hwrng > "$RNG_PATH"
current_rng=$(cat "$RNG_PATH")
if [ "$current_rng" != "qcom_hwrng" ]; then
log_fail "$TESTNAME : Failed to set qcom_hwrng as the current RNG source"
echo "$TESTNAME FAIL" > "$res_file"
echo "$PREV_RNG" > "$RNG_PATH"
exit 1
else
log_info "qcom_hwrng successfully set as the current RNG source."
fi
RNG_SOURCE="/dev/hwrng"
if [ ! -e "$RNG_SOURCE" ]; then
log_info "$TESTNAME : $RNG_SOURCE not available"
echo "$TESTNAME SKIP" > "$res_file"
echo "$PREV_RNG" > "$RNG_PATH"
exit 0
fi

TMP_OUT="./qcom_hwrng_output.txt"
ENTROPY_B=20000032
FAILURE_THRESHOLD=10

log_info "Using FIPS 140-2 failure threshold: $FAILURE_THRESHOLD"
log_info "Running rngtest with $ENTROPY_B bytes of entropy from $RNG_SOURCE..."

dd if="$RNG_SOURCE" bs=1 count="$ENTROPY_B" status=none 2>/dev/null > temp_entropy.bin
rngtest -c 1000 < temp_entropy.bin > "$TMP_OUT" 2>&1
rm -f temp_entropy.bin

failures=$(awk '/^rngtest: FIPS 140-2 failures:/ {print $NF}' "$TMP_OUT" | head -n1)
rm -f "$TMP_OUT"

if [ -z "$failures" ] || ! echo "$failures" | grep -Eq '^[0-9]+$'; then
log_fail "rngtest did not return a valid integer for failures; got: '$failures'"
echo "$TESTNAME FAIL" > "$res_file"
echo "$PREV_RNG" > "$RNG_PATH"
exit 1
fi
log_info "rngtest: FIPS 140-2 failures = $failures"
if [ "$failures" -lt "$FAILURE_THRESHOLD" ]; then
log_pass "$TESTNAME : Test Passed ($failures failures)"
echo "$TESTNAME PASS" > "$res_file"
else
log_fail "$TESTNAME : Test Failed ($failures failures)"
echo "$TESTNAME FAIL" > "$res_file"
fi

echo "$PREV_RNG" > "$RNG_PATH"
log_info "-------------------Completed $TESTNAME Testcase----------------------------"
exit 0
Loading