1 //-----------------------------------------------------------------------------
2 // Copyright (C) Merlok - 2017
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
7 //-----------------------------------------------------------------------------
8 // Command: hf mf list. It shows data from arm buffer.
9 //-----------------------------------------------------------------------------
11 #include "cmdhflist.h"
21 #include "iso14443crc.h"
23 #include "protocols.h"
36 static enum MifareAuthSeq MifareAuthState
;
37 static TAuthData AuthData
;
39 void ClearAuthData() {
42 AuthData
.first_auth
= false;
46 * @brief iso14443A_CRC_check Checks CRC in command or response
50 * @return 0 : CRC-command, CRC not ok
51 * 1 : CRC-command, CRC ok
54 uint8_t iso14443A_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
58 if(len
<= 2) return 2;
60 if(isResponse
& (len
< 6)) return 2;
62 ComputeCrc14443(CRC_14443_A
, data
, len
-2, &b1
, &b2
);
63 if (b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
70 uint8_t mifare_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
72 switch(MifareAuthState
) {
78 return iso14443A_CRC_check(isResponse
, data
, len
);
84 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
88 case ISO14443A_CMD_WUPA
:
89 snprintf(exp
,size
,"WUPA");
90 MifareAuthState
= masNone
;
92 case ISO14443A_CMD_ANTICOLL_OR_SELECT
:{
93 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
94 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
97 snprintf(exp
,size
,"SELECT_UID"); break;
100 snprintf(exp
,size
,"ANTICOLL"); break;
103 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
104 //95 20 = Anticollision of cascade level2
105 //95 70 = Select of cascade level2
108 snprintf(exp
,size
,"SELECT_UID-2"); break;
111 snprintf(exp
,size
,"ANTICOLL-2"); break;
114 case ISO14443A_CMD_REQA
:
115 snprintf(exp
,size
,"REQA");
116 MifareAuthState
= masNone
;
118 case ISO14443A_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
119 case ISO14443A_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
120 case ISO14443A_CMD_HALT
:
121 snprintf(exp
,size
,"HALT");
122 MifareAuthState
= masNone
;
124 case ISO14443A_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
125 case MIFARE_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
126 case MIFARE_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
127 case MIFARE_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
128 case MIFARE_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
129 case MIFARE_AUTH_KEYA
:
131 snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]);
132 MifareAuthState
= masNt
;
134 // case MIFARE_ULEV1_VERSION : both 0x60.
135 snprintf(exp
,size
,"EV1 VERSION");
138 case MIFARE_AUTH_KEYB
:
139 MifareAuthState
= masNt
;
140 snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]);
142 case MIFARE_MAGICWUPC1
: snprintf(exp
,size
,"MAGIC WUPC1"); break;
143 case MIFARE_MAGICWUPC2
: snprintf(exp
,size
,"MAGIC WUPC2"); break;
144 case MIFARE_MAGICWIPEC
: snprintf(exp
,size
,"MAGIC WIPEC"); break;
145 case MIFARE_ULC_AUTH_1
: snprintf(exp
,size
,"AUTH "); break;
146 case MIFARE_ULC_AUTH_2
: snprintf(exp
,size
,"AUTH_ANSW"); break;
147 case MIFARE_ULEV1_AUTH
:
149 snprintf(exp
,size
,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd
[1], cmd
[2], cmd
[3], cmd
[4] );
151 snprintf(exp
,size
,"PWD-AUTH");
153 case MIFARE_ULEV1_FASTREAD
:{
154 if ( cmdsize
>=3 && cmd
[2] <= 0xE6)
155 snprintf(exp
,size
,"READ RANGE (%d-%d)",cmd
[1],cmd
[2]);
157 snprintf(exp
,size
,"?");
160 case MIFARE_ULC_WRITE
:{
162 snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]);
164 snprintf(exp
,size
,"?");
167 case MIFARE_ULEV1_READ_CNT
:{
169 snprintf(exp
,size
,"READ CNT(%d)",cmd
[1]);
171 snprintf(exp
,size
,"?");
174 case MIFARE_ULEV1_INCR_CNT
:{
176 snprintf(exp
,size
,"INCR(%d)",cmd
[1]);
178 snprintf(exp
,size
,"?");
181 case MIFARE_ULEV1_READSIG
: snprintf(exp
,size
,"READ_SIG"); break;
182 case MIFARE_ULEV1_CHECKTEAR
: snprintf(exp
,size
,"CHK_TEARING(%d)",cmd
[1]); break;
183 case MIFARE_ULEV1_VCSL
: snprintf(exp
,size
,"VCSL"); break;
184 default: snprintf(exp
,size
,"?"); break;
189 void annotateMifare(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
, bool isResponse
) {
191 if (MifareAuthState
== masNone
) {
192 if (cmdsize
== 7 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT
&& cmd
[1] == 0x70) {
194 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
196 if (cmdsize
== 7 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2
&& cmd
[1] == 0x70) {
198 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
202 switch(MifareAuthState
) {
204 if (cmdsize
== 4 && isResponse
) {
205 snprintf(exp
,size
,"AUTH: nt %s", (AuthData
.first_auth
) ? "" : "(enc)");
206 MifareAuthState
= masNrAr
;
207 if (AuthData
.first_auth
)
208 AuthData
.nt
= bytes_to_num(cmd
, cmdsize
);
210 AuthData
.nt_enc
= bytes_to_num(cmd
, cmdsize
);
213 MifareAuthState
= masError
;
217 if (cmdsize
== 8 && !isResponse
) {
218 snprintf(exp
,size
,"AUTH: nr ar (enc)");
219 MifareAuthState
= masAt
;
220 AuthData
.nr_enc
= bytes_to_num(cmd
, cmdsize
);
221 AuthData
.ar_enc
= bytes_to_num(&cmd
[3], cmdsize
);
224 MifareAuthState
= masError
;
228 if (cmdsize
== 4 && isResponse
) {
229 snprintf(exp
,size
,"AUTH: at (enc)");
230 MifareAuthState
= masFirstData
;
231 AuthData
.at_enc
= bytes_to_num(cmd
, cmdsize
);
234 MifareAuthState
= masError
;
242 annotateIso14443a(exp
, size
, cmd
, cmdsize
);