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.
124 if self.waddr == WORD_ADDR_COMMAND:
127 if len(b) - 1 != count:
128 self.put_warning(b[0][0], b[-1][1],
129 'Invalid frame length: Got {}, expecting {} '.format(
132 self.opcode = b[2][2]
133 self.put_opcode(b[2])
134 self.put_param1(b[3])
135 self.put_param2([b[4], b[5]])
136 self.put_data(b[6:-2])
137 self.put_crc([b[-2], b[-1]])
139 def output_rx_bytes(self):
143 if self.waddr == WORD_ADDR_RESET:
144 self.put_data([b[1]])
145 self.put_crc([b[2], b[3]])
146 self.put_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.put_data([b[1]])
150 self.put_crc([b[2], b[3]])
151 self.put_status(b[0][0], b[-1][1], b[1][2])
153 self.put_data(b[1:-2])
154 self.put_crc([b[-2], b[-1]])
156 def putx(self, s, data):
157 self.put(s[0], s[1], self.out_ann, data)
159 def puty(self, s, data):
160 self.put(s[0][0], s[1][1], self.out_ann, data)
162 def putz(self, ss, es, data):
163 self.put(ss, es, self.out_ann, data)
165 def put_waddr(self, s):
166 self.putx(s, [0, ['Word addr: %s' % WORD_ADDR[s[2]]]])
168 def put_count(self, s):
169 self.putx(s, [1, ['Count: %s' % s[2]]])
171 def put_opcode(self, s):
172 self.putx(s, [2, ['Opcode: %s' % OPCODES[s[2]]]])
174 def put_param1(self, s):
176 if op in (OPCODE_CHECK_MAC, OPCODE_DEV_REV, OPCODE_HMAC, \
177 OPCODE_MAC, OPCODE_NONCE, OPCODE_RANDOM, OPCODE_SHA):
178 self.putx(s, [3, ['Mode: %02X' % s[2]]])
179 elif op == OPCODE_DERIVE_KEY:
180 self.putx(s, [3, ['Random: %s' % s[2]]])
181 elif op == OPCODE_GEN_DIG:
182 self.putx(s, [3, ['Zone: %s' % ZONES[s[2]]]])
183 elif op == OPCODE_LOCK:
184 self.putx(s, [3, ['Zone: {}, Summary: {}'.format(
185 'DATA/OTP' if s[2] else 'CONFIG',
186 'Ignored' if s[2] & 0x80 else 'Used')]])
187 elif op == OPCODE_PAUSE:
188 self.putx(s, [3, ['Selector: %02X' % s[2]]])
189 elif op == OPCODE_READ:
190 self.putx(s, [3, ['Zone: {}, Length: {}'.format(ZONES[s[2] & 0x03],
191 '32 bytes' if s[2] & 0x90 else '4 bytes')]])
192 elif op == OPCODE_WRITE:
193 self.putx(s, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[s[2] & 0x03],
194 'Yes' if s[2] & 0x40 else 'No', '32 bytes' if s[2] & 0x90 else '4 bytes')]])
196 self.putx(s, [3, ['Param1: %02X' % s[2]]])
198 def put_param2(self, s):
200 if op == OPCODE_DERIVE_KEY:
201 self.puty(s, [4, ['TargetKey: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
202 elif op in (OPCODE_NONCE, OPCODE_PAUSE, OPCODE_RANDOM):
203 self.puty(s, [4, ['Zero: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
204 elif op in (OPCODE_HMAC, OPCODE_MAC, OPCODE_CHECK_MAC, OPCODE_GEN_DIG):
205 self.puty(s, [4, ['SlotID: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
206 elif op == OPCODE_LOCK:
207 self.puty(s, [4, ['Summary: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
208 elif op in (OPCODE_READ, OPCODE_WRITE):
209 self.puty(s, [4, ['Address: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
210 elif op == OPCODE_UPDATE_EXTRA:
211 self.puty(s, [4, ['NewValue: {:02x}'.format(s[0][2])]])
213 self.puty(s, [4, ['-']])
215 def put_data(self, s):
219 if op == OPCODE_CHECK_MAC:
220 self.putz(s[0][0], s[31][1], [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in s[0:31])]])
221 self.putz(s[32][0], s[63][1], [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in s[32:63])]])
222 self.putz(s[64][0], s[76][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s[64:76])]])
223 elif op == OPCODE_DERIVE_KEY:
224 self.putz(s[0][0], s[31][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]])
225 elif op == OPCODE_GEN_DIG:
226 self.putz(s[0][0], s[3][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s)]])
227 elif op == OPCODE_MAC:
228 self.putz(s[0][0], s[31][1], [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in s)]])
229 elif op == OPCODE_WRITE:
230 if len(s) > 32: # Value + MAC.
231 self.putz(s[0][0], s[-31][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]])
232 self.putz(s[-32][0], s[-1][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]])
234 self.putz(s[0][0], s[-1][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]])
236 self.putz(s[0][0], s[-1][1], [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in s)]])
238 def put_crc(self, s):
239 self.puty(s, [6, ['CRC: {:02X} {:02X}'.format(s[0][2], s[1][2])]])
241 def put_status(self, ss, es, status):
242 self.putz(ss, es, [7, ['Status: %s' % STATUS[status]]])
244 def put_warning(self, ss, es, msg):
245 self.putz(ss, es, [8, ['Warning: %s' % msg]])
247 def decode(self, ss, es, data):
251 if self.state == 'IDLE':
252 # Wait for an I²C START condition.
255 self.state = 'GET SLAVE ADDR'
257 elif self.state == 'GET SLAVE ADDR':
258 # Wait for an address read/write operation.
259 if cmd == 'ADDRESS READ':
260 self.state = 'READ REGS'
261 elif cmd == 'ADDRESS WRITE':
262 self.state = 'WRITE REGS'
263 elif self.state == 'READ REGS':
264 if cmd == 'DATA READ':
265 self.bytes.append([ss, es, databyte])
268 # Reset the opcode before received data, as this causes
269 # responses to be displayed incorrectly.
271 self.output_rx_bytes()
275 elif self.state == 'WRITE REGS':
276 if cmd == 'DATA WRITE':
277 self.bytes.append([ss, es, databyte])
280 self.output_tx_bytes()