2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2018 Michalis Pappas <mpappas@fastmail.fm>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
22 WORD_ADDR_RESET = 0x00
23 WORD_ADDR_SLEEP = 0x01
25 WORD_ADDR_COMMAND = 0x03
27 WORD_ADDR = {0x00: 'RESET', 0x01: 'SLEEP', 0x02: 'IDLE', 0x03: 'COMMAND'}
29 OPCODE_DERIVE_KEY = 0x1c
33 OPCODE_CHECK_MAC = 0x28
41 OPCODE_UPDATE_EXTRA = 0x20
65 ZONES = {0x00: 'CONFIG', 0x01: 'OTP', 0x02: 'DATA'}
68 STATUS_CHECKMAC_FAIL = 0x01
69 STATUS_PARSE_ERROR = 0x03
70 STATUS_EXECUTION_ERROR = 0x0f
72 STATUS_CRC_COMM_ERROR = 0xff
75 0x00: 'Command success',
76 0x01: 'Checkmac failure',
78 0x0f: 'Execution error',
80 0xff: 'CRC / communications error',
83 class Decoder(srd.Decoder):
87 longname = 'Microchip ATSHA204A'
88 desc = 'Microchip ATSHA204A CryptoAuthentication device'
91 outputs = ['atsha204a']
93 ('waddr', 'Word address'),
100 ('status', 'Status'),
101 ('warning', 'Warning'),
104 ('frame', 'Frame', (0, 1, 2, 3, 4, 5, 6)),
105 ('status', 'Status', (7,)),
106 ('warnings', 'Warnings', (8,)),
111 self.waddr = self.opcode = -1
112 self.ss_block = self.es_block = 0
116 self.out_ann = self.register(srd.OUTPUT_ANN)
118 def output_tx_bytes(self):
120 if len(b) < 1: # Ignore wakeup.
123 self.display_waddr(b[0])
124 if self.waddr == WORD_ADDR_COMMAND:
126 self.display_count(b[1])
127 if len(b) - 1 != count:
128 self.display_warning(b[0][0], b[-1][1],
129 'Invalid frame length: Got {}, expecting {} '.format(
132 self.opcode = b[2][2]
133 self.display_opcode(b[2])
134 self.display_param1(b[3])
135 self.display_param2([b[4], b[5]])
136 self.display_data(b[6:-2])
137 self.display_crc([b[-2], b[-1]])
139 def output_rx_bytes(self):
142 self.display_count(b[0])
143 if self.waddr == WORD_ADDR_RESET:
144 self.display_data([b[1]])
145 self.display_crc([b[2], b[3]])
146 self.display_status(b[0][0], b[-1][1], b[1][2])
147 elif self.waddr == WORD_ADDR_COMMAND:
148 if count == 4: # Status / Error.
149 self.display_data([b[1]])
150 self.display_crc([b[2], b[3]])
151 self.display_status(b[0][0], b[-1][1], b[1][2])
153 self.display_data(b[1:-2])
154 self.display_crc([b[-2], b[-1]])
156 def display_waddr(self, data):
157 self.put(data[0], data[1], self.out_ann, [0, ['Word addr: %s' % WORD_ADDR[data[2]]]])
159 def display_count(self, data):
160 self.put(data[0], data[1], self.out_ann, [1, ['Count: %s' % data[2]]])
162 def display_opcode(self, data):
163 self.put(data[0], data[1], self.out_ann, [2, ['Opcode: %s' % OPCODES[data[2]]]])
165 def display_param1(self, data):
166 if (self.opcode == OPCODE_CHECK_MAC) or (self.opcode == OPCODE_DEV_REV) or \
167 (self.opcode == OPCODE_HMAC) or (self.opcode == OPCODE_MAC) or \
168 (self.opcode == OPCODE_NONCE) or (self.opcode == OPCODE_RANDOM) or \
169 (self.opcode == OPCODE_SHA):
170 self.put(data[0], data[1], self.out_ann, [3, ['Mode: %02X' % data[2]]])
171 elif self.opcode == OPCODE_DERIVE_KEY:
172 self.put(data[0], data[1], self.out_ann, [3, ['Random: %s' % data[2]]])
173 elif self.opcode == OPCODE_GEN_DIG:
174 self.put(data[0], data[1], self.out_ann, [3, ['Zone: %s' % ZONES[data[2]]]])
175 elif self.opcode == OPCODE_LOCK:
176 self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Summary: {}'.format(
177 'DATA/OTP' if data[2] else 'CONFIG',
178 'Ignored' if data[2] & 0x80 else 'Used')]])
179 elif self.opcode == OPCODE_PAUSE:
180 self.put(data[0], data[1], self.out_ann, [3, ['Selector: %02X' % data[2]]])
181 elif self.opcode == OPCODE_READ:
182 self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Length: {}'.format(ZONES[data[2] & 0x03],
183 '32 bytes' if data[2] & 0x90 else '4 bytes')]])
184 elif self.opcode == OPCODE_WRITE:
185 self.put(data[0], data[1], self.out_ann, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[data[2] & 0x03],
186 'Yes' if data[2] & 0x40 else 'No', '32 bytes' if data[2] & 0x90 else '4 bytes')]])
188 self.put(data[0], data[1], self.out_ann, [3, ['Param1: %02X' % data[2]]])
190 def display_param2(self, data):
191 if self.opcode == OPCODE_DERIVE_KEY:
192 self.put(data[0][0], data[1][1], self.out_ann, [4, ['TargetKey: {:02x} {:02x}'.format(data[1][2], data[0][2])]])
193 elif (self.opcode == OPCODE_NONCE) or (self.opcode == OPCODE_PAUSE) or (self.opcode == OPCODE_RANDOM):
194 self.put(data[0][0], data[1][1], self.out_ann, [4, ['Zero: {:02x} {:02x}'.format(data[1][2], data[0][2])]])
195 elif (self.opcode == OPCODE_HMAC) or (self.opcode == OPCODE_MAC) or \
196 (self.opcode == OPCODE_CHECK_MAC) or (self.opcode == OPCODE_GEN_DIG):
197 self.put(data[0][0], data[1][1], self.out_ann, [4, ['SlotID: {:02x} {:02x}'.format(data[1][2], data[0][2])]])
198 elif self.opcode == OPCODE_LOCK:
199 self.put(data[0][0], data[1][1], self.out_ann, [4, ['Summary: {:02x} {:02x}'.format(data[1][2], data[0][2])]])
200 elif (self.opcode == OPCODE_READ) or (self.opcode == OPCODE_WRITE):
201 self.put(data[0][0], data[1][1], self.out_ann, [4, ['Address: {:02x} {:02x}'.format(data[1][2], data[0][2])]])
202 elif self.opcode == OPCODE_UPDATE_EXTRA:
203 self.put(data[0][0], data[1][1], self.out_ann, [4, ['NewValue: {:02x}'.format(data[0][2])]])
205 self.put(data[0][0], data[1][1], self.out_ann, [4, ['-']])
207 def display_data(self, data):
210 if self.opcode == OPCODE_CHECK_MAC:
211 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])]])
212 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])]])
213 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])]])
214 elif self.opcode == OPCODE_DERIVE_KEY:
215 self.put(data[0][0], data[31][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]])
216 elif self.opcode == OPCODE_GEN_DIG:
217 self.put(data[0][0], data[3][1], self.out_ann, [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in data)]])
218 elif self.opcode == OPCODE_MAC:
219 self.put(data[0][0], data[31][1], self.out_ann, [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in data)]])
220 elif self.opcode == OPCODE_WRITE:
221 if len(data) > 32: # Value + MAC.
222 self.put(data[0][0], data[-31][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]])
223 self.put(data[-32][0], data[-1][1], self.out_ann, [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in data)]])
225 self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in data)]])
227 self.put(data[0][0], data[-1][1], self.out_ann, [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in data)]])
229 def display_crc(self, data):
230 self.put(data[0][0], data[1][1], self.out_ann, [6, ['CRC: {:02X} {:02X}'.format(data[0][2], data[1][2])]])
232 def display_status(self, start, end, status):
233 self.put(start, end, self.out_ann, [7, ['Status: %s' % STATUS[status]]])
235 def display_warning(self, start, end, msg):
236 self.put(start, end, self.out_ann, [8, ['Warning: %s' % msg]])
238 def decode(self, ss, es, data):
242 if self.state == 'IDLE':
243 # Wait for an I²C START condition.
246 self.state = 'GET SLAVE ADDR'
248 elif self.state == 'GET SLAVE ADDR':
249 # Wait for an address read/write operation.
250 if cmd == 'ADDRESS READ':
251 self.state = 'READ REGS'
252 elif cmd == 'ADDRESS WRITE':
253 self.state = 'WRITE REGS'
254 elif self.state == 'READ REGS':
255 if cmd == 'DATA READ':
256 self.bytes.append([ss, es, databyte])
259 # Reset the opcode before received data, as this causes
260 # responses to be displayed incorrectly.
262 self.output_rx_bytes()
266 elif self.state == 'WRITE REGS':
267 if cmd == 'DATA WRITE':
268 self.bytes.append([ss, es, databyte])
271 self.output_tx_bytes()