diff options
Diffstat (limited to 'src/sw/hash_tester.py')
-rwxr-xr-x | src/sw/hash_tester.py | 76 |
1 files changed, 50 insertions, 26 deletions
diff --git a/src/sw/hash_tester.py b/src/sw/hash_tester.py index e2b3777..6a5ff0b 100755 --- a/src/sw/hash_tester.py +++ b/src/sw/hash_tester.py @@ -158,9 +158,6 @@ SHA512_DOUBLE_DIGEST = [ 0x8e959b75, 0xdae313da, 0x8cf4f728, 0x14fc143f, 0x501d289e, 0x4900f7e4, 0x331b99de, 0xc4b5433a, 0xc7d329ee, 0xb6dd2654, 0x5e96e55b, 0x874be909 ] -def hexlist(list): - return "[ " + ' '.join('%02x' % b for b in list) + " ]" - #---------------------------------------------------------------- # I2C class #---------------------------------------------------------------- @@ -172,6 +169,9 @@ I2C_addr = 0x0f # from /usr/include/linux/i2c-dev.h I2C_SLAVE = 0x0703 +def hexlist(list): + return "[ " + ' '.join('%02x' % b for b in list) + " ]" + class I2C: # file handle for the i2c device file = None @@ -202,15 +202,11 @@ class I2C: print "write %s" % hexlist(buf) self.file.write(bytearray(buf)) - # read 5 or 9 response bytes to a buffer - def read(self, len): - buf = [] - # read() on the i2c device will only return 1 byte at a time - for i in range(len): - buf.append(ord(self.file.read(1))) - if DEBUG: - print "read %s" % hexlist(buf) - return buf + # read one response byte from the i2c device + def read(self): + # read() on the i2c device will only return one byte at a time, + # and tc.get_resp() needs to parse the response one byte at a time + return ord(self.file.read(1)) #---------------------------------------------------------------- # test-case class @@ -221,12 +217,16 @@ SOC = 0x55 EOC = 0xaa READ_CMD = 0x10 WRITE_CMD = 0x11 +RESET_CMD = 0x01 # response codes SOR = 0xaa EOR = 0x55 READ_OK = 0x7f WRITE_OK = 0x7e +RESET_OK = 0x7d +UNKNOWN = 0xfe +ERROR = 0xfd class TcError(Exception): pass @@ -248,22 +248,46 @@ class tc: buf = [SOC, READ_CMD, self.addr0, self.addr1, EOC] self.i2c.write(buf) - def get_resp(self, expected): - buf = self.i2c.read(len(expected)) + def get_resp(self): + buf = [] + len = 2 + i = 0 + while i < len: + b = self.i2c.read() + if ((i == 0) and (b != SOR)): + # we've gotten out of sync, and there's probably nothing we can do + print "response byte 0: expected 0x%02x (SOR), got 0x%02x" % (SOR, b) + raise TcError() + elif (i == 1): # response code + try: + # anonymous dictionary of message lengths + len = {READ_OK:9, WRITE_OK:5, RESET_OK:3, ERROR:4, UNKNOWN:4}[b] + except KeyError: # unknown response code + # we've gotten out of sync, and there's probably nothing we can do + print "unknown response code 0x%02x" % b + raise TcError() + buf.append(b) + i += 1 + if DEBUG: + print "read %s" % hexlist(buf) + return buf + + def get_expected(self, expected): + buf = self.get_resp() if (buf != expected): print "expected %s,\nreceived %s" % (hexlist(expected), hexlist(buf)) raise TcError() def get_write_resp(self): expected = [SOR, WRITE_OK, self.addr0, self.addr1, EOR] - self.get_resp(expected) + self.get_expected(expected) def get_read_resp(self, data): expected = [SOR, READ_OK, self.addr0, self.addr1] for s in (24, 16, 8, 0): expected.append(data >> s & 0xff) expected.append(EOR) - self.get_resp(expected) + self.get_expected(expected) def write(self, data): self.send_write_cmd(data) @@ -289,7 +313,7 @@ def tc_wait(i2c, addr0, status): t = tc(i2c, addr0, ADDR_STATUS) while 1: t.send_read_cmd() - buf = t.i2c.read(9) + buf = t.get_resp() if ((buf[7] & status) == status): break @@ -328,9 +352,9 @@ def sha1_wait_valid(i2c): def TC1(i2c): print "TC1: Reading name, type and version words from SHA-1 core." - sha1_read(i2c, ADDR_NAME0, 0x73686131) # "sha1" - sha1_read(i2c, ADDR_NAME1, 0x20202020) # " " - sha1_read(i2c, ADDR_VERSION, 0x302e3530) # "0.50" + sha1_read(i2c, ADDR_NAME0, 0x73686131) # "sha1" + sha1_read(i2c, ADDR_NAME1, 0x20202020) # " " + sha1_read(i2c, ADDR_VERSION, 0x302e3530) # "0.50" # TC2: SHA-1 Single block message test as specified by NIST. def TC2(i2c): @@ -413,9 +437,9 @@ def sha256_wait_valid(i2c): def TC4(i2c): print "TC4: Reading name, type and version words from SHA-256 core." - sha256_read(i2c, ADDR_NAME0, 0x73686132) # "sha2" - sha256_read(i2c, ADDR_NAME1, 0x2d323536) # "-256" - sha256_read(i2c, ADDR_VERSION, 0x302e3830) # "0.80" + sha256_read(i2c, ADDR_NAME0, 0x73686132) # "sha2" + sha256_read(i2c, ADDR_NAME1, 0x2d323536) # "-256" + sha256_read(i2c, ADDR_VERSION, 0x302e3830) # "0.80" # TC5: SHA-256 Single block message test as specified by NIST. def TC5(i2c): @@ -539,9 +563,9 @@ def sha512_wait_valid(i2c): def TC8(i2c): print "TC8: Reading name, type and version words from SHA-512 core." - sha512_read(i2c, ADDR_NAME0, 0x73686132) # "sha2" - sha512_read(i2c, ADDR_NAME1, 0x2d353132) # "-512" - sha512_read(i2c, ADDR_VERSION, 0x302e3830) # "0.80" + sha512_read(i2c, ADDR_NAME0, 0x73686132) # "sha2" + sha512_read(i2c, ADDR_NAME1, 0x2d353132) # "-512" + sha512_read(i2c, ADDR_VERSION, 0x302e3830) # "0.80" # TC9: SHA-512 Single block message test as specified by NIST. # We do this for all modes. |