Skip to content

Commit c8edda8

Browse files
committed
Add: Qualcomm HWRNG test script with documentation
- Added initial test script to validate Qualcomm HWRNG functionality - Refined script and documentation based on review feedback Signed-off-by: Naveenkumar Suresh <[email protected]>
1 parent 296abc9 commit c8edda8

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
# Qualcomm Hardware Random Number Generator (QRNG) Script
5+
# Overview
6+
7+
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.
8+
9+
## Features
10+
11+
- Driver Validation: Confirms the presence and correct configuration of the qcom_hwrng kernel driver.
12+
- Dependency Check: Verifies the availability of required tools like rngtest before execution.
13+
- Automated Result Logging: Outputs test results to a .res file for automated result collection.
14+
- Remote Execution Ready: Supports remote deployment and execution via scp and ssh.
15+
16+
## Prerequisites
17+
18+
Ensure the following components are present in the target:
19+
20+
- `rngtest` (Binary Available in /usr/bin) - this test app can be compiled from https://github.com/cernekee/rng-tools/
21+
22+
## Directory Structure
23+
```
24+
Runner/
25+
├── suites/
26+
│ ├── Kernel/
27+
│ │ │ ├── Baseport/
28+
│ │ │ │ ├── qcom_hwrng/
29+
│ │ │ │ │ ├── run.sh
30+
```
31+
## Usage
32+
33+
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.
34+
35+
2. Verify Transfer: Ensure that the repo have been successfully copied to the /var directory on the target device.
36+
37+
3. Run Scripts: Navigate to the /var directory on the target device and execute the scripts as needed.
38+
39+
---
40+
Quick Example
41+
```
42+
git clone <this-repo>
43+
cd <this-repo>
44+
scp -r common Runner user@target_device_ip:/<user-defined-location>
45+
ssh user@target_device_ip
46+
cd /<user-defined-location>/Runner && ./run-test.sh qcom_hwrng
47+
48+
Sample output:
49+
sh-5.2# ./run-test.sh qcom_hwrng
50+
[Executing test case: qcom_hwrng] 2025-11-03 10:28:02 -
51+
[INFO] 2025-11-03 10:28:02 - -----------------------------------------------------------------------------------------
52+
[INFO] 2025-11-03 10:28:02 - -------------------Starting qcom_hwrng Testcase----------------------------
53+
[INFO] 2025-11-03 10:28:02 - === Test Initialization ===
54+
[INFO] 2025-11-03 10:28:02 - Checking if dependency binary is available
55+
[INFO] 2025-11-03 10:28:02 - qcom_hwrng successfully set as the current RNG source.
56+
[INFO] 2025-11-03 10:28:02 - Using FIPS 140-2 failure threshold: 10
57+
[INFO] 2025-11-03 10:28:02 - Running rngtest with 20000032 bytes of entropy from /dev/hwrng...
58+
[INFO] 2025-11-03 10:28:35 - rngtest: FIPS 140-2 failures = 1
59+
[PASS] 2025-11-03 10:28:35 - qcom_hwrng : Test Passed (1 failures)
60+
[INFO] 2025-11-03 10:28:35 - -------------------Completed qcom_hwrng Testcase----------------------------
61+
[PASS] 2025-11-03 10:28:35 - qcom_hwrng passed
62+
63+
[INFO] 2025-11-03 10:28:35 - ========== Test Summary ==========
64+
PASSED:
65+
qcom_hwrng
66+
67+
FAILED:
68+
None
69+
70+
SKIPPED:
71+
None
72+
[INFO] 2025-11-03 10:28:35 - ==================================
73+
```
74+
4. Results will be available in the `/<user-defined-location>/Runner/suites/Kernel/Baseport/qcom_hwrng/` directory.
75+
76+
## Notes
77+
78+
- The script sets qcom_hwrng as the primary hwrng.
79+
- It validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality.
80+
- If any critical tool is missing, the script exits with an error message.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
res_file="./qcom_hwrng.res"
18+
19+
if [ -z "$INIT_ENV" ]; then
20+
echo "[ERROR] Could not find init_env (starting at $SCRIPT_DIR)" >&2
21+
echo "qcom_hwrng SKIP" > "$res_file"
22+
exit 0
23+
fi
24+
if [ -z "$__INIT_ENV_LOADED" ]; then
25+
# shellcheck disable=SC1090
26+
. "$INIT_ENV"
27+
fi
28+
# Always source functestlib.sh, using $TOOLS exported by init_env
29+
# shellcheck disable=SC1090,SC1091
30+
. "$TOOLS/functestlib.sh"
31+
32+
TESTNAME="qcom_hwrng"
33+
34+
if [ "$(id -u)" -ne 0 ]; then
35+
log_info "$TESTNAME : Root privileges required"
36+
echo "$TESTNAME SKIP" > "$res_file"
37+
exit 0
38+
fi
39+
40+
test_path=$(find_test_case_by_name "$TESTNAME")
41+
if [ -z "$test_path" ] || ! cd "$test_path"; then
42+
log_info "$TESTNAME : Test path not found or cd failed"
43+
echo "$TESTNAME SKIP" > "$res_file"
44+
exit 0
45+
fi
46+
47+
log_info "-----------------------------------------------------------------------------------------"
48+
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
49+
log_info "=== Test Initialization ==="
50+
51+
log_info "Checking if dependency binary is available"
52+
if ! check_dependencies rngtest dd; then
53+
log_info "$TESTNAME : Required dependencies not met"
54+
echo "$TESTNAME SKIP" > "$res_file"
55+
exit 0
56+
fi
57+
58+
# Set the hardware RNG source to Qualcomm's RNG
59+
RNG_PATH="/sys/class/misc/hw_random/rng_current"
60+
if [ ! -e "$RNG_PATH" ]; then
61+
log_fail "$TESTNAME : RNG path $RNG_PATH does not exist"
62+
echo "$TESTNAME FAIL" > "$res_file"
63+
exit 1
64+
fi
65+
66+
PREV_RNG=$(cat "$RNG_PATH")
67+
echo qcom_hwrng > "$RNG_PATH"
68+
current_rng=$(cat "$RNG_PATH")
69+
if [ "$current_rng" != "qcom_hwrng" ]; then
70+
log_fail "$TESTNAME : Failed to set qcom_hwrng as the current RNG source"
71+
echo "$TESTNAME FAIL" > "$res_file"
72+
echo "$PREV_RNG" > "$RNG_PATH"
73+
exit 1
74+
else
75+
log_info "qcom_hwrng successfully set as the current RNG source."
76+
fi
77+
RNG_SOURCE="/dev/hwrng"
78+
if [ ! -e "$RNG_SOURCE" ]; then
79+
log_info "$TESTNAME : $RNG_SOURCE not available"
80+
echo "$TESTNAME SKIP" > "$res_file"
81+
echo "$PREV_RNG" > "$RNG_PATH"
82+
exit 0
83+
fi
84+
85+
TMP_OUT="./qcom_hwrng_output.txt"
86+
ENTROPY_B=20000032
87+
FAILURE_THRESHOLD=10
88+
89+
log_info "Using FIPS 140-2 failure threshold: $FAILURE_THRESHOLD"
90+
log_info "Running rngtest with $ENTROPY_B bytes of entropy from $RNG_SOURCE..."
91+
92+
dd if="$RNG_SOURCE" bs=1 count="$ENTROPY_B" status=none 2>/dev/null > temp_entropy.bin
93+
rngtest -c 1000 < temp_entropy.bin > "$TMP_OUT" 2>&1
94+
rm -f temp_entropy.bin
95+
96+
failures=$(awk '/^rngtest: FIPS 140-2 failures:/ {print $NF}' "$TMP_OUT" | head -n1)
97+
rm -f "$TMP_OUT"
98+
99+
if [ -z "$failures" ] || ! echo "$failures" | grep -Eq '^[0-9]+$'; then
100+
log_fail "rngtest did not return a valid integer for failures; got: '$failures'"
101+
echo "$TESTNAME FAIL" > "$res_file"
102+
echo "$PREV_RNG" > "$RNG_PATH"
103+
exit 1
104+
fi
105+
log_info "rngtest: FIPS 140-2 failures = $failures"
106+
if [ "$failures" -lt "$FAILURE_THRESHOLD" ]; then
107+
log_pass "$TESTNAME : Test Passed ($failures failures)"
108+
echo "$TESTNAME PASS" > "$res_file"
109+
else
110+
log_fail "$TESTNAME : Test Failed ($failures failures)"
111+
echo "$TESTNAME FAIL" > "$res_file"
112+
fi
113+
114+
echo "$PREV_RNG" > "$RNG_PATH"
115+
log_info "-------------------Completed $TESTNAME Testcase----------------------------"
116+
exit 0

0 commit comments

Comments
 (0)