X-Git-Url: https://sigrok.org/gitweb/?p=libsigrokdecode.git;a=blobdiff_plain;f=decoders%2Fatsha204a%2Fpd.py;h=a6a5a1dfa328953355fb02ef8673538485891f89;hp=3b7ee53cf8c074a589b0e7f640c915aff2589aa2;hb=3a67b032235e719dc4e74f735d1c95ef2d481a33;hpb=d62ee705a0807493781bf1e314c73399bb8d72c5 diff --git a/decoders/atsha204a/pd.py b/decoders/atsha204a/pd.py index 3b7ee53..a6a5a1d 100644 --- a/decoders/atsha204a/pd.py +++ b/decoders/atsha204a/pd.py @@ -85,7 +85,7 @@ class Decoder(srd.Decoder): id = 'atsha204a' name = 'ATSHA204A' longname = 'Microchip ATSHA204A' - desc = 'Microchip ATSHA204A CryptoAuthentication device' + desc = 'Microchip ATSHA204A CryptoAuthentication device.' license = 'gplv2+' inputs = ['i2c'] outputs = ['atsha204a'] @@ -107,6 +107,9 @@ class Decoder(srd.Decoder): ) def __init__(self): + self.reset() + + def reset(self): self.state = 'IDLE' self.waddr = self.opcode = -1 self.ss_block = self.es_block = 0 @@ -120,117 +123,129 @@ class Decoder(srd.Decoder): if len(b) < 1: # Ignore wakeup. return self.waddr = b[0][2] - self.display_waddr(b[0]) + self.put_waddr(b[0]) if self.waddr == WORD_ADDR_COMMAND: count = b[1][2] - self.display_count(b[1]) + self.put_count(b[1]) if len(b) - 1 != count: - self.display_warning(b[0][0], b[-1][1], + self.put_warning(b[0][0], b[-1][1], 'Invalid frame length: Got {}, expecting {} '.format( - len(b) - 1, count)) + len(b) - 1, count)) return self.opcode = b[2][2] - self.display_opcode(b[2]) - self.display_param1(b[3]) - self.display_param2([b[4], b[5]]) - self.display_data(b[6:-2]) - self.display_crc([b[-2], b[-1]]) + self.put_opcode(b[2]) + self.put_param1(b[3]) + self.put_param2([b[4], b[5]]) + self.put_data(b[6:-2]) + self.put_crc([b[-2], b[-1]]) def output_rx_bytes(self): b = self.bytes count = b[0][2] - self.display_count(b[0]) + self.put_count(b[0]) if self.waddr == WORD_ADDR_RESET: - self.display_data([b[1]]) - self.display_crc([b[2], b[3]]) - self.display_status(b[0][0], b[-1][1], b[1][2]) + self.put_data([b[1]]) + self.put_crc([b[2], b[3]]) + self.put_status(b[0][0], b[-1][1], b[1][2]) elif self.waddr == WORD_ADDR_COMMAND: if count == 4: # Status / Error. - self.display_data([b[1]]) - self.display_crc([b[2], b[3]]) - self.display_status(b[0][0], b[-1][1], b[1][2]) + self.put_data([b[1]]) + self.put_crc([b[2], b[3]]) + self.put_status(b[0][0], b[-1][1], b[1][2]) else: - self.display_data(b[1:-2]) - self.display_crc([b[-2], b[-1]]) + self.put_data(b[1:-2]) + self.put_crc([b[-2], b[-1]]) + + def putx(self, s, data): + self.put(s[0], s[1], self.out_ann, data) + + def puty(self, s, data): + self.put(s[0][0], s[1][1], self.out_ann, data) + + def putz(self, ss, es, data): + self.put(ss, es, self.out_ann, data) - def display_waddr(self, data): - self.put(data[0], data[1], self.out_ann, [0, ['Word addr: %s' % WORD_ADDR[data[2]]]]) + def put_waddr(self, s): + self.putx(s, [0, ['Word addr: %s' % WORD_ADDR[s[2]]]]) - def display_count(self, data): - self.put(data[0], data[1], self.out_ann, [1, ['Count: %s' % data[2]]]) + def put_count(self, s): + self.putx(s, [1, ['Count: %s' % s[2]]]) - def display_opcode(self, data): - self.put(data[0], data[1], self.out_ann, [2, ['Opcode: %s' % OPCODES[data[2]]]]) + def put_opcode(self, s): + self.putx(s, [2, ['Opcode: %s' % OPCODES[s[2]]]]) - def display_param1(self, data): - if self.opcode in (OPCODE_CHECK_MAC, OPCODE_DEV_REV, OPCODE_HMAC, \ + def put_param1(self, s): + op = self.opcode + if op in (OPCODE_CHECK_MAC, OPCODE_DEV_REV, OPCODE_HMAC, \ OPCODE_MAC, OPCODE_NONCE, OPCODE_RANDOM, OPCODE_SHA): - self.put(data[0], data[1], self.out_ann, [3, ['Mode: %02X' % data[2]]]) - elif self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0], data[1], self.out_ann, [3, ['Random: %s' % data[2]]]) - elif self.opcode == OPCODE_GEN_DIG: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: %s' % ZONES[data[2]]]]) - elif self.opcode == OPCODE_LOCK: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Summary: {}'.format( - 'DATA/OTP' if data[2] else 'CONFIG', - 'Ignored' if data[2] & 0x80 else 'Used')]]) - elif self.opcode == OPCODE_PAUSE: - self.put(data[0], data[1], self.out_ann, [3, ['Selector: %02X' % data[2]]]) - elif self.opcode == OPCODE_READ: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Length: {}'.format(ZONES[data[2] & 0x03], - '32 bytes' if data[2] & 0x90 else '4 bytes')]]) - elif self.opcode == OPCODE_WRITE: - self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[data[2] & 0x03], - 'Yes' if data[2] & 0x40 else 'No', '32 bytes' if data[2] & 0x90 else '4 bytes')]]) + self.putx(s, [3, ['Mode: %02X' % s[2]]]) + elif op == OPCODE_DERIVE_KEY: + self.putx(s, [3, ['Random: %s' % s[2]]]) + elif op == OPCODE_GEN_DIG: + self.putx(s, [3, ['Zone: %s' % ZONES[s[2]]]]) + elif op == OPCODE_LOCK: + self.putx(s, [3, ['Zone: {}, Summary: {}'.format( + 'DATA/OTP' if s[2] else 'CONFIG', + 'Ignored' if s[2] & 0x80 else 'Used')]]) + elif op == OPCODE_PAUSE: + self.putx(s, [3, ['Selector: %02X' % s[2]]]) + elif op == OPCODE_READ: + self.putx(s, [3, ['Zone: {}, Length: {}'.format(ZONES[s[2] & 0x03], + '32 bytes' if s[2] & 0x90 else '4 bytes')]]) + elif op == OPCODE_WRITE: + self.putx(s, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[s[2] & 0x03], + 'Yes' if s[2] & 0x40 else 'No', '32 bytes' if s[2] & 0x90 else '4 bytes')]]) else: - self.put(data[0], data[1], self.out_ann, [3, ['Param1: %02X' % data[2]]]) + self.putx(s, [3, ['Param1: %02X' % s[2]]]) - def display_param2(self, data): - if self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['TargetKey: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode in (OPCODE_NONCE, OPCODE_PAUSE, OPCODE_RANDOM): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Zero: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode in (OPCODE_HMAC, OPCODE_MAC, OPCODE_CHECK_MAC, OPCODE_GEN_DIG): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['SlotID: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode == OPCODE_LOCK: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Summary: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode in (OPCODE_READ, OPCODE_WRITE): - self.put(data[0][0], data[1][1], self.out_ann, [4, ['Address: {:02x} {:02x}'.format(data[1][2], data[0][2])]]) - elif self.opcode == OPCODE_UPDATE_EXTRA: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['NewValue: {:02x}'.format(data[0][2])]]) + def put_param2(self, s): + op = self.opcode + if op == OPCODE_DERIVE_KEY: + self.puty(s, [4, ['TargetKey: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_NONCE, OPCODE_PAUSE, OPCODE_RANDOM): + self.puty(s, [4, ['Zero: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_HMAC, OPCODE_MAC, OPCODE_CHECK_MAC, OPCODE_GEN_DIG): + self.puty(s, [4, ['SlotID: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op == OPCODE_LOCK: + self.puty(s, [4, ['Summary: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op in (OPCODE_READ, OPCODE_WRITE): + self.puty(s, [4, ['Address: {:02x} {:02x}'.format(s[1][2], s[0][2])]]) + elif op == OPCODE_UPDATE_EXTRA: + self.puty(s, [4, ['NewValue: {:02x}'.format(s[0][2])]]) else: - self.put(data[0][0], data[1][1], self.out_ann, [4, ['-']]) + self.puty(s, [4, ['-']]) - def display_data(self, data): - if len(data) == 0: + def put_data(self, s): + if len(s) == 0: return - if self.opcode == OPCODE_CHECK_MAC: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in data[0:31])]]) - self.put(data[32][0], data[63][1], self.out_ann, [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in data[32:63])]]) - self.put(data[64][0], data[76][1], self.out_ann, [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in data[64:76])]]) - elif self.opcode == OPCODE_DERIVE_KEY: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_GEN_DIG: - self.put(data[0][0], data[3][1], self.out_ann, [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_MAC: - self.put(data[0][0], data[31][1], self.out_ann, [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - elif self.opcode == OPCODE_WRITE: - if len(data) > 32: # Value + MAC. - self.put(data[0][0], data[-31][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]]) - self.put(data[-32][0], data[-1][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + op = self.opcode + if op == OPCODE_CHECK_MAC: + self.putz(s[0][0], s[31][1], [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in s[0:31])]]) + self.putz(s[32][0], s[63][1], [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in s[32:63])]]) + self.putz(s[64][0], s[76][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s[64:76])]]) + elif op == OPCODE_DERIVE_KEY: + self.putz(s[0][0], s[31][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_GEN_DIG: + self.putz(s[0][0], s[3][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_MAC: + self.putz(s[0][0], s[31][1], [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + elif op == OPCODE_WRITE: + if len(s) > 32: # Value + MAC. + self.putz(s[0][0], s[-31][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]]) + self.putz(s[-32][0], s[-1][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]]) else: # Just value. - self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + self.putz(s[0][0], s[-1][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]]) else: - self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in data)]]) + self.putz(s[0][0], s[-1][1], [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in s)]]) - def display_crc(self, data): - self.put(data[0][0], data[1][1], self.out_ann, [6, ['CRC: {:02X} {:02X}'.format(data[0][2], data[1][2])]]) + def put_crc(self, s): + self.puty(s, [6, ['CRC: {:02X} {:02X}'.format(s[0][2], s[1][2])]]) - def display_status(self, start, end, status): - self.put(start, end, self.out_ann, [7, ['Status: %s' % STATUS[status]]]) + def put_status(self, ss, es, status): + self.putz(ss, es, [7, ['Status: %s' % STATUS[status]]]) - def display_warning(self, start, end, msg): - self.put(start, end, self.out_ann, [8, ['Warning: %s' % msg]]) + def put_warning(self, ss, es, msg): + self.putz(ss, es, [8, ['Warning: %s' % msg]]) def decode(self, ss, es, data): cmd, databyte = data