diff options
Diffstat (limited to 'test/tpm_test')
-rwxr-xr-x | test/tpm_test/nist_entropy.sh | 38 | ||||
-rwxr-xr-x | test/tpm_test/tpmtest.py | 29 | ||||
-rw-r--r-- | test/tpm_test/trng_test.py | 118 |
3 files changed, 141 insertions, 44 deletions
diff --git a/test/tpm_test/nist_entropy.sh b/test/tpm_test/nist_entropy.sh index f69c5652cd..5344a49890 100755 --- a/test/tpm_test/nist_entropy.sh +++ b/test/tpm_test/nist_entropy.sh @@ -3,19 +3,29 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -# NIST toolset needs sudo emerge dev-libs/libdivsufsort -rm -rf /tmp/ea -git clone --depth 1 https://github.com/usnistgov/SP800-90B_EntropyAssessment.git /tmp/ea/ -make -C /tmp/ea/cpp/ non_iid -make -C /tmp/ea/cpp/ restart -TRNG_OUT=/tmp/trng_output -rm -f $TRNG_OUT -./tpmtest.py -t -if [ ! -f "$TRNG_OUT" ]; then - echo "$TRNG_OUT does not exist" +# NIST toolset needs sudo emerge dev-libs/libdivsufsort and bz2 +set -e +TMP_PATH="/tmp/ea" +NIST_URL="https://github.com/usnistgov/SP800-90B_EntropyAssessment.git" +TRNG_OUT="${TMP_PATH}/trng_output" +EA_LOG="ea_non_iid.log" +rm -rf "${TMP_PATH}" +git clone --depth 1 "${NIST_URL}" "${TMP_PATH}" +# build entropy assessment tool using mode for non independent and identically +# distributed data, as for H1 TRNG we can't justify the oppposite +make -j -C "${TMP_PATH}/cpp/" non_iid restart +rm -f "${TRNG_OUT}" +# -t0 requests raw random samples from TRNG +./tpmtest.py -t0 -o "${TRNG_OUT}" +if [[ ! -f "${TRNG_OUT}" ]]; then + echo "${TRNG_OUT} does not exist" exit 1 fi -/tmp/ea/cpp/ea_non_iid -a $TRNG_OUT | tee ea_non_iid.log -entropy=`grep min ea_non_iid.log | awk '{ print $5 }'` -echo "Minimal entropy" $entropy -/tmp/ea/cpp/ea_restart $TRNG_OUT $entropy | tee -a ea_non_iid.log +rm -f "${EA_LOG}" +"${TMP_PATH}/cpp/ea_non_iid" -a "${TRNG_OUT}" | tee "${EA_LOG}" +entropy="$(awk '/min/ {print $5}' "${EA_LOG}")" +if [[ -z "${entropy}" ]]; then + entropy="$(awk '/H_original/ {print $2}' "${EA_LOG}")" +fi +echo "Minimal entropy ${entropy}" +"${TMP_PATH}/cpp/ea_restart" "${TRNG_OUT}" "${entropy}" | tee -a "${EA_LOG}" diff --git a/test/tpm_test/tpmtest.py b/test/tpm_test/tpmtest.py index 0ec33ce8f1..04567d67cb 100755 --- a/test/tpm_test/tpmtest.py +++ b/test/tpm_test/tpmtest.py @@ -148,34 +148,47 @@ class TPM: def usage(): """Print usage information""" - print('Syntax: tpmtest.py [-d | -t | -h ]\n' - ' -d - prints additional debug information during tests\n' - ' -t - dump raw output from TRNG to /tmp/trng_output\n' - ' -h - this help\n') + print('Syntax: tpmtest.py [-d] | [-t source [-o file] [-s bits] ]| -h ]\n' + ' -d - prints additional debug information during tests\n' + ' -t source - only dump raw output from TRNG. source values:\n' + ' 0 - raw TRNG' + ' [-o file] - set output file, default /tmp/trng_output\n' + ' [-s bits] - TRNG sample size in bit, default = 1\n' + ' -h - this help\n') def main(): """Run TPM tests""" try: - opts, _ = getopt.getopt(sys.argv[1:], 'dth', 'help') + opts, _ = getopt.getopt(sys.argv[1:], 'dt:hs:o:', 'help') except getopt.GetoptError as err: print(str(err)) usage() sys.exit(2) debug_needed = False trng_only = False - for option, _ in opts: + trng_output = '/tmp/trng_output' + trng_sample_bits = 1 + trng_mode = 0 + + for option, arg in opts: if option == '-d': debug_needed = True elif option == '-t': trng_only = True + trng_mode = int(arg) + elif option == '-o': + trng_output = arg + elif option == '-s': + trng_sample_bits = int(arg) elif option in ('-h', '--help'): usage() sys.exit(0) try: tpm_object = TPM(debug_mode=debug_needed) if trng_only: - trng_test.trng_test(tpm_object) - sys.exit(1) + trng_test.trng_test(tpm_object, trng_output, + trng_mode, trng_sample_bits) + sys.exit(0) crypto_test.crypto_tests(tpm_object, os.path.join(ROOT_DIR, 'crypto_test.xml')) drbg_test.drbg_test(tpm_object) diff --git a/test/tpm_test/trng_test.py b/test/tpm_test/trng_test.py index c4d9395e80..60faa32e99 100644 --- a/test/tpm_test/trng_test.py +++ b/test/tpm_test/trng_test.py @@ -3,21 +3,27 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. """Tests for trng.""" -from __future__ import print_function -import struct +from math import gcd +import struct import subcmd import utils -TRNG_TEST_FMT = '>H' +TRNG_TEST_FMT = '>HB' TRNG_TEST_RSP_FMT = '>H2IH' TRNG_TEST_CC = 0x33 + 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 +TRNG_SAMPLE_COUNT = 1000000 # NIST require at least 1000000 of 8-bit samples -def get_random_command(size): +# Command structure, shared out of band with the test running on the target: +# field | size | note +# =================================================================== +# text_len | 2 | number of bytes to read, big endian +# type | 1 | 0 = TRNG, other values reserved for extensions +def get_random_command(size, trng_op): """Encode get_random command""" - return struct.pack(TRNG_TEST_FMT, size) + return struct.pack(TRNG_TEST_FMT, size, trng_op) def get_random_command_rsp(size): """Create expected response to get_random""" @@ -25,29 +31,97 @@ def get_random_command_rsp(size): struct.calcsize(TRNG_TEST_RSP_FMT) + size, 0, TRNG_TEST_CC) +def to_bitstring(s, n=1): + """Split bytes into individual samples -def trng_test(tpm): - """Download entropy samples from TRNG - - Command structure, shared out of band with the test running on the target: + convert input packed byte array to n-bits in a byte representation + used by NIST tests. It's designed to recover sequence of samples + as it comes from TRNG, including the fact that rand_bytes() reverse + byte order in every 32-bit chunk. + """ + out = b'' + val_left = 0 + bits_left = 0 + while s: + val = (struct.unpack('>I', s[0:4].rjust(4, b'\0'))[0] << bits_left) +\ + val_left + bits_left += 8 * len(s[0:4]) + s = s[4:] + while bits_left >= n: + out += struct.pack('B', val & ((1 << n) - 1)) + val >>= n + bits_left -= n + val_left = val + return out - field | size | note - =================================================================== - text_len | 2 | size of the text to process, big endian +def trng_test(tpm, trng_output, trng_mode, tsb=1): + """Download entropy samples from TRNG Args: tpm: a tpm object used to communicate with the device + trng_output: file name + trng_mode: source of randomness [0 - TRNG] + tsb: number of bits in each TRNG sample, should be in sync with + TRNG configuration. Default is 1 bit per sample. Raises: subcmd.TpmTestError: on unexpected target responses """ - with open('/tmp/trng_output', 'wb') as out_file: - for block in range(0, TRNG_SAMPLE_COUNT): + + if trng_mode not in [0]: + raise subcmd.TpmTestError('Unknown random source: %d' % trng_mode) + + # minimal recommended by NIST is 1000 samples per block + # TRNG_SAMPLE_BITS is internal setting for TRNG which is important for + # entropy analysis. TRNG internally gets a 16 bit sample (measurement of + # time to collapse ring oscillator). Then some slice of these bits + # [0:TRNG_SAMPLE_BITS] is added (packed) to TRNG FIFO which has internal + # size of 2048 bits. Reading from TRNG always return 32 bits from FIFO. + # To extract actual samples from packed 32-bit reading, need to reverse + # and split read 32-bit into individual samples, each size TRNG_SAMPLE_BITS + # bits. As it's only possible to read 32 bits at once and TRNG_SAMPLE_BITS + # can be anything from 1 to 16, including non-power of 2, need to ensure + # readings to result in non-fractional number of samples. At the same time + # NIST requires minimum 1000 samples at once, and we also want to reduce + # number of read requests which are adding overhead. + # this variable should be divisible by 4 to match 32bit reads from TRNG + + if not 8 >= tsb > 0: + raise subcmd.TpmTestError('NIST only supports 1 to 8 bits per sample') + + # compute number of bytes, which is multiple of 4 containing whole number + # of samples, each size tsb bits. This a reduction of + # tsb * 32 // gcd(tsb, 32) // 8 + lcm = (tsb * 4) // gcd(tsb, 4) + + # combine reads of small batches if possible, 2032 is max size of TPM + # command response, adjusted to header size + bytes_per_read = (2032 // lcm) * lcm + + samples_per_read = 8 * bytes_per_read // tsb + + if samples_per_read < 1000: + raise subcmd.TpmTestError("Can't meet NIST requirement of min 1000 " + 'samples in batch - %d bytes only contain' + ' %d samples' % (bytes_per_read, + samples_per_read)) + print('TRNG bits per sample = ', tsb, 'Read size (bytes) =', + bytes_per_read, 'Samples per read =', samples_per_read) + + remaining_samples = TRNG_SAMPLE_COUNT + with open(trng_output, 'wb') as out_file: + while remaining_samples: 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')) + get_random_command(bytes_per_read, + trng_mode))) + if response[:12] != get_random_command_rsp(bytes_per_read): + raise subcmd.TpmTestError("Unexpected response to '%s': %s" % + ('trng', utils.hex_dump(response))) + bits = to_bitstring(response[12:], tsb) + bits = bits[:remaining_samples] + out_file.write(bits) + remaining_samples -= len(bits) + print('%s %d%%\r' % (utils.cursor_back(), + ((TRNG_SAMPLE_COUNT - remaining_samples)*100)\ + // TRNG_SAMPLE_COUNT), end='') + print('%sSUCCESS: %s' % (utils.cursor_back(), trng_output)) |