summaryrefslogtreecommitdiff
path: root/test/entropy.c
diff options
context:
space:
mode:
authorNicolas Boichat <drinkcat@google.com>2017-05-31 12:12:12 +0800
committerchrome-bot <chrome-bot@chromium.org>2017-06-06 08:52:16 -0700
commit39db721f9ace13cc210278c88c3f364b5d67b5b8 (patch)
tree6a0ca910942bf8c41560e91b73a8a58d04bc146c /test/entropy.c
parent2f436f4cc9bd026ed83511401ab17d68f5f8e5a3 (diff)
downloadchrome-ec-39db721f9ace13cc210278c88c3f364b5d67b5b8.tar.gz
hammer: Add board_get_entropy function (and test)
This function will be used to generate some entropy using the Clock Recovery System. BRANCH=none BUG=b:38486828 TEST=make BOARD=hammer -j tests ./util/flash_ec --board=hammer --image=build/hammer/test-entropy.bin EC console: runtest TEST=Test fails when no USB connection is active TEST=Test passes when USB connection is active TEST=Pasting the values into: tr ';' '\n' | awk 'BEGIN { e = 0; tot=16384.0 } { p = $1/tot; if (p > 0) { e -= p*log(p)/log(2) } } END { print e }' shows an entropy > 4 bits per sample. Change-Id: I2363c7bce42c72c33ef0bf3f099d709ee9c13d13 Reviewed-on: https://chromium-review.googlesource.com/518608 Commit-Ready: Nicolas Boichat <drinkcat@chromium.org> Tested-by: Nicolas Boichat <drinkcat@chromium.org> Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
Diffstat (limited to 'test/entropy.c')
-rw-r--r--test/entropy.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/test/entropy.c b/test/entropy.c
new file mode 100644
index 0000000000..f01a3dd0a0
--- /dev/null
+++ b/test/entropy.c
@@ -0,0 +1,60 @@
+/* Copyright 2017 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Tests entropy source.
+ */
+
+#include "console.h"
+#include "common.h"
+#include "rollback.h"
+#include "test_util.h"
+#include "timer.h"
+#include "util.h"
+#include "watchdog.h"
+
+static int buckets[256];
+
+void run_test(void)
+{
+ const int loopcount = 512;
+
+ uint8_t buffer[32];
+ timestamp_t t0, t1;
+ int i, j;
+
+ memset(buckets, 0, sizeof(buckets));
+
+ for (i = 0; i < loopcount; i++) {
+ t0 = get_time();
+ if (!board_get_entropy(buffer, sizeof(buffer))) {
+ ccprintf("Cannot get entropy\n");
+ test_fail();
+ return;
+ }
+ t1 = get_time();
+ if (i == 0)
+ ccprintf("Got %d bytes in %ld us\n",
+ sizeof(buffer), t1.val - t0.val);
+
+ for (j = 0; j < sizeof(buffer); j++)
+ buckets[buffer[j]]++;
+
+ watchdog_reload();
+ }
+
+ ccprintf("Total count: %d\n", loopcount * sizeof(buffer));
+ ccprintf("Buckets: ");
+ for (j = 0; j < 256; j++) {
+ ccprintf("%d;", buckets[j]);
+ cflush();
+ }
+ ccprintf("\n");
+ /*
+ * From the data above, entropy can be obtained with this command:
+ * tr ';' '\n' | awk 'BEGIN { e = 0; tot=16384.0 }
+ { p = $1/tot; if (p > 0) { e -= p*log(p)/log(2) } }
+ END { print e }'
+ */
+ test_pass();
+}