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"
35 static enum MifareAuthSeq MifareAuthState
;
38 * @brief iso14443A_CRC_check Checks CRC in command or response
42 * @return 0 : CRC-command, CRC not ok
43 * 1 : CRC-command, CRC ok
46 uint8_t iso14443A_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
50 if(len
<= 2) return 2;
52 if(isResponse
& (len
< 6)) return 2;
54 ComputeCrc14443(CRC_14443_A
, data
, len
-2, &b1
, &b2
);
55 if (b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
62 uint8_t mifare_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
64 switch(MifareAuthState
) {
69 return iso14443A_CRC_check(isResponse
, data
, len
);
76 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
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)
86 snprintf(exp
,size
,"SELECT_UID"); break;
89 snprintf(exp
,size
,"ANTICOLL"); break;
92 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
93 //95 20 = Anticollision of cascade level2
94 //95 70 = Select of cascade level2
97 snprintf(exp
,size
,"SELECT_UID-2"); break;
100 snprintf(exp
,size
,"ANTICOLL-2"); break;
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
;
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
:
117 snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]);
118 MifareAuthState
= masNt
;
120 // case MIFARE_ULEV1_VERSION : both 0x60.
121 snprintf(exp
,size
,"EV1 VERSION");
124 case MIFARE_AUTH_KEYB
:
125 MifareAuthState
= masNt
;
126 snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]);
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
:
135 snprintf(exp
,size
,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd
[1], cmd
[2], cmd
[3], cmd
[4] );
137 snprintf(exp
,size
,"PWD-AUTH");
139 case MIFARE_ULEV1_FASTREAD
:{
140 if ( cmdsize
>=3 && cmd
[2] <= 0xE6)
141 snprintf(exp
,size
,"READ RANGE (%d-%d)",cmd
[1],cmd
[2]);
143 snprintf(exp
,size
,"?");
146 case MIFARE_ULC_WRITE
:{
148 snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]);
150 snprintf(exp
,size
,"?");
153 case MIFARE_ULEV1_READ_CNT
:{
155 snprintf(exp
,size
,"READ CNT(%d)",cmd
[1]);
157 snprintf(exp
,size
,"?");
160 case MIFARE_ULEV1_INCR_CNT
:{
162 snprintf(exp
,size
,"INCR(%d)",cmd
[1]);
164 snprintf(exp
,size
,"?");
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;
175 void annotateMifare(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
, bool isResponse
) {
176 switch(MifareAuthState
) {
179 snprintf(exp
,size
,"AUTH: nt");
180 MifareAuthState
= masNrAr
;
184 MifareAuthState
= masError
;
185 printf("--err %d\n", cmdsize
);
190 snprintf(exp
,size
,"AUTH: nr ar");
191 MifareAuthState
= masAt
;
194 MifareAuthState
= masError
;
199 snprintf(exp
,size
,"AUTH: at");
200 MifareAuthState
= masData
;
203 MifareAuthState
= masError
;
211 annotateIso14443a(exp
, size
, cmd
, cmdsize
);