diff options
-rwxr-xr-x | src/sw/hash_tester.py | 459 |
1 files changed, 335 insertions, 124 deletions
diff --git a/src/sw/hash_tester.py b/src/sw/hash_tester.py index 73d64e2..c76c273 100755 --- a/src/sw/hash_tester.py +++ b/src/sw/hash_tester.py @@ -13,9 +13,8 @@ # http://pyserial.sourceforge.net/ # # -# Author: Joachim Strombergson -# Copyright (c) 2014, Secworks Sweden AB -# All rights reserved. +# Author: Joachim Strömbergson +# Copyright (c) 2014, SUNET # # Redistribution and use in source and binary forms, with or # without modification, are permitted provided that the following @@ -52,13 +51,24 @@ import serial import os import time import threading +import hashlib #------------------------------------------------------------------- # Defines. #------------------------------------------------------------------- +# Serial port defines. +# CONFIGURE YOUR DEVICE HERE! +SERIAL_DEVICE = '/dev/cu.usbserial-A801SA6T' +BAUD_RATE = 9600 +DATA_BITS = 8 +STOP_BITS = 1 + + +# Verbose operation on/off VERBOSE = False + # Memory map. SOC = '\x55' EOC = '\xaa' @@ -138,6 +148,33 @@ NAME0_ADDR = '\x00' NAME1_ADDR = '\x01' VERSION_ADDR = '\x02' +sha1_block_addr = [SHA1_ADDR_BLOCK0, SHA1_ADDR_BLOCK1, + SHA1_ADDR_BLOCK2, SHA1_ADDR_BLOCK3, + SHA1_ADDR_BLOCK4, SHA1_ADDR_BLOCK5, + SHA1_ADDR_BLOCK6, SHA1_ADDR_BLOCK7, + SHA1_ADDR_BLOCK8, SHA1_ADDR_BLOCK9, + SHA1_ADDR_BLOCK10, SHA1_ADDR_BLOCK11, + SHA1_ADDR_BLOCK12, SHA1_ADDR_BLOCK13, + SHA1_ADDR_BLOCK14, SHA1_ADDR_BLOCK15] + +sha1_digest_addr = [SHA1_ADDR_DIGEST0, SHA1_ADDR_DIGEST1, + SHA1_ADDR_DIGEST2, SHA1_ADDR_DIGEST3, + SHA1_ADDR_DIGEST4] + +sha256_block_addr = [SHA256_ADDR_BLOCK0, SHA256_ADDR_BLOCK1, + SHA256_ADDR_BLOCK2, SHA256_ADDR_BLOCK3, + SHA256_ADDR_BLOCK4, SHA256_ADDR_BLOCK5, + SHA256_ADDR_BLOCK6, SHA256_ADDR_BLOCK7, + SHA256_ADDR_BLOCK8, SHA256_ADDR_BLOCK9, + SHA256_ADDR_BLOCK10, SHA256_ADDR_BLOCK11, + SHA256_ADDR_BLOCK12, SHA256_ADDR_BLOCK13, + SHA256_ADDR_BLOCK14, SHA256_ADDR_BLOCK15] + +sha256_digest_addr = [SHA256_ADDR_DIGEST0, SHA256_ADDR_DIGEST1, + SHA256_ADDR_DIGEST2, SHA256_ADDR_DIGEST3, + SHA256_ADDR_DIGEST4, SHA256_ADDR_DIGEST5, + SHA256_ADDR_DIGEST6, SHA256_ADDR_DIGEST7] + #------------------------------------------------------------------- # print_response() @@ -190,12 +227,16 @@ def read_serial_thread(serialport): buffer = [] while True: - response = serialport.read() - buffer.append(response) - if response == '\x55': - print_response(buffer) - buffer = [] - + if serialport.isOpen(): + response = serialport.read() + buffer.append(response) + if ((response == '\x55') and len(buffer) > 7): + print_response(buffer) + buffer = [] + else: + print "No open device yet." + time.sleep(0.1) + #------------------------------------------------------------------- # write_serial_bytes() @@ -211,8 +252,173 @@ def write_serial_bytes(tx_cmd, serialport): # Allow the device to complete the transaction. time.sleep(0.1) + + +#------------------------------------------------------------------- +# single_block_test_sha256() +# +# Write a given block to SHA-256 and perform single block +# processing. +#------------------------------------------------------------------- +def single_block_test_sha256(block, ser): + # Write block to SHA-2. + for i in range(len(block) / 4): + message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\ + block[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start initial block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser) + + # Extract contents of the digest registers. + for i in range(8): + message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + +#------------------------------------------------------------------- +# double_block_test_sha256() +# +# Run double block message test. +#------------------------------------------------------------------- +def double_block_test_sha256(block1, block2, ser): + # Write block1 to SHA-256. + for i in range(len(block1) / 4): + message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\ + block1[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start initial block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser) + + # Extract contents of the digest registers. + for i in range(8): + message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + # Write block2 to SHA-256. + for i in range(len(block2) / 4): + message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\ + block2[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start next block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser) + + # Extract contents of the digest registers. + for i in range(8): + message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + +#------------------------------------------------------------------- +# huge_message_test_sha256() + +# Test with a message with a huge number (n) number of blocks. +#------------------------------------------------------------------- +def huge_message_test_sha256(block, n, ser): + # Write block to SHA-256. + for i in range(len(block) / 4): + message = [SOC, WRITE_CMD, SHA256_ADDR_PREFIX,] + [sha256_block_addr[i]] +\ + block[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start initial block hashing, wait. + write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) + time.sleep(0.1) + print "Block 0 done." + + # First blocl done. + n = n - 1 + + for i in range(n): + # Start next block hashing, wait. + write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser) + time.sleep(0.1) + print "Block %d done." % (i + 1) + + # Extract contents of the digest registers. + for i in range(8): + message = [SOC, READ_CMD, SHA256_ADDR_PREFIX] + [sha256_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + +#------------------------------------------------------------------- +# single_block_test_sha1() +# +# Write a given block to SHA-1 and perform single block +# processing. +#------------------------------------------------------------------- +def single_block_test_sha1(block, ser): + # Write block to SHA-1. + for i in range(len(block) / 4): + message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\ + block[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start initial block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS, EOC], ser) + + # Extract the digest. + for i in range(5): + message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" - + +#------------------------------------------------------------------- +# double_block_test_sha1 +# +# Run double block message test for SHA-1. +#------------------------------------------------------------------- +def double_block_test_sha1(block1, block2, ser): + # Write block1 to SHA-1. + for i in range(len(block1) / 4): + message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\ + block1[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start initial block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS, EOC], ser) + + # Extract the digest. + for i in range(5): + message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + # Write block2 to SHA-1. + for i in range(len(block2) / 4): + message = [SOC, WRITE_CMD, SHA1_ADDR_PREFIX,] + [sha1_block_addr[i]] +\ + block2[(i * 4) : ((i * 4 ) + 4)] + [EOC] + write_serial_bytes(message, ser) + + # Start next block hashing, wait and check status. + write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x02', EOC], ser) + time.sleep(0.1) + write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS, EOC], ser) + + # Extract the digest. + for i in range(5): + message = [SOC, READ_CMD, SHA1_ADDR_PREFIX] + [sha1_digest_addr[i]] + [EOC] + write_serial_bytes(message, ser) + print"" + + #------------------------------------------------------------------- # main() # @@ -221,11 +427,11 @@ def write_serial_bytes(tx_cmd, serialport): def main(): # Open device ser = serial.Serial() - ser.port='/dev/cu.usbserial-A801SA6T' - ser.baudrate=9600 - ser.bytesize=8 + ser.port=SERIAL_DEVICE + ser.baudrate=BAUD_RATE + ser.bytesize=DATA_BITS ser.parity='N' - ser.stopbits=1 + ser.stopbits=STOP_BITS ser.timeout=1 ser.writeTimeout=0 @@ -247,67 +453,75 @@ def main(): my_thread.daemon = True my_thread.start() - # TC1-1: Read name and version from SHA-1 core. - print "TC1-1: Reading name, type and version words from SHA-1 core." + + # TC1: Read name and version from SHA-1 core. + print "TC1: Reading name, type and version words from SHA-1 core." write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_NAME0, EOC], ser) write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_NAME1, EOC], ser) write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_VERSION, EOC], ser) print"" - # TC1-2: Writing and reading a block into the SHA-1 core. - print "TC1-2: Writing and reading a block into the SHA-1 core." - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK0, '\x61', '\x62', '\x63', '\x80', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK1, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK2, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK3, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK4, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK5, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK6, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK7, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK8, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK9, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK10, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK11, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK12, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK13, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK14, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK15, '\x00', '\x00', '\x00', '\x18', EOC], ser) - print "TC1-2: All block words written. Time to read." - - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK0, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK1, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK2, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK3, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK4, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK5, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK6, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK7, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK8, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK9, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK10, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK11, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK12, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK13, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK14, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_BLOCK15, EOC], ser) - print "TC1-2: All block words read." - print"" - # TC1-3: Running single block procssing on SHA-1 with the current block. - print "TC1-3: Starting the SHA-1 core on single block procssing with the current block." - write_serial_bytes([SOC, WRITE_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) - time.sleep(0.1) - print "TC1-3: Reading SHA-1 status and digest." - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_STATUS, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_DIGEST0, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_DIGEST1, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_DIGEST2, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_DIGEST3, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA1_ADDR_PREFIX, SHA1_ADDR_DIGEST4, EOC], ser) - print"" - - # TC2-1: Read name and version from SHA-256 core. - print "TC2-1: Reading name, type and version words from SHA-256 core." + # TC2: Single block message test as specified by NIST. + print "TC2: Single block message test for SHA-1." + tc2_block = ['\x61', '\x62', '\x63', '\x80', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x18'] + + tc2_sha1_expected = [0xa9993e36, 0x4706816a, 0xba3e2571, + 0x7850c26c, 0x9cd0d89d] + + print "TC2: Expected digest values as specified by NIST:" + for i in tc2_sha1_expected: + print("0x%08x " % i) + print("") + single_block_test_sha1(tc2_block, ser) + + + # TC3: Double block message test as specified by NIST. + print "TC3: Double block message test for SHA-1." + tc3_1_block = ['\x61', '\x62', '\x63', '\x64', '\x62', '\x63', '\x64', '\x65', + '\x63', '\x64', '\x65', '\x66', '\x64', '\x65', '\x66', '\x67', + '\x65', '\x66', '\x67', '\x68', '\x66', '\x67', '\x68', '\x69', + '\x67', '\x68', '\x69', '\x6A', '\x68', '\x69', '\x6A', '\x6B', + '\x69', '\x6A', '\x6B', '\x6C', '\x6A', '\x6B', '\x6C', '\x6D', + '\x6B', '\x6C', '\x6D', '\x6E', '\x6C', '\x6D', '\x6E', '\x6F', + '\x6D', '\x6E', '\x6F', '\x70', '\x6E', '\x6F', '\x70', '\x71', + '\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00'] + + tc3_2_block = ['\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', + '\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x01', '\xC0'] + + tc3_1_sha1_expected = [0xF4286818, 0xC37B27AE, 0x0408F581, + 0x84677148, 0x4A566572] + + tc3_2_sha1_expected = [0x84983E44, 0x1C3BD26E, 0xBAAE4AA1, + 0xF95129E5, 0xE54670F1] + + print "TC3: Expected digest values for first block as specified by NIST:" + for i in tc3_1_sha1_expected: + print("0x%08x " % i) + print("") + print "TC3: Expected digest values for second block as specified by NIST:" + for i in tc3_2_sha1_expected: + print("0x%08x " % i) + print("") + double_block_test_sha1(tc3_1_block, tc3_2_block, ser) + + + # TC4: Read name and version from SHA-256 core. + print "TC4: Reading name, type and version words from SHA-256 core." my_cmd = [SOC, READ_CMD, SHA256_ADDR_PREFIX, NAME0_ADDR, EOC] write_serial_bytes(my_cmd, ser) my_cmd = [SOC, READ_CMD, SHA256_ADDR_PREFIX, NAME1_ADDR, EOC] @@ -317,61 +531,58 @@ def main(): print"" - # TC2-2: Writing and reading a block into the SHA-256 core. - print "TC2-2: Writing and reading a block into the SHA-256 core." - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK0, '\x61', '\x62', '\x63', '\x80', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK1, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK2, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK3, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK4, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK5, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK6, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK7, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK8, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK9, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK10, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK11, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK12, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK13, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK14, '\x00', '\x00', '\x00', '\x00', EOC], ser) - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK15, '\x00', '\x00', '\x00', '\x18', EOC], ser) - print "TC1-2: All block words written. Time to read." - - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK0, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK1, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK2, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK3, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK4, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK5, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK6, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK7, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK8, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK9, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK10, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK11, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK12, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK13, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK14, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_BLOCK15, EOC], ser) - print "TC2-2: All block words read." - print"" - - - # TC2-3: Running single block procssing on SHA-256 with the current block. - print "TC2-3: Starting the SHA-256 core on single block procssing with the current block." - write_serial_bytes([SOC, WRITE_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_CTRL, '\x00', '\x00', '\x00', '\x01', EOC], ser) - time.sleep(0.1) - print "TC2-3: Reading SHA-256 status and digest." - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_STATUS, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST0, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST1, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST2, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST3, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST4, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST5, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST6, EOC], ser) - write_serial_bytes([SOC, READ_CMD, SHA256_ADDR_PREFIX, SHA256_ADDR_DIGEST7, EOC], ser) - print"" + # TC5: Single block message test as specified by NIST. + print "TC5: Single block message test for SHA-256." + + tc5_sha256_expected = [0xBA7816BF, 0x8F01CFEA, 0x414140DE, 0x5DAE2223, + 0xB00361A3, 0x96177A9C, 0xB410FF61, 0xF20015AD] + + print "TC5: Expected digest values as specified by NIST:" + for i in tc5_sha256_expected: + print("0x%08x " % i) + print("") + single_block_test_sha256(tc2_block, ser) + + + # TC6: Double block message test as specified by NIST. + print "TC6: Double block message test for SHA-256." + + tc6_1_sha256_expected = [0x85E655D6, 0x417A1795, 0x3363376A, 0x624CDE5C, + 0x76E09589, 0xCAC5F811, 0xCC4B32C1, 0xF20E533A] + + tc6_2_sha256_expected = [0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039, + 0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1] + + print "TC6: Expected digest values for first block as specified by NIST:" + for i in tc6_1_sha256_expected: + print("0x%08x " % i) + print("") + print "TC6: Expected digest values for second block as specified by NIST:" + for i in tc6_2_sha256_expected: + print("0x%08x " % i) + print("") + double_block_test_sha256(tc3_1_block, tc3_2_block, ser) + + # TC7: Huge message test. +# n = 10 +# print "TC7: Message with %d blocks test for SHA-256." % n +# tc7_block = ['\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef', +# '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f', +# '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef', +# '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f', +# '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef', +# '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f', +# '\xaa', '\x55', '\xaa', '\x55', '\xde', '\xad', '\xbe', '\xef', +# '\x55', '\xaa', '\x55', '\xaa', '\xf0', '\x0f', '\xf0', '\x0f'] +# +# tc7_expected = [0xf407ff0d, 0xb9dce2f6, 0x9b9759a9, 0xd3cdc805, +# 0xf250086d, 0x73bbefd5, 0xa972e0f7, 0x61a9c13e] +# +# print "TC7: Expected digest values after %d blocks:" %n +# for i in tc7_expected: +# print("0x%08x " % i) +# print("") +# huge_message_test_sha256(tc7_block, n, ser) # Exit nicely. if VERBOSE: |