]>
Commit | Line | Data |
---|---|---|
4f131b53 | 1 | //----------------------------------------------------------------------------- |
2 | // Copyright (C) Merlok - 2017 | |
3 | // | |
4 | // This code is licensed to you under the terms of the GNU GPL, version 2 or, | |
5 | // at your option, any later version. See the LICENSE.txt file for the text of | |
6 | // the license. | |
7 | //----------------------------------------------------------------------------- | |
8 | // Command: hf mf list. It shows data from arm buffer. | |
9 | //----------------------------------------------------------------------------- | |
10 | ||
11 | #include "cmdhflist.h" | |
12 | ||
13 | #include <stdlib.h> | |
14 | #include <stdio.h> | |
15 | #include <string.h> | |
6612a5a2 | 16 | #include <stdint.h> |
17 | #include <stdbool.h> | |
18 | #include "util.h" | |
19 | #include "data.h" | |
20 | #include "ui.h" | |
21 | #include "iso14443crc.h" | |
22 | #include "parity.h" | |
23 | #include "protocols.h" | |
4f131b53 | 24 | |
25 | ||
6612a5a2 | 26 | enum MifareAuthSeq { |
27 | masNone, | |
28 | masNt, | |
29 | masNrAr, | |
30 | masAt, | |
31 | masData, | |
32 | masDataNested, | |
33 | masError, | |
34 | }; | |
35 | static enum MifareAuthSeq MifareAuthState; | |
36 | ||
37 | /** | |
38 | * @brief iso14443A_CRC_check Checks CRC in command or response | |
39 | * @param isResponse | |
40 | * @param data | |
41 | * @param len | |
42 | * @return 0 : CRC-command, CRC not ok | |
43 | * 1 : CRC-command, CRC ok | |
44 | * 2 : Not crc-command | |
45 | */ | |
46 | uint8_t iso14443A_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
47 | { | |
48 | uint8_t b1,b2; | |
49 | ||
50 | if(len <= 2) return 2; | |
51 | ||
52 | if(isResponse & (len < 6)) return 2; | |
53 | ||
54 | ComputeCrc14443(CRC_14443_A, data, len-2, &b1, &b2); | |
55 | if (b1 != data[len-2] || b2 != data[len-1]) { | |
56 | return 0; | |
57 | } else { | |
58 | return 1; | |
59 | } | |
60 | } | |
61 | ||
62 | uint8_t mifare_CRC_check(bool isResponse, uint8_t* data, uint8_t len) | |
63 | { | |
64 | switch(MifareAuthState) { | |
65 | case masNone: | |
66 | case masData: | |
67 | case masDataNested: | |
68 | case masError: | |
69 | return iso14443A_CRC_check(isResponse, data, len); | |
70 | default: | |
71 | return 2; | |
72 | } | |
73 | ||
74 | } | |
75 | ||
76 | void annotateIso14443a(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize) | |
77 | { | |
78 | switch(cmd[0]) | |
79 | { | |
80 | case ISO14443A_CMD_WUPA: snprintf(exp,size,"WUPA"); break; | |
81 | case ISO14443A_CMD_ANTICOLL_OR_SELECT:{ | |
82 | // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor) | |
83 | // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK) | |
84 | if(cmd[1] == 0x70) | |
85 | { | |
86 | snprintf(exp,size,"SELECT_UID"); break; | |
87 | }else | |
88 | { | |
89 | snprintf(exp,size,"ANTICOLL"); break; | |
90 | } | |
91 | } | |
92 | case ISO14443A_CMD_ANTICOLL_OR_SELECT_2:{ | |
93 | //95 20 = Anticollision of cascade level2 | |
94 | //95 70 = Select of cascade level2 | |
95 | if(cmd[2] == 0x70) | |
96 | { | |
97 | snprintf(exp,size,"SELECT_UID-2"); break; | |
98 | }else | |
99 | { | |
100 | snprintf(exp,size,"ANTICOLL-2"); break; | |
101 | } | |
102 | } | |
103 | case ISO14443A_CMD_REQA: snprintf(exp,size,"REQA"); break; | |
104 | case ISO14443A_CMD_READBLOCK: snprintf(exp,size,"READBLOCK(%d)",cmd[1]); break; | |
105 | case ISO14443A_CMD_WRITEBLOCK: snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); break; | |
106 | case ISO14443A_CMD_HALT: | |
107 | snprintf(exp,size,"HALT"); | |
108 | MifareAuthState = masNone; | |
109 | break; | |
110 | case ISO14443A_CMD_RATS: snprintf(exp,size,"RATS"); break; | |
111 | case MIFARE_CMD_INC: snprintf(exp,size,"INC(%d)",cmd[1]); break; | |
112 | case MIFARE_CMD_DEC: snprintf(exp,size,"DEC(%d)",cmd[1]); break; | |
113 | case MIFARE_CMD_RESTORE: snprintf(exp,size,"RESTORE(%d)",cmd[1]); break; | |
114 | case MIFARE_CMD_TRANSFER: snprintf(exp,size,"TRANSFER(%d)",cmd[1]); break; | |
115 | case MIFARE_AUTH_KEYA: | |
116 | if ( cmdsize > 3) { | |
117 | snprintf(exp,size,"AUTH-A(%d)",cmd[1]); | |
118 | MifareAuthState = masNt; | |
119 | } else { | |
120 | // case MIFARE_ULEV1_VERSION : both 0x60. | |
121 | snprintf(exp,size,"EV1 VERSION"); | |
122 | } | |
123 | break; | |
124 | case MIFARE_AUTH_KEYB: | |
125 | MifareAuthState = masNt; | |
126 | snprintf(exp,size,"AUTH-B(%d)",cmd[1]); | |
127 | break; | |
128 | case MIFARE_MAGICWUPC1: snprintf(exp,size,"MAGIC WUPC1"); break; | |
129 | case MIFARE_MAGICWUPC2: snprintf(exp,size,"MAGIC WUPC2"); break; | |
130 | case MIFARE_MAGICWIPEC: snprintf(exp,size,"MAGIC WIPEC"); break; | |
131 | case MIFARE_ULC_AUTH_1: snprintf(exp,size,"AUTH "); break; | |
132 | case MIFARE_ULC_AUTH_2: snprintf(exp,size,"AUTH_ANSW"); break; | |
133 | case MIFARE_ULEV1_AUTH: | |
134 | if ( cmdsize == 7 ) | |
135 | snprintf(exp,size,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd[1], cmd[2], cmd[3], cmd[4] ); | |
136 | else | |
137 | snprintf(exp,size,"PWD-AUTH"); | |
138 | break; | |
139 | case MIFARE_ULEV1_FASTREAD:{ | |
140 | if ( cmdsize >=3 && cmd[2] <= 0xE6) | |
141 | snprintf(exp,size,"READ RANGE (%d-%d)",cmd[1],cmd[2]); | |
142 | else | |
143 | snprintf(exp,size,"?"); | |
144 | break; | |
145 | } | |
146 | case MIFARE_ULC_WRITE:{ | |
147 | if ( cmd[1] < 0x21 ) | |
148 | snprintf(exp,size,"WRITEBLOCK(%d)",cmd[1]); | |
149 | else | |
150 | snprintf(exp,size,"?"); | |
151 | break; | |
152 | } | |
153 | case MIFARE_ULEV1_READ_CNT:{ | |
154 | if ( cmd[1] < 5 ) | |
155 | snprintf(exp,size,"READ CNT(%d)",cmd[1]); | |
156 | else | |
157 | snprintf(exp,size,"?"); | |
158 | break; | |
159 | } | |
160 | case MIFARE_ULEV1_INCR_CNT:{ | |
161 | if ( cmd[1] < 5 ) | |
162 | snprintf(exp,size,"INCR(%d)",cmd[1]); | |
163 | else | |
164 | snprintf(exp,size,"?"); | |
165 | break; | |
166 | } | |
167 | case MIFARE_ULEV1_READSIG: snprintf(exp,size,"READ_SIG"); break; | |
168 | case MIFARE_ULEV1_CHECKTEAR: snprintf(exp,size,"CHK_TEARING(%d)",cmd[1]); break; | |
169 | case MIFARE_ULEV1_VCSL: snprintf(exp,size,"VCSL"); break; | |
170 | default: snprintf(exp,size,"?"); break; | |
171 | } | |
172 | return; | |
173 | } | |
174 | ||
175 | void annotateMifare(char *exp, size_t size, uint8_t* cmd, uint8_t cmdsize, bool isResponse) { | |
176 | switch(MifareAuthState) { | |
177 | case masNt: | |
178 | if (cmdsize == 4) { | |
179 | snprintf(exp,size,"AUTH: nt"); | |
180 | MifareAuthState = masNrAr; | |
181 | printf("--ntok\n"); | |
182 | return; | |
183 | } else { | |
184 | MifareAuthState = masError; | |
185 | printf("--err %d\n", cmdsize); | |
186 | } | |
187 | break; | |
188 | case masNrAr: | |
189 | if (cmdsize == 8) { | |
190 | snprintf(exp,size,"AUTH: nr ar"); | |
191 | MifareAuthState = masAt; | |
192 | return; | |
193 | } else { | |
194 | MifareAuthState = masError; | |
195 | } | |
196 | break; | |
197 | case masAt: | |
198 | if (cmdsize == 4) { | |
199 | snprintf(exp,size,"AUTH: at"); | |
200 | MifareAuthState = masData; | |
201 | return; | |
202 | } else { | |
203 | MifareAuthState = masError; | |
204 | } | |
205 | break; | |
206 | default: | |
207 | break; | |
208 | } | |
209 | ||
210 | if (!isResponse) | |
211 | annotateIso14443a(exp, size, cmd, cmdsize); | |
212 | ||
213 | } |