Skip to content

Commit 7eb2a59

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 5baf561 commit 7eb2a59

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
│ │ ├── FunctionalArea/
28+
│ │ │ ├── baseport/
29+
│ │ │ │ ├── qcom_hwrng/
30+
│ │ │ │ │ ├── run.sh
31+
```
32+
## Usage
33+
34+
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.
35+
36+
2. Verify Transfer: Ensure that the repo have been successfully copied to the /var directory on the target device.
37+
38+
3. Run Scripts: Navigate to the /var directory on the target device and execute the scripts as needed.
39+
40+
---
41+
Quick Example
42+
```
43+
git clone <this-repo>
44+
cd <this-repo>
45+
scp -r common Runner user@target_device_ip:/<user-defined-location>
46+
ssh user@target_device_ip
47+
cd /<user-defined-location>/Runner && ./run-test.sh qcom_hwrng
48+
49+
Sample output:
50+
sh-5.2# ./run-test.sh qcom_hwrng
51+
[Executing test case: qcom_hwrng] 1970-01-01 00:17:53 -
52+
[INFO] 1970-01-01 00:17:53 - -----------------------------------------------------------------------------------------
53+
[INFO] 1970-01-01 00:17:53 - -------------------Starting qcom_hwrng Testcase----------------------------
54+
[INFO] 1970-01-01 00:17:53 - === Test Initialization ===
55+
[INFO] 1970-01-01 00:17:53 - Checking if dependency binary is available
56+
[INFO] 1970-01-01 00:17:53 - qcom_hwrng successfully set as the current RNG source.
57+
[INFO] 1970-01-01 00:17:53 - Running rngtest with 20000032 bytes of entropy from /dev/random...
58+
rngtest 6.15
59+
Copyright (c) 2004 by Henrique de Moraes Holschuh
60+
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
61+
62+
rngtest: starting FIPS tests...
63+
rngtest: bits received from input: 20000032
64+
rngtest: FIPS 140-2 successes: 999
65+
rngtest: FIPS 140-2 failures: 1
66+
rngtest: FIPS 140-2(2001-10-10) Monobit: 0
67+
rngtest: FIPS 140-2(2001-10-10) Poker: 0
68+
rngtest: FIPS 140-2(2001-10-10) Runs: 0
69+
rngtest: FIPS 140-2(2001-10-10) Long run: 1
70+
rngtest: FIPS 140-2(2001-10-10) Continuous run: 0
71+
rngtest: input channel speed: (min=3.682; avg=5.473; max=7.173)Mibits/s
72+
rngtest: FIPS tests speed: (min=84.771; avg=138.269; max=155.069)Mibits/s
73+
rngtest: Program run time: 3623356 microseconds
74+
[INFO] 1970-01-01 00:17:56 - rngtest: FIPS 140-2 failures = 1
75+
[PASS] 1970-01-01 00:17:56 - qcom_hwrng : Test Passed (1 failures)
76+
[PASS] 1970-01-01 00:17:56 - qcom_hwrng passed
77+
78+
[INFO] 1970-01-01 00:17:57 - ========== Test Summary ==========
79+
PASSED:
80+
qcom_hwrng
81+
82+
FAILED:
83+
None
84+
[INFO] 1970-01-01 00:17:57 - ==================================
85+
```
86+
4. Results will be available in the `/<user-defined-location>/Runner/suites/Kernel/FunctionalArea/baseport/qcom_hwrng/` directory.
87+
88+
## Notes
89+
90+
- The script sets qcom_hwrng as the primary hwrng.
91+
- It validates Qualcomm Hardware Random Number Generator (HWRNG) basic functionality.
92+
- If any critical tool is missing, the script exits with an error message.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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

Comments
 (0)