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