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"
24 #include "crapto1/crapto1.h"
25 #include "mifarehost.h"
38 static enum MifareAuthSeq MifareAuthState
;
39 static TAuthData AuthData
;
41 void ClearAuthData() {
44 AuthData
.first_auth
= true;
48 * @brief iso14443A_CRC_check Checks CRC in command or response
52 * @return 0 : CRC-command, CRC not ok
53 * 1 : CRC-command, CRC ok
56 uint8_t iso14443A_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
60 if(len
<= 2) return 2;
62 if(isResponse
& (len
< 6)) return 2;
64 ComputeCrc14443(CRC_14443_A
, data
, len
-2, &b1
, &b2
);
65 if (b1
!= data
[len
-2] || b2
!= data
[len
-1]) {
72 uint8_t mifare_CRC_check(bool isResponse
, uint8_t* data
, uint8_t len
)
74 switch(MifareAuthState
) {
80 return iso14443A_CRC_check(isResponse
, data
, len
);
86 void annotateIso14443a(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
)
90 case ISO14443A_CMD_WUPA
:
91 snprintf(exp
,size
,"WUPA");
92 MifareAuthState
= masNone
;
94 case ISO14443A_CMD_ANTICOLL_OR_SELECT
:{
95 // 93 20 = Anticollision (usage: 9320 - answer: 4bytes UID+1byte UID-bytes-xor)
96 // 93 70 = Select (usage: 9370+5bytes 9320 answer - answer: 1byte SAK)
99 snprintf(exp
,size
,"SELECT_UID"); break;
102 snprintf(exp
,size
,"ANTICOLL"); break;
105 case ISO14443A_CMD_ANTICOLL_OR_SELECT_2
:{
106 //95 20 = Anticollision of cascade level2
107 //95 70 = Select of cascade level2
110 snprintf(exp
,size
,"SELECT_UID-2"); break;
113 snprintf(exp
,size
,"ANTICOLL-2"); break;
116 case ISO14443A_CMD_REQA
:
117 snprintf(exp
,size
,"REQA");
118 MifareAuthState
= masNone
;
120 case ISO14443A_CMD_READBLOCK
: snprintf(exp
,size
,"READBLOCK(%d)",cmd
[1]); break;
121 case ISO14443A_CMD_WRITEBLOCK
: snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]); break;
122 case ISO14443A_CMD_HALT
:
123 snprintf(exp
,size
,"HALT");
124 MifareAuthState
= masNone
;
126 case ISO14443A_CMD_RATS
: snprintf(exp
,size
,"RATS"); break;
127 case MIFARE_CMD_INC
: snprintf(exp
,size
,"INC(%d)",cmd
[1]); break;
128 case MIFARE_CMD_DEC
: snprintf(exp
,size
,"DEC(%d)",cmd
[1]); break;
129 case MIFARE_CMD_RESTORE
: snprintf(exp
,size
,"RESTORE(%d)",cmd
[1]); break;
130 case MIFARE_CMD_TRANSFER
: snprintf(exp
,size
,"TRANSFER(%d)",cmd
[1]); break;
131 case MIFARE_AUTH_KEYA
:
133 snprintf(exp
,size
,"AUTH-A(%d)",cmd
[1]);
134 MifareAuthState
= masNt
;
136 // case MIFARE_ULEV1_VERSION : both 0x60.
137 snprintf(exp
,size
,"EV1 VERSION");
140 case MIFARE_AUTH_KEYB
:
141 MifareAuthState
= masNt
;
142 snprintf(exp
,size
,"AUTH-B(%d)",cmd
[1]);
144 case MIFARE_MAGICWUPC1
: snprintf(exp
,size
,"MAGIC WUPC1"); break;
145 case MIFARE_MAGICWUPC2
: snprintf(exp
,size
,"MAGIC WUPC2"); break;
146 case MIFARE_MAGICWIPEC
: snprintf(exp
,size
,"MAGIC WIPEC"); break;
147 case MIFARE_ULC_AUTH_1
: snprintf(exp
,size
,"AUTH "); break;
148 case MIFARE_ULC_AUTH_2
: snprintf(exp
,size
,"AUTH_ANSW"); break;
149 case MIFARE_ULEV1_AUTH
:
151 snprintf(exp
,size
,"PWD-AUTH KEY: 0x%02x%02x%02x%02x", cmd
[1], cmd
[2], cmd
[3], cmd
[4] );
153 snprintf(exp
,size
,"PWD-AUTH");
155 case MIFARE_ULEV1_FASTREAD
:{
156 if ( cmdsize
>=3 && cmd
[2] <= 0xE6)
157 snprintf(exp
,size
,"READ RANGE (%d-%d)",cmd
[1],cmd
[2]);
159 snprintf(exp
,size
,"?");
162 case MIFARE_ULC_WRITE
:{
164 snprintf(exp
,size
,"WRITEBLOCK(%d)",cmd
[1]);
166 snprintf(exp
,size
,"?");
169 case MIFARE_ULEV1_READ_CNT
:{
171 snprintf(exp
,size
,"READ CNT(%d)",cmd
[1]);
173 snprintf(exp
,size
,"?");
176 case MIFARE_ULEV1_INCR_CNT
:{
178 snprintf(exp
,size
,"INCR(%d)",cmd
[1]);
180 snprintf(exp
,size
,"?");
183 case MIFARE_ULEV1_READSIG
: snprintf(exp
,size
,"READ_SIG"); break;
184 case MIFARE_ULEV1_CHECKTEAR
: snprintf(exp
,size
,"CHK_TEARING(%d)",cmd
[1]); break;
185 case MIFARE_ULEV1_VCSL
: snprintf(exp
,size
,"VCSL"); break;
186 default: snprintf(exp
,size
,"?"); break;
191 void annotateMifare(char *exp
, size_t size
, uint8_t* cmd
, uint8_t cmdsize
, uint8_t* parity
, uint8_t paritysize
, bool isResponse
) {
193 if (MifareAuthState
== masNone
) {
194 if (cmdsize
== 9 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT
&& cmd
[1] == 0x70) {
196 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
198 if (cmdsize
== 9 && cmd
[0] == ISO14443A_CMD_ANTICOLL_OR_SELECT_2
&& cmd
[1] == 0x70) {
200 AuthData
.uid
= bytes_to_num(&cmd
[2], 4);
204 switch(MifareAuthState
) {
206 if (cmdsize
== 4 && isResponse
) {
207 snprintf(exp
,size
,"AUTH: nt %s", (AuthData
.first_auth
) ? "" : "(enc)");
208 MifareAuthState
= masNrAr
;
209 if (AuthData
.first_auth
)
210 AuthData
.nt
= bytes_to_num(cmd
, 4);
212 AuthData
.nt_enc
= bytes_to_num(cmd
, 4);
213 AuthData
.nt_enc_par
= parity
[0];
216 MifareAuthState
= masError
;
220 if (cmdsize
== 8 && !isResponse
) {
221 snprintf(exp
,size
,"AUTH: nr ar (enc)");
222 MifareAuthState
= masAt
;
223 AuthData
.nr_enc
= bytes_to_num(cmd
, 4);
224 AuthData
.ar_enc
= bytes_to_num(&cmd
[4], 4);
225 AuthData
.ar_enc_par
= parity
[0] << 4;
228 MifareAuthState
= masError
;
232 if (cmdsize
== 4 && isResponse
) {
233 snprintf(exp
,size
,"AUTH: at (enc)");
234 MifareAuthState
= masFirstData
;
235 AuthData
.at_enc
= bytes_to_num(cmd
, 4);
236 AuthData
.at_enc_par
= parity
[0];
239 MifareAuthState
= masError
;
247 annotateIso14443a(exp
, size
, cmd
, cmdsize
);
251 bool DecodeMifareData(uint8_t *cmd
, uint8_t cmdsize
, bool isResponse
, uint8_t *mfData
, size_t *mfDataLen
) {
257 if (MifareAuthState
== masFirstData
) {
258 if (AuthData
.first_auth
) {
259 uint32_t ks2
= AuthData
.ar_enc
^ prng_successor(AuthData
.nt
, 64);
260 uint32_t ks3
= AuthData
.at_enc
^ prng_successor(AuthData
.nt
, 96);
261 struct Crypto1State
*revstate
= lfsr_recovery64(ks2
, ks3
);
262 lfsr_rollback_word(revstate
, 0, 0);
263 lfsr_rollback_word(revstate
, 0, 0);
264 lfsr_rollback_word(revstate
, AuthData
.nr_enc
, 1);
265 lfsr_rollback_word(revstate
, AuthData
.uid
^ AuthData
.nt
, 0);
268 crypto1_get_lfsr(revstate
, &lfsr
);
269 crypto1_destroy(revstate
);
271 PrintAndLog(" | * | key | probable key:%x%x Prng:%s ks2:%08x ks3:%08x | |",
272 (unsigned int)((lfsr
& 0xFFFFFFFF00000000) >> 32), (unsigned int)(lfsr
& 0xFFFFFFFF),
273 validate_prng_nonce(AuthData
.nt
) ? "WEAK": "HARD",
277 AuthData
.first_auth
= false;
279 printf("uid:%x nt:%x ar_enc:%x at_enc:%x\n", AuthData
.uid
, AuthData
.nt
, AuthData
.ar_enc
, AuthData
.at_enc
);
284 MifareAuthState
= masData
;
288 if (MifareAuthState
== masData
) {
291 return *mfDataLen
> 0;