summaryrefslogtreecommitdiff
path: root/test/tpm_test/trng_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/tpm_test/trng_test.py')
-rw-r--r--test/tpm_test/trng_test.py45
1 files changed, 24 insertions, 21 deletions
diff --git a/test/tpm_test/trng_test.py b/test/tpm_test/trng_test.py
index aac2803076..ddfc659339 100644
--- a/test/tpm_test/trng_test.py
+++ b/test/tpm_test/trng_test.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
# Copyright 2019 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.
@@ -16,15 +16,18 @@ TRNG_SAMPLE_SIZE = 1000 # minimal recommended by NIST is 1000 bytes per sample
TRNG_SAMPLE_COUNT = 1000 # NIST require at least 1000000 of 8-bit samples
def get_random_command(size):
- return struct.pack(TRNG_TEST_FMT, size)
+ """Encode get_random command"""
+ return struct.pack(TRNG_TEST_FMT, size)
def get_random_command_rsp(size):
- return struct.pack(TRNG_TEST_RSP_FMT, 0x8001,
- struct.calcsize(TRNG_TEST_RSP_FMT) + size, 0, TRNG_TEST_CC)
+ """Create expected response to get_random"""
+ return struct.pack(TRNG_TEST_RSP_FMT, 0x8001,
+ struct.calcsize(TRNG_TEST_RSP_FMT) + size,
+ 0, TRNG_TEST_CC)
def trng_test(tpm):
- """Download entropy samples from TRNG
+ """Download entropy samples from TRNG
Command structure, shared out of band with the test running on the target:
@@ -32,19 +35,19 @@ def trng_test(tpm):
===================================================================
text_len | 2 | size of the text to process, big endian
- Args:
- tpm: a tpm object used to communicate with the device
-
- Raises:
- subcmd.TpmTestError: on unexpected target responses
- """
- with open('/tmp/trng_output', 'wb') as f:
- for x in range(0, TRNG_SAMPLE_COUNT):
- wrapped_response = tpm.command(tpm.wrap_ext_command(TRNG_TEST_CC,
- get_random_command(TRNG_SAMPLE_SIZE)))
- if wrapped_response[:12] != get_random_command_rsp(TRNG_SAMPLE_SIZE):
- raise subcmd.TpmTestError("Unexpected response to '%s': %s" %
- ("trng", utils.hex_dump(wrapped_response)))
- f.write(wrapped_response[12:])
- print('%s %d%%\r' %( utils.cursor_back(), (x/10)), end=""),
- print('%sSUCCESS: %s' % (utils.cursor_back(), 'trng'))
+ Args:
+ tpm: a tpm object used to communicate with the device
+
+ Raises:
+ subcmd.TpmTestError: on unexpected target responses
+ """
+ with open('/tmp/trng_output', 'wb') as out_file:
+ for block in range(0, TRNG_SAMPLE_COUNT):
+ response = tpm.command(tpm.wrap_ext_command(TRNG_TEST_CC,
+ get_random_command(TRNG_SAMPLE_SIZE)))
+ if response[:12] != get_random_command_rsp(TRNG_SAMPLE_SIZE):
+ raise subcmd.TpmTestError("Unexpected response to \'%s\': %s" %
+ ('trng', utils.hex_dump(response)))
+ out_file.write(response[12:])
+ print('%s %d%%\r' % (utils.cursor_back(), (block/10)), end='')
+ print('%sSUCCESS: %s' % (utils.cursor_back(), 'trng'))