]> sigrok.org Git - libsigrokdecode.git/blame - decoders/atsha204a/pd.py
atsha204a: Add the reset() method.
[libsigrokdecode.git] / decoders / atsha204a / pd.py
CommitLineData
f96a9602
MP
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Michalis Pappas <mpappas@fastmail.fm>
5##
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.
10##
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.
15##
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/>.
18##
19
20import sigrokdecode as srd
21
22WORD_ADDR_RESET = 0x00
23WORD_ADDR_SLEEP = 0x01
24WORD_ADDR_IDLE = 0x02
25WORD_ADDR_COMMAND = 0x03
26
27WORD_ADDR = {0x00: 'RESET', 0x01: 'SLEEP', 0x02: 'IDLE', 0x03: 'COMMAND'}
28
29OPCODE_DERIVE_KEY = 0x1c
30OPCODE_DEV_REV = 0x30
31OPCODE_GEN_DIG = 0x15
32OPCODE_HMAC = 0x11
33OPCODE_CHECK_MAC = 0x28
34OPCODE_LOCK = 0x17
35OPCODE_MAC = 0x08
36OPCODE_NONCE = 0x16
37OPCODE_PAUSE = 0x01
38OPCODE_RANDOM = 0x1b
39OPCODE_READ = 0x02
40OPCODE_SHA = 0x47
41OPCODE_UPDATE_EXTRA = 0x20
42OPCODE_WRITE = 0x12
43
44OPCODES = {
45 0x01: 'Pause',
46 0x02: 'Read',
47 0x08: 'MAC',
48 0x11: 'HMAC',
49 0x12: 'Write',
50 0x15: 'GenDig',
51 0x16: 'Nonce',
52 0x17: 'Lock',
53 0x1b: 'Random',
54 0x1c: 'DeriveKey',
55 0x20: 'UpdateExtra',
56 0x28: 'CheckMac',
57 0x30: 'DevRev',
58 0x47: 'SHA',
59}
60
61ZONE_CONFIG = 0x00
62ZONE_OTP = 0x01
63ZONE_DATA = 0x02
64
65ZONES = {0x00: 'CONFIG', 0x01: 'OTP', 0x02: 'DATA'}
66
67STATUS_SUCCESS = 0x00
68STATUS_CHECKMAC_FAIL = 0x01
69STATUS_PARSE_ERROR = 0x03
70STATUS_EXECUTION_ERROR = 0x0f
71STATUS_READY = 0x11
72STATUS_CRC_COMM_ERROR = 0xff
73
74STATUS = {
75 0x00: 'Command success',
76 0x01: 'Checkmac failure',
77 0x03: 'Parse error',
78 0x0f: 'Execution error',
79 0x11: 'Ready',
80 0xff: 'CRC / communications error',
81}
82
83class Decoder(srd.Decoder):
84 api_version = 3
85 id = 'atsha204a'
86 name = 'ATSHA204A'
87 longname = 'Microchip ATSHA204A'
88 desc = 'Microchip ATSHA204A CryptoAuthentication device'
89 license = 'gplv2+'
90 inputs = ['i2c']
91 outputs = ['atsha204a']
92 annotations = (
93 ('waddr', 'Word address'),
94 ('count', 'Count'),
95 ('opcode', 'Opcode'),
96 ('param1', 'Param1'),
97 ('param2', 'Param2'),
98 ('data', 'Data'),
99 ('crc', 'CRC'),
100 ('status', 'Status'),
101 ('warning', 'Warning'),
102 )
103 annotation_rows = (
104 ('frame', 'Frame', (0, 1, 2, 3, 4, 5, 6)),
105 ('status', 'Status', (7,)),
106 ('warnings', 'Warnings', (8,)),
107 )
108
109 def __init__(self):
719bde9b
UH
110 self.reset()
111
112 def reset(self):
f96a9602
MP
113 self.state = 'IDLE'
114 self.waddr = self.opcode = -1
115 self.ss_block = self.es_block = 0
116 self.bytes = []
117
118 def start(self):
119 self.out_ann = self.register(srd.OUTPUT_ANN)
120
121 def output_tx_bytes(self):
d6430dff
UH
122 b = self.bytes
123 if len(b) < 1: # Ignore wakeup.
f96a9602 124 return
d6430dff 125 self.waddr = b[0][2]
26d56154 126 self.put_waddr(b[0])
f96a9602 127 if self.waddr == WORD_ADDR_COMMAND:
d6430dff 128 count = b[1][2]
26d56154 129 self.put_count(b[1])
d6430dff 130 if len(b) - 1 != count:
26d56154 131 self.put_warning(b[0][0], b[-1][1],
f96a9602 132 'Invalid frame length: Got {}, expecting {} '.format(
8968f2fe 133 len(b) - 1, count))
f96a9602 134 return
d6430dff 135 self.opcode = b[2][2]
26d56154
UH
136 self.put_opcode(b[2])
137 self.put_param1(b[3])
138 self.put_param2([b[4], b[5]])
139 self.put_data(b[6:-2])
140 self.put_crc([b[-2], b[-1]])
f96a9602
MP
141
142 def output_rx_bytes(self):
d6430dff
UH
143 b = self.bytes
144 count = b[0][2]
26d56154 145 self.put_count(b[0])
f96a9602 146 if self.waddr == WORD_ADDR_RESET:
26d56154
UH
147 self.put_data([b[1]])
148 self.put_crc([b[2], b[3]])
149 self.put_status(b[0][0], b[-1][1], b[1][2])
f96a9602
MP
150 elif self.waddr == WORD_ADDR_COMMAND:
151 if count == 4: # Status / Error.
26d56154
UH
152 self.put_data([b[1]])
153 self.put_crc([b[2], b[3]])
154 self.put_status(b[0][0], b[-1][1], b[1][2])
f96a9602 155 else:
26d56154
UH
156 self.put_data(b[1:-2])
157 self.put_crc([b[-2], b[-1]])
f96a9602 158
8968f2fe
UH
159 def putx(self, s, data):
160 self.put(s[0], s[1], self.out_ann, data)
f96a9602 161
8968f2fe
UH
162 def puty(self, s, data):
163 self.put(s[0][0], s[1][1], self.out_ann, data)
f96a9602 164
8968f2fe
UH
165 def putz(self, ss, es, data):
166 self.put(ss, es, self.out_ann, data)
f96a9602 167
26d56154 168 def put_waddr(self, s):
8968f2fe
UH
169 self.putx(s, [0, ['Word addr: %s' % WORD_ADDR[s[2]]]])
170
26d56154 171 def put_count(self, s):
8968f2fe
UH
172 self.putx(s, [1, ['Count: %s' % s[2]]])
173
26d56154 174 def put_opcode(self, s):
8968f2fe
UH
175 self.putx(s, [2, ['Opcode: %s' % OPCODES[s[2]]]])
176
26d56154 177 def put_param1(self, s):
88567e4a
UH
178 op = self.opcode
179 if op in (OPCODE_CHECK_MAC, OPCODE_DEV_REV, OPCODE_HMAC, \
d62ee705 180 OPCODE_MAC, OPCODE_NONCE, OPCODE_RANDOM, OPCODE_SHA):
8968f2fe 181 self.putx(s, [3, ['Mode: %02X' % s[2]]])
88567e4a 182 elif op == OPCODE_DERIVE_KEY:
8968f2fe 183 self.putx(s, [3, ['Random: %s' % s[2]]])
88567e4a 184 elif op == OPCODE_GEN_DIG:
8968f2fe 185 self.putx(s, [3, ['Zone: %s' % ZONES[s[2]]]])
88567e4a 186 elif op == OPCODE_LOCK:
8968f2fe
UH
187 self.putx(s, [3, ['Zone: {}, Summary: {}'.format(
188 'DATA/OTP' if s[2] else 'CONFIG',
189 'Ignored' if s[2] & 0x80 else 'Used')]])
88567e4a 190 elif op == OPCODE_PAUSE:
8968f2fe 191 self.putx(s, [3, ['Selector: %02X' % s[2]]])
88567e4a 192 elif op == OPCODE_READ:
8968f2fe
UH
193 self.putx(s, [3, ['Zone: {}, Length: {}'.format(ZONES[s[2] & 0x03],
194 '32 bytes' if s[2] & 0x90 else '4 bytes')]])
88567e4a 195 elif op == OPCODE_WRITE:
8968f2fe
UH
196 self.putx(s, [3, ['Zone: {}, Encrypted: {}, Length: {}'.format(ZONES[s[2] & 0x03],
197 'Yes' if s[2] & 0x40 else 'No', '32 bytes' if s[2] & 0x90 else '4 bytes')]])
f96a9602 198 else:
8968f2fe 199 self.putx(s, [3, ['Param1: %02X' % s[2]]])
f96a9602 200
26d56154 201 def put_param2(self, s):
88567e4a
UH
202 op = self.opcode
203 if op == OPCODE_DERIVE_KEY:
8968f2fe 204 self.puty(s, [4, ['TargetKey: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
88567e4a 205 elif op in (OPCODE_NONCE, OPCODE_PAUSE, OPCODE_RANDOM):
8968f2fe 206 self.puty(s, [4, ['Zero: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
88567e4a 207 elif op in (OPCODE_HMAC, OPCODE_MAC, OPCODE_CHECK_MAC, OPCODE_GEN_DIG):
8968f2fe 208 self.puty(s, [4, ['SlotID: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
88567e4a 209 elif op == OPCODE_LOCK:
8968f2fe 210 self.puty(s, [4, ['Summary: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
88567e4a 211 elif op in (OPCODE_READ, OPCODE_WRITE):
8968f2fe 212 self.puty(s, [4, ['Address: {:02x} {:02x}'.format(s[1][2], s[0][2])]])
88567e4a 213 elif op == OPCODE_UPDATE_EXTRA:
8968f2fe 214 self.puty(s, [4, ['NewValue: {:02x}'.format(s[0][2])]])
f96a9602 215 else:
8968f2fe 216 self.puty(s, [4, ['-']])
f96a9602 217
26d56154 218 def put_data(self, s):
8968f2fe 219 if len(s) == 0:
f96a9602 220 return
88567e4a
UH
221 op = self.opcode
222 if op == OPCODE_CHECK_MAC:
8968f2fe
UH
223 self.putz(s[0][0], s[31][1], [5, ['ClientChal: %s' % ' '.join(format(i[2], '02x') for i in s[0:31])]])
224 self.putz(s[32][0], s[63][1], [5, ['ClientResp: %s' % ' '.join(format(i[2], '02x') for i in s[32:63])]])
225 self.putz(s[64][0], s[76][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s[64:76])]])
88567e4a 226 elif op == OPCODE_DERIVE_KEY:
8968f2fe 227 self.putz(s[0][0], s[31][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]])
88567e4a 228 elif op == OPCODE_GEN_DIG:
8968f2fe 229 self.putz(s[0][0], s[3][1], [5, ['OtherData: %s' % ' '.join(format(i[2], '02x') for i in s)]])
88567e4a 230 elif op == OPCODE_MAC:
8968f2fe 231 self.putz(s[0][0], s[31][1], [5, ['Challenge: %s' % ' '.join(format(i[2], '02x') for i in s)]])
88567e4a 232 elif op == OPCODE_WRITE:
8968f2fe
UH
233 if len(s) > 32: # Value + MAC.
234 self.putz(s[0][0], s[-31][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]])
235 self.putz(s[-32][0], s[-1][1], [5, ['MAC: %s' % ' '.join(format(i[2], '02x') for i in s)]])
f96a9602 236 else: # Just value.
8968f2fe 237 self.putz(s[0][0], s[-1][1], [5, ['Value: %s' % ' '.join(format(i[2], '02x') for i in s)]])
f96a9602 238 else:
8968f2fe 239 self.putz(s[0][0], s[-1][1], [5, ['Data: %s' % ' '.join(format(i[2], '02x') for i in s)]])
f96a9602 240
26d56154 241 def put_crc(self, s):
8968f2fe 242 self.puty(s, [6, ['CRC: {:02X} {:02X}'.format(s[0][2], s[1][2])]])
f96a9602 243
26d56154 244 def put_status(self, ss, es, status):
8968f2fe 245 self.putz(ss, es, [7, ['Status: %s' % STATUS[status]]])
f96a9602 246
26d56154 247 def put_warning(self, ss, es, msg):
8968f2fe 248 self.putz(ss, es, [8, ['Warning: %s' % msg]])
f96a9602
MP
249
250 def decode(self, ss, es, data):
251 cmd, databyte = data
252
253 # State machine.
254 if self.state == 'IDLE':
255 # Wait for an I²C START condition.
256 if cmd != 'START':
257 return
258 self.state = 'GET SLAVE ADDR'
259 self.ss_block = ss
260 elif self.state == 'GET SLAVE ADDR':
261 # Wait for an address read/write operation.
262 if cmd == 'ADDRESS READ':
263 self.state = 'READ REGS'
264 elif cmd == 'ADDRESS WRITE':
265 self.state = 'WRITE REGS'
266 elif self.state == 'READ REGS':
267 if cmd == 'DATA READ':
268 self.bytes.append([ss, es, databyte])
269 elif cmd == 'STOP':
270 self.es_block = es
271 # Reset the opcode before received data, as this causes
272 # responses to be displayed incorrectly.
273 self.opcode = -1
274 self.output_rx_bytes()
275 self.waddr = -1
276 self.bytes = []
277 self.state = 'IDLE'
278 elif self.state == 'WRITE REGS':
279 if cmd == 'DATA WRITE':
280 self.bytes.append([ss, es, databyte])
281 elif cmd == 'STOP':
282 self.es_block = es
283 self.output_tx_bytes()
284 self.bytes = []
285 self.state = 'IDLE'
286