From 65bed7c6f418827aade0d3edecf178d251a3c377 Mon Sep 17 00:00:00 2001 From: Andrea Grandi Date: Thu, 10 Nov 2022 00:27:23 +0000 Subject: test: Add rng_benchmark test BRANCH=none BUG=b:246836252 TEST=./test/run_device_tests.py --board bloonchipper -t rng_benchmark TEST=./test/run_device_tests.py --board dartmonkey -t rng_benchmark Signed-off-by: Andrea Grandi Change-Id: Ib1a2a4b0fc8e3d55022e94727417c38e5e94359b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/4019773 Reviewed-by: Bobby Casey Code-Coverage: Zoss Reviewed-by: Tom Hughes --- board/hatch_fp/build.mk | 1 + board/nocturne_fp/build.mk | 1 + board/nucleo-dartmonkey/build.mk | 1 + board/nucleo-f412zg/build.mk | 1 + board/nucleo-h743zi/build.mk | 1 + test/build.mk | 1 + test/rng_benchmark.cc | 81 ++++++++++++++++++++++++++++++++++++++++ test/rng_benchmark.tasklist | 9 +++++ test/run_device_tests.py | 1 + 9 files changed, 97 insertions(+) create mode 100644 test/rng_benchmark.cc create mode 100644 test/rng_benchmark.tasklist diff --git a/board/hatch_fp/build.mk b/board/hatch_fp/build.mk index 541ac5f198..894c907ab9 100644 --- a/board/hatch_fp/build.mk +++ b/board/hatch_fp/build.mk @@ -49,6 +49,7 @@ test-list-y=\ pingpong \ printf \ queue \ + rng_benchmark \ rollback \ rollback_entropy \ rsa3 \ diff --git a/board/nocturne_fp/build.mk b/board/nocturne_fp/build.mk index 1d8e9174f7..2bc59c1f13 100644 --- a/board/nocturne_fp/build.mk +++ b/board/nocturne_fp/build.mk @@ -49,6 +49,7 @@ test-list-y=\ pingpong \ printf \ queue \ + rng_benchmark \ rollback \ rollback_entropy \ rsa3 \ diff --git a/board/nucleo-dartmonkey/build.mk b/board/nucleo-dartmonkey/build.mk index 809db95260..b544490fc9 100644 --- a/board/nucleo-dartmonkey/build.mk +++ b/board/nucleo-dartmonkey/build.mk @@ -28,6 +28,7 @@ test-list-y=\ pingpong \ printf \ queue \ + rng_benchmark \ rollback \ rollback_entropy \ rsa3 \ diff --git a/board/nucleo-f412zg/build.mk b/board/nucleo-f412zg/build.mk index 37402b7e68..d923a7a40f 100644 --- a/board/nucleo-f412zg/build.mk +++ b/board/nucleo-f412zg/build.mk @@ -25,6 +25,7 @@ test-list-y=\ pingpong \ printf \ queue \ + rng_benchmark \ rollback \ rollback_entropy \ rsa3 \ diff --git a/board/nucleo-h743zi/build.mk b/board/nucleo-h743zi/build.mk index 3a59c5d47d..ad04a68918 100644 --- a/board/nucleo-h743zi/build.mk +++ b/board/nucleo-h743zi/build.mk @@ -25,6 +25,7 @@ test-list-y=\ pingpong \ printf \ queue \ + rng_benchmark \ rollback \ rollback_entropy \ rsa3 \ diff --git a/test/build.mk b/test/build.mk index 384ad60a59..30908d309e 100644 --- a/test/build.mk +++ b/test/build.mk @@ -237,6 +237,7 @@ power_button-y=power_button.o powerdemo-y=powerdemo.o printf-y=printf.o queue-y=queue.o +rng_benchmark-y=rng_benchmark.o rollback-y=rollback.o rollback_entropy-y=rollback_entropy.o rollback_secret-y=rollback_secret.o diff --git a/test/rng_benchmark.cc b/test/rng_benchmark.cc new file mode 100644 index 0000000000..2e47b8ea27 --- /dev/null +++ b/test/rng_benchmark.cc @@ -0,0 +1,81 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Measure performance of the hardware True Random Number Generator (TRNG) + * compared to the std::rand(). + */ + +#include "benchmark.h" + +#include "console.h" +#include +#include +#include + +extern "C" { +#include "test_util.h" +#include "trng.h" +} + +test_static int test_rng() +{ + constexpr int num_iterations = 100; + Benchmark benchmark({ .num_iterations = num_iterations }); + std::array trng_out; + std::array rand_out; + + // Try the hardware true random number generator + trng_out.fill(0); + + trng_init(); + auto result = benchmark.run("trng", [&trng_out]() { + static int i = 0; + trng_out[i++] = trng_rand(); + }); + trng_exit(); + + TEST_ASSERT(result.has_value()); + for (int i = 0; i < trng_out.size() - 1; ++i) { + TEST_NE(trng_out[i], trng_out[i + 1], "%d"); + cflush(); + } + + // Repeat the test by turning the TNRG on and off at each iteration + trng_out.fill(0); + result = benchmark.run("trng_on_off", [&trng_out]() { + trng_init(); + static int i = 0; + trng_out[i++] = trng_rand(); + trng_exit(); + }); + + TEST_ASSERT(result.has_value()); + for (int i = 0; i < trng_out.size() - 1; ++i) { + TEST_NE(trng_out[i], trng_out[i + 1], "%d"); + cflush(); + } + + // Repeat the test using std::rand() for comparison + rand_out.fill(0); + result = benchmark.run("std::rand", [&rand_out]() { + static int i = 0; + rand_out[i++] = std::rand(); + }); + + TEST_ASSERT(result.has_value()); + for (int i = 0; i < rand_out.size() - 1; ++i) { + TEST_NE(rand_out[i], rand_out[i + 1], "%d"); + cflush(); + } + + benchmark.print_results(); + return EC_SUCCESS; +} + +void run_test(int argc, const char **argv) +{ + test_reset(); + RUN_TEST(test_rng); + test_print_result(); +} \ No newline at end of file diff --git a/test/rng_benchmark.tasklist b/test/rng_benchmark.tasklist new file mode 100644 index 0000000000..19615b9ea8 --- /dev/null +++ b/test/rng_benchmark.tasklist @@ -0,0 +1,9 @@ +/* Copyright 2022 The ChromiumOS Authors + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * See CONFIG_TASK_LIST in config.h for details. + */ +#define CONFIG_TEST_TASK_LIST \ No newline at end of file diff --git a/test/run_device_tests.py b/test/run_device_tests.py index 0b5a291b6c..fcf9a99651 100755 --- a/test/run_device_tests.py +++ b/test/run_device_tests.py @@ -257,6 +257,7 @@ class AllTests: TestConfig(test_name="pingpong"), TestConfig(test_name="printf"), TestConfig(test_name="queue"), + TestConfig(test_name="rng_benchmark"), TestConfig( config_name="rollback_region0", test_name="rollback", -- cgit v1.2.1