]> cvs.zerfleddert.de Git - proxmark3-svn/blame_incremental - armsrc/mifarecmd.c
fix 'hf iclass sim'
[proxmark3-svn] / armsrc / mifarecmd.c
... / ...
CommitLineData
1//-----------------------------------------------------------------------------\r
2// Merlok - June 2011, 2012\r
3// Gerhard de Koning Gans - May 2008\r
4// Hagen Fritsch - June 2010\r
5// Midnitesnake - Dec 2013\r
6// Andy Davies - Apr 2014\r
7// Iceman - May 2014\r
8//\r
9// This code is licensed to you under the terms of the GNU GPL, version 2 or,\r
10// at your option, any later version. See the LICENSE.txt file for the text of\r
11// the license.\r
12//-----------------------------------------------------------------------------\r
13// Routines to support ISO 14443 type A.\r
14//-----------------------------------------------------------------------------\r
15\r
16#include "mifarecmd.h"\r
17\r
18#include <stdint.h>\r
19\r
20#include "proxmark3.h"\r
21#include "cmd.h"\r
22#include "crapto1/crapto1.h"\r
23#include "iso14443a.h"\r
24#include "BigBuf.h"\r
25#include "mifareutil.h"\r
26#include "apps.h"\r
27#include "protocols.h"\r
28#include "util.h"\r
29#include "parity.h"\r
30#include "crc.h"\r
31#include "fpgaloader.h"\r
32\r
33#define HARDNESTED_AUTHENTICATION_TIMEOUT 848 // card times out 1ms after wrong authentication (according to NXP documentation)\r
34#define HARDNESTED_PRE_AUTHENTICATION_LEADTIME 400 // some (non standard) cards need a pause after select before they are ready for first authentication\r
35\r
36/*\r
37// the block number for the ISO14443-4 PCB\r
38static uint8_t pcb_blocknum = 0;\r
39// Deselect card by sending a s-block. the crc is precalced for speed\r
40static uint8_t deselect_cmd[] = {0xc2,0xe0,0xb4};\r
41\r
42static void OnSuccess(){\r
43 pcb_blocknum = 0;\r
44 ReaderTransmit(deselect_cmd, 3 , NULL);\r
45 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
46 LEDsoff();\r
47}\r
48*/\r
49\r
50static void OnError(uint8_t reason){\r
51 // pcb_blocknum = 0;\r
52 // ReaderTransmit(deselect_cmd, 3 , NULL);\r
53 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
54 LED_D_OFF();\r
55 cmd_send(CMD_ACK,0,reason,0,0,0);\r
56 LED_A_OFF();\r
57}\r
58\r
59//-----------------------------------------------------------------------------\r
60// Select, Authenticate, Read a MIFARE tag.\r
61// read block\r
62//-----------------------------------------------------------------------------\r
63void MifareReadBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
64{\r
65 LED_A_ON();\r
66\r
67 uint8_t blockNo = arg0;\r
68 uint8_t keyType = arg1;\r
69 uint64_t ui64Key = 0;\r
70 ui64Key = bytes_to_num(datain, 6);\r
71\r
72 byte_t isOK = 0;\r
73 byte_t dataoutbuf[16];\r
74 uint8_t uid[10];\r
75 uint32_t cuid;\r
76 struct Crypto1State mpcs = {0, 0};\r
77 struct Crypto1State *pcs;\r
78 pcs = &mpcs;\r
79\r
80 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
81\r
82 clear_trace();\r
83\r
84 while (true) {\r
85 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
86 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
87 break;\r
88 };\r
89\r
90 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
91 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
92 break;\r
93 };\r
94\r
95 if(mifare_classic_readblock(pcs, cuid, blockNo, dataoutbuf)) {\r
96 if (MF_DBGLEVEL >= 1) Dbprintf("Read block error");\r
97 break;\r
98 };\r
99\r
100 if(mifare_classic_halt(pcs, cuid)) {\r
101 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
102 break;\r
103 };\r
104\r
105 isOK = 1;\r
106 break;\r
107 }\r
108\r
109 // ----------------------------- crypto1 destroy\r
110 crypto1_destroy(pcs);\r
111\r
112 if (MF_DBGLEVEL >= 2) DbpString("READ BLOCK FINISHED");\r
113\r
114 LED_B_ON();\r
115 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16);\r
116 LED_B_OFF();\r
117\r
118 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
119 LEDsoff();\r
120}\r
121\r
122void MifareUC_Auth(uint8_t arg0, uint8_t *keybytes){\r
123\r
124 LED_A_ON();\r
125 bool turnOffField = (arg0 == 1);\r
126\r
127 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
128\r
129 if (!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
130 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
131 OnError(0);\r
132 return;\r
133 };\r
134\r
135 if (!mifare_ultra_auth(keybytes)){\r
136 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication failed");\r
137 OnError(1);\r
138 return;\r
139 }\r
140\r
141 if (turnOffField) {\r
142 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
143 LED_D_OFF();\r
144 }\r
145\r
146 cmd_send(CMD_ACK,1,0,0,0,0);\r
147 LED_A_OFF();\r
148}\r
149\r
150// Arg0 = BlockNo,\r
151// Arg1 = UsePwd bool\r
152// datain = PWD bytes,\r
153void MifareUReadBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
154{\r
155 LED_A_ON();\r
156\r
157 uint8_t blockNo = arg0;\r
158 byte_t dataout[16] = {0x00};\r
159 bool useKey = (arg1 == 1); //UL_C\r
160 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
161\r
162 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
163\r
164 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r
165 if(!len) {\r
166 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%02X)",len);\r
167 OnError(1);\r
168 return;\r
169 }\r
170\r
171 // UL-C authentication\r
172 if (useKey) {\r
173 uint8_t key[16] = {0x00};\r
174 memcpy(key, datain, sizeof(key) );\r
175\r
176 if ( !mifare_ultra_auth(key) ) {\r
177 OnError(1);\r
178 return;\r
179 }\r
180 }\r
181\r
182 // UL-EV1 / NTAG authentication\r
183 if (usePwd) {\r
184 uint8_t pwd[4] = {0x00};\r
185 memcpy(pwd, datain, 4);\r
186 uint8_t pack[4] = {0,0,0,0};\r
187 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
188 OnError(1);\r
189 return;\r
190 }\r
191 }\r
192\r
193 if (mifare_ultra_readblock(blockNo, dataout)) {\r
194 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block error");\r
195 OnError(2);\r
196 return;\r
197 }\r
198\r
199 if (mifare_ultra_halt()) {\r
200 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
201 OnError(3);\r
202 return;\r
203 }\r
204\r
205 cmd_send(CMD_ACK,1,0,0,dataout,16);\r
206 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
207 LED_D_OFF();\r
208 LED_A_OFF();\r
209}\r
210\r
211//-----------------------------------------------------------------------------\r
212// Select, Authenticate, Read a MIFARE tag.\r
213// read sector (data = 4 x 16 bytes = 64 bytes, or 16 x 16 bytes = 256 bytes)\r
214//-----------------------------------------------------------------------------\r
215void MifareReadSector(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
216{\r
217 // params\r
218 uint8_t sectorNo = arg0;\r
219 uint8_t keyType = arg1;\r
220 uint64_t ui64Key = 0;\r
221 ui64Key = bytes_to_num(datain, 6);\r
222\r
223 // variables\r
224 byte_t isOK = 0;\r
225 byte_t dataoutbuf[16 * 16];\r
226 uint8_t uid[10];\r
227 uint32_t cuid;\r
228 struct Crypto1State mpcs = {0, 0};\r
229 struct Crypto1State *pcs;\r
230 pcs = &mpcs;\r
231\r
232 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
233\r
234 clear_trace();\r
235\r
236 LED_A_ON();\r
237 LED_B_OFF();\r
238 LED_C_OFF();\r
239\r
240 isOK = 1;\r
241 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
242 isOK = 0;\r
243 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
244 }\r
245\r
246\r
247 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
248 isOK = 0;\r
249 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
250 }\r
251\r
252 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
253 if(mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf + 16 * blockNo)) {\r
254 isOK = 0;\r
255 if (MF_DBGLEVEL >= 1) Dbprintf("Read sector %2d block %2d error", sectorNo, blockNo);\r
256 break;\r
257 }\r
258 }\r
259\r
260 if(mifare_classic_halt(pcs, cuid)) {\r
261 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
262 }\r
263\r
264 // ----------------------------- crypto1 destroy\r
265 crypto1_destroy(pcs);\r
266\r
267 if (MF_DBGLEVEL >= 2) DbpString("READ SECTOR FINISHED");\r
268\r
269 LED_B_ON();\r
270 cmd_send(CMD_ACK,isOK,0,0,dataoutbuf,16*NumBlocksPerSector(sectorNo));\r
271 LED_B_OFF();\r
272\r
273 // Thats it...\r
274 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
275 LEDsoff();\r
276}\r
277\r
278// arg0 = blockNo (start)\r
279// arg1 = Pages (number of blocks)\r
280// arg2 = useKey\r
281// datain = KEY bytes\r
282void MifareUReadCard(uint8_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
283{\r
284 LED_A_ON();\r
285 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
286\r
287 // free eventually allocated BigBuf memory\r
288 BigBuf_free();\r
289\r
290 // params\r
291 uint8_t blockNo = arg0;\r
292 uint16_t blocks = arg1;\r
293 bool useKey = (arg2 == 1); //UL_C\r
294 bool usePwd = (arg2 == 2); //UL_EV1/NTAG\r
295 uint32_t countblocks = 0;\r
296 uint8_t *dataout = BigBuf_malloc(CARD_MEMORY_SIZE);\r
297 if (dataout == NULL){\r
298 Dbprintf("out of memory");\r
299 OnError(1);\r
300 return;\r
301 }\r
302\r
303 int len = iso14443a_select_card(NULL, NULL, NULL, true, 0, true);\r
304 if (!len) {\r
305 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card (RC:%d)",len);\r
306 OnError(1);\r
307 return;\r
308 }\r
309\r
310 // UL-C authentication\r
311 if (useKey) {\r
312 uint8_t key[16] = {0x00};\r
313 memcpy(key, datain, sizeof(key) );\r
314\r
315 if ( !mifare_ultra_auth(key) ) {\r
316 OnError(1);\r
317 return;\r
318 }\r
319 }\r
320\r
321 // UL-EV1 / NTAG authentication\r
322 if (usePwd) {\r
323 uint8_t pwd[4] = {0x00};\r
324 memcpy(pwd, datain, sizeof(pwd));\r
325 uint8_t pack[4] = {0,0,0,0};\r
326\r
327 if (!mifare_ul_ev1_auth(pwd, pack)){\r
328 OnError(1);\r
329 return;\r
330 }\r
331 }\r
332\r
333 for (int i = 0; i < blocks; i++){\r
334 if ((i*4) + 4 >= CARD_MEMORY_SIZE) {\r
335 Dbprintf("Data exceeds buffer!!");\r
336 break;\r
337 }\r
338\r
339 len = mifare_ultra_readblock(blockNo + i, dataout + 4 * i);\r
340\r
341 if (len) {\r
342 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Read block %d error",i);\r
343 // if no blocks read - error out\r
344 if (i==0){\r
345 OnError(2);\r
346 return;\r
347 } else {\r
348 //stop at last successful read block and return what we got\r
349 break;\r
350 }\r
351 } else {\r
352 countblocks++;\r
353 }\r
354 }\r
355\r
356 len = mifare_ultra_halt();\r
357 if (len) {\r
358 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Halt error");\r
359 OnError(3);\r
360 return;\r
361 }\r
362\r
363 if (MF_DBGLEVEL >= MF_DBG_DEBUG) Dbprintf("Blocks read %d", countblocks);\r
364\r
365 cmd_send(CMD_ACK, 1, countblocks*4, BigBuf_max_traceLen(), 0, 0);\r
366\r
367 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
368 LED_D_OFF();\r
369 BigBuf_free();\r
370 LED_A_OFF();\r
371}\r
372\r
373//-----------------------------------------------------------------------------\r
374// Select, Authenticate, Write a MIFARE tag.\r
375// read block\r
376//-----------------------------------------------------------------------------\r
377void MifareWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t arg2, uint8_t *datain)\r
378{\r
379 // params\r
380 uint8_t blockNo = arg0;\r
381 uint8_t keyType = arg1;\r
382 uint64_t ui64Key = 0;\r
383 byte_t blockdata[16];\r
384\r
385 ui64Key = bytes_to_num(datain, 6);\r
386 memcpy(blockdata, datain + 10, 16);\r
387\r
388 // variables\r
389 byte_t isOK = 0;\r
390 uint8_t uid[10];\r
391 uint32_t cuid;\r
392 struct Crypto1State mpcs = {0, 0};\r
393 struct Crypto1State *pcs;\r
394 pcs = &mpcs;\r
395\r
396 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
397\r
398 clear_trace();\r
399\r
400 LED_A_ON();\r
401 LED_B_OFF();\r
402 LED_C_OFF();\r
403\r
404 while (true) {\r
405 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
406 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
407 break;\r
408 };\r
409\r
410 if(mifare_classic_auth(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST)) {\r
411 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
412 break;\r
413 };\r
414\r
415 if(mifare_classic_writeblock(pcs, cuid, blockNo, blockdata)) {\r
416 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
417 break;\r
418 };\r
419\r
420 if(mifare_classic_halt(pcs, cuid)) {\r
421 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
422 break;\r
423 };\r
424\r
425 isOK = 1;\r
426 break;\r
427 }\r
428\r
429 // ----------------------------- crypto1 destroy\r
430 crypto1_destroy(pcs);\r
431\r
432 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
433\r
434 LED_B_ON();\r
435 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
436 LED_B_OFF();\r
437\r
438\r
439 // Thats it...\r
440 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
441 LEDsoff();\r
442}\r
443\r
444/* // Command not needed but left for future testing\r
445void MifareUWriteBlockCompat(uint8_t arg0, uint8_t *datain)\r
446{\r
447 uint8_t blockNo = arg0;\r
448 byte_t blockdata[16] = {0x00};\r
449\r
450 memcpy(blockdata, datain, 16);\r
451\r
452 uint8_t uid[10] = {0x00};\r
453\r
454 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
455\r
456 clear_trace();\r
457 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
458\r
459 if(!iso14443a_select_card(uid, NULL, NULL, true, 0)) {\r
460 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
461 OnError(0);\r
462 return;\r
463 };\r
464\r
465 if(mifare_ultra_writeblock_compat(blockNo, blockdata)) {\r
466 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
467 OnError(0);\r
468 return; };\r
469\r
470 if(mifare_ultra_halt()) {\r
471 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
472 OnError(0);\r
473 return;\r
474 };\r
475\r
476 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
477\r
478 cmd_send(CMD_ACK,1,0,0,0,0);\r
479 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
480 LEDsoff();\r
481}\r
482*/\r
483\r
484// Arg0 : Block to write to.\r
485// Arg1 : 0 = use no authentication.\r
486// 1 = use 0x1A authentication.\r
487// 2 = use 0x1B authentication.\r
488// datain : 4 first bytes is data to be written.\r
489// : 4/16 next bytes is authentication key.\r
490void MifareUWriteBlock(uint8_t arg0, uint8_t arg1, uint8_t *datain)\r
491{\r
492 uint8_t blockNo = arg0;\r
493 bool useKey = (arg1 == 1); //UL_C\r
494 bool usePwd = (arg1 == 2); //UL_EV1/NTAG\r
495 byte_t blockdata[4] = {0x00};\r
496\r
497 memcpy(blockdata, datain,4);\r
498\r
499 LEDsoff();\r
500 LED_A_ON();\r
501 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
502\r
503 clear_trace();\r
504\r
505 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
506 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
507 OnError(0);\r
508 return;\r
509 };\r
510\r
511 // UL-C authentication\r
512 if ( useKey ) {\r
513 uint8_t key[16] = {0x00};\r
514 memcpy(key, datain+4, sizeof(key) );\r
515\r
516 if ( !mifare_ultra_auth(key) ) {\r
517 OnError(1);\r
518 return;\r
519 }\r
520 }\r
521\r
522 // UL-EV1 / NTAG authentication\r
523 if (usePwd) {\r
524 uint8_t pwd[4] = {0x00};\r
525 memcpy(pwd, datain+4, 4);\r
526 uint8_t pack[4] = {0,0,0,0};\r
527 if (!mifare_ul_ev1_auth(pwd, pack)) {\r
528 OnError(1);\r
529 return;\r
530 }\r
531 }\r
532\r
533 if(mifare_ultra_writeblock(blockNo, blockdata)) {\r
534 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
535 OnError(0);\r
536 return;\r
537 };\r
538\r
539 if(mifare_ultra_halt()) {\r
540 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
541 OnError(0);\r
542 return;\r
543 };\r
544\r
545 if (MF_DBGLEVEL >= 2) DbpString("WRITE BLOCK FINISHED");\r
546\r
547 cmd_send(CMD_ACK,1,0,0,0,0);\r
548 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
549 LEDsoff();\r
550}\r
551\r
552void MifareUSetPwd(uint8_t arg0, uint8_t *datain){\r
553\r
554 uint8_t pwd[16] = {0x00};\r
555 byte_t blockdata[4] = {0x00};\r
556\r
557 memcpy(pwd, datain, 16);\r
558\r
559 LED_A_ON(); LED_B_OFF(); LED_C_OFF();\r
560 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
561\r
562 clear_trace();\r
563\r
564 if(!iso14443a_select_card(NULL, NULL, NULL, true, 0, true)) {\r
565 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
566 OnError(0);\r
567 return;\r
568 };\r
569\r
570 blockdata[0] = pwd[7];\r
571 blockdata[1] = pwd[6];\r
572 blockdata[2] = pwd[5];\r
573 blockdata[3] = pwd[4];\r
574 if(mifare_ultra_writeblock( 44, blockdata)) {\r
575 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
576 OnError(44);\r
577 return;\r
578 };\r
579\r
580 blockdata[0] = pwd[3];\r
581 blockdata[1] = pwd[2];\r
582 blockdata[2] = pwd[1];\r
583 blockdata[3] = pwd[0];\r
584 if(mifare_ultra_writeblock( 45, blockdata)) {\r
585 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
586 OnError(45);\r
587 return;\r
588 };\r
589\r
590 blockdata[0] = pwd[15];\r
591 blockdata[1] = pwd[14];\r
592 blockdata[2] = pwd[13];\r
593 blockdata[3] = pwd[12];\r
594 if(mifare_ultra_writeblock( 46, blockdata)) {\r
595 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
596 OnError(46);\r
597 return;\r
598 };\r
599\r
600 blockdata[0] = pwd[11];\r
601 blockdata[1] = pwd[10];\r
602 blockdata[2] = pwd[9];\r
603 blockdata[3] = pwd[8];\r
604 if(mifare_ultra_writeblock( 47, blockdata)) {\r
605 if (MF_DBGLEVEL >= 1) Dbprintf("Write block error");\r
606 OnError(47);\r
607 return;\r
608 };\r
609\r
610 if(mifare_ultra_halt()) {\r
611 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
612 OnError(0);\r
613 return;\r
614 };\r
615\r
616 cmd_send(CMD_ACK,1,0,0,0,0);\r
617 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
618 LEDsoff();\r
619}\r
620\r
621// Return 1 if the nonce is invalid else return 0\r
622int valid_nonce(uint32_t Nt, uint32_t NtEnc, uint32_t Ks1, uint8_t *parity) {\r
623 return ((oddparity8((Nt >> 24) & 0xFF) == ((parity[0]) ^ oddparity8((NtEnc >> 24) & 0xFF) ^ BIT(Ks1,16))) & \\r
624 (oddparity8((Nt >> 16) & 0xFF) == ((parity[1]) ^ oddparity8((NtEnc >> 16) & 0xFF) ^ BIT(Ks1,8))) & \\r
625 (oddparity8((Nt >> 8) & 0xFF) == ((parity[2]) ^ oddparity8((NtEnc >> 8) & 0xFF) ^ BIT(Ks1,0)))) ? 1 : 0;\r
626}\r
627\r
628\r
629//-----------------------------------------------------------------------------\r
630// acquire encrypted nonces in order to perform the attack described in\r
631// Carlo Meijer, Roel Verdult, "Ciphertext-only Cryptanalysis on Hardened\r
632// Mifare Classic Cards" in Proceedings of the 22nd ACM SIGSAC Conference on\r
633// Computer and Communications Security, 2015\r
634//-----------------------------------------------------------------------------\r
635void MifareAcquireEncryptedNonces(uint32_t arg0, uint32_t arg1, uint32_t flags, uint8_t *datain)\r
636{\r
637 uint64_t ui64Key = 0;\r
638 uint8_t uid[10];\r
639 uint32_t cuid;\r
640 uint8_t cascade_levels = 0;\r
641 struct Crypto1State mpcs = {0, 0};\r
642 struct Crypto1State *pcs;\r
643 pcs = &mpcs;\r
644 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
645 int16_t isOK = 0;\r
646 uint8_t par_enc[1];\r
647 uint8_t nt_par_enc = 0;\r
648 uint8_t buf[USB_CMD_DATA_SIZE];\r
649 uint32_t timeout;\r
650\r
651 uint8_t blockNo = arg0 & 0xff;\r
652 uint8_t keyType = (arg0 >> 8) & 0xff;\r
653 uint8_t targetBlockNo = arg1 & 0xff;\r
654 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
655 ui64Key = bytes_to_num(datain, 6);\r
656 bool initialize = flags & 0x0001;\r
657 bool slow = flags & 0x0002;\r
658 bool field_off = flags & 0x0004;\r
659\r
660 LED_A_ON();\r
661 LED_C_OFF();\r
662\r
663 if (initialize) {\r
664 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
665 clear_trace();\r
666 set_tracing(true);\r
667 }\r
668\r
669 LED_C_ON();\r
670\r
671 uint16_t num_nonces = 0;\r
672 bool have_uid = false;\r
673 for (uint16_t i = 0; i <= USB_CMD_DATA_SIZE - 9; ) {\r
674\r
675 // Test if the action was cancelled\r
676 if(BUTTON_PRESS()) {\r
677 isOK = 2;\r
678 field_off = true;\r
679 break;\r
680 }\r
681\r
682 if (!have_uid) { // need a full select cycle to get the uid first\r
683 iso14a_card_select_t card_info;\r
684 if(!iso14443a_select_card(uid, &card_info, &cuid, true, 0, true)) {\r
685 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (ALL)");\r
686 continue;\r
687 }\r
688 switch (card_info.uidlen) {\r
689 case 4 : cascade_levels = 1; break;\r
690 case 7 : cascade_levels = 2; break;\r
691 case 10: cascade_levels = 3; break;\r
692 default: break;\r
693 }\r
694 have_uid = true;\r
695 } else { // no need for anticollision. We can directly select the card\r
696 if(!iso14443a_select_card(uid, NULL, NULL, false, cascade_levels, true)) {\r
697 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Can't select card (UID)");\r
698 continue;\r
699 }\r
700 }\r
701\r
702 if (slow) {\r
703 timeout = GetCountSspClk() + HARDNESTED_PRE_AUTHENTICATION_LEADTIME;\r
704 while(GetCountSspClk() < timeout);\r
705 }\r
706\r
707 uint32_t nt1;\r
708 if (mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, NULL)) {\r
709 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth1 error");\r
710 continue;\r
711 }\r
712\r
713 // nested authentication\r
714 uint16_t len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par_enc, NULL);\r
715 if (len != 4) {\r
716 if (MF_DBGLEVEL >= 1) Dbprintf("AcquireNonces: Auth2 error len=%d", len);\r
717 continue;\r
718 }\r
719\r
720 // send an incomplete dummy response in order to trigger the card's authentication failure timeout\r
721 uint8_t dummy_answer[1] = {0};\r
722 ReaderTransmit(dummy_answer, 1, NULL);\r
723\r
724 timeout = GetCountSspClk() + HARDNESTED_AUTHENTICATION_TIMEOUT;\r
725\r
726 num_nonces++;\r
727 if (num_nonces % 2) {\r
728 memcpy(buf+i, receivedAnswer, 4);\r
729 nt_par_enc = par_enc[0] & 0xf0;\r
730 } else {\r
731 nt_par_enc |= par_enc[0] >> 4;\r
732 memcpy(buf+i+4, receivedAnswer, 4);\r
733 memcpy(buf+i+8, &nt_par_enc, 1);\r
734 i += 9;\r
735 }\r
736\r
737 // wait for the card to become ready again\r
738 while(GetCountSspClk() < timeout);\r
739\r
740 }\r
741\r
742 LED_C_OFF();\r
743\r
744 crypto1_destroy(pcs);\r
745\r
746 LED_B_ON();\r
747 cmd_send(CMD_ACK, isOK, cuid, num_nonces, buf, sizeof(buf));\r
748 LED_B_OFF();\r
749\r
750 if (MF_DBGLEVEL >= 3) DbpString("AcquireEncryptedNonces finished");\r
751\r
752 if (field_off) {\r
753 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
754 LEDsoff();\r
755 }\r
756}\r
757\r
758\r
759//-----------------------------------------------------------------------------\r
760// MIFARE nested authentication.\r
761//\r
762//-----------------------------------------------------------------------------\r
763void MifareNested(uint32_t arg0, uint32_t arg1, uint32_t calibrate, uint8_t *datain)\r
764{\r
765 // params\r
766 uint8_t blockNo = arg0 & 0xff;\r
767 uint8_t keyType = (arg0 >> 8) & 0xff;\r
768 uint8_t targetBlockNo = arg1 & 0xff;\r
769 uint8_t targetKeyType = (arg1 >> 8) & 0xff;\r
770 uint64_t ui64Key = 0;\r
771\r
772 ui64Key = bytes_to_num(datain, 6);\r
773\r
774 // variables\r
775 uint16_t rtr, i, j, len;\r
776 uint16_t davg;\r
777 static uint16_t dmin, dmax;\r
778 uint8_t uid[10];\r
779 uint32_t cuid, nt1, nt2, nttmp, nttest, ks1;\r
780 uint8_t par[1];\r
781 uint32_t target_nt[2], target_ks[2];\r
782\r
783 uint8_t par_array[4];\r
784 uint16_t ncount = 0;\r
785 struct Crypto1State mpcs = {0, 0};\r
786 struct Crypto1State *pcs;\r
787 pcs = &mpcs;\r
788 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
789\r
790 uint32_t auth1_time, auth2_time;\r
791 static uint16_t delta_time;\r
792\r
793 LED_A_ON();\r
794 LED_C_OFF();\r
795 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
796\r
797 // free eventually allocated BigBuf memory\r
798 BigBuf_free();\r
799\r
800 if (calibrate) clear_trace();\r
801 set_tracing(true);\r
802\r
803 // statistics on nonce distance\r
804 int16_t isOK = 0;\r
805 #define NESTED_MAX_TRIES 12\r
806 uint16_t unsuccessfull_tries = 0;\r
807 if (calibrate) { // for first call only. Otherwise reuse previous calibration\r
808 LED_B_ON();\r
809 WDT_HIT();\r
810\r
811 davg = dmax = 0;\r
812 dmin = 2000;\r
813 delta_time = 0;\r
814\r
815 for (rtr = 0; rtr < 17; rtr++) {\r
816\r
817 // Test if the action was cancelled\r
818 if(BUTTON_PRESS()) {\r
819 isOK = -2;\r
820 break;\r
821 }\r
822\r
823 // prepare next select. No need to power down the card.\r
824 if(mifare_classic_halt(pcs, cuid)) {\r
825 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
826 rtr--;\r
827 continue;\r
828 }\r
829\r
830 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
831 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
832 rtr--;\r
833 continue;\r
834 };\r
835\r
836 auth1_time = 0;\r
837 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
838 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
839 rtr--;\r
840 continue;\r
841 };\r
842\r
843 if (delta_time) {\r
844 auth2_time = auth1_time + delta_time;\r
845 } else {\r
846 auth2_time = 0;\r
847 }\r
848 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_NESTED, &nt2, &auth2_time)) {\r
849 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error");\r
850 rtr--;\r
851 continue;\r
852 };\r
853\r
854 nttmp = prng_successor(nt1, 100); //NXP Mifare is typical around 840,but for some unlicensed/compatible mifare card this can be 160\r
855 for (i = 101; i < 1200; i++) {\r
856 nttmp = prng_successor(nttmp, 1);\r
857 if (nttmp == nt2) break;\r
858 }\r
859\r
860 if (i != 1200) {\r
861 if (rtr != 0) {\r
862 davg += i;\r
863 dmin = MIN(dmin, i);\r
864 dmax = MAX(dmax, i);\r
865 }\r
866 else {\r
867 delta_time = auth2_time - auth1_time + 32; // allow some slack for proper timing\r
868 }\r
869 if (MF_DBGLEVEL >= 3) Dbprintf("Nested: calibrating... ntdist=%d", i);\r
870 } else {\r
871 unsuccessfull_tries++;\r
872 if (unsuccessfull_tries > NESTED_MAX_TRIES) { // card isn't vulnerable to nested attack (random numbers are not predictable)\r
873 isOK = -3;\r
874 }\r
875 }\r
876 }\r
877\r
878 davg = (davg + (rtr - 1)/2) / (rtr - 1);\r
879\r
880 if (MF_DBGLEVEL >= 3) Dbprintf("rtr=%d isOK=%d min=%d max=%d avg=%d, delta_time=%d", rtr, isOK, dmin, dmax, davg, delta_time);\r
881\r
882 dmin = davg - 2;\r
883 dmax = davg + 2;\r
884\r
885 LED_B_OFF();\r
886\r
887 }\r
888 // -------------------------------------------------------------------------------------------------\r
889\r
890 LED_C_ON();\r
891\r
892 // get crypted nonces for target sector\r
893 for(i=0; i < 2 && !isOK; i++) { // look for exactly two different nonces\r
894\r
895 target_nt[i] = 0;\r
896 while(target_nt[i] == 0) { // continue until we have an unambiguous nonce\r
897\r
898 // prepare next select. No need to power down the card.\r
899 if(mifare_classic_halt(pcs, cuid)) {\r
900 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Halt error");\r
901 continue;\r
902 }\r
903\r
904 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
905 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Can't select card");\r
906 continue;\r
907 };\r
908\r
909 auth1_time = 0;\r
910 if(mifare_classic_authex(pcs, cuid, blockNo, keyType, ui64Key, AUTH_FIRST, &nt1, &auth1_time)) {\r
911 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth1 error");\r
912 continue;\r
913 };\r
914\r
915 // nested authentication\r
916 auth2_time = auth1_time + delta_time;\r
917 len = mifare_sendcmd_short(pcs, AUTH_NESTED, 0x60 + (targetKeyType & 0x01), targetBlockNo, receivedAnswer, par, &auth2_time);\r
918 if (len != 4) {\r
919 if (MF_DBGLEVEL >= 1) Dbprintf("Nested: Auth2 error len=%d", len);\r
920 continue;\r
921 };\r
922\r
923 nt2 = bytes_to_num(receivedAnswer, 4);\r
924 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: Testing nt1=%08x nt2enc=%08x nt2par=%02x", i+1, nt1, nt2, par[0]);\r
925\r
926 // Parity validity check\r
927 for (j = 0; j < 4; j++) {\r
928 par_array[j] = (oddparity8(receivedAnswer[j]) != ((par[0] >> (7-j)) & 0x01));\r
929 }\r
930\r
931 ncount = 0;\r
932 nttest = prng_successor(nt1, dmin - 1);\r
933 for (j = dmin; j < dmax + 1; j++) {\r
934 nttest = prng_successor(nttest, 1);\r
935 ks1 = nt2 ^ nttest;\r
936\r
937 if (valid_nonce(nttest, nt2, ks1, par_array)){\r
938 if (ncount > 0) { // we are only interested in disambiguous nonces, try again\r
939 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (ambigous), ntdist=%d", i+1, j);\r
940 target_nt[i] = 0;\r
941 break;\r
942 }\r
943 target_nt[i] = nttest;\r
944 target_ks[i] = ks1;\r
945 ncount++;\r
946 if (i == 1 && target_nt[1] == target_nt[0]) { // we need two different nonces\r
947 target_nt[i] = 0;\r
948 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#2: dismissed (= nonce#1), ntdist=%d", j);\r
949 break;\r
950 }\r
951 if (MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: valid, ntdist=%d", i+1, j);\r
952 }\r
953 }\r
954 if (target_nt[i] == 0 && j == dmax+1 && MF_DBGLEVEL >= 3) Dbprintf("Nonce#%d: dismissed (all invalid)", i+1);\r
955 }\r
956 }\r
957\r
958 LED_C_OFF();\r
959\r
960 // ----------------------------- crypto1 destroy\r
961 crypto1_destroy(pcs);\r
962\r
963 byte_t buf[4 + 4 * 4];\r
964 memcpy(buf, &cuid, 4);\r
965 memcpy(buf+4, &target_nt[0], 4);\r
966 memcpy(buf+8, &target_ks[0], 4);\r
967 memcpy(buf+12, &target_nt[1], 4);\r
968 memcpy(buf+16, &target_ks[1], 4);\r
969\r
970 LED_B_ON();\r
971 cmd_send(CMD_ACK, isOK, 0, targetBlockNo + (targetKeyType * 0x100), buf, sizeof(buf));\r
972 LED_B_OFF();\r
973\r
974 if (MF_DBGLEVEL >= 3) DbpString("NESTED FINISHED");\r
975\r
976 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
977 LEDsoff();\r
978}\r
979\r
980//-----------------------------------------------------------------------------\r
981// MIFARE check keys. key count up to 85.\r
982//\r
983//-----------------------------------------------------------------------------\r
984void MifareChkKeys(uint16_t arg0, uint16_t arg1, uint8_t arg2, uint8_t *datain)\r
985{\r
986 uint8_t blockNo = arg0 & 0xff;\r
987 uint8_t keyType = (arg0 >> 8) & 0xff;\r
988 bool clearTrace = arg1 & 0x01;\r
989 bool multisectorCheck = arg1 & 0x02;\r
990 uint8_t set14aTimeout = (arg1 >> 8) & 0xff;\r
991 uint8_t keyCount = arg2;\r
992\r
993 LED_A_ON();\r
994\r
995 // clear debug level\r
996 int OLD_MF_DBGLEVEL = MF_DBGLEVEL;\r
997 MF_DBGLEVEL = MF_DBG_NONE;\r
998\r
999 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1000\r
1001 if (clearTrace) {\r
1002 clear_trace();\r
1003 }\r
1004 set_tracing(true);\r
1005\r
1006 if (set14aTimeout){\r
1007 iso14a_set_timeout(set14aTimeout * 10); // timeout: ms = x/106 35-minimum, 50-OK 106-recommended 500-safe\r
1008 }\r
1009\r
1010 if (multisectorCheck) {\r
1011 TKeyIndex keyIndex = {{0}};\r
1012 uint8_t sectorCnt = blockNo;\r
1013 int res = MifareMultisectorChk(datain, keyCount, sectorCnt, keyType, OLD_MF_DBGLEVEL, &keyIndex);\r
1014\r
1015 if (res >= 0) {\r
1016 cmd_send(CMD_ACK, 1, 0, 0, keyIndex, 80);\r
1017 } else {\r
1018 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
1019 }\r
1020 } else { \r
1021 int res = MifareChkBlockKeys(datain, keyCount, blockNo, keyType, OLD_MF_DBGLEVEL);\r
1022 \r
1023 if (res > 0) {\r
1024 cmd_send(CMD_ACK, 1, 0, 0, datain + (res - 1) * 6, 6);\r
1025 } else {\r
1026 cmd_send(CMD_ACK, 0, 0, 0, NULL, 0);\r
1027 }\r
1028 }\r
1029\r
1030 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1031 LED_D_OFF();\r
1032\r
1033 // restore debug level\r
1034 MF_DBGLEVEL = OLD_MF_DBGLEVEL;\r
1035 \r
1036 LED_A_OFF();\r
1037}\r
1038\r
1039\r
1040//-----------------------------------------------------------------------------\r
1041// MIFARE Personalize UID. Only for Mifare Classic EV1 7Byte UID\r
1042//-----------------------------------------------------------------------------\r
1043void MifarePersonalizeUID(uint8_t keyType, uint8_t perso_option, uint8_t *data) {\r
1044\r
1045 uint8_t uid[10];\r
1046 uint32_t cuid;\r
1047 struct Crypto1State mpcs = {0, 0};\r
1048 struct Crypto1State *pcs;\r
1049 pcs = &mpcs;\r
1050\r
1051 LED_A_ON();\r
1052 clear_trace();\r
1053\r
1054 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1055\r
1056 bool isOK = false;\r
1057 while (true) {\r
1058 if (!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1059 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1060 break;\r
1061 }\r
1062\r
1063 uint8_t block_number = 0;\r
1064 uint64_t key = bytes_to_num(data, 6);\r
1065 if (mifare_classic_auth(pcs, cuid, block_number, keyType, key, AUTH_FIRST)) {\r
1066 if (MF_DBGLEVEL >= 1) Dbprintf("Auth error");\r
1067 break;\r
1068 }\r
1069\r
1070 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1071 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1072 int len = mifare_sendcmd_short(pcs, true, MIFARE_EV1_PERSONAL_UID, perso_option, receivedAnswer, receivedAnswerPar, NULL);\r
1073 if (len != 1 || receivedAnswer[0] != CARD_ACK) {\r
1074 if (MF_DBGLEVEL >= 1) Dbprintf("Cmd Error: %02x", receivedAnswer[0]);\r
1075 break;;\r
1076 }\r
1077 isOK = true;\r
1078 break;\r
1079 }\r
1080\r
1081 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1082 LED_D_OFF();\r
1083\r
1084 crypto1_destroy(pcs);\r
1085\r
1086 if (MF_DBGLEVEL >= 2) DbpString("PERSONALIZE UID FINISHED");\r
1087\r
1088 cmd_send(CMD_ACK, isOK, 0, 0, NULL, 0);\r
1089\r
1090 LED_A_OFF();\r
1091}\r
1092\r
1093//-----------------------------------------------------------------------------\r
1094// MIFARE commands set debug level\r
1095//\r
1096//-----------------------------------------------------------------------------\r
1097void MifareSetDbgLvl(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1098 MF_DBGLEVEL = arg0;\r
1099 Dbprintf("Debug level: %d", MF_DBGLEVEL);\r
1100}\r
1101\r
1102//-----------------------------------------------------------------------------\r
1103// Work with emulator memory\r
1104//\r
1105// Note: we call FpgaDownloadAndGo(FPGA_BITSTREAM_HF) here although FPGA is not\r
1106// involved in dealing with emulator memory. But if it is called later, it might\r
1107// destroy the Emulator Memory.\r
1108//-----------------------------------------------------------------------------\r
1109\r
1110void MifareEMemClr(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1111 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1112 emlClearMem();\r
1113}\r
1114\r
1115void MifareEMemSet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1116 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1117 emlSetMem(datain, arg0, arg1); // data, block num, blocks count\r
1118}\r
1119\r
1120void MifareEMemGet(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1121 FpgaDownloadAndGo(FPGA_BITSTREAM_HF);\r
1122 byte_t buf[USB_CMD_DATA_SIZE];\r
1123 emlGetMem(buf, arg0, arg1); // data, block num, blocks count (max 4)\r
1124\r
1125 LED_B_ON();\r
1126 cmd_send(CMD_ACK,arg0,arg1,0,buf,USB_CMD_DATA_SIZE);\r
1127 LED_B_OFF();\r
1128}\r
1129\r
1130//-----------------------------------------------------------------------------\r
1131// Load a card into the emulator memory\r
1132//\r
1133//-----------------------------------------------------------------------------\r
1134void MifareECardLoad(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1135 uint8_t numSectors = arg0;\r
1136 uint8_t keyType = arg1;\r
1137 uint64_t ui64Key = 0;\r
1138 uint32_t cuid;\r
1139 struct Crypto1State mpcs = {0, 0};\r
1140 struct Crypto1State *pcs;\r
1141 pcs = &mpcs;\r
1142\r
1143 // variables\r
1144 byte_t dataoutbuf[16];\r
1145 byte_t dataoutbuf2[16];\r
1146 uint8_t uid[10];\r
1147\r
1148 LED_A_ON();\r
1149 LED_B_OFF();\r
1150 LED_C_OFF();\r
1151 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1152\r
1153 clear_trace();\r
1154 set_tracing(false);\r
1155\r
1156 bool isOK = true;\r
1157\r
1158 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1159 isOK = false;\r
1160 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1161 }\r
1162\r
1163 for (uint8_t sectorNo = 0; isOK && sectorNo < numSectors; sectorNo++) {\r
1164 ui64Key = emlGetKey(sectorNo, keyType);\r
1165 if (sectorNo == 0){\r
1166 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_FIRST)) {\r
1167 isOK = false;\r
1168 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth error", sectorNo);\r
1169 break;\r
1170 }\r
1171 } else {\r
1172 if(isOK && mifare_classic_auth(pcs, cuid, FirstBlockOfSector(sectorNo), keyType, ui64Key, AUTH_NESTED)) {\r
1173 isOK = false;\r
1174 if (MF_DBGLEVEL >= 1) Dbprintf("Sector[%2d]. Auth nested error", sectorNo);\r
1175 break;\r
1176 }\r
1177 }\r
1178\r
1179 for (uint8_t blockNo = 0; isOK && blockNo < NumBlocksPerSector(sectorNo); blockNo++) {\r
1180 if(isOK && mifare_classic_readblock(pcs, cuid, FirstBlockOfSector(sectorNo) + blockNo, dataoutbuf)) {\r
1181 isOK = false;\r
1182 if (MF_DBGLEVEL >= 1) Dbprintf("Error reading sector %2d block %2d", sectorNo, blockNo);\r
1183 break;\r
1184 };\r
1185 if (isOK) {\r
1186 if (blockNo < NumBlocksPerSector(sectorNo) - 1) {\r
1187 emlSetMem(dataoutbuf, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1188 } else { // sector trailer, keep the keys, set only the AC\r
1189 emlGetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1190 memcpy(&dataoutbuf2[6], &dataoutbuf[6], 4);\r
1191 emlSetMem(dataoutbuf2, FirstBlockOfSector(sectorNo) + blockNo, 1);\r
1192 }\r
1193 }\r
1194 }\r
1195\r
1196 }\r
1197\r
1198 if(mifare_classic_halt(pcs, cuid)) {\r
1199 if (MF_DBGLEVEL >= 1) Dbprintf("Halt error");\r
1200 };\r
1201\r
1202 // ----------------------------- crypto1 destroy\r
1203 crypto1_destroy(pcs);\r
1204\r
1205 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1206 LEDsoff();\r
1207\r
1208 if (MF_DBGLEVEL >= 2) DbpString("EMUL FILL SECTORS FINISHED");\r
1209\r
1210}\r
1211\r
1212\r
1213//-----------------------------------------------------------------------------\r
1214// Work with "magic Chinese" card (email him: ouyangweidaxian@live.cn)\r
1215//\r
1216//-----------------------------------------------------------------------------\r
1217\r
1218static bool isBlockTrailer(int blockN) {\r
1219 if (blockN >= 0 && blockN < 128) {\r
1220 return ((blockN & 0x03) == 0x03);\r
1221 }\r
1222 if (blockN >= 128 && blockN <= 256) {\r
1223 return ((blockN & 0x0F) == 0x0F);\r
1224 }\r
1225 return false;\r
1226}\r
1227\r
1228void MifareCWipe(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1229 // var\r
1230 byte_t isOK = 0;\r
1231 uint32_t numBlocks = arg0;\r
1232 // cmdParams:\r
1233 // bit 0 - wipe gen1a\r
1234 // bit 1 - fill card with default data\r
1235 // bit 2 - gen1a = 0, gen1b = 1\r
1236 uint8_t cmdParams = arg1;\r
1237 bool needWipe = cmdParams & 0x01;\r
1238 bool needFill = cmdParams & 0x02;\r
1239 bool gen1b = cmdParams & 0x04;\r
1240\r
1241 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1242 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1243\r
1244 uint8_t block0[16] = {0x01, 0x02, 0x03, 0x04, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xBE, 0xAF};\r
1245 uint8_t block1[16] = {0x00};\r
1246 uint8_t blockK[16] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x08, 0x77, 0x8F, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};\r
1247 uint8_t d_block[18] = {0x00};\r
1248\r
1249 // card commands\r
1250 uint8_t wupC1[] = { 0x40 };\r
1251 uint8_t wupC2[] = { 0x43 };\r
1252 uint8_t wipeC[] = { 0x41 };\r
1253\r
1254 // iso14443 setup\r
1255 LED_A_ON();\r
1256 LED_B_OFF();\r
1257 LED_C_OFF();\r
1258 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1259\r
1260 // tracing\r
1261 clear_trace();\r
1262 set_tracing(true);\r
1263\r
1264 while (true){\r
1265 // wipe\r
1266 if (needWipe){\r
1267 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1268 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1269 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1270 break;\r
1271 };\r
1272\r
1273 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1274 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1275 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1276 break;\r
1277 };\r
1278\r
1279 if(mifare_classic_halt(NULL, 0)) {\r
1280 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1281 };\r
1282 };\r
1283\r
1284 // put default data\r
1285 if (needFill){\r
1286 // select commands\r
1287 ReaderTransmitBitsPar(wupC1, 7, 0, NULL);\r
1288\r
1289 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r
1290 if (!gen1b) {\r
1291\r
1292 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1293 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1294 break;\r
1295 };\r
1296\r
1297 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1298 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1299 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1300 break;\r
1301 };\r
1302 }\r
1303\r
1304 // send blocks command\r
1305 for (int blockNo = 0; blockNo < numBlocks; blockNo++) {\r
1306 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1307 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1308 break;\r
1309 };\r
1310\r
1311 // check type of block and add crc\r
1312 if (!isBlockTrailer(blockNo)){\r
1313 memcpy(d_block, block1, 16);\r
1314 } else {\r
1315 memcpy(d_block, blockK, 16);\r
1316 }\r
1317 if (blockNo == 0) {\r
1318 memcpy(d_block, block0, 16);\r
1319 }\r
1320 AppendCrc14443a(d_block, 16);\r
1321\r
1322 // send write command\r
1323 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1324 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1325 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1326 break;\r
1327 };\r
1328 }\r
1329\r
1330 // halt\r
1331 // do no issue halt command for gen1b\r
1332 if (!gen1b) {\r
1333 if (mifare_classic_halt(NULL, 0)) {\r
1334 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1335 break;\r
1336 }\r
1337 }\r
1338 }\r
1339 break;\r
1340 }\r
1341\r
1342 // send USB response\r
1343 LED_B_ON();\r
1344 cmd_send(CMD_ACK,isOK,0,0,NULL,0);\r
1345 LED_B_OFF();\r
1346\r
1347 // reset fpga\r
1348 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1349 LEDsoff();\r
1350\r
1351 return;\r
1352}\r
1353\r
1354void MifareCSetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1355\r
1356 // params\r
1357 uint8_t needWipe = arg0;\r
1358 // bit 0 - need get UID\r
1359 // bit 1 - need wupC\r
1360 // bit 2 - need HALT after sequence\r
1361 // bit 3 - need init FPGA and field before sequence\r
1362 // bit 4 - need reset FPGA and LED\r
1363 // bit 6 - gen1b backdoor type\r
1364 uint8_t workFlags = arg1;\r
1365 uint8_t blockNo = arg2;\r
1366\r
1367 // card commands\r
1368 uint8_t wupC1[] = { 0x40 };\r
1369 uint8_t wupC2[] = { 0x43 };\r
1370 uint8_t wipeC[] = { 0x41 };\r
1371\r
1372 // variables\r
1373 byte_t isOK = 0;\r
1374 uint8_t uid[10] = {0x00};\r
1375 uint8_t d_block[18] = {0x00};\r
1376 uint32_t cuid;\r
1377\r
1378 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1379 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1380\r
1381 // reset FPGA and LED\r
1382 if (workFlags & 0x08) {\r
1383 LED_A_ON();\r
1384 LED_B_OFF();\r
1385 LED_C_OFF();\r
1386 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1387\r
1388 clear_trace();\r
1389 set_tracing(true);\r
1390 }\r
1391\r
1392 while (true) {\r
1393\r
1394 // get UID from chip\r
1395 if (workFlags & 0x01) {\r
1396 if(!iso14443a_select_card(uid, NULL, &cuid, true, 0, true)) {\r
1397 if (MF_DBGLEVEL >= 1) Dbprintf("Can't select card");\r
1398 // Continue, if we set wrong UID or wrong UID checksum or some ATQA or SAK we will can't select card. But we need to write block 0 to make card work.\r
1399 //break;\r
1400 };\r
1401\r
1402 if(mifare_classic_halt(NULL, cuid)) {\r
1403 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1404 // Continue, some magic tags misbehavies and send an answer to it.\r
1405 // break;\r
1406 };\r
1407 };\r
1408\r
1409 // reset chip\r
1410 // Wipe command don't work with gen1b\r
1411 if (needWipe && !(workFlags & 0x40)){\r
1412 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1413 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1414 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1415 break;\r
1416 };\r
1417\r
1418 ReaderTransmit(wipeC, sizeof(wipeC), NULL);\r
1419 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1420 if (MF_DBGLEVEL >= 1) Dbprintf("wipeC error");\r
1421 break;\r
1422 };\r
1423\r
1424 if(mifare_classic_halt(NULL, 0)) {\r
1425 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1426 // Continue, some magic tags misbehavies and send an answer to it.\r
1427 // break;\r
1428 };\r
1429 };\r
1430\r
1431 // write block\r
1432 if (workFlags & 0x02) {\r
1433 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1434\r
1435 // gen1b magic tag : do no issue wupC2 and don't expect CARD_ACK response after SELECT_UID (after getting UID from chip in 'hf mf csetuid' command)\r
1436 if (!(workFlags & 0x40)) {\r
1437\r
1438 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1439 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1440 break;\r
1441 };\r
1442\r
1443 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1444 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1445 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1446 break;\r
1447 };\r
1448 }\r
1449 }\r
1450\r
1451 if ((mifare_sendcmd_short(NULL, 0, 0xA0, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1452 if (MF_DBGLEVEL >= 1) Dbprintf("write block send command error");\r
1453 break;\r
1454 };\r
1455\r
1456 memcpy(d_block, datain, 16);\r
1457 AppendCrc14443a(d_block, 16);\r
1458\r
1459 ReaderTransmit(d_block, sizeof(d_block), NULL);\r
1460 if ((ReaderReceive(receivedAnswer, receivedAnswerPar) != 1) || (receivedAnswer[0] != CARD_ACK)) {\r
1461 if (MF_DBGLEVEL >= 1) Dbprintf("write block send data error");\r
1462 break;\r
1463 };\r
1464\r
1465 if (workFlags & 0x04) {\r
1466 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1467 if (!(workFlags & 0x40)) {\r
1468 if (mifare_classic_halt(NULL, 0)) {\r
1469 if (MF_DBGLEVEL > 2) Dbprintf("Halt error");\r
1470 // Continue, some magic tags misbehavies and send an answer to it.\r
1471 // break;\r
1472 }\r
1473 }\r
1474 }\r
1475\r
1476 isOK = 1;\r
1477 break;\r
1478 }\r
1479\r
1480 LED_B_ON();\r
1481 cmd_send(CMD_ACK,isOK,0,0,uid,4);\r
1482 LED_B_OFF();\r
1483\r
1484 if ((workFlags & 0x10) || (!isOK)) {\r
1485 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1486 LEDsoff();\r
1487 }\r
1488}\r
1489\r
1490\r
1491void MifareCGetBlock(uint32_t arg0, uint32_t arg1, uint32_t arg2, uint8_t *datain){\r
1492\r
1493 // params\r
1494 // bit 1 - need wupC\r
1495 // bit 2 - need HALT after sequence\r
1496 // bit 3 - need init FPGA and field before sequence\r
1497 // bit 4 - need reset FPGA and LED\r
1498 // bit 5 - need to set datain instead of issuing USB reply (called via ARM for StandAloneMode14a)\r
1499 // bit 6 - gen1b backdoor type\r
1500 uint8_t workFlags = arg0;\r
1501 uint8_t blockNo = arg2;\r
1502\r
1503 // card commands\r
1504 uint8_t wupC1[] = { 0x40 };\r
1505 uint8_t wupC2[] = { 0x43 };\r
1506\r
1507 // variables\r
1508 byte_t isOK = 0;\r
1509 uint8_t data[18] = {0x00};\r
1510 uint32_t cuid = 0;\r
1511\r
1512 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1513 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1514\r
1515 if (workFlags & 0x08) {\r
1516 LED_A_ON();\r
1517 LED_B_OFF();\r
1518 LED_C_OFF();\r
1519 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1520\r
1521 clear_trace();\r
1522 set_tracing(true);\r
1523 }\r
1524\r
1525 while (true) {\r
1526 if (workFlags & 0x02) {\r
1527 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1528 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1529 if (MF_DBGLEVEL >= 1) Dbprintf("wupC1 error");\r
1530 break;\r
1531 };\r
1532 // do no issue for gen1b magic tag\r
1533 if (!(workFlags & 0x40)) {\r
1534 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1535 if(!ReaderReceive(receivedAnswer, receivedAnswerPar) || (receivedAnswer[0] != CARD_ACK)) {\r
1536 if (MF_DBGLEVEL >= 1) Dbprintf("wupC2 error");\r
1537 break;\r
1538 };\r
1539 }\r
1540 }\r
1541\r
1542 // read block\r
1543 if ((mifare_sendcmd_short(NULL, 0, 0x30, blockNo, receivedAnswer, receivedAnswerPar, NULL) != 18)) {\r
1544 if (MF_DBGLEVEL >= 1) Dbprintf("read block send command error");\r
1545 break;\r
1546 };\r
1547 memcpy(data, receivedAnswer, 18);\r
1548\r
1549 if (workFlags & 0x04) {\r
1550 // do no issue halt command for gen1b magic tag (#db# halt error. response len: 1)\r
1551 if (!(workFlags & 0x40)) {\r
1552 if (mifare_classic_halt(NULL, cuid)) {\r
1553 if (MF_DBGLEVEL > 1) Dbprintf("Halt error");\r
1554 // Continue, some magic tags misbehavies and send an answer to it.\r
1555 // break;\r
1556 }\r
1557 }\r
1558 }\r
1559\r
1560 isOK = 1;\r
1561 break;\r
1562 }\r
1563\r
1564 LED_B_ON();\r
1565 if (workFlags & 0x20) {\r
1566 if (isOK)\r
1567 memcpy(datain, data, 18);\r
1568 }\r
1569 else\r
1570 cmd_send(CMD_ACK,isOK,0,0,data,18);\r
1571 LED_B_OFF();\r
1572\r
1573 if ((workFlags & 0x10) || (!isOK)) {\r
1574 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1575 LEDsoff();\r
1576 }\r
1577}\r
1578\r
1579void MifareCIdent(){\r
1580\r
1581 // card commands\r
1582 uint8_t wupC1[] = { 0x40 };\r
1583 uint8_t wupC2[] = { 0x43 };\r
1584\r
1585 // variables\r
1586 byte_t isOK = 0;\r
1587\r
1588 uint8_t receivedAnswer[MAX_MIFARE_FRAME_SIZE];\r
1589 uint8_t receivedAnswerPar[MAX_MIFARE_PARITY_SIZE];\r
1590\r
1591 LED_A_ON();\r
1592 LED_B_OFF();\r
1593 LED_C_OFF();\r
1594 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1595\r
1596 clear_trace();\r
1597 set_tracing(true);\r
1598\r
1599 ReaderTransmitBitsPar(wupC1,7,0, NULL);\r
1600 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r
1601 isOK = 2;\r
1602\r
1603 ReaderTransmit(wupC2, sizeof(wupC2), NULL);\r
1604 if(ReaderReceive(receivedAnswer, receivedAnswerPar) && (receivedAnswer[0] == CARD_ACK)) {\r
1605 isOK = 1;\r
1606 };\r
1607 };\r
1608\r
1609 // From iceman1001: removed the if, since some magic tags misbehavies and send an answer to it.\r
1610 mifare_classic_halt(NULL, 0);\r
1611\r
1612 LED_B_ON();\r
1613 cmd_send(CMD_ACK,isOK,0,0,0,0);\r
1614 LED_B_OFF();\r
1615\r
1616 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1617 LEDsoff();\r
1618}\r
1619\r
1620//\r
1621// DESFIRE\r
1622//\r
1623\r
1624void Mifare_DES_Auth1(uint8_t arg0, uint8_t *datain){\r
1625\r
1626 byte_t dataout[11] = {0x00};\r
1627 uint8_t uid[10] = {0x00};\r
1628 uint32_t cuid;\r
1629\r
1630 iso14443a_setup(FPGA_HF_ISO14443A_READER_LISTEN);\r
1631 clear_trace();\r
1632\r
1633 int len = iso14443a_select_card(uid, NULL, &cuid, true, 0, true);\r
1634 if(!len) {\r
1635 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Can't select card");\r
1636 OnError(1);\r
1637 return;\r
1638 };\r
1639\r
1640 if(mifare_desfire_des_auth1(cuid, dataout)){\r
1641 if (MF_DBGLEVEL >= MF_DBG_ERROR) Dbprintf("Authentication part1: Fail.");\r
1642 OnError(4);\r
1643 return;\r
1644 }\r
1645\r
1646 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 1 FINISHED");\r
1647 cmd_send(CMD_ACK,1,cuid,0,dataout, sizeof(dataout));\r
1648}\r
1649\r
1650void Mifare_DES_Auth2(uint32_t arg0, uint8_t *datain){\r
1651\r
1652 uint32_t cuid = arg0;\r
1653 uint8_t key[16] = {0x00};\r
1654 byte_t isOK = 0;\r
1655 byte_t dataout[12] = {0x00};\r
1656\r
1657 memcpy(key, datain, 16);\r
1658\r
1659 isOK = mifare_desfire_des_auth2(cuid, key, dataout);\r
1660\r
1661 if( isOK) {\r
1662 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) Dbprintf("Authentication part2: Failed");\r
1663 OnError(4);\r
1664 return;\r
1665 }\r
1666\r
1667 if (MF_DBGLEVEL >= MF_DBG_EXTENDED) DbpString("AUTH 2 FINISHED");\r
1668\r
1669 cmd_send(CMD_ACK, isOK, 0, 0, dataout, sizeof(dataout));\r
1670 FpgaWriteConfWord(FPGA_MAJOR_MODE_OFF);\r
1671 LEDsoff();\r
1672}\r
1673\r
Impressum, Datenschutz