]> git.zerfleddert.de Git - proxmark3-svn/blob - armsrc/mifareutil.c
fix WaitForResponse (without timeout)
[proxmark3-svn] / armsrc / mifareutil.c
1 //-----------------------------------------------------------------------------
2 // Merlok, May 2011, 2012
3 // Many authors, whom made it possible
4 //
5 // This code is licensed to you under the terms of the GNU GPL, version 2 or,
6 // at your option, any later version. See the LICENSE.txt file for the text of
7 // the license.
8 //-----------------------------------------------------------------------------
9 // Work with mifare cards.
10 //-----------------------------------------------------------------------------
11
12 #include "mifareutil.h"
13
14 #include <string.h>
15 #include <stdbool.h>
16
17 #include "proxmark3.h"
18 #include "apps.h"
19 #include "util.h"
20 #include "parity.h"
21 #include "iso14443crc.h"
22 #include "iso14443a.h"
23 #include "crapto1/crapto1.h"
24 #include "mbedtls/des.h"
25 #include "protocols.h"
26
27 int MF_DBGLEVEL = MF_DBG_INFO;
28
29 // crypto1 helpers
30 void mf_crypto1_decryptEx(struct Crypto1State *pcs, uint8_t *data_in, int len, uint8_t *data_out){
31 uint8_t bt = 0;
32 int i;
33
34 if (len != 1) {
35 for (i = 0; i < len; i++)
36 data_out[i] = crypto1_byte(pcs, 0x00, 0) ^ data_in[i];
37 } else {
38 bt = 0;
39 for (i = 0; i < 4; i++)
40 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data_in[0], i)) << i;
41
42 data_out[0] = bt;
43 }
44 return;
45 }
46
47 void mf_crypto1_decrypt(struct Crypto1State *pcs, uint8_t *data, int len){
48 mf_crypto1_decryptEx(pcs, data, len, data);
49 }
50
51 void mf_crypto1_encryptEx(struct Crypto1State *pcs, uint8_t *data, uint8_t *in, uint16_t len, uint8_t *par) {
52 uint8_t bt = 0;
53 int i;
54 par[0] = 0;
55
56 for (i = 0; i < len; i++) {
57 bt = data[i];
58 data[i] = crypto1_byte(pcs, in==NULL?0x00:in[i], 0) ^ data[i];
59 if((i&0x0007) == 0)
60 par[i>>3] = 0;
61 par[i>>3] |= (((filter(pcs->odd) ^ oddparity8(bt)) & 0x01)<<(7-(i&0x0007)));
62 }
63 return;
64 }
65
66 void mf_crypto1_encrypt(struct Crypto1State *pcs, uint8_t *data, uint16_t len, uint8_t *par) {
67 mf_crypto1_encryptEx(pcs, data, NULL, len, par);
68 }
69
70 uint8_t mf_crypto1_encrypt4bit(struct Crypto1State *pcs, uint8_t data) {
71 uint8_t bt = 0;
72 int i;
73
74 for (i = 0; i < 4; i++)
75 bt |= (crypto1_bit(pcs, 0, 0) ^ BIT(data, i)) << i;
76
77 return bt;
78 }
79
80 // send X byte basic commands
81 int mifare_sendcmd(uint8_t cmd, uint8_t* data, uint8_t data_size, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
82 {
83 uint8_t dcmd[data_size+3];
84 dcmd[0] = cmd;
85 memcpy(dcmd+1,data,data_size);
86 AppendCrc14443a(dcmd, data_size+1);
87 ReaderTransmit(dcmd, sizeof(dcmd), timing);
88 int len = ReaderReceive(answer, answer_parity);
89 if(!len) {
90 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("%02X Cmd failed. Card timeout.", cmd);
91 len = ReaderReceive(answer,answer_parity);
92 //return 0;
93 }
94 return len;
95 }
96
97 // send 2 byte commands
98 int mifare_sendcmd_short(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t data, uint8_t *answer, uint8_t *answer_parity, uint32_t *timing)
99 {
100 uint8_t dcmd[4], ecmd[4];
101 uint16_t pos, res;
102 uint8_t par[1]; // 1 Byte parity is enough here
103 dcmd[0] = cmd;
104 dcmd[1] = data;
105 AppendCrc14443a(dcmd, 2);
106
107 memcpy(ecmd, dcmd, sizeof(dcmd));
108
109 if (crypted) {
110 par[0] = 0;
111 for (pos = 0; pos < 4; pos++)
112 {
113 ecmd[pos] = crypto1_byte(pcs, 0x00, 0) ^ dcmd[pos];
114 par[0] |= (((filter(pcs->odd) ^ oddparity8(dcmd[pos])) & 0x01) << (7-pos));
115 }
116 ReaderTransmitPar(ecmd, sizeof(ecmd), par, timing);
117 } else {
118 ReaderTransmit(dcmd, sizeof(dcmd), timing);
119 }
120
121 int len = ReaderReceive(answer, par);
122
123 if (answer_parity) *answer_parity = par[0];
124
125 if (crypted == CRYPT_ALL) {
126 if (len == 1) {
127 res = 0;
128 for (pos = 0; pos < 4; pos++)
129 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(answer[0], pos)) << pos;
130
131 answer[0] = res;
132
133 } else {
134 for (pos = 0; pos < len; pos++)
135 {
136 answer[pos] = crypto1_byte(pcs, 0x00, 0) ^ answer[pos];
137 }
138 }
139 }
140
141 return len;
142 }
143
144
145 // mifare classic commands
146 int mifare_classic_auth(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested, uint32_t *auth_timeout) {
147
148 return mifare_classic_authex(pcs, uid, blockNo, keyType, ui64Key, isNested, NULL, NULL, auth_timeout);
149 }
150
151
152 int mifare_classic_authex(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t keyType, uint64_t ui64Key, uint8_t isNested, uint32_t *ntptr, uint32_t *timing, uint32_t *auth_timeout) {
153
154 int len;
155 uint32_t pos;
156 uint8_t par[1] = {0x00};
157 byte_t nr[4];
158 uint32_t nt, ntpp; // Supplied tag nonce
159
160 uint8_t mf_nr_ar[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
161 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
162 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
163
164 // Transmit MIFARE_CLASSIC_AUTH
165 len = mifare_sendcmd_short(pcs, isNested, keyType & 0x01 ? MIFARE_AUTH_KEYB : MIFARE_AUTH_KEYA, blockNo, receivedAnswer, receivedAnswerPar, timing);
166 if (MF_DBGLEVEL >= 4) Dbprintf("rand tag nonce len: %x", len);
167 if (len != 4) return 1;
168
169 // "random" reader nonce:
170 nr[0] = 0x55;
171 nr[1] = 0x41;
172 nr[2] = 0x49;
173 nr[3] = 0x92;
174
175 // Save the tag nonce (nt)
176 nt = bytes_to_num(receivedAnswer, 4);
177
178 // ----------------------------- crypto1 create
179 if (isNested)
180 crypto1_destroy(pcs);
181
182 // Init cipher with key
183 crypto1_create(pcs, ui64Key);
184
185 if (isNested == AUTH_NESTED) {
186 // decrypt nt with help of new key
187 nt = crypto1_word(pcs, nt ^ uid, 1) ^ nt;
188 } else {
189 // Load (plain) uid^nt into the cipher
190 crypto1_word(pcs, nt ^ uid, 0);
191 }
192
193 // some statistic
194 if (!ntptr && (MF_DBGLEVEL >= 3))
195 Dbprintf("auth uid: %08x nt: %08x", uid, nt);
196
197 // save Nt
198 if (ntptr)
199 *ntptr = nt;
200
201 // Generate (encrypted) nr+parity by loading it into the cipher (Nr)
202 par[0] = 0;
203 for (pos = 0; pos < 4; pos++) {
204 mf_nr_ar[pos] = crypto1_byte(pcs, nr[pos], 0) ^ nr[pos];
205 par[0] |= (((filter(pcs->odd) ^ oddparity8(nr[pos])) & 0x01) << (7-pos));
206 }
207
208 // Skip 32 bits in pseudo random generator
209 nt = prng_successor(nt,32);
210
211 // ar+parity
212 for (pos = 4; pos < 8; pos++) {
213 nt = prng_successor(nt,8);
214 mf_nr_ar[pos] = crypto1_byte(pcs,0x00,0) ^ (nt & 0xff);
215 par[0] |= (((filter(pcs->odd) ^ oddparity8(nt)) & 0x01) << (7-pos));
216 }
217
218 // Transmit reader nonce and reader answer
219 ReaderTransmitPar(mf_nr_ar, sizeof(mf_nr_ar), par, NULL);
220
221 // Receive 4 byte tag answer
222 uint32_t save_timeout = iso14a_get_timeout(); // save standard timeout
223 if (auth_timeout && *auth_timeout) {
224 iso14a_set_timeout(*auth_timeout); // set timeout for authentication response
225 }
226 uint32_t auth_timeout_start = GetCountSspClk();
227 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
228 iso14a_set_timeout(save_timeout); // restore standard timeout
229 if (!len) {
230 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Card timeout.");
231 return 2;
232 }
233 if (auth_timeout && !*auth_timeout) { // measure time for future authentication response timeout
234 *auth_timeout = (GetCountSspClk() - auth_timeout_start - (len * 9 + 2) * 8) / 8 + 1;
235 }
236
237 ntpp = prng_successor(nt, 32) ^ crypto1_word(pcs, 0, 0);
238
239 if (ntpp != bytes_to_num(receivedAnswer, 4)) {
240 if (MF_DBGLEVEL >= 1) Dbprintf("Authentication failed. Error card response.");
241 return 3;
242 }
243
244 return 0;
245 }
246
247
248 int mifare_classic_readblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData) {
249 // variables
250 int len;
251 uint8_t bt[2];
252
253 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
254 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
255
256 // command MIFARE_CLASSIC_READBLOCK
257 len = mifare_sendcmd_short(pcs, 1, MIFARE_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
258 if (len == 1) {
259 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
260 return 1;
261 }
262 if (len != 18) {
263 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: card timeout. len: %x", len);
264 return 2;
265 }
266
267 memcpy(bt, receivedAnswer + 16, 2);
268 AppendCrc14443a(receivedAnswer, 16);
269 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
270 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd CRC response error.");
271 return 3;
272 }
273
274 memcpy(blockData, receivedAnswer, 16);
275 return 0;
276 }
277
278 // mifare ultralight commands
279 int mifare_ul_ev1_auth(uint8_t *keybytes, uint8_t *pack){
280
281 uint16_t len;
282 uint8_t resp[4];
283 uint8_t respPar[1];
284 uint8_t key[4] = {0x00};
285 memcpy(key, keybytes, 4);
286
287 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
288 Dbprintf("EV1 Auth : %02x%02x%02x%02x", key[0], key[1], key[2], key[3]);
289 len = mifare_sendcmd(MIFARE_ULEV1_AUTH, key, sizeof(key), resp, respPar, NULL);
290 //len = mifare_sendcmd_short_mfuev1auth(NULL, 0, 0x1B, key, resp, respPar, NULL);
291 if (len != 4) {
292 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x %u", resp[0], len);
293 return 0;
294 }
295
296 if (MF_DBGLEVEL >= MF_DBG_EXTENDED)
297 Dbprintf("Auth Resp: %02x%02x%02x%02x", resp[0],resp[1],resp[2],resp[3]);
298
299 memcpy(pack, resp, 4);
300 return 1;
301 }
302
303 int mifare_ultra_auth(uint8_t *keybytes){
304
305 /// 3des2k
306
307 mbedtls_des3_context ctx = { {0} };
308 uint8_t random_a[8] = {1,1,1,1,1,1,1,1};
309 uint8_t random_b[8] = {0x00};
310 uint8_t enc_random_b[8] = {0x00};
311 uint8_t rnd_ab[16] = {0x00};
312 uint8_t IV[8] = {0x00};
313 uint8_t key[16] = {0x00};
314 memcpy(key, keybytes, 16);
315
316 uint16_t len;
317 uint8_t resp[19] = {0x00};
318 uint8_t respPar[3] = {0,0,0};
319
320 // REQUEST AUTHENTICATION
321 len = mifare_sendcmd_short(NULL, 1, MIFARE_ULC_AUTH_1, 0x00, resp, respPar ,NULL);
322 if (len != 11) {
323 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
324 return 0;
325 }
326
327 // tag nonce.
328 memcpy(enc_random_b,resp+1,8);
329
330 // decrypt nonce.
331 // tdes_2key_dec(random_b, enc_random_b, sizeof(random_b), key, IV );
332 mbedtls_des3_set2key_dec(&ctx, key);
333 mbedtls_des3_crypt_cbc(&ctx // des3_context
334 , MBEDTLS_DES_DECRYPT // int mode
335 , sizeof(random_b) // length
336 , IV // iv[8]
337 , enc_random_b // input
338 , random_b // output
339 );
340
341 rol(random_b,8);
342 memcpy(rnd_ab ,random_a,8);
343 memcpy(rnd_ab+8,random_b,8);
344
345 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
346 Dbprintf("enc_B: %02x %02x %02x %02x %02x %02x %02x %02x",
347 enc_random_b[0],enc_random_b[1],enc_random_b[2],enc_random_b[3],enc_random_b[4],enc_random_b[5],enc_random_b[6],enc_random_b[7]);
348
349 Dbprintf(" B: %02x %02x %02x %02x %02x %02x %02x %02x",
350 random_b[0],random_b[1],random_b[2],random_b[3],random_b[4],random_b[5],random_b[6],random_b[7]);
351
352 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
353 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
354
355 Dbprintf("rnd_ab: %02x %02x %02x %02x %02x %02x %02x %02x",
356 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15] );
357 }
358
359 // encrypt out, in, length, key, iv
360 //tdes_2key_enc(rnd_ab, rnd_ab, sizeof(rnd_ab), key, enc_random_b);
361 mbedtls_des3_set2key_enc(&ctx, key);
362 mbedtls_des3_crypt_cbc(&ctx // des3_context
363 , MBEDTLS_DES_ENCRYPT // int mode
364 , sizeof(rnd_ab) // length
365 , enc_random_b // iv[8]
366 , rnd_ab // input
367 , rnd_ab // output
368 );
369
370 //len = mifare_sendcmd_short_mfucauth(NULL, 1, 0xAF, rnd_ab, resp, respPar, NULL);
371 len = mifare_sendcmd(MIFARE_ULC_AUTH_2, rnd_ab, sizeof(rnd_ab), resp, respPar, NULL);
372 if (len != 11) {
373 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", resp[0]);
374 return 0;
375 }
376
377 uint8_t enc_resp[8] = { 0,0,0,0,0,0,0,0 };
378 uint8_t resp_random_a[8] = { 0,0,0,0,0,0,0,0 };
379 memcpy(enc_resp, resp+1, 8);
380
381 // decrypt out, in, length, key, iv
382 // tdes_2key_dec(resp_random_a, enc_resp, 8, key, enc_random_b);
383 mbedtls_des3_set2key_dec(&ctx, key);
384 mbedtls_des3_crypt_cbc(&ctx // des3_context
385 , MBEDTLS_DES_DECRYPT // int mode
386 , 8 // length
387 , enc_random_b // iv[8]
388 , enc_resp // input
389 , resp_random_a // output
390 );
391 if ( memcmp(resp_random_a, random_a, 8) != 0 ) {
392 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("failed authentication");
393 return 0;
394 }
395
396 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
397 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
398 rnd_ab[0],rnd_ab[1],rnd_ab[2],rnd_ab[3],
399 rnd_ab[4],rnd_ab[5],rnd_ab[6],rnd_ab[7]);
400
401 Dbprintf("e_AB: %02x %02x %02x %02x %02x %02x %02x %02x",
402 rnd_ab[8],rnd_ab[9],rnd_ab[10],rnd_ab[11],
403 rnd_ab[12],rnd_ab[13],rnd_ab[14],rnd_ab[15]);
404
405 Dbprintf("a: %02x %02x %02x %02x %02x %02x %02x %02x",
406 random_a[0],random_a[1],random_a[2],random_a[3],
407 random_a[4],random_a[5],random_a[6],random_a[7]);
408
409 Dbprintf("b: %02x %02x %02x %02x %02x %02x %02x %02x",
410 resp_random_a[0],resp_random_a[1],resp_random_a[2],resp_random_a[3],
411 resp_random_a[4],resp_random_a[5],resp_random_a[6],resp_random_a[7]);
412 }
413 return 1;
414 }
415
416
417 #define MFU_MAX_RETRIES 5
418 int mifare_ultra_readblock(uint8_t blockNo, uint8_t *blockData)
419 {
420 uint16_t len;
421 uint8_t bt[2];
422 uint8_t receivedAnswer[MAX_FRAME_SIZE];
423 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
424 uint8_t retries;
425 int result = 0;
426
427 for (retries = 0; retries < MFU_MAX_RETRIES; retries++) {
428 len = mifare_sendcmd_short(NULL, 1, MIFARE_CMD_READBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
429 if (len == 1) {
430 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
431 result = 1;
432 continue;
433 }
434 if (len != 18) {
435 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd Error: card timeout. len: %x", len);
436 result = 2;
437 continue;
438 }
439
440 memcpy(bt, receivedAnswer + 16, 2);
441 AppendCrc14443a(receivedAnswer, 16);
442 if (bt[0] != receivedAnswer[16] || bt[1] != receivedAnswer[17]) {
443 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Cmd CRC response error.");
444 result = 3;
445 continue;
446 }
447
448 // No errors encountered; don't retry
449 result = 0;
450 break;
451 }
452
453 if (result != 0) {
454 Dbprintf("Cmd Error: too many retries; read failed");
455 return result;
456 }
457
458 memcpy(blockData, receivedAnswer, 16);
459 return 0;
460 }
461
462 int mifare_classic_writeblock(struct Crypto1State *pcs, uint32_t uid, uint8_t blockNo, uint8_t *blockData)
463 {
464 // variables
465 uint16_t len, i;
466 uint32_t pos;
467 uint8_t par[3] = {0}; // enough for 18 Bytes to send
468 byte_t res;
469
470 uint8_t d_block[18], d_block_enc[18];
471 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
472 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
473
474 // command MIFARE_CLASSIC_WRITEBLOCK
475 len = mifare_sendcmd_short(pcs, 1, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
476
477 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
478 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
479 return 1;
480 }
481
482 memcpy(d_block, blockData, 16);
483 AppendCrc14443a(d_block, 16);
484
485 // crypto
486 for (pos = 0; pos < 18; pos++)
487 {
488 d_block_enc[pos] = crypto1_byte(pcs, 0x00, 0) ^ d_block[pos];
489 par[pos>>3] |= (((filter(pcs->odd) ^ oddparity8(d_block[pos])) & 0x01) << (7 - (pos&0x0007)));
490 }
491
492 ReaderTransmitPar(d_block_enc, sizeof(d_block_enc), par, NULL);
493
494 // Receive the response
495 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
496
497 res = 0;
498 for (i = 0; i < 4; i++)
499 res |= (crypto1_bit(pcs, 0, 0) ^ BIT(receivedAnswer[0], i)) << i;
500
501 if ((len != 1) || (res != 0x0A)) {
502 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd send data2 Error: %02x", res);
503 return 2;
504 }
505
506 return 0;
507 }
508
509 /* // command not needed, but left for future testing
510 int mifare_ultra_writeblock_compat(uint8_t blockNo, uint8_t *blockData)
511 {
512 uint16_t len;
513 uint8_t par[3] = {0}; // enough for 18 parity bits
514 uint8_t d_block[18] = {0x00};
515 uint8_t receivedAnswer[MAX_FRAME_SIZE];
516 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
517
518 len = mifare_sendcmd_short(NULL, true, MIFARE_CMD_WRITEBLOCK, blockNo, receivedAnswer, receivedAnswerPar, NULL);
519
520 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
521 if (MF_DBGLEVEL >= MF_DBG_ERROR)
522 Dbprintf("Cmd Addr Error: %02x", receivedAnswer[0]);
523 return 1;
524 }
525
526 memcpy(d_block, blockData, 16);
527 AppendCrc14443a(d_block, 16);
528
529 ReaderTransmitPar(d_block, sizeof(d_block), par, NULL);
530
531 len = ReaderReceive(receivedAnswer, receivedAnswerPar);
532
533 if ((len != 1) || (receivedAnswer[0] != 0x0A)) { // 0x0a - ACK
534 if (MF_DBGLEVEL >= MF_DBG_ERROR)
535 Dbprintf("Cmd Data Error: %02x %d", receivedAnswer[0],len);
536 return 2;
537 }
538 return 0;
539 }
540 */
541
542 int mifare_ultra_writeblock(uint8_t blockNo, uint8_t *blockData)
543 {
544 uint16_t len;
545 uint8_t d_block[5] = {0x00};
546 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
547 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
548
549 // command MIFARE_CLASSIC_WRITEBLOCK
550 d_block[0]= blockNo;
551 memcpy(d_block+1,blockData,4);
552 //AppendCrc14443a(d_block, 6);
553
554 len = mifare_sendcmd(0xA2, d_block, sizeof(d_block), receivedAnswer, receivedAnswerPar, NULL);
555
556 if (receivedAnswer[0] != 0x0A) { // 0x0a - ACK
557 if (MF_DBGLEVEL >= MF_DBG_ERROR)
558 Dbprintf("Cmd Send Error: %02x %d", receivedAnswer[0],len);
559 return 1;
560 }
561 return 0;
562 }
563
564 int mifare_classic_halt(struct Crypto1State *pcs, uint32_t uid)
565 {
566 uint16_t len;
567 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
568 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
569
570 len = mifare_sendcmd_short(pcs, pcs == NULL ? false:true, ISO14443A_CMD_HALT, 0x00, receivedAnswer, receivedAnswerPar, NULL);
571 if (len != 0) {
572 if (MF_DBGLEVEL >= MF_DBG_ERROR)
573 Dbprintf("halt error. response len: %x", len);
574 return 1;
575 }
576
577 return 0;
578 }
579
580 int mifare_ultra_halt()
581 {
582 uint16_t len;
583 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
584 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
585
586 len = mifare_sendcmd_short(NULL, true, ISO14443A_CMD_HALT, 0x00, receivedAnswer, receivedAnswerPar, NULL);
587 if (len != 0) {
588 if (MF_DBGLEVEL >= MF_DBG_ERROR)
589 Dbprintf("halt error. response len: %x", len);
590 return 1;
591 }
592 return 0;
593 }
594
595
596 // Mifare Memory Structure: up to 32 Sectors with 4 blocks each (1k and 2k cards),
597 // plus evtl. 8 sectors with 16 blocks each (4k cards)
598 uint8_t NumBlocksPerSector(uint8_t sectorNo)
599 {
600 if (sectorNo < 32)
601 return 4;
602 else
603 return 16;
604 }
605
606 uint8_t FirstBlockOfSector(uint8_t sectorNo)
607 {
608 if (sectorNo < 32)
609 return sectorNo * 4;
610 else
611 return 32*4 + (sectorNo - 32) * 16;
612
613 }
614
615 uint8_t SectorTrailer(uint8_t blockNo)
616 {
617 if (blockNo < 32*4) {
618 return (blockNo | 0x03);
619 } else {
620 return (blockNo | 0x0f);
621 }
622 }
623
624 bool IsSectorTrailer(uint8_t blockNo)
625 {
626 return (blockNo == SectorTrailer(blockNo));
627 }
628
629 // work with emulator memory
630 void emlSetMem(uint8_t *data, int blockNum, int blocksCount) {
631 uint8_t* emCARD = BigBuf_get_EM_addr();
632 memcpy(emCARD + blockNum * 16, data, blocksCount * 16);
633 }
634
635 void emlGetMem(uint8_t *data, int blockNum, int blocksCount) {
636 uint8_t* emCARD = BigBuf_get_EM_addr();
637 memcpy(data, emCARD + blockNum * 16, blocksCount * 16);
638 }
639
640 void emlGetMemBt(uint8_t *data, int bytePtr, int byteCount) {
641 uint8_t* emCARD = BigBuf_get_EM_addr();
642 memcpy(data, emCARD + bytePtr, byteCount);
643 }
644
645 int emlCheckValBl(int blockNum) {
646 uint8_t* emCARD = BigBuf_get_EM_addr();
647 uint8_t* data = emCARD + blockNum * 16;
648
649 if ((data[0] != (data[4] ^ 0xff)) || (data[0] != data[8]) ||
650 (data[1] != (data[5] ^ 0xff)) || (data[1] != data[9]) ||
651 (data[2] != (data[6] ^ 0xff)) || (data[2] != data[10]) ||
652 (data[3] != (data[7] ^ 0xff)) || (data[3] != data[11]) ||
653 (data[12] != (data[13] ^ 0xff)) || (data[12] != data[14]) ||
654 (data[12] != (data[15] ^ 0xff))
655 )
656 return 1;
657 return 0;
658 }
659
660 int emlGetValBl(uint32_t *blReg, uint8_t *blBlock, int blockNum) {
661 uint8_t* emCARD = BigBuf_get_EM_addr();
662 uint8_t* data = emCARD + blockNum * 16;
663
664 if (emlCheckValBl(blockNum)) {
665 return 1;
666 }
667
668 memcpy(blReg, data, 4);
669 *blBlock = data[12];
670 return 0;
671 }
672
673 int emlSetValBl(uint32_t blReg, uint8_t blBlock, int blockNum) {
674 uint8_t* emCARD = BigBuf_get_EM_addr();
675 uint8_t* data = emCARD + blockNum * 16;
676
677 memcpy(data + 0, &blReg, 4);
678 memcpy(data + 8, &blReg, 4);
679 blReg = blReg ^ 0xffffffff;
680 memcpy(data + 4, &blReg, 4);
681
682 data[12] = blBlock;
683 data[13] = blBlock ^ 0xff;
684 data[14] = blBlock;
685 data[15] = blBlock ^ 0xff;
686
687 return 0;
688 }
689
690 uint64_t emlGetKey(int sectorNum, int keyType) {
691 uint8_t key[6];
692 uint8_t* emCARD = BigBuf_get_EM_addr();
693
694 memcpy(key, emCARD + 16 * (FirstBlockOfSector(sectorNum) + NumBlocksPerSector(sectorNum) - 1) + keyType * 10, 6);
695 return bytes_to_num(key, 6);
696 }
697
698 void emlClearMem(void) {
699 int b;
700
701 const uint8_t trailer[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x80, 0x69, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
702 const uint8_t uid[] = {0xe6, 0x84, 0x87, 0xf3, 0x16, 0x88, 0x04, 0x00, 0x46, 0x8e, 0x45, 0x55, 0x4d, 0x70, 0x41, 0x04};
703 uint8_t* emCARD = BigBuf_get_EM_addr();
704
705 memset(emCARD, 0, CARD_MEMORY_SIZE);
706
707 // fill sectors trailer data
708 for(b = 3; b < 256; b<127?(b+=4):(b+=16)) {
709 emlSetMem((uint8_t *)trailer, b , 1);
710 }
711
712 // uid
713 emlSetMem((uint8_t *)uid, 0, 1);
714 return;
715 }
716
717
718 // Mifare desfire commands
719 int mifare_sendcmd_special(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer, uint8_t *answer_parity, uint32_t *timing)
720 {
721 uint8_t dcmd[5] = {0x00};
722 dcmd[0] = cmd;
723 memcpy(dcmd+1,data,2);
724 AppendCrc14443a(dcmd, 3);
725
726 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
727 int len = ReaderReceive(answer, answer_parity);
728 if(!len) {
729 if (MF_DBGLEVEL >= MF_DBG_ERROR)
730 Dbprintf("Authentication failed. Card timeout.");
731 return 1;
732 }
733 return len;
734 }
735
736 int mifare_sendcmd_special2(struct Crypto1State *pcs, uint8_t crypted, uint8_t cmd, uint8_t* data, uint8_t* answer,uint8_t *answer_parity, uint32_t *timing)
737 {
738 uint8_t dcmd[20] = {0x00};
739 dcmd[0] = cmd;
740 memcpy(dcmd+1,data,17);
741 AppendCrc14443a(dcmd, 18);
742
743 ReaderTransmit(dcmd, sizeof(dcmd), NULL);
744 int len = ReaderReceive(answer, answer_parity);
745 if(!len){
746 if (MF_DBGLEVEL >= MF_DBG_ERROR)
747 Dbprintf("Authentication failed. Card timeout.");
748 return 1;
749 }
750 return len;
751 }
752
753 int mifare_desfire_des_auth1(uint32_t uid, uint8_t *blockData){
754
755 int len;
756 // load key, keynumber
757 uint8_t data[2]={0x0a, 0x00};
758 uint8_t receivedAnswer[MAX_FRAME_SIZE];
759 uint8_t receivedAnswerPar[MAX_PARITY_SIZE];
760
761 len = mifare_sendcmd_special(NULL, 1, 0x02, data, receivedAnswer,receivedAnswerPar,NULL);
762 if (len == 1) {
763 if (MF_DBGLEVEL >= MF_DBG_ERROR)
764 Dbprintf("Cmd Error: %02x", receivedAnswer[0]);
765 return 1;
766 }
767
768 if (len == 12) {
769 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
770 Dbprintf("Auth1 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
771 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
772 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
773 receivedAnswer[10],receivedAnswer[11]);
774 }
775 memcpy(blockData, receivedAnswer, 12);
776 return 0;
777 }
778 return 1;
779 }
780
781 int mifare_desfire_des_auth2(uint32_t uid, uint8_t *key, uint8_t *blockData){
782
783 int len;
784 uint8_t data[17] = {0x00};
785 data[0] = 0xAF;
786 memcpy(data+1,key,16);
787
788 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];
789 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];
790
791 len = mifare_sendcmd_special2(NULL, 1, 0x03, data, receivedAnswer, receivedAnswerPar ,NULL);
792
793 if ((receivedAnswer[0] == 0x03) && (receivedAnswer[1] == 0xae)) {
794 if (MF_DBGLEVEL >= MF_DBG_ERROR)
795 Dbprintf("Auth Error: %02x %02x", receivedAnswer[0], receivedAnswer[1]);
796 return 1;
797 }
798
799 if (len == 12){
800 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) {
801 Dbprintf("Auth2 Resp: %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
802 receivedAnswer[0],receivedAnswer[1],receivedAnswer[2],receivedAnswer[3],receivedAnswer[4],
803 receivedAnswer[5],receivedAnswer[6],receivedAnswer[7],receivedAnswer[8],receivedAnswer[9],
804 receivedAnswer[10],receivedAnswer[11]);
805 }
806 memcpy(blockData, receivedAnswer, 12);
807 return 0;
808 }
809 return 1;
810 }
811
812 //-----------------------------------------------------------------------------
813 // MIFARE check keys
814 //
815 //-----------------------------------------------------------------------------
816 // one key check
817 int MifareChkBlockKey(uint8_t *uid, uint32_t *cuid, uint8_t *cascade_levels, uint64_t ui64Key, uint8_t blockNo, uint8_t keyType, uint32_t *auth_timeout, uint8_t debugLevel) {
818
819 struct Crypto1State mpcs = {0, 0};
820 struct Crypto1State *pcs;
821 pcs = &mpcs;
822
823 // Iceman: use piwi's faster nonce collecting part in hardnested.
824 if (*cascade_levels == 0) { // need a full select cycle to get the uid first
825 iso14a_card_select_t card_info;
826 if (!iso14443a_select_card(uid, &card_info, cuid, true, 0, true)) {
827 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card");
828 return 1;
829 }
830 switch (card_info.uidlen) {
831 case 4 : *cascade_levels = 1; break;
832 case 7 : *cascade_levels = 2; break;
833 case 10: *cascade_levels = 3; break;
834 default: break;
835 }
836 } else { // no need for anticollision. We can directly select the card
837 if (!iso14443a_select_card(uid, NULL, NULL, false, *cascade_levels, true)) {
838 if (debugLevel >= 1) Dbprintf("ChkKeys: Can't select card (UID) lvl=%d", *cascade_levels);
839 return 1;
840 }
841 }
842
843 if (mifare_classic_auth(pcs, *cuid, blockNo, keyType, ui64Key, AUTH_FIRST, auth_timeout)) { // authentication failed
844 return 2;
845 } else {
846 mifare_classic_halt(pcs, *cuid);
847 }
848
849 return 0;
850 }
851
852
853 // multi key check
854 int MifareChkBlockKeys(uint8_t *keys, uint8_t keyCount, uint8_t blockNo, uint8_t keyType, uint32_t *auth_timeout, uint8_t debugLevel) {
855
856 uint8_t uid[10];
857 uint32_t cuid = 0;
858 uint8_t cascade_levels = 0;
859 uint64_t ui64Key = 0;
860
861 int retryCount = 0;
862 for (uint8_t i = 0; i < keyCount; i++) {
863
864 ui64Key = bytes_to_num(keys + i * 6, 6);
865 int res = MifareChkBlockKey(uid, &cuid, &cascade_levels, ui64Key, blockNo, keyType, auth_timeout, debugLevel);
866
867 // can't select
868 if (res == 1) {
869 retryCount++;
870 if (retryCount >= 5) {
871 Dbprintf("ChkKeys: block=%d key=%d. Can't select. Exit...", blockNo, keyType);
872 return -1;
873 }
874 --i; // try the same key once again
875
876 SpinDelay(20);
877 // Dbprintf("ChkKeys: block=%d key=%d. Try the same key once again...", blockNo, keyType);
878 continue;
879 }
880
881 // can't authenticate
882 if (res == 2) {
883 retryCount = 0;
884 continue; // can't auth. wrong key.
885 }
886
887 // successful authentication
888 return i + 1;
889 }
890
891 if (BUTTON_PRESS()) {
892 return -2;
893 }
894
895 return 0;
896 }
897
898
899 // multisector multikey check
900 int MifareMultisectorChk(uint8_t *keys, uint8_t keyCount, uint8_t SectorCount, uint8_t keyType, uint32_t *auth_timeout, uint8_t debugLevel, TKeyIndex *keyIndex) {
901 int res = 0;
902
903 // int clk = GetCountSspClk();
904
905 for(int sc = 0; sc < SectorCount; sc++){
906 WDT_HIT();
907
908 int keyAB = keyType;
909 do {
910 res = MifareChkBlockKeys(keys, keyCount, FirstBlockOfSector(sc), keyAB & 0x01, auth_timeout, debugLevel);
911 if (res < 0) {
912 return res;
913 }
914 if (res > 0) {
915 (*keyIndex)[keyAB & 0x01][sc] = res;
916 }
917 } while(--keyAB > 0);
918 }
919
920 // Dbprintf("%d %d", GetCountSspClk() - clk, (GetCountSspClk() - clk)/(SectorCount*keyCount*(keyType==2?2:1)));
921
922 return 1;
923 }
924
925
Impressum, Datenschutz